SoftBedHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using JLHHJSvr.BLL;
  2. using JLHHJSvr.Com.Model;
  3. using JLHHJSvr.LJException;
  4. using JLHHJSvr.Tools;
  5. using LJLib.DAL.SQL;
  6. using NPOI.SS.Formula.Functions;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Web;
  14. namespace JLHHJSvr.Helper
  15. {
  16. internal class SoftBedHelper : HelperBase
  17. {
  18. /// <summary>
  19. /// 获取软床报价单据
  20. /// </summary>
  21. /// <param name="billid"></param>
  22. /// <param name="fields"></param>
  23. /// <param name="bill"></param>
  24. public u_softbed GetSoftBed(int billid,string fields = null)
  25. {
  26. fields = fields ?? @"softbed_id,softbed_code,softbed_relcode,softbed_name,deptid,create_date,create_emp,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  27. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  28. dept_cost,foreign_cost";
  29. var bill = new u_softbed() { softbed_id = billid };
  30. if (DbSqlHelper.SelectOne(cmd, bill, fields) == 0) return null;
  31. return bill;
  32. }
  33. /// <summary>
  34. /// 获取软床报价单据明细
  35. /// </summary>
  36. /// <param name="billid"></param>
  37. /// <param name="fields"></param>
  38. /// <param name="mxlist"></param>
  39. public void GetSoftBedMxList(int billid,string fields,out List<u_softbed_mx> mxlist,out List<u_configure_codemx> codeList)
  40. {
  41. fields = fields ?? @"softbed_id,printid,pzid,pz_printid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  42. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,pzname,pzmxname";
  43. mxlist = new List<u_softbed_mx>();
  44. var selectStr = @"SELECT u_softbed_mx.softbed_id
  45. ,u_softbed_mx.printid
  46. ,u_softbed_mx.pzid
  47. ,u_softbed_mx.pz_printid
  48. ,u_softbed_mx.mtrlid
  49. ,u_softbed_mx.mtrlname
  50. ,u_softbed_mx.mtrlcode
  51. ,u_softbed_mx.mtrlmode
  52. ,u_softbed_mx.unit
  53. ,u_softbed_mx.has_type
  54. ,u_softbed_mx.allow_edit
  55. ,u_softbed_mx.cutting_length
  56. ,u_softbed_mx.cutting_width
  57. ,u_softbed_mx.cutting_qty
  58. ,u_softbed_mx.useqty
  59. ,u_softbed_mx.use_formula
  60. ,u_softbed_mx.use_formula_str
  61. ,u_softbed_mx.actual_useqty
  62. ,u_softbed_mx.loss_rate
  63. ,u_softbed_mx.price
  64. ,u_softbed_mx.price_formula
  65. ,u_softbed_mx.price_formula_str
  66. ,u_softbed_mx.cost_price
  67. ,u_softbed_mx.cost_amt
  68. ,u_configure_code.name AS pzname
  69. ,u_configure_codemx.namemx AS pzmxname
  70. FROM u_softbed_mx
  71. INNER JOIN u_configure_codemx ON u_configure_codemx.pzid = u_softbed_mx.pzid
  72. AND u_configure_codemx.printid = u_softbed_mx.pz_printid
  73. INNER JOIN u_configure_code ON u_configure_code.pzid = u_configure_codemx.pzid";
  74. DbSqlHelper.SelectJoin(cmd, selectStr, "softbed_id = @billid", new Dictionary<string, object>() { { "@billid", billid } }, "printid,pz_printid", fields, 0, 0, mxlist);
  75. codeList = mxlist.GroupBy(item => new { item.pzid, item.pz_pringid })
  76. .Select(group => new u_configure_codemx
  77. {
  78. pzid = group.Key.pzid,
  79. printid = group.Key.pz_pringid,
  80. pzname = group.First().pzname,
  81. namemx = group.First().pzmxname,
  82. })
  83. .ToList();
  84. }
  85. /// <summary>
  86. /// 获取软床报价单据明细
  87. /// </summary>
  88. /// <param name="billid"></param>
  89. /// <param name="fields"></param>
  90. /// <param name="mxlist"></param>
  91. public List<u_softbed_mx> GetSoftBedMxList(int billid, string fields = null)
  92. {
  93. GetSoftBedMxList(billid, fields, out var mxlist, out _);
  94. return mxlist;
  95. }
  96. /// <summary>
  97. /// 根据部件配置项值获取对应BOM明细
  98. /// </summary>
  99. /// <param name="mxlist"></param>
  100. /// <param name="codeList"></param>
  101. public List<u_softbed_mx> GetSoftBedMxBomList(List<u_configure_codemx> codeList)
  102. {
  103. var mxlist = new List<u_softbed_mx>();
  104. foreach(var code in codeList)
  105. {
  106. var resultList = new List<u_configure_codemxbom>();
  107. var outputFields = @"pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,cost,cost_emp,cost_date,sonloss,sonloss_formula,
  108. sondecloss,sondecloss_formula,deptid_scll,default_length,default_width,default_qty,mtrlcode,mtrlname,mtrlmode,unit,
  109. mtrlsectype,zxmtrlmode,usermtrlmode";
  110. var selectStr = @"SELECT u_configure_codemxbom.pzid
  111. ,u_configure_codemxbom.printid
  112. ,u_configure_codemxbom.pid
  113. ,u_configure_codemxbom.mtrlid
  114. ,u_configure_codemxbom.sonscale
  115. ,u_configure_codemxbom.sonscale_formula
  116. ,u_configure_codemxbom.mng_cost_rate
  117. ,u_configure_codemxbom.profit_rate
  118. ,u_configure_codemxbom.realqty
  119. ,u_configure_codemxbom.cost
  120. ,u_configure_codemxbom.cost_emp
  121. ,u_configure_codemxbom.cost_date
  122. ,u_configure_codemxbom.sonloss
  123. ,u_configure_codemxbom.sonloss_formula
  124. ,u_configure_codemxbom.sondecloss
  125. ,u_configure_codemxbom.sondecloss_formula
  126. ,u_configure_codemxbom.deptid_scll
  127. ,u_configure_codemxbom.default_length
  128. ,u_configure_codemxbom.default_width
  129. ,u_configure_codemxbom.default_qty
  130. ,u_mtrldef.mtrlcode
  131. ,u_mtrldef.mtrlname
  132. ,u_mtrldef.mtrlmode
  133. ,u_mtrldef.unit
  134. ,u_mtrldef.mtrlsectype
  135. ,u_mtrldef.zxmtrlmode
  136. ,u_mtrldef.usermtrlmode
  137. FROM u_configure_codemxbom
  138. INNER JOIN u_mtrldef ON u_configure_codemxbom.mtrlid = u_mtrldef.mtrlid";
  139. DbSqlHelper.SelectJoin(cmd,selectStr,
  140. "u_configure_codemxbom.pzid = @pzid AND u_configure_codemxbom.printid = @printid",
  141. new Dictionary<string, object>() { { "@pzid", code.pzid }, { "@printid",code.printid} },
  142. "u_configure_codemxbom.pid", outputFields, 0,0,resultList);
  143. foreach(var item in resultList)
  144. {
  145. mxlist.Add(InitSoftBedMxFromBom(item));
  146. }
  147. }
  148. return mxlist;
  149. }
  150. /// <summary>
  151. /// 初始化配置项值带出的BOM清单列表
  152. /// </summary>
  153. /// <param name="bomItem"></param>
  154. /// <returns></returns>
  155. public u_softbed_mx InitSoftBedMxFromBom(u_configure_codemxbom bomItem)
  156. {
  157. var mx = new u_softbed_mx()
  158. {
  159. pzid = bomItem.pzid,
  160. pz_pringid = bomItem.printid,
  161. mtrlid = bomItem.mtrlid,
  162. mtrlname = bomItem.mtrlname,
  163. mtrlcode = bomItem.mtrlcode,
  164. mtrlmode = bomItem.mtrlmode,
  165. unit = bomItem.unit,
  166. allow_edit = 0,
  167. has_type = bomItem.has_type, // 1-床头、2-床头柜、4-床架
  168. cutting_length = bomItem.default_length,
  169. cutting_width = bomItem.default_width,
  170. cutting_qty = bomItem.default_qty,
  171. useqty = bomItem.sonscale,
  172. use_formula = "",
  173. use_formula_str = bomItem.sonscale_formula,
  174. actual_useqty = 0,
  175. loss_rate = bomItem.sonloss,
  176. cost_price = bomItem.cost,
  177. cost_amt = 0
  178. };
  179. // 用量
  180. // 实际用量: 用量 * 损耗
  181. mx.actual_useqty = mx.useqty * mx.loss_rate;
  182. // 成本金额: 实际用量 * 成本单价
  183. mx.cost_amt = mx.actual_useqty * mx.cost_price;
  184. return mx;
  185. }
  186. /// <summary>
  187. /// 新建/修改 软床报价
  188. /// </summary>
  189. /// <param name="softbed"></param>
  190. /// <exception cref="LJCommonException"></exception>
  191. public void SaveSoftBed(u_softbed softbed)
  192. {
  193. if (softbed == null) throw new LJCommonException("软床报价保存失败,数据异常!");
  194. if (softbed.is_template == 0 && (softbed.mxList == null || softbed.mxList.Count <= 0)) throw new LJCommonException("软床报价保存失败,明细内容为空!");
  195. // 初始化属性
  196. AutoInit.AutoInitS(softbed);
  197. foreach(var mx in softbed.mxList)
  198. {
  199. AutoInit.AutoInitS(mx);
  200. }
  201. // TODO:计算价格
  202. //
  203. var dtNow = context.opdate;
  204. var fields = @"softbed_relcode,softbed_name,deptid,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  205. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  206. dept_cost,foreign_cost";
  207. var mx_fields = @"softbed_id,printid,pzid,pz_printid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  208. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,pzname,pzmxname";
  209. if(softbed.softbed_id == 0)
  210. {
  211. // 新建
  212. fields += ",softbed_id,softbed_code,create_date,create_emp";
  213. softbed.softbed_id = BllHelper.GetID(cmd, "u_softbed");
  214. softbed.create_date = dtNow;
  215. softbed.create_emp = context.tokendata.username;
  216. if (string.IsNullOrEmpty(softbed.softbed_code))
  217. {
  218. // 编号-年月日+流水
  219. softbed.softbed_code = $"RC-{dtNow.ToString("yyyyMMdd")}{(softbed.softbed_id % 10000).ToString("D4")}";
  220. }
  221. DbSqlHelper.Insert(cmd, "u_softbed", null, softbed, fields);
  222. } else
  223. {
  224. // 修改
  225. softbed.update_date = dtNow;
  226. softbed.update_emp = context.tokendata.username;
  227. fields += ",update_date,update_emp";
  228. cmd.CommandText = @"DELETE FROM u_softbed_mx WHERE softbed_id = @softbed_id";
  229. cmd.Parameters.Clear();
  230. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  231. cmd.ExecuteNonQuery();
  232. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", fields);
  233. }
  234. for (int i = 0; i < softbed.mxList.Count; i++)
  235. {
  236. var mx = softbed.mxList[i];
  237. mx.softbed_id = softbed.softbed_id;
  238. mx.printid = i + 1;
  239. DbSqlHelper.Insert(cmd, "u_softbed_mx", null, mx, mx_fields);
  240. }
  241. SaveSoftBedTemplate(softbed);
  242. }
  243. /// <summary>
  244. /// 新建软床报价模板
  245. /// </summary>
  246. /// <param name="softbed"></param>
  247. private void SaveSoftBedTemplate(u_softbed softbed)
  248. {
  249. if (softbed.is_template == 0) return;
  250. if(string.IsNullOrEmpty(softbed.softbed_code))
  251. {
  252. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  253. }
  254. string prefix = $"{softbed.softbed_code}|";
  255. var configureList = new List<u_configure_type>();
  256. if(softbed.has_headboard == 1)
  257. {
  258. configureList.Add(new u_configure_type()
  259. {
  260. contfigtypename = $"{prefix}床头",
  261. contfigtype = 0,
  262. usechflag = 1,
  263. flag = 0
  264. });
  265. }
  266. if(softbed.has_bedframe == 1)
  267. {
  268. configureList.Add(new u_configure_type()
  269. {
  270. contfigtypename = $"{prefix}床架",
  271. contfigtype = 0,
  272. usechflag = 1,
  273. flag = 0
  274. });
  275. }
  276. if (softbed.has_nightstand == 1)
  277. {
  278. configureList.Add(new u_configure_type()
  279. {
  280. contfigtypename = $"{prefix}床头柜",
  281. contfigtype = 0,
  282. usechflag = 1,
  283. flag = 0
  284. });
  285. }
  286. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  287. foreach(var configure in configureList)
  288. {
  289. // 判断是否已存在
  290. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  291. cmd.Parameters.Clear();
  292. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  293. var cnt = Convert.ToInt32(cmd.ExecuteScalar());
  294. if (cnt > 0) continue;
  295. baseInfoHelper.SaveConfigureType(configure);
  296. }
  297. }
  298. /// <summary>
  299. /// 复制软床报价
  300. /// </summary>
  301. /// <param name="softbed"></param>
  302. /// <param name="newId"></param>
  303. public void CopySoftBed(u_softbed softbed, out int newId)
  304. {
  305. var fields = @"mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  306. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,moneyrate";
  307. var softbed_copy = GetSoftBed(softbed.softbed_id, fields);
  308. // 判断copy_bedNet.bednetcode是否存在@@字符串,如果存在,则删除@@后面的字符串,包括@@
  309. if (softbed_copy.softbed_code.IndexOf("@@") > -1)
  310. {
  311. softbed_copy.softbed_code = softbed_copy.softbed_code.Substring(softbed_copy.softbed_code.IndexOf("@@"));
  312. }
  313. softbed_copy.softbed_code += " @@";
  314. softbed_copy.softbed_code += DateTime.Now.ToString("yyyMMdd_mmhhss");
  315. softbed_copy.softbed_id = 0;
  316. SaveSoftBed(softbed_copy);
  317. newId = softbed_copy.softbed_id;
  318. }
  319. /// <summary>
  320. /// 删除软床报价
  321. /// </summary>
  322. /// <param name="billid"></param>
  323. /// <exception cref="LJCommonException"></exception>
  324. public void DeleteSoftBed(int billid)
  325. {
  326. var softbed = GetSoftBed(billid, "softbed_code,flag");
  327. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  328. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  329. DbSqlHelper.Delete(cmd, softbed);
  330. }
  331. /// <summary>
  332. /// 审核软床报价
  333. /// </summary>
  334. /// <param name="billid"></param>
  335. /// <exception cref="LJCommonException"></exception>
  336. public void AuditSoftBed(int billid)
  337. {
  338. var softbed = GetSoftBed(billid, "softbed_code,flag");
  339. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  340. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  341. softbed.flag = 1;
  342. softbed.audit_date = context.opdate;
  343. softbed.audit_emp = context.tokendata.username;
  344. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  345. }
  346. /// <summary>
  347. /// 撤审软床报价
  348. /// </summary>
  349. /// <param name="billid"></param>
  350. /// <exception cref="LJCommonException"></exception>
  351. public void CAuditSoftBed(int billid)
  352. {
  353. var softbed = GetSoftBed(billid, "softbed_code,flag");
  354. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  355. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  356. softbed.flag = 0;
  357. softbed.audit_date = null;
  358. softbed.audit_emp = string.Empty;
  359. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  360. }
  361. #region 通用公式方法
  362. private HashSet<CalCulationFormula> const_formula = new HashSet<CalCulationFormula>();
  363. private string ConvertToEnglishSymbols(string input)
  364. {
  365. input = input.Replace("(", "(")
  366. .Replace(")", ")")
  367. .Replace(",", ",")
  368. .Replace("。", ".")
  369. .Replace(":", ":")
  370. .Replace(";", ";")
  371. .Replace("“", "\"")
  372. .Replace("”", "\"")
  373. .Replace("‘", "'")
  374. .Replace("’", "'");
  375. return input;
  376. }
  377. private void FormatExpression(string expression)
  378. {
  379. expression = ConvertToEnglishSymbols(expression);
  380. }
  381. #endregion
  382. }
  383. }