if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdCgHead') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tOrdCgHead
go
create view tOrdCgHead as
	select BillNo,SupCode,SupName, h.OrgCode as YsOrgCode,(select OrgName from t_shop where OrgCode=h.OrgCode) as YsOrgName,
		contrno as HtCode,(select contrname from t_contract where contrno = h.contrno) as HtName,
		(CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) as JzDate,
		(CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,DATEADD(DAY,1,CONVERT(datetime,Rzdate,120)) + ' ' + RzTime,120) END) as LastQrTime,
		s.addr as YsOrgAddress,s.linkMan as YsOrgLinkman,s.tel as YsOrgLkmTel,usercode as CgyCode, username as CgyName,arrivedate as ZdDate, validdate as YxDate,
		case IsNull(Rzdate,'') when '' then '0' else '9' end as DataStatus,
		'0' AS YsTimes,counts as CgCount,cost as CgHCost, netcost as CgWCost,
		(cost - netcost) as CgJTaxTotal,h.Remark,(CASE ISNULL(Qrdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Qrdate + ' ' + QrTime,120) END) as QrTime, QrCount,QrStatus
	from t_order_head h left join t_shop s on h.orgcode = s.orgcode where isnull(rzdate,'')<>''
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdCgBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tOrdCgBody
go
create view tOrdCgBody as
	select BillNo,SerialNo,PluCode,PluName,Barcode,Unit,Spec,CargoNo,
		PUnit as PackUnit,itemcnt as PackQty,PCount as PackCount,SglCount,Counts as CgCount,
		jprice as HJPrice,netjprice as WJPrice,JTaxRate,cost as CgHCost,netcost as CgWCost,(cost - netcost) as CgJTaxTotal,Remark,
		QrCount
	from t_order_body
go

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,'')<>''
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdJhBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tOrdJhBody
go
create view tOrdJhBody as
	select billno,serialNo,pluCode,pluName,barcode as pluBarcode,unit as pluUnit,spec as pluSpec,cargono as pluCargoNo,
		punit as packUnit,itemcnt as packQty,pcount as packCount,sglCount,counts as cgCount,jprice as hPrice,netjprice as wPrice,
		JTaxRate as taxRate,cost as hTotal,netcost as wTotal,(cost - netcost) as taxTotal,productdate as scDate,qualitydate as dqDate,remark
	from t_accept_body
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,'')<>''
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdThBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tOrdThBody
go
create view tOrdThBody as
	select BillNo,SerialNo,0 as PluID,PluCode,PluName,BarCode,Spec,Unit,CargoNo,
		jprice as HJPrice,netjprice as WJPrice,JTaxRate,punit as PackUnit,itemcnt as PackQty,punit as PackCount,
		SglCount,counts as ThCount,cost as HCost,netcost as WCost,(cost - netcost)  as JTaxTotal,
		(select valuename from t_enum_value where enumcode = '100001' and valuecode = t_return_body.kindcd) as ThReason,Remark
	from t_return_body
go

