纽威
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.

355 lines
22 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.DataProject
  11. {
  12. public class CreateAssemVouch
  13. {
  14. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  15. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  16. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  17. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["AssemCRDNAME"];
  18. /// <summary>
  19. /// 生成形态转换单
  20. /// </summary>
  21. /// <param name="Bills"></param>
  22. /// <returns></returns>
  23. public string Create(List<ICSAssemVouch> Bills)
  24. {
  25. string msg = "";
  26. string connS = "";
  27. SqlConnection conn = new SqlConnection();
  28. SqlCommand cmd = new SqlCommand();
  29. DataTable dtNew1 = null;
  30. DataTable dtNew2 = null;
  31. if (Bills.Count <= 0)
  32. {
  33. throw new Exception("传送数据为空!");
  34. }
  35. string sql = "";
  36. int num = 0;
  37. foreach (ICSAssemVouch head in Bills)
  38. {
  39. try
  40. {
  41. string iFatherIdTwo = "";
  42. connS = string.Format(connString, head.WorkPoint);
  43. conn = new System.Data.SqlClient.SqlConnection(connS);
  44. conn.Open();
  45. SqlTransaction sqlTran = conn.BeginTransaction();
  46. cmd = new SqlCommand();
  47. cmd.Transaction = sqlTran;
  48. cmd.Connection = conn;
  49. string[] ss = head.WorkPoint.Split('_');
  50. ERPDB = ss[1];
  51. string[] dd = CRNALL.Split('~');
  52. string crdname1 = dd[0];
  53. string crdname2 = dd[1];
  54. string carname = dd[2];
  55. string surface = dd[3];
  56. string carname1 = dd[4];
  57. string carname2 = dd[5];
  58. string surface1 = dd[6];
  59. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  60. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  61. string cardnewcode2 = DBHelper.GetCardNumber(carname2, cmd);
  62. string cRDcode08 = DBHelper.GetRDCode(crdname1, cmd);
  63. string cRDcode09 = DBHelper.GetRDCode(crdname2, cmd);
  64. ICSUserInfo userInfo = new ICSUserInfo();
  65. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  66. #region 形态转换单
  67. #region 形态转换单表头
  68. //形态转换单CartonNo:0305
  69. //取得out库单的默认显示模版
  70. string DEF_ID07 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  71. //取得形态转换入库单单据 表头ID,表体DID
  72. num = head.details.Count();
  73. Dictionary<string, int> dic = DBHelper.GetAllCode(ERPDB, surface, num.ToString(), head.WorkPoint);
  74. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  75. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  76. DateTime date = DateTime.Now;
  77. // 取得表头单据号
  78. string Code07 = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  79. sql = @"INSERT INTO AssemVouch
  80. (cAVCode,cVouchType,dAVDate,cDepCode,cIRdCode,cORdCode,cMaker,iNetLock,
  81. ID,bTransFlag,VT_ID,cVerifyPerson,dVerifyDate,
  82. dnmaketime,dnverifytime,iPrintCount,ctransflag)
  83. VALUES
  84. (@cAVCode,'15',CONVERT(NVARCHAR(15),GETDATE(),23),@cDepCode,@cIRdCode,@cORdCode,@cMaker,0,
  85. @ID,'0',@VT_ID,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  86. GETDATE(),GETDATE(),0,1)
  87. ";
  88. cmd.Parameters.Clear();
  89. cmd.Parameters.Add(new SqlParameter("@cAVCode", Code07));
  90. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  91. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  92. cmd.Parameters.Add(new SqlParameter("@cIRdCode", cRDcode08));
  93. cmd.Parameters.Add(new SqlParameter("@cORdCode", cRDcode09));
  94. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  95. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID07));
  96. cmd.CommandText = sql;
  97. DBHelper.CmdExecuteNonQuery(sql, cmd, "形态转换单表头保存失败!");
  98. #endregion
  99. #region 其他出库单表头
  100. //取得out库单的默认显示模版
  101. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode2, cmd);
  102. //取得表头ID,表体DID
  103. Dictionary<string, int> dic09 = DBHelper.GetAllCode(ERPDB, surface1, num.ToString(), head.WorkPoint);
  104. int iFatherId09 = Convert.ToInt32(dic09["iFatherId"].ToString());
  105. int iChildId09 = Convert.ToInt32(dic09["iChildId"].ToString());
  106. // 取得表头单据号
  107. string Code09 = DBHelper.GetAllRDCode(cardnewcode2, "" + date + "", "admin", head.WorkPoint);
  108. sql = @"INSERT INTO dbo.RdRecord09
  109. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  110. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  111. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  112. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,ctransflag,cDepCode)
  113. SELECT @ID,'0','09','','',@cBusCode,@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRDcode,
  114. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  115. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,'1',cDepCode from dbo.AssemVouch WHERE cAVCode='" + Code07 + "' ";
  116. cmd.Parameters.Clear();
  117. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId09));
  118. cmd.Parameters.Add(new SqlParameter("@cBusCode", Code07));
  119. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.OutWhCode));
  120. cmd.Parameters.Add(new SqlParameter("@cCode", Code09.ToString().PadLeft(10, '0')));
  121. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  122. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  123. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  124. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  125. cmd.Parameters.Add(new SqlParameter("@cRDcode", cRDcode09));
  126. cmd.CommandText = sql;
  127. DBHelper.CmdExecuteNonQuery(sql, cmd, "其他出库单表头保存失败!");
  128. #endregion
  129. #region 其他入库单表头
  130. //取得out库单的默认显示模版
  131. string DEF_ID08 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  132. //取得入库单单据 表头ID,表体DID
  133. Dictionary<string, int> dic08 = DBHelper.GetAllCode(ERPDB, surface1, num.ToString(), head.WorkPoint);
  134. int iFatherId08 = Convert.ToInt32(dic08["iFatherId"].ToString());
  135. int iChildId08 = Convert.ToInt32(dic08["iChildId"].ToString());
  136. // 取得表头单据号
  137. string Code08 = DBHelper.GetAllRDCode(cardnewcode1, "" + date + "", "admin", head.WorkPoint);
  138. sql = @"INSERT INTO dbo.RdRecord08
  139. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  140. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  141. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  142. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,ctransflag,cDepCode)
  143. SELECT @ID,'1','08','','',@cBusCode,@cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRDcode,
  144. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  145. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,'1',cDepCode from dbo.AssemVouch WHERE cAVCode='" + Code07 + "' ";
  146. cmd.Parameters.Clear();
  147. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId08));
  148. cmd.Parameters.Add(new SqlParameter("@cBusCode", Code07));
  149. cmd.Parameters.Add(new SqlParameter("@cIWhCode", head.InWhCode));
  150. cmd.Parameters.Add(new SqlParameter("@cCode", Code08));
  151. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  152. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  153. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  154. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  155. cmd.Parameters.Add(new SqlParameter("@cRDcode", cRDcode08));
  156. cmd.CommandText = sql;
  157. DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表头保存失败!");
  158. #endregion
  159. int Rd08irowno = 0;
  160. int Rd09irowno = 0;
  161. foreach (ICSAssemVouchs body in head.details)
  162. {
  163. #region 形态转换单转换前表体
  164. sql = @"INSERT INTO dbo.AssemVouchs
  165. (ID,cAVCode,bAVType,cWhCode,cInvCode,
  166. iAVQuantity,cAVBatch,autoID,bCosting,iExpiratDateCalcu,
  167. irowno,iGroupNO)
  168. VALUES(@ID,@cAVCode,@bAVType,@cWhCode,@cInvCode,
  169. @iAVQuantity,@cAVBatch,@autoID,0,0,
  170. @irowno,@iGroupNO)";
  171. cmd.Parameters.Clear();
  172. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  173. cmd.Parameters.Add(new SqlParameter("@cAVCode", Code07));
  174. cmd.Parameters.Add(new SqlParameter("@bAVType", body.Type));
  175. cmd.Parameters.Add(new SqlParameter("@cWhCode", body.WHCode));
  176. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  177. cmd.Parameters.Add(new SqlParameter("@iAVQuantity", body.Quantity));
  178. cmd.Parameters.Add(new SqlParameter("@cAVBatch", body.BatchCode));
  179. cmd.Parameters.Add(new SqlParameter("@autoID", iChildId));
  180. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  181. cmd.Parameters.Add(new SqlParameter("@iGroupNO", body.GroupNO));
  182. cmd.CommandText = sql;
  183. DBHelper.CmdExecuteNonQuery(sql, cmd, "形态转换单表体保存失败!");
  184. #endregion
  185. if (body.Type == "转换前")
  186. {
  187. Rd09irowno += 1;
  188. #region 其他出库单表体
  189. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  190. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + "'and cWhCode='" + head.OutWhCode + "'";
  191. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + @"'AND cFree1='" + body.cFree1 + @"' AND cFree2='" + body.cFree2 + @"' AND cFree3='" + body.cFree3 + @"' AND cFree4='" + body.cFree4 + @"' AND cFree5='" + body.cFree5 + @"' AND cFree6='" + body.cFree6 + @"' AND cFree7='" + body.cFree7 + @"' AND cFree8='" + body.cFree8 + @"' AND cFree9='" + body.cFree9 + @"' AND cFree10='" + body.cFree10 + @"' and cWhCode='" + head.OutWhCode + "'";
  192. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  193. if (dtItem != null && dtItem.Rows.Count > 0)
  194. {
  195. if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.BatchCode))
  196. {
  197. throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode);
  198. }
  199. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  200. {
  201. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  202. }
  203. }
  204. else
  205. {
  206. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  207. }
  208. sql = @"INSERT INTO dbo.rdrecords09
  209. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,
  210. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iGroupNO,iPPrice)
  211. SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,null,@iNum,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,
  212. '0','0','0','0','1','0','0','0',@irowno,null,@cTVBatch,iAVPCost,'1',iAVPPrice from AssemVouchs where autoID=" + iChildId + @" ;";
  213. cmd.Parameters.Clear();
  214. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId09));
  215. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId09));
  216. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  217. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  218. cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode));
  219. cmd.Parameters.Add(new SqlParameter("@iTrIds", iChildId));
  220. cmd.Parameters.Add(new SqlParameter("@irowno", Rd09irowno));
  221. cmd.CommandText = sql;
  222. DBHelper.CmdExecuteNonQuery(sql, cmd, "其他出库单表体保存失败!");
  223. sql = @"INSERT INTO dbo.rdrecords09sub
  224. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  225. values('" + iChildId09 + "','" + iFatherId09 + "',0,0)";
  226. cmd.CommandText = sql;
  227. cmd.ExecuteNonQuery();
  228. #endregion
  229. if (head.UpdateStock)
  230. {
  231. #region 更新现存量
  232. VouchKey key09 = new VouchKey();
  233. key09.cBustypeUN = "转换出库";
  234. key09.cVouchTypeUN = "09";
  235. key09.TableName = "IA_ST_UnAccountVouch09";
  236. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.OutWhCode, body.BatchCode, -body.Quantity, key09);
  237. DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.OutWhCode, body.BatchCode, -body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key09);
  238. #endregion
  239. }
  240. }
  241. else
  242. {
  243. Rd08irowno += 1;
  244. #region 其他入库单表体
  245. sql = @"INSERT INTO dbo.rdrecords08
  246. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,
  247. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,iGroupNO)
  248. SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,@iAVQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,
  249. '0','0','0','0','1','0','0','0',@irowno,null,@cTVBatch,iAVPCost,iAVPPrice,'1' from AssemVouchs where autoID=" + iChildId + @" ;";
  250. cmd.Parameters.Clear();
  251. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId08));
  252. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId08));
  253. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  254. cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode));
  255. cmd.Parameters.Add(new SqlParameter("@iTrIds", iChildId));
  256. cmd.Parameters.Add(new SqlParameter("@iAVQuantity", body.Quantity));
  257. cmd.Parameters.Add(new SqlParameter("@irowno", Rd08irowno));
  258. cmd.CommandText = sql;
  259. DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表体保存失败!");
  260. #endregion
  261. if (head.UpdateStock)
  262. {
  263. #region 更新现存量
  264. VouchKey key08 = new VouchKey();
  265. key08.cBustypeUN = "转换入库";
  266. key08.cVouchTypeUN = "08";
  267. key08.TableName = "IA_ST_UnAccountVouch08";
  268. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.InWhCode, body.BatchCode, body.Quantity, key08);
  269. DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.InWhCode, body.BatchCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key08);
  270. #endregion
  271. }
  272. }
  273. iFatherIdTwo += "'" + iFatherId + "',";
  274. iChildId--;
  275. iChildId09--;
  276. iChildId08--;
  277. }
  278. #endregion
  279. #region 查询
  280. sql = @"SELECT a.ID AS Costre,a.ID,b.cAVCode AS MTCode,a.cDepCode AS DepCode,'' AS DepName,a.cMaker AS CreateUser,a.dnmaketime AS CreateDateTime,a.cVerifyPerson AS Checker,a.dnverifytime AS CheckDateTime,
  281. b.autoID AS DetailID,b.irowno AS Sequence,b.iGroupNO AS GroupNO,b.bAVType AS Type,b.cInvCode AS InvCode,b.cWhCode AS WHCode,b.iAVQuantity AS Quantity,0 AS Amount,b.cAVBatch AS BatchCode,'' AS ProjectCode
  282. INTO #TempERP
  283. FROM AssemVouch a
  284. INNER JOIN AssemVouchs b ON a.ID=b.ID
  285. WHERE a.ID IN({0})
  286. SELECT DISTINCT Costre,ID,MTCode,DepCode,DepName,CreateUser,CreateDateTime,Checker,CheckDateTime FROM #TempERP
  287. SELECT Costre,DetailID,Sequence,GroupNO,Type,InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount
  288. FROM #TempERP
  289. DROP TABLE #TempERP";
  290. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  291. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  292. if (dtNew1 == null)
  293. dtNew1 = ds.Tables[0];
  294. else
  295. dtNew1.Merge(ds.Tables[0]);
  296. if (dtNew2 == null)
  297. dtNew2 = ds.Tables[1];
  298. else
  299. dtNew2.Merge(ds.Tables[1]);
  300. #endregion
  301. cmd.Transaction.Commit();
  302. }
  303. catch (Exception ex)
  304. {
  305. cmd.Transaction.Rollback();
  306. log.Error(ex.Message);
  307. throw new Exception(ex.Message);
  308. }
  309. finally
  310. {
  311. if (conn.State == ConnectionState.Open)
  312. {
  313. conn.Close();
  314. }
  315. conn.Dispose();
  316. }
  317. }
  318. string RelationName = "details";
  319. DataSet dz = new DataSet();
  320. dtNew1.TableName = "Table0";
  321. dtNew2.TableName = "Table1";
  322. dz.Tables.Add(dtNew1.Copy());
  323. dz.Tables.Add(dtNew2.Copy());
  324. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  325. dz.Relations.Add(dh);
  326. msg = DBHelper.DataSetToJson(dz, RelationName);
  327. return msg;
  328. }
  329. }
  330. }