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

314 lines
19 KiB

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