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

1515 lines
94 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  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. /// <summary>
  14. /// 调拨单
  15. /// </summary>
  16. public class TransferDoc
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["TransDCRDNAME"];
  23. /// <summary>
  24. /// 审核调拨单
  25. /// </summary>
  26. /// <param name="Bills"></param>
  27. /// <returns></returns>
  28. public bool ConfirmTrans(List<ICSTransferDoc> Bills)
  29. {
  30. bool ResultFlag = false;
  31. string connS = "";
  32. //int num = 0;
  33. SqlConnection conn = new SqlConnection();
  34. SqlCommand cmd = new SqlCommand();
  35. if (Bills.Count <= 0)
  36. {
  37. throw new Exception("传送数据为空!");
  38. }
  39. LogInfo(Bills);
  40. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  41. foreach (string WorkPoint in result)
  42. {
  43. try
  44. {
  45. connS = string.Format(connString, WorkPoint);
  46. conn = new System.Data.SqlClient.SqlConnection(connS);
  47. conn.Open();
  48. SqlTransaction sqlTran = conn.BeginTransaction();
  49. cmd = new SqlCommand();
  50. cmd.Transaction = sqlTran;
  51. cmd.Connection = conn;
  52. foreach (ICSTransferDoc head in Bills)
  53. {
  54. if (WorkPoint != head.WorkPoint)
  55. continue;
  56. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  57. throw new Exception("U8正在整理现存量,请稍后再试");
  58. string[] ss = head.WorkPoint.Split('_');
  59. ERPDB = ss[1];
  60. string sql = "";
  61. ICSUserInfo userInfo = new ICSUserInfo();
  62. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  63. string cIWhCode = string.Empty;
  64. string cOWhCode = string.Empty;
  65. #region 检查
  66. sql = "select * from dbo.TransVouch where ID='" + head.ID + "';";
  67. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  68. if (dtCheck != null && dtCheck.Rows.Count > 0)
  69. {
  70. string cHandler = dtCheck.Rows[0]["cVerifyPerson"].ToString();
  71. if (!string.IsNullOrEmpty(cHandler))
  72. {
  73. throw new Exception("调拨单ID:" + head.ID + "不是开立状态!");
  74. }
  75. cIWhCode = dtCheck.Rows[0]["cIWhCode"].ToString();
  76. cOWhCode = dtCheck.Rows[0]["cOWhCode"].ToString();
  77. sql = string.Format("select * from Warehouse where cWhCode in ('{0}','{1}')", dtCheck.Rows[0]["cOWhCode"].ToString(), dtCheck.Rows[0]["cIWhCode"].ToString());
  78. DataTable dtWh = DBHelper.SQlReturnData(sql, cmd);
  79. if (dtWh != null && dtWh.Rows.Count > 1)
  80. {
  81. }
  82. else
  83. {
  84. throw new Exception("调拨单中转入或者转出仓库不存在!");
  85. }
  86. }
  87. else
  88. {
  89. throw new Exception("调拨单ID:" + head.ID + "在U8中不存在!");
  90. }
  91. #endregion
  92. #region 审核
  93. sql = "UPDATE dbo.TransVouch SET cVerifyPerson='" + userInfo.UserName + "' ,dVerifyDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  94. cmd.CommandText = sql;
  95. try
  96. {
  97. cmd.ExecuteNonQuery();
  98. }
  99. catch (Exception ex)
  100. {
  101. log.Error("审核调拨单失败!异常:" + ex.Message + ";\r\n SQL:" + sql);
  102. throw new Exception("程序异常,请联系开发人员!");
  103. }
  104. int bodyID = 0;
  105. // foreach (ICSTransferDoc body in Bills)
  106. // {
  107. // //回写调出仓库待发数量
  108. // string fOutcBatch = string.Empty;
  109. // //bodyID = Convert.ToInt32(body);
  110. // sql = @"UPDATE a SET fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0)
  111. // FROM dbo.CurrentStock a
  112. // LEFT JOIN (SELECT a.cOWhCode,a.cIWhCode,b.cInvCode,b.autoID,
  113. // ISNULL(b.cTVBatch,'') AS cTVBatch FROM dbo.TransVouch a
  114. // INNER JOIN dbo.TransVouchs b ON a.cTVCode = b.cTVCode)b
  115. // ON a.cInvCode=b.cInvCode AND a.cBatch=b.cTVBatch and a.cWhCode=b.cOWhCode WHERE b.autoID=@bodyID";
  116. // cmd.Parameters.Clear();
  117. // //cmd.Parameters.Add(new SqlParameter("@Qty", dtCheck.Rows[0]["iQuantity"].ToString()));
  118. // cmd.Parameters.Add(new SqlParameter("@bodyID",body.ID));
  119. // cmd.CommandText = sql;
  120. // try
  121. // {
  122. // cmd.ExecuteNonQuery();
  123. // }
  124. // catch (Exception ex)
  125. // {
  126. // log.Error("回写现存量表 待调拨数量!异常:" + ex.Message + ";\r\n SQL:" + sql);
  127. // throw new Exception("程序异常,请联系开发人员!");
  128. // }
  129. //// #region 判断现存量是否足够
  130. //// sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransOutQuantity<0)
  131. //// BEGIN
  132. //// DECLARE @MSG NVARCHAR(100)
  133. //// SELECT @MSG='ERP待调拨出库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransOutQuantity<0
  134. //// RAISERROR(@MSG,16,1)
  135. //// END";
  136. //// cmd.CommandText = sql;
  137. //// cmd.ExecuteNonQuery();
  138. //// #endregion
  139. // //回写调入仓库在途数量
  140. // fOutcBatch = string.Empty;
  141. // sql = @"UPDATE a SET fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)
  142. // FROM dbo.CurrentStock a
  143. // LEFT JOIN (SELECT a.cOWhCode,a.cIWhCode,b.cInvCode,b.autoID,
  144. // ISNULL(b.cTVBatch,'') AS cTVBatch FROM dbo.TransVouch a
  145. // INNER JOIN dbo.TransVouchs b ON a.cTVCode = b.cTVCode)b
  146. // ON a.cInvCode=b.cInvCode AND a.cBatch=b.cTVBatch and a.cWhCode=b.cIWhCode WHERE b.autoID=@bodyID";
  147. // cmd.Parameters.Clear();
  148. // //cmd.Parameters.Add(new SqlParameter("@Qty", dtCheck.Rows[0]["iQuantity"].ToString()));
  149. // cmd.Parameters.Add(new SqlParameter("@bodyID", body.ID));
  150. // cmd.CommandText = sql;
  151. // try
  152. // {
  153. // cmd.ExecuteNonQuery();
  154. // }
  155. // catch (Exception ex)
  156. // {
  157. // log.Error("回写现存量表在途数量!异常:" + ex.Message + ";\r\n SQL:" + sql);
  158. // throw new Exception("程序异常,请联系开发人员!");
  159. // }
  160. //// #region 判断现存量是否足够
  161. //// sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransInQuantity<0)
  162. //// BEGIN
  163. //// DECLARE @MSG NVARCHAR(100)
  164. //// SELECT @MSG='ERP待调拨入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransInQuantity<0
  165. //// RAISERROR(@MSG,16,1)
  166. //// END";
  167. //// cmd.CommandText = sql;
  168. //// cmd.ExecuteNonQuery();
  169. //// #endregion
  170. // }
  171. #endregion
  172. //0305 形态转换单CartonNum
  173. sql = "select * from dbo.TransVouchs where ID='" + head.ID + "';";
  174. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  175. #region 其他入库单
  176. //取得out库单的默认显示模版
  177. string[] dd = CRNALL.Split('~');
  178. string crdname = dd[2];
  179. string crdname1 = dd[3];
  180. string carname = dd[4];
  181. string carname1 = dd[5];
  182. string surface = dd[6];
  183. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  184. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  185. VouchKey key08 = new VouchKey();
  186. //key08 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  187. //// 取得表头单据号
  188. //string Code08 = U8Helper.GetRdrecord08Code(ErpName, "QR", cmd);
  189. var num = dtChecks.Rows.Count;
  190. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  191. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  192. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  193. DateTime date = DateTime.Now;
  194. string cRDcode08 = DBHelper.GetRDCode(crdname1, cmd);
  195. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode1, "" + date + "", "admin", cRDcode08, head.WorkPoint);
  196. string DEF_ID08 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  197. #region 其他入库单表头
  198. sql = @"INSERT INTO dbo.RdRecord08
  199. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  200. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  201. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  202. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  203. SELECT @ID,'1','08','','',a.cTVCode,cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),a.cTVCode,@cRdCode,
  204. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  205. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cIDepCode from dbo.TransVouch a WHERE ID='" + head.ID + "' ";
  206. cmd.Parameters.Clear();
  207. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  208. //cmd.Parameters.Add(new SqlParameter("@cBusCode", head.ID));
  209. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  210. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  211. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  212. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  213. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  214. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode08));
  215. cmd.CommandText = sql;
  216. try
  217. {
  218. cmd.ExecuteNonQuery();
  219. }
  220. catch (Exception ex)
  221. {
  222. log.Error("生成其他入库单表头失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  223. throw new Exception("程序异常,请联系开发人员!");
  224. }
  225. #endregion
  226. #region 3.5 其他入库单表体
  227. for (int i = 0; i < dtChecks.Rows.Count; i++)
  228. {
  229. #region 3.5.1 表体
  230. sql = @"INSERT INTO dbo.rdrecords08
  231. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,
  232. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  233. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,
  234. '0','0','0','0','1','0','0','0',@irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  235. from TransVouchs a
  236. inner join TransVouch b on a.ID=b.ID
  237. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"';";
  238. cmd.Parameters.Clear();
  239. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  240. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  241. cmd.Parameters.Add(new SqlParameter("@irowno", dtChecks.Rows[i]["irowno"].ToString()));
  242. cmd.CommandText = sql;
  243. try
  244. {
  245. cmd.ExecuteNonQuery();
  246. }
  247. catch (Exception ex)
  248. {
  249. log.Error("生成其他出库单表体失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  250. throw new Exception("程序异常,请联系开发人员!");
  251. }
  252. #endregion
  253. //更新现存量
  254. key08.cBustypeUN = "调拨入库";
  255. key08.cVouchTypeUN = "08";
  256. key08.TableName = "IA_ST_UnAccountVouch08";
  257. key08.UpdateTodoQuantity = head.UpdateTodoQuantity;
  258. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key08);
  259. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  260. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  261. "", key08, "", "", "", "", 0, 0, 0, 0, -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), -Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), 0, 0, iFatherId, iChildId);
  262. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, "", Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key08);
  263. iChildId--;
  264. }
  265. #endregion
  266. #endregion
  267. #region 其他出库单
  268. //取得采购入库单单据 表头ID,表体DID
  269. VouchKey key09 = new VouchKey();
  270. //key09 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  271. //// 取得表头单据号
  272. //string Code09 = U8Helper.GetRdrecord09Code(ErpName, "QC", cmd);
  273. var nums = dtChecks.Rows.Count;
  274. Dictionary<string, int> dics = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + nums + "", head.WorkPoint);
  275. int iFatherIds = Convert.ToInt32(dics["iFatherId"].ToString());
  276. int iChildIds = Convert.ToInt32(dics["iChildId"].ToString());
  277. DateTime dates = DateTime.Now;
  278. string cRDcode09 = DBHelper.GetRDCode(crdname, cmd);
  279. string iBaseCodeLens = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode09, head.WorkPoint);
  280. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  281. #region 其他出库单表头
  282. sql = @"INSERT INTO dbo.RdRecord09
  283. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  284. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  285. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  286. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  287. SELECT @ID,'0','09','','',a.cTVCode,cOWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,
  288. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  289. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cODepCode from dbo.TransVouch a WHERE ID='" + head.ID + "' ";
  290. cmd.Parameters.Clear();
  291. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  292. //cmd.Parameters.Add(new SqlParameter("@cBusCode", dtCheck.Rows[0]["cTVCode"].ToString()));
  293. //cmd.Parameters.Add(new SqlParameter("@cWhCode", cOWhCode));
  294. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLens));
  295. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  296. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  297. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  298. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  299. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode09));
  300. cmd.CommandText = sql;
  301. try
  302. {
  303. cmd.ExecuteNonQuery();
  304. }
  305. catch (Exception ex)
  306. {
  307. log.Error("生成其他出库单失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  308. throw new Exception("程序异常,请联系开发人员!");
  309. }
  310. #endregion
  311. #region 3.5 其他出库单表体
  312. for (int i = 0; i < dtChecks.Rows.Count; i++)
  313. {
  314. #region 3.5.1 表体
  315. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  316. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtCheck.Rows[0]["cInvCode"].ToString() + "'AND cWhCode='" + dtCheck.Rows[0]["cOWhCode"].ToString() + "'";
  317. //DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  318. //if (dtItem != null && dtItem.Rows.Count > 0)
  319. //{
  320. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cTVBatch))
  321. // {
  322. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  323. // }
  324. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  325. // {
  326. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  327. // }
  328. //}
  329. //else
  330. //{
  331. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  332. //}
  333. //key09.DID += 1;
  334. //Rd09irowno += 1;
  335. sql = @"INSERT INTO dbo.rdrecords09
  336. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,
  337. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  338. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,
  339. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  340. from TransVouchs a
  341. inner join TransVouch b on a.ID=b.ID
  342. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"' ;";
  343. cmd.Parameters.Clear();
  344. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildIds));
  345. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  346. cmd.CommandText = sql;
  347. try
  348. {
  349. cmd.ExecuteNonQuery();
  350. }
  351. catch (Exception ex)
  352. {
  353. log.Error("生成其他出库单表体失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  354. throw new Exception("程序异常,请联系开发人员!");
  355. }
  356. sql = @"INSERT INTO dbo.rdrecords09sub
  357. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  358. values('" + iChildIds + "','" + iFatherIds + "',0,0)";
  359. cmd.CommandText = sql;
  360. cmd.ExecuteNonQuery();
  361. #endregion
  362. if (head.UpdateStock)
  363. {
  364. //更新现存量
  365. key09.cBustypeUN = "调拨出库";
  366. key09.cVouchTypeUN = "09";
  367. key09.TableName = "IA_ST_UnAccountVouch09";
  368. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, "", -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key09);
  369. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key09);
  370. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), -Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  371. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  372. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), -Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), iFatherIds, iChildIds);
  373. }
  374. iChildIds--;
  375. }
  376. #endregion
  377. #endregion
  378. }
  379. cmd.Transaction.Commit();
  380. }
  381. catch (Exception ex)
  382. {
  383. cmd.Transaction.Rollback();
  384. log.Error(ex.Message);
  385. throw new Exception(ex.Message);
  386. }
  387. finally
  388. {
  389. if (conn.State == ConnectionState.Open)
  390. {
  391. conn.Close();
  392. }
  393. conn.Dispose();
  394. }
  395. }
  396. ResultFlag = true;
  397. return ResultFlag;
  398. }
  399. /// <summary>
  400. /// 生成无源头调拨单
  401. /// </summary>
  402. /// <param name="Bills"></param>
  403. /// <returns></returns>
  404. public string ConfirmTransIN(List<ICSTransfer> Bills)
  405. {
  406. string connS = "";
  407. string szJson = "";
  408. DataSet ds = null;
  409. string iFatherIdTwo = "";
  410. SqlConnection conn = new SqlConnection();
  411. SqlCommand cmd = new SqlCommand();
  412. if (Bills.Count <= 0)
  413. {
  414. throw new Exception("传送数据为空!");
  415. }
  416. LogInfo1(Bills);
  417. //MergeObject(Bills, cmd);
  418. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  419. foreach (string WorkPoint in result)
  420. {
  421. try
  422. {
  423. connS = string.Format(connString, WorkPoint);
  424. conn = new System.Data.SqlClient.SqlConnection(connS);
  425. conn.Open();
  426. SqlTransaction sqlTran = conn.BeginTransaction();
  427. cmd = new SqlCommand();
  428. cmd.Transaction = sqlTran;
  429. cmd.Connection = conn;
  430. foreach (ICSTransfer head in Bills)
  431. {
  432. iFatherIdTwo = "";
  433. if (WorkPoint != head.WorkPoint)
  434. continue;
  435. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  436. throw new Exception("U8正在整理现存量,请稍后再试");
  437. string[] ss = head.WorkPoint.Split('_');
  438. ERPDB = ss[1];
  439. string[] dd = CRNALL.Split('~');
  440. string crdname = dd[2];
  441. string crdname1 = dd[3];
  442. string carname = dd[4];
  443. string carname1 = dd[5];
  444. string surface = dd[6];
  445. string carname2 = dd[0];
  446. string surface1 = dd[1];
  447. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  448. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  449. string cardnewcode2 = DBHelper.GetCardNumber(carname2, cmd);
  450. int num = 0;
  451. foreach (ICSTransfer nu in Bills)
  452. {
  453. num = head.details.Count();
  454. }
  455. string sql = "";
  456. ICSUserInfo userInfo = new ICSUserInfo();
  457. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  458. string cIWhCode = string.Empty;
  459. string cOWhCode = string.Empty;
  460. #region 调拨单
  461. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint);
  462. int iFather = Convert.ToInt32(dic1["iFatherId"].ToString());
  463. int iChild = Convert.ToInt32(dic1["iChildId"].ToString());
  464. DateTime date = DateTime.Now;
  465. string iBaseCode = DBHelper.GetAllRDCode(cardnewcode2, "" + date + "", "admin", "", head.WorkPoint);
  466. string cRDcode2 = DBHelper.GetRDCode(crdname, cmd);
  467. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  468. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode2, cmd);
  469. sql = @"INSERT INTO dbo.TransVouch
  470. (cTVCode,dTVDate,cOWhCode,cIWhCode,cODepCode,cIDepCode,cIRdCode,cORdCode,cMaker,ID
  471. ,VT_ID,itransflag,dnmaketime
  472. ,iswfcontrolled,iPrintCount)
  473. SELECT @cTVCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cOWhCode,@cIWhCode,@cODepCode,@cIDepCode,@cIRdCode,@cORdCode,@cMaker,@ID
  474. ,@VT_ID,'',GETDATE()
  475. ,0,0
  476. ";
  477. cmd.Parameters.Clear();
  478. cmd.Parameters.Add(new SqlParameter("@ID", iFather));
  479. cmd.Parameters.Add(new SqlParameter("@cTVCode", iBaseCode));
  480. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  481. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  482. cmd.Parameters.Add(new SqlParameter("@cIRdCode", cRDcode1));
  483. cmd.Parameters.Add(new SqlParameter("@cORdCode", cRDcode2));
  484. cmd.Parameters.Add(new SqlParameter("@cOWhCode", head.FromWHCode));
  485. cmd.Parameters.Add(new SqlParameter("@cIWhCode", head.ToWHCode));
  486. cmd.Parameters.Add(new SqlParameter("@cODepCode", head.FromDepCode));
  487. cmd.Parameters.Add(new SqlParameter("@cIDepCode", head.ToDepCode));
  488. cmd.CommandText = sql;
  489. try
  490. {
  491. cmd.ExecuteNonQuery();
  492. }
  493. catch (Exception ex)
  494. {
  495. log.Error("生成调拨单表头失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  496. throw new Exception("程序异常,请联系开发人员!");
  497. }
  498. iFatherIdTwo = "'" + iFather + "',";
  499. foreach (ICSTransfers body in head.details)
  500. {
  501. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  502. {
  503. if (body.BatchCode == "")
  504. {
  505. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  506. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  507. }
  508. }
  509. else
  510. {
  511. if (body.BatchCode != "")
  512. {
  513. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  514. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  515. }
  516. }
  517. sql = @" INSERT INTO dbo.TransVouchs
  518. (cTVCode,cInvCode,iTVNum,iTVQuantity,cTVBatch,autoID,ID,bCosting,cItemCode
  519. ,iExpiratDateCalcu,irowno,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  520. SELECT @cTVCode,@cInvCode,@iTVNum,@iTVQuantity,@cTVBatch,@autoID,@ID,1,@cItemCode
  521. ,0,@irowno,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  522. ";
  523. cmd.Parameters.Clear();
  524. cmd.Parameters.Add(new SqlParameter("@ID", iFather));
  525. cmd.Parameters.Add(new SqlParameter("@cTVCode", iBaseCode));
  526. cmd.Parameters.Add(new SqlParameter("@autoID", iChild));
  527. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  528. cmd.Parameters.Add(new SqlParameter("@iTVNum", body.Amount.ToString("0.00")));
  529. cmd.Parameters.Add(new SqlParameter("@iTVQuantity", body.Quantity));
  530. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  531. cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode));
  532. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  533. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  534. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  535. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  536. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  537. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  538. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  539. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  540. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  541. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  542. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  543. cmd.CommandText = sql;
  544. iChild--;
  545. try
  546. {
  547. cmd.ExecuteNonQuery();
  548. }
  549. catch (Exception ex)
  550. {
  551. log.Error("生成调拨单子表失败!调拨单:" + iBaseCode + ",行:" + body.Sequence + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  552. throw new Exception("程序异常,请联系开发人员!");
  553. }
  554. }
  555. #endregion
  556. #region 检查
  557. sql = "select * from dbo.TransVouch where ID='" + iFather + "';";
  558. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  559. if (dtCheck != null && dtCheck.Rows.Count > 0)
  560. {
  561. string cHandler = dtCheck.Rows[0]["cVerifyPerson"].ToString();
  562. if (!string.IsNullOrEmpty(cHandler))
  563. {
  564. throw new Exception("调拨单:" + iBaseCode + "不是开立状态!");
  565. }
  566. cIWhCode = dtCheck.Rows[0]["cIWhCode"].ToString();
  567. cOWhCode = dtCheck.Rows[0]["cOWhCode"].ToString();
  568. sql = string.Format("select * from Warehouse where cWhCode in ('{0}','{1}')", dtCheck.Rows[0]["cOWhCode"].ToString(), dtCheck.Rows[0]["cIWhCode"].ToString());
  569. DataTable dtWh = DBHelper.SQlReturnData(sql, cmd);
  570. if (dtWh != null && dtWh.Rows.Count > 1)
  571. {
  572. }
  573. else
  574. {
  575. throw new Exception("调拨单中转入或者转出仓库不存在!");
  576. }
  577. }
  578. else
  579. {
  580. throw new Exception("调拨单:" + iBaseCode + "在U8中不存在!");
  581. }
  582. #endregion
  583. #region 审核
  584. sql = "UPDATE dbo.TransVouch SET cVerifyPerson='" + userInfo.UserName + "' ,dVerifyDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE cTVCode='" + iBaseCode + "'";
  585. cmd.CommandText = sql;
  586. try
  587. {
  588. cmd.ExecuteNonQuery();
  589. }
  590. catch (Exception ex)
  591. {
  592. log.Error("审核调拨单失败!异常:" + ex.Message + ";\r\n SQL:" + sql);
  593. throw new Exception("程序异常,请联系开发人员!");
  594. }
  595. int bodyID = 0;
  596. #endregion
  597. sql = "select * from dbo.TransVouchs where ID='" + iFather + "';";
  598. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  599. #region 其他入库单
  600. //取得out库单的默认显示模版
  601. VouchKey key08 = new VouchKey();
  602. num = dtChecks.Rows.Count;
  603. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  604. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  605. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  606. DateTime date1 = DateTime.Now;
  607. string cRDcode08 = DBHelper.GetRDCode(crdname1, cmd);
  608. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", cRDcode08, head.WorkPoint);
  609. string DEF_ID08 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  610. #region 其他入库单表头
  611. sql = @"INSERT INTO dbo.RdRecord08
  612. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  613. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  614. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  615. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  616. SELECT @ID,'1','08','','',a.cTVCode,cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),a.cTVCode,@cRdCode,
  617. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  618. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cIDepCode
  619. from dbo.TransVouch a WHERE cTVCode='" + iBaseCode + "' ";
  620. cmd.Parameters.Clear();
  621. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  622. //cmd.Parameters.Add(new SqlParameter("@cBusCode", head.ID));
  623. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  624. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  625. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  626. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  627. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  628. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode08));
  629. cmd.CommandText = sql;
  630. try
  631. {
  632. cmd.ExecuteNonQuery();
  633. }
  634. catch (Exception ex)
  635. {
  636. log.Error("生成其他入库单表头失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  637. throw new Exception("程序异常,请联系开发人员!");
  638. }
  639. #endregion
  640. #region 3.5 其他入库单表体
  641. for (int i = 0; i < dtChecks.Rows.Count; i++)
  642. {
  643. #region 3.5.1 表体
  644. sql = @"INSERT INTO dbo.rdrecords08
  645. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,cItemCode,
  646. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  647. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,a.cItemCode,
  648. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  649. from TransVouchs a
  650. inner join TransVouch b on a.ID=b.ID
  651. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"';";
  652. cmd.Parameters.Clear();
  653. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  654. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  655. cmd.CommandText = sql;
  656. try
  657. {
  658. cmd.ExecuteNonQuery();
  659. }
  660. catch (Exception ex)
  661. {
  662. log.Error("生成其他出库单表体失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  663. throw new Exception("程序异常,请联系开发人员!");
  664. }
  665. #endregion
  666. //更新现存量
  667. key08.cBustypeUN = "调拨入库";
  668. key08.cVouchTypeUN = "08";
  669. key08.TableName = "IA_ST_UnAccountVouch08";
  670. key08.UpdateTodoQuantity = head.UpdateTodoQuantity;
  671. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key08);
  672. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  673. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  674. "", key08, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  675. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, "", Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key08);
  676. iChildId--;
  677. }
  678. #endregion
  679. #endregion
  680. #region 其他出库单
  681. //取得采购入库单单据 表头ID,表体DID
  682. VouchKey key09 = new VouchKey();
  683. //key09 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  684. //// 取得表头单据号
  685. //string Code09 = U8Helper.GetRdrecord09Code(ErpName, "QC", cmd);
  686. var nums = dtChecks.Rows.Count + num;
  687. Dictionary<string, int> dics = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + nums + "", head.WorkPoint);
  688. int iFatherIds = Convert.ToInt32(dics["iFatherId"].ToString());
  689. int iChildIds = Convert.ToInt32(dics["iChildId"].ToString());
  690. DateTime dates = DateTime.Now;
  691. string cRDcode09 = DBHelper.GetRDCode(crdname, cmd);
  692. string iBaseCodeLens = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode09, head.WorkPoint);
  693. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  694. #region 其他出库单表头
  695. sql = @"INSERT INTO dbo.RdRecord09
  696. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  697. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  698. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  699. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  700. SELECT @ID,'0','09','','',a.cTVCode,cOWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,
  701. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  702. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cODepCode
  703. from dbo.TransVouch a WHERE cTVCode='" + iBaseCode + "' ";
  704. cmd.Parameters.Clear();
  705. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  706. //cmd.Parameters.Add(new SqlParameter("@cBusCode", dtCheck.Rows[0]["cTVCode"].ToString()));
  707. //cmd.Parameters.Add(new SqlParameter("@cWhCode", cOWhCode));
  708. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLens));
  709. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  710. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  711. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  712. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  713. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode09));
  714. cmd.CommandText = sql;
  715. try
  716. {
  717. cmd.ExecuteNonQuery();
  718. }
  719. catch (Exception ex)
  720. {
  721. log.Error("生成其他出库单失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  722. throw new Exception("程序异常,请联系开发人员!");
  723. }
  724. #endregion
  725. #region 3.5 其他出库单表体
  726. for (int i = 0; i < dtChecks.Rows.Count; i++)
  727. {
  728. #region 3.5.1 表体
  729. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  730. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtCheck.Rows[0]["cInvCode"].ToString() + "'AND cWhCode='" + dtCheck.Rows[0]["cOWhCode"].ToString() + "'";
  731. //DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  732. //if (dtItem != null && dtItem.Rows.Count > 0)
  733. //{
  734. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cTVBatch))
  735. // {
  736. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  737. // }
  738. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  739. // {
  740. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  741. // }
  742. //}
  743. //else
  744. //{
  745. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  746. //}
  747. //key09.DID += 1;
  748. //Rd09irowno += 1;
  749. sql = @"INSERT INTO dbo.rdrecords09
  750. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,cItemCode,
  751. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  752. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,a.cItemCode,
  753. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  754. from TransVouchs a
  755. inner join TransVouch b on a.ID=b.ID
  756. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"' ;";
  757. cmd.Parameters.Clear();
  758. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildIds));
  759. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  760. cmd.CommandText = sql;
  761. try
  762. {
  763. cmd.ExecuteNonQuery();
  764. }
  765. catch (Exception ex)
  766. {
  767. log.Error("生成其他出库单表体失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  768. throw new Exception("程序异常,请联系开发人员!");
  769. }
  770. sql = @"INSERT INTO dbo.rdrecords09sub
  771. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  772. values('" + iChildIds + "','" + iFatherIds + "',0,0)";
  773. cmd.CommandText = sql;
  774. cmd.ExecuteNonQuery();
  775. #endregion
  776. //更新现存量
  777. key09.cBustypeUN = "调拨出库";
  778. key09.cVouchTypeUN = "09";
  779. key09.TableName = "IA_ST_UnAccountVouch09";
  780. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, "", -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key09);
  781. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key09);
  782. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), -Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  783. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  784. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherIds, iChildIds);
  785. iChildIds--;
  786. }
  787. #endregion
  788. #endregion
  789. #region 查询
  790. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cTVCode as TransCode,a.cODepCode FromDepCode,c.cDepName FromDepName,a.cOWhCode FromWHCode,e.cWhName FromWHName
  791. ,a.cIDepCode ToDepCode,d.cDepName ToDepName,a.cIWhCode ToWHCode,f.cWhName ToWHName
  792. ,a.cTranRequestCode as TACode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  793. ,a.cVerifyPerson as Checker ,a.dnverifytime as CheckDateTime
  794. from TransVouch a
  795. left join Department c on a.cODepCode=c.cDepCode
  796. left join Department d on a.cIDepCode=d.cDepCode
  797. left join Warehouse e on a.cOWhCode=e.cWhCode
  798. left join Warehouse f on a.cIWhCode=f.cWhCode
  799. WHERE A.cVerifyPerson IS NOT NULL AND A.ID in({0})
  800. select DISTINCT a.ID as IDs, b.AutoID as DetailID,b.irowno as Sequence ,b.cInvCode as InvCode ,
  801. b.iTVQuantity as Quantity ,b.iTVNum as Amount,b.iTRIds as TADetailID
  802. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cTVBatch,'') BatchCode,'' version ,'' brand,
  803. isnull(b.cFree1,'') as cFree1,
  804. isnull(b.cFree2,'') as cFree2,
  805. isnull(b.cFree3,'') as cFree3,
  806. isnull(b.cFree4,'') as cFree4,
  807. isnull(b.cFree5,'') as cFree5,
  808. isnull(b.cFree6,'') as cFree6,
  809. isnull(b.cFree7,'') as cFree7,
  810. isnull(b.cFree8,'') as cFree8,
  811. isnull(b.cFree9,'') as cFree9,
  812. isnull(b.cFree10,'') as cFree10
  813. from TransVouch a
  814. left join TransVouchs b on a.ID=b.ID
  815. WHERE A.cVerifyPerson IS NOT NULL AND A.ID in({0}) ";
  816. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  817. if (ds != null)
  818. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  819. else
  820. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  821. #endregion
  822. }
  823. cmd.Transaction.Commit();
  824. }
  825. catch (Exception ex)
  826. {
  827. cmd.Transaction.Rollback();
  828. log.Error(ex.Message);
  829. throw new Exception(ex.Message);
  830. }
  831. finally
  832. {
  833. if (conn.State == ConnectionState.Open)
  834. {
  835. conn.Close();
  836. }
  837. conn.Dispose();
  838. }
  839. }
  840. szJson = JSON.DataSetToJson(ds, "details", "IDs");
  841. return szJson;
  842. }
  843. /// <summary>
  844. /// 生成调拨单
  845. /// </summary>
  846. /// <param name="Bills"></param>
  847. /// <returns></returns>
  848. public string ConfirmTrans(List<ICSTransfer> Bills)
  849. {
  850. string connS = "";
  851. string szJson = "";
  852. DataSet ds = null;
  853. string iFatherIdTwo = "";
  854. SqlConnection conn = new SqlConnection();
  855. SqlCommand cmd = new SqlCommand();
  856. if (Bills.Count <= 0)
  857. {
  858. throw new Exception("传送数据为空!");
  859. }
  860. LogInfo1(Bills);
  861. //MergeObject(Bills, cmd);
  862. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  863. foreach (string WorkPoint in result)
  864. {
  865. try
  866. {
  867. connS = string.Format(connString, WorkPoint);
  868. conn = new System.Data.SqlClient.SqlConnection(connS);
  869. conn.Open();
  870. SqlTransaction sqlTran = conn.BeginTransaction();
  871. cmd = new SqlCommand();
  872. cmd.Transaction = sqlTran;
  873. cmd.Connection = conn;
  874. foreach (ICSTransfer head in Bills)
  875. {
  876. iFatherIdTwo = "";
  877. if (WorkPoint != head.WorkPoint)
  878. continue;
  879. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  880. throw new Exception("U8正在整理现存量,请稍后再试");
  881. string[] ss = head.WorkPoint.Split('_');
  882. ERPDB = ss[1];
  883. string[] dd = CRNALL.Split('~');
  884. string crdname = dd[2];
  885. string crdname1 = dd[3];
  886. string carname = dd[4];
  887. string carname1 = dd[5];
  888. string surface = dd[6];
  889. string carname2 = dd[0];
  890. string surface1 = dd[1];
  891. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  892. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  893. string cardnewcode2 = DBHelper.GetCardNumber(carname2, cmd);
  894. int num = 0;
  895. foreach (ICSTransfer nu in Bills)
  896. {
  897. num = head.details.Count();
  898. }
  899. string sql = "";
  900. ICSUserInfo userInfo = new ICSUserInfo();
  901. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  902. string cIWhCode = string.Empty;
  903. string cOWhCode = string.Empty;
  904. #region 调拨单
  905. sql = "select * from dbo.ST_AppTransVouchs where ID in (select ID from dbo.ST_AppTransVouch where cTVCode='" + head.TACode + "')";
  906. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  907. num = dtChecks.Rows.Count;
  908. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint);
  909. int iFather = Convert.ToInt32(dic1["iFatherId"].ToString());
  910. int iChild = Convert.ToInt32(dic1["iChildId"].ToString());
  911. DateTime date = DateTime.Now;
  912. string iBaseCode = DBHelper.GetAllRDCode(cardnewcode2, "" + date + "", "admin", "", head.WorkPoint);
  913. string cRDcode2 = DBHelper.GetRDCode(crdname, cmd);
  914. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  915. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode2, cmd);
  916. sql = @"INSERT INTO dbo.TransVouch
  917. (cTVCode,dTVDate,cOWhCode,cIWhCode,cODepCode,cIDepCode,cIRdCode,cORdCode,cMaker,ID
  918. ,VT_ID,itransflag,dnmaketime
  919. ,iswfcontrolled,iPrintCount)
  920. SELECT @cTVCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cOWhCode,@cIWhCode,cODepCode,cIDepCode,cIRdCode,cORdCode,@cMaker,@ID
  921. ,@VT_ID,'',GETDATE()
  922. ,0,0
  923. FROM ST_AppTransVouch
  924. WHERE cTVCode='" + head.TACode + @"'
  925. ";
  926. cmd.Parameters.Clear();
  927. cmd.Parameters.Add(new SqlParameter("@ID", iFather));
  928. cmd.Parameters.Add(new SqlParameter("@cTVCode", iBaseCode));
  929. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  930. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  931. cmd.Parameters.Add(new SqlParameter("@cOWhCode", head.FromWHCode));
  932. cmd.Parameters.Add(new SqlParameter("@cIWhCode", head.ToWHCode));
  933. cmd.CommandText = sql;
  934. try
  935. {
  936. cmd.ExecuteNonQuery();
  937. }
  938. catch (Exception ex)
  939. {
  940. log.Error("生成调拨单表头失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  941. throw new Exception("程序异常,请联系开发人员!");
  942. }
  943. iFatherIdTwo += "'" + iFather + "',";
  944. foreach (ICSTransfers body in head.details)
  945. {
  946. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  947. {
  948. if (body.BatchCode == "")
  949. {
  950. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  951. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  952. }
  953. }
  954. else
  955. {
  956. if (body.BatchCode != "")
  957. {
  958. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  959. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  960. }
  961. }
  962. sql = @" INSERT INTO dbo.TransVouchs
  963. (cTVCode,cInvCode,iTVNum,iTVQuantity,cTVBatch,autoID,ID,bCosting,iTRIds,iMPoIds,cmocode,imoseq,cItemCode
  964. ,iExpiratDateCalcu,irowno,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  965. SELECT @cTVCode,@cInvCode,@iTVNum,@iTVQuantity,@cTVBatch,@autoID,@ID,1,autoid,iMPoIds,cmocode,imoseq,@cItemCode
  966. ,0,irowno,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  967. FROM ST_AppTransVouchs WHERE autoID='" + body.TADetailID + @"'
  968. update A set A.iTvSumQuantity=isnull(A.iTvSumQuantity,0)+B.iTVQuantity,A.iTVSumNum=isnull(A.iTVSumNum,0)+B.iTVNum
  969. FROM ST_AppTransVouchs A
  970. LEFT JOIN TransVouchs B ON A.autoID=B.iTRIds
  971. where A.autoID='" + body.TADetailID + @"'
  972. ";
  973. cmd.Parameters.Clear();
  974. cmd.Parameters.Add(new SqlParameter("@ID", iFather));
  975. cmd.Parameters.Add(new SqlParameter("@cTVCode", iBaseCode));
  976. cmd.Parameters.Add(new SqlParameter("@autoID", iChild));
  977. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  978. cmd.Parameters.Add(new SqlParameter("@iTVQuantity", body.Quantity));
  979. cmd.Parameters.Add(new SqlParameter("@iTVNum", body.Amount.ToString("0.00")));
  980. cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode));
  981. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  982. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  983. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  984. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  985. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  986. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  987. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  988. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  989. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  990. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  991. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  992. cmd.CommandText = sql;
  993. iChild--;
  994. try
  995. {
  996. cmd.ExecuteNonQuery();
  997. }
  998. catch (Exception ex)
  999. {
  1000. log.Error("生成调拨单子表失败!调拨单:" + iBaseCode + ",行:" + body.Sequence + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  1001. throw new Exception("程序异常,请联系开发人员!");
  1002. }
  1003. }
  1004. #endregion
  1005. #region 检查
  1006. sql = "select * from dbo.TransVouch where ID='" + iFather + "';";
  1007. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1008. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1009. {
  1010. string cHandler = dtCheck.Rows[0]["cVerifyPerson"].ToString();
  1011. if (!string.IsNullOrEmpty(cHandler))
  1012. {
  1013. throw new Exception("调拨单:" + iBaseCode + "不是开立状态!");
  1014. }
  1015. cIWhCode = dtCheck.Rows[0]["cIWhCode"].ToString();
  1016. cOWhCode = dtCheck.Rows[0]["cOWhCode"].ToString();
  1017. sql = string.Format("select * from Warehouse where cWhCode in ('{0}','{1}')", dtCheck.Rows[0]["cOWhCode"].ToString(), dtCheck.Rows[0]["cIWhCode"].ToString());
  1018. DataTable dtWh = DBHelper.SQlReturnData(sql, cmd);
  1019. if (dtWh != null && dtWh.Rows.Count > 1)
  1020. {
  1021. }
  1022. else
  1023. {
  1024. throw new Exception("调拨单中转入或者转出仓库不存在!");
  1025. }
  1026. }
  1027. else
  1028. {
  1029. throw new Exception("调拨单:" + iBaseCode + "在U8中不存在!");
  1030. }
  1031. #endregion
  1032. #region 审核
  1033. sql = "UPDATE dbo.TransVouch SET cVerifyPerson='" + userInfo.UserName + "' ,dVerifyDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE cTVCode='" + iBaseCode + "'";
  1034. cmd.CommandText = sql;
  1035. try
  1036. {
  1037. cmd.ExecuteNonQuery();
  1038. }
  1039. catch (Exception ex)
  1040. {
  1041. log.Error("审核调拨单失败!异常:" + ex.Message + ";\r\n SQL:" + sql);
  1042. throw new Exception("程序异常,请联系开发人员!");
  1043. }
  1044. int bodyID = 0;
  1045. #endregion
  1046. sql = "select * from dbo.TransVouchs where ID='" + iFather + "';";
  1047. dtChecks = DBHelper.SQlReturnData(sql, cmd);
  1048. #region 其他入库单
  1049. //取得out库单的默认显示模版
  1050. VouchKey key08 = new VouchKey();
  1051. num = dtChecks.Rows.Count;
  1052. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  1053. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  1054. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  1055. DateTime date1 = DateTime.Now;
  1056. string cRDcode08 = DBHelper.GetRDCode(crdname1, cmd);
  1057. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", cRDcode08, head.WorkPoint);
  1058. string DEF_ID08 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  1059. #region 其他入库单表头
  1060. sql = @"INSERT INTO dbo.RdRecord08
  1061. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  1062. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  1063. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  1064. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  1065. SELECT @ID,'1','08','','',a.cTVCode,cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),a.cTVCode,cIRdCode,
  1066. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  1067. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cIDepCode
  1068. from dbo.TransVouch a
  1069. WHERE cTVCode='" + iBaseCode + "' ";
  1070. cmd.Parameters.Clear();
  1071. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  1072. //cmd.Parameters.Add(new SqlParameter("@cBusCode", head.ID));
  1073. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  1074. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  1075. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  1076. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  1077. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  1078. cmd.CommandText = sql;
  1079. try
  1080. {
  1081. cmd.ExecuteNonQuery();
  1082. }
  1083. catch (Exception ex)
  1084. {
  1085. log.Error("生成其他入库单表头失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  1086. throw new Exception("程序异常,请联系开发人员!");
  1087. }
  1088. #endregion
  1089. #region 3.5 其他入库单表体
  1090. for (int i = 0; i < dtChecks.Rows.Count; i++)
  1091. {
  1092. #region 3.5.1 表体
  1093. sql = @"INSERT INTO dbo.rdrecords08
  1094. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,cItemCode,
  1095. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1096. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,a.cItemCode,
  1097. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  1098. from TransVouchs a
  1099. inner join TransVouch b on a.ID=b.ID
  1100. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"';";
  1101. cmd.Parameters.Clear();
  1102. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  1103. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  1104. cmd.CommandText = sql;
  1105. try
  1106. {
  1107. cmd.ExecuteNonQuery();
  1108. }
  1109. catch (Exception ex)
  1110. {
  1111. log.Error("生成其他出库单表体失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  1112. throw new Exception("程序异常,请联系开发人员!");
  1113. }
  1114. #endregion
  1115. //更新现存量
  1116. key08.cBustypeUN = "调拨入库";
  1117. key08.cVouchTypeUN = "08";
  1118. key08.TableName = "IA_ST_UnAccountVouch08";
  1119. key08.UpdateTodoQuantity = head.UpdateTodoQuantity;
  1120. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key08);
  1121. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  1122. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  1123. "", key08, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  1124. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, "", Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key08);
  1125. iChildId--;
  1126. }
  1127. #endregion
  1128. #endregion
  1129. #region 其他出库单
  1130. //取得采购入库单单据 表头ID,表体DID
  1131. VouchKey key09 = new VouchKey();
  1132. //key09 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  1133. //// 取得表头单据号
  1134. //string Code09 = U8Helper.GetRdrecord09Code(ErpName, "QC", cmd);
  1135. var nums = dtChecks.Rows.Count + num;
  1136. Dictionary<string, int> dics = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + nums + "", head.WorkPoint);
  1137. int iFatherIds = Convert.ToInt32(dics["iFatherId"].ToString());
  1138. int iChildIds = Convert.ToInt32(dics["iChildId"].ToString());
  1139. DateTime dates = DateTime.Now;
  1140. string cRDcode09 = DBHelper.GetRDCode(crdname, cmd);
  1141. string iBaseCodeLens = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode09, head.WorkPoint);
  1142. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  1143. #region 其他出库单表头
  1144. sql = @"INSERT INTO dbo.RdRecord09
  1145. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  1146. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  1147. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  1148. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  1149. SELECT @ID,'0','09','','',a.cTVCode,cOWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,
  1150. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  1151. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cODepCode
  1152. from dbo.TransVouch a WHERE cTVCode='" + iBaseCode + "' ";
  1153. cmd.Parameters.Clear();
  1154. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  1155. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLens));
  1156. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  1157. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  1158. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  1159. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  1160. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode09));
  1161. cmd.CommandText = sql;
  1162. try
  1163. {
  1164. cmd.ExecuteNonQuery();
  1165. }
  1166. catch (Exception ex)
  1167. {
  1168. log.Error("生成其他出库单失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  1169. throw new Exception("程序异常,请联系开发人员!");
  1170. }
  1171. #endregion
  1172. #region 3.5 其他出库单表体
  1173. for (int i = 0; i < dtChecks.Rows.Count; i++)
  1174. {
  1175. #region 3.5.1 表体
  1176. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  1177. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtCheck.Rows[0]["cInvCode"].ToString() + "'AND cWhCode='" + dtCheck.Rows[0]["cOWhCode"].ToString() + "'";
  1178. //DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  1179. //if (dtItem != null && dtItem.Rows.Count > 0)
  1180. //{
  1181. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cTVBatch))
  1182. // {
  1183. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  1184. // }
  1185. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  1186. // {
  1187. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  1188. // }
  1189. //}
  1190. //else
  1191. //{
  1192. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  1193. //}
  1194. //key09.DID += 1;
  1195. //Rd09irowno += 1;
  1196. sql = @"INSERT INTO dbo.rdrecords09
  1197. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum,cItemCode,
  1198. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1199. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,iTVNum,a.cItemCode,
  1200. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice ,a.cFree1,a.cFree2,a.cFree3,a.cFree4,a.cFree5,a.cFree6,a.cFree7,a.cFree8,a.cFree9,a.cFree10
  1201. from TransVouchs a
  1202. inner join TransVouch b on a.ID=b.ID
  1203. where a.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"'
  1204. update a set a.TransQty=isnull(a.TransQty,0)+b.iTVQuantity from mom_moallocate a left join TransVouchs b on a.AllocateId=b.iMPoIds
  1205. where b.autoid='" + dtChecks.Rows[i]["AutoID"].ToString() + @"'
  1206. ;";
  1207. cmd.Parameters.Clear();
  1208. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildIds));
  1209. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  1210. cmd.CommandText = sql;
  1211. try
  1212. {
  1213. cmd.ExecuteNonQuery();
  1214. }
  1215. catch (Exception ex)
  1216. {
  1217. log.Error("生成其他出库单表体失败!调拨单:" + iBaseCode + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  1218. throw new Exception("程序异常,请联系开发人员!");
  1219. }
  1220. sql = @"INSERT INTO dbo.rdrecords09sub
  1221. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  1222. values('" + iChildIds + "','" + iFatherIds + "',0,0)";
  1223. cmd.CommandText = sql;
  1224. cmd.ExecuteNonQuery();
  1225. #endregion
  1226. //更新现存量
  1227. key09.cBustypeUN = "调拨出库";
  1228. key09.cVouchTypeUN = "09";
  1229. key09.TableName = "IA_ST_UnAccountVouch09";
  1230. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, "", -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key09);
  1231. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key09);
  1232. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, dtChecks.Rows[i]["cTVBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), Convert.ToDecimal(dtChecks.Rows[i]["iTVNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(),
  1233. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  1234. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherIds, iChildIds);
  1235. iChildIds--;
  1236. }
  1237. #endregion
  1238. #endregion
  1239. #region 查询
  1240. sql = @" select top 1 a.ID as ID,a.ID as IDs,a.cTVCode as TransCode,a.cODepCode FromDepCode,c.cDepName FromDepName,a.cOWhCode FromWHCode,e.cWhName FromWHName
  1241. ,a.cIDepCode ToDepCode,d.cDepName ToDepName,a.cIWhCode ToWHCode,f.cWhName ToWHName
  1242. ,a.cTranRequestCode as TACode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  1243. ,a.cVerifyPerson as Checker ,a.dnverifytime as CheckDateTime
  1244. from TransVouch a
  1245. left join Department c on a.cODepCode=c.cDepCode
  1246. left join Department d on a.cIDepCode=d.cDepCode
  1247. left join Warehouse e on a.cOWhCode=e.cWhCode
  1248. left join Warehouse f on a.cIWhCode=f.cWhCode
  1249. WHERE A.cVerifyPerson IS NOT NULL AND A.ID in({0})
  1250. select DISTINCT a.ID as IDs, b.AutoID as DetailID,b.irowno as Sequence ,b.cInvCode as InvCode ,
  1251. b.iTVQuantity as Quantity ,b.iTVNum as Amount,b.iTRIds as TADetailID
  1252. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cTVBatch,'') BatchCode,'' version ,'' brand,
  1253. isnull(b.cFree1,'') as cFree1,
  1254. isnull(b.cFree2,'') as cFree2,
  1255. isnull(b.cFree3,'') as cFree3,
  1256. isnull(b.cFree4,'') as cFree4,
  1257. isnull(b.cFree5,'') as cFree5,
  1258. isnull(b.cFree6,'') as cFree6,
  1259. isnull(b.cFree7,'') as cFree7,
  1260. isnull(b.cFree8,'') as cFree8,
  1261. isnull(b.cFree9,'') as cFree9,
  1262. isnull(b.cFree10,'') as cFree10
  1263. from TransVouch a
  1264. left join TransVouchs b on a.ID=b.ID
  1265. WHERE A.cVerifyPerson IS NOT NULL AND A.ID in({0}) ";
  1266. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  1267. if (ds != null)
  1268. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  1269. else
  1270. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1271. #endregion
  1272. }
  1273. cmd.Transaction.Commit();
  1274. }
  1275. catch (Exception ex)
  1276. {
  1277. cmd.Transaction.Rollback();
  1278. log.Error(ex.Message);
  1279. throw new Exception(ex.Message);
  1280. }
  1281. finally
  1282. {
  1283. if (conn.State == ConnectionState.Open)
  1284. {
  1285. conn.Close();
  1286. }
  1287. conn.Dispose();
  1288. }
  1289. }
  1290. szJson = JSON.DataSetToJson(ds, "details", "IDs");
  1291. return szJson;
  1292. }
  1293. /// <summary>
  1294. /// 记录日志
  1295. /// </summary>
  1296. /// <param name="Bills"></param>
  1297. private void LogInfo(List<ICSTransferDoc> Bills)
  1298. {
  1299. string HeadList = string.Empty;
  1300. string BodyList = string.Empty;
  1301. foreach (ICSTransferDoc head in Bills)
  1302. {
  1303. HeadList += "\r\n 表头调拨单号:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1304. foreach (ICSTransferDoc body in Bills)
  1305. {
  1306. BodyList += "\r\n 表体主键ID: " + body.ID + "";
  1307. }
  1308. }
  1309. log.Info(HeadList);
  1310. log.Info(BodyList);
  1311. }
  1312. /// <summary>
  1313. /// 记录日志
  1314. /// </summary>
  1315. /// <param name="Bills"></param>
  1316. private void LogInfo1(List<ICSTransfer> Bills)
  1317. {
  1318. string HeadList = string.Empty;
  1319. string BodyList = string.Empty;
  1320. foreach (ICSTransfer head in Bills)
  1321. {
  1322. HeadList += "\r\n 表头调拨单号:" + head.TACode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1323. foreach (ICSTransfer body in Bills)
  1324. {
  1325. BodyList += "\r\n 表体主键ID: " + body.TACode + "";
  1326. }
  1327. }
  1328. log.Info(HeadList);
  1329. log.Info(BodyList);
  1330. }
  1331. }
  1332. }