# t_view_update

执行:
ALTER  VIEW dbo.v_op_card_seae
AS
SELECT dbo.t_op_card.CA_ID, dbo.t_op_card.单证名称, dbo.t_op_card.单证号码, 
      dbo.t_op_card.接单日期 as 单证接单日期, dbo.t_op_card.送单日期, dbo.t_op_card.是否回单, 
      dbo.t_op_card.回单日期,dbo.t_op_card.是否还单,dbo.t_op_card.还单日期,dbo.t_op_card.备注 as 单证备注,dbo.t_op_seae.* 
FROM dbo.t_op_card INNER JOIN
      dbo.t_op_seae ON dbo.t_op_card.编号 = dbo.t_op_seae.编号


执行:
alter   VIEW dbo.v_dui_zhfee
AS
SELECT t_ch_dui_detail.对帐编号,t_ch_fee.编号,t_ch_fee.客户名称, t_ch_fee.费用名称,t_ch_fee.费用英文名称
, t_ch_fee.标准,t_ch_fee.汇率, t_ch_fee.单价, t_ch_fee.数量, t_ch_fee.金额
, t_ch_fee.币别, t_ch_fee.结算金额, t_ch_fee.类型,t_ch_fee.不含税价,t_ch_fee.税率,t_ch_fee.税额,t_ch_fee.不含税金额,t_ch_fee.财务税率,t_ch_fee.财务税额,t_ch_fee.财务不含税价,
      t_ch_fee.金额 - t_ch_fee.结算金额 AS 未结金额, t_ch_fee.CH_ID, t_ch_fee.顺序
FROM dbo.t_ch_dui_detail
left join t_ch_fee on (t_ch_fee.CH_ID=t_ch_dui_detail.ch_id)


执行:
ALTER  VIEW dbo.v_dui_aire
AS
SELECT dbo.t_ch_dui_bill.对帐编号, dbo.t_op_aire.*, 
      dbo.v_dui_fee_sum.RMB金额, dbo.v_dui_fee_sum.RMB结算, 
      dbo.v_dui_fee_sum.RMB未结, dbo.v_dui_fee_sum.USD金额, 
      dbo.v_dui_fee_sum.USD结算, dbo.v_dui_fee_sum.USD未结
FROM dbo.t_ch_dui_bill INNER JOIN
      dbo.t_op_aire ON dbo.t_ch_dui_bill.编号 = dbo.t_op_aire.编号 INNER JOIN
      dbo.v_dui_fee_sum ON dbo.t_ch_dui_bill.对帐编号 = dbo.v_dui_fee_sum.对帐编号
AND dbo.t_ch_dui_bill.编号 = dbo.v_dui_fee_sum.编号


执行:
ALTER  VIEW dbo.v_dui_airi
AS
SELECT dbo.t_ch_dui_bill.对帐编号, dbo.t_op_airi.*, 
      dbo.v_dui_fee_sum.RMB金额, dbo.v_dui_fee_sum.RMB结算, 
      dbo.v_dui_fee_sum.RMB未结, dbo.v_dui_fee_sum.USD金额, 
      dbo.v_dui_fee_sum.USD结算, dbo.v_dui_fee_sum.USD未结
FROM dbo.t_ch_dui_bill INNER JOIN
      dbo.t_op_airi ON dbo.t_ch_dui_bill.编号 = dbo.t_op_airi.编号 INNER JOIN
      dbo.v_dui_fee_sum ON dbo.t_ch_dui_bill.对帐编号 = dbo.v_dui_fee_sum.对帐编号
AND dbo.t_ch_dui_bill.编号 = dbo.v_dui_fee_sum.编号


执行:
ALTER    VIEW dbo.v_dui_allfee_sum
AS
SELECT 对帐编号, 是否对帐,编号
       ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END) AS 应收RMB金额
       ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) 
      AS 应收RMB结算, (SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END)
      - SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) ) AS 应收RMB未结, 
      SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) AS 应收USD金额, 
      SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END) AS 应收USD结算, 
      (SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) 
      - SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END)) AS 应收USD未结,
       SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END) AS 应付RMB金额
      ,SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) AS 应付RMB结算
     , (SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END)
      - SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) ) AS 应付RMB未结, 
      SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) AS 应付USD金额, 
      SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END) AS 应付USD结算, 
      (SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) 
      - SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END)) AS 应付USD未结
      ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 不含税金额*汇率 END END) AS 不含税应收RMB金额
      ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 不含税金额 ELSE 0 END END) AS 不含税应收USD金额
      ,SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 不含税金额*汇率 END END) AS 不含税应付RMB金额
      ,SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 不含税金额 ELSE 0 END END) AS 不含税应付USD金额
      ,SUM(CASE 类型 WHEN '收' THEN 金额*汇率 else 0  END) AS 应收合计
      ,SUM(CASE 类型 WHEN '收' THEN (金额-结算金额)*汇率 else 0  END) AS 未收合计
      ,SUM(CASE 类型 WHEN '付' THEN 金额*汇率 else 0  END) AS 应付合计
      ,SUM(CASE 类型 WHEN '付' THEN (金额-结算金额)*汇率 else 0  END) AS 未付合计
