if exists (select * from sysobjects where id = OBJECT_ID(N'scm_bus_jh_head') and OBJECTPROPERTY(id, N'IsView') = 1)
  drop view scm_bus_jh_head
go

create view scm_bus_jh_head as
  select billNo,(CASE ISNULL(LrDate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,LrDate + ' ' + LrTime,120) END) as gmtCreate,
    username as createUserName,
    (CASE ISNULL(LrDate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,LrDate + ' ' + LrTime,120) END) as gmtModified,
    username as updateUserName,
    supCode,supName,contrno as HtCode,(select contrname from t_contract where contrno = t_accept_head.contrno) as HtName,
    OrderNo as refBillNo,orgcode as shOrgCode,(select orgname from t_shop where orgcode = t_accept_head.orgcode) as shOrgName,counts as jhCount,
    (CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) as JzDate,
    cost as hTotal,netcost as wTotal,(cost - netcost) as taxTotal,remark
  from t_accept_head where isnull(rzdate,'')<>'' and orgcode<>'C009'
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdThHead') and OBJECTPROPERTY(id, N'IsView') = 1)
  drop view tOrdThHead
go
create view tOrdThHead as
  select BillNo,
    (CASE ISNULL(LrDate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,LrDate + ' ' + LrTime,120) END) as LrDate,
    0 as UserID,UserCode,UserName,
    (CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) AS JzDate,
    0 as JzrID,zdrcode as JzrCode,zdrname as JzrName,
    OrgCode,(select orgname from t_shop where orgcode =h.orgcode ) as OrgName,SupCode,SupName,
    0 as CntID,contrno as HtCode,(select contrname from t_contract where contrno = h.contrno) as HtName,
    cost as HCost,netcost as WCost,(cost - netcost) as JTaxTotal,
    (select valuename from t_enum_value where enumcode = '100001' and valuecode = h.kindcd) as ThReason,Remark
  from t_return_head h where isnull(rzdate,'')<>'' and orgcode<>'C009'
go