using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.Common; using System.Data.Entity.Migrations; using System.Diagnostics; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using Colo.Web.admin.user; using DSWeb.Areas.CommMng.Models; using DSWeb.Areas.MvcShipping.Helper; using DSWeb.Common.DB; using DSWeb.EntityDA; using DSWeb.MvcShipping.DAL.MsChFeeDAL; using DSWeb.MvcShipping.DAL.MsOpApplyDAL; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Helper.Repository; using DSWeb.MvcShipping.Models.MsInfoClient; using DSWeb.MvcShipping.Models.MsOpApply; using DSWeb.SoftMng.Model; using DSWeb.TruckMng.Comm.Cookie; using HcUtility.Comm; using java.rmi.server; using Microsoft.Practices.EnterpriseLibrary.Data; using NPOI.SS.Formula.Functions; namespace DSWeb.Areas.CommMng.DAL { public class PublicAPIDAL { public static JsonResponse GuanMaoYun_Head_Save(GuanMaoYun_Head head) { var result = new JsonResponse(); //将head转化为报关业务 //var OpapplyHead = getHeadInfo(head); var cdc = new CommonDataContext(); var admin = cdc.VW_user.First(x => x.CODENAME == "admin"); var OpapplyHead = getHeadInfo(head, "add", admin); var OpapplyGoodsDetailList = new List(); var OpapplyCtnList = new List(); //根据唯一编号判断是否重复 //如重复则更新 如不重复则添加(MsOpApply 和 MsOpApplyDetail都是) // var bsno = OpapplyHead.BSNO; var _count = BasicDataRefDAL.GetExist("OP_APPLY", " BSNO='" + bsno + "' "); var delDetailList = new List(); if (_count == 0) { OpapplyHead = getHeadInfo(head,"add",admin); OpapplyHead.DbOperationType = DbOperationType.DbotIns; OpapplyHead.ModelUIStatus = "I"; if (OpapplyHead.CUSTNO == "") { var isuse = false; var _r1 = MsOpApplyDAL.getCUSTNO(OpapplyHead, admin.USERID, admin.SHOWNAME, admin.COMPANYID); if (_r1.Success) { OpapplyHead.CUSTNO = _r1.Data.ToString(); //iscreatecustno = true; } else { var jsonRespose2 = new JsonResponse { Success = _r1.Success, Message = _r1.Message, Data = null }; return jsonRespose2; } } OpapplyGoodsDetailList = getGoodsDetailListInfo(head); OpapplyCtnList = getCtnListInfo(head); } else { OpapplyHead = getHeadInfo(head, "update"); OpapplyHead.DbOperationType = DbOperationType.DbotUpd; OpapplyHead.ModelUIStatus = "E"; //var currhead= MsOpApplyDAL.GetData("BSNO='"+ bsno + "'"); OpapplyGoodsDetailList = getGoodsDetailListInfo(head); OpapplyCtnList = getCtnListInfo(head); //同时将后台已有而此次没有的明细加入删除列表 var currDetailList= MsOpApplyDAL.GetBodyList(" BsNo='" + bsno + "'"); if (currDetailList.Count > 0) { foreach (var item in currDetailList) { if (OpapplyGoodsDetailList.Exists(x => x.BsNo == bsno && x.SerialNo == item.SerialNo)) { } else { delDetailList.Add(item); } } } } var modb = new ModelObjectRepository(); DBResult _r = modb.Save(OpapplyHead , ModelObjectConvert.ToModelObjectList(OpapplyGoodsDetailList) , ModelObjectConvert.ToModelObjectList(delDetailList) ); result = new JsonResponse { Success = _r.Success, Message = _r.Message,Data=_r.Data }; return result; } public static JsonResponse GuanMaoYun_Fee_Save(GuanMaoYun_Fee FeeHead) { var result = new JsonResponse(); //根据 GuanMaoYun_Fee.CUSTOMER 查找客户 //在 code_cust_edi当中寻找EDICODE=关贸云的对应关系,取EDINAME=GuanMaoYun_Fee.CUSTOMER的CUST var BSNO = FeeHead.JOB_ID; var _count = BasicDataRefDAL.GetExist("OP_APPLY", " BSNO='" + BSNO + "' "); if (_count == 0) { result = new JsonResponse { Success = false, Message = "【" + BSNO + "】没有该业务编号的业务,请先导出业务" }; return result; } var cdc = new CommonDataContext(); var feecustidList = new List(); foreach (var costs in FeeHead.COSTS) { feecustidList.Add(costs.COAGENT_ID.ToString()); } var customernameList = cdc.code_cust_edi.Where(x => x.EDINAME == "关贸云" && feecustidList.Contains(x.EDICODE) ).ToList(); var CUSTOMER = new info_client_md(); if (customernameList.Count > 0) { foreach (var Fee in FeeHead.COSTS) { if (customernameList.Exists(x => x.EDICODE == Fee.COAGENT_ID)) { } else { result = new JsonResponse { Success = false, Message = "【" + Fee.COAGENT_ID + "】没有对应的往来单位信息" }; return result; } } //CUSTOMER = cdc.info_client.FirstOrDefault(x => x.SHORTNAME == customernameList[0].CUST); } else { var _str = string.Join(",", feecustidList); result = new JsonResponse { Success = false, Message = "【" + _str + "】没有对应的往来单位信息" }; return result; } var CurrChfeelist = cdc.ch_fee.Where(x => x.BSNO == BSNO && x.BXGID=="GMY").ToList(); foreach (var item in FeeHead.COSTS) { var customername= cdc.code_cust_edi.FirstOrDefault(x => x.EDINAME == "关贸云" && x.EDICODE == item.COAGENT_ID); CUSTOMER = cdc.info_client.FirstOrDefault(x => x.SHORTNAME == customername.CUST); //如果存在SOURCE_JOB_BALANCE_ID等于当前存在的某费用 //如状态为 1 6 录入 提交 驳回 则直接更新之 //如状态不为1 6 则返回错误 //如不存在SOURCE_JOB_BALANCE_ID等于当前存在的某费用GID 则添加 if (CurrChfeelist.Exists(x => x.GID == item.SOURCE_JOB_BALANCE_ID)) { //如状态为 1 6 录入 提交 驳回 则直接更新之 //如状态不为1 6 则返回错误 var currfee = CurrChfeelist.First(x => x.GID == item.SOURCE_JOB_BALANCE_ID); if (currfee.FEESTATUS == 1 || currfee.FEESTATUS == 6)//currfee.FEESTATUS == 2 || { currfee = getFee(FeeHead, item, CUSTOMER, currfee); cdc.ch_fee.AddOrUpdate(currfee); cdc.SaveChanges(); result.OK(); } else { result.SetError("费用已审核通过,请将当前业务的费用流程回退至可被更改的状态(录入状态 驳回提交)"); //return result; } } else { //如不存在SOURCE_JOB_BALANCE_ID等于当前存在的某费用GID 则添加 var newfee = getFee(FeeHead, item, CUSTOMER); cdc.ch_fee.Add(newfee); cdc.SaveChanges(); result.OK(); } } foreach (var currfee in CurrChfeelist) { if (FeeHead.COSTS.Exists(x => x.SOURCE_JOB_BALANCE_ID == currfee.GID)) { } else { //如不存在则删除 if (currfee.FEESTATUS == 1 || currfee.FEESTATUS == 6)//currfee.FEESTATUS == 2 || { BasicDataRefDAL.ExecSql($"delete from ch_fee where gid='{currfee.GID}'"); } else { result.SetError("费用已审核通过,请将当前业务的费用流程回退至可被更改的状态(录入状态 驳回提交)"); //return result; } } } return result; } public static MsOpApply getHeadInfo(GuanMaoYun_Head headinfo,string opstatus,VW_user_md userinfo=null) { var result = new MsOpApply(); if (opstatus == "add") { result.INPUTBY = userinfo.SHOWNAME; result.CORPID = userinfo.COMPANYID; } else { result = MsOpApplyDAL.GetData("BSNO='" + headinfo.BUSINESS_ORDER_INFO.WORK_NO + "'"); } var head = headinfo.DECL_HEAD; result.BSNO = headinfo.GetBSNO(); result.MASTERNO = headinfo.BUSINESS_ORDER_INFO.WORK_NO; result.CUSCIQNO = head.EPROT_NO; result.CUSTOMERNAME = headinfo.BUSINESS_ORDER_INFO.EXPORT_NAME; //E出口 I进口 if (head.IE_TYPE == "E") result.BSTYPE = "2"; if (head.IE_TYPE == "I") result.BSTYPE = "1"; //head.DECL_TYPE //暂不处理 //head.DECL_PORT result.MBLNO = head.BILL_NO; result.VESSEL = head.TRANS_TOOL_NAME; result.VOYNO = head.VOYAGE_NO; if (head.是出口()) { result.ETD = head.IE_DATE.ToString("yyyy-MM-dd"); if (result.ETD == "0001-01-01") result.ETD = null; result.PORTDISCHARGE = head.DEST_PORT_CODE; result.DESPPORT = head.CIQ_ENTY_PORT_CODE; } if (head.是进口()) { result.ETA = head.IE_DATE.ToString("yyyy-MM-dd"); if (result.ETD == "0001-01-01") result.ETD = null; result.PORTLOAD = head.DEST_PORT_CODE; result.GOODSOURCE = head.COUNTRY_CODE_EN; result.CONSIGNEE = head.EXPORT_NAME;//经营单位名称 进口:境内收货人名称 出口:发货人名称 } result.ARCOUNTRY = head.COUNTRY_CODE_EN; //result.CUSTOMNO = head.CUSTOMS_NO; result.CUSTOMDATE = head.DECL_DATE.ToString("yyyy-MM-dd"); if (result.CUSTOMDATE == "0001-01-01") result.CUSTOMDATE = null; result.INSPECTIONNO = head.CORRELATION_DECL_NO; result.ENTERP = head.EXPORT_NAME; //经营单位名称 result.ENTERPID = head.EXPORT_CODE; //经营单位代码 result.BOOKNO = head.HAND_BOOK_NO;//手册备案号 result.CONTRACTNO = head.CONTR_NO;//合同号 result.PKGS = string.IsNullOrWhiteSpace(head.TOTAL_PACKEGES) ? 0 : Convert.ToDecimal(head.TOTAL_PACKEGES); result.KGS = string.IsNullOrWhiteSpace(head.TOTAL_GROSS_WEIGHT) ? 0 : Convert.ToDecimal(head.TOTAL_GROSS_WEIGHT); result.NETWEIGHT = string.IsNullOrWhiteSpace(head.TOTAL_NET_WEIGHT) ? 0 : Convert.ToDecimal(head.TOTAL_NET_WEIGHT); //result.CBM = string.IsNullOrWhiteSpace(head.TOTAL_NET_WEIGHT) ? 0 : Convert.ToDecimal(head.TOTAL_NET_WEIGHT); //result.BYCUSTOM = head.cus; result.BSCUSTOMNO = head.RELATIVE_ENTRY_ID;//关联报关单号 result.TRADETERM = head.TRADE_TYPE_CODE;//贸易方式 result.CURRENCY = head.CURRENCY_CODE; result.CURRENCY = head.CURRENCY_CODE; result.CARGOVALUE = head.TOTAL_AMOUNT; result.INQUNO = head.CIQ_BILL_NO; //检验检疫号 result.CUSCIQNO = head.EPROT_NO; //统一编号 电子口岸统一编号 result.TRANCUSTOMNO = head.TRN_PRE_ID; result.EXPCUSTOMTYPE = head.IE_PORT; //出境关别 代码 //result.DESPPORT = head.CIQ_ENTY_PORT_CODE; result.CUSTOMSNUM = headinfo.GOODS_LIST.Count(); return result; } public static List getGoodsDetailListInfo(GuanMaoYun_Head head) { var result = new List(); var BSNO = head.BUSINESS_ORDER_INFO.WORK_NO; if (head.GOODS_LIST.Count > 0) { foreach (var good in head.GOODS_LIST) { var newrec = new MsOpApplyDetail(); var goodinfo = good.DECL_GOODS; newrec.BsNo = BSNO; newrec.SerialNo = BasicDataRefDAL.String2Decimal(goodinfo.GOODS_SEQ); newrec.GoodsCode = goodinfo.HSCODE_TS; newrec.GoodsName = goodinfo.GOODS_NAME; newrec.GOODSMODEL = goodinfo.GOODS_MODEL; newrec.Pkgs = BasicDataRefDAL.String2Decimal(goodinfo.PACKEGES); newrec.GOODSNUM = BasicDataRefDAL.String2Decimal(goodinfo.PACKEGES); newrec.KGS = goodinfo.QTY; //newrec. = goodinfo.QTY; newrec.Price = BasicDataRefDAL.String2Decimal(goodinfo.PRICE); newrec.Total = BasicDataRefDAL.String2Decimal(goodinfo.AMOUNT); newrec.Currency = goodinfo.CURRENCY; result.Add(newrec); } } return result; } public static List getCtnListInfo(GuanMaoYun_Head head) { var result = new List(); var BSNO = head.BUSINESS_ORDER_INFO.WORK_NO; //if (head.DECL_TRN_CONTAINER_LIST.Count > 0) //{ // foreach (var good in head.GOODS_LIST) // { // var newrec = new MsOpCtn(); // result.Add(newrec); // } //} return result; } public static ch_fee_md getFee(GuanMaoYun_Fee FeeHead, COSTS Fee,info_client_md CUSTOMER,ch_fee_md chfee=null) { //if (chfee == null) //{ // chfee = new ch_fee_md(); //} string USERID = "1BEC90E1-9780-472F-90C2-0C6390C044A4"; string BSNO = FeeHead.JOB_ID; string CUSTOMERNAME = CUSTOMER.SHORTNAME; int FEETYPE=Fee.BALANCE_DIRECTION_ID.ToUpper() == "PAYOUT" ? 2 : 1; string FEENAME = string.IsNullOrWhiteSpace( Fee.FEE_TYPE_NAME)?Fee.MEMO: Fee.FEE_TYPE_NAME; decimal AMOUNT = Fee.AMOUNT==null?0:(decimal)Fee.AMOUNT; string UNIT = ""; decimal QUANTITY = Fee.UNIT_NUMBER == null ? 0 : (decimal)Fee.UNIT_NUMBER; chfee = MsChFeeDAL.getChFee(USERID, BSNO, CUSTOMERNAME, FEETYPE, FEENAME, AMOUNT, UNIT, QUANTITY); chfee.BXGID = "GMY";//用于标记关贸云产生的费用 //chfee.GID = Fee.SOURCE_JOB_BALANCE_ID; //chfee.BSNO = FeeHead.JOB_ID; //chfee.FEETYPE = Fee.BALANCE_DIRECTION_ID.ToUpper() == "PAYOUT" ? 2 : 1; //chfee.CUSTOMERNAME = CUSTOMER.SHORTNAME; //chfee.CURRENCY = CURRENCYCODE.GetName(string.IsNullOrWhiteSpace( Fee.MONEY_TYPE_ID)?142: Convert.ToInt32(Fee.MONEY_TYPE_ID)); //chfee.EXCHANGERATE = Fee.EXCHANGE_RATE; //chfee.UNIT = ""; //chfee.AMOUNT = Fee.AMOUNT; //chfee.QUANTITY = Fee.UNIT_NUMBER; return chfee; } } }