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.
 
 
 
 
 

622 lines
27 KiB

using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Models.ScpEntity.ExcelImportEntity;
using CK.SCP.Utils;
using System.Data.Entity.Core;
using CK.SCP.Models.ScpEntity.ExcelExportEnttity;
using System.Text.RegularExpressions;
using System.Threading;
using System.Data;
using Newtonsoft.Json;
using CK.SCP.Models.UniApiEntity;
using CK.SCP.Models.AppBoxEntity;
namespace CK.SCP.Controller
{
public class SCP_MPO_CONTROLLER
{
/// <summary>
/// 获取一般材料订单
/// </summary>
/// <param name="p_entity"></param>
/// <param name="p_action"></param>
public static void Get_V_TB_MPO_List(V_TB_MPO p_entity, Action<ResultObject<IQueryable<V_TB_MPO>>> p_action)
{
ResultObject<IQueryable<V_TB_MPO>> _ret = new ResultObject<IQueryable<V_TB_MPO>>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
IQueryable<V_TB_MPO> q = db.V_TB_MPO;
if (p_entity.DocDateBegin != DateTime.MinValue)
{
q = q.Where(p => p.DocDate >= p_entity.DocDateBegin);
}
if (p_entity.DocDateEnd != DateTime.MinValue)
{
q = q.Where(p => p.DocDate <= p_entity.DocDateEnd);
}
if (!string.IsNullOrWhiteSpace(p_entity.PurdocNO))
{
q = q.Where(p => p.PurdocNO == p_entity.PurdocNO.Trim());
}
if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0)
{
q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendorNO));
}
if (!string.IsNullOrWhiteSpace(p_entity.VendorNO))
{
q = q.Where(p => p.VendorNO.Contains(p_entity.VendorNO));
}
if (!string.IsNullOrWhiteSpace(p_entity.VendorDesc))
{
q = q.Where(p => p.VendorDesc.Contains(p_entity.VendorDesc));
}
if (!string.IsNullOrWhiteSpace(p_entity.CreatedBy))
{
q = q.Where(p => p.CreatedBy.Contains(p_entity.CreatedBy));
}
if (!string.IsNullOrWhiteSpace(p_entity.AcceptStatusSelect))
{
var acceptStatus = int.Parse(p_entity.AcceptStatusSelect);
q = q.Where(p => p.AcceptStatus == acceptStatus);
}
_ret.State = ReturnStatus.Succeed;
_ret.Result = q;
p_action(_ret);
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(dbEx);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", sb.ToString());
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//并发冲突异常
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", ex.ToString());
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", ex.ToString());
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(e);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", e.Message);
throw e;
}
}
/// <summary>
/// 获取一般材料订单明细
/// </summary>
/// <param name="p_entity"></param>
/// <param name="p_action"></param>
public static void Get_V_TB_MPO_DETAIL_List(V_TB_MPO_DETAIL p_entity, Action<ResultObject<IQueryable<V_TB_MPO_DETAIL>>> p_action)
{
ResultObject<IQueryable<V_TB_MPO_DETAIL>> _ret = new ResultObject<IQueryable<V_TB_MPO_DETAIL>>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
IQueryable<V_TB_MPO_DETAIL> q = db.V_TB_MPO_DETAIL;
if (!string.IsNullOrWhiteSpace(p_entity.Ctype))
{
q = q.Where(p => p.Ctype == p_entity.Ctype);
}
if (p_entity.purdocnolist != null && p_entity.purdocnolist.Count > 0)
{
q = q.Where(p => p_entity.purdocnolist.Contains(p.PurdocNO));
}
_ret.State = ReturnStatus.Succeed;
_ret.Result = q;
p_action(_ret);
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(dbEx);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", sb.ToString());
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//并发冲突异常
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", ex.ToString());
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", ex.ToString());
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
_ret.ErrorList.Add(e);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", e.Message);
throw e;
}
}
/// <summary>
/// 一般材料订单明细列表
/// </summary>
/// <param name="p_lst">PurdocNOlist</param>
/// <returns></returns>
public static List<V_TB_MPO_DETAIL> GetMPODetailList(List<String> p_lst)
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
return db.V_TB_MPO_DETAIL.Where(p => p_lst.Contains(p.PurdocNO)).ToList();
}
}
/// <summary>
/// 作废传接口
/// </summary>
/// <param name="ls"></param>
/// <returns></returns>
public static ResultObject<bool> Save_ts_uni_api(List<string> ls)
{
ResultObject<bool> _ret = new ResultObject<bool>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
{
List<string> lineError = new List<string>();
List<string> ErrorList = new List<string>();
foreach (var po in ls)
{
var po1 = db.TB_MATERIALORDERS.FirstOrDefault(t => t.PurdocNO == po && t.Ctype != "D");
po1.AcceptStatus = (int)GeneralMaterialOrderState.Finish;
db.TB_MATERIALORDERS.AddOrUpdate(po1);
var po2 = db.TB_MATERIALORDERS_DETAIL.Where(t => t.PurdocNO == po && t.Ctype != "D").ToList();
foreach (var po3 in po2)
{
po3.AcceptStatus = (int)GeneralMaterialOrderState.Finish;
db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(po3);
}
var lx = db.TS_UNI_API.Where(t => t.BillNum == po && t.Receiver == "I").ToList();
foreach (var po4 in lx)
{
po4.State = 1;
po4.Receiver = "D";
db.TS_UNI_API.AddOrUpdate(po4);
}
}
}
if (_ret.MessageList.Count == 0)
{
int state = db.SaveChanges();
if (state != -1)
{
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
}
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
}
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(dbEx);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", sb.ToString());
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//并发冲突异常
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString());
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString());
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", e.Message);
_ret.Result = false;
_ret.ErrorList.Add(e);
throw e;
}
return _ret;
}
/// <summary>
/// 保存状态
/// </summary>
/// <param name="p_list"></param>
/// <param name="p_state"></param>
/// <returns></returns>
public static ResultObject<bool> Save_MPO_STATE(List<string> p_list, GeneralMaterialOrderState p_state)
{
ResultObject<bool> _ret = new ResultObject<bool>();
try
{
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
switch (p_state)
{
case GeneralMaterialOrderState.Finish:
var orblist = db.TB_MATERIALORDERS.Where(q => p_list.Contains(q.PurdocNO)).ToList();
var ctylist = orblist.Select(q => q.Ctype).ToList();
if (ctylist.Contains("D"))
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.Message = "存在已经作废的数据,请重新选择!";
_ret.MessageList.Add(_ret.Message);
return _ret;
}
var acclist = orblist.Select(q => q.AcceptStatus).ToList();
if (acclist.Contains((int)GeneralMaterialOrderState.Finish))
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.Message = "存在已经确认的数据,请重新选择!";
_ret.MessageList.Add(_ret.Message);
return _ret;
}
foreach (var item in p_list)
{
var tbMater = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == item);
tbMater.AcceptStatus = (int)GeneralMaterialOrderState.Finish;
var tbMaterList = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == item).ToList();
tbMaterList.ForEach(q =>
{
q.AcceptStatus = (int)GeneralMaterialOrderState.Finish;
});
db.TB_MATERIALORDERS.AddOrUpdate(itm1 => itm1.UID, tbMater);
db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(itm => itm.UID, tbMaterList.ToArray());
}
break;
case GeneralMaterialOrderState.New:
var MAblist = db.TB_MATERIALORDERS.Where(q => p_list.Contains(q.PurdocNO)).ToList();
var ctypelist = MAblist.Select(q => q.Ctype).ToList();
if (ctypelist.Contains("D"))
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.Message = "存在已经作废的数据,请重新选择!";
_ret.MessageList.Add(_ret.Message);
return _ret;
}
var acceplist = MAblist.Select(q => q.AcceptStatus).ToList();
if (acceplist.Contains((int)GeneralMaterialOrderState.New))
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.Message = "存在已经待确认的数据,请重新选择!";
_ret.MessageList.Add(_ret.Message);
return _ret;
}
foreach (var item in p_list)
{
var tbMater = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == item);
tbMater.AcceptStatus = (int)GeneralMaterialOrderState.New;
var tbMaterList = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == item).ToList();
tbMaterList.ForEach(q =>
{
q.AcceptStatus = (int)GeneralMaterialOrderState.New;
q.UpdateTime = DateTime.Now.ToString();
});
db.TB_MATERIALORDERS.AddOrUpdate(itm1 => itm1.UID, tbMater);
db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(itm => itm.UID, tbMaterList.ToArray());
}
break;
}
if (db.SaveChanges() != -1)
{
_ret.State = ReturnStatus.Succeed;
_ret.Result = true;
return _ret;
}
else
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.Message = "数据更新失败!";
_ret.MessageList.Add(_ret.Message);
return _ret;
}
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(dbEx);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", sb.ToString());
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//并发冲突异常
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", ex.ToString());
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
_ret.State = ReturnStatus.Failed;
_ret.Result = false;
_ret.ErrorList.Add(ex);
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", ex.ToString());
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
catch (Exception e)
{
_ret.State = ReturnStatus.Failed;
LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", e.Message);
_ret.Result = false;
_ret.ErrorList.Add(e);
}
return _ret;
}
/// <summary>
/// 获取材料订单主表数据
/// </summary>
/// <param name="PurdocNO"></param>
/// <returns></returns>
public static TB_MATERIALORDERS GetTbMaterialorders(string PurdocNO)
{
TB_MATERIALORDERS MATERIALORDERS = new TB_MATERIALORDERS();
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
MATERIALORDERS = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == PurdocNO);
}
return MATERIALORDERS;
}
/// <summary>
/// 获取材料订单明细表数据
/// </summary>
/// <param name="PurdocNO"></param>
/// <returns></returns>
public static List<TB_MATERIALORDERS_DETAIL> GetTbMaterialordersDetailList(string PurdocNO)
{
List<TB_MATERIALORDERS_DETAIL> MATERIALORDERS = new List<TB_MATERIALORDERS_DETAIL>();
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
MATERIALORDERS = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == PurdocNO).ToList();
}
if (MATERIALORDERS.Count() > 0)
{
MATERIALORDERS.ForEach(q =>
{
if (q.Ctype == "D")
{
q.CType_DESC = "是";
}
else
{
q.CType_DESC = "否";
}
});
}
return MATERIALORDERS;
}
/// <summary>
/// 打印数据
/// </summary>
/// <param name="p_AsnBillNum"></param>
/// <returns></returns>
public static DataSet MPO_REPORT(string PurdocNO)
{
DataSet ds = new DataSet();
TB_MATERIALORDERS order = GetTbMaterialorders(PurdocNO);
V_MPO_PRINT print = new V_MPO_PRINT();
print.PURDOCNO = order.PurdocNO;
print.DOCDATE = order.DocDate.ToString("yyyy-MM-dd");
print.PHONE = order.PHONE;
print.FAXNUM = order.FAXNUM;
print.PURGRPDESC = order.PurgrpDesc;
print.VendorNO = order.VendorNO;
print.FIRSTTELNO = order.FIRSTTELNO;
print.STAGEDESC = order.STAGEDESC;
TA_VENDER vender = GET_TA_VENDER(order.VendorNO);
if (vender != null)
{
print.CAAIFax = vender.VendName;
print.CurrentUserAdress = vender.Address;
print.RESPONSIBLESALESPERSON = vender.Contacter;
print.SupplierFax = vender.Fax;
}
List<TB_MATERIALORDERS_DETAIL> detailList = GetTbMaterialordersDetailList(PurdocNO);
List<V_MPO_PRINT_DETAIL> lsDetail = new List<V_MPO_PRINT_DETAIL>();
decimal SUM = 0.00M;
detailList.ForEach(p =>
{
V_MPO_PRINT_DETAIL _detail = new V_MPO_PRINT_DETAIL();
_detail.PURCHASEREQNO = p.PurchaseReqNO;
_detail.PROPOSERDESC = p.ProposerDesc;
_detail.MTLNO = p.MtlNO;
_detail.SHORTTEXT = p.Shorttext;
_detail.SIZECOL = p.SizeCol;
_detail.UNITDES = p.Unitdes;
_detail.QUANTITY = p.Quantity.ToString("#.####");
_detail.PRICE = p.PRICE.ToString("#.######");
_detail.NETVALUE = p.NetValue.ToString("#.######");
_detail.PURINFORECORD = p.PurinfoReCord;
_detail.ITEMDELIVERYDATE = p.ItemDeliveryDate.ToString("yyyy-MM-dd");
_detail.GLACCTNODES = p.Glaccnum;
_detail.COSTCENTERDES = p.CostCenterDes;
_detail.PURDOCITEMNO = p.PURDOCITEMNO;
_detail.CType_DESC = p.CType_DESC;
lsDetail.Add(_detail);
if (p.Ctype != "D")
{
SUM += p.NetValue;
}
});
print.Total = SUM.ToString("F6");
var detail = detailList.FirstOrDefault(q => !string.IsNullOrEmpty(q.Remake));
if (detail != null)
{
print.Remake = detail.Remake;
}
var dt = ConvertHelper.ToDataTable(new List<V_MPO_PRINT> { print });
ds.Tables.Add(dt);
var dt1 = ConvertHelper.ToDataTable(lsDetail);
ds.Tables.Add(dt1);
return ds;
}
/// <summary>
/// 获取打印相关的供应商信息
/// </summary>
/// <param name="VENDID"></param>
/// <returns></returns>
public static TA_VENDER GET_TA_VENDER (string VENDID)
{
TA_VENDER _VENDER = new TA_VENDER();
using (ScpEntities db = EntitiesFactory.CreateScpInstance())
{
_VENDER = db.TA_VENDER.FirstOrDefault(q => q.VendId == VENDID);
}
return _VENDER;
}
}
}