You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
17 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Models.MsStevedores;
using System.Transactions;
using System.Data.SqlClient;
using DSWeb.DataAccess;
using DSWeb.MvcShipping.Models.MsOpCtnStatus;
using System.Linq;
using DSWeb.Areas.MvcShipping.Models.MsOpCtnFeeModelModel;
using DSWeb.Areas.MvcShipping.DAL.MsOpCtnFeeModelDAL;
using DSWeb.Areas.MvcShipping.Models.MsOpCtnFeeReport;
using DSWeb.MvcShipping.DAL.MsSysBillNoSet;
namespace DSWeb.Areas.MvcShipping.DAL.MsOpCtnFeeReportDAL
{
public class MsOpCtnFeeReportDAL
{
private static int _Direction;
/// <summary>
///
/// </summary>
/// <param name="condition"></param>
/// <param name="dircetion">0:国内部分 1:国外部分</param>
/// <returns></returns>
public static List<MsOpCtnFeeReport> GetFeeDataList(string condition, int dircetion)
{
_Direction = dircetion;
string whereSql = dircetion == 0 ? " jcyy in ('空箱出场','装船')" : " jcyy in ('卸船','出闸口','空箱还场') ";
if (condition != "" && condition != null)
{
whereSql = whereSql + " and " + condition;
}
List<MsCtnStatus> statusList = MsOpCtnStatusDAL.GetDataStatusList(whereSql);
string tempXianghao = "";
List<MsOpCtnFeeReport> list = new List<MsOpCtnFeeReport>();
for (int i = 0; i < statusList.Count; i++)
{
MsCtnStatus ms = statusList[i];
if (ms.box_no != tempXianghao)
{
tempXianghao = ms.box_no;
MsOpCtnFeeReport rpt = new MsOpCtnFeeReport();
rpt.MBLNO = ms.blno;
rpt.XiangHao = tempXianghao;
rpt.XiangXing = ms.cntrsize;
rpt.TEU = ms.TEU;
rpt.MuDiGang = _Direction == 0 ?
statusList.Find(s => s.jcyy == "装船").cz :
ms.cz;
var listX = statusList.FindAll(s => s.box_no == tempXianghao);
foreach (var item in listX)
{
if (item.jcyy == "卸船")
{
rpt.DaoGangRiQi = item.jctime;
}
else if (item.jcyy == "出闸口")
{
rpt.ChuZhaKouRiQi = item.cctime;
}
else if (item.jcyy == "空箱还场")
{
rpt.HuanXiangRiQi = item.jctime;
}
else if (item.jcyy == "空箱出场")
{
rpt.KongXiangChuChangRiQi = item.cctime;
}
else if (item.jcyy == "装船")
{
rpt.ZhuangChuanRiQi = item.jctime;
}
else
{
continue;
}
}
//用箱时间
if (_Direction == 1)
{
rpt.YongXiangShiJianGangNei = GetTimeDiff(rpt.DaoGangRiQi, rpt.ChuZhaKouRiQi);
if (rpt.ChuZhaKouRiQi == "")
{
if (rpt.HuanXiangRiQi != "")
{
rpt.YongXiangShiJianGangWai = GetTimeDiff(rpt.DaoGangRiQi, rpt.HuanXiangRiQi);
}
else
{
//无换箱日期,无出闸口日期,则统计至从卸船到当天的用箱天数
int timeDifftmp = GetTimeDiff(rpt.DaoGangRiQi,DateTime.Now.ToString("yyyy-MM-dd"));
rpt.YongXiangShiJianGangWai = timeDifftmp;
rpt.YongXiangShiJianGangNei = timeDifftmp;
}
}
else
{
rpt.YongXiangShiJianGangWai = GetTimeDiff(rpt.ChuZhaKouRiQi, rpt.HuanXiangRiQi);
}
}
else if (_Direction == 0)
{
rpt.YongXiangShiJianGangNei = 0;
if (rpt.ZhuangChuanRiQi=="")
{
//无装船日期的话,国内部分用箱时间统计到当天
rpt.YongXiangShiJianGangWai = GetTimeDiff(rpt.KongXiangChuChangRiQi,DateTime.Now.ToString("yyyy-MM-dd"));
}
else
{
rpt.YongXiangShiJianGangWai = GetTimeDiff(rpt.KongXiangChuChangRiQi, rpt.ZhuangChuanRiQi);
}
}
//免箱使
//获取箱使费模板
string code = GetDiDianCodeWithDiDian(rpt.MuDiGang);
string wheresqlNei = @" MainGID = (select GID from Op_Ctn_FEEMODEL where GangNeiGangWai = 0 and PortAndYardCode = '" + code + "') and XiangXing = '" + rpt.XiangXing + "'";
string wheresqlWai = @" MainGID = (select GID from Op_Ctn_FEEMODEL where GangNeiGangWai = 1 and PortAndYardCode = '" + code + "') and XiangXing = '" + rpt.XiangXing + "'";
List<MsOpCtnFeeDetailsModel> modelListNei = MsOpCtnFeeModelDAL.MsOpCtnFeeModelDAL.GetDataDetailsList(wheresqlNei);
List<MsOpCtnFeeDetailsModel> modelListWai = MsOpCtnFeeModelDAL.MsOpCtnFeeModelDAL.GetDataDetailsList(wheresqlWai);
int freedays1 = 0;
int freedays2 = 0;
freedays1 = GetFreeDaysWithBLNO(rpt.MBLNO, modelListNei, modelListWai, out freedays2);
rpt.MianXiangShiGangNei = freedays1;
rpt.MianXiangShiGangWai = freedays2;
//超期时间
rpt.ChaoQiShiJianGangNei = rpt.YongXiangShiJianGangNei - rpt.MianXiangShiGangNei;
rpt.ChaoQiShiJianGangWai = rpt.YongXiangShiJianGangWai - rpt.MianXiangShiGangWai;
rpt.ChaoQiShiJianGangNei = rpt.ChaoQiShiJianGangNei > 0 ? rpt.ChaoQiShiJianGangNei : 0;
rpt.ChaoQiShiJianGangWai = rpt.ChaoQiShiJianGangWai > 0 ? rpt.ChaoQiShiJianGangWai : 0;
//超期费用
float feeNei = GetFeeWithTimeDiffAndFeeModels(rpt.ChaoQiShiJianGangNei, modelListNei);
float feeWai = GetFeeWithTimeDiffAndFeeModels(rpt.ChaoQiShiJianGangWai, modelListWai);
rpt.ChaoQiFeiYongGangNei = feeNei;
rpt.ChaoQiFeiYongGangWai = feeWai;
rpt.HeJi = feeNei + feeWai;
list.Add(rpt);
}
else
{
continue;
}
}
return list;
}
//获取天数差
private static int GetTimeDiff(string d1str, string d2str)
{
if (d1str == "" || d2str == "")
{
return 0;
}
try
{
DateTime d1 = DateTime.Parse(d1str);
DateTime d2 = DateTime.Parse(d2str);
TimeSpan ts1 = new TimeSpan(d1.Ticks);
TimeSpan ts2 = new TimeSpan(d2.Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
return Convert.ToInt32(ts3.TotalDays);
}
catch (Exception)
{
return 0;
throw;
}
}
/// <summary>
///
/// </summary>
/// <param name="timeDiff"></param>
/// <param name="modelList"></param>
/// <returns></returns>
private static float GetFeeWithTimeDiffAndFeeModels(int timeDiff, List<MsOpCtnFeeDetailsModel> modelList)
{
//剩余的天数
int residueDays = timeDiff;
float fee = 0f;
foreach (var item in modelList)
{
if (item.JiFeiDengJi=="1")
{
continue;
}
if (residueDays <= 0)
{
break;
}
if (residueDays < Convert.ToInt32(item.JiFeiQuJian))
{
fee += float.Parse(item.JiFeiDanJia) * (Math.Abs(residueDays));
residueDays = residueDays - residueDays;
}
else
{
residueDays = residueDays - Convert.ToInt32(item.JiFeiQuJian);
fee += float.Parse(item.JiFeiDanJia) * Convert.ToInt32(item.JiFeiQuJian);
}
}
return fee;
}
//根据提单号获取shippingbox订舱时填写的免箱期
private static int GetFreeDaysWithBLNO(string BLNO, List<MsOpCtnFeeDetailsModel> modelNei, List<MsOpCtnFeeDetailsModel> modelWai, out int FreeDays2)
{
FreeDays2 = 0;
if (BLNO == "")
{
return 0;
}
try
{
Database db = DatabaseFactory.CreateDatabase("SHIPPINGBOX");
string sql = "select FREEDAYS1 from BookingCargoSpace where MBLNO = '" + BLNO + "'";
int freedays1 = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql));
string sql2 = "select FREEDAYS2 from BookingCargoSpace where MBLNO = '" + BLNO + "'";
int freedays2 = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql2));
//获取箱使费模板中的免箱期
int mFreeNei = 0;
int mFreeWai = 0;
if (modelNei.Count > 0)
{
mFreeNei = Convert.ToInt32(modelNei.Find(m => m.JiFeiDengJi == "1").JiFeiQuJian);
}
if (modelWai.Count > 0)
{
mFreeWai = Convert.ToInt32(modelWai.Find(m => m.JiFeiDengJi == "1").JiFeiQuJian);
}
freedays1 = mFreeNei > freedays1 ? mFreeNei : freedays1;
FreeDays2 = mFreeWai > freedays2 ? mFreeWai : freedays2;
return freedays1;
}
catch (Exception)
{
return 0;
}
}
private static string GetDiDianCodeWithDiDian(string d)
{
//场站
string sql1 = "select codename from info_client where shortname = '" + d + "' ";
Database db = DatabaseFactory.CreateDatabase();
var code = db.ExecuteScalar(CommandType.Text, sql1);
if (code == null || code.ToString() == "")
{
//港口
return d;
}
return code.ToString();
}
public static bool RuZhang(List<MsOpCtnFeeReport> feeReports,out string msg)
{
bool ruzhangRst = false;
//插入对账表
string GID = Guid.NewGuid().ToString();
string DuiZhangNo = GetDuiZhangNo();
string mblnos = "";
foreach (var rpt in feeReports)
{
insertOp_Ctn_FeeDuiZhuang(rpt, DuiZhangNo);
mblnos+="'"+rpt.MBLNO+"',";
}
//更新动态表
string timeStr = DateTime.Now.ToString("yyyy-MM-dd");
mblnos = mblnos.Length > 0 ? mblnos.Substring(0, mblnos.Length - 1) : "";
updateStatusForContainer_business(mblnos,timeStr);
#region 费用入库
//删除旧数据
Database db = DatabaseFactory.CreateDatabase();
string delSql = "delete from ch_fee where bsno = '" + DuiZhangNo + "'";
db.ExecuteNonQuery(CommandType.Text, delSql);
//计算费用
//去掉已经对账的金额
float yingshou = feeReports.Sum(r=>r.HeJi);
float yiduizhang = GetYiDuiZhangFee(mblnos);
yingshou = yingshou - yiduizhang;
string biBie = "RMB";
//插入新数据
if (yingshou > 0)
{
//应收
StringBuilder sb = new StringBuilder();
sb.Append(@"insert into ch_fee (GID,BSNO,FEETYPE,FEENAME,REMARK,CUSTOMERTYPE,CUSTOMERNAME");
sb.Append(@",UNIT,UNITPRICE,QUANTITY,AMOUNT,CURRENCY,EXCHANGERATE,FEESTATUS,NOTAXAMOUNT,TAXUNITPRICE,WMSOUTBSNO,ENTEROPERATOR)");
sb.Append(@"VALUES(");
sb.Append("'" + Guid.NewGuid() + "',");//GID
sb.Append("'" + DuiZhangNo + "',");//BSNO
sb.Append("1" + ",");//FEETYPE
sb.Append("'" + "修箱费" + "',");//FEENAME
sb.Append("'',");//REMARK
sb.Append("'',");//CUSTOMERTYPE
sb.Append("'ceshi',");//CUSTOMERNAME
sb.Append("'',");//UNIT
sb.Append(yingshou + ",");//UNITPRICE
sb.Append("1" + ",");//QUANTITY
sb.Append(yingshou + ",");//AMOUNT
sb.Append("'" + biBie + "',");//CURRENCY
sb.Append("1" + ",");//EXCHANGERATE
sb.Append("1" + ",");//FEESTATUS
sb.Append(yingshou + ",");//NOTAXAMOUNT
sb.Append(yingshou + ",");//TAXUNITPRICE
sb.Append("'" + DuiZhangNo + "',");//WMSOUTBSNO
sb.Append("''");//ENTEROPERATOR
sb.Append(")");
ruzhangRst = db.ExecuteNonQuery(CommandType.Text, sb.ToString())>0;
msg = "操作完成";
}
else
{
msg = "对账金额为负数!";
ruzhangRst = false;
}
return ruzhangRst;
#endregion
}
private static bool insertOp_Ctn_FeeDuiZhuang(MsOpCtnFeeReport rpt, string DuiZhangNo)
{
string GID = Guid.NewGuid().ToString();
string timeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
StringBuilder sb = new StringBuilder();
sb.Append("insert into Op_Ctn_FeeDuiZhuang ");
sb.Append(" (GID,MBLNO,XiangHao,DuiZhangNo,DuiZhangStatus,DuiZhangTime,DuiZhangJinE) ");
sb.Append(" values( ");
sb.Append("'" + GID + "',");
sb.Append("'" + rpt.MBLNO + "',");
sb.Append("'" + rpt.XiangHao + "',");
sb.Append("'" + DuiZhangNo + "',");
sb.Append("'',");//对账时箱子的状态
sb.Append("'" + timeStr + "',");
sb.Append(rpt.HeJi.ToString() + "");
sb.Append(")");
int rst = 0;
try
{
Database db = DatabaseFactory.CreateDatabase();
rst = db.ExecuteNonQuery(CommandType.Text,sb.ToString());
}
catch (Exception)
{
rst = 0;
throw;
}
return rst > 0;
}
private static bool updateStatusForContainer_business(string mblnos,string duizhangriqi)
{
StringBuilder sb = new StringBuilder();
sb.Append("update container_business ");
sb.Append(" set duizhangriqi = '"+duizhangriqi+"',");
sb.Append(" where blno in ("+mblnos+")");
Database db = DatabaseFactory.CreateDatabase();
try
{
int rst = db.ExecuteNonQuery(CommandType.Text, sb.ToString());
return rst > 0;
}
catch (Exception)
{
return false;
throw;
}
}
private static string GetDuiZhangNo() {
string sql = "select max(DuiZhangNo) from Op_Ctn_FeeDuiZhuang ";
Database db = DatabaseFactory.CreateDatabase();
var rst = db.ExecuteScalar(CommandType.Text,sql);
int no=0;
if (rst==DBNull.Value||rst ==null)
{
string year = DateTime.Now.Year.ToString();
no = int.Parse(year + "000001");
}
else
{
no = int.Parse(rst.ToString())+1;
}
return no.ToString();
}
private static float GetYiDuiZhangFee(string mblno)
{
string sql = "select SUM(duizhangjine) from [Op_Ctn_FeeDuiZhuang] where mblno in ( "+mblno+") ";
Database db = DatabaseFactory.CreateDatabase();
var rst = db.ExecuteScalar(CommandType.Text, sql);
float fee = rst == DBNull.Value ? 0 : float.Parse(rst.ToString());
return fee;
}
}
}