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

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