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

360 lines
19 KiB

4 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Models.UniApiEntity;
using Newtonsoft.Json;
using CK.SCP.Models.AppBoxEntity;
namespace CK.SCP.GrupUniApi.Controller
{
public static class OdbcApiScpController
{
private static string CreateSeq(string pre)
{
return pre + "_" + DateTime.Now.ToString(ScpCache.Config.SCP接口数据流水号格式);
}
private static void PutScpCtrl(UniApiEntities idb, string seq, string tableName, int rowCount,string p_domain,string p_site)
{
var wmsCtrl = new xxscm_ctrl
{
xxscm_seq = seq,
xxscm_table = tableName,
xxscm_table_qty = rowCount,
xxscm_qad = ((int)UniApiState.).ToString(),
xxscm_mes = ((int)UniApiState.).ToString(),
xxscm_scm = ((int)UniApiState.).ToString(),
xxscm_wms = ((int)UniApiState.).ToString(),
xxscm_rmks = "",
xxscm_domain =p_domain,
xxscm_site = p_site,
//xxscm_create_time = DateTime.Now,
};
idb.xxscm_ctrl.Add(wmsCtrl);
}
public static void PutAll(ScpEntities sdb, UniApiEntities idb, List<TS_UNI_API> dataList)
{
var billNumList = dataList.Select(p => p.BillNum).Distinct();
foreach (string billNum in billNumList)
{
var list = dataList.Where(p => p.BillNum == billNum).ToList();
//if (GlobalVar.ApiConfig.Scm采购订单)
// PutScmPo(idb,
// list.Where(p => p.InterfaceType == UniApiType.PO.ToString()).ToList());
if (GlobalVar.ApiConfig.Scm发货单)
PutScmAsn(idb, list.Where(p => p.InterfaceType == UniApiType.Receive.ToString()).ToList());
//if (GlobalVar.ApiConfig.Scm条码明细)
// PutScmBarcode(idb, list.Where(p => p.InterfaceType == UniApiType.BarCode.ToString()).ToList());
if (GlobalVar.ApiConfig.Scm发票)
PutScmInvoice(idb, list.Where(p => p.InterfaceType == UniApiType.Invoice.ToString()).ToList());
Thread.Sleep(10);
}
}
/// <summary>
/// 发货单
/// </summary>
/// <param name="idb"></param>
/// <param name="dataList"></param>
private static void PutScmAsn(UniApiEntities idb, List<TS_UNI_API> dataList)
{
var uni_api = dataList.FirstOrDefault();
string seq = string.Empty;
if (dataList.Count > 0)
{
seq = CreateSeq(string.IsNullOrEmpty(uni_api.Domain)?uni_api.Site:uni_api.Domain);
}
var tableName = ScmTableName.xxscm_tx_mstr.ToString();
var qadDataList = new List<xxscm_tx_mstr>();
foreach (var wmsData in dataList)
{
var qadData = qadDataList.SingleOrDefault(p => p.xxscm_tx_nbr == wmsData.BillNum
&& p.xxscm_tx_domain == wmsData.Site
&& p.xxscm_tx_part == wmsData.PartCode && p.xxscm_tx_batch==wmsData.Batch && p.xxscm_tx_vend_batch== wmsData.VendBatch);
if (qadData == null)
{
qadData = new xxscm_tx_mstr
{
xxscm_tx_seq = seq,
xxscm_tx_nbr = wmsData.BillNum,
4 years ago
xxscm_tx_part = wmsData.PartCode,
xxscm_tx_qadread = ((int)UniApiState.).ToString(),
xxscm_tx_wmsread = ((int)UniApiState.).ToString(),
xxscm_tx_scmread = ((int)UniApiState.).ToString(),
xxscm_tx_mesread = ((int)UniApiState.).ToString(),
xxscm_tx_createur = wmsData.CreateOper,
xxscm_tx_createdt = wmsData.CreateTime,
xxscm_tx_updateur = "",
xxscm_tx_updatedt = DateTime.Now,
xxscm_tx_rmks = "",
xxscm_tx_domain = wmsData.Domain,
xxscm_tx_po_nbr = wmsData.ErpBillNum,
xxscm_tx_po_line = wmsData.ErpLineNum.ToString(),
xxscm_tx_conv = wmsData.UmConv,
xxscm_tx_date_Promise = wmsData.ValidDate,
xxscm_tx_date_due = wmsData.ValidDate,
xxscm_tx_date_ship = wmsData.CreateTime,
xxscm_tx_loc_um = wmsData.LocUnit == null ? "" : wmsData.LocUnit,
xxscm_tx_po_um = wmsData.PoUnit == null ? "" : wmsData.PoUnit,
xxscm_tx_pprice = wmsData.Price,
xxscm_tx_qty_std = wmsData.PackQty,
xxscm_tx_qty_total = wmsData.Qty,
xxscm_tx_status = ((int)FormState.).ToString(),
xxscm_tx_type = "",
xxscm_tx_vend = wmsData.VendId,
xxscm_tx_vend_batch = wmsData.VendBatch,
xxscm_tx_ware = "",
xxscm_tx_ware_class = "",
xxscm_tx_batch = wmsData.Batch,
xxscm_tx_produce_date = wmsData.ValidDate,
xxscm_tx_site = (!string.IsNullOrEmpty(wmsData.Site)) ? wmsData.Site : wmsData.Domain
4 years ago
};
//PutScpCtrl(idb, qadData.xxscm_tx_seq, tableName,1,wmsData.Domain,wmsData.Site );
qadDataList.Add(qadData);
}
wmsData.State = (int)BillState.Finish;
wmsData.PutTime = DateTime.Now;
}
if (qadDataList.Count == 0) return;
idb.xxscm_tx_mstr.AddRange(qadDataList);
Console.WriteLine($"新增 发货单 数据:{qadDataList.Count} 条");
PutScpCtrl(idb, seq, tableName, qadDataList.Count,uni_api.Domain,uni_api.Site);
}
/// <summary>
/// 采购订单
/// </summary>
/// <param name = "idb" ></ param >
/// < param name="dataList"></param>
//private static void PutScmPo(UniApiEntities idb, List<TS_UNI_API> dataList)
//{
// var seq = CreateSeq("PO");
// var tableName = ScmTableName.xxscm_pod_det.ToString();
// var qadDataList = new List<xxscm_pod_det>();
// foreach (var wmsData in dataList)
// {
// var qadData = qadDataList.SingleOrDefault(p => p.xxscm_pod_nbr == wmsData.ErpBillNum
// && p.xxscm_pod_line == wmsData.ErpLineNum
// && p.xxscm_pod_site == ScpCache.Config.QAD地点
// && p.xxscm_pod_part == wmsData.PartCode);
// if (qadData == null)
// {
// if (wmsData.PartCode != null)
// qadData = new xxscm_pod_det
// {
// xxscm_pod_seq = seq,
// xxscm_pod_nbr = wmsData.ErpBillNum,
// xxscm_pod_part = wmsData.PartCode,
// xxscm_pod_site = ScpCache.Config.QAD地点,
// xxscm_pod_vend = wmsData.VendId,
// xxscm_pod_loc_um = wmsData.LocUnit,
// xxscm_pod_qadread = ((int)UniApiState.待执行).ToString(),
// xxscm_pod_wmsread = ((int)UniApiState.待执行).ToString(),
// xxscm_pod_scmread = ((int)UniApiState.待执行).ToString(),
// xxscm_pod_mesread = ((int)UniApiState.待执行).ToString(),
// xxscm_pod_createur = wmsData.CreateOper,
// xxscm_pod_createdt = wmsData.CreateTime,
// xxscm_pod_updateur = "",
// xxscm_pod_updatedt = DateTime.Now,
// xxscm_pod_rmks = "",
// xxscm_pod_domain = ScpCache.Config.QAD域,
// xxscm_pod_attn = wmsData.Attn,
// xxscm_pod_buyer = wmsData.Buyer,
// xxscm_pod_buyer_phone = wmsData.BuyerPhone,
// xxscm_pod_confirm = false,
// xxscm_pod_curr = wmsData.Currency,
// xxscm_pod_desc = ScpCache.GetPartDesc(wmsData.PartCode),
// xxscm_pod_due_date = wmsData.ValidDate,
// xxscm_pod_line = wmsData.ErpLineNum,
// xxscm_pod_made = "",
// xxscm_pod_modtype = wmsData.ModType,
// xxscm_pod_name = "",
// xxscm_pod_price = wmsData.Price,
// xxscm_pod_qty_ord = wmsData.PackQty,
// xxscm_pod_qty_std = wmsData.Qty,
// xxscm_pod_qty_rct = 0,
// xxscm_pod_ship = wmsData.SourceBillNum,
// xxscm_pod_status = wmsData.State.ToString(),
// xxscm_pod_um = wmsData.PoUnit,
// xxscm_pod_um_conv = wmsData.UmConv,
// };
// qadDataList.Add(qadData);
// }
// wmsData.State = (int)BillState.Finish;
// wmsData.PutTime = DateTime.Now;
// }
// if (qadDataList.Count == 0) return;
// idb.xxscm_pod_det.AddRange(qadDataList);
// Console.WriteLine($"新增 采购订单 数据:{qadDataList.Count} 条");
// PutScpCtrl(idb, seq, tableName, qadDataList.Count);
//}
/// <summary>
/// 条码
/// </summary>
/// <param name="idb"></param>
/// <param name="dataList"></param>
private static void PutScmBarcode(UniApiEntities idb, List<TS_UNI_API> dataList)
{
var tableName = ScmTableName.xxscm_tx_det.ToString();
var qadDataList = new List<xxscm_tx_det>();
foreach (var wmsData in dataList)
{
var qadData = qadDataList.SingleOrDefault(p => p.xxscm_tx_nbr == wmsData.BillNum
&& p.xxscm_tx_boxcode == wmsData.Barcode);
if (qadData == null)
{
qadData = new xxscm_tx_det
{
xxscm_tx_seq = CreateSeq(wmsData.Domain),
xxscm_tx_nbr = wmsData.BillNum,
xxscm_tx_part = wmsData.PartCode,
xxscm_tx_qadread = ((int)UniApiState.).ToString(),
xxscm_tx_wmsread = ((int)UniApiState.).ToString(),
xxscm_tx_scmread = ((int)UniApiState.).ToString(),
xxscm_tx_mesread = ((int)UniApiState.).ToString(),
xxscm_tx_createur = wmsData.CreateOper,
xxscm_tx_createdt = wmsData.CreateTime,
xxscm_tx_updateur = "",
xxscm_tx_updatedt = DateTime.Now,
xxscm_tx_rmks = "",
xxscm_tx_domain = wmsData.Domain,
xxscm_tx_vend = wmsData.VendId,
xxscm_tx_batch = wmsData.VendBatch,
xxscm_tx_boxcode = wmsData.Barcode,
xxscm_tx_qty_real = wmsData.Qty,
4 years ago
4 years ago
};
qadDataList.Add(qadData);
}
wmsData.State = (int)BillState.Finish;
wmsData.PutTime = DateTime.Now;
}
if (qadDataList.Count == 0) return;
idb.xxscm_tx_det.AddRange(qadDataList);
Console.WriteLine($"新增 条码 数据:{qadDataList.Count} 条");
}
/// <summary>
/// 发票
/// </summary>
/// <param name="idb"></param>
/// <param name="dataList"></param>
private static void PutScmInvoice(UniApiEntities idb, List<TS_UNI_API> dataList)
{
try
{
if (dataList.Count > 0)
{
var tableName = ScmTableName.xxscm_inv_det.ToString();
var _ls = dataList.Select(p => p.BillNum).Distinct().ToList();
foreach (var invBillNum in _ls)
{
var _invList = idb.xxscm_inv_det.Where(p => p.xxscm_inv_nbr == invBillNum ).ToList();
if (_invList.Count == 0 )
{
var _unapiList = dataList.Where(p => p.BillNum == invBillNum).ToList();
var _invbill = _invList.FirstOrDefault();
if (_unapiList.Count > 0)
{
Thread.Sleep(10);
string seq = string.Empty;
var _api = _unapiList.FirstOrDefault();
seq = CreateSeq(string.IsNullOrEmpty(_api.Domain) ? _api.Site : _api.Domain);
var qadDataList = new List<xxscm_inv_det>();
int i = 0;
_unapiList.ForEach(p =>
{
if (!string.IsNullOrEmpty(p.Attn))
{
var _invoice = JsonConvert.DeserializeObject<V_TB_INVOICE>(p.Attn);
try
{
if (p.SourceBillNum != p.ErpBillNum)
{
var qadData = new xxscm_inv_det();
qadData.xxscm_inv_seq = seq;
qadData.xxscm_inv_nbr = p.BillNum;
qadData.xxscm_inv_site = p.Site;
qadData.xxscm_inv_part = p.PartCode;
qadData.xxscm_inv_qty = p.Qty;
qadData.xxscm_inv_qadread = ((int)UniApiState.).ToString();
qadData.xxscm_inv_wmsread = ((int)UniApiState.).ToString();
qadData.xxscm_inv_scmread = ((int)UniApiState.).ToString();
qadData.xxscm_inv_mesread = ((int)UniApiState.).ToString();
qadData.xxscm_inv_createur = p.CreateOper;
qadData.xxscm_inv_createdt = p.CreateTime;
qadData.xxscm_inv_updateur = "";
qadData.xxscm_inv_updatedt = DateTime.Now;
qadData.xxscm_inv_rmks = "";
qadData.xxscm_inv_domain = p.Domain;
qadData.xxscm_inv_amt = Math.Round(p.Price * p.Qty, 2);
qadData.xxscm_inv_asn = p.SourceBillNum;
qadData.xxscm_inv_curr = p.Currency==null? "CNY" : p.Currency;
4 years ago
qadData.xxscm_inv_vend = p.VendId;
qadData.xxscm_inv_project = p.VendBatch;//代替项目传值
qadData.xxscm_inv_line = p.ErpLineNum;
qadData.xxscm_inv_invoice = p.Invoice;
qadData.xxscm_inv_order = p.ErpBillNum;
qadData.xxscm_inv_price = p.Price;
qadData.xxscm_inv_taxamt = Math.Round(Math.Round(p.Price * p.Qty,2) * p.Tax, 2) + Math.Round(p.Price * p.Qty, 2);
qadData.xxscm_inv_receiver = p.Receiver;
qadData.xxscm_inv_taxt = p.Tax;
qadData.xxscm_inv_comments = "";
qadData.xxscm_inv_holdamt = 0;
qadData.xxscm_inv_acctuser = "";
qadData.xxscm_inv_date = _invoice.UpdateTime == null ? _invoice.CreateTime.Value.AddDays(1) : _invoice.UpdateTime.Value;
qadData.xxscm_inv_totamt = _invoice.Amount.Value;
qadData.xxscm_inv_tottax = _invoice.Total.Value;
4 years ago
qadData.xxscm_inv_totdiscount = _invoice.DiscountPrice == null ? 0 : _invoice.DiscountPrice.Value;
qadData.xxscm_inv_tottaxvar = _invoice.BlancePrice == null ? 0 : _invoice.BlancePrice.Value;
4 years ago
qadData.xxscm_inv_totclaim= _invoice.ContractPrice == null ? 0 : _invoice.ContractPrice.Value;
qadData.xxscm_inv_extend1 = _invoice.Extend1 == null ? 0 : _invoice.Extend1.Value;
qadData.xxscm_inv_extend2 = _invoice.Extend2 == null ? 0 : _invoice.Extend2.Value;
4 years ago
qadDataList.Add(qadData);
p.State = (int)BillState.Finish;
p.PutTime = DateTime.Now;
i++;
}
}
catch (Exception e)
{
string s = e.Message+"行号"+i.ToString();
}
}
});
idb.xxscm_inv_det.AddRange(qadDataList);
Console.WriteLine($"新增 发票 数据:{qadDataList.Count} 条");
PutScpCtrl(idb, seq, tableName, qadDataList.Count, _api.Domain, _api.Site);
}
}
}
}
}
catch (Exception e)
{
throw e;
}
}
}
}