SoftBedHelper.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. using DirectService.Tools;
  2. using JLHHJSvr.BLL;
  3. using JLHHJSvr.Com.Model;
  4. using JLHHJSvr.Excutor;
  5. using JLHHJSvr.LJException;
  6. using JLHHJSvr.Tools;
  7. using LJLib.DAL.SQL;
  8. using NPOI.HSSF.Record;
  9. using NPOI.SS.Formula.Functions;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. using System.Web;
  19. using System.Web.Configuration;
  20. using static JLHHJSvr.Helper.CacheHelper;
  21. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
  22. namespace JLHHJSvr.Helper
  23. {
  24. internal class SoftBedHelper : HelperBase
  25. {
  26. /// <summary>
  27. /// 获取软床报价单据
  28. /// </summary>
  29. /// <param name="billid"></param>
  30. /// <param name="fields"></param>
  31. /// <param name="bill"></param>
  32. public u_softbed GetSoftBed(int billid,string fields = null)
  33. {
  34. 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,
  35. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  36. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version,flag,audit_date,audit_emp,update_date,update_emp";
  37. var bill = new u_softbed() { softbed_id = billid };
  38. if (DbSqlHelper.SelectOne(cmd, bill, fields) == 0) return null;
  39. return bill;
  40. }
  41. /// <summary>
  42. /// 获取软床报价单据明细
  43. /// </summary>
  44. /// <param name="billid"></param>
  45. /// <param name="fields"></param>
  46. /// <param name="mxlist"></param>
  47. public List<u_softbed_mx> GetSoftBedMxList(int billid,string fields = null)
  48. {
  49. fields = fields ?? @"softbed_id,printid,formulaid,pzid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  50. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,pzname,formulaname,parts_type";
  51. var mxlist = new List<u_softbed_mx>();
  52. var selectStr = @"SELECT u_softbed_mx.softbed_id
  53. ,u_softbed_mx.printid
  54. ,u_softbed_mx.formulaid
  55. ,u_softbed_mx.pzid
  56. ,u_softbed_mx.mtrlid
  57. ,u_softbed_mx.mtrlname
  58. ,u_softbed_mx.mtrlcode
  59. ,u_softbed_mx.mtrlmode
  60. ,u_softbed_mx.unit
  61. ,u_softbed_mx.has_type
  62. ,u_softbed_mx.allow_edit
  63. ,u_softbed_mx.cutting_length
  64. ,u_softbed_mx.cutting_width
  65. ,u_softbed_mx.cutting_qty
  66. ,u_softbed_mx.useqty
  67. ,u_softbed_mx.use_formula
  68. ,u_softbed_mx.use_formula_str
  69. ,u_softbed_mx.actual_useqty
  70. ,u_softbed_mx.loss_rate
  71. ,u_softbed_mx.price
  72. ,u_softbed_mx.price_formula
  73. ,u_softbed_mx.price_formula_str
  74. ,u_softbed_mx.cost_price
  75. ,u_softbed_mx.cost_amt
  76. ,ISNULL(u_configure_code.name,u_softbed_mx.pzname) AS pzname
  77. ,u_softbed_mx.parts_type
  78. ,ISNULL(u_softbed_formula.formulaname,'') AS formulaname
  79. FROM u_softbed_mx
  80. LEFT JOIN u_configure_code ON u_softbed_mx.pzid = u_configure_code.pzid
  81. LEFT JOIN u_softbed_formula ON u_softbed_mx.formulaid = u_softbed_formula.formulaid
  82. ";
  83. DbSqlHelper.SelectJoin(cmd, selectStr, "softbed_id = @billid", new Dictionary<string, object>() { { "@billid", billid } }, "has_type,printid", fields, 0, 0, mxlist);
  84. return mxlist;
  85. }
  86. public List<u_softbed_mx> GetChangeSoftBedMxList(int billid,List<u_softbed_mx> mxList,List<u_configure_codemx> codeMxList)
  87. {
  88. var newList = new List<u_softbed_mx>();
  89. var pzid0List = new List<u_softbed_mx>();
  90. var softbed = GetSoftBed(billid, "billid,softbed_code,has_headboard,has_nightstand,has_bedframe,is_template,template_id");
  91. if (softbed == null) return newList;
  92. // 换料清单
  93. var bomList = GetSoftBedMxBomList(codeMxList);
  94. // var typeDict = new Dictionary<string, List<u_softbed_mx>>();
  95. //foreach(var mx in mxList)
  96. //{
  97. // var key = $"{mx.has_type}|{mx.pzid}";
  98. // if(!typeDict.ContainsKey(key))
  99. // {
  100. // typeDict.Add(key, new List<u_softbed_mx>());
  101. // }
  102. // typeDict[key].Add(mx);
  103. // if (mx.pzid == 0) pzid0List.Add(mx);
  104. // }
  105. // 原Dictionary替换为List<KeyValuePair>,维护键值对的插入顺序
  106. var typeList = new List<KeyValuePair<string, List<u_softbed_mx>>>();
  107. foreach (var mx in mxList)
  108. {
  109. var key = $"{mx.has_type}|{mx.pzid}";
  110. var existingItem = typeList.FirstOrDefault(kvp => kvp.Key == key);
  111. if (existingItem.Equals(default(KeyValuePair<string, List<u_softbed_mx>>)))
  112. {
  113. var newMxList = new List<u_softbed_mx> { mx };
  114. typeList.Add(new KeyValuePair<string, List<u_softbed_mx>>(key, newMxList));
  115. }
  116. else
  117. {
  118. existingItem.Value.Add(mx);
  119. }
  120. if (mx.pzid == 0)
  121. {
  122. pzid0List.Add(mx);
  123. }
  124. }
  125. var bomDict = new Dictionary<string, List<u_softbed_mx>>();
  126. foreach (var bom in bomList)
  127. {
  128. var key = $"{bom.has_type}|{bom.pzid}";
  129. if (!bomDict.ContainsKey(key))
  130. {
  131. bomDict.Add(key, new List<u_softbed_mx>());
  132. }
  133. bomDict[key].Add(bom);
  134. }
  135. /**
  136. * 1、模板报价
  137. * 2、非模板报价
  138. * 仅替换配置,保留其他明细信息
  139. * 1、如果明细存在bomList中,证明没有替换,直接添加到newList
  140. * 2、如果明细不存在bomList中,证明可能是给替换的(2.1)或者是非部件(即手动添加2.2)
  141. */
  142. foreach (var kvp in typeList)
  143. {
  144. if (bomDict.TryGetValue(kvp.Key, out var temp_bomList))
  145. {
  146. bool isExist = false;
  147. foreach (var mx in kvp.Value)
  148. {
  149. isExist = temp_bomList.FindIndex(t => t.mtrlid == mx.mtrlid) > -1;
  150. }
  151. if (isExist)
  152. {
  153. // 1
  154. foreach (var mx in kvp.Value)
  155. {
  156. mx.printid = newList.Count + 1;
  157. newList.Add(mx);
  158. }
  159. }
  160. else
  161. {
  162. // 2.1
  163. foreach (var mx in temp_bomList)
  164. {
  165. mx.printid = newList.Count + 1;
  166. newList.Add(mx);
  167. }
  168. }
  169. bomDict.Remove(kvp.Key);
  170. }
  171. }
  172. // 新增的部件
  173. foreach (var item in bomDict)
  174. {
  175. foreach (var mx in item.Value)
  176. {
  177. mx.printid = newList.Count + 1;
  178. newList.Add(mx);
  179. }
  180. }
  181. // 2.2
  182. foreach (var mx in pzid0List)
  183. {
  184. mx.printid = newList.Count + 1;
  185. newList.Add(mx);
  186. }
  187. if (newList.Count <= 0) newList.AddRange(bomList);
  188. return newList;
  189. }
  190. /// <summary>
  191. /// 根据部件配置项值获取对应BOM明细
  192. /// </summary>
  193. /// <param name="mxlist"></param>
  194. /// <param name="codeList"></param>
  195. public List<u_softbed_mx> GetSoftBedMxBomList(List<u_configure_codemx> codeList)
  196. {
  197. var mxlist = new List<u_softbed_mx>();
  198. foreach(var code in codeList)
  199. {
  200. var resultList = new List<u_configure_codemxbom>();
  201. var outputFields = @"pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,cost,sonloss,sonloss_formula,
  202. sondecloss,sondecloss_formula,default_length,default_width,default_qty,mtrlcode,mtrlname,mtrlmode,unit,
  203. mtrlsectype,zxmtrlmode,usermtrlmode,price,pzname";
  204. var selectStr = @"SELECT u_configure_codemxbom.pzid
  205. ,u_configure_codemxbom.printid
  206. ,u_configure_codemxbom.pid
  207. ,u_configure_codemxbom.mtrlid
  208. ,u_configure_codemxbom.sonscale
  209. ,u_configure_codemxbom.sonscale_formula
  210. ,u_configure_codemxbom.mng_cost_rate
  211. ,u_configure_codemxbom.profit_rate
  212. ,u_configure_codemxbom.realqty
  213. ,u_configure_codemxbom.cost
  214. ,u_configure_codemxbom.sonloss
  215. ,u_configure_codemxbom.sonloss_formula
  216. ,u_configure_codemxbom.sondecloss
  217. ,u_configure_codemxbom.sondecloss_formula
  218. ,u_configure_codemxbom.default_length
  219. ,u_configure_codemxbom.default_width
  220. ,u_configure_codemxbom.default_qty
  221. ,u_mtrldef.mtrlcode
  222. ,u_mtrldef.mtrlname
  223. ,u_mtrldef.mtrlmode
  224. ,u_mtrldef.unit
  225. ,u_mtrldef.mtrlsectype
  226. ,u_mtrldef.zxmtrlmode
  227. ,u_mtrldef.usermtrlmode
  228. ,u_erpmtrl_price.price
  229. ,u_configure_codemx.namemx AS pzname
  230. FROM u_configure_codemxbom
  231. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  232. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  233. INNER JOIN u_mtrldef ON u_configure_codemxbom.mtrlid = u_mtrldef.mtrlid
  234. LEFT OUTER JOIN u_erpmtrl_price ON u_configure_codemxbom.mtrlid = u_erpmtrl_price.mtrlid";
  235. DbSqlHelper.SelectJoin(cmd,selectStr,
  236. "u_configure_codemxbom.pzid = @pzid AND u_configure_codemxbom.printid = @printid",
  237. new Dictionary<string, object>() { { "@pzid", code.pzid }, { "@printid",code.printid} },
  238. "u_configure_codemxbom.pid", outputFields, 0,0,resultList);
  239. foreach(var item in resultList)
  240. {
  241. mxlist.Add(InitSoftBedMxFromBom(item, code.has_type.Value));
  242. }
  243. }
  244. return mxlist;
  245. }
  246. /// <summary>
  247. /// 初始化配置项值带出的BOM清单列表
  248. /// </summary>
  249. /// <param name="bomItem"></param>
  250. /// <returns></returns>
  251. public u_softbed_mx InitSoftBedMxFromBom(u_configure_codemxbom bomItem,byte has_type)
  252. {
  253. var mx = new u_softbed_mx()
  254. {
  255. pzid = bomItem.pzid,
  256. mtrlid = bomItem.mtrlid,
  257. mtrlname = bomItem.mtrlname,
  258. mtrlcode = bomItem.mtrlcode,
  259. mtrlmode = bomItem.mtrlmode,
  260. unit = bomItem.unit,
  261. allow_edit = 0,
  262. has_type = has_type, // 1-床头、2-床头柜、4-床架
  263. cutting_length = bomItem.default_length,
  264. cutting_width = bomItem.default_width,
  265. cutting_qty = bomItem.default_qty,
  266. price = bomItem.price,
  267. price_formula = "",
  268. price_formula_str = "",
  269. useqty = bomItem.sonscale,
  270. use_formula = "",
  271. use_formula_str = "",
  272. actual_useqty = 0,
  273. loss_rate = bomItem.sonloss,
  274. cost_price = 0,
  275. cost_amt = 0,
  276. pzname = bomItem.pzname
  277. };
  278. return mx;
  279. }
  280. /// <summary>
  281. /// 新建/修改 软床报价
  282. /// </summary>
  283. /// <param name="softbed"></param>
  284. /// <exception cref="LJCommonException"></exception>
  285. public void SaveSoftBed(u_softbed softbed)
  286. {
  287. SaveCheck(softbed);
  288. // 初始化属性
  289. AutoInit.AutoInitS(softbed);
  290. foreach(var mx in softbed.mxList)
  291. {
  292. AutoInit.AutoInitS(mx);
  293. if (mx.has_type != 8) mx.parts_type = string.Empty;
  294. else if (string.IsNullOrEmpty(mx.parts_type)) throw new LJCommonException($"明细行:{mx.printid},存在其他配件明细未设置配件类型,请检查!");
  295. }
  296. // 计算价格
  297. CalCulateFormula(softbed);
  298. //
  299. var dtNow = context.opdate;
  300. var fields = @"softbed_relcode,softbed_name,deptid,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  301. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  302. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version";
  303. var mx_fields = @"softbed_id,printid,pzid,formulaid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  304. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,parts_type,pzname";
  305. if(softbed.softbed_id == 0)
  306. {
  307. // 新建
  308. fields += ",softbed_id,softbed_code,create_date,create_emp";
  309. softbed.softbed_id = BllHelper.GetID(cmd, "u_softbed");
  310. softbed.create_date = dtNow;
  311. softbed.create_emp = context.tokendata.username;
  312. softbed.version = 1;
  313. if (string.IsNullOrEmpty(softbed.softbed_code))
  314. {
  315. // 编号-年月日+流水
  316. softbed.softbed_code = $"RC-{dtNow.ToString("yyyyMMdd")}{(softbed.softbed_id % 10000).ToString("D4")}";
  317. }
  318. DbSqlHelper.Insert(cmd, "u_softbed", null, softbed, fields);
  319. } else
  320. {
  321. // 修改
  322. softbed.update_date = dtNow;
  323. softbed.update_emp = context.tokendata.username;
  324. softbed.version++;
  325. fields += ",update_date,update_emp";
  326. cmd.CommandText = @"DELETE FROM u_softbed_mx WHERE softbed_id = @softbed_id";
  327. cmd.Parameters.Clear();
  328. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  329. cmd.ExecuteNonQuery();
  330. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", fields);
  331. }
  332. for (int i = 0; i < softbed.mxList.Count; i++)
  333. {
  334. var mx = softbed.mxList[i];
  335. mx.softbed_id = softbed.softbed_id;
  336. mx.printid = i + 1;
  337. DbSqlHelper.Insert(cmd, "u_softbed_mx", null, mx, mx_fields);
  338. }
  339. SaveSoftBedTemplate(softbed);
  340. // 解锁
  341. LockHelper.UnLockBill(cmd, BillKeyWord, softbed.softbed_id, context.tokendata.username, 0);
  342. }
  343. private void SaveCheck(u_softbed softbed)
  344. {
  345. if (softbed == null) throw new LJCommonException("软床报价保存失败,数据异常!");
  346. if (softbed.is_template == 0 && (softbed.mxList == null || softbed.mxList.Count <= 0)) throw new LJCommonException("软床报价保存失败,明细内容为空!");
  347. if(softbed.softbed_id > 0)
  348. {
  349. // 检查锁
  350. if (LockHelper.CheckIsLock(cmd, MattressHelper.BillKeyWord, softbed.softbed_id, context.tokendata.username, out string arg_msg)) throw new LJCommonException(arg_msg);
  351. // 检测版本控制
  352. CheckBillVersion<u_softbed>(cmd, softbed.softbed_id, softbed.version);
  353. //var _softbed = GetSoftBed(softbed.softbed_id, "softbed_id,softbed_code,version,update_emp");
  354. //if (_softbed.version != softbed.version) throw new LJCommonException($"单据:{_softbed.softbed_code}已被{_softbed.update_emp}修改,请重新加载最新数据再操作!");
  355. }
  356. if(softbed.softbed_id > 0 && softbed.is_template != 0)
  357. {
  358. //
  359. var configureList = GetSoftBedConfigureName(softbed);
  360. var bomSet = new Dictionary<int,List<u_configure_codemxbom>>();
  361. foreach(var configure in configureList)
  362. {
  363. cmd.CommandText = @"SELECT u_configure_code.pzid
  364. ,u_configure_codemxbom.mtrlid
  365. ,LTRIM(RTRIM(u_configure_type.contfigtypename))
  366. ,u_configure_type.contfigtypeid
  367. FROM u_configure_codemxbom
  368. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  369. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  370. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  371. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid
  372. WHERE LTRIM(RTRIM(u_configure_type.contfigtypename)) = @contfigtypename";
  373. cmd.Parameters.Clear();
  374. cmd.Parameters.AddWithValue("@contfigtypename", configure.Value.contfigtypename);
  375. using (var reader = cmd.ExecuteReader())
  376. {
  377. while (reader.Read())
  378. {
  379. if (!bomSet.ContainsKey(configure.Key)) bomSet.Add(configure.Key, new List<u_configure_codemxbom>());
  380. bomSet[configure.Key].Add(new u_configure_codemxbom()
  381. {
  382. pzid = Convert.ToInt32(reader["pzid"]),
  383. mtrlid = Convert.ToInt32(reader["mtrlid"]),
  384. typeid = Convert.ToInt32(reader["contfigtypeid"]),
  385. has_type = configure.Key
  386. });
  387. }
  388. }
  389. }
  390. foreach (var mx in softbed.mxList)
  391. {
  392. if(!bomSet.TryGetValue(mx.has_type, out var bomList))
  393. {
  394. throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}没有设置物料清单");
  395. }
  396. //var cnt = bomList.Where(t => t.has_type == mx.has_type && t.mtrlid == mx.mtrlid).Count();
  397. //if (cnt > 1) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}存在多个清单,但没有设置部件!");
  398. //else if (cnt == 0) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}不存在于物料清单中,请检查!");
  399. }
  400. }
  401. }
  402. /// <summary>
  403. /// 新建软床报价模板
  404. /// </summary>
  405. /// <param name="softbed"></param>
  406. private void SaveSoftBedTemplate(u_softbed softbed)
  407. {
  408. if (softbed.is_template == 0) return;
  409. if(string.IsNullOrEmpty(softbed.softbed_code))
  410. {
  411. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  412. }
  413. string prefix = $"{softbed.softbed_code}|";
  414. var configureList = GetSoftBedConfigureName(softbed).Values.ToList();
  415. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  416. foreach(var configure in configureList)
  417. {
  418. // 判断是否已存在
  419. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  420. cmd.Parameters.Clear();
  421. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  422. var cnt = Convert.ToInt32(cmd.ExecuteScalar());
  423. if (cnt > 0) continue;
  424. baseInfoHelper.SaveConfigureType(configure);
  425. }
  426. // 生成标准选配项值ifdft
  427. if(softbed.codeMxList != null && softbed.codeMxList.Count > 0)
  428. {
  429. foreach (var codeMx in softbed.codeMxList)
  430. {
  431. if (codeMx.pzid == 0) continue;
  432. // 判断是否有标准
  433. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  434. cmd.Parameters.Clear();
  435. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  436. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  437. if(Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  438. {
  439. cmd.CommandText = @"UPDATE u_configure_codemx SET ifdft = 0 WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  440. cmd.Parameters.Clear();
  441. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  442. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  443. cmd.ExecuteNonQuery();
  444. }
  445. codeMx.ifdft = 1;
  446. DbSqlHelper.Update(cmd, "u_configure_codemx", null, codeMx, "pzid,printid", "ifdft");
  447. }
  448. }
  449. }
  450. /// <summary>
  451. /// 返回软床报价相关配置名
  452. /// </summary>
  453. /// <param name="softbed"></param>
  454. /// <returns></returns>
  455. private Dictionary<byte, u_configure_type> GetSoftBedConfigureName(u_softbed softbed)
  456. {
  457. var dict = new Dictionary<byte, u_configure_type>();
  458. string prefix = $"{softbed.softbed_code}|";
  459. if (softbed.has_headboard == 1)
  460. {
  461. dict.Add(1, new u_configure_type()
  462. {
  463. contfigtypeid = 0,
  464. contfigtypename = $"{prefix}床头",
  465. contfigtype = 0,
  466. usechflag = 1,
  467. flag = 0
  468. });
  469. }
  470. if (softbed.has_nightstand == 1)
  471. {
  472. dict.Add(2, new u_configure_type()
  473. {
  474. contfigtypeid = 0,
  475. contfigtypename = $"{prefix}床头柜",
  476. contfigtype = 0,
  477. usechflag = 1,
  478. flag = 0
  479. });
  480. }
  481. if (softbed.has_bedframe == 1)
  482. {
  483. dict.Add(4,new u_configure_type()
  484. {
  485. contfigtypeid = 0,
  486. contfigtypename = $"{prefix}床架",
  487. contfigtype = 0,
  488. usechflag = 1,
  489. flag = 0
  490. });
  491. }
  492. return dict;
  493. }
  494. /// <summary>
  495. /// 获取软床报价配置
  496. /// </summary>
  497. /// <param name="softbed"></param>
  498. /// <param name="extraWheres"></param>
  499. /// <returns></returns>
  500. public List<u_configure_type> GetSoftBedConfigureList(u_softbed softbed,List<string> extraWheres)
  501. {
  502. var codeMxList = new List<u_configure_codemx>();
  503. var selectStr = @"SELECT u_configure_codemx.pzid
  504. ,u_configure_codemx.printid
  505. ,u_configure_codemx.pzcodemx
  506. ,u_configure_codemx.namemx
  507. ,u_configure_code.name AS pzname
  508. ,u_configure_code.pzcode
  509. ,u_configure_code.inputtype
  510. ,LTRIM(RTRIM(u_configure_type.contfigtypename)) AS contfigtypename
  511. ,u_configure_code.typeid
  512. ,u_configure_codemx.ifdft
  513. FROM u_configure_codemx
  514. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  515. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid";
  516. var whereList = new List<string>();
  517. var nameList = new List<string>();
  518. if (softbed.softbed_id > 0 || softbed.template_id > 0)
  519. {
  520. // 模版配置获取
  521. int billid = softbed.softbed_id > 0 ? softbed.softbed_id : softbed.template_id;
  522. var _softbed = GetSoftBed(billid, "softbed_id,softbed_code,has_headboard,has_nightstand,has_bedframe,template_id,is_template");
  523. if(_softbed.is_template == 0)
  524. {
  525. _softbed = GetSoftBed(_softbed.template_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  526. }
  527. string prefix = $"{_softbed.softbed_code}|";
  528. if(_softbed.has_headboard == 1)
  529. {
  530. nameList.Add($"{prefix}床头");
  531. }
  532. if (_softbed.has_nightstand == 1)
  533. {
  534. nameList.Add($"{prefix}床头柜");
  535. }
  536. if (_softbed.has_bedframe == 1)
  537. {
  538. nameList.Add($"{prefix}床架");
  539. }
  540. }
  541. if(nameList.Count > 0)
  542. {
  543. whereList.Add($"LTRIM(RTRIM(u_configure_type.contfigtypename)) IN {ListEx.getString(nameList)}");
  544. }
  545. if(extraWheres != null && extraWheres.Count > 0)
  546. {
  547. whereList.AddRange(extraWheres);
  548. }
  549. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), null, "typeid,pzid,printid", "typeid,pzid,printid,pzcodemx,namemx,pzname,pzcode,contfigtypename,ifdft", 0, 0, codeMxList);
  550. // 部件选配项
  551. var codeMap = new Dictionary<int, u_configure_code>();
  552. foreach(var codeMx in codeMxList)
  553. {
  554. int pzid = codeMx.pzid.Value;
  555. if (!codeMap.TryGetValue(pzid, out var config))
  556. {
  557. config = new u_configure_code
  558. {
  559. pzid = pzid,
  560. name = codeMx.pzname,
  561. contfigtypename = codeMx.contfigtypename,
  562. pzcode = codeMx.pzcode,
  563. typeid = codeMx.typeid,
  564. codeMxList = new List<u_configure_codemx>()
  565. };
  566. codeMap.Add(pzid, config);
  567. }
  568. config.codeMxList.Add(codeMx);
  569. }
  570. // 部件选配类型
  571. var typeMap = new Dictionary<int, u_configure_type>();
  572. foreach(var code in codeMap.Values.ToList())
  573. {
  574. int typeid = code.typeid.Value;
  575. if (!typeMap.TryGetValue(typeid, out var config))
  576. {
  577. config = new u_configure_type
  578. {
  579. contfigtypeid = typeid,
  580. contfigtypename = code.contfigtypename,
  581. codeList = new List<u_configure_code>()
  582. };
  583. typeMap.Add(typeid, config);
  584. }
  585. config.codeList.Add(code);
  586. }
  587. return typeMap.Values.ToList();
  588. }
  589. /// <summary>
  590. /// 复制软床报价
  591. /// </summary>
  592. /// <param name="softbed"></param>
  593. /// <param name="newId"></param>
  594. public u_softbed CopySoftBed(u_softbed softbed)
  595. {
  596. var softbed_copy = ObjectHelper.DeepCopy(softbed);
  597. softbed_copy.softbed_id = 0;
  598. SaveSoftBed(softbed_copy);
  599. if(softbed_copy.is_template == 1 && softbed.copy_id > 0)
  600. {
  601. // 复制配置到新配置
  602. var _softbed = GetSoftBed(softbed.copy_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  603. if(_softbed.has_headboard == 1 && softbed_copy.has_headboard == 1)
  604. {
  605. CopyConfigureData($"{_softbed.softbed_code}|床头",$"{softbed_copy.softbed_code}|床头");
  606. }
  607. if (_softbed.has_nightstand == 1 && softbed_copy.has_nightstand == 1)
  608. {
  609. CopyConfigureData($"{_softbed.softbed_code}|床头柜", $"{softbed_copy.softbed_code}|床头柜");
  610. }
  611. if (_softbed.has_bedframe == 1 && softbed_copy.has_bedframe == 1)
  612. {
  613. CopyConfigureData($"{_softbed.softbed_code}|床架", $"{softbed_copy.softbed_code}|床架");
  614. }
  615. }
  616. return softbed_copy;
  617. }
  618. /// <summary>
  619. /// 删除软床报价
  620. /// </summary>
  621. /// <param name="billid"></param>
  622. /// <exception cref="LJCommonException"></exception>
  623. public void DeleteSoftBed(int billid)
  624. {
  625. var softbed = GetSoftBed(billid, "softbed_code,flag");
  626. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  627. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  628. DbSqlHelper.Delete(cmd, softbed);
  629. }
  630. /// <summary>
  631. /// 审核软床报价
  632. /// </summary>
  633. /// <param name="billid"></param>
  634. /// <exception cref="LJCommonException"></exception>
  635. public void AuditSoftBed(int billid)
  636. {
  637. var softbed = GetSoftBed(billid, "softbed_code,flag");
  638. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  639. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  640. softbed.flag = 1;
  641. softbed.audit_date = context.opdate;
  642. softbed.audit_emp = context.tokendata.username;
  643. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  644. }
  645. /// <summary>
  646. /// 撤审软床报价
  647. /// </summary>
  648. /// <param name="billid"></param>
  649. /// <exception cref="LJCommonException"></exception>
  650. public void CAuditSoftBed(int billid)
  651. {
  652. var softbed = GetSoftBed(billid, "softbed_code,flag");
  653. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  654. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  655. softbed.flag = 0;
  656. softbed.audit_date = null;
  657. softbed.audit_emp = string.Empty;
  658. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  659. }
  660. public void CalCulateFormula(u_softbed softbed)
  661. {
  662. InitSoftBed(softbed);
  663. InitReplaceMents(softbed);
  664. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  665. foreach (var mx in softbed.mxList)
  666. {
  667. InitMxReplaceMents(softbed, mx);
  668. formula.CalculateAll();
  669. // 转换后的文本公式
  670. mx.use_formula_str = formula.GetFormulaItem("【实际用量】").formula_transform;
  671. mx.price_formula_str = formula.GetFormulaItem("【成本单价】").formula_transform;
  672. // 成本单价
  673. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  674. // 实际用量
  675. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  676. // 成本金额
  677. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  678. // 总材料成本
  679. total_mtrl_cost += mx.cost_amt;
  680. // 总人力成本
  681. total_hr_cost += 0;
  682. }
  683. if(softbed.mxList != null && softbed.mxList.Count > 0)
  684. {
  685. softbed.total_mtrl_cost = total_mtrl_cost;
  686. softbed.total_hr_cost = total_hr_cost;
  687. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  688. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  689. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  690. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  691. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  692. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  693. }
  694. //formula.CalculateAll();
  695. }
  696. private void InitSoftBed(u_softbed softbed)
  697. {
  698. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  699. //
  700. softbed.dept_profitrate = dept.profitrate;
  701. //
  702. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  703. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  704. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  705. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  706. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  707. // 检查佣金是否小于1
  708. if (softbed.commission < 1)
  709. {
  710. throw new LJCommonException("佣金点数不能小于1!");
  711. }
  712. // 检查税率是否小于1
  713. if (softbed.taxrate < 1)
  714. {
  715. throw new LJCommonException("税率不能小于1!");
  716. }
  717. // 检查额外点数是否小于1
  718. if (softbed.other_rate < 1)
  719. {
  720. throw new LJCommonException("税率不能小于1!");
  721. }
  722. // 检查部门利润率是否为0
  723. if (softbed.dept_profitrate == 0)
  724. {
  725. throw new LJCommonException("部门利润率不能为0!");
  726. }
  727. if(softbed.is_template == 1)
  728. {
  729. // 使用模板报价
  730. softbed.template_id = 0;
  731. softbed.template_code = string.Empty;
  732. softbed.template_name = string.Empty;
  733. }
  734. }
  735. private void InitReplaceMents(u_softbed softbed)
  736. {
  737. // 常量变量
  738. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  739. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  740. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  741. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  742. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  743. formula.AddFormulaItem("【税率】", softbed.taxrate);
  744. formula.AddFormulaItem("【工厂利润率】", 1);
  745. formula.AddFormulaItem("【FOB】", 0);
  746. formula.AddFormulaItem("【外销加点】", 0);
  747. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  748. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  749. // 公式变量
  750. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  751. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  752. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  753. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  754. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  755. }
  756. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  757. {
  758. // 默认公式变量
  759. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  760. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  761. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  762. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  763. // 常量变量
  764. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  765. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  766. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  767. formula.AddFormulaItem("【用料量】", mx.useqty);
  768. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  769. formula.AddFormulaItem("【材料单价】", mx.price);
  770. if(mx.formulaid > 0)
  771. {
  772. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  773. ,u_softbed_formula.formulaname
  774. ,u_softbed_formula.use_formula
  775. ,u_softbed_formula.price_formula
  776. FROM u_softbed_formula
  777. WHERE formulaid = @formulaid";
  778. cmd.Parameters.Clear();
  779. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  780. using(var reader = cmd.ExecuteReader())
  781. {
  782. if(reader.Read())
  783. {
  784. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  785. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  786. }
  787. }
  788. }
  789. }
  790. private List<string> GetConfigureTypeNameFromBillId(int billid)
  791. {
  792. var _softbed = GetSoftBed(billid, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  793. var nameList = new List<string>();
  794. string prefix = $"{_softbed.softbed_code}|";
  795. if (_softbed.has_headboard == 1)
  796. {
  797. nameList.Add($"{_softbed.softbed_code}|床头");
  798. }
  799. if (_softbed.has_nightstand == 1)
  800. {
  801. nameList.Add($"{_softbed.softbed_code}|床头柜");
  802. }
  803. if (_softbed.has_bedframe == 1)
  804. {
  805. nameList.Add($"{_softbed.softbed_code}|床架");
  806. }
  807. return nameList;
  808. }
  809. private void CopyConfigureData(string SourceName, string TargetName)
  810. {
  811. var commonHelper = GetHelper<CommonHelper>(cmd, context);
  812. var basicInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  813. // 入参非空验证
  814. if (string.IsNullOrWhiteSpace(SourceName))
  815. {
  816. throw new LJCommonException("源配置名称不能为空");
  817. }
  818. if (string.IsNullOrWhiteSpace(TargetName))
  819. {
  820. throw new LJCommonException("目标配置名称不能为空");
  821. }
  822. int SourceId = 0, TargetId = 0;
  823. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@SourceName))";
  824. cmd.Parameters.Clear();
  825. cmd.Parameters.AddWithValue("@SourceName", SourceName);
  826. using(var reader = cmd.ExecuteReader())
  827. {
  828. if(reader.Read())
  829. {
  830. SourceId = Convert.ToInt32(reader["contfigtypeid"]);
  831. }
  832. }
  833. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@TargetName))";
  834. cmd.Parameters.Clear();
  835. cmd.Parameters.AddWithValue("@TargetName", TargetName);
  836. using (var reader = cmd.ExecuteReader())
  837. {
  838. if (reader.Read())
  839. {
  840. TargetId = Convert.ToInt32(reader["contfigtypeid"]);
  841. }
  842. }
  843. if (SourceId == 0) throw new LJCommonException($"源配置名称「{SourceName.Trim()}」未找到对应的结果");
  844. if (TargetId == 0) throw new LJCommonException($"目标配置名称「{TargetName.Trim()}」未找到对应的结果");
  845. // 部件选配项
  846. var codeList = commonHelper.ExecuteDynamicSelectToList<u_configure_code>("web_configure_codelist",new Newtonsoft.Json.Linq.JObject() { { "typeid",SourceId} },0,0);
  847. var code_copyList = new List<u_configure_code>();
  848. var codeIdMap = new Dictionary<int, int>();
  849. foreach(var code in codeList)
  850. {
  851. var code_copy = ObjectHelper.DeepCopy(code);
  852. code_copy.pzid = BllHelper.GetID(cmd, "u_configure_code");
  853. code_copy.typeid = TargetId;
  854. code_copyList.Add(code_copy);
  855. if (codeIdMap.ContainsKey(code.pzid.Value)) continue;
  856. codeIdMap.Add(code.pzid.Value, code_copy.pzid.Value);
  857. //basicInfoHelper.SaveConfigureCode(code_copy);
  858. }
  859. DbSqlHelper.BulkInsert(cmd, code_copyList, "u_configure_code", null, "typeid,pzid,pzcode,inputtype,name,configtype,ifcross,ifcheck,ifuse,rulestr,ifnum,decnum,maxnum,minnum,pricestr,priceratestr,ifpack,zj_special");
  860. // 部件选配值
  861. var codeMxList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemx>("web_configure_codemxlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  862. var codeMx_copyList = new List<u_configure_codemx>();
  863. foreach (var codeMx in codeMxList)
  864. {
  865. if (!codeIdMap.TryGetValue(codeMx.pzid.Value, out int newId)) continue;
  866. var codeMx_copy = ObjectHelper.DeepCopy(codeMx);
  867. codeMx_copy.pzid = newId;
  868. codeMx_copyList.Add(codeMx_copy);
  869. }
  870. DbSqlHelper.BulkInsert(cmd, codeMx_copyList, "u_configure_codemx",null, "pzid,printid,pzcodemx,namemx,gradestr,mtrlcode,price,ifdft,MCostRate,ProfitRate,dscrp,ifuse,ifnoch,pricerate,packqty,packvol,price_pz,grade");
  871. // 部件选配物料清单
  872. var codeMxBomList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemxbom>("web_configure_codemxbomlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  873. var codeMxBom_copyList = new List<u_configure_codemxbom>();
  874. foreach (var codeMxBom in codeMxBomList)
  875. {
  876. if (!codeIdMap.TryGetValue(codeMxBom.pzid, out int newId)) continue;
  877. var codeMxBom_copy = ObjectHelper.DeepCopy(codeMxBom);
  878. codeMxBom_copy.pzid = newId;
  879. codeMxBom_copyList.Add(codeMxBom_copy);
  880. }
  881. DbSqlHelper.BulkInsert(cmd, codeMxBom_copyList, "u_configure_codemxbom",null, "pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,sonloss,sonloss_formula,sondecloss,sondecloss_formula,default_length,default_width,default_qty");
  882. }
  883. #region 通用公式
  884. private CalculateFormula formula = new CalculateFormula();
  885. private string BillKeyWord = "u_softbed";
  886. #endregion
  887. }
  888. }