爱思开
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.

1572 lines
96 KiB

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