if exists (select * from sysobjects where id = OBJECT_ID(N'scm_sup_jjtz_head') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view scm_sup_jjtz_head
go
create view scm_sup_jjtz_head as
	select billNo,supCode,supName,
		contrno as cntCode,(select contrname from t_contract where contrno = h.contrno) as cntName,
		case tzType when '2' then '0' when '0' then '2' when '1' then '1' end as tzType,tzCount,
		(CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) as tzDate,
		cjcost as hTotal,netcjcost as wTotal,(cjcost-netcjcost) as taxTotal,remark as tzReason
	from t_adjbuyprice_head h where isnull(rzdate,'')<>''
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tOrdTjjBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tOrdTjjBody
go
create view tOrdTjjBody as
	select billNo,serialNo,pluCode,pluName,barcode as pluBarcode,unit as pluUnit,spec as pluSpec,cargono as pluCargoNo,
		tzCount,jprice as oldhPrice,netjprice as oldwPrice,newjprice as hPrice,newnetjprice as wPrice,cjcost as tzTotal,remark
	from t_adjbuyprice_body
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tAcpJsHead') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tAcpJsHead
go
create view tAcpJsHead as
	select BillNo,SupCode,SupName,SupCode as EtpCode,SupName as EtpName, 
		contrno as HtCode, (select contrname from t_contract where contrno = h.contrno) as HtName,
		'0' as JsCode,enddate as EndJsDate, QrStatus,QrDate, QrTime,'' as QrOpinion,
		(CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) as JzDate,
		(dztotal-bczrtotal) as YfTotal,total as HYfTotal,(total-Taxamt) as WYfTotal,Taxamt as JTaxTotal,feetotal as FyTotal,
		(-1*bczrtotal) as TzTotal, 0 as ZkAsnTotal,0 as LastYkTotal,0 as BcYkTotal,Remark,Rzdate,chkstatus
	from t_paysoa_head h where 1=1
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tAcpJsRcpBody') and OBJECTPROPERTY(id, N'IsView') = 1)
  drop view tAcpJsRcpBody
go
create view tAcpJsRcpBody as
  select b.BillNo,b.orgcode as YwOrgCode,(select orgname from t_shop where orgcode = b.orgcode) as YwOrgName,
    YwBillNo,billtype as YwType,
    case  billtype when  '0' then '验收单' when '1' then '退货单' when '2' then '进价调整单' when '3' then '直送单'  when '4' then '直送退货' when '5' then '直送进价调整单' when '6' then '配送单' when '7' then '配送退货单' when '8' then '配送进价调整单' end  as YwTypeName, 
    (CASE ISNULL(Rzdate,'') WHEN '' THEN NULL ELSE CONVERT(datetime,Rzdate + ' ' + RzTime,120) END) as JzDate, 
    case when  billtype = '0' or billtype = '3' then (select cost from t_accept_head where billno = b.YwBillNo) 
      when  billtype = '1' or billtype = '4' then (select cost from t_return_head where billno = b.YwBillNo)
      when  billtype = '2' or billtype = '5' or billtype = '8' then (select cjcost from t_adjbuyprice_head where billno = b.YwBillNo)
      when  billtype = '6' or billtype = '7' then (select zpamount from t_dist_head where billno = b.YwBillNo)
      end as HJTotal,
    case when  billtype = '0' or billtype = '3' then (select netcost from t_accept_head where billno = b.YwBillNo) 
      when  billtype = '1' or billtype = '4' then (select netcost from t_return_head where billno = b.YwBillNo)
      when  billtype = '2' or billtype = '5' or billtype = '8' then (select netcjcost from t_adjbuyprice_head where billno = b.YwBillNo)
      when  billtype = '6' or billtype = '7' then (select zpnetamount from t_dist_head where billno = b.YwBillNo)
      end as WJTotal,
    case when  billtype = '0' or billtype = '3' then (select cost-netcost from t_accept_head where billno = b.YwBillNo) 
      when  billtype = '1' or billtype = '4' then (select cost-netcost from t_return_head where billno = b.YwBillNo)
      when  billtype = '2' or billtype = '5' or billtype = '8' then (select netcost-netcjcost from t_adjbuyprice_head where billno = b.YwBillNo)
      when  billtype = '6' or billtype = '7' then (select zpamount-zpnetamount from t_dist_head where billno = b.YwBillNo)
      end as JTaxTotal
  from t_paysoa_body b, t_paysoa_head h where h.billno = b.billno
go

/**if exists (select * from sysobjects where id = OBJECT_ID(N'tAcpJsKxBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tAcpJsKxBody
go
create view tAcpJsKxBody as
	select BillNo,b.orgcode as YwOrgCode,(select orgname from t_shop where orgcode = b.orgcode) as YwOrgName,
		itemcode as KxCode, (select feename from t_fee where feecode = b.itemcode) as KxName,
		case basetype when '00' then '1' else '0' end as AsnType,basetype as JsBase,
		(select valuename from t_enum_value where enumcode='100013' and valuecode=b.basetype ) as JsBaseName,
		RateBase,IsInvoice,feetotal as KxTotal,tendcode as ZfCode,(select tendname from t_tend where tendcode=b.tendcode) as ZfName,Remark
	from  t_paysoa_clause b
go*/
if exists (select * from sysobjects where id = OBJECT_ID(N'tAcpJsKxBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tAcpJsKxBody
go
create view tAcpJsKxBody as
	select b.BillNo,c.supcode,c.contrno,'*' as YwOrgCode,'' as YwOrgName,b.feecode as KxCode,b.feename as KxName,
		case a.calctype when '0' then '1' else '0' end as AsnType,a.basetype as JsBase,
		(CASE a.basetype WHEN '0' THEN '进货金额' WHEN '1' THEN '销售金额' ELSE '未知' END) AS JsBaseName,
		'0' AS RateBase,'0' AS IsInvoice,b.feetotal as KxTotal,'' as ZfCode,'' as ZfName,b.Remark AS Remark
	from  t_fee_clause a, t_paysoa_fee b, t_paysoa_head c WHERE b.billno=c.billno
		  and a.supcode=c.supcode AND a.FeeNo=b.FeeNo AND a.FeeCode=b.FeeCode
go

if exists (select * from sysobjects where id = OBJECT_ID(N'tAcpJsTaxBody') and OBJECTPROPERTY(id, N'IsView') = 1)
	drop view tAcpJsTaxBody
go
create view tAcpJsTaxBody as
	select b.BillNo,JTaxRate,cost as HJTotal,netcost as WJTotal,b.taxamt as JTaxTotal,'0' as JTaxCalType, '0' as IsInvoice,0 AS YkHJTotal,
	contrno as cntCode, (select contrname from t_contract where contrno = h.contrno) as cntName
	from t_paysoa_tax b left join t_paysoa_head h on h.billno = b.billno
go