FROM t_ch_fee
GROUP BY 对帐编号, 是否对帐,编号
HAVING (是否对帐 = 1)
UNION
SELECT 对帐编号, 1 AS 是否对帐,编号, SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END) 
      AS 应收RMB金额, SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) 
      AS 应收RMB结算, (SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END)
      - SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) ) AS 应收RMB未结, 
      SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) AS 应收USD金额, 
      SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END) AS 应收USD结算, 
      (SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) 
      - SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END)) AS 应收USD未结,
     SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END) 
      AS 应付RMB金额, SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) 
      AS 应付RMB结算, (SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END END)
      - SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END END) ) AS 应付RMB未结, 
      SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) AS 应付USD金额, 
      SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END) AS 应付USD结算, 
      (SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END END) 
      - SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END END)) AS 应付USD未结
      ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 不含税金额*汇率 END END) AS 不含税应收RMB金额
      ,SUM(CASE 类型 WHEN '付' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 不含税金额 ELSE 0 END END) AS 不含税应收USD金额
      ,SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 0 ELSE 不含税金额*汇率 END END) AS 不含税应付RMB金额
      ,SUM(CASE 类型 WHEN '收' THEN 0 ELSE CASE 币别 WHEN 'USD' THEN 不含税金额 ELSE 0 END END) AS 不含税应付USD金额
      ,SUM(CASE 类型 WHEN '收' THEN 金额*汇率 else 0  END) AS 应收合计
      ,SUM(CASE 类型 WHEN '收' THEN (金额-结算金额)*汇率 else 0  END) AS 未收合计
      ,SUM(CASE 类型 WHEN '付' THEN 金额*汇率 else 0  END) AS 应付合计
      ,SUM(CASE 类型 WHEN '付' THEN (金额-结算金额)*汇率 else 0  END) AS 未付合计
FROM v_dui_zhfee
GROUP BY 对帐编号,编号


执行:
alter   VIEW dbo.v_dui_bscard
AS
SELECT dbo.t_ch_dui_bill.对帐编号, dbo.v_op_bscard_all.*, 
      dbo.v_dui_allfee_sum.应收RMB金额, dbo.v_dui_allfee_sum.应收RMB结算, 
      dbo.v_dui_allfee_sum.应收RMB未结, dbo.v_dui_allfee_sum.应收USD金额, 
      dbo.v_dui_allfee_sum.应收USD结算, dbo.v_dui_allfee_sum.应收USD未结,
      dbo.v_dui_allfee_sum.应付RMB金额, dbo.v_dui_allfee_sum.应付RMB结算, 
      dbo.v_dui_allfee_sum.应付RMB未结, dbo.v_dui_allfee_sum.应付USD金额, 
      dbo.v_dui_allfee_sum.应付USD结算, dbo.v_dui_allfee_sum.应付USD未结,
      dbo.v_dui_allfee_sum.不含税应收RMB金额,dbo.v_dui_allfee_sum.不含税应收USD金额
      ,dbo.v_dui_allfee_sum.不含税应付RMB金额,dbo.v_dui_allfee_sum.不含税应付USD金额
       
      ,dbo.v_dui_allfee_sum.应收合计,dbo.v_dui_allfee_sum.未收合计
      ,dbo.v_dui_allfee_sum.应付合计,dbo.v_dui_allfee_sum.未付合计

 
FROM dbo.t_ch_dui_bill INNER JOIN
      dbo.v_op_bscard_all ON dbo.t_ch_dui_bill.编号 = dbo.v_op_bscard_all.编号 INNER JOIN
      dbo.v_dui_allfee_sum ON dbo.t_ch_dui_bill.对帐编号 = dbo.v_dui_allfee_sum.对帐编号
AND dbo.t_ch_dui_bill.编号 = dbo.v_dui_allfee_sum.编号


执行:
ALTER  VIEW dbo.v_dui_fee_all
AS
SELECT 对帐编号, 是否对帐, 
      SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 金额 * 汇率 END) AS RMB金额, 
      SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额 * 汇率 END) AS RMB结算, 
      (SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 金额 * 汇率 END) 
      - SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额 * 汇率 END)) AS RMB未结, 
      SUM(CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END) AS USD金额, 
      SUM(CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END) AS USD结算, 
      (SUM(CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END) 
      - SUM(CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END)) AS USD未结
FROM t_ch_fee
GROUP BY 对帐编号, 是否对帐
HAVING (是否对帐 = 1)


执行:
ALTER      VIEW dbo.v_dui_fee_sum
AS
SELECT 对帐编号, 是否对帐,编号, SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END) 
      AS RMB金额, SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END) 
      AS RMB结算, (SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 金额*汇率 END)
      - SUM(CASE 币别 WHEN 'USD' THEN 0 ELSE 结算金额*汇率 END) ) AS RMB未结, 
      SUM(CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END) AS USD金额, 
      SUM(CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END) AS USD结算, 
      (SUM(CASE 币别 WHEN 'USD' THEN 金额 ELSE 0 END) 
      - SUM(CASE 币别 WHEN 'USD' THEN 结算金额 ELSE 0 END)) AS USD未结
FROM t_ch_fee
GROUP BY 对帐编号, 是否对帐,编号
HAVING (是否对帐 = 1)


执行:
ALTER  VIEW dbo.v_dui_seae
AS
SELECT dbo.t_ch_dui_bill.对帐编号, dbo.t_op_seae.*, 
      dbo.v_dui_fee_sum.RMB金额, dbo.v_dui_fee_sum.RMB结算, 
      dbo.v_dui_fee_sum.RMB未结, dbo.v_dui_fee_sum.USD金额, 
      dbo.v_dui_fee_sum.USD结算, dbo.v_dui_fee_sum.USD未结
FROM dbo.t_ch_dui_bill INNER JOIN
      dbo.t_op_seae ON dbo.t_ch_dui_bill.编号 = dbo.t_op_seae.编号 INNER JOIN
      dbo.v_dui_fee_sum ON dbo.t_ch_dui_bill.对帐编号 = dbo.v_dui_fee_sum.对帐编号
AND dbo.t_ch_dui_bill.编号 = dbo.v_dui_fee_sum.编号


执行:
ALTER  VIEW dbo.v_dui_seai
AS
SELECT dbo.t_ch_dui_bill.对帐编号, dbo.t_op_seai.*, 
      dbo.v_dui_fee_sum.RMB金额, dbo.v_dui_fee_sum.RMB结算, 
      dbo.v_dui_fee_sum.RMB未结, dbo.v_dui_fee_sum.USD金额, 
      dbo.v_dui_fee_sum.USD结算, dbo.v_dui_fee_sum.USD未结
FROM dbo.t_ch_dui_bill INNER JOIN
      dbo.t_op_seai ON dbo.t_ch_dui_bill.编号 = dbo.t_op_seai.编号 INNER JOIN
      dbo.v_dui_fee_sum ON dbo.t_ch_dui_bill.对帐编号 = dbo.v_dui_fee_sum.对帐编号
AND dbo.t_ch_dui_bill.编号 = dbo.v_dui_fee_sum.编号


执行:
alter   VIEW dbo.v_dui_fee
AS
SELECT 编号, 对帐编号, 客户名称,费用英文名称, 费用名称, 标准,类型,汇率, 单价, 数量, 金额, 币别, 结算金额,不含税价,税率,税额,不含税金额,财务税率,财务税额,财务不含税价, 
      金额 - 结算金额 AS 未结金额, CH_ID, 顺序
FROM dbo.t_ch_fee
WHERE (是否对帐 = 1)
UNION
SELECT 编号, 对帐编号, 客户名称,费用英文名称, 费用名称, 标准,类型,汇率, 单价, 数量, 金额, 币别, 结算金额,不含税价,税率,税额,不含税金额,财务税率,财务税额,财务不含税价, 
      金额 - 结算金额 AS 未结金额, CH_ID, 顺序
FROM v_dui_zhfee


执行:
ALTER  VIEW dbo.v_ch_dui
AS
SELECT t_ch_dui.*,v_dui_fee.CH_ID
FROM dbo.t_ch_dui
left join v_dui_fee on (t_ch_dui.对帐编号=v_dui_fee.对帐编号)


执行:
ALTER   VIEW dbo.v_op_fangdan_bscard
AS
SELECT dbo.t_op_fangdan.blid, dbo.t_op_fangdan.放单状态, dbo.t_op_fangdan.申请日期, 
      dbo.t_op_fangdan.申请人,t_op_fangdan.财务审核状态, dbo.t_op_fangdan.财务审核日期, 
      dbo.t_op_fangdan.财务审核人,dbo.t_op_fangdan.经理审核状态,dbo.t_op_fangdan.经理审核日期,dbo.t_op_fangdan.经理审核人,
      dbo.v_op_bscard.* 
FROM dbo.t_op_fangdan INNER JOIN
      dbo.v_op_bscard ON dbo.t_op_fangdan.编号 = dbo.v_op_bscard.编号


