SoftBedHelper.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 (mx.has_type == 8) continue;
  393. if(!bomSet.TryGetValue(mx.has_type, out var bomList))
  394. {
  395. throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}没有设置物料清单");
  396. }
  397. //var cnt = bomList.Where(t => t.has_type == mx.has_type && t.mtrlid == mx.mtrlid).Count();
  398. //if (cnt > 1) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}存在多个清单,但没有设置部件!");
  399. //else if (cnt == 0) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}不存在于物料清单中,请检查!");
  400. }
  401. }
  402. }
  403. /// <summary>
  404. /// 新建软床报价模板
  405. /// </summary>
  406. /// <param name="softbed"></param>
  407. private void SaveSoftBedTemplate(u_softbed softbed)
  408. {
  409. if (softbed.is_template == 0) return;
  410. if(string.IsNullOrEmpty(softbed.softbed_code))
  411. {
  412. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  413. }
  414. string prefix = $"{softbed.softbed_code}|";
  415. var configureList = GetSoftBedConfigureName(softbed).Values.ToList();
  416. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  417. foreach(var configure in configureList)
  418. {
  419. // 判断是否已存在
  420. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  421. cmd.Parameters.Clear();
  422. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  423. var cnt = Convert.ToInt32(cmd.ExecuteScalar());
  424. if (cnt > 0) continue;
  425. baseInfoHelper.SaveConfigureType(configure);
  426. }
  427. // 生成标准选配项值ifdft
  428. if(softbed.codeMxList != null && softbed.codeMxList.Count > 0)
  429. {
  430. foreach (var codeMx in softbed.codeMxList)
  431. {
  432. if (codeMx.pzid == 0) continue;
  433. // 判断是否有标准
  434. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  435. cmd.Parameters.Clear();
  436. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  437. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  438. if(Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  439. {
  440. cmd.CommandText = @"UPDATE u_configure_codemx SET ifdft = 0 WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  441. cmd.Parameters.Clear();
  442. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  443. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  444. cmd.ExecuteNonQuery();
  445. }
  446. codeMx.ifdft = 1;
  447. DbSqlHelper.Update(cmd, "u_configure_codemx", null, codeMx, "pzid,printid", "ifdft");
  448. }
  449. }
  450. }
  451. /// <summary>
  452. /// 返回软床报价相关配置名
  453. /// </summary>
  454. /// <param name="softbed"></param>
  455. /// <returns></returns>
  456. private Dictionary<byte, u_configure_type> GetSoftBedConfigureName(u_softbed softbed)
  457. {
  458. var dict = new Dictionary<byte, u_configure_type>();
  459. string prefix = $"{softbed.softbed_code}|";
  460. if (softbed.has_headboard == 1)
  461. {
  462. dict.Add(1, new u_configure_type()
  463. {
  464. contfigtypeid = 0,
  465. contfigtypename = $"{prefix}床头",
  466. contfigtype = 0,
  467. usechflag = 1,
  468. flag = 0
  469. });
  470. }
  471. if (softbed.has_nightstand == 1)
  472. {
  473. dict.Add(2, new u_configure_type()
  474. {
  475. contfigtypeid = 0,
  476. contfigtypename = $"{prefix}床头柜",
  477. contfigtype = 0,
  478. usechflag = 1,
  479. flag = 0
  480. });
  481. }
  482. if (softbed.has_bedframe == 1)
  483. {
  484. dict.Add(4,new u_configure_type()
  485. {
  486. contfigtypeid = 0,
  487. contfigtypename = $"{prefix}床架",
  488. contfigtype = 0,
  489. usechflag = 1,
  490. flag = 0
  491. });
  492. }
  493. return dict;
  494. }
  495. /// <summary>
  496. /// 获取软床报价配置
  497. /// </summary>
  498. /// <param name="softbed"></param>
  499. /// <param name="extraWheres"></param>
  500. /// <returns></returns>
  501. public List<u_configure_type> GetSoftBedConfigureList(u_softbed softbed,List<string> extraWheres)
  502. {
  503. var codeMxList = new List<u_configure_codemx>();
  504. var selectStr = @"SELECT u_configure_codemx.pzid
  505. ,u_configure_codemx.printid
  506. ,u_configure_codemx.pzcodemx
  507. ,u_configure_codemx.namemx
  508. ,u_configure_code.name AS pzname
  509. ,u_configure_code.pzcode
  510. ,u_configure_code.inputtype
  511. ,LTRIM(RTRIM(u_configure_type.contfigtypename)) AS contfigtypename
  512. ,u_configure_code.typeid
  513. ,u_configure_codemx.ifdft
  514. FROM u_configure_codemx
  515. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  516. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid";
  517. var whereList = new List<string>();
  518. var nameList = new List<string>();
  519. if (softbed.softbed_id > 0 || softbed.template_id > 0)
  520. {
  521. // 模版配置获取
  522. int billid = softbed.softbed_id > 0 ? softbed.softbed_id : softbed.template_id;
  523. var _softbed = GetSoftBed(billid, "softbed_id,softbed_code,has_headboard,has_nightstand,has_bedframe,template_id,is_template");
  524. if(_softbed.is_template == 0)
  525. {
  526. _softbed = GetSoftBed(_softbed.template_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  527. }
  528. string prefix = $"{_softbed.softbed_code}|";
  529. if(_softbed.has_headboard == 1)
  530. {
  531. nameList.Add($"{prefix}床头");
  532. }
  533. if (_softbed.has_nightstand == 1)
  534. {
  535. nameList.Add($"{prefix}床头柜");
  536. }
  537. if (_softbed.has_bedframe == 1)
  538. {
  539. nameList.Add($"{prefix}床架");
  540. }
  541. }
  542. if(nameList.Count > 0)
  543. {
  544. whereList.Add($"LTRIM(RTRIM(u_configure_type.contfigtypename)) IN {ListEx.getString(nameList)}");
  545. }
  546. if(extraWheres != null && extraWheres.Count > 0)
  547. {
  548. whereList.AddRange(extraWheres);
  549. }
  550. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), null, "typeid,pzid,printid", "typeid,pzid,printid,pzcodemx,namemx,pzname,pzcode,contfigtypename,ifdft", 0, 0, codeMxList);
  551. // 部件选配项
  552. var codeMap = new Dictionary<int, u_configure_code>();
  553. foreach(var codeMx in codeMxList)
  554. {
  555. int pzid = codeMx.pzid.Value;
  556. if (!codeMap.TryGetValue(pzid, out var config))
  557. {
  558. config = new u_configure_code
  559. {
  560. pzid = pzid,
  561. name = codeMx.pzname,
  562. contfigtypename = codeMx.contfigtypename,
  563. pzcode = codeMx.pzcode,
  564. typeid = codeMx.typeid,
  565. codeMxList = new List<u_configure_codemx>()
  566. };
  567. codeMap.Add(pzid, config);
  568. }
  569. config.codeMxList.Add(codeMx);
  570. }
  571. // 部件选配类型
  572. var typeMap = new Dictionary<int, u_configure_type>();
  573. foreach(var code in codeMap.Values.ToList())
  574. {
  575. int typeid = code.typeid.Value;
  576. if (!typeMap.TryGetValue(typeid, out var config))
  577. {
  578. config = new u_configure_type
  579. {
  580. contfigtypeid = typeid,
  581. contfigtypename = code.contfigtypename,
  582. codeList = new List<u_configure_code>()
  583. };
  584. typeMap.Add(typeid, config);
  585. }
  586. config.codeList.Add(code);
  587. }
  588. return typeMap.Values.ToList();
  589. }
  590. /// <summary>
  591. /// 复制软床报价
  592. /// </summary>
  593. /// <param name="softbed"></param>
  594. /// <param name="newId"></param>
  595. public u_softbed CopySoftBed(u_softbed softbed)
  596. {
  597. var softbed_copy = ObjectHelper.DeepCopy(softbed);
  598. softbed_copy.softbed_id = 0;
  599. SaveSoftBed(softbed_copy);
  600. if(softbed_copy.is_template == 1 && softbed.copy_id > 0)
  601. {
  602. // 复制配置到新配置
  603. var _softbed = GetSoftBed(softbed.copy_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  604. if(_softbed.has_headboard == 1 && softbed_copy.has_headboard == 1)
  605. {
  606. CopyConfigureData($"{_softbed.softbed_code}|床头",$"{softbed_copy.softbed_code}|床头");
  607. }
  608. if (_softbed.has_nightstand == 1 && softbed_copy.has_nightstand == 1)
  609. {
  610. CopyConfigureData($"{_softbed.softbed_code}|床头柜", $"{softbed_copy.softbed_code}|床头柜");
  611. }
  612. if (_softbed.has_bedframe == 1 && softbed_copy.has_bedframe == 1)
  613. {
  614. CopyConfigureData($"{_softbed.softbed_code}|床架", $"{softbed_copy.softbed_code}|床架");
  615. }
  616. }
  617. return softbed_copy;
  618. }
  619. /// <summary>
  620. /// 删除软床报价
  621. /// </summary>
  622. /// <param name="billid"></param>
  623. /// <exception cref="LJCommonException"></exception>
  624. public void DeleteSoftBed(int billid)
  625. {
  626. var softbed = GetSoftBed(billid, "softbed_code,flag");
  627. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  628. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  629. DbSqlHelper.Delete(cmd, softbed);
  630. }
  631. /// <summary>
  632. /// 审核软床报价
  633. /// </summary>
  634. /// <param name="billid"></param>
  635. /// <exception cref="LJCommonException"></exception>
  636. public void AuditSoftBed(int billid)
  637. {
  638. var softbed = GetSoftBed(billid, "softbed_code,flag");
  639. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  640. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  641. softbed.flag = 1;
  642. softbed.audit_date = context.opdate;
  643. softbed.audit_emp = context.tokendata.username;
  644. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  645. }
  646. /// <summary>
  647. /// 撤审软床报价
  648. /// </summary>
  649. /// <param name="billid"></param>
  650. /// <exception cref="LJCommonException"></exception>
  651. public void CAuditSoftBed(int billid)
  652. {
  653. var softbed = GetSoftBed(billid, "softbed_code,flag");
  654. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  655. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  656. softbed.flag = 0;
  657. softbed.audit_date = null;
  658. softbed.audit_emp = string.Empty;
  659. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  660. }
  661. public void CalCulateFormula(u_softbed softbed)
  662. {
  663. InitSoftBed(softbed);
  664. InitReplaceMents(softbed);
  665. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  666. foreach (var mx in softbed.mxList)
  667. {
  668. InitMxReplaceMents(softbed, mx);
  669. formula.CalculateAll();
  670. // 转换后的文本公式
  671. mx.use_formula_str = formula.GetFormulaItem("【实际用量】").formula_transform;
  672. mx.price_formula_str = formula.GetFormulaItem("【成本单价】").formula_transform;
  673. // 成本单价
  674. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  675. // 实际用量
  676. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  677. // 成本金额
  678. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  679. // 总材料成本
  680. total_mtrl_cost += mx.cost_amt;
  681. // 总人力成本
  682. total_hr_cost += 0;
  683. }
  684. if(softbed.mxList != null && softbed.mxList.Count > 0)
  685. {
  686. softbed.total_mtrl_cost = total_mtrl_cost;
  687. softbed.total_hr_cost = total_hr_cost;
  688. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  689. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  690. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  691. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  692. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  693. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  694. }
  695. //formula.CalculateAll();
  696. }
  697. private void InitSoftBed(u_softbed softbed)
  698. {
  699. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  700. //
  701. softbed.dept_profitrate = dept.profitrate;
  702. //
  703. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  704. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  705. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  706. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  707. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  708. // 检查佣金是否小于1
  709. if (softbed.commission < 1)
  710. {
  711. throw new LJCommonException("佣金点数不能小于1!");
  712. }
  713. // 检查税率是否小于1
  714. if (softbed.taxrate < 1)
  715. {
  716. throw new LJCommonException("税率不能小于1!");
  717. }
  718. // 检查额外点数是否小于1
  719. if (softbed.other_rate < 1)
  720. {
  721. throw new LJCommonException("税率不能小于1!");
  722. }
  723. // 检查部门利润率是否为0
  724. if (softbed.dept_profitrate == 0)
  725. {
  726. throw new LJCommonException("部门利润率不能为0!");
  727. }
  728. if(softbed.is_template == 1)
  729. {
  730. // 使用模板报价
  731. softbed.template_id = 0;
  732. softbed.template_code = string.Empty;
  733. softbed.template_name = string.Empty;
  734. }
  735. }
  736. private void InitReplaceMents(u_softbed softbed)
  737. {
  738. // 常量变量
  739. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  740. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  741. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  742. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  743. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  744. formula.AddFormulaItem("【税率】", softbed.taxrate);
  745. formula.AddFormulaItem("【工厂利润率】", 1);
  746. formula.AddFormulaItem("【FOB】", 0);
  747. formula.AddFormulaItem("【外销加点】", 0);
  748. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  749. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  750. // 公式变量
  751. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  752. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  753. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  754. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  755. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  756. }
  757. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  758. {
  759. // 默认公式变量
  760. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  761. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  762. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  763. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  764. // 常量变量
  765. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  766. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  767. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  768. formula.AddFormulaItem("【用料量】", mx.useqty);
  769. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  770. formula.AddFormulaItem("【材料单价】", mx.price);
  771. if(mx.formulaid > 0)
  772. {
  773. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  774. ,u_softbed_formula.formulaname
  775. ,u_softbed_formula.use_formula
  776. ,u_softbed_formula.price_formula
  777. FROM u_softbed_formula
  778. WHERE formulaid = @formulaid";
  779. cmd.Parameters.Clear();
  780. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  781. using(var reader = cmd.ExecuteReader())
  782. {
  783. if(reader.Read())
  784. {
  785. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  786. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  787. }
  788. }
  789. }
  790. }
  791. private List<string> GetConfigureTypeNameFromBillId(int billid)
  792. {
  793. var _softbed = GetSoftBed(billid, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  794. var nameList = new List<string>();
  795. string prefix = $"{_softbed.softbed_code}|";
  796. if (_softbed.has_headboard == 1)
  797. {
  798. nameList.Add($"{_softbed.softbed_code}|床头");
  799. }
  800. if (_softbed.has_nightstand == 1)
  801. {
  802. nameList.Add($"{_softbed.softbed_code}|床头柜");
  803. }
  804. if (_softbed.has_bedframe == 1)
  805. {
  806. nameList.Add($"{_softbed.softbed_code}|床架");
  807. }
  808. return nameList;
  809. }
  810. private void CopyConfigureData(string SourceName, string TargetName)
  811. {
  812. var commonHelper = GetHelper<CommonHelper>(cmd, context);
  813. var basicInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  814. // 入参非空验证
  815. if (string.IsNullOrWhiteSpace(SourceName))
  816. {
  817. throw new LJCommonException("源配置名称不能为空");
  818. }
  819. if (string.IsNullOrWhiteSpace(TargetName))
  820. {
  821. throw new LJCommonException("目标配置名称不能为空");
  822. }
  823. int SourceId = 0, TargetId = 0;
  824. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@SourceName))";
  825. cmd.Parameters.Clear();
  826. cmd.Parameters.AddWithValue("@SourceName", SourceName);
  827. using(var reader = cmd.ExecuteReader())
  828. {
  829. if(reader.Read())
  830. {
  831. SourceId = Convert.ToInt32(reader["contfigtypeid"]);
  832. }
  833. }
  834. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@TargetName))";
  835. cmd.Parameters.Clear();
  836. cmd.Parameters.AddWithValue("@TargetName", TargetName);
  837. using (var reader = cmd.ExecuteReader())
  838. {
  839. if (reader.Read())
  840. {
  841. TargetId = Convert.ToInt32(reader["contfigtypeid"]);
  842. }
  843. }
  844. if (SourceId == 0) throw new LJCommonException($"源配置名称「{SourceName.Trim()}」未找到对应的结果");
  845. if (TargetId == 0) throw new LJCommonException($"目标配置名称「{TargetName.Trim()}」未找到对应的结果");
  846. // 部件选配项
  847. var codeList = commonHelper.ExecuteDynamicSelectToList<u_configure_code>("web_configure_codelist",new Newtonsoft.Json.Linq.JObject() { { "typeid",SourceId} },0,0);
  848. var code_copyList = new List<u_configure_code>();
  849. var codeIdMap = new Dictionary<int, int>();
  850. foreach(var code in codeList)
  851. {
  852. var code_copy = ObjectHelper.DeepCopy(code);
  853. code_copy.pzid = BllHelper.GetID(cmd, "u_configure_code");
  854. code_copy.typeid = TargetId;
  855. code_copyList.Add(code_copy);
  856. if (codeIdMap.ContainsKey(code.pzid.Value)) continue;
  857. codeIdMap.Add(code.pzid.Value, code_copy.pzid.Value);
  858. //basicInfoHelper.SaveConfigureCode(code_copy);
  859. }
  860. 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");
  861. // 部件选配值
  862. var codeMxList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemx>("web_configure_codemxlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  863. var codeMx_copyList = new List<u_configure_codemx>();
  864. foreach (var codeMx in codeMxList)
  865. {
  866. if (!codeIdMap.TryGetValue(codeMx.pzid.Value, out int newId)) continue;
  867. var codeMx_copy = ObjectHelper.DeepCopy(codeMx);
  868. codeMx_copy.pzid = newId;
  869. codeMx_copyList.Add(codeMx_copy);
  870. }
  871. 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");
  872. // 部件选配物料清单
  873. var codeMxBomList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemxbom>("web_configure_codemxbomlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  874. var codeMxBom_copyList = new List<u_configure_codemxbom>();
  875. foreach (var codeMxBom in codeMxBomList)
  876. {
  877. if (!codeIdMap.TryGetValue(codeMxBom.pzid, out int newId)) continue;
  878. var codeMxBom_copy = ObjectHelper.DeepCopy(codeMxBom);
  879. codeMxBom_copy.pzid = newId;
  880. codeMxBom_copyList.Add(codeMxBom_copy);
  881. }
  882. 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");
  883. }
  884. #region 通用公式
  885. private CalculateFormula formula = new CalculateFormula();
  886. private string BillKeyWord = "u_softbed";
  887. #endregion
  888. }
  889. }