using System ;
using System.Data ;
using System.Data.Common ;
using System.Collections ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsChFeeAutoDui ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.EntityDA ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
using System.Data.SqlClient ;
using DSWeb.DataAccess ;
using System.IO ;
using HcUtility.Core ;
using DSWeb.Areas.CommMng.DAL ;
using System.Web ;
using DSWeb.TruckMng.Comm.Cookie ;
using DSWeb.MvcShipping.Controllers ;
using DSWeb.TruckMng.Helper.Repository ;
using DSWeb.TruckMng.Helper ;
using DSWeb.MvcShipping.DAL.Chfee_AuditDAL ;
using DSWeb.Areas.Account.Models.MsOpBill ;
namespace DSWeb.MvcShipping.DAL.MsChFeeAutoDuiDAL
{
public class MsChFeeAutoDuiDAL
{
#region 查询
static public List < MsChFeeAutoDuimb > GetDataList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( " select CH_FEE_AutoDui.GID,CUSTOMERNAME,FEETYPE,DUISTATUS,dbo.trimdate(STARTDATE) STARTDATE " ) ;
strSql . Append ( " ,dbo.trimdate(ENDDATE) ENDDATE,OP,dbo.trimtime(OPDATE) OPDATE,CH_FEE_AutoDui.REMARK,CH_FEE_AutoDui.REMARK2,STATUS,'' STATUS_REF,convert(bigint ,TimeMark) as TimeMark " ) ;
strSql . Append ( " ,isnull(HAVEDEBIT,0) HAVEDEBIT,DUINO,FEEDC,CURR,FSTATUS,DUITYPE,FEEDUITYPE,FEEDUITYPECONDIT,SALECORP,D.RMBAMOUNT,D.USDAMOUNT,D.L_RMBAMOUNT,D.L_USDAMOUNT " ) ;
strSql . Append ( " from CH_FEE_AutoDui " ) ;
strSql . Append ( " left join (select BSNO,SUM(RMBAMOUNT) RMBAMOUNT,SUM(USDAMOUNT) USDAMOUNT,SUM(L_RMBAMOUNT) L_RMBAMOUNT,SUM(L_USDAMOUNT) L_USDAMOUNT FROM CH_FEE_AutoDuiDetail GROUP BY BSNO) D ON (D.BSNO=GID) " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
else
{
}
/ /
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by OPDATE desc" ) ;
}
return SetData ( strSql ) ;
}
static public MsChFeeAutoDuimb GetData ( string condition )
{
var _t = "" ;
var list = GetDataList ( condition , _t ) ;
if ( list . Count > 0 )
return list [ 0 ] ;
return new MsChFeeAutoDuimb ( ) ;
}
private static List < MsChFeeAutoDuimb > SetData ( StringBuilder strSql )
{
var headList = new List < MsChFeeAutoDuimb > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
MsChFeeAutoDuimb data = new MsChFeeAutoDuimb ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . CUSTOMERNAME = Convert . ToString ( reader [ "CUSTOMERNAME" ] ) ;
data . FEETYPE = Convert . ToString ( reader [ "FEETYPE" ] ) ; //1收 2付 3全部
data . DUISTATUS = Convert . ToString ( reader [ "DUISTATUS" ] ) ;
data . STARTDATE = Convert . ToString ( reader [ "STARTDATE" ] ) ;
data . ENDDATE = Convert . ToString ( reader [ "ENDDATE" ] ) ;
data . OP = Convert . ToString ( reader [ "OP" ] ) ;
data . OPDATE = Convert . ToString ( reader [ "OPDATE" ] ) ;
data . REMARK = Convert . ToString ( reader [ "REMARK" ] ) ;
data . REMARK2 = Convert . ToString ( reader [ "REMARK2" ] ) ;
data . STATUS = Convert . ToString ( reader [ "STATUS" ] ) ; //暂时用来存放对账编号
data . STATUS_REF = "" ; //暂时无用
data . TimeMark = Convert . ToString ( reader [ "TimeMark" ] ) ;
data . HAVEDEBIT = Convert . ToString ( reader [ "HAVEDEBIT" ] ) ;
data . DUINO = Convert . ToString ( reader [ "DUINO" ] ) ;
data . FEEDC = Convert . ToString ( reader [ "FEEDC" ] ) ;
data . CURR = Convert . ToString ( reader [ "CURR" ] ) ;
data . FSTATUS = Convert . ToString ( reader [ "FSTATUS" ] ) ;
data . DUITYPE = Convert . ToString ( reader [ "DUITYPE" ] ) ; //1收 2付 3全部
if ( data . DUITYPE = = "" ) data . DUITYPE = "1" ;
data . FEEDUITYPE = Convert . ToString ( reader [ "FEEDUITYPE" ] ) ;
data . FEEDUITYPECONDIT = Convert . ToString ( reader [ "FEEDUITYPECONDIT" ] ) ;
data . SALECORP = Convert . ToString ( reader [ "SALECORP" ] ) ;
data . RMBAMOUNT = Convert . ToString ( reader [ "RMBAMOUNT" ] ) ;
data . USDAMOUNT = Convert . ToString ( reader [ "USDAMOUNT" ] ) ;
data . L_RMBAMOUNT = Convert . ToString ( reader [ "L_RMBAMOUNT" ] ) ;
data . L_USDAMOUNT = Convert . ToString ( reader [ "L_USDAMOUNT" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 查询对账明细
static public List < CH_FEE_AutoDuiDetailmb > GetBodyList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "select dl.*,b.ETD,B.CUSTOMERNAME,B.OP,B.CARRIER,B.TEU,B.SALE,B.CUSTNO from " ) ;
// strSql.Append("(select GID,BSNO,(select top 1 BSNO from v_op_bill where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口')) B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2 ");
strSql . Append ( "(select GID,BSNO,B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2,B_AMOUNT " ) ;
strSql . Append ( " from CH_FEE_AutoDuiDetail D " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
else
{
}
strSql . Append ( ") as dl left join v_op_bs B ON (B.BSNO=dl.B_BSNO) " ) ;
/ /
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by ISOK,ETD,MBLNO" ) ;
}
return SetBody ( strSql ) ;
}
static public string GetBodyListStr ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "select dl.*,b.ETD,B.CUSTOMERNAME,B.OP,B.CARRIER,B.TEU,B.SALE,B.CUSTNO from " ) ;
// strSql.Append("(select GID,BSNO,(select top 1 BSNO from v_op_bill where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口')) B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2 ");
strSql . Append ( "(select GID,BSNO,B_BSNO,MBLNO,B_MBLNO,RMBAMOUNT,USDAMOUNT,L_RMBAMOUNT,L_USDAMOUNT,ISOK,REMARK,REMARK2,B_AMOUNT " ) ;
strSql . Append ( ",isnull(RMBAMOUNT,0)-isnull(L_RMBAMOUNT,0) Y_RMBAMOUNT,isnull(USDAMOUNT,0)-isnull(L_USDAMOUNT,0) Y_USDAMOUNT " ) ;
strSql . Append ( " from CH_FEE_AutoDuiDetail D " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
else
{
}
strSql . Append ( ") as dl left join v_op_bs B ON (B.BSNO=dl.B_BSNO) " ) ;
/ /
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by ISOK,ETD,MBLNO" ) ;
}
return strSql . ToString ( ) ;
}
static public List < CH_FEE_AutoDuiDetailmb > GetBodyJoinList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "select D.GID,D.BSNO,B.BSNO B_BSNO,D.MBLNO,d.B_MBLNO,dbo.trimdate(B.ETD) ETD,B.CUSTOMERNAME,B.OP,B.CARRIER,B.TEU,B.SALE,B.CUSTNO,D.RMBAMOUNT,D.USDAMOUNT,D.L_RMBAMOUNT,D.L_USDAMOUNT,D.ISOK,D.REMARK,D.REMARK2,D.B_AMOUNT " ) ;
strSql . Append ( " from CH_FEE_AutoDuiDetail D left join V_op_bill B on REPLACE(REPLACE(B.MBLNO,'-',''),' ','')=REPLACE(REPLACE(D.MBLNO,'-',''),' ','') " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
else
{
}
/ /
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by ISOK,ETD,MBLNO" ) ;
}
return SetBody ( strSql ) ;
}
static public List < CH_FEE_AutoDuiDetailmb > GetLocalList ( string CUSTOMERNAME , string AutoDui_GID , string FEETYPE , string HAVEDEBIT , string FEEDC , string CURR , string FSTATUS , string FEEDUITYPE , string FEEDUITYPECONDIT , string SALECORP )
{
var strSql = new StringBuilder ( ) ;
if ( FEETYPE = = "2" )
{
strSql . Append ( " SELECT '' GID,'" + AutoDui_GID + "' BSNO,B.MBLNO,B.MBLNO B_MBLNO,'' B_BSNO,'' ETD,'' CUSTOMERNAME,'' OP,B.CARRIER,MAX(B.TEU) TEU,'' SALE,'' CUSTNO,'0' RMBAMOUNT,'0' USDAMOUNT,'0' ISOK,'' REMARK,'' REMARK2, " ) ;
if ( FSTATUS = = "3" | | FSTATUS = = "4" )
{
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT " ) ;
}
else
{
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT " ) ;
}
strSql . Append ( ",isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0) AS B_AMOUNT " ) ;
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.SETTLEMENT and F.CUSTOMERNAME='" + CUSTOMERNAME + "' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (fd.MBLNO=B.MBLNO OR fd.MBLNO=B.HBLNO) and fd.BSNO='" + AutoDui_GID + "') ");
strSql . Append ( " where F.AMOUNT<>F.SETTLEMENT and F.CUSTOMERNAME='" + CUSTOMERNAME + "' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (REPLACE(REPLACE(fd.MBLNO,'-',''),' ','')=REPLACE(REPLACE(B.MBLNO,'-',''),' ','') OR fd.MBLNO=B.HBLNO) and fd.BSNO='" + AutoDui_GID + "') " ) ;
if ( HAVEDEBIT = = "0" ) {
strSql . Append ( " and isnull(f.ISDEBIT,0)=0 " ) ;
}
if ( ( FEEDC ! = "0" ) & & ( FEEDC . Trim ( ) ! = "" ) ) {
strSql . Append ( " and f.FEETYPE=" + FEEDC ) ;
}
if ( CURR ! = "" )
{
strSql . Append ( " and f.CURRENCY='" + CURR + "'" ) ;
}
if ( FSTATUS = = "1" | | FSTATUS = = "3" ) {
strSql . Append ( " AND (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9)" ) ;
} else if ( FSTATUS = = "2" ) {
strSql . Append ( " AND (F.FEESTATUS!=0 and F.FEESTATUS!=8 and F.FEESTATUS!=9) " ) ;
}
if ( FSTATUS = = "3" | | FSTATUS = = "4" )
{
strSql . Append ( " AND (f.AMOUNT-isnull(f.SETTLEMENT,0)-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0))<>0" ) ;
}
if ( SALECORP ! = "" ) {
strSql . Append ( " and f.SALECORP='" + SALECORP + "'" ) ;
}
if ( FEEDUITYPE ! = "" )
{
if ( FEEDUITYPECONDIT = = "不等于" )
{
strSql . Append ( " and not EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='" + FEEDUITYPE + "') " ) ;
}
else
{
strSql . Append ( " and EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='" + FEEDUITYPE + "') " ) ;
}
}
strSql . Append ( " GROUP BY B.MBLNO,B.CARRIER " ) ;
}
else {
strSql . Append ( " SELECT '' GID,'" + AutoDui_GID + "' BSNO,B.MBLNO,B.MBLNO B_MBLNO,'' B_BSNO,'' ETD,'' CUSTOMERNAME,'' OP,B.CARRIER,MAX(B.TEU) TEU,'0' RMBAMOUNT,'0' USDAMOUNT,'0' ISOK,'' REMARK,'' REMARK2, " ) ;
if ( FSTATUS = = "3" | | FSTATUS = = "4" )
{
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT-isnull(f.ORDERAMOUNT,0)+isnull(f.ORDERSETTLEMENT,0),0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT " ) ;
}
else
{
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'RMB' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS L_RMBAMOUNT, " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE CURRENCY WHEN 'USD' THEN ISNULL(AMOUNT-SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS L_USDAMOUNT " ) ;
}
strSql . Append ( ",isnull(SUM(CASE FEETYPE WHEN 1 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0)- " ) ;
strSql . Append ( " isnull(SUM(CASE FEETYPE WHEN 2 THEN (CASE WHEN F.FEESTATUS=0 OR F.FEESTATUS=8 OR F.FEESTATUS=9 THEN 0 ELSE ISNULL(AMOUNT,0) END)ELSE 0 END),0) AS B_AMOUNT " ) ;
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.SETTLEMENT and F.CUSTOMERNAME='" + CUSTOMERNAME + "' and EXISTS (select 1 from CH_FEE_AutoDuiDetail fd where (REPLACE(REPLACE(fd.MBLNO,'-',''),' ','')=REPLACE(REPLACE(B.MBLNO,'-',''),' ','') OR fd.MBLNO=B.HBLNO) and fd.BSNO='" + AutoDui_GID + "') " ) ;
if ( HAVEDEBIT = = "0" )
{
strSql . Append ( " and isnull(f.ISDEBIT,0)=0 " ) ;
}
if ( ( FEEDC ! = "0" ) & & ( FEEDC . Trim ( ) ! = "" ) )
{
strSql . Append ( " and f.FEETYPE=" + FEEDC ) ;
}
if ( CURR ! = "" )
{
strSql . Append ( " and f.CURRENCY='" + CURR + "'" ) ;
}
if ( SALECORP ! = "" )
{
strSql . Append ( " and f.SALECORP='" + SALECORP + "'" ) ;
}
if ( FSTATUS = = "1" )
{
strSql . Append ( " AND (F.FEESTATUS=0 or F.FEESTATUS=8 or F.FEESTATUS=9)" ) ;
}
else if ( FSTATUS = = "2" )
{
strSql . Append ( " AND (F.FEESTATUS!=0 and F.FEESTATUS!=8 and F.FEESTATUS!=9) " ) ;
}
if ( FEEDUITYPE ! = "" )
{
if ( FEEDUITYPECONDIT = = "不等于" )
{
strSql . Append ( " and not EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='" + FEEDUITYPE + "') " ) ;
}
else
{
strSql . Append ( " and EXISTS (select 1 from code_fee c where c.[NAME]=f.FEENAME and c.DUITYPE='" + FEEDUITYPE + "') " ) ;
}
}
strSql . Append ( " GROUP BY B.MBLNO,B.CARRIER " ) ;
}
return SetBody ( strSql ) ;
}
private static List < CH_FEE_AutoDuiDetailmb > SetBody ( StringBuilder strSql )
{
var headList = new List < CH_FEE_AutoDuiDetailmb > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
SqlCommand cmd = new SqlCommand ( ) ;
cmd . CommandText = strSql . ToString ( ) ;
cmd . CommandTimeout = 120000 ; //要加这一句
using ( IDataReader reader = db . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
CH_FEE_AutoDuiDetailmb data = new CH_FEE_AutoDuiDetailmb ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . BSNO = Convert . ToString ( reader [ "BSNO" ] ) ;
data . B_BSNO = Convert . ToString ( reader [ "B_BSNO" ] ) ;
data . ETD = Convert . ToString ( reader [ "ETD" ] ) ;
data . CUSTOMERNAME = Convert . ToString ( reader [ "CUSTOMERNAME" ] ) ;
data . OP = Convert . ToString ( reader [ "OP" ] ) ;
data . CARRIER = Convert . ToString ( reader [ "CARRIER" ] ) ;
data . TEU = Convert . ToString ( reader [ "TEU" ] ) ;
data . SALE = Convert . ToString ( reader [ "SALE" ] ) ;
data . CUSTNO = Convert . ToString ( reader [ "CUSTNO" ] ) ;
data . MBLNO = Convert . ToString ( reader [ "MBLNO" ] ) ;
data . B_MBLNO = Convert . ToString ( reader [ "B_MBLNO" ] ) ;
data . RMBAMOUNT = Convert . ToString ( reader [ "RMBAMOUNT" ] ) ;
data . USDAMOUNT = Convert . ToString ( reader [ "USDAMOUNT" ] ) ;
data . L_RMBAMOUNT = Convert . ToString ( reader [ "L_RMBAMOUNT" ] ) ;
data . L_USDAMOUNT = Convert . ToString ( reader [ "L_USDAMOUNT" ] ) ;
data . ISOK = Convert . ToString ( reader [ "ISOK" ] ) ;
data . REMARK = Convert . ToString ( reader [ "REMARK" ] ) ;
data . REMARK2 = Convert . ToString ( reader [ "REMARK2" ] ) ;
data . B_AMOUNT = Convert . ToString ( reader [ "B_AMOUNT" ] ) ;
try
{
data . Y_RMBAMOUNT = Convert . ToDecimal ( data . RMBAMOUNT ) - Convert . ToDecimal ( data . L_RMBAMOUNT ) ;
data . Y_USDAMOUNT = Convert . ToDecimal ( data . USDAMOUNT ) - Convert . ToDecimal ( data . L_USDAMOUNT ) ;
}
catch {
}
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 导入excel
public enum RowState : uint
{
Insert ,
Update ,
None
}
public static bool ImportExcelData ( HttpRequestBase request , DataTable table , out string msg ,
out Int32 InsertCount , out Int32 UpdateCount , string AutoDui_GID , string CUSTOMERNAME , string FEETYPE , out List < CH_FEE_AutoDuiDetailmb > headList , string userid , string username , string companyid )
{
var isSucess = false ;
msg = "" ;
headList = new List < CH_FEE_AutoDuiDetailmb > ( ) ;
var headdate = GetData ( "GID='" + AutoDui_GID + "'" ) ;
if ( table = = null ) throw new ArgumentNullException ( "table" ) ;
var billNoList = new List < string > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
InsertCount = 0 ;
UpdateCount = 0 ;
msg = "" ;
/ *
var objcodename = db . ExecuteScalar ( CommandType . Text , "select ISAGENTCN from Info_Client where shortname='" + AIRLINES + "'" ) ;
var ISAGENTCN = Convert . ToString ( objcodename ) ;
var AIRCODE_OBJ = db . ExecuteScalar ( CommandType . Text , "select codename from Info_Client where shortname='" + AIRLINES + "'" ) ;
var AIRCODE = Convert . ToString ( AIRCODE_OBJ ) ;
* /
using ( DbConnection connection = db . CreateConnection ( ) )
//using (SqlTransaction idbTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
connection . Open ( ) ;
try
{
var orgCode = CookieConfig . GetCookie_OrgCode ( request ) ;
var userCode = CookieConfig . GetCookie_UserCode ( request ) ;
var userName = CookieConfig . GetCookie_UserName ( request ) ;
var _i = 0 ;
//var ExpDate = "";
//var headList = new List<CH_FEE_AutoDuiDetailmb>();
var _DelDetailList = new List < CH_FEE_AutoDuiDetailmb > ( ) ;
var _oldDetailList_NoMBL = GetBodyList ( " BSNO = '" + AutoDui_GID + "' " ) ;
foreach ( DataRow row in table . Rows )
{
_i + + ;
//if (_i < 1) continue;
//var _ACCDATE = ACCDATE;
var _p = 0 ;
if ( string . IsNullOrEmpty ( Convert . ToString ( row [ _p + 0 ] ) . Replace ( " " , "" ) . Replace ( "'" , "" ) ) )
{
break ;
}
var _Detail = new CH_FEE_AutoDuiDetailmb ( ) ;
//_Detail.GID = Guid.NewGuid().ToString();
_Detail . MBLNO = Convert . ToString ( row [ _p + 0 ] ) . Replace ( " " , "" ) . Replace ( "'" , "" ) ; //.Trim()
_Detail . B_MBLNO = _Detail . MBLNO ;
_Detail . MBLNO = GetMBLNO ( _Detail . B_MBLNO ) ;
var RMBAMOUNT = Convert . ToDecimal ( row [ _p + 1 ] ) ;
var USDAMOUNT = Convert . ToDecimal ( row [ _p + 2 ] ) ;
_Detail . RMBAMOUNT = Convert . ToString ( RMBAMOUNT ) ;
_Detail . USDAMOUNT = Convert . ToString ( USDAMOUNT ) ;
// _Detail.B_MBLNO=
//var L_RMBAMOUNT = GetCustFee(CUSTOMERNAME, _Detail.MBLNO, FEETYPE, "RMB", headdate.HAVEDEBIT);
//if (L_RMBAMOUNT == "") {
//业务当中没有该提单号的业务
var 已 有 该 提 单 号 = false ;
var 此 次 表 内 有 该 提 单 号 = false ;
foreach ( var _old in _oldDetailList_NoMBL ) {
if ( _old . MBLNO = = _Detail . MBLNO ) {
已 有 该 提 单 号 = true ;
continue ;
}
}
//if (已有该提单号 == false) {//如果在此次表内就有重复提单号
foreach ( var _h in headList )
{
if ( _h . MBLNO = = _Detail . MBLNO )
{
此 次 表 内 有 该 提 单 号 = true ;
_h . RMBAMOUNT = Convert . ToString ( Convert . ToDecimal ( _h . RMBAMOUNT ) + Convert . ToDecimal ( _Detail . RMBAMOUNT ) ) ;
_h . USDAMOUNT = Convert . ToString ( Convert . ToDecimal ( _h . USDAMOUNT ) + Convert . ToDecimal ( _Detail . USDAMOUNT ) ) ;
continue ;
}
}
if ( 已 有 该 提 单 号 | | 此 次 表 内 有 该 提 单 号 ) {
continue ;
}
if ( 已 有 该 提 单 号 )
{ //如果该自动对账当中已经存在相同提单号的记录
_Detail = _oldDetailList_NoMBL [ 0 ] ;
//_Detail.BSNO="";
_Detail . RMBAMOUNT = Convert . ToString ( RMBAMOUNT ) ;
_Detail . USDAMOUNT = Convert . ToString ( USDAMOUNT ) ;
_Detail . L_RMBAMOUNT = "0" ;
_Detail . L_USDAMOUNT = "0" ;
UpdateCount + + ;
} / * else
if ( 此 次 表 内 有 该 提 单 号 )
{ //如果该自动对账当中已经存在相同提单号的记录
_Detail = _oldDetailList_NoMBL [ 0 ] ;
//_Detail.BSNO="";
_Detail . RMBAMOUNT = Convert . ToString ( RMBAMOUNT + Convert . ToDecimal ( _Detail . RMBAMOUNT ) ) ;
_Detail . USDAMOUNT = Convert . ToString ( USDAMOUNT + Convert . ToDecimal ( _Detail . USDAMOUNT ) ) ;
_Detail . L_RMBAMOUNT = "0" ;
_Detail . L_USDAMOUNT = "0" ;
//UpdateCount++;
}
* /
else
{ //如果不存在
_Detail . GID = Guid . NewGuid ( ) . ToString ( ) ;
_Detail . BSNO = "" ;
_Detail . RMBAMOUNT = Convert . ToString ( RMBAMOUNT ) ;
_Detail . USDAMOUNT = Convert . ToString ( USDAMOUNT ) ;
_Detail . L_RMBAMOUNT = "0" ;
_Detail . L_USDAMOUNT = "0" ;
InsertCount + + ;
}
/ *
if ( msg = = "" )
{
//msg = "有如下提单号在系统内还不存在:" + _Detail.MBLNO;
}
else {
//msg = msg + " ; " + _Detail.MBLNO;
} * /
//_Detail.L_RMBAMOUNT="0";
//_Detail.L_USDAMOUNT="0";
headList . Add ( _Detail ) ;
//continue;
//}
/ *
_Detail . L_RMBAMOUNT = L_RMBAMOUNT ;
_Detail . L_USDAMOUNT = GetCustFee ( CUSTOMERNAME , _Detail . MBLNO , FEETYPE , "USD" , headdate . HAVEDEBIT ) ;
//var _RowState = RowState.Insert;
//string strCheckRow = "select gid from CH_FEE_AutoDuiDetail where BSNO = '" + AutoDui_GID + "' and MBLNO='" + _Detail.MBLNO + "' '" ;
//object statusObj = SqlHelper.ExecuteScalar(db.ConnectionString, CommandType.Text, strCheckRow, null);
//var gid = Guid.NewGuid().ToString("N").ToUpper();
var _oldDetailList = GetBodyList ( " BSNO = '" + AutoDui_GID + "' and MBLNO='" + _Detail . MBLNO + "' " ) ;
if ( _oldDetailList . Count > 0 ) { //如果已经存在相同提单号的
_Detail = _oldDetailList [ 0 ] ;
//_Detail.BSNO="";
_Detail . RMBAMOUNT = Convert . ToString ( RMBAMOUNT ) ;
_Detail . USDAMOUNT = Convert . ToString ( USDAMOUNT ) ;
//_Detail.L_RMBAMOUNT = GetCustFee(CUSTOMERNAME, _Detail.MBLNO, FEETYPE, "RMB", headdate.HAVEDEBIT);
//_Detail.L_USDAMOUNT = GetCustFee(CUSTOMERNAME, _Detail.MBLNO, FEETYPE, "USD", headdate.HAVEDEBIT);
_Detail . L_RMBAMOUNT = "0" ;
_Detail . L_USDAMOUNT = "0" ;
UpdateCount + + ;
}
else
{ //如果不存在
_Detail . BSNO = "" ;
InsertCount + + ;
} * /
/ *
if ( RMBAMOUNT = = Convert . ToDecimal ( _Detail . L_RMBAMOUNT ) & & USDAMOUNT = = Convert . ToDecimal ( _Detail . L_USDAMOUNT ) ) {
_Detail . ISOK = "1" ;
_Detail . REMARK2 = "自动对账成功" ;
} * /
//headList.Add(_Detail);
}
var duihead = GetData ( "GID='" + AutoDui_GID + "'" ) ;
duihead . DbOperationType = DbOperationType . DbotUpd ;
duihead . ModelUIStatus = "E" ;
var modb = new ModelObjectRepository ( ) ;
DBResult result = modb . Save ( duihead ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( headList ) ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( _DelDetailList )
) ;
T_ALL_DA T_ALL_DA = new EntityDA . T_ALL_DA ( ) ;
//var blUpSQL = " update CH_FEE_AutoDuiDetail set B_BSNO=(select top 1 BSNO from v_op_bS where (MBLNO=D.b_mblno or HBLNO=D.MBLNO) and OPLBNAME in('海运进口','海运出口','空运进口','空运出口') ) FROM CH_FEE_AutoDuiDetail d where bsno='" + duihead.GID+"'";
var blUpSQL = " update CH_FEE_AutoDuiDetail set B_BSNO=(select top 1 BSNO from v_op_bS where (REPLACE(REPLACE(MBLNO,'-',''),' ','')=REPLACE(REPLACE(D.MBLNO,'-',''),' ','') or HBLNO=D.MBLNO) ) FROM CH_FEE_AutoDuiDetail d where bsno='" + duihead . GID + "'" ;
bool bl = T_ALL_DA . GetExecuteSqlCommand ( blUpSQL ) ;
/ /
result = ReMake ( duihead , userid , username , companyid ) ;
isSucess = true ;
}
catch ( Exception exception )
{
//idbTran.Rollback();
isSucess = false ;
msg = exception . Message ;
}
}
//UnKnowenTruckNo = UnKnowenTruckNo + " " + UnKnowenAirPort;
return isSucess ;
}
public static string GetMBLNO ( string MBLNO )
{
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( DbConnection connection = db . CreateConnection ( ) )
//using (SqlTransaction idbTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
connection . Open ( ) ;
try
{
var _L_RMBAMOUNT = db . ExecuteScalar ( CommandType . Text , "select MBLNO FROM v_op_bill where REPLACE(REPLACE(MBLNO,'-',''),' ','')=REPLACE(REPLACE('" + MBLNO + "','-',''),' ','') " ) ;
if ( string . IsNullOrEmpty ( Convert . ToString ( _L_RMBAMOUNT ) ) )
{
_L_RMBAMOUNT = db . ExecuteScalar ( CommandType . Text , "select MBLNO FROM v_op_bill where BSNO IN (SELECT BSNO FROM op_manifest WHERE MBLNO='" + MBLNO + "') " ) ;
if ( string . IsNullOrEmpty ( Convert . ToString ( _L_RMBAMOUNT ) ) )
return MBLNO ;
else
return Convert . ToString ( _L_RMBAMOUNT ) ;
}
else
{
return MBLNO ;
}
}
catch ( Exception exception )
{
return "" ;
}
}
}
public static DBResult ReMake ( MsChFeeAutoDuimb _head , string userid , string username , string companyid )
{
var head = GetData ( "GID='" + _head . GID + "'" ) ;
var DetailList = MsChFeeAutoDuiDAL . GetBodyList ( "BSNO='" + head . GID + "'" ) ;
if ( _head . DUITYPE ! = "2" )
DetailList = InsertLocalDetail ( head , DetailList , userid , username , companyid ) ;
var _DelDetailList = new List < CH_FEE_AutoDuiDetailmb > ( ) ;
var modb = new ModelObjectRepository ( ) ;
DBResult result = modb . SaveComm ( "BSNO" , head . GID ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( DetailList ) ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( _DelDetailList )
) ;
DetailList = GetBodyList ( "BSNO='" + head . GID + "'" ) ;
//var _DelDetailList = new List<CH_FEE_AutoDuiDetailmb>();
var LocalDetailList = GetLocalList ( head . CUSTOMERNAME , head . GID , head . FEETYPE , head . HAVEDEBIT , head . FEEDC , head . CURR , head . FSTATUS , head . FEEDUITYPE , head . FEEDUITYPECONDIT , head . SALECORP ) ;
//var LocalDetailList = DetailList;
//var result = new DBResult();
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( DbConnection connection = db . CreateConnection ( ) )
//using (SqlTransaction idbTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
{
connection . Open ( ) ;
try
{
foreach ( CH_FEE_AutoDuiDetailmb _D in DetailList )
{
if ( _D . B_BSNO = = "" )
{
_D . REMARK2 = "我方无此提单号" ;
continue ;
}
else {
}
if ( _D . ISOK = = "1" | | _D . ISOK = = "True" ) {
//重新计算时不再跳过对账成功的业务。
//continue;
}
var _localDetail = new CH_FEE_AutoDuiDetailmb ( ) ;
var get = false ;
foreach ( var _L in LocalDetailList ) {
if ( _L . MBLNO = = _D . MBLNO ) {
_localDetail = _L ;
get = true ;
continue ;
}
}
if ( get = = false )
{
_D . L_RMBAMOUNT = "0" ;
_D . L_USDAMOUNT = "0" ;
}
else
{
_D . L_RMBAMOUNT = _localDetail . L_RMBAMOUNT ;
_D . L_USDAMOUNT = _localDetail . L_USDAMOUNT ;
_D . B_AMOUNT = _localDetail . B_AMOUNT ;
}
if ( _D . REMARK2 ! = "对方账单无此提单号" )
{
if ( Convert . ToDecimal ( _D . RMBAMOUNT ) = = Convert . ToDecimal ( _D . L_RMBAMOUNT ) & & Convert . ToDecimal ( _D . USDAMOUNT ) = = Convert . ToDecimal ( _D . L_USDAMOUNT ) )
{
_D . ISOK = "1" ;
_D . REMARK2 = "自动对账成功" ;
}
else
{
_D . ISOK = "0" ;
if ( Convert . ToDecimal ( _D . RMBAMOUNT ) > Convert . ToDecimal ( _D . L_RMBAMOUNT ) )
{
_D . REMARK2 = "对方RMB金额较多" ;
}
else if ( Convert . ToDecimal ( _D . RMBAMOUNT ) < Convert . ToDecimal ( _D . L_RMBAMOUNT ) )
{
_D . REMARK2 = "我方RMB金额较多" ;
}
if ( Convert . ToDecimal ( _D . USDAMOUNT ) > Convert . ToDecimal ( _D . L_USDAMOUNT ) )
{
if ( _D . REMARK2 . Contains ( "对方RMB金额较多" ) = = true )
{
_D . REMARK2 = "对方金额较多" ;
}
else
{
if ( _D . REMARK2 . Contains ( "对方USD金额较多" ) = = false )
{
_D . REMARK2 = _D . REMARK2 + "对方USD金额较多" ;
}
}
}
else if ( Convert . ToDecimal ( _D . USDAMOUNT ) < Convert . ToDecimal ( _D . L_USDAMOUNT ) )
{
if ( _D . REMARK2 . Contains ( "我方RMB金额较多" ) = = true )
{
_D . REMARK2 = "我方金额较多" ;
}
else
{
if ( _D . REMARK2 . Contains ( "我方USD金额较多" ) = = false )
{
_D . REMARK2 = _D . REMARK2 + "我方USD金额较多" ;
}
}
}
}
}
}
head . DbOperationType = DbOperationType . DbotUpd ;
head . ModelUIStatus = "E" ;
head . STATUS = GetSTATUS ( DetailList ) ;
if ( head . STATUS = = "对账平衡" )
{
head . DUISTATUS = "对账平衡" ;
}
else {
head . DUISTATUS = "对账不平衡" ;
}
//var modb = new ModelObjectRepository();
result = modb . Save ( head ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( DetailList ) ,
ModelObjectConvert < CH_FEE_AutoDuiDetailmb > . ToModelObjectList ( _DelDetailList )
) ;
}
catch ( Exception exception )
{
//idbTran.Rollback();
result . Success = false ;
result . Message = exception . Message ;
}
}
//UnKnowenTruckNo = UnKnowenTruckNo + " " + UnKnowenAirPort;
return result ;
}
private static List < CH_FEE_AutoDuiDetailmb > InsertLocalDetail ( MsChFeeAutoDuimb head , List < CH_FEE_AutoDuiDetailmb > DetailList , string userid , string username , string companyid )
{
//如果有开始和结束日期, 查询所有结算对象中包含head.customername的业务
//然后遍历DetailList,如果提单号没有则向DetailList当中添加该业务。
var BillList = new List < MsOpBill > ( ) ;
if ( head . STARTDATE ! = "" & & head . ENDDATE ! = "" )
{
if ( head . HAVEDEBIT = = "0" )
{
// BillList = Chfee_AuditDAL.Chfee_AuditDAL.GetBillDataList(" OPLBNAME='海运出口' and ETD between '" + head.STARTDATE + "' and '" + head.ENDDATE + "' and exists(select 1 from ch_fee where customername='" + head.CUSTOMERNAME + "' and bsno=V_op_Bill.BSNO and Amount<>settlement and isnull(ISDEBIT,0)=0) ", userid, username, companyid);
BillList = Chfee_AuditDAL . Chfee_AuditDAL . GetBillDataList ( " ETD between '" + head . STARTDATE + "' and '" + head . ENDDATE + "' and exists(select 1 from ch_fee where customername='" + head . CUSTOMERNAME + "' and bsno=V_op_Bill.BSNO and Amount<>settlement and isnull(ISDEBIT,0)=0) " , userid , username , companyid ) ;
}
else
{
BillList = Chfee_AuditDAL . Chfee_AuditDAL . GetBillDataList ( " ETD between '" + head . STARTDATE + "' and '" + head . ENDDATE + "' and exists(select 1 from ch_fee where customername='" + head . CUSTOMERNAME + "' and bsno=V_op_Bill.BSNO and Amount<>settlement) " , userid , username , companyid ) ;
}
foreach ( var _bill in BillList )
{
var 需 要 添 加 = true ;
foreach ( var _d in DetailList )
{
if ( _bill . MBLNO = = _d . MBLNO )
{
需 要 添 加 = false ;
continue ;
}
}
if ( 需 要 添 加 )
{
var newdetail = new CH_FEE_AutoDuiDetailmb ( ) ;
newdetail . GID = Guid . NewGuid ( ) . ToString ( ) ;
newdetail . MBLNO = _bill . MBLNO ;
newdetail . ISOK = "0" ;
newdetail . BSNO = "" ;
newdetail . RMBAMOUNT = "0" ;
newdetail . USDAMOUNT = "0" ;
newdetail . L_RMBAMOUNT = "0" ;
newdetail . L_USDAMOUNT = "0" ;
newdetail . B_MBLNO = _bill . MBLNO ;
newdetail . REMARK2 = "对方账单无此提单号" ;
DetailList . Add ( newdetail ) ;
}
}
}
return DetailList ;
}
public static string GetSTATUS ( List < CH_FEE_AutoDuiDetailmb > DetailList ) {
var _r = "" ;
var _rb = true ;
var _RMB = 0 M ;
var _USD = 0 M ;
foreach ( var _D in DetailList ) {
if ( _D . ISOK ! = "1" ) {
_rb = false ;
_RMB + = Convert . ToDecimal ( _D . RMBAMOUNT ) - Convert . ToDecimal ( _D . L_RMBAMOUNT ) ;
_USD + = Convert . ToDecimal ( _D . USDAMOUNT ) - Convert . ToDecimal ( _D . L_USDAMOUNT ) ;
}
}
if ( _rb ! = true ) {
_r = "对账不平衡:" ;
if ( _RMB > 0 ) {
_r + = "对方RMB金额较多, 差额" + Convert . ToString ( _RMB ) ;
}
if ( _RMB < 0 )
{
_r + = "我方RMB金额较多, 差额" + Convert . ToString ( _RMB ) ;
}
if ( _USD > 0 )
{
if ( _r ! = "对账不平衡:" ) {
_r + = "," ;
}
_r + = "对方USD金额较多, 差额" + Convert . ToString ( _USD ) ;
}
if ( _USD < 0 )
{
if ( _r ! = "对账不平衡:" )
{
_r + = "," ;
}
_r + = "我方USD金额较多, 差额" + Convert . ToString ( _USD ) ;
}
}
if ( _rb = = true )
{
return "对账平衡" ;
}
else {
return _r ;
}
}
# endregion
}
}