strSql.Append(",EMAIL,GOODSNAME,(Select GOODNAME from code_goods_inv where GID=ch_fee_invoiceapplication_detail.GOODSNAME) AS GOODSNAMEREF");
strSql.Append(",(SELECT TOP 1 INVOICENO FROM ch_fee_invoice WHERE BILLNO = ch_fee_invoiceapplication_detail.INVBILLNO) INVOICENO,INVOICELINE ");
strSql.Append(",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=80003 and EnumValueID=ch_fee_invoiceapplication_detail.INVOICELINE) as INVOICELINEREF");
strSql.Append(" FROM ch_fee_invoiceapplication_detail ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" LEFT JOIN v_op_gain_cust_sum g ON (g.BSNO=B.BSNO and g.CUSTOMERNAME=f.CUSTOMERNAME) ");
strSql.Append(" left join ch_dui_detail d on (d.CH_ID=f.GID) WHERE D.DUINO='"+duino+"' AND ISNULL(D.INVNO,'')='' ");
strSql.Append(" and (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" LEFT JOIN v_op_gain_cust_sum g ON (g.BSNO=B.BSNO and g.CUSTOMERNAME=f.CUSTOMERNAME) ");
strSql.Append(" where (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" where f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" left join ch_dui_detail d on (d.CH_ID=f.GID) WHERE D.DUINO='"+duino+"' AND ISNULL(D.INVNO,'')='' ");
strSql.Append(" and (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and f.FEESTATUS IN (0,8,9) and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" and f.ISINVOICE<>1 and f.FEESTATUS IN (0,8,9) and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append("SELECT COUNT(*) FROM (select b.BSNO,f.CUSTOMERNAME ");
strSql.Append(" FROM CH_FEE f ");
strSql.Append(" INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) ");
strSql.Append(" where (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and f.FEESTATUS IN (0,8,9) and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" where f.ISINVOICE<>1 and f.FEESTATUS IN (0,8,9) and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(",(select ShowName from [user] where GID=f.EnteroPerator) as OpName");
strSql.Append(",f.INPUTMODE,f.UNIT,f.UNITPRICE,f.QUANTITY,d.DEBITAMOUNT from ch_fee f ");
strSql.Append(" left join ch_dui_detail d on (d.CH_ID=f.GID) WHERE D.DUINO='"+duino+"' AND ISNULL(D.INVNO,'')='' ");
strSql.Append(" and (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(",f.feeName,f.customerName,f.amount,f.amount-isnull(f.INVOICE,0)-isnull(f.ORDERINVOICE,0)+isnull(f.ORDERINVSETTLEMENT,0) as balamount,f.currency,f.exChangerate,f.AccTaxRate,f.remark");
strSql.Append(",(select ShowName from [user] where GID=f.EnteroPerator) as OpName");
strSql.Append(",f.INPUTMODE,f.UNIT,f.UNITPRICE,f.QUANTITY,0 DEBITAMOUNT from ch_fee f ");
strSql.Append(" where (f.AMOUNT-f.ORDERAMOUNT)<>0 and f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
strSql.Append(" where f.ISINVOICE<>1 and (f.AMOUNT<>(ISNULL(f.INVOICE,0)+ISNULL(f.ORDERINVOICE,0)-ISNULL(f.ORDERINVSETTLEMENT,0))) and ISNULL(f.INVLINKGID,'')='' ");
@"UPDATE CH_FEE SET ORDERAMOUNT=ISNULL(ORDERAMOUNT,0)+@ORDERAMOUNT,ORDERINVOICE=isnull(ORDERINVOICE,0)+@ORDERAMOUNT WHERE GID=@GID AND ((AMOUNT>=0 AND ((AMOUNT-SETTLEMENT)-@ORDERAMOUNT2)>=0)
@"UPDATE CH_FEE SET ORDERAMOUNT=ISNULL(ORDERAMOUNT,0)+@ORDERAMOUNT,ORDERINVOICE=isnull(ORDERINVOICE,0)+@ORDERAMOUNT WHERE GID=@GID AND ((AMOUNT>=0 AND ((AMOUNT-SETTLEMENT)-@ORDERAMOUNT2)>=0)
varcmdupdate=db.GetSqlStringCommand("update ch_fee set ORDERAMOUNT=isnull((select sum(Amount) from ch_fee_do where GID<>@FEEDOID and feeid=ch_fee.gid and CATEGORY in(2,5) ),0) where GID=@GID");
if(headdata.SETTLEMODE==3){
cmdupdate=db.GetSqlStringCommand("update ch_fee set ORDERAMOUNT=isnull((select sum(Amount) from ch_fee_do where GID<>@FEEDOID and feeid=ch_fee.gid and CATEGORY in(2,5) ),0),ORDERINVOICE=ORDERINVOICE-@ORDERINVOICE where GID=@GID");
}
varcmddelete=db.GetSqlStringCommand("delete from ch_fee_do where GID=@FEEDOID");