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

914 lines
46 KiB

  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 OutsourcingReceiveDoc
  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. /// <summary>
  23. /// 获取委外采购入库单
  24. /// </summary>
  25. /// <param name="infos"></param>
  26. /// <returns></returns>
  27. public string Get(List<ICSOutsourcingReceiveDoc> infos)
  28. {
  29. List<ICSOutsourcingReceiveDoc> szJson = new List<ICSOutsourcingReceiveDoc>();
  30. DataTable dt = null;
  31. string json = "";
  32. if (infos.Count <= 0)
  33. {
  34. throw new Exception("传送数据为空!");
  35. }
  36. string res = string.Empty;
  37. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  38. conn.Open();
  39. SqlTransaction sqlTran = conn.BeginTransaction();
  40. SqlCommand cmd = new SqlCommand();
  41. cmd.Transaction = sqlTran;
  42. cmd.Connection = conn;
  43. try
  44. {
  45. string sql = string.Empty;
  46. foreach (ICSOutsourcingReceiveDoc info in infos)
  47. {
  48. if (info.MTime < new DateTime(2000, 01, 01))
  49. throw new Exception("请输入正确的操作时间:" + info.MTime);
  50. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  51. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  52. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  53. from rdrecord11 a
  54. left join rdrecords11 b on a.ID=b.ID
  55. left join Department c on a.cDepCode=c.cDepCode
  56. left join Warehouse d on a.cWhCode=d.cWhCode
  57. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  58. where a.cBusType=''";
  59. if (!string.IsNullOrWhiteSpace(info.ORCVTCode))
  60. {
  61. sql += " and a.cCode='{0}'";
  62. }
  63. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  64. {
  65. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  66. }
  67. if (!string.IsNullOrWhiteSpace(info.User))
  68. {
  69. sql += "and a.CMAKER='{2}'";
  70. }
  71. sql = string.Format(sql, info.ORCVTCode, info.MTime, info.User);
  72. dt = DBHelper.SQlReturnData(sql, cmd);
  73. json = JsonConvert.SerializeObject(dt);
  74. if (dt.Rows.Count <= 0 || dt == null)
  75. throw new Exception("委外发料单:" + info.ORCVTCode + ",无信息!");
  76. }
  77. cmd.Transaction.Commit();
  78. return json;
  79. }
  80. catch (Exception ex)
  81. {
  82. cmd.Transaction.Rollback();
  83. log.Error(ex.Message);
  84. throw new Exception(ex.Message);
  85. }
  86. finally
  87. {
  88. if (conn.State == ConnectionState.Open)
  89. {
  90. conn.Close();
  91. }
  92. conn.Dispose();
  93. }
  94. }
  95. /// <summary>
  96. /// 创建委外采购入(到货单)
  97. /// </summary>
  98. /// <param name="Bills"></param>
  99. /// <returns></returns>
  100. public string CreateOutsourcingReceiveDocByPOArrive(List<ICSOutsourcingReceiveDoc> Bills)
  101. {
  102. string msg = "";
  103. int num = 0;
  104. string sql = "";
  105. string iFatherIdTwo = "";
  106. //取得采购入库单单据 表头ID,表体DID
  107. VouchKey key = new VouchKey();
  108. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  109. conn.Open();
  110. SqlTransaction sqlTran = conn.BeginTransaction();
  111. SqlCommand cmd = new SqlCommand();
  112. cmd.Transaction = sqlTran;
  113. cmd.Connection = conn;
  114. try
  115. {
  116. if (Bills.Count <= 0)
  117. {
  118. throw new Exception("传送数据为空!");
  119. }
  120. LogInfo(Bills);
  121. foreach (ICSOutsourcingReceiveDoc head in Bills)
  122. {
  123. num = head.details.Count();
  124. //取得out库单的默认显示模版
  125. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  126. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  127. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  128. DateTime date = DateTime.Now;
  129. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  130. #region 验证数量不能大于到货单数量
  131. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  132. // {
  133. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  134. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  135. // if (dtQty != null && dtQty.Rows.Count > 0)
  136. // {
  137. // //到货单可入数量
  138. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  139. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  140. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  141. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  142. // if (ReceQty + body.Quantity > PUQty)
  143. // {
  144. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  145. // }
  146. // }
  147. // else
  148. // {
  149. // throw new Exception("U8不存在该到货单行!");
  150. // }
  151. // }
  152. #endregion
  153. #region 委外采购入库单 表头
  154. sql = @"INSERT INTO dbo.RdRecord01
  155. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  156. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  157. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  158. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  159. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  160. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  161. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  162. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  163. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  164. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  165. ireturncount ,iverifystate ,iswfcontrolled ,
  166. cModifyPerson ,dnmaketime ,dnverifytime ,
  167. bredvouch ,bCredit ,iPrintCount)
  168. SELECT @ID,1,'01','','',@cWhCode,
  169. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  170. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  171. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  172. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  173. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  174. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  175. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  176. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  177. 0,0,0,
  178. 0,0,0,
  179. '',GETDATE(),GETDATE(),
  180. 0,0,0
  181. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNCode + "'";
  182. cmd.Parameters.Clear();
  183. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  184. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  185. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  186. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  187. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  188. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  189. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  190. cmd.CommandText = sql;
  191. try
  192. {
  193. int count = cmd.ExecuteNonQuery();
  194. if (count <= 0)
  195. {
  196. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  197. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  203. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  204. }
  205. #endregion
  206. #region 采购入库单 表体
  207. foreach (ICSOutsourcingReceiveDocs body in head.details)
  208. {
  209. sql = @"INSERT INTO dbo.rdrecords01
  210. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  211. iFlag ,iSQuantity ,iSNum ,iMoney ,
  212. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  213. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  214. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  215. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  216. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  217. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  218. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  219. 0,0,0,0,
  220. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  221. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  222. c.cVenCode,a.Autoid,0,0,0,0,
  223. 0,a.iTaxRate,0,0,1,d.cCode,0,
  224. 0,0,0,1,c.cCode,
  225. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost
  226. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  227. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  228. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + "'";
  229. cmd.Parameters.Clear();
  230. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  231. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  232. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  233. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  234. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  235. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  236. cmd.CommandText = sql;
  237. try
  238. {
  239. int count = cmd.ExecuteNonQuery();
  240. if (count <= 0)
  241. {
  242. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  243. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  244. }
  245. }
  246. catch (Exception ex)
  247. {
  248. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  249. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  250. }
  251. //更新现存量
  252. key.cBustypeUN = "委外加工";
  253. key.cVouchTypeUN = "01";
  254. key.TableName = "IA_ST_UnAccountVouch01";
  255. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode,"", body.Quantity, key);
  256. //OM_MODetails
  257. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  258. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  259. WHERE a.Autoid='" + body.ODNDetailID + "'";
  260. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  261. //回写PU_ArrivalVouchs失败
  262. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  263. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  264. iChildId--;
  265. }
  266. #endregion
  267. iFatherIdTwo += "'" + iFatherId + "',";
  268. }
  269. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  270. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  271. from rdrecord01 a
  272. left join Department c on a.cDepCode=c.cDepCode
  273. left join Vendor d on a.cVenCode=d.cVenCode
  274. where a.cBusType='' AND A.ID in ({0})
  275. select a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,iArrsId as ODNDetailID
  276. from rdrecord01 a
  277. left join rdrecords01 b on a.ID=b.ID
  278. left join Department c on a.cDepCode=c.cDepCode
  279. left join Vendor d on a.cVenCode=d.cVenCode
  280. where a.cBusType='' AND A.ID in ({0}) ";
  281. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  282. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  283. string RelationName = "details";
  284. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  285. ds.Relations.Add(dr);
  286. msg = DBHelper.DataSetToJson(ds, RelationName);
  287. cmd.Transaction.Commit();
  288. return msg;
  289. }
  290. catch (Exception ex)
  291. {
  292. cmd.Transaction.Rollback();
  293. log.Error(ex.Message);
  294. throw new Exception(ex.Message);
  295. }
  296. finally
  297. {
  298. if (conn.State == ConnectionState.Open)
  299. {
  300. conn.Close();
  301. }
  302. conn.Dispose();
  303. }
  304. }
  305. /// <summary>
  306. /// 创建委外采购入(委外到货单)
  307. /// </summary>
  308. /// <param name="Bills"></param>
  309. /// <returns></returns>
  310. public string CreateOutsourcingReceiveDocByPU(List<ICSOutsourcingReceiveDoc> Bills)
  311. {
  312. string msg = "";
  313. int num = 0;
  314. //取得out库单的默认显示模版
  315. //取得采购入库单单据 表头ID,表体DID
  316. VouchKey key = new VouchKey();
  317. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  318. conn.Open();
  319. SqlTransaction sqlTran = conn.BeginTransaction();
  320. SqlCommand cmd = new SqlCommand();
  321. cmd.Transaction = sqlTran;
  322. cmd.Connection = conn;
  323. try
  324. {
  325. if (Bills.Count <= 0)
  326. {
  327. throw new Exception("传送数据为空!");
  328. }
  329. LogInfo(Bills);
  330. foreach (ICSOutsourcingReceiveDoc head in Bills)
  331. {
  332. num = head.details.Count();
  333. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  334. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  335. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  336. DateTime date = DateTime.Now;
  337. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  338. string sql = "";
  339. #region 验证数量不能大于到货单数量
  340. foreach (ICSOutsourcingReceiveDocs body in head.details)
  341. {
  342. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  343. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  344. if (dtQty != null && dtQty.Rows.Count > 0)
  345. {
  346. //到货单可入数量
  347. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  348. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  349. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  350. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  351. if (ReceQty + body.Quantity > PUQty)
  352. {
  353. throw new Exception("入库数量大于当前到货单行可入库数量!");
  354. }
  355. }
  356. else
  357. {
  358. throw new Exception("U8不存在该到货单行!");
  359. }
  360. }
  361. #endregion
  362. #region 委外采购入库单 表头
  363. sql = @"INSERT INTO dbo.RdRecord01
  364. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  365. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  366. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  367. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  368. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  369. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  370. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  371. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  372. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  373. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  374. ireturncount ,iverifystate ,iswfcontrolled ,
  375. cModifyPerson ,dnmaketime ,dnverifytime ,
  376. bredvouch ,bCredit ,iPrintCount)
  377. SELECT @ID,1,'01','','',@cWhCode,
  378. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  379. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  380. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  381. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  382. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  383. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  384. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  385. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  386. 0,0,0,
  387. 0,0,0,
  388. '',GETDATE(),GETDATE(),
  389. 0,0,0
  390. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode ='" + head.ODNCode + "'";
  391. cmd.Parameters.Clear();
  392. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  393. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  394. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  395. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  396. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  397. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  398. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  399. cmd.CommandText = sql;
  400. try
  401. {
  402. int count = cmd.ExecuteNonQuery();
  403. if (count <= 0)
  404. {
  405. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  406. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  412. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  413. }
  414. #endregion
  415. #region 采购入库单 表体
  416. int irowno = 0;
  417. foreach (ICSOutsourcingReceiveDocs body in head.details)
  418. {
  419. sql = @"INSERT INTO dbo.rdrecords01
  420. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  421. iFlag ,iSQuantity ,iSNum ,iMoney ,
  422. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  423. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  424. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  425. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  426. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  427. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  428. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  429. 0,0,0,0,
  430. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  431. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  432. c.cVenCode,a.Autoid,0,0,0,0,
  433. 0,a.iTaxRate,0,0,1,d.cCode,0,
  434. 0,0,0,1,c.cCode,
  435. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost
  436. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  437. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  438. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + "'";
  439. cmd.Parameters.Clear();
  440. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  441. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  442. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  443. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  444. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  445. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  446. cmd.CommandText = sql;
  447. try
  448. {
  449. int count = cmd.ExecuteNonQuery();
  450. if (count <= 0)
  451. {
  452. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  453. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  454. }
  455. }
  456. catch (Exception ex)
  457. {
  458. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  459. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  460. }
  461. //更新现存量
  462. key.cBustypeUN = "委外加工";
  463. key.cVouchTypeUN = "01";
  464. key.TableName = "IA_ST_UnAccountVouch01";
  465. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  466. //OM_MODetails
  467. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  468. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  469. WHERE a.Autoid='" + body.ODNDetailID + "'";
  470. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  471. //回写PU_ArrivalVouchs失败
  472. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  473. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  474. //更新待入库数量
  475. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  476. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  477. {
  478. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  479. //{
  480. // sql += " and cBatch='" + body.cBatch + "'";
  481. //}
  482. }
  483. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  484. // #region 判断现存量是否足够
  485. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  486. // BEGIN
  487. // DECLARE @MSG NVARCHAR(100)
  488. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  489. // RAISERROR(@MSG,16,1)
  490. // END";
  491. // cmd.CommandText = sql;
  492. // cmd.ExecuteNonQuery();
  493. // #endregion
  494. }
  495. #endregion
  496. }
  497. //GetSerialCode(iFatherId.ToString());
  498. cmd.Transaction.Commit();
  499. return msg;
  500. }
  501. catch (Exception ex)
  502. {
  503. cmd.Transaction.Rollback();
  504. log.Error(ex.Message);
  505. throw new Exception(ex.Message);
  506. }
  507. finally
  508. {
  509. if (conn.State == ConnectionState.Open)
  510. {
  511. conn.Close();
  512. }
  513. conn.Dispose();
  514. }
  515. }
  516. /// <summary>
  517. /// 创建委外采购入(委外到货单)
  518. /// </summary>
  519. /// <param name="Bills"></param>
  520. /// <returns></returns>
  521. public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  522. {
  523. string msg = "";
  524. int num = 0;
  525. string iFatherIdTwo = "";
  526. string sql = "";
  527. DataTable dt = null;
  528. VouchKey key = new VouchKey();
  529. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  530. conn.Open();
  531. SqlTransaction sqlTran = conn.BeginTransaction();
  532. SqlCommand cmd = new SqlCommand();
  533. cmd.Transaction = sqlTran;
  534. cmd.Connection = conn;
  535. try
  536. {
  537. if (Bills.Count <= 0)
  538. {
  539. throw new Exception("传送数据为空!");
  540. }
  541. LogInfo(Bills);
  542. foreach (ICSOutsourcingReceiveDoc head in Bills)
  543. {
  544. string RdCode= DBHelper.GetRDCode("采购入库", cmd);
  545. num = head.details.Count();
  546. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  547. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  548. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  549. DateTime date = DateTime.Now;
  550. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  551. #region 验证数量不能大于到货单数量
  552. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  553. //{
  554. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  555. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  556. // if (dtQty != null && dtQty.Rows.Count > 0)
  557. // {
  558. // //到货单可入数量
  559. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  560. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  561. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  562. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  563. // if (ReceQty + body.Quantity > PUQty)
  564. // {
  565. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  566. // }
  567. // }
  568. // else
  569. // {
  570. // throw new Exception("U8不存在该到货单行!");
  571. // }
  572. //}
  573. #endregion
  574. #region 委外采购入库单 表头
  575. sql = @"INSERT INTO dbo.RdRecord01
  576. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  577. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  578. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  579. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  580. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  581. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  582. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  583. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  584. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  585. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  586. ireturncount ,iverifystate ,iswfcontrolled ,
  587. cModifyPerson ,dnmaketime ,dnverifytime ,
  588. bredvouch ,bCredit ,iPrintCount)
  589. SELECT @ID,1,'01','','',@cWhCode,
  590. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@RdCode,@cDepCode,a.cPersonCode,'01',
  591. a.cVenCode,'',a.cCode,@cHandler,a.cMemo,
  592. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  593. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  594. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  595. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  596. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  597. '',a.iTaxRate,a.nflat,a.cexch_name,0,
  598. 0,0,0,
  599. 0,0,0,
  600. '',GETDATE(),GETDATE(),
  601. 0,0,0
  602. FROM dbo.OM_MOMain a WHERE a.cCode='" + head.OOCode + "'";
  603. cmd.Parameters.Clear();
  604. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  605. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  606. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  607. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  608. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  609. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  610. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  611. cmd.Parameters.Add(new SqlParameter("@RdCode", RdCode));
  612. cmd.CommandText = sql;
  613. try
  614. {
  615. int count = cmd.ExecuteNonQuery();
  616. if (count <= 0)
  617. {
  618. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  619. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  620. }
  621. }
  622. catch (Exception ex)
  623. {
  624. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  625. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  626. }
  627. #endregion
  628. #region 采购入库单 表体
  629. foreach (ICSOutsourcingReceiveDocs body in head.details)
  630. {
  631. sql = @"INSERT INTO dbo.rdrecords01
  632. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  633. iFlag ,iSQuantity ,iSNum ,iMoney ,
  634. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  635. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  636. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  637. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  638. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode
  639. ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  640. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  641. 0,0,0,0,
  642. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  643. a.cItem_class,a.cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  644. @VenCode,'',0,0,0,0,
  645. 0,d.nflat,0,0,1,'',0,
  646. 0,0,0,1,'',
  647. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice
  648. FROM dbo.OM_MODetails a
  649. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  650. where a.MODetailsID='" + body.OODetailID + "'";
  651. cmd.Parameters.Clear();
  652. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  653. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  654. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  655. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  656. cmd.Parameters.Add(new SqlParameter("@irowno",body.Sequence));
  657. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  658. cmd.Parameters.Add(new SqlParameter("@VenCode", head.VenCode));
  659. cmd.CommandText = sql;
  660. try
  661. {
  662. int count = cmd.ExecuteNonQuery();
  663. if (count <= 0)
  664. {
  665. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  666. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  667. }
  668. }
  669. catch (Exception ex)
  670. {
  671. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  672. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  673. }
  674. //更新现存量
  675. key.cBustypeUN = "委外加工";
  676. key.cVouchTypeUN = "01";
  677. key.TableName = "IA_ST_UnAccountVouch01";
  678. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  679. //OM_MODetails
  680. sql = @"UPDATE a SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  681. FROM dbo.OM_MODetails a
  682. WHERE a.MODetailsID='" + body.OODetailID + "'";
  683. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  684. //更新待入库数量
  685. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  686. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  687. {
  688. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  689. //{
  690. // sql += " and cBatch='" + body.cBatch + "'";
  691. //}
  692. }
  693. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  694. iChildId--;
  695. }
  696. iFatherIdTwo += "'" + iFatherId + "',";
  697. #endregion
  698. }
  699. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  700. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  701. from rdrecord01 a
  702. left join Department c on a.cDepCode=c.cDepCode
  703. left join Vendor d on a.cVenCode=d.cVenCode
  704. where a.cBusType='' AND A.ID in ({0})
  705. select a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,b.iOMoDID as OODetailID
  706. from rdrecord01 a
  707. left join rdrecords01 b on a.ID=b.ID
  708. left join Department c on a.cDepCode=c.cDepCode
  709. left join Vendor d on a.cVenCode=d.cVenCode
  710. where a.cBusType='' AND A.ID in ({0}) ";
  711. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  712. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  713. string RelationName = "details";
  714. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  715. ds.Relations.Add(dr);
  716. cmd.Transaction.Commit();
  717. msg = DBHelper.DataSetToJson(ds, RelationName);
  718. return msg;
  719. }
  720. catch (Exception ex)
  721. {
  722. cmd.Transaction.Rollback();
  723. log.Error(ex.Message);
  724. throw new Exception(ex.Message);
  725. }
  726. finally
  727. {
  728. if (conn.State == ConnectionState.Open)
  729. {
  730. conn.Close();
  731. }
  732. conn.Dispose();
  733. }
  734. }
  735. /// <summary>
  736. /// 审核委外采购入库
  737. /// </summary>
  738. /// <param name="infos"></param>
  739. /// <returns></returns>
  740. public string Approve(List<ICSOutsourcingReceiveDoc> infos)
  741. {
  742. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  743. string json = "";
  744. if (infos.Count <= 0)
  745. {
  746. throw new Exception("传送数据为空!");
  747. }
  748. string res = string.Empty;
  749. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  750. conn.Open();
  751. SqlTransaction sqlTran = conn.BeginTransaction();
  752. SqlCommand cmd = new SqlCommand();
  753. cmd.Transaction = sqlTran;
  754. cmd.Connection = conn;
  755. try
  756. {
  757. string sql = string.Empty;
  758. foreach (ICSOutsourcingReceiveDoc info in infos)
  759. {
  760. if (info.MTime < new DateTime(2000, 01, 01))
  761. throw new Exception("请输入正确的操作时间:" + info.MTime);
  762. sql = @"UPDATE RdRecord01 SET cHandler ='" + info.User + @"' ,
  763. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  764. sql = string.Format(sql, info.ID);
  765. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  766. }
  767. cmd.Transaction.Commit();
  768. return json;
  769. }
  770. catch (Exception ex)
  771. {
  772. cmd.Transaction.Rollback();
  773. log.Error(ex.Message);
  774. throw new Exception(ex.Message);
  775. }
  776. finally
  777. {
  778. if (conn.State == ConnectionState.Open)
  779. {
  780. conn.Close();
  781. }
  782. conn.Dispose();
  783. }
  784. }
  785. /// <summary>
  786. /// 删除委外采购入库单
  787. /// </summary>
  788. /// <param name="infos"></param>
  789. /// <returns></returns>
  790. public string Delete(List<ICSOutsourcingReceiveDoc> infos)
  791. {
  792. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  793. if (infos.Count <= 0)
  794. {
  795. throw new Exception("传送数据为空!");
  796. }
  797. string res = string.Empty;
  798. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  799. conn.Open();
  800. SqlTransaction sqlTran = conn.BeginTransaction();
  801. SqlCommand cmd = new SqlCommand();
  802. cmd.Transaction = sqlTran;
  803. cmd.Connection = conn;
  804. try
  805. {
  806. string sql = string.Empty;
  807. foreach (ICSOutsourcingReceiveDoc info in infos)
  808. {
  809. if (info.MTime < new DateTime(2000, 01, 01))
  810. throw new Exception("请输入正确的操作时间:" + info.MTime);
  811. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  812. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  813. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  814. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  815. // WHERE a.ID='" + info.ID + "'";
  816. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  817. }
  818. cmd.Transaction.Commit();
  819. return res;
  820. ;
  821. }
  822. catch (Exception ex)
  823. {
  824. cmd.Transaction.Rollback();
  825. log.Error(ex.Message);
  826. throw new Exception(ex.Message);
  827. }
  828. finally
  829. {
  830. if (conn.State == ConnectionState.Open)
  831. {
  832. conn.Close();
  833. }
  834. conn.Dispose();
  835. }
  836. }
  837. /// <summary>
  838. ///
  839. /// 记录日志
  840. /// </summary>
  841. /// <param name="Bills"></param>
  842. private void LogInfo(List<ICSOutsourcingReceiveDoc> Bills)
  843. {
  844. string HeadList = string.Empty;
  845. string BodyList = string.Empty;
  846. foreach (ICSOutsourcingReceiveDoc head in Bills)
  847. {
  848. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  849. foreach (ICSOutsourcingReceiveDocs body in head.details)
  850. {
  851. BodyList += "\r\n 表体主键ID: " + body.ODNDetailID + ",数量:" + body.Quantity;
  852. }
  853. }
  854. log.Info(HeadList);
  855. log.Info(BodyList);
  856. }
  857. public string GetSerialCode(string ID)
  858. {
  859. string sql = "EXEC Addins_GetSerialCode '{0}'";
  860. sql = string.Format(sql, new object[] { ID });
  861. return DBHelper.ExecuteScalar(CommandType.Text, sql, connString).ToString();
  862. }
  863. }
  864. }