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

1555 lines
95 KiB

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