LogicHandler.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Windows.Forms;
  7. using UAS_LabelMachine.Entity;
  8. namespace UAS_LabelMachine.PublicMethod
  9. {
  10. class LogicHandler
  11. {
  12. public static DataHelper dh = SystemInf.dh;
  13. /// <summary>
  14. /// 特殊业务过滤逻辑,目前只有海创在使用
  15. /// </summary>
  16. /// <param name="iCustCode"></param>
  17. /// <param name="iBrand"></param>
  18. /// <param name="iSpec"></param>
  19. /// <param name="iQty"></param>
  20. /// <param name="iDateCode"></param>
  21. /// <param name="iLotno"></param>
  22. /// <param name="oSpec"></param>
  23. /// <param name="oQty"></param>
  24. /// <param name="oDatecode"></param>
  25. /// <param name="oLotno"></param>
  26. public static void FilterData(string iCustCode, string iBrand, string iSpec, string iQty, string iDateCode, string iLotno, out string oSpec, out string oQty, out string oDatecode, out string oLotno)
  27. {
  28. oSpec = "";
  29. oQty = "";
  30. oDatecode = "";
  31. oLotno = "";
  32. string[] param = new string[] { iCustCode, iBrand, iSpec, iQty, iDateCode, iLotno, oSpec, oQty, oDatecode, oLotno };
  33. dh.CallProcedure("sp_datafilter", ref param);
  34. oSpec = param[6];
  35. oQty = param[7];
  36. oDatecode = param[8];
  37. oLotno = param[9];
  38. }
  39. /// <summary>
  40. /// 通用方法,日期格式转换,转换成指定的年月日
  41. /// </summary>
  42. /// <param name="iDatecode"></param>
  43. /// <param name="iCustcode"></param>
  44. /// <param name="oYear"></param>
  45. /// <param name="oMonth"></param>
  46. /// <param name="oDay"></param>
  47. /// <param name="oDate"></param>
  48. public static void GetTimeFromDatecode(string iDatecode, string iCustcode, out string oYear, out string oMonth, out string oDay, out string oDate)
  49. {
  50. oYear = "0";
  51. oMonth = "0";
  52. oDay = "0";
  53. oDate = "19000101";
  54. string[] param = new string[] { iDatecode, iCustcode, oYear, oMonth, oDay, oDate };
  55. dh.CallProcedure("GETTIMEFROMDATECODE", ref param);
  56. oYear = param[2];
  57. oMonth = param[3];
  58. oDay = param[4];
  59. oDate = param[5];
  60. }
  61. /// <summary>
  62. /// 特殊业务条码处理,目前针对海创长城客户
  63. /// </summary>
  64. /// <param name="iInoutno"></param>
  65. /// <param name="oSQL"></param>
  66. public static void CustBarCode(string iInoutno, out string oSQL)
  67. {
  68. oSQL = "";
  69. string[] param = new string[] { iInoutno, oSQL };
  70. dh.CallProcedure("SP_CUSTBARCODE", ref param);
  71. oSQL = param[1];
  72. }
  73. /// <summary>
  74. /// 获取生成条码的SQL,目前添加用户海创和凯而高,凯而高有特殊定制逻辑
  75. /// </summary>
  76. /// <param name="iPiid"></param>
  77. /// <param name="iCustCode"></param>
  78. /// <param name="oSQL"></param>
  79. public static void GenerateBarCode(string iPiid, string iCustCode, out string oSQL)
  80. {
  81. oSQL = "";
  82. string[] param = new string[] { iPiid, iCustCode, oSQL };
  83. dh.CallProcedure("sp_GenerateBarCode", ref param);
  84. oSQL = param[2];
  85. }
  86. public static void ImportExcel(string iMaster, string iFileName, string iPiID, string iInOutNo, string iCustCode)
  87. {
  88. switch (iMaster)
  89. {
  90. //海创
  91. case "N_HC":
  92. ImportExcel_HC(iFileName, iPiID, iInOutNo, iCustCode);
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. //海创的导入方法
  99. public static void ImportExcel_HC(string iFileName, string iPiID, string iInOutNo, string iCustCode)
  100. {
  101. StringBuilder sql = new StringBuilder();
  102. //获取最大的流水号
  103. DataTable dt = ExcelHandler.ExcelToDataTable(iFileName, true);
  104. if (dt == null)
  105. {
  106. MessageBox.Show("文件" + iFileName + "被占用或者格式不正确");
  107. return;
  108. }
  109. string outsql = "";
  110. GenerateBarCode(iPiID, iCustCode, out outsql);
  111. sql.Clear();
  112. sql.Append(outsql);
  113. DataTable dt1 = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  114. //需要插入的参数
  115. ArrayList<string> barcode = new ArrayList<string>();
  116. ArrayList<string> custbarcode = new ArrayList<string>();
  117. ArrayList<string> datecode = new ArrayList<string>();
  118. ArrayList<string> lotno = new ArrayList<string>();
  119. ArrayList<string> pib_inqty = new ArrayList<string>();
  120. ArrayList<string> piboutboxcode2 = new ArrayList<string>();
  121. ArrayList<string> pdordercode = new ArrayList<string>();
  122. ArrayList<string> pdorderdetno = new ArrayList<string>();
  123. ArrayList<string> pdpdno = new ArrayList<string>();
  124. ArrayList<string> pdid = new ArrayList<string>();
  125. ArrayList<string> prbrand = new ArrayList<string>();
  126. ArrayList<string> pdprodcode = new ArrayList<string>();
  127. ArrayList<string> pib_custoutboxcode = new ArrayList<string>();
  128. ArrayList<string> brand = new ArrayList<string>();
  129. ArrayList<string> madein = new ArrayList<string>();
  130. ArrayList<string> prdetail = new ArrayList<string>();
  131. ArrayList<string> ordercode = new ArrayList<string>();
  132. ArrayList<string> size = new ArrayList<string>();
  133. ArrayList<string> weight = new ArrayList<string>();
  134. //用于数量的校验
  135. Dictionary<string, decimal> sumqty = new Dictionary<string, decimal>();
  136. Dictionary<string, Dictionary<string, decimal>> outsumqty = new Dictionary<string, Dictionary<string, decimal>>();
  137. for (int i = 0; i < dt.Rows.Count; i++)
  138. {
  139. string pr_spec = dt.Rows[i]["规格型号"].ToString().Trim();
  140. if (pr_spec.Trim() == "")
  141. {
  142. break;
  143. }
  144. string qty = Regex.Replace(dt.Rows[i]["数量"].ToString(), @"[^\d]*", ""); ;
  145. //累计Excel该型号的数量
  146. if (!sumqty.ContainsKey(pr_spec))
  147. {
  148. sumqty.Add(pr_spec, 0);
  149. }
  150. //累计Excel该型号的数量
  151. sumqty[pr_spec] += decimal.Parse(qty);
  152. }
  153. for (int i = 0; i < dt1.Rows.Count; i++)
  154. {
  155. //不包含型号则进行添加
  156. string pr_spec = dt1.Rows[i]["pr_spec"].ToString().Trim();
  157. string pd_pdno = dt1.Rows[i]["pd_pdno"].ToString();
  158. decimal pd_outqty = decimal.Parse(dt1.Rows[i]["pd_outqty"].ToString());
  159. if (!outsumqty.ContainsKey(pr_spec))
  160. {
  161. Dictionary<string, decimal> dic = new Dictionary<string, decimal>();
  162. dic.Add(pd_pdno, pd_outqty);
  163. outsumqty.Add(pr_spec, dic);
  164. }
  165. else
  166. {
  167. outsumqty[pr_spec].Add(pd_pdno, pd_outqty);
  168. }
  169. }
  170. //上一个的最小外箱号,如果箱号没有发生变化则外箱条码也不变化
  171. for (int i = 0; i < dt.Rows.Count; i++)
  172. {
  173. string pr_spec = dt.Rows[i]["规格型号"].ToString().Trim();
  174. string 品牌 = dt.Rows[i]["品牌"].ToString().Trim();
  175. string 产地 = dt.Rows[i]["产地"].ToString().Trim();
  176. string 尺寸 = dt.Rows[i]["尺寸"].ToString().Trim();
  177. string 重量 = dt.Rows[i]["重量"].ToString().Trim();
  178. string 产品名称 = dt.Rows[i]["产品名称"].ToString().Trim();
  179. string 订单编号 = dt.Rows[i]["订单编号"].ToString().Trim();
  180. if (pr_spec.Trim() == "")
  181. {
  182. break;
  183. }
  184. string qty = dt.Rows[i]["数量"].ToString();
  185. string DateCode = "";
  186. string LotNo = "";
  187. if (dt.Columns.Contains("DC/LOT NO"))
  188. {
  189. if (dt.Rows[i]["DC/LOT NO"].ToString().IndexOf("-") > 0)
  190. {
  191. DateCode = dt.Rows[i]["DC/LOT NO"].ToString().Split('-')[0];
  192. LotNo = dt.Rows[i]["DC/LOT NO"].ToString().Split('-')[1];
  193. }
  194. else if (dt.Rows[i]["DC/LOT NO"].ToString().IndexOf(" ") > 0)
  195. {
  196. DateCode = dt.Rows[i]["DC/LOT NO"].ToString().Split(' ')[0];
  197. LotNo = dt.Rows[i]["DC/LOT NO"].ToString().Split(' ')[1];
  198. }
  199. }
  200. if (dt.Columns.Contains("DC") && DateCode == "")
  201. {
  202. DateCode = dt.Rows[i]["DC"].ToString();
  203. }
  204. if (dt.Columns.Contains("LOT NO") && LotNo == "")
  205. {
  206. LotNo = dt.Rows[i]["LOT NO"].ToString();
  207. }
  208. string pib_outboxcode2 = dt.Rows[i]["箱号"].ToString();
  209. DataRow[] dr = dt1.Select("pr_spec='" + pr_spec + "'");
  210. if (dr.Length == 0)
  211. {
  212. FilterData(iCustCode, "FUDAN", pr_spec, qty, DateCode, LotNo, out pr_spec, out qty, out DateCode, out LotNo);
  213. dr = dt1.Select("pr_spec='" + pr_spec + "'");
  214. }
  215. else
  216. {
  217. FilterData(iCustCode, dr[0]["pr_brand"].ToString(), pr_spec, qty, DateCode, LotNo, out pr_spec, out qty, out DateCode, out LotNo);
  218. }
  219. if (dr.Length == 0)
  220. {
  221. MessageBox.Show("出货单" + iInOutNo + "不包含型号" + pr_spec);
  222. return;
  223. }
  224. decimal outqty = decimal.Parse(Regex.Replace(qty, @"[^\d]*", ""));
  225. for (int k = 0; k < dr.Length; k++)
  226. {
  227. string pd_orderdetno = dr[k]["pd_orderdetno"].ToString();
  228. string pd_prodcode = dr[k]["pd_prodcode"].ToString();
  229. string pd_ordercode = dr[k]["pd_ordercode"].ToString();
  230. string pd_piid = dr[k]["pd_piid"].ToString();
  231. string pr_brand = dr[k]["pr_brand"].ToString();
  232. string pd_pdno = dr[k]["pd_pdno"].ToString();
  233. string pd_id = dr[k]["pd_id"].ToString();
  234. string pd_piclass = dr[k]["pd_piclass"].ToString();
  235. string pr_id = dr[k]["pr_id"].ToString();
  236. string pr_madein = dr[k]["pr_madein"].ToString();
  237. //已导入数量
  238. decimal zxbzs = decimal.Parse(dr[k]["pr_zxbzs"].ToString());
  239. decimal pd_outqty = 0;
  240. decimal barcodenum = 0;
  241. if (outsumqty[pr_spec][pd_pdno] == 0)
  242. {
  243. continue;
  244. }
  245. //如果导入行的数量小于改明细的出货数量
  246. if (outsumqty[pr_spec][pd_pdno] >= outqty)
  247. {
  248. //使用导入明细行数量
  249. pd_outqty = outqty;
  250. }
  251. else
  252. {
  253. ////使用该出货单明细行数量,并且数量置为0
  254. pd_outqty = outsumqty[pr_spec][pd_pdno];
  255. }
  256. string pib_barcode = dh.getFieldDataByCondition("prodiobarcode", "max(pib_barcode)", "PIB_INOUTNO='" + iInOutNo + "'").ToString();
  257. if (pd_outqty % zxbzs != 0)
  258. {
  259. barcodenum = Math.Floor(pd_outqty / zxbzs) + 1;
  260. //如果有余数先加上所有最小包最后加尾数
  261. for (int j = 0; j < barcodenum - 1; j++)
  262. {
  263. barcode.Add(BaseUtil.BarcodeMethod1(pd_id, pr_id, pib_barcode));
  264. custbarcode.Add(BarcodeMethod1(UAS_出货标签打印.PrefixFixed, UAS_出货标签打印.Suffix, UAS_出货标签打印.SerialNumIndex, UAS_出货标签打印.SerialNumLength, UAS_出货标签打印.Radix));
  265. pib_custoutboxcode.Add(pib_outboxcode2);
  266. piboutboxcode2.Add(pib_outboxcode2);
  267. pib_inqty.Add(zxbzs);
  268. datecode.Add(DateCode);
  269. lotno.Add(LotNo);
  270. pdordercode.Add(订单编号 == "" ? pd_ordercode : 订单编号);
  271. pdorderdetno.Add(pd_orderdetno);
  272. pdpdno.Add(pd_pdno);
  273. pdid.Add(pd_id);
  274. prbrand.Add(品牌 == "" ? pr_brand : 品牌);
  275. madein.Add(产地 == "" ? pr_madein : 产地);
  276. size.Add(尺寸);
  277. weight.Add(重量);
  278. prdetail.Add(产品名称);
  279. ordercode.Add(订单编号);
  280. pdprodcode.Add(pd_prodcode);
  281. }
  282. custbarcode.Add(BarcodeMethod1(UAS_出货标签打印.PrefixFixed, UAS_出货标签打印.Suffix, UAS_出货标签打印.SerialNumIndex, UAS_出货标签打印.SerialNumLength, UAS_出货标签打印.Radix));
  283. pib_custoutboxcode.Add(pib_outboxcode2);
  284. barcode.Add(BaseUtil.BarcodeMethod1(pd_id, pr_id, pib_barcode));
  285. pib_inqty.Add(pd_outqty % zxbzs);
  286. piboutboxcode2.Add(pib_outboxcode2);
  287. datecode.Add(DateCode);
  288. lotno.Add(LotNo);
  289. pdordercode.Add(订单编号 == "" ? pd_ordercode : 订单编号);
  290. pdorderdetno.Add(pd_orderdetno);
  291. pdpdno.Add(pd_pdno);
  292. pdid.Add(pd_id);
  293. prbrand.Add(品牌 == "" ? pr_brand : 品牌);
  294. madein.Add(产地 == "" ? pr_madein : 产地);
  295. size.Add(尺寸);
  296. weight.Add(重量);
  297. prdetail.Add(产品名称);
  298. ordercode.Add(订单编号);
  299. pdprodcode.Add(pd_prodcode);
  300. }
  301. else
  302. {
  303. barcodenum = pd_outqty / zxbzs;
  304. for (int j = 0; j < barcodenum; j++)
  305. {
  306. barcode.Add(BaseUtil.BarcodeMethod1(pd_id, pr_id, pib_barcode));
  307. custbarcode.Add(BarcodeMethod1(UAS_出货标签打印.PrefixFixed, UAS_出货标签打印.Suffix, UAS_出货标签打印.SerialNumIndex, UAS_出货标签打印.SerialNumLength, UAS_出货标签打印.Radix));
  308. pib_custoutboxcode.Add(pib_outboxcode2);
  309. pib_inqty.Add(zxbzs);
  310. piboutboxcode2.Add(pib_outboxcode2);
  311. datecode.Add(DateCode);
  312. lotno.Add(LotNo);
  313. pdordercode.Add(订单编号 == "" ? pd_ordercode : 订单编号);
  314. pdorderdetno.Add(pd_orderdetno);
  315. pdpdno.Add(pd_pdno);
  316. pdid.Add(pd_id);
  317. prbrand.Add(品牌 == "" ? pr_brand : 品牌);
  318. madein.Add(产地 == "" ? pr_madein : 产地);
  319. size.Add(尺寸);
  320. weight.Add(重量);
  321. prdetail.Add(产品名称);
  322. pdprodcode.Add(pd_prodcode);
  323. }
  324. }
  325. outqty -= outsumqty[pr_spec][pd_pdno];
  326. outsumqty[pr_spec][pd_pdno] -= pd_outqty;
  327. }
  328. }
  329. string Lastoutbox = "";
  330. string Lastoutboxcode = "";
  331. List<string> custoutboxcode = new List<string>();
  332. //根据记录的箱号,在料盘的流水之后拼接外箱的流水
  333. for (int i = 0; i < pib_custoutboxcode.Count; i++)
  334. {
  335. if (Lastoutbox != pib_custoutboxcode[i].ToString())
  336. {
  337. Lastoutboxcode = BarcodeMethod1(UAS_出货标签打印.PrefixFixed, UAS_出货标签打印.Suffix, UAS_出货标签打印.SerialNumIndex, UAS_出货标签打印.SerialNumLength, UAS_出货标签打印.Radix);
  338. custoutboxcode.Add(Lastoutboxcode);
  339. Lastoutbox = pib_custoutboxcode[i].ToString();
  340. }
  341. else
  342. {
  343. custoutboxcode.Add(Lastoutboxcode);
  344. }
  345. }
  346. //插叙所有的统计数量
  347. string ErrMsg = "";
  348. foreach (var item in sumqty)
  349. {
  350. dt = (DataTable)dh.ExecuteSql("select nvl(sum(pd_outqty),0)pd_outqty,pr_spec from prodiodetail left join product on pr_code=pd_prodcode where pd_piid='" + iPiID + "' group by pr_spec", "select");
  351. dt1 = (DataTable)dh.ExecuteSql("select nvl(sum(pib_qty),0)pib_qty,pr_spec from prodiobarcode left join product on pib_prodcode=pr_code where pib_piid='" + iPiID + "' group by pr_spec", "select");
  352. DataRow[] dr = dt.Select("pr_spec='" + item.Key + "'");
  353. DataRow[] dr1 = dt1.Select("pr_spec='" + item.Key + "'");
  354. if (dr.Length > 0)
  355. {
  356. decimal pd_outqty = decimal.Parse(dr[0]["pd_outqty"].ToString());
  357. decimal pib_qty = 0;
  358. //如果有已导入的数量
  359. if (dr1.Length > 0)
  360. {
  361. pib_qty = decimal.Parse(dr1[0]["pib_qty"].ToString());
  362. }
  363. //本次导入的数量和原有导入数量累加和出库数量做对比
  364. if (pd_outqty < item.Value + pib_qty)
  365. {
  366. ErrMsg += "型号" + dr[0]["pr_spec"].ToString() + "出货数量" + pd_outqty + ",已导入数量" + pib_qty + ",本次导入" + item.Value + ",超出数量" + (item.Value + pib_qty - pd_outqty) + "\n";
  367. }
  368. }
  369. }
  370. if (ErrMsg == "")
  371. {
  372. if (pdprodcode.Count > 0)
  373. {
  374. sql.Clear();
  375. sql.Append("insert into prodiobarcode (PIB_ID,PIB_PRODCODE,pib_inman,PIB_INDATE,PIB_INOUTNO,PIB_PIID,pib_brand,PIB_BARCODE,PIB_PDNO,");
  376. sql.Append("PIB_PDID,PIB_PICLASS,PIB_QTY,pib_datecode,pib_lotno,PIB_IFPRINT,PIB_IFPICK,PIB_ORDERCODE,pib_orderdetno,");
  377. sql.Append("pib_outboxcode2,pib_custbarcode,pib_custoutboxcode,pib_madein,pib_size,pib_weight,pib_prdetail)values(prodiobarcode_seq.nextval,:pd_prodcode,'" + User.UserName + "',sysdate,");
  378. sql.Append("'" + iInOutNo + "'," + iPiID + ",:pr_brand,:barcode,:pd_pdno,:pd_id,'',:pib_inqty,");
  379. sql.Append(":pib_datecode,:pib_lotno,0,-1,:pd_ordercode,:pd_orderdetno,:outboxcode,:custbarcode,:pib_custoutboxcode,:pib_madein,:pib_size,:pib_weight,:pib_prdetail)");
  380. dh.BatchInsert(sql.ToString(), new string[] { "pd_prodcode", "pr_brand", "barcode", "pd_pdno", "pd_id", "pib_inqty", "pib_datecode", "pib_lotno", "pd_ordercode", "pd_orderdetno", "outboxcode", "custbarcode", "pib_custoutboxcode", "pib_madein", "pib_size", "pib_weight", "pib_prdetail" }, pdprodcode.ToArray(), prbrand.ToArray(), barcode.ToArray(), pdpdno.ToArray(), pdid.ToArray(), pib_inqty.ToArray(), datecode.ToArray(), lotno.ToArray(), pdordercode.ToArray(), pdorderdetno.ToArray(), piboutboxcode2.ToArray(), custbarcode.ToArray(), custoutboxcode.ToArray(), madein.ToArray(), size.ToArray(), weight.ToArray(), prdetail.ToArray());
  381. //更新流水
  382. dh.UpdateByCondition("RuleMaxNum", "rmn_maxnumber='" + UAS_出货标签打印.custserialnum + "'", "rmn_nrcode='" + UAS_出货标签打印.NrCode + "' and rmn_prefix='" + UAS_出货标签打印.Prefix + "'");
  383. }
  384. else
  385. {
  386. MessageBox.Show("解析数据不包含出货单数据,请重新导入", "提示");
  387. }
  388. }
  389. else
  390. {
  391. MessageBox.Show(ErrMsg, "超出数量提醒");
  392. }
  393. }
  394. //生成客户条码
  395. public static string BarcodeMethod1(string Prefix, string Suffix, int Index, int Length, int radix)
  396. {
  397. string str = Prefix;
  398. //如果是流水则需要在前面加0
  399. string serialcode = BaseUtil.DToAny(UAS_出货标签打印.custserialnum, radix);
  400. for (int j = serialcode.ToString().Length; j < Length; j++)
  401. {
  402. serialcode = "0" + serialcode;
  403. }
  404. str += serialcode;
  405. str += Suffix;
  406. UAS_出货标签打印.custserialnum = UAS_出货标签打印.custserialnum + 1;
  407. return str;
  408. }
  409. }
  410. }