using log4net ;
using Newtonsoft.Json ;
using Quartz ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.SqlClient ;
using System.Linq ;
using System.Text ;
using JobAutoCreateFee.Model ;
namespace JobCreateFee
{
public class JobCreateFee : IJob
{
private ILog log = LogManager . GetLogger ( typeof ( JobCreateFee ) ) ;
public void Execute ( IJobExecutionContext context )
{
string connStr = context . JobDetail . JobDataMap . GetString ( "ConnectString" ) ;
string reqUrl = context . JobDetail . JobDataMap . GetString ( "ReqUrl" ) ;
string companyCode = context . JobDetail . JobDataMap . GetString ( "CompanyCode" ) ;
string companySecret = context . JobDetail . JobDataMap . GetString ( "CompanySecret" ) ;
string querySql = context . JobDetail . JobDataMap . GetString ( "QuerySql" ) ;
string projectquerySql = context . JobDetail . JobDataMap . GetString ( "projectquerySql" ) ;
using ( SqlConnection dbcon = new SqlConnection ( connStr ) )
{
decimal usdexrate = GetUsdExrate ( dbcon ) ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "select * from t_ch_project_auto where (费用类别='场站' OR 费用类别='舱单' OR 费用类别='VGM' OR 费用类别='代理费') and 开始日期<=GETDATE() AND (结束日期>=GETDATE() or isnull(结束日期,'')='') " + projectquerySql + " order by 默认方案,优先级 desc " ) ;
SqlCommand cmd = new SqlCommand ( ) ;
cmd . CommandText = strSql . ToString ( ) ;
cmd . CommandTimeout = 120000 ; //要加这一句
cmd . Connection = dbcon ;
SqlDataAdapter adapter = new SqlDataAdapter ( cmd ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
if ( table . Rows . Count > 0 )
{
var feeprojectlist = CreateFeeProjectList ( table ) ;
if ( feeprojectlist ! = null )
{
foreach ( var enumFeeproject in feeprojectlist )
{
log . Debug ( $"开始费用方案:{enumFeeproject.FADESCRIP}--{DateTime.Now.ToString()}" ) ;
var bsstr = CreateSql ( enumFeeproject . FA_ID , dbcon ) ;
var strSqlBs = new StringBuilder ( ) ;
strSqlBs . Clear ( ) ;
strSqlBs . Append ( "select B.编号,ISNULL(F.编号,'') FEE编号,B.主提单号,B.委托单位,B.船公司,B.货代公司,B.报关行,(select top 1 客户简称 from t_crm_client where EDI代码 IN (SELECT 船代 from t_op_seae_edi where 主提单号=B.主提单号)) 船代,B.场站,B.代理 " ) ;
strSqlBs . Append ( ",ISNULL(F.[场站收费],0) 场站收费,ISNULL(F.[场站付费],0) 场站付费,ISNULL(F.[危险品申报收费],0) 危险品申报收费," ) ;
strSqlBs . Append ( "ISNULL(F.[危险品申报付费],0) 危险品申报付费,ISNULL(F.[舱单收费],0) 舱单收费,ISNULL(F.[舱单付费],0) 舱单付费," ) ;
strSqlBs . Append ( "ISNULL(F.[VGM收费],0) VGM收费,ISNULL(F.[VGM付费],0) VGM付费,ISNULL(F.[操作费收费],0) 操作费收费,ISNULL(F.[操作费付费],0) 操作费付费,ISNULL(F.[独立方案],'') 独立方案" ) ;
strSqlBs . Append ( " from t_op_seae B left join t_op_seae_fee F ON (F.编号=B.编号) where B.业务类型='普通货' " ) ;
//if (enumFeeproject.ISDEP)
//{
//}
//else
//{
if ( enumFeeproject . PROJECTTYPE = = "场站" )
strSqlBs . Append ( " and B.货物标识<>'D' and B.货物标识<>'T' and B.委托单位 not in (select 委托单位 from t_ch_project_auto_cust) and B.装运方式='整箱' and (ISNULL(F.编号,'')='' or (isnull(F.场站收费,0)=0 or isnull(F.场站付费,0)=0)) " ) ;
if ( enumFeeproject . PROJECTTYPE = = "舱单" )
strSqlBs . Append ( " and (ISNULL(F.编号,'')='' or (isnull(F.舱单收费,0)=0 or isnull(F.舱单付费,0)=0)) " ) ;
if ( enumFeeproject . PROJECTTYPE = = "VGM" )
strSqlBs . Append ( " and B.委托单位 not in (select 委托单位 from t_ch_project_auto_cust) and B.装运方式='整箱' and (ISNULL(F.编号,'')='' or (isnull(F.VGM收费,0)=0 or isnull(F.VGM付费,0)=0)) AND ISNULL(B.VGM,0)=1 " ) ;
if ( enumFeeproject . PROJECTTYPE = = "代理费" )
strSqlBs . Append ( " and B.货物标识<>'D' and B.货物标识<>'T' and B.委托单位 not in (select 委托单位 from t_ch_project_auto_cust) and B.装运方式='整箱' and (ISNULL(F.编号,'')='' or (isnull(F.操作费收费,0)=0 or isnull(F.操作费付费,0)=0)) " ) ;
//}
if ( querySql ! = "" )
strSqlBs . Append ( querySql ) ;
strSqlBs . Append ( bsstr ) ;
log . Debug ( $"费用方案语句:{strSqlBs.ToString()}" ) ;
SqlCommand cmdBS = new SqlCommand ( ) ;
cmdBS . CommandText = strSqlBs . ToString ( ) ;
cmdBS . CommandTimeout = 120000 ; //要加这一句
cmdBS . Connection = dbcon ;
SqlDataAdapter bsadapter = new SqlDataAdapter ( cmdBS ) ;
DataTable bstable = new DataTable ( ) ;
bsadapter . Fill ( bstable ) ;
dbcon . Close ( ) ;
if ( bstable . Rows . Count > 0 )
{
var OpSeaeList = CreateOpSeaList ( bstable ) ;
var FeeTemplateDetailList = GetFeeTemplate ( enumFeeproject . FA_ID , dbcon ) ;
try
{
insertFee ( OpSeaeList , FeeTemplateDetailList , enumFeeproject , dbcon , usdexrate ) ;
}
catch ( Exception e )
{
log . Debug ( $"{enumFeeproject.FADESCRIP + " " + enumFeeproject.PROJECTTYPE}生成费用错误:{e.Message}" ) ;
}
}
}
}
}
}
}
static public List < MsOpSeae > CreateOpSeaList ( DataTable table )
{
var headList = new List < MsOpSeae > ( ) ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
MsOpSeae data = new MsOpSeae ( ) ;
data . BSNO = row [ "编号" ] . ToString ( ) ;
data . ISBSNO = row [ "FEE编号" ] . ToString ( ) ;
data . MBLNO = row [ "主提单号" ] . ToString ( ) ;
data . CUSTOMERNAME = row [ "委托单位" ] . ToString ( ) ;
data . CARRIER = row [ "船公司" ] . ToString ( ) ;
data . FORWARDER = row [ "货代公司" ] . ToString ( ) ;
data . CUSTOMSER = row [ "报关行" ] . ToString ( ) ;
data . CUSTSERVICE = row [ "船代" ] . ToString ( ) ;
data . YARD = row [ "场站" ] . ToString ( ) ;
data . AGENTID = row [ "代理" ] . ToString ( ) ;
data . ISYARDDR = Convert . ToBoolean ( row [ "场站收费" ] . ToString ( ) ) ;
data . ISYARDCR = Convert . ToBoolean ( row [ "场站付费" ] . ToString ( ) ) ;
data . ISDGDR = Convert . ToBoolean ( row [ "危险品申报收费" ] . ToString ( ) ) ;
data . ISDGCR = Convert . ToBoolean ( row [ "危险品申报付费" ] . ToString ( ) ) ;
data . ISCDDR = Convert . ToBoolean ( row [ "舱单收费" ] . ToString ( ) ) ;
data . ISCDCR = Convert . ToBoolean ( row [ "舱单付费" ] . ToString ( ) ) ;
data . ISVGMDR = Convert . ToBoolean ( row [ "VGM收费" ] . ToString ( ) ) ;
data . ISVGMCR = Convert . ToBoolean ( row [ "VGM付费" ] . ToString ( ) ) ;
data . ISCZFDR = Convert . ToBoolean ( row [ "操作费收费" ] . ToString ( ) ) ;
data . ISCZFCR = Convert . ToBoolean ( row [ "操作费付费" ] . ToString ( ) ) ;
data . DLFA = row [ "独立方案" ] . ToString ( ) ;
headList . Add ( data ) ;
}
}
return headList ;
}
static public List < MsFeeProject > CreateFeeProjectList ( DataTable table )
{
var headList = new List < MsFeeProject > ( ) ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
MsFeeProject data = new MsFeeProject ( ) ;
data . FA_ID = row [ "方案代码" ] . ToString ( ) ;
data . FADESCRIP = row [ "方案描述" ] . ToString ( ) ;
data . PROJECTTYPE = row [ "费用类别" ] . ToString ( ) ;
if ( row [ "默认方案" ] ! = DBNull . Value )
data . ISDEFAUT = Convert . ToBoolean ( row [ "默认方案" ] . ToString ( ) ) ;
if ( row [ "独立方案" ] ! = DBNull . Value )
data . ISDEP = Convert . ToBoolean ( row [ "独立方案" ] . ToString ( ) ) ;
data . FEETYPE = row [ "收付类型" ] . ToString ( ) ;
data . INPUTBY = row [ "录入人" ] . ToString ( ) ;
headList . Add ( data ) ;
}
}
return headList ;
}
private string insertFee ( List < MsOpSeae > MsOpSeaeList , List < MsChFee > MsChFeeList , MsFeeProject feeProject , SqlConnection dbcon , decimal usdexrate )
{
if ( MsOpSeaeList ! = null )
{
foreach ( var enumValue in MsOpSeaeList )
{
try
{
if ( ( feeProject . PROJECTTYPE = = "场站" & & ( ( feeProject . FEETYPE = = "全部" & & ! enumValue . ISYARDDR & & ! enumValue . ISYARDCR )
| | ( feeProject . FEETYPE = = "收费" & & ! enumValue . ISYARDDR ) | | ( feeProject . FEETYPE = = "付费" & & ! enumValue . ISYARDCR ) ) )
| | ( feeProject . PROJECTTYPE = = "舱单" & & ( ( feeProject . FEETYPE = = "全部" & & ! enumValue . ISCDDR & & ! enumValue . ISCDCR )
| | ( feeProject . FEETYPE = = "收费" & & ! enumValue . ISCDDR ) | | ( feeProject . FEETYPE = = "付费" & & ! enumValue . ISCDCR ) ) )
| | ( feeProject . PROJECTTYPE = = "VGM" & & ( ( feeProject . FEETYPE = = "全部" & & ! enumValue . ISVGMDR & & ! enumValue . ISVGMCR )
| | ( feeProject . FEETYPE = = "收费" & & ! enumValue . ISVGMDR ) | | ( feeProject . FEETYPE = = "付费" & & ! enumValue . ISVGMCR ) ) )
| | ( feeProject . PROJECTTYPE = = "代理费" & & ( ( feeProject . FEETYPE = = "全部" & & ! enumValue . ISCZFDR & & ! enumValue . ISCZFCR )
| | ( feeProject . FEETYPE = = "收费" & & ! enumValue . ISCZFDR ) | | ( feeProject . FEETYPE = = "付费" & & ! enumValue . ISCZFCR ) )
| | ( feeProject . ISDEP & & enumValue . DLFA . IndexOf ( "(" + feeProject . FA_ID + ")" ) < 0 ) )
)
{
log . Debug ( $"{enumValue.MBLNO}开始生成费用,方案:{feeProject.FADESCRIP}--{DateTime.Now.ToString()}" ) ;
var FeeUnitList = GetFeeUnit ( enumValue . BSNO , dbcon ) ;
if ( MsChFeeList ! = null )
{
var iscreatefee = false ;
foreach ( var enumFeeValue in MsChFeeList )
{
decimal exrate = 1 ;
if ( enumFeeValue . Currency = = "USD" ) exrate = usdexrate ;
decimal feequantity = 0 ;
decimal amount = 0 ;
decimal tax = 0 ;
if ( enumFeeValue . Reason ! = "" )
{
var formulastr = enumFeeValue . Reason ;
FeeUnitList . ForEach ( i = >
{
formulastr = formulastr . Replace ( "[" + i . FEEUNIT + "]" , i . FEEQUANTITY . ToString ( ) ) ;
} ) ;
;
try
{
DataTable dt = new DataTable ( ) ;
if ( dt . Compute ( formulastr , "false" ) ! = DBNull . Value )
{
amount = Convert . ToDecimal ( dt . Compute ( formulastr , "false" ) . ToString ( ) ) ;
feequantity = 1 ;
enumFeeValue . UnitPrice = amount ;
FeeUnitList . ForEach ( i = >
{
if ( i . FEEUNIT = = enumFeeValue . Unit )
{
feequantity = i . FEEQUANTITY ;
amount = feequantity * enumFeeValue . UnitPrice ;
}
} ) ;
}
}
catch ( Exception e )
{
log . Debug ( $"{enumValue.MBLNO}公式错误:{e.Message}" ) ;
}
}
else
{
FeeUnitList . ForEach ( i = >
{
if ( i . FEEUNIT = = enumFeeValue . Unit )
{
feequantity = i . FEEQUANTITY ;
amount = feequantity * enumFeeValue . UnitPrice ;
}
} ) ;
}
tax = amount * enumFeeValue . TAXRATE ;
var customername = GetClient ( enumFeeValue . CustomerType , enumValue , enumFeeValue . CustomerName ) ;
if ( enumFeeValue . UnitPrice ! = 0 & & amount ! = 0 & & customername ! = "" )
{
string sql = "INSERT INTO [t_ch_fee] ([类型],[费用状态],[编号],[费用名称],[结算单位],[客户名称],[标准]" ;
sql = sql + ",[单价],[数量],[金额],[币别],[汇率],[备注],[结算金额],[开票金额]" ;
sql = sql + ",[录入人],[录入日期],[是否对帐],[顺序],[冲抵金额]" ;
sql = sql + ",[机密],[是否垫付],[申请金额],[不开发票],[费用英文名称]" ;
sql = sql + ",[申请开票],[提交],[不含税价],[税率],[税额],PROJECTFEEID) VALUES " ;
sql = sql + "('" + enumFeeValue . FeeType + "','审核通过','" + enumValue . BSNO + "','" + enumFeeValue . FeeName + "','" + enumFeeValue . CustomerType + "','" + customername + "','" + enumFeeValue . Unit . Replace ( "'" , "''" ) + "'" ;
sql = sql + "," + enumFeeValue . UnitPrice + "," + feequantity . ToString ( ) + "," + amount . ToString ( ) + ",'" + enumFeeValue . Currency + "'," + exrate . ToString ( ) + ",'" + enumFeeValue . Remark + "',0,0" ;
sql = sql + ",'DEMO-SA',GETDATE(),0," + enumFeeValue . Sort + ",0" ;
sql = sql + ",0,0,0,0,'" + enumFeeValue . FeeEName + "'" ;
sql = sql + ",0,0," + enumFeeValue . UnitPrice + "," + enumFeeValue . TAXRATE + "," + tax . ToString ( ) + ",'" + enumFeeValue . FA_ID + "')" ;
log . Debug ( $"{enumValue.MBLNO},方案:{feeProject.FADESCRIP}-费用语句-{sql}" ) ;
dbcon . Open ( ) ;
SqlCommand cmd = new SqlCommand ( sql , dbcon ) ;
cmd . ExecuteNonQuery ( ) ;
dbcon . Close ( ) ;
iscreatefee = true ;
}
if ( enumFeeValue . UnitPrice = = 0 ) iscreatefee = true ;
}
if ( enumValue . ISBSNO = = "" & & iscreatefee )
{
string sqlexe = "INSERT INTO [t_op_seae_fee] ([编号],[场站收费],[场站付费],[危险品申报收费],[危险品申报付费],[舱单收费],[舱单付费],[VGM收费],[VGM付费],[操作费收费],[操作费付费])" ;
sqlexe = sqlexe + "VALUES" ;
sqlexe = sqlexe + "('" + enumValue . BSNO + "',0,0,0,0,0,0,0,0,0,0)" ;
dbcon . Open ( ) ;
SqlCommand cmdexe = new SqlCommand ( sqlexe , dbcon ) ;
cmdexe . ExecuteNonQuery ( ) ;
dbcon . Close ( ) ;
enumValue . ISBSNO = enumValue . BSNO ;
log . Debug ( $"{enumValue.MBLNO + " , 方 案 " + feeProject.FADESCRIP}生成费用" ) ;
}
if ( iscreatefee )
{
string sqlupdate = "UPDATE [t_op_seae_fee] set 编号=编号 " ;
if ( feeProject . ISDEP )
{
sqlupdate = sqlupdate + " ,独立方案=ISNULL(独立方案,'')+'(" + feeProject . FA_ID + ")'" ;
enumValue . DLFA = enumValue . DLFA + "(" + feeProject . FA_ID + ")" ;
}
else
{
if ( feeProject . FEETYPE = = "全部" & & feeProject . PROJECTTYPE = = "场站" )
{
sqlupdate = sqlupdate + " ,场站收费=1,场站付费=1 " ;
enumValue . ISYARDDR = true ;
enumValue . ISYARDCR = true ;
}
if ( feeProject . FEETYPE = = "收费" & & feeProject . PROJECTTYPE = = "场站" )
{
sqlupdate = sqlupdate + " ,场站收费=1 " ;
enumValue . ISYARDDR = true ;
}
if ( feeProject . FEETYPE = = "付费" & & feeProject . PROJECTTYPE = = "场站" )
{
sqlupdate = sqlupdate + " ,场站付费=1 " ;
enumValue . ISYARDCR = true ;
}
//if (feeProject.FEETYPE == "全部" && feeProject.PROJECTTYPE == "危险品申报")
// sqlupdate = sqlupdate + " ,危险品申报收费=1,危险品申报付费=1 ";
//if (feeProject.FEETYPE == "收费" && feeProject.PROJECTTYPE == "危险品申报")
// sqlupdate = sqlupdate + " ,危险品申报收费=1 ";
//if (feeProject.FEETYPE == "付费" && feeProject.PROJECTTYPE == "危险品申报")
// sqlupdate = sqlupdate + " ,危险品申报付费=1 ";
if ( feeProject . FEETYPE = = "全部" & & feeProject . PROJECTTYPE = = "舱单" )
{
sqlupdate = sqlupdate + " ,舱单收费=1,舱单付费=1 " ;
enumValue . ISCDDR = true ;
enumValue . ISCDCR = true ;
}
if ( feeProject . FEETYPE = = "收费" & & feeProject . PROJECTTYPE = = "舱单" )
{
sqlupdate = sqlupdate + " ,舱单收费=1 " ;
enumValue . ISCDDR = true ;
}
if ( feeProject . FEETYPE = = "付费" & & feeProject . PROJECTTYPE = = "舱单" )
{
sqlupdate = sqlupdate + " ,舱单付费=1 " ;
enumValue . ISCDCR = true ;
}
if ( feeProject . FEETYPE = = "全部" & & feeProject . PROJECTTYPE = = "VGM" )
{
sqlupdate = sqlupdate + " ,VGM收费=1,VGM付费=1 " ;
enumValue . ISVGMDR = true ;
enumValue . ISVGMCR = true ;
}
if ( feeProject . FEETYPE = = "收费" & & feeProject . PROJECTTYPE = = "VGM" )
{
sqlupdate = sqlupdate + " ,VGM收费=1 " ;
enumValue . ISVGMDR = true ;
}
if ( feeProject . FEETYPE = = "付费" & & feeProject . PROJECTTYPE = = "VGM" )
{
sqlupdate = sqlupdate + " ,VGM付费=1 " ;
enumValue . ISVGMCR = true ;
}
if ( feeProject . FEETYPE = = "全部" & & feeProject . PROJECTTYPE = = "代理费" )
{
sqlupdate = sqlupdate + " ,操作费收费=1,操作费付费=1 " ;
enumValue . ISCZFDR = true ;
enumValue . ISCZFCR = true ;
}
if ( feeProject . FEETYPE = = "收费" & & feeProject . PROJECTTYPE = = "代理费" )
{
sqlupdate = sqlupdate + " ,操作费收费=1 " ;
enumValue . ISCZFDR = true ;
}
if ( feeProject . FEETYPE = = "付费" & & feeProject . PROJECTTYPE = = "代理费" )
{
sqlupdate = sqlupdate + " ,操作费付费=1 " ;
enumValue . ISCZFCR = true ;
}
}
dbcon . Open ( ) ;
sqlupdate = sqlupdate + " WHERE 编号='" + enumValue . BSNO + "'" ;
SqlCommand cmdupdate = new SqlCommand ( sqlupdate , dbcon ) ;
cmdupdate . ExecuteNonQuery ( ) ;
if ( feeProject . PROJECTTYPE = = "舱单" ) {
sqlupdate = "UPDATE t_op_seae_edi set 生成费用=1 WHERE 主提单号='" + enumValue . MBLNO + "'" ;
cmdupdate = new SqlCommand ( sqlupdate , dbcon ) ;
cmdupdate . ExecuteNonQuery ( ) ;
}
dbcon . Close ( ) ;
}
}
}
}
catch {
log . Debug ( $"{enumValue.MBLNO},方案:{feeProject.FADESCRIP}-生成费用出错!-{DateTime.Now.ToString()}" ) ;
}
}
}
return "" ;
}
static public string CreateSql ( string FA_ID , SqlConnection dbcon )
{
var str = " and 1=1 " ;
var headList = new List < MsChFee > ( ) ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT *" ) ;
strSql . Append ( " from t_ch_project_Condition where 方案代码=" + FA_ID ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( strSql . ToString ( ) , dbcon ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
if ( row [ "条件名称" ] . ToString ( ) = = "车船直装货物" | | row [ "条件名称" ] . ToString ( ) = = "是否自有箱" )
{
str = str + " AND ISNULL(" + row [ "条件名称" ] . ToString ( ) + ",0)" ;
if ( row [ "条件关系" ] . ToString ( ) = = "等于" ) str = str + "=" + row [ "条件值" ] . ToString ( ) . Replace ( "是" , "1" ) . Replace ( "否" , "0" ) + "" ;
if ( row [ "条件关系" ] . ToString ( ) = = "不等于" ) str = str + "<>" + row [ "条件值" ] . ToString ( ) . Replace ( "是" , "1" ) . Replace ( "否" , "0" ) + "" ;
}
else if ( row [ "条件名称" ] . ToString ( ) = = "船代" )
{
str = str + " AND 主提单号 in (select 主提单号 from t_op_seae_edi where " + row [ "条件名称" ] . ToString ( ) ;
if ( row [ "条件关系" ] . ToString ( ) = = "等于" ) str = str + "='" + row [ "条件值" ] . ToString ( ) + "'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "不等于" ) str = str + "<>'" + row [ "条件值" ] . ToString ( ) + "'" ;
str = str + ")" ;
}
else
{
str = str + " AND " + row [ "条件名称" ] . ToString ( ) ;
if ( row [ "条件关系" ] . ToString ( ) = = "等于" ) str = str + "='" + row [ "条件值" ] . ToString ( ) + "'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "不等于" ) str = str + "<>'" + row [ "条件值" ] . ToString ( ) + "'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "模糊" ) str = str + " like '%" + row [ "条件值" ] . ToString ( ) + "%'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "不包含" ) str = str + " not like '%" + row [ "条件值" ] . ToString ( ) + "%'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "大于" ) str = str + ">='" + row [ "条件值" ] . ToString ( ) + "'" ;
if ( row [ "条件关系" ] . ToString ( ) = = "小于" ) str = str + "<='" + row [ "条件值" ] . ToString ( ) + "'" ;
}
}
}
return str ;
}
static public List < MsChFee > GetFeeTemplate ( string FA_ID , SqlConnection dbcon )
{
var headList = new List < MsChFee > ( ) ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT [DE_ID],[方案代码],[类型],[费用名称],[结算单位],[客户名称],[币别],[汇率],[标准],[单价],[费用备注],ISNULL([顺序],0) 顺序,ISNULL([税率],0) 税率,[单价公式],[费用英文名称]" ) ;
strSql . Append ( " from t_ch_project_auto_fee where 方案代码=" + FA_ID ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( strSql . ToString ( ) , dbcon ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
var Feedata = new MsChFee ( ) ;
Feedata . DE_ID = row [ "DE_ID" ] . ToString ( ) ;
Feedata . FA_ID = row [ "方案代码" ] . ToString ( ) ;
Feedata . FeeType = row [ "类型" ] . ToString ( ) ;
Feedata . FeeName = row [ "费用名称" ] . ToString ( ) ;
Feedata . CustomerType = row [ "结算单位" ] . ToString ( ) ;
Feedata . CustomerName = row [ "客户名称" ] . ToString ( ) ;
Feedata . Currency = row [ "币别" ] . ToString ( ) ;
if ( row [ "汇率" ] ! = DBNull . Value )
Feedata . ExChangerate = Convert . ToDecimal ( row [ "汇率" ] . ToString ( ) ) ;
if ( row [ "单价" ] ! = DBNull . Value )
Feedata . UnitPrice = Convert . ToDecimal ( row [ "单价" ] . ToString ( ) ) ;
if ( row [ "税率" ] ! = DBNull . Value )
Feedata . TAXRATE = Convert . ToDecimal ( row [ "税率" ] . ToString ( ) ) ;
Feedata . Unit = row [ "标准" ] . ToString ( ) ;
// Feedata. = row["税率"].ToString();
Feedata . Remark = row [ "费用备注" ] . ToString ( ) ;
Feedata . Reason = row [ "单价公式" ] . ToString ( ) ;
Feedata . Sort = row [ "顺序" ] . ToString ( ) ;
Feedata . FeeEName = row [ "费用英文名称" ] . ToString ( ) ;
headList . Add ( Feedata ) ;
}
}
return headList ;
}
static public List < MsFeeUnit > GetFeeUnit ( string BSNO , SqlConnection dbcon )
{
decimal TEUNUM ;
TEUNUM = 0 ;
decimal PKGS ;
PKGS = 0 ;
decimal KGS ;
KGS = 0 ;
decimal CBM ;
CBM = 0 ;
decimal NETWEIGHT = 0 ;
decimal CTNNUM ;
CTNNUM = 0 ;
var headList = new List < MsFeeUnit > ( ) ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT NO,PKGS,KGS,CBM,NETWEIGHT,CDP,FCDP,CTNALL,CTNNUM,TEU,提箱小票数" ) ;
strSql . Append ( ",(CASE WHEN 提箱小票数=0 THEN 0 ELSE CASE WHEN 提箱小票数>总箱数 THEN 提箱小票数-总箱数 ELSE 0 END END) 超额小票数" ) ;
strSql . Append ( ",(CASE WHEN 提箱小票数=0 THEN 0 ELSE CASE WHEN 提箱小票数>总箱数 THEN 总箱数 ELSE 提箱小票数 END END) 不超额小票数 FROM (" ) ;
strSql . Append ( "SELECT '1' as NO,件数 PKGS,重量 KGS,尺码 CBM,重量 AS NETWEIGHT,ISNULL((select COUNT(*) FROM t_op_seae_edi WHERE 船代<>'更改' and 主提单号=t_op_seae.主提单号 and isnull(订舱号码,'')<>''),0) CDP," ) ;
strSql . Append ( "ISNULL((select COUNT(*) FROM t_op_seae_edi WHERE 船代<>'更改' and 主提单号=t_op_seae.主提单号 AND ISNULL(分提单号,'')<>'' and isnull(订舱号码,'')<>''),0) FCDP," ) ;
strSql . Append ( "'' as CTNALL,0 AS CTNNUM,0 AS TEU,isnull((select sum(ctnrCount) from t_op_txxp_log where 编号=t_op_seae.编号),0) 提箱小票数 " ) ;
strSql . Append ( ",isnull((select sum(数量) from t_op_ctn where 编号=t_op_seae.编号),0) 总箱数 " ) ;
// strSql.Append(",isnull((select sum(ctnrCount) from t_op_txxp_log where 编号=t_op_seae.编号),0)-isnull((select sum(数量) from t_op_ctn where 编号=t_op_seae.编号),0) 超额小票数 ");
strSql . Append ( " from t_op_seae " ) ;
strSql . Append ( " where 编号='" + BSNO + "'" ) ;
strSql . Append ( ") AS T " ) ;
strSql . Append ( " union " ) ;
strSql . Append ( "SELECT '2' AS NO,0 AS PKGS,0 AS KGS,0 AS CBM,0 AS NETWEIGHT,0 AS CDP,0 AS FCDP," ) ;
strSql . Append ( "P.表现形式 CTNALL,SUM(P.数量) AS CTNNUM,SUM(P.TEU) AS TEU,0 提箱小票数,0 超额小票数,0 不超额小票数" ) ;
strSql . Append ( " from t_op_ctn P " ) ;
strSql . Append ( " where p.编号='" + BSNO + "'" ) ;
strSql . Append ( " GROUP BY P.表现形式 order by PKGS desc" ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( strSql . ToString ( ) , dbcon ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
var isfirst = 0 ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
if ( row [ "NO" ] . ToString ( ) = = "1" ) {
var data = new MsFeeUnit ( ) ;
data . FEEUNIT = "单票" ;
data . FEEQUANTITY = 1 ;
headList . Add ( data ) ;
if ( ! string . IsNullOrEmpty ( row [ "PKGS" ] . ToString ( ) ) )
PKGS = Convert . ToDecimal ( row [ "PKGS" ] . ToString ( ) ) ;
if ( ! string . IsNullOrEmpty ( row [ "KGS" ] . ToString ( ) ) )
KGS = Convert . ToDecimal ( row [ "KGS" ] . ToString ( ) ) ;
if ( ! string . IsNullOrEmpty ( row [ "CBM" ] . ToString ( ) ) )
CBM = Convert . ToDecimal ( row [ "CBM" ] . ToString ( ) ) ;
if ( ! string . IsNullOrEmpty ( row [ "NETWEIGHT" ] . ToString ( ) ) )
NETWEIGHT = Convert . ToDecimal ( row [ "NETWEIGHT" ] . ToString ( ) ) ;
var data2 = new MsFeeUnit ( ) ;
data2 . FEEUNIT = "重量" ;
data2 . FEEQUANTITY = KGS ;
headList . Add ( data2 ) ;
var data3 = new MsFeeUnit ( ) ;
data3 . FEEUNIT = "尺码" ;
data3 . FEEQUANTITY = CBM ;
headList . Add ( data3 ) ;
var data4 = new MsFeeUnit ( ) ;
data4 . FEEUNIT = "件数" ;
data4 . FEEQUANTITY = PKGS ;
headList . Add ( data4 ) ;
var data5 = new MsFeeUnit ( ) ;
data5 . FEEUNIT = "舱单票数" ;
data5 . FEEQUANTITY = Convert . ToInt32 ( row [ "CDP" ] . ToString ( ) ) ;
headList . Add ( data5 ) ;
var data6 = new MsFeeUnit ( ) ;
data6 . FEEUNIT = "舱单分票" ;
data6 . FEEQUANTITY = Convert . ToInt32 ( row [ "FCDP" ] . ToString ( ) ) ;
headList . Add ( data6 ) ;
var txxpdata = new MsFeeUnit ( ) ;
txxpdata . FEEUNIT = "提箱小票数" ;
txxpdata . FEEQUANTITY = Convert . ToInt32 ( row [ "提箱小票数" ] . ToString ( ) ) ;
headList . Add ( txxpdata ) ;
var txxpdata2 = new MsFeeUnit ( ) ;
txxpdata2 . FEEUNIT = "超额小票数" ;
txxpdata2 . FEEQUANTITY = Convert . ToInt32 ( row [ "超额小票数" ] . ToString ( ) ) ;
headList . Add ( txxpdata2 ) ;
if ( Convert . ToInt32 ( row [ "不超额小票数" ] . ToString ( ) ) > 0 )
{
var txxpdata3 = new MsFeeUnit ( ) ;
txxpdata3 . FEEUNIT = "不超额小票数" ;
txxpdata3 . FEEQUANTITY = Convert . ToInt32 ( row [ "不超额小票数" ] . ToString ( ) ) ;
headList . Add ( txxpdata3 ) ;
}
else {
//var txxpdata3 = new MsFeeUnit();
//txxpdata3.FEEUNIT = "不超额小票数";
//txxpdata3.FEEQUANTITY = Convert.ToInt32(row["不超额小票数"].ToString());
//headList.Add(txxpdata3);
}
}
if ( row [ "CTNALL" ] . ToString ( ) ! = "" ) {
var ctndata = new MsFeeUnit ( ) ;
ctndata . FEEUNIT = row [ "CTNALL" ] . ToString ( ) ;
if ( ! string . IsNullOrEmpty ( row [ "CTNNUM" ] . ToString ( ) ) )
ctndata . FEEQUANTITY = Convert . ToDecimal ( row [ "CTNNUM" ] . ToString ( ) ) ;
CTNNUM = CTNNUM + ctndata . FEEQUANTITY ;
if ( ! string . IsNullOrEmpty ( row [ "TEU" ] . ToString ( ) ) )
TEUNUM = TEUNUM + Convert . ToDecimal ( row [ "TEU" ] . ToString ( ) ) ;
headList . Add ( ctndata ) ;
}
isfirst = 1 ;
}
var teudata = new MsFeeUnit ( ) ;
teudata . FEEUNIT = "TEU" ;
teudata . FEEQUANTITY = TEUNUM ;
headList . Add ( teudata ) ;
var ctnalldata = new MsFeeUnit ( ) ;
ctnalldata . FEEUNIT = "箱数" ;
ctnalldata . FEEQUANTITY = CTNNUM ;
headList . Add ( ctnalldata ) ;
}
return headList ;
}
static public string GetClient ( string custtype , MsOpSeae opSeae , string cust )
{
if ( cust ! = "" ) return cust ;
var custname = "" ;
if ( custtype = = "委托单位" ) custname = opSeae . CUSTOMERNAME ;
if ( custtype = = "船公司" ) custname = opSeae . CARRIER ;
if ( custtype = = "货代公司" ) custname = opSeae . FORWARDER ;
if ( custtype = = "报关行" ) custname = opSeae . CUSTOMSER ;
if ( custtype = = "船代" ) custname = opSeae . CUSTSERVICE ;
if ( custtype = = "场站" ) custname = opSeae . YARD ;
if ( custtype = = "代理" ) custname = opSeae . AGENTID ;
return custname ;
}
static public decimal GetUsdExrate ( SqlConnection dbcon )
{
decimal usdexrate = 0 ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "select 汇率 from t_code_usdexrate where 开始日期<=GETDATE() and 结束日期>=GETDATE()" ) ;
SqlDataAdapter adapter = new SqlDataAdapter ( strSql . ToString ( ) , dbcon ) ;
DataTable table = new DataTable ( ) ;
adapter . Fill ( table ) ;
if ( table . Rows . Count > 0 )
{
foreach ( DataRow row in table . Rows )
{
usdexrate = Convert . ToDecimal ( row [ "汇率" ] . ToString ( ) ) ;
}
}
if ( usdexrate = = 0 ) {
SqlDataAdapter adapter2 = new SqlDataAdapter ( "select 汇率 from t_code_currency where 币别='USD'" , dbcon ) ;
DataTable table2 = new DataTable ( ) ;
adapter2 . Fill ( table2 ) ;
if ( table2 . Rows . Count > 0 )
{
foreach ( DataRow row in table2 . Rows )
{
usdexrate = Convert . ToDecimal ( row [ "汇率" ] . ToString ( ) ) ;
}
}
}
return usdexrate ;
}
}
}