|
|
using ICSSoft.Common; using ICSSoft.Entity; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ICSSoft.DataProject { /// <summary>
/// 生产订单
/// </summary>
public class ManufactureOrder { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"]; private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"]; private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"]; /// <summary>
/// 查找生产订单
/// </summary>
/// <param name="infos"></param>
/// <returns></returns>
public string GET(List<ICSManufactureOrder> infos) {
DataTable dt = null; string json = ""; if (infos.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { string sql = string.Empty; foreach (ICSManufactureOrder info in infos) { if (info.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + info.MTime); sql = @" select a.MoId ,a.MoCode ,a.CreateUser ,a.CreateTime ,b.RelsUser ,b.RelsTime ,b.MoDId ,b.SortSeq,b.InvCode,b.Qty,b.AuxQty,b.QualifiedInQty,b.MDeptCode,
e.CDEPNAME, b.WhCode,f.cWhName,c.StartDate,c.DueDate FROM dbo.mom_order a INNER JOIN dbo.mom_orderdetail b ON a.MoId = b.MoId INNER JOIN dbo.mom_morder c ON b.MoDId = c.MoDId LEFT JOIN dbo.mom_orderdetail_extradefine d ON b.MoDId = d.MoDId INNER JOIN DEPARTMENT e ON b.MDeptCode=e.CDEPCODE INNER JOIN Warehouse f ON b.WhCode=f.cWhCode WHERE 1=1";
if (!string.IsNullOrWhiteSpace(info.MOCode)) { sql += " and a.MoCode='{0}'"; } if (!string.IsNullOrWhiteSpace(info.MTime.ToString())) { sql += " and ISNULL(b.cbCloseTime,ISNULL(a.cChangAuditTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime))))>='{1}'"; } if (!string.IsNullOrWhiteSpace(info.User)) { sql += "and a.CMAKER='{2}'"; } sql = string.Format(sql, info.MOCode, info.MTime, info.User); dt = DBHelper.SQlReturnData(sql, cmd); json = JsonConvert.SerializeObject(dt); if (dt.Rows.Count <= 0 || dt == null) throw new Exception("生产订单:" + info.MOCode + ",无信息!"); } cmd.Transaction.Commit(); return json; } catch (Exception ex) { cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } /// <summary>
/// 创建生产订单
/// </summary>
/// <param name="infos"></param>
/// <returns></returns>
public string CreateManufactureOrder(List<ICSManufactureOrder> Bills) { string msg=""; List<ICSManufactureOrder> szJson = new List<ICSManufactureOrder>(); DataTable dt = null; decimal iTaxRate = 0; DateTime time = DateTime.Now; int num = 0; foreach (ICSManufactureOrder head in Bills) { num = head.details.Count(); } Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "mom_order", "" + num + ""); int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString()); int iChildId = Convert.ToInt32(dic["iChildId"].ToString()); DateTime date = DateTime.Now; string iBaseCodeLen = DBHelper.GetAllRDCode("MO21", "" + time + "", "admin"); VouchKey key = new VouchKey(); //int iBaseCodeLen = Convert.ToInt32(dic["iBaseCodeLen"].ToString());
//int cVouchCodeBase = Convert.ToInt32(dic["cVouchCodeBase"].ToString());
List<ICSPOArrive> datas = new List<ICSPOArrive>(); string json = ""; if (Bills.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; foreach (ICSManufactureOrder head in Bills) { if (head.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + head.MTime); string sql = ""; #region 生产订单
sql = @"INSERT INTO mom_order(MoId, MoCode,cSysBarCode, CreateDate, CreateTime,
CreateUser, ModifyDate, ModifyTime, ModifyUser, UpdCount , VTid,RelsVTid) values (@MoId,@MoCode,NULL, CONVERT(VARCHAR(10),GETDATE(),23), GETDATE(), '@CreateUser', NULL, NULL, NULL, 0 , @VTID,0)" + Environment.NewLine;
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@MoId", iFatherId)); cmd.Parameters.Add(new SqlParameter("@MoCode", iBaseCodeLen)); cmd.Parameters.Add(new SqlParameter("@CreateUser",head.User)); cmd.Parameters.Add(new SqlParameter("@VTID", iChildId)); cmd.CommandText = sql; try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生成生产顶订单表头失败,受影响行数<=0;"); throw new Exception("生成生产顶订单表头失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); } #endregion
#region 采购入库单表体
foreach (ICSManufactureOrders body in head.details) { sql += @"INSERT INTO mom_orderdetail(MoDId, MoId, SortSeq, MoClass, MoTypeId,
Qty, MrpQty, AuxUnitCode, AuxQty, ChangeRate, MoLotCode, WhCode, MDeptCode, OrderType, OrderDId, OrderCode, OrderSeq, CustCode, DeclaredQty, QualifiedInQty, Status, OrgStatus, BomId, RoutingId, CustBomId, DemandId, PlanCode, PartId, InvCode,cbSysBarCode , Free1, Free2, Free3, Free4, Free5, Free6, Free7, Free8, Free9, Free10 , SfcFlag, CrpFlag, QcFlag,LeadTime,WIPType, SupplyWhCode,OpScheduleType,ReasonCode,Remark ,SourceQCId, SourceQCDId,SourceMoId,SourceMoDId,SourceQCVouchType,SourceSvcVouchType, PAllocateId ,RelsTime,AuditStatus,Iswfcontrolled,iVerifyState, iReturnCount ,CollectiveFlag,SoType,ReformFlag, RelsUser, RelsDate, FmFlag,BomType,RoutingType,OrdFlag, RunCardFlag,RequisitionFlag,AlloVTid,RelsAlloVTid) SELECT @MoDId, @MoId, @SortSeq, '1', NULL, @Qty, 0, NULL, NULL, NULL, NULL, NULL, @MDeptCode, '0', '0', NULL, NULL,NULL, '0', '0', '3', '2', a.BomId, '0', '0', '0', NULL, c.PartId, @InvCode,'' , '', '', '', '', '', '', '', '', '', '' , '0', '0', '0', '0','', NULL,'3',NULL,'','0', '0','0','0','0','0', '0',GETDATE(),'1','0','0', '0' ,'0','0','0',@RelsUser, NULL, '0' ,a.BomType,'0','0', '0','0',NULL,NULL FROM dbo.bom_bom a INNER JOIN dbo.bom_parent b ON a.BomId = b.BomId INNER JOIN dbo.bas_part c ON b.ParentId = c.PartId WHERE c.InvCode=@InvCode " + Environment.NewLine;
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@MoDId", iChildId)); cmd.Parameters.Add(new SqlParameter("@SortSeq",body.Sequence)); cmd.Parameters.Add(new SqlParameter("@MDeptCode", body.DepCode)); cmd.Parameters.Add(new SqlParameter("@Qty", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@InvCode", body.InvCode)); cmd.Parameters.Add(new SqlParameter("@RelsUser",head.User )); cmd.Parameters.Add(new SqlParameter("@MoId", iFatherId)); //cmd.Parameters.Add(new SqlParameter("@RelsDate", body.Quantity));
cmd.CommandText = sql; try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生成生产顶订单表头失败,受影响行数<=0;"); throw new Exception("生成生产顶订单表头失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); }
#region 生产订单资料
sql += @"INSERT INTO mom_morder(MoDId, MoId, StartDate, DueDate)
values (" + iChildId + ", " + iFatherId + ", '" + body.StartDate + "', '" + body.DueDate + "') " + Environment.NewLine;
cmd.CommandText = sql; try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生产订单资料表失败,受影响行数<=0;"); throw new Exception("生产订单资料表失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生产订单资料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); throw new Exception("生产订单资料表失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); } #endregion
#region 考虑虚拟件,如果包含虚拟件,取虚拟件的下一级,如果虚拟件的下级bom未维护,不做处理
sql += @"SELECT c.InvCode AS PInvCode,0 AS [Level], CAST(1 AS nvarchar(MAX)) AS SEQ,CAST(1 AS nvarchar(MAX)) AS Sort,
d.MoDId,a.SortSeq,a.OpSeq,a.ComponentId, a.FVFlag,a.BaseQtyN,a.BaseQtyD,'0' AS ParentScrap,a.CompScrap, CAST(d.Qty AS DECIMAL(28,6)) AS Qty,'0' AS IssQty,'0' AS DeclaredQty, '@StartDate' AS StartDemDate, '@DueDate' AS EndDemDate, NULL AS WhCode,NULL AS LotNo,e.WIPType,a.ByproductFlag,'0' AS QcFlag, '0' AS Offset,a.Free1,a.Free2,a.Free3, a.Free4,a.Free5,a.Free6,a.Free7,a.Free8, a.Free9,a.Free10,a.OpComponentId,a.Define22,a.Define23, a.Define24,a.Define25,a.Define26,a.Define27,a.Define28, a.Define29,a.Define30,a.Define31,a.Define32,a.Define33, a.Define34,a.Define35,a.Define36,a.Define37,'0' AS ReplenishQty, a.Remark,'0' AS TransQty,a.ProductType,'0' AS SoType,NULL AS SoDId, NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty, '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,'0' AS RequisitionFlag,'0' AS RequisitionQty, '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS MoallocateSubId,NULL AS cSubSysBarCode,'0' AS PickingQty, '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode INTO #TempBOMP" + iFatherId + @" FROM bas_part c INNER JOIN mom_orderdetail d ON c.PartId=d.PartId LEFT JOIN bom_parent b ON 1<>1 --b.ParentId=c.PartId LEFT JOIN bom_opcomponent a ON a.BomId=b.BomId LEFT JOIN bom_opcomponentopt e ON a.OptionsId=e.OptionsId WHERE d.MoDId=" + iChildId + @" ORDER BY d.MoDId ;
SELECT row_number() OVER (partition by c.InvCode ORDER BY c.InvCode,d.SortSeq) AS SEQ,c.InvCode AS PInvCode,f.InvCode AS InvCode, d.SortSeq,d.OpSeq,d.ComponentId, d.FVFlag,d.BaseQtyN,d.BaseQtyD,'0' AS ParentScrap,d.CompScrap, CAST(d.BaseQtyN/d.BaseQtyD AS DECIMAL(28,6)) AS Qty,'0' AS IssQty,'0' AS DeclaredQty, '@StartDate' AS StartDemDate, '@DueDate' AS EndDemDate, NULL AS WhCode,NULL AS LotNo,e.WIPType,d.ByproductFlag,'0' AS QcFlag, '0' AS Offset,d.Free1,d.Free2,d.Free3, d.Free4,d.Free5,d.Free6,d.Free7,d.Free8, d.Free9,d.Free10,d.OpComponentId,d.Define22,d.Define23, d.Define24,d.Define25,d.Define26,d.Define27,d.Define28, d.Define29,d.Define30,d.Define31,d.Define32,d.Define33, d.Define34,d.Define35,d.Define36,d.Define37,'0' AS ReplenishQty, d.Remark,'0' AS TransQty,d.ProductType,'0' AS SoType,NULL AS SoDId, NULL AS SoCode,NULL AS SoSeq,NULL AS DemandCode,'0' AS QmFlag,'0' AS OrgQty, '0' AS OrgAuxQty,NULL AS CostItemCode,NULL AS CostItemName,'0' AS RequisitionFlag,'0' AS RequisitionQty, '0' AS RequisitionIssQty,'0' AS CostWIPRel,NULL AS MoallocateSubId,NULL AS cSubSysBarCode,'0' AS PickingQty, '0' AS PickingAuxQty,'0' AS UpperMoQty,'0' AS InvAlloeFlag,NULL AS FactoryCode INTO #TempBOM" + iFatherId + @" FROM dbo.bom_bom a INNER JOIN dbo.bom_parent b on a.BomId = b.BomId INNER JOIN dbo.bas_part c on b.ParentId = c.PartId INNER JOIN dbo.bom_opcomponent d on a.BomId=d.BomId INNER JOIN dbo.bom_opcomponentopt e ON d.OptionsId=e.OptionsId INNER JOIN dbo.bas_part f on d.ComponentId = f.PartId WHERE a.VersionEndDate>=CONVERT(VARCHAR(10),GETDATE(),23) ORDER BY c.InvCode,d.SortSeq ; WITH ICSBomALL AS ( SELECT PInvCode AS TInvCode, PInvCode AS PInvCode, PInvCode AS InvCode, --CAST('1' AS tinyint) AS WIPType, [Level], SEQ, CAST(REPLICATE('0',5-len(Sort))+Sort AS nvarchar(MAX)) AS Sort, MoDId,SortSeq,OpSeq,ComponentId, FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap, CAST(Qty AS DECIMAL(28,6)) AS Qty,IssQty,DeclaredQty, StartDemDate, EndDemDate, WhCode,LotNo,WIPType,ByproductFlag,QcFlag, Offset,Free1,Free2,Free3, Free4,Free5,Free6,Free7,Free8, Free9,Free10,OpComponentId,Define22,Define23, Define24,Define25,Define26,Define27,Define28, Define29,Define30,Define31,Define32,Define33, Define34,Define35,Define36,Define37,ReplenishQty, Remark,TransQty,ProductType,SoType,SoDId, SoCode,SoSeq,DemandCode,QmFlag,OrgQty, OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty, RequisitionIssQty,CostWIPRel,MoallocateSubId,cSubSysBarCode,PickingQty, PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode FROM #TempBOMP" + iFatherId + @" UNION ALL SELECT b.TInvCode, a.PInvCode, a.InvCode, --a.WIPType, b.[Level]+1 AS [Level], CAST(b.SEQ AS nvarchar(MAX))+'.'+CAST(a.SEQ AS nvarchar(MAX)) AS SEQ, CAST(b.Sort+'.'+REPLICATE('0',5-len(a.SEQ))+CAST(a.SEQ AS nvarchar(MAX)) AS nvarchar(MAX)) AS Sort, b.MoDId,a.SortSeq,a.OpSeq,a.ComponentId, a.FVFlag,a.BaseQtyN,a.BaseQtyD,a.ParentScrap,a.CompScrap, CAST(a.Qty*b.Qty AS DECIMAL(28,6)) AS Qty,a.IssQty,a.DeclaredQty, a.StartDemDate, a.EndDemDate, a.WhCode,a.LotNo,a.WIPType,a.ByproductFlag,a.QcFlag, a.Offset,a.Free1,a.Free2,a.Free3, a.Free4,a.Free5,a.Free6,a.Free7,a.Free8, a.Free9,a.Free10,a.OpComponentId,a.Define22,a.Define23, a.Define24,a.Define25,a.Define26,a.Define27,a.Define28, a.Define29,a.Define30,a.Define31,a.Define32,a.Define33, a.Define34,a.Define35,a.Define36,a.Define37,a.ReplenishQty, a.Remark,a.TransQty,a.ProductType,a.SoType,a.SoDId, a.SoCode,a.SoSeq,a.DemandCode,a.QmFlag,a.OrgQty, a.OrgAuxQty,a.CostItemCode,a.CostItemName,a.RequisitionFlag,a.RequisitionQty, a.RequisitionIssQty,a.CostWIPRel,a.MoallocateSubId,a.cSubSysBarCode,a.PickingQty, a.PickingAuxQty,a.UpperMoQty,a.InvAlloeFlag,a.FactoryCode FROM #TempBOM" + iFatherId + @" a INNER JOIN ICSBomALL b ON a.PInvCode=b.InvCode --过滤:只显示一级,如果是虚拟件,查询下一级别 WHERE b.[Level]='0' OR b.WIPType='4' ) SELECT row_number() OVER (ORDER BY Sort) AS RowIndex,* INTO #TempMOAll" + iFatherId + @" FROM ICSBomALL WHERE [Level]<>'0' AND WIPType<>'4' ORDER BY Sort";
//更新临时表的ID
// sql += @" DECLARE @CurrentRow" + iFatherId + @" INT,@TotalRow" + iFatherId + @" INT
// SET @TotalRow" + iFatherId + @"=@@rowcount
// SET @CurrentRow" + iFatherId + @"=1
// WHILE @CurrentRow" + iFatherId + @"<=@TotalRow" + iFatherId + @"
// BEGIN
// " + ICSHelper.GetIDSql(cCompanyCode, "mom_moallocate", appmokey, GetID.ALL) + @"
// UPDATE #TempMOAll" + appskey + @" SET RowIndex=" + baseInt + "+@DID" + appmokey + @" WHERE RowIndex=@CurrentRow" + appskey + @"
// SET @CurrentRow" + appskey + @"+=1
// END
// " + Environment.NewLine;
sql += @" INSERT INTO mom_moallocate (AllocateId,MoDId,SortSeq,OpSeq,ComponentId,
FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap, Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate, WhCode,LotNo,WIPType,ByproductFlag,QcFlag, Offset,InvCode,Free1,Free2,Free3, Free4,Free5,Free6,Free7,Free8, Free9,Free10,OpComponentId,Define22,Define23, Define24,Define25,Define26,Define27,Define28, Define29,Define30,Define31,Define32,Define33, Define34,Define35,Define36,Define37,ReplenishQty, Remark,TransQty,ProductType,SoType,SoDId, SoCode,SoSeq,DemandCode,QmFlag,OrgQty, OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty, RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty, PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode) SELECT RowIndex,MoDId,SortSeq,OpSeq,ComponentId, FVFlag,BaseQtyN,BaseQtyD,ParentScrap,CompScrap, Qty,IssQty,DeclaredQty,StartDemDate,EndDemDate, WhCode,LotNo,WIPType,ByproductFlag,QcFlag, Offset,InvCode,Free1,Free2,Free3, Free4,Free5,Free6,Free7,Free8, Free9,Free10,OpComponentId,Define22,Define23, Define24,Define25,Define26,Define27,Define28, Define29,Define30,Define31,Define32,Define33, Define34,Define35,Define36,Define37,ReplenishQty, Remark,TransQty,ProductType,SoType,SoDId, SoCode,SoSeq,DemandCode,QmFlag,OrgQty, OrgAuxQty,CostItemCode,CostItemName,RequisitionFlag,RequisitionQty, RequisitionIssQty,CostWIPRel,cSubSysBarCode,PickingQty, PickingAuxQty,UpperMoQty,InvAlloeFlag,FactoryCode FROM #TempMOAll" + iFatherId + Environment.NewLine;
sql += @" DROP TABLE #TempMOAll" + iFatherId + Environment.NewLine; sql += @" DROP TABLE #TempBOMP" + iFatherId + Environment.NewLine; sql += @" DROP TABLE #TempBOM" + iFatherId + Environment.NewLine; cmd.CommandText = sql; try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生成生产顶订单表头失败,受影响行数<=0;"); throw new Exception("生成生产顶订单表头失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); throw new Exception("生成生产顶订单表头失败!异常:" + ex.Message + ";SQL:\r\n" + sql, ex); } #endregion
} #endregion
////更新主键ID,DID
//DBHelper.UpdateIDandDID("rd", head.list.Count(), cmd);
} return msg; } /// <summary>
/// 删除生产订单
/// </summary>
/// <param name="infos"></param>
/// <returns></returns>
public string Delete(List<ICSManufactureOrder> infos) { List<ICSManufactureOrder> szJson = new List<ICSManufactureOrder>(); if (infos.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { string sql = string.Empty; foreach (ICSManufactureOrder info in infos) { if (info.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + info.MTime); sql = @" DELETE [{1}].dbo.mom_order WHERE ID={0}"; sql = string.Format(sql, info.ID, ERPDB); DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!"); } cmd.Transaction.Commit(); return res; ; } catch (Exception ex) { cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } } }
|