SoftBedHelper.cs 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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 NPOI.Util;
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Diagnostics;
  16. using System.Linq;
  17. using System.Security.Cryptography;
  18. using System.Text;
  19. using System.Text.RegularExpressions;
  20. using System.Threading.Tasks;
  21. using System.Web;
  22. using System.Web.Configuration;
  23. using System.Web.UI.WebControls;
  24. using static JLHHJSvr.Helper.CacheHelper;
  25. using static System.Runtime.CompilerServices.RuntimeHelpers;
  26. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Menu;
  27. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
  28. namespace JLHHJSvr.Helper
  29. {
  30. internal class SoftBedHelper : HelperBase
  31. {
  32. /// <summary>
  33. /// 获取软床报价单据
  34. /// </summary>
  35. /// <param name="billid"></param>
  36. /// <param name="fields"></param>
  37. /// <param name="bill"></param>
  38. public u_softbed GetSoftBed(int billid,string fields = null)
  39. {
  40. 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,
  41. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  42. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version,flag,audit_date,audit_emp,update_date,update_emp";
  43. var bill = new u_softbed() { softbed_id = billid };
  44. if (DbSqlHelper.SelectOne(cmd, bill, fields) == 0) return null;
  45. return bill;
  46. }
  47. /// <summary>
  48. /// 获取软床报价单据明细
  49. /// </summary>
  50. /// <param name="billid"></param>
  51. /// <param name="fields"></param>
  52. /// <param name="mxlist"></param>
  53. public List<u_softbed_mx> GetSoftBedMxList(int billid,string fields = null)
  54. {
  55. fields = fields ?? @"softbed_id,printid,formulaid,pzid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,if_no_config,cutting_length,cutting_width,cutting_qty,
  56. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,pzname,formulaname,parts_type,pzmxid,pznamemx";
  57. var mxlist = new List<u_softbed_mx>();
  58. var selectStr = @"SELECT u_softbed_mx.softbed_id
  59. ,u_softbed_mx.printid
  60. ,u_softbed_mx.formulaid
  61. ,u_softbed_mx.pzid
  62. ,u_softbed_mx.mtrlid
  63. ,u_softbed_mx.mtrlname
  64. ,u_softbed_mx.mtrlcode
  65. ,u_softbed_mx.mtrlmode
  66. ,u_softbed_mx.unit
  67. ,u_softbed_mx.has_type
  68. ,u_softbed_mx.allow_edit
  69. ,u_softbed_mx.if_no_config
  70. ,u_softbed_mx.cutting_length
  71. ,u_softbed_mx.cutting_width
  72. ,u_softbed_mx.cutting_qty
  73. ,u_softbed_mx.useqty
  74. ,u_softbed_mx.use_formula
  75. ,u_softbed_mx.use_formula_str
  76. ,u_softbed_mx.actual_useqty
  77. ,u_softbed_mx.loss_rate
  78. ,u_softbed_mx.price
  79. ,u_softbed_mx.price_formula
  80. ,u_softbed_mx.price_formula_str
  81. ,u_softbed_mx.cost_price
  82. ,u_softbed_mx.cost_amt
  83. ,ISNULL(u_configure_code.name,u_softbed_mx.pzname) AS pzname
  84. ,u_softbed_mx.parts_type
  85. ,ISNULL(u_softbed_formula.formulaname,'') AS formulaname
  86. ,u_softbed_mx.pzmxid
  87. ,ISNULL(u_configure_codemx.namemx,'') AS pznamemx
  88. FROM u_softbed_mx
  89. LEFT JOIN u_configure_code ON u_softbed_mx.pzid = u_configure_code.pzid
  90. LEFT JOIN u_configure_codemx ON u_softbed_mx.pzid = u_configure_codemx.pzid
  91. AND u_softbed_mx.pzmxid = u_configure_codemx.printid
  92. LEFT JOIN u_softbed_formula ON u_softbed_mx.formulaid = u_softbed_formula.formulaid
  93. ";
  94. DbSqlHelper.SelectJoin(cmd, selectStr, "softbed_id = @billid", new Dictionary<string, object>() { { "@billid", billid } }, "has_type,printid", fields, 0, 0, mxlist);
  95. return mxlist;
  96. }
  97. public List<u_softbed_mx> GetChangeSoftBedMxList(int billid,List<u_softbed_mx> mxList,List<u_configure_codemx> codeMxList)
  98. {
  99. var newList = new List<u_softbed_mx>();
  100. var pzid0List = new List<u_softbed_mx>();
  101. var softbed = GetSoftBed(billid, "billid,softbed_code,has_headboard,has_nightstand,has_bedframe,is_template,template_id");
  102. if (softbed == null) return newList;
  103. // 换料清单
  104. var bomList = GetSoftBedMxBomList(codeMxList);
  105. // var typeDict = new Dictionary<string, List<u_softbed_mx>>();
  106. //foreach(var mx in mxList)
  107. //{
  108. // var key = $"{mx.has_type}|{mx.pzid}";
  109. // if(!typeDict.ContainsKey(key))
  110. // {
  111. // typeDict.Add(key, new List<u_softbed_mx>());
  112. // }
  113. // typeDict[key].Add(mx);
  114. // if (mx.pzid == 0) pzid0List.Add(mx);
  115. // }
  116. // 原Dictionary替换为List<KeyValuePair>,维护键值对的插入顺序
  117. var typeList = new List<KeyValuePair<string, List<u_softbed_mx>>>();
  118. foreach (var mx in mxList)
  119. {
  120. var key = $"{mx.has_type}|{mx.pzid}";
  121. var existingItem = typeList.FirstOrDefault(kvp => kvp.Key == key);
  122. if (existingItem.Equals(default(KeyValuePair<string, List<u_softbed_mx>>)))
  123. {
  124. var newMxList = new List<u_softbed_mx> { mx };
  125. typeList.Add(new KeyValuePair<string, List<u_softbed_mx>>(key, newMxList));
  126. }
  127. else
  128. {
  129. existingItem.Value.Add(mx);
  130. }
  131. if (mx.pzid == 0)
  132. {
  133. pzid0List.Add(mx);
  134. }
  135. }
  136. var bomDict = new Dictionary<string, List<u_softbed_mx>>();
  137. foreach (var bom in bomList)
  138. {
  139. var key = $"{bom.has_type}|{bom.pzid}";
  140. if (!bomDict.ContainsKey(key))
  141. {
  142. bomDict.Add(key, new List<u_softbed_mx>());
  143. }
  144. bomDict[key].Add(bom);
  145. }
  146. /**
  147. * 1、模板报价
  148. * 2、非模板报价
  149. * 仅替换配置,保留其他明细信息
  150. * 1、如果明细存在bomList中,证明没有替换,直接添加到newList
  151. * 2、如果明细不存在bomList中,证明可能是给替换的(2.1)或者是非部件(即手动添加2.2)
  152. */
  153. foreach (var kvp in typeList)
  154. {
  155. if (bomDict.TryGetValue(kvp.Key, out var temp_bomList))
  156. {
  157. bool isExist = false;
  158. foreach (var mx in kvp.Value)
  159. {
  160. isExist = temp_bomList.FindIndex(t => t.mtrlid == mx.mtrlid) > -1;
  161. }
  162. if (isExist)
  163. {
  164. // 1
  165. foreach (var mx in kvp.Value)
  166. {
  167. mx.printid = newList.Count + 1;
  168. newList.Add(mx);
  169. }
  170. }
  171. else
  172. {
  173. // 2.1
  174. foreach (var mx in temp_bomList)
  175. {
  176. mx.printid = newList.Count + 1;
  177. newList.Add(mx);
  178. }
  179. }
  180. bomDict.Remove(kvp.Key);
  181. }
  182. }
  183. // 新增的部件
  184. foreach (var item in bomDict)
  185. {
  186. foreach (var mx in item.Value)
  187. {
  188. mx.printid = newList.Count + 1;
  189. newList.Add(mx);
  190. }
  191. }
  192. // 2.2
  193. foreach (var mx in pzid0List)
  194. {
  195. mx.printid = newList.Count + 1;
  196. newList.Add(mx);
  197. }
  198. if (newList.Count <= 0) newList.AddRange(bomList);
  199. return newList;
  200. }
  201. /// <summary>
  202. /// 根据部件配置项值获取对应BOM明细
  203. /// </summary>
  204. /// <param name="mxlist"></param>
  205. /// <param name="codeList"></param>
  206. public List<u_softbed_mx> GetSoftBedMxBomList(List<u_configure_codemx> codeList)
  207. {
  208. var mxlist = new List<u_softbed_mx>();
  209. foreach(var code in codeList)
  210. {
  211. var resultList = new List<u_configure_codemxbom>();
  212. var outputFields = @"pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,cost,sonloss,sonloss_formula,
  213. sondecloss,sondecloss_formula,default_length,default_width,default_qty,mtrlcode,mtrlname,mtrlmode,unit,
  214. mtrlsectype,zxmtrlmode,usermtrlmode,price,pzname";
  215. var selectStr = @"SELECT u_configure_codemxbom.pzid
  216. ,u_configure_codemxbom.printid
  217. ,u_configure_codemxbom.pid
  218. ,u_configure_codemxbom.mtrlid
  219. ,u_configure_codemxbom.sonscale
  220. ,u_configure_codemxbom.sonscale_formula
  221. ,u_configure_codemxbom.mng_cost_rate
  222. ,u_configure_codemxbom.profit_rate
  223. ,u_configure_codemxbom.realqty
  224. ,u_configure_codemxbom.cost
  225. ,u_configure_codemxbom.sonloss
  226. ,u_configure_codemxbom.sonloss_formula
  227. ,u_configure_codemxbom.sondecloss
  228. ,u_configure_codemxbom.sondecloss_formula
  229. ,u_configure_codemxbom.default_length
  230. ,u_configure_codemxbom.default_width
  231. ,u_configure_codemxbom.default_qty
  232. ,u_mtrldef.mtrlcode
  233. ,u_mtrldef.mtrlname
  234. ,u_mtrldef.mtrlmode
  235. ,u_mtrldef.unit
  236. ,u_mtrldef.mtrlsectype
  237. ,u_mtrldef.zxmtrlmode
  238. ,u_mtrldef.usermtrlmode
  239. ,u_erpmtrl_price.price
  240. ,u_configure_codemx.namemx AS pzname
  241. FROM u_configure_codemxbom
  242. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  243. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  244. INNER JOIN u_mtrldef ON u_configure_codemxbom.mtrlid = u_mtrldef.mtrlid
  245. LEFT OUTER JOIN u_erpmtrl_price ON u_configure_codemxbom.mtrlid = u_erpmtrl_price.mtrlid";
  246. DbSqlHelper.SelectJoin(cmd,selectStr,
  247. "u_configure_codemxbom.pzid = @pzid AND u_configure_codemxbom.printid = @printid",
  248. new Dictionary<string, object>() { { "@pzid", code.pzid }, { "@printid",code.printid} },
  249. "u_configure_codemxbom.pid", outputFields, 0,0,resultList);
  250. foreach(var item in resultList)
  251. {
  252. mxlist.Add(InitSoftBedMxFromBom(item, code.has_type.Value));
  253. }
  254. }
  255. return mxlist;
  256. }
  257. /// <summary>
  258. /// 初始化配置项值带出的BOM清单列表
  259. /// </summary>
  260. /// <param name="bomItem"></param>
  261. /// <returns></returns>
  262. public u_softbed_mx InitSoftBedMxFromBom(u_configure_codemxbom bomItem,byte has_type)
  263. {
  264. var mx = new u_softbed_mx()
  265. {
  266. pzid = bomItem.pzid,
  267. mtrlid = bomItem.mtrlid,
  268. mtrlname = bomItem.mtrlname,
  269. mtrlcode = bomItem.mtrlcode,
  270. mtrlmode = bomItem.mtrlmode,
  271. unit = bomItem.unit,
  272. allow_edit = 0,
  273. has_type = has_type, // 1-床头、2-床头柜、4-床架
  274. cutting_length = bomItem.default_length,
  275. cutting_width = bomItem.default_width,
  276. cutting_qty = bomItem.default_qty,
  277. price = bomItem.price,
  278. price_formula = "",
  279. price_formula_str = "",
  280. useqty = bomItem.sonscale,
  281. use_formula = "",
  282. use_formula_str = "",
  283. actual_useqty = 0,
  284. loss_rate = bomItem.sonloss,
  285. cost_price = 0,
  286. cost_amt = 0,
  287. pzname = bomItem.pzname
  288. };
  289. return mx;
  290. }
  291. /// <summary>
  292. /// 新建/修改 软床报价
  293. /// </summary>
  294. /// <param name="softbed"></param>
  295. /// <exception cref="LJCommonException"></exception>
  296. public void SaveSoftBed(u_softbed softbed)
  297. {
  298. SaveCheck(softbed);
  299. // 初始化属性
  300. AutoInit.AutoInitS(softbed);
  301. foreach(var mx in softbed.mxList)
  302. {
  303. AutoInit.AutoInitS(mx);
  304. if (mx.has_type != 8) mx.parts_type = string.Empty;
  305. else if (string.IsNullOrEmpty(mx.parts_type)) throw new LJCommonException($"明细行:{mx.printid},存在其他配件明细未设置配件类型,请检查!");
  306. }
  307. // 计算价格
  308. CalCulateFormula(softbed);
  309. //
  310. var dtNow = context.opdate;
  311. var fields = @"softbed_relcode,softbed_name,deptid,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  312. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  313. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version";
  314. var mx_fields = @"softbed_id,printid,pzid,formulaid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,if_no_config,cutting_length,cutting_width,cutting_qty,
  315. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,parts_type,pzname,pzmxid";
  316. if(softbed.softbed_id == 0)
  317. {
  318. // 新建
  319. fields += ",softbed_id,softbed_code,create_date,create_emp";
  320. softbed.softbed_id = BllHelper.GetID(cmd, "u_softbed");
  321. softbed.create_date = dtNow;
  322. softbed.create_emp = context.tokendata.username;
  323. softbed.version = 1;
  324. if (string.IsNullOrEmpty(softbed.softbed_code))
  325. {
  326. // 编号-年月日+流水
  327. softbed.softbed_code = $"RC-{dtNow.ToString("yyyyMMdd")}{(softbed.softbed_id % 10000).ToString("D4")}";
  328. }
  329. DbSqlHelper.Insert(cmd, "u_softbed", null, softbed, fields);
  330. } else
  331. {
  332. // 修改
  333. softbed.update_date = dtNow;
  334. softbed.update_emp = context.tokendata.username;
  335. softbed.version++;
  336. fields += ",update_date,update_emp";
  337. cmd.CommandText = @"DELETE FROM u_softbed_mx WHERE softbed_id = @softbed_id";
  338. cmd.Parameters.Clear();
  339. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  340. cmd.ExecuteNonQuery();
  341. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", fields);
  342. }
  343. for (int i = 0; i < softbed.mxList.Count; i++)
  344. {
  345. var mx = softbed.mxList[i];
  346. mx.softbed_id = softbed.softbed_id;
  347. mx.printid = i + 1;
  348. DbSqlHelper.Insert(cmd, "u_softbed_mx", null, mx, mx_fields);
  349. }
  350. SaveSoftBedTemplate(softbed);
  351. // 解锁
  352. LockHelper.UnLockBill(cmd, BillKeyWord, softbed.softbed_id, context.tokendata.username, 0);
  353. }
  354. private void SaveCheck(u_softbed softbed)
  355. {
  356. if (softbed == null) throw new LJCommonException("软床报价保存失败,数据异常!");
  357. if (softbed.is_template == 0 && (softbed.mxList == null || softbed.mxList.Count <= 0)) throw new LJCommonException("软床报价保存失败,明细内容为空!");
  358. if(softbed.softbed_id > 0)
  359. {
  360. // 检查锁
  361. if (LockHelper.CheckIsLock(cmd, MattressHelper.BillKeyWord, softbed.softbed_id, context.tokendata.username, out string arg_msg)) throw new LJCommonException(arg_msg);
  362. // 检测版本控制
  363. CheckBillVersion<u_softbed>(cmd, softbed.softbed_id, softbed.version);
  364. //var _softbed = GetSoftBed(softbed.softbed_id, "softbed_id,softbed_code,version,update_emp");
  365. //if (_softbed.version != softbed.version) throw new LJCommonException($"单据:{_softbed.softbed_code}已被{_softbed.update_emp}修改,请重新加载最新数据再操作!");
  366. }
  367. if(softbed.softbed_id > 0 && softbed.is_template != 0)
  368. {
  369. //
  370. var configureList = GetSoftBedConfigureName(softbed);
  371. var bomSet = new Dictionary<int,List<u_configure_codemxbom>>();
  372. foreach(var configure in configureList)
  373. {
  374. cmd.CommandText = @"SELECT u_configure_code.pzid
  375. ,u_configure_codemxbom.mtrlid
  376. ,LTRIM(RTRIM(u_configure_type.contfigtypename))
  377. ,u_configure_type.contfigtypeid
  378. FROM u_configure_codemxbom
  379. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  380. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  381. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  382. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid
  383. WHERE LTRIM(RTRIM(u_configure_type.contfigtypename)) = @contfigtypename";
  384. cmd.Parameters.Clear();
  385. cmd.Parameters.AddWithValue("@contfigtypename", configure.Value.contfigtypename);
  386. using (var reader = cmd.ExecuteReader())
  387. {
  388. while (reader.Read())
  389. {
  390. if (!bomSet.ContainsKey(configure.Key)) bomSet.Add(configure.Key, new List<u_configure_codemxbom>());
  391. bomSet[configure.Key].Add(new u_configure_codemxbom()
  392. {
  393. pzid = Convert.ToInt32(reader["pzid"]),
  394. mtrlid = Convert.ToInt32(reader["mtrlid"]),
  395. typeid = Convert.ToInt32(reader["contfigtypeid"]),
  396. has_type = configure.Key
  397. });
  398. }
  399. }
  400. }
  401. foreach (var mx in softbed.mxList)
  402. {
  403. if (mx.has_type == 8 || mx.if_no_config == 1) continue;
  404. if(!bomSet.TryGetValue(mx.has_type, out var bomList))
  405. {
  406. throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}没有设置物料清单");
  407. }
  408. //var cnt = bomList.Where(t => t.has_type == mx.has_type && t.mtrlid == mx.mtrlid).Count();
  409. //if (cnt > 1) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}存在多个清单,但没有设置部件!");
  410. //else if (cnt == 0) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}不存在于物料清单中,请检查!");
  411. }
  412. }
  413. }
  414. /// <summary>
  415. /// 新建软床报价模板
  416. /// </summary>
  417. /// <param name="softbed"></param>
  418. private void SaveSoftBedTemplate(u_softbed softbed)
  419. {
  420. SaveSoftBedTemplateV2(softbed);
  421. return;
  422. if (softbed.is_template == 0) return;
  423. if(string.IsNullOrEmpty(softbed.softbed_code))
  424. {
  425. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  426. }
  427. string prefix = $"{softbed.softbed_code}|";
  428. var configureList = GetSoftBedConfigureName(softbed).Values.ToList();
  429. foreach(var configure in configureList)
  430. {
  431. SaveSoftBedConfigure(configure);
  432. }
  433. // 生成标准选配项值ifdft
  434. if(softbed.codeMxList != null && softbed.codeMxList.Count > 0)
  435. {
  436. foreach (var codeMx in softbed.codeMxList)
  437. {
  438. if (codeMx.pzid == 0) continue;
  439. // 判断是否有标准
  440. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx 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. if(Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  445. {
  446. cmd.CommandText = @"UPDATE u_configure_codemx SET ifdft = 0 WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  447. cmd.Parameters.Clear();
  448. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  449. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  450. cmd.ExecuteNonQuery();
  451. }
  452. codeMx.ifdft = 1;
  453. DbSqlHelper.Update(cmd, "u_configure_codemx", null, codeMx, "pzid,printid", "ifdft");
  454. }
  455. }
  456. }
  457. /// <summary>
  458. /// 返回软床报价相关配置名
  459. /// </summary>
  460. /// <param name="softbed"></param>
  461. /// <returns></returns>
  462. private Dictionary<byte, u_configure_type> GetSoftBedConfigureName(u_softbed softbed)
  463. {
  464. var dict = new Dictionary<byte, u_configure_type>();
  465. string prefix = $"{softbed.softbed_code}|";
  466. if (softbed.has_headboard == 1)
  467. {
  468. dict.Add(1, new u_configure_type()
  469. {
  470. contfigtypeid = 0,
  471. contfigtypename = $"{prefix}床头",
  472. contfigtype = 0,
  473. usechflag = 1,
  474. flag = 0,
  475. codeList = GetSoftBedConfigureCode(softbed.mxList,1)
  476. });
  477. }
  478. if (softbed.has_nightstand == 1)
  479. {
  480. dict.Add(2, new u_configure_type()
  481. {
  482. contfigtypeid = 0,
  483. contfigtypename = $"{prefix}床头柜",
  484. contfigtype = 0,
  485. usechflag = 1,
  486. flag = 0,
  487. codeList = GetSoftBedConfigureCode(softbed.mxList, 2)
  488. });
  489. }
  490. if (softbed.has_bedframe == 1)
  491. {
  492. dict.Add(4,new u_configure_type()
  493. {
  494. contfigtypeid = 0,
  495. contfigtypename = $"{prefix}床架",
  496. contfigtype = 0,
  497. usechflag = 1,
  498. flag = 0,
  499. codeList = GetSoftBedConfigureCode(softbed.mxList, 4)
  500. });
  501. }
  502. return dict;
  503. }/// <summary>
  504. /// 返回软床报价相关配置项
  505. /// </summary>
  506. /// <param name="softbed"></param>
  507. /// <returns></returns>
  508. private List<u_configure_code> GetSoftBedConfigureCode(List<u_softbed_mx> mxList,byte has_type)
  509. {
  510. var tempMxList = mxList.Where(t => t.has_type == has_type && t.if_no_config == 0);
  511. var codeMap = new Dictionary<string, List<u_configure_codemx>> ();
  512. var codeMxMap = new Dictionary<Tuple<string,string>, List<u_softbed_mx>>();
  513. foreach(var mx in tempMxList)
  514. {
  515. //
  516. var codeMxKey = Tuple.Create(mx.pzname,mx.pznamemx);
  517. //
  518. if (!codeMxMap.ContainsKey(codeMxKey)) codeMxMap.Add(codeMxKey, new List<u_softbed_mx>());
  519. codeMxMap[codeMxKey].Add(mx);
  520. }
  521. var codeMxList = new List<u_configure_codemx>();
  522. foreach(var item in codeMxMap)
  523. {
  524. if (item.Value.Count <= 0) continue;
  525. var key = item.Key;
  526. var configureCodeMx = new u_configure_codemx()
  527. {
  528. pzid = item.Value.First().pzid,
  529. namemx = key.Item2,
  530. printid = item.Value.First().pzmxid,
  531. ifdft = 1
  532. };
  533. var bomList = new List<u_configure_codemxbom>();
  534. foreach(var mx in item.Value)
  535. {
  536. bomList.Add(new u_configure_codemxbom()
  537. {
  538. pzid = mx.pzid,
  539. printid = mx.pzmxid,
  540. pid = 0,
  541. mtrlid = mx.mtrlid,
  542. default_length = mx.cutting_length,
  543. default_width = mx.cutting_width,
  544. default_qty = mx.cutting_qty,
  545. price = mx.price,
  546. price_formula = mx.price_formula,
  547. sonscale = mx.useqty,
  548. sonscale_formula = mx.use_formula,
  549. sonloss = mx.loss_rate
  550. });
  551. }
  552. configureCodeMx.bomList = bomList;
  553. codeMxList.Add(configureCodeMx);
  554. if (!codeMap.ContainsKey(key.Item1)) codeMap.Add(key.Item1, codeMxList);
  555. }
  556. var resultList = new List<u_configure_code>();
  557. foreach(var item in codeMap)
  558. {
  559. if (item.Value.Count <= 0) continue;
  560. var key = item.Key;
  561. resultList.Add(new u_configure_code()
  562. {
  563. name = key,
  564. typeid = 0,
  565. pzid = item.Value.First().pzid,
  566. ifuse = 1,
  567. codeMxList = item.Value
  568. });
  569. }
  570. return resultList;
  571. }
  572. private void SaveSoftBedConfigure(u_configure_type configure)
  573. {
  574. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  575. // 判断配置是否已存在
  576. cmd.CommandText = @"SELECT COUNT(*) AS printid FROM u_configure_type INNER JOIN u_configure_code ON u_configure_type.contfigtypeid = u_configure_code.typeid WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  577. cmd.Parameters.Clear();
  578. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  579. int cnt = Convert.ToInt32(cmd.ExecuteScalar());
  580. if (cnt == 0)
  581. {
  582. baseInfoHelper.SaveConfigureType(configure);
  583. }
  584. // 处理配置项
  585. var codeList = new List<u_configure_code>();
  586. var codeMxList = new List<u_configure_codemx>();
  587. var codeMxBomList = new List<u_configure_codemxbom>();
  588. int codeNum = cnt == 0 ? 1 : cnt;
  589. foreach (var code in configure.codeList)
  590. {
  591. int printid = 1;
  592. if (code.pzid > 0)
  593. {
  594. cmd.CommandText = @"SELECT ISNULL(MAX(printid),0) AS printid FROM u_configure_codemx WHERE pzid = @pzid";
  595. cmd.Parameters.Clear();
  596. cmd.Parameters.AddWithValue("@pzid", code.pzid);
  597. using (var reader = cmd.ExecuteReader())
  598. {
  599. if (reader.Read())
  600. {
  601. printid = Convert.ToInt32(reader["printid"]);
  602. }
  603. }
  604. }
  605. //
  606. code.typeid = configure.contfigtypeid;
  607. code.pzid = code.pzid > 0 ? code.pzid : BllHelper.GetID(cmd, "u_configure_code");
  608. code.pzcode = FormatCode(codeNum, 4);
  609. foreach(var codeMx in code.codeMxList)
  610. {
  611. codeMx.pzid = code.pzid;
  612. codeMx.printid = printid++;
  613. codeMx.pzcodemx = $"{code.pzcode}_{FormatCode(printid, 3)}";
  614. codeMxList.Add(codeMx);
  615. }
  616. }
  617. foreach(var codeMx in codeMxList)
  618. {
  619. int pid = 1;
  620. if(cnt > 0)
  621. {
  622. cmd.CommandText = @"SELECT ISNULL(MAX(pid),0) AS printid FROM u_configure_codemxbom WHERE pzid = @pzid AND printid = @printid";
  623. cmd.Parameters.Clear();
  624. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  625. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  626. using (var reader = cmd.ExecuteReader())
  627. {
  628. if (reader.Read())
  629. {
  630. pid = Convert.ToInt32(reader["printid"]);
  631. }
  632. }
  633. }
  634. foreach (var bom in codeMx.bomList)
  635. {
  636. bom.printid = codeMx.printid.Value;
  637. bom.pid = pid++;
  638. codeMxBomList.Add(bom);
  639. }
  640. }
  641. DbSqlHelper.BulkInsert(cmd, codeList, "u_configure_code", null, "typeid,pzid,pzcode,inputtype,name,configtype,ifcross,ifcheck,ifuse,rulestr,ifnum,decnum,maxnum,minnum,pricestr,priceratestr,ifpack,zj_special");
  642. DbSqlHelper.BulkInsert(cmd, codeMxList, "u_configure_codemx", null, "pzid,printid,pzcodemx,namemx,gradestr,mtrlcode,price,ifdft,MCostRate,ProfitRate,dscrp,ifuse,ifnoch,pricerate,packqty,packvol,price_pz,grade");
  643. DbSqlHelper.BulkInsert(cmd, codeMxBomList, "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");
  644. }
  645. public static string FormatCode(int number, int length)
  646. {
  647. int max = (int)Math.Pow(10, length) - 1;
  648. if (number > max)
  649. {
  650. throw new Exception($"编码超出长度限制,{length}位最多只能到 {max}");
  651. }
  652. return number.ToString($"D{length}");
  653. }
  654. /// <summary>
  655. /// 获取软床报价配置
  656. /// </summary>
  657. /// <param name="softbed"></param>
  658. /// <param name="extraWheres"></param>
  659. /// <returns></returns>
  660. public List<u_configure_type> GetSoftBedConfigureList(u_softbed softbed,List<string> extraWheres)
  661. {
  662. var codeMxList = new List<u_configure_codemx>();
  663. var selectStr = @"SELECT u_configure_codemx.pzid
  664. ,u_configure_codemx.printid
  665. ,u_configure_codemx.pzcodemx
  666. ,u_configure_codemx.namemx
  667. ,u_configure_code.name AS pzname
  668. ,u_configure_code.pzcode
  669. ,u_configure_code.inputtype
  670. ,LTRIM(RTRIM(u_configure_type.contfigtypename)) AS contfigtypename
  671. ,u_configure_code.typeid
  672. ,u_configure_codemx.ifdft
  673. FROM u_configure_codemx
  674. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  675. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid";
  676. var whereList = new List<string>();
  677. var nameList = new List<string>();
  678. if (softbed.softbed_id > 0 || softbed.template_id > 0)
  679. {
  680. // 模版配置获取
  681. int billid = softbed.softbed_id > 0 ? softbed.softbed_id : softbed.template_id;
  682. var _softbed = GetSoftBed(billid, "softbed_id,softbed_code,has_headboard,has_nightstand,has_bedframe,template_id,is_template");
  683. if(_softbed.is_template == 0)
  684. {
  685. _softbed = GetSoftBed(_softbed.template_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  686. }
  687. string prefix = $"{_softbed.softbed_code}|";
  688. if(_softbed.has_headboard == 1)
  689. {
  690. nameList.Add($"{prefix}床头");
  691. }
  692. if (_softbed.has_nightstand == 1)
  693. {
  694. nameList.Add($"{prefix}床头柜");
  695. }
  696. if (_softbed.has_bedframe == 1)
  697. {
  698. nameList.Add($"{prefix}床架");
  699. }
  700. }
  701. if(nameList.Count > 0)
  702. {
  703. whereList.Add($"LTRIM(RTRIM(u_configure_type.contfigtypename)) IN {ListEx.getString(nameList)}");
  704. }
  705. if(extraWheres != null && extraWheres.Count > 0)
  706. {
  707. whereList.AddRange(extraWheres);
  708. }
  709. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), null, "typeid,pzid,printid", "typeid,pzid,printid,pzcodemx,namemx,pzname,pzcode,contfigtypename,ifdft", 0, 0, codeMxList);
  710. // 部件选配项
  711. var codeMap = new Dictionary<int, u_configure_code>();
  712. foreach(var codeMx in codeMxList)
  713. {
  714. int pzid = codeMx.pzid.Value;
  715. if (!codeMap.TryGetValue(pzid, out var config))
  716. {
  717. config = new u_configure_code
  718. {
  719. pzid = pzid,
  720. name = codeMx.pzname,
  721. contfigtypename = codeMx.contfigtypename,
  722. pzcode = codeMx.pzcode,
  723. typeid = codeMx.typeid,
  724. codeMxList = new List<u_configure_codemx>()
  725. };
  726. codeMap.Add(pzid, config);
  727. }
  728. config.codeMxList.Add(codeMx);
  729. }
  730. // 部件选配类型
  731. var typeMap = new Dictionary<int, u_configure_type>();
  732. foreach(var code in codeMap.Values.ToList())
  733. {
  734. int typeid = code.typeid.Value;
  735. if (!typeMap.TryGetValue(typeid, out var config))
  736. {
  737. config = new u_configure_type
  738. {
  739. contfigtypeid = typeid,
  740. contfigtypename = code.contfigtypename,
  741. codeList = new List<u_configure_code>()
  742. };
  743. typeMap.Add(typeid, config);
  744. }
  745. config.codeList.Add(code);
  746. }
  747. return typeMap.Values.ToList();
  748. }
  749. /// <summary>
  750. /// 复制软床报价
  751. /// </summary>
  752. /// <param name="softbed"></param>
  753. /// <param name="newId"></param>
  754. public u_softbed CopySoftBed(u_softbed softbed)
  755. {
  756. var softbed_copy = ObjectHelper.DeepCopy(softbed);
  757. softbed_copy.softbed_id = 0;
  758. softbed_copy.softbed_code = string.Empty;
  759. SaveSoftBed(softbed_copy);
  760. //if(softbed_copy.is_template == 1 && softbed.copy_id > 0)
  761. //{
  762. // // 复制配置到新配置
  763. // var _softbed = GetSoftBed(softbed.copy_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  764. // if(_softbed.has_headboard == 1 && softbed_copy.has_headboard == 1)
  765. // {
  766. // CopyConfigureData($"{_softbed.softbed_code}|床头",$"{softbed_copy.softbed_code}|床头");
  767. // }
  768. // if (_softbed.has_nightstand == 1 && softbed_copy.has_nightstand == 1)
  769. // {
  770. // CopyConfigureData($"{_softbed.softbed_code}|床头柜", $"{softbed_copy.softbed_code}|床头柜");
  771. // }
  772. // if (_softbed.has_bedframe == 1 && softbed_copy.has_bedframe == 1)
  773. // {
  774. // CopyConfigureData($"{_softbed.softbed_code}|床架", $"{softbed_copy.softbed_code}|床架");
  775. // }
  776. //}
  777. return softbed_copy;
  778. }
  779. /// <summary>
  780. /// 删除软床报价
  781. /// </summary>
  782. /// <param name="billid"></param>
  783. /// <exception cref="LJCommonException"></exception>
  784. public void DeleteSoftBed(int billid)
  785. {
  786. var softbed = GetSoftBed(billid, "softbed_code,flag");
  787. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  788. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  789. DbSqlHelper.Delete(cmd, softbed);
  790. }
  791. /// <summary>
  792. /// 审核软床报价
  793. /// </summary>
  794. /// <param name="billid"></param>
  795. /// <exception cref="LJCommonException"></exception>
  796. public void AuditSoftBed(int billid)
  797. {
  798. var softbed = GetSoftBed(billid, "softbed_code,flag");
  799. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  800. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  801. softbed.flag = 1;
  802. softbed.audit_date = context.opdate;
  803. softbed.audit_emp = context.tokendata.username;
  804. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  805. }
  806. /// <summary>
  807. /// 撤审软床报价
  808. /// </summary>
  809. /// <param name="billid"></param>
  810. /// <exception cref="LJCommonException"></exception>
  811. public void CAuditSoftBed(int billid)
  812. {
  813. var softbed = GetSoftBed(billid, "softbed_code,flag");
  814. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  815. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  816. softbed.flag = 0;
  817. softbed.audit_date = null;
  818. softbed.audit_emp = string.Empty;
  819. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  820. }
  821. public void CalCulateFormula(u_softbed softbed)
  822. {
  823. InitSoftBed(softbed);
  824. InitReplaceMents(softbed);
  825. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  826. foreach (var mx in softbed.mxList)
  827. {
  828. InitMxReplaceMents(softbed, mx);
  829. formula.CalculateAll();
  830. // 转换后的文本公式
  831. mx.use_formula_str = formula.GetFormulaItem("【用料量】").formula_transform ?? string.Empty;
  832. mx.price_formula_str = formula.GetFormulaItem("【材料单价】").formula_transform ?? string.Empty;
  833. // 成本单价
  834. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  835. // 实际用量
  836. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  837. // 成本金额
  838. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  839. // 总材料成本
  840. total_mtrl_cost += mx.cost_amt;
  841. // 总人力成本
  842. total_hr_cost += 0;
  843. }
  844. if(softbed.mxList != null && softbed.mxList.Count > 0)
  845. {
  846. softbed.total_mtrl_cost = total_mtrl_cost;
  847. softbed.total_hr_cost = total_hr_cost;
  848. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  849. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  850. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  851. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  852. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  853. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  854. }
  855. //formula.CalculateAll();
  856. }
  857. private void InitSoftBed(u_softbed softbed)
  858. {
  859. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  860. //
  861. softbed.dept_profitrate = dept.profitrate;
  862. //
  863. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  864. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  865. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  866. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  867. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  868. // 检查佣金是否小于1
  869. if (softbed.commission < 1)
  870. {
  871. throw new LJCommonException("佣金点数不能小于1!");
  872. }
  873. // 检查税率是否小于1
  874. if (softbed.taxrate < 1)
  875. {
  876. throw new LJCommonException("税率不能小于1!");
  877. }
  878. // 检查额外点数是否小于1
  879. if (softbed.other_rate < 1)
  880. {
  881. throw new LJCommonException("税率不能小于1!");
  882. }
  883. // 检查部门利润率是否为0
  884. if (softbed.dept_profitrate == 0)
  885. {
  886. throw new LJCommonException("部门利润率不能为0!");
  887. }
  888. if(softbed.is_template == 1)
  889. {
  890. // 使用模板报价
  891. softbed.template_id = 0;
  892. softbed.template_code = string.Empty;
  893. softbed.template_name = string.Empty;
  894. }
  895. }
  896. private void InitReplaceMents(u_softbed softbed)
  897. {
  898. // 常量变量
  899. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  900. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  901. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  902. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  903. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  904. formula.AddFormulaItem("【税率】", softbed.taxrate);
  905. formula.AddFormulaItem("【工厂利润率】", 1);
  906. formula.AddFormulaItem("【FOB】", 0);
  907. formula.AddFormulaItem("【外销加点】", 0);
  908. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  909. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  910. // 公式变量
  911. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  912. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  913. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  914. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  915. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  916. }
  917. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  918. {
  919. // 默认公式变量
  920. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  921. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  922. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  923. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  924. // 用料量
  925. if(string.IsNullOrEmpty(mx.use_formula))
  926. {
  927. formula.AddFormulaItem("【用料量】", mx.useqty);
  928. } else
  929. {
  930. formula.AddFormulaItem("【用料量】", mx.use_formula);
  931. }
  932. // 材料单价
  933. if (string.IsNullOrEmpty(mx.price_formula))
  934. {
  935. formula.AddFormulaItem("【材料单价】", mx.price);
  936. }
  937. else
  938. {
  939. formula.AddFormulaItem("【材料单价】", mx.price_formula);
  940. }
  941. // 常量变量
  942. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  943. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  944. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  945. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  946. if(mx.formulaid > 0)
  947. {
  948. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  949. ,u_softbed_formula.formulaname
  950. ,u_softbed_formula.use_formula
  951. ,u_softbed_formula.price_formula
  952. FROM u_softbed_formula
  953. WHERE formulaid = @formulaid";
  954. cmd.Parameters.Clear();
  955. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  956. using(var reader = cmd.ExecuteReader())
  957. {
  958. if(reader.Read())
  959. {
  960. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  961. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  962. }
  963. }
  964. }
  965. }
  966. private List<string> GetConfigureTypeNameFromBillId(int billid)
  967. {
  968. var _softbed = GetSoftBed(billid, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  969. var nameList = new List<string>();
  970. string prefix = $"{_softbed.softbed_code}|";
  971. if (_softbed.has_headboard == 1)
  972. {
  973. nameList.Add($"{_softbed.softbed_code}|床头");
  974. }
  975. if (_softbed.has_nightstand == 1)
  976. {
  977. nameList.Add($"{_softbed.softbed_code}|床头柜");
  978. }
  979. if (_softbed.has_bedframe == 1)
  980. {
  981. nameList.Add($"{_softbed.softbed_code}|床架");
  982. }
  983. return nameList;
  984. }
  985. private void CopyConfigureData(string SourceName, string TargetName)
  986. {
  987. var commonHelper = GetHelper<CommonHelper>(cmd, context);
  988. var basicInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  989. // 入参非空验证
  990. if (string.IsNullOrWhiteSpace(SourceName))
  991. {
  992. throw new LJCommonException("源配置名称不能为空");
  993. }
  994. if (string.IsNullOrWhiteSpace(TargetName))
  995. {
  996. throw new LJCommonException("目标配置名称不能为空");
  997. }
  998. int SourceId = 0, TargetId = 0;
  999. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@SourceName))";
  1000. cmd.Parameters.Clear();
  1001. cmd.Parameters.AddWithValue("@SourceName", SourceName);
  1002. using(var reader = cmd.ExecuteReader())
  1003. {
  1004. if(reader.Read())
  1005. {
  1006. SourceId = Convert.ToInt32(reader["contfigtypeid"]);
  1007. }
  1008. }
  1009. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@TargetName))";
  1010. cmd.Parameters.Clear();
  1011. cmd.Parameters.AddWithValue("@TargetName", TargetName);
  1012. using (var reader = cmd.ExecuteReader())
  1013. {
  1014. if (reader.Read())
  1015. {
  1016. TargetId = Convert.ToInt32(reader["contfigtypeid"]);
  1017. }
  1018. }
  1019. if (SourceId == 0) throw new LJCommonException($"源配置名称「{SourceName.Trim()}」未找到对应的结果");
  1020. if (TargetId == 0) throw new LJCommonException($"目标配置名称「{TargetName.Trim()}」未找到对应的结果");
  1021. // 部件选配项
  1022. var codeList = commonHelper.ExecuteDynamicSelectToList<u_configure_code>("web_configure_codelist",new Newtonsoft.Json.Linq.JObject() { { "typeid",SourceId} },0,0);
  1023. var code_copyList = new List<u_configure_code>();
  1024. var codeIdMap = new Dictionary<int, int>();
  1025. foreach(var code in codeList)
  1026. {
  1027. var code_copy = ObjectHelper.DeepCopy(code);
  1028. code_copy.pzid = BllHelper.GetID(cmd, "u_configure_code");
  1029. code_copy.typeid = TargetId;
  1030. code_copyList.Add(code_copy);
  1031. if (codeIdMap.ContainsKey(code.pzid.Value)) continue;
  1032. codeIdMap.Add(code.pzid.Value, code_copy.pzid.Value);
  1033. //basicInfoHelper.SaveConfigureCode(code_copy);
  1034. }
  1035. 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");
  1036. // 部件选配值
  1037. var codeMxList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemx>("web_configure_codemxlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  1038. var codeMx_copyList = new List<u_configure_codemx>();
  1039. foreach (var codeMx in codeMxList)
  1040. {
  1041. if (!codeIdMap.TryGetValue(codeMx.pzid.Value, out int newId)) continue;
  1042. var codeMx_copy = ObjectHelper.DeepCopy(codeMx);
  1043. codeMx_copy.pzid = newId;
  1044. codeMx_copyList.Add(codeMx_copy);
  1045. }
  1046. 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");
  1047. // 部件选配物料清单
  1048. var codeMxBomList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemxbom>("web_configure_codemxbomlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  1049. var codeMxBom_copyList = new List<u_configure_codemxbom>();
  1050. foreach (var codeMxBom in codeMxBomList)
  1051. {
  1052. if (!codeIdMap.TryGetValue(codeMxBom.pzid, out int newId)) continue;
  1053. var codeMxBom_copy = ObjectHelper.DeepCopy(codeMxBom);
  1054. codeMxBom_copy.pzid = newId;
  1055. codeMxBom_copyList.Add(codeMxBom_copy);
  1056. }
  1057. 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");
  1058. }
  1059. /// <summary>
  1060. /// 新版模板核价资料保存。
  1061. /// 仅处理模板报价,将报价明细同步到核价配置资料,并将生成的 pzid/pzmxid 回写到报价明细。
  1062. /// </summary>
  1063. /// <param name="softbed"></param>
  1064. public void SaveSoftBedTemplateV2(u_softbed softbed)
  1065. {
  1066. if (softbed == null) throw new LJCommonException("模板核价保存失败,数据异常!");
  1067. if (softbed.is_template == 0) return;
  1068. if (string.IsNullOrWhiteSpace(softbed.softbed_code)) throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  1069. if (softbed.mxList == null || softbed.mxList.Count <= 0) return;
  1070. var typeNameMap = GetSoftBedTemplateTypeNameMapV2(softbed);
  1071. foreach (var item in typeNameMap)
  1072. {
  1073. var typeMxList = softbed.mxList
  1074. .Where(t => t.has_type == item.Key)
  1075. .OrderBy(t => t.printid)
  1076. .ToList();
  1077. if (typeMxList.Count <= 0) continue;
  1078. var configMxList = typeMxList
  1079. .Where(t => t.if_no_config != 1)
  1080. .OrderBy(t => t.printid)
  1081. .ToList();
  1082. if (configMxList.Count <= 0) continue;
  1083. ValidateSoftBedTemplateMxListV2(item.Value, configMxList);
  1084. int typeId = EnsureSoftBedTemplateTypeV2(item.Value);
  1085. var codeMxGroups = configMxList
  1086. .GroupBy(t => new
  1087. {
  1088. PzName = (t.pzname ?? string.Empty).Trim(),
  1089. PzNameMx = (t.pznamemx ?? string.Empty).Trim()
  1090. })
  1091. .ToList();
  1092. foreach (var group in codeMxGroups)
  1093. {
  1094. var currentMxList = group.OrderBy(t => t.printid).ToList();
  1095. var firstMx = currentMxList.First();
  1096. int pzid = EnsureSoftBedTemplateCodeV2(typeId, firstMx.pzid, group.Key.PzName);
  1097. int pzmxid = EnsureSoftBedTemplateCodeMxV2(pzid, firstMx.pzmxid, group.Key.PzNameMx);
  1098. foreach (var mx in currentMxList)
  1099. {
  1100. mx.pzid = pzid;
  1101. mx.pzmxid = pzmxid;
  1102. mx.pzname = group.Key.PzName;
  1103. mx.pznamemx = group.Key.PzNameMx;
  1104. }
  1105. ReplaceSoftBedTemplateBomV2(pzid, pzmxid, currentMxList);
  1106. }
  1107. }
  1108. BackWriteSoftBedTemplateMxIdsV2(softbed);
  1109. }
  1110. private Dictionary<byte, string> GetSoftBedTemplateTypeNameMapV2(u_softbed softbed)
  1111. {
  1112. var dict = new Dictionary<byte, string>();
  1113. string prefix = $"{softbed.softbed_code}|";
  1114. if (softbed.has_headboard == 1) dict[1] = $"{prefix}床头";
  1115. if (softbed.has_nightstand == 1) dict[2] = $"{prefix}床头柜";
  1116. if (softbed.has_bedframe == 1) dict[4] = $"{prefix}床架";
  1117. return dict;
  1118. }
  1119. private void ValidateSoftBedTemplateMxListV2(string typeName, List<u_softbed_mx> mxList)
  1120. {
  1121. foreach (var mx in mxList)
  1122. {
  1123. if (string.IsNullOrWhiteSpace(mx.pzname))
  1124. {
  1125. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 未设置部件选配项名称(pzname)");
  1126. }
  1127. if (string.IsNullOrWhiteSpace(mx.pznamemx))
  1128. {
  1129. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 未设置部件选配明细名称(pznamemx)");
  1130. }
  1131. if (mx.mtrlid <= 0)
  1132. {
  1133. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 物料ID无效");
  1134. }
  1135. }
  1136. }
  1137. private int EnsureSoftBedTemplateTypeV2(string typeName)
  1138. {
  1139. cmd.CommandText = @"SELECT TOP 1 contfigtypeid
  1140. FROM u_configure_type
  1141. WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@contfigtypename))";
  1142. cmd.Parameters.Clear();
  1143. cmd.Parameters.AddWithValue("@contfigtypename", typeName);
  1144. object result = cmd.ExecuteScalar();
  1145. if (result != null && result != DBNull.Value)
  1146. {
  1147. return Convert.ToInt32(result);
  1148. }
  1149. var configure = new u_configure_type()
  1150. {
  1151. contfigtypeid = BllHelper.GetID(cmd, "u_configure_type"),
  1152. contfigtypename = typeName,
  1153. contfigtype = 0,
  1154. usechflag = 1,
  1155. flag = 0
  1156. };
  1157. DbSqlHelper.Insert(cmd, "u_configure_type", null, configure, "contfigtypeid,contfigtype,contfigtypename,usechflag,flag");
  1158. return configure.contfigtypeid.Value;
  1159. }
  1160. private int EnsureSoftBedTemplateCodeV2(int typeId, int currentPzid, string codeName)
  1161. {
  1162. if (string.IsNullOrWhiteSpace(codeName)) throw new LJCommonException("模板核价保存失败,配置项名称不能为空");
  1163. string pzcode = string.Empty;
  1164. if (currentPzid > 0)
  1165. {
  1166. bool isExists = false;
  1167. int dbTypeId = 0;
  1168. string dbName = string.Empty;
  1169. cmd.CommandText = @"SELECT TOP 1 typeid,pzcode,name
  1170. FROM u_configure_code
  1171. WHERE pzid = @pzid";
  1172. cmd.Parameters.Clear();
  1173. cmd.Parameters.AddWithValue("@pzid", currentPzid);
  1174. using (var reader = cmd.ExecuteReader())
  1175. {
  1176. if (reader.Read())
  1177. {
  1178. isExists = true;
  1179. pzcode = Convert.ToString(reader["pzcode"]);
  1180. dbTypeId = Convert.ToInt32(reader["typeid"]);
  1181. dbName = Convert.ToString(reader["name"]);
  1182. }
  1183. }
  1184. if (isExists)
  1185. {
  1186. if (dbTypeId != typeId || !string.Equals((dbName ?? string.Empty).Trim(), codeName.Trim(), StringComparison.Ordinal))
  1187. {
  1188. cmd.CommandText = @"UPDATE u_configure_code
  1189. SET typeid = @typeid,
  1190. pzcode = @pzcode,
  1191. name = @name,
  1192. inputtype = @inputtype,
  1193. configtype = @configtype,
  1194. ifcross = @ifcross,
  1195. ifcheck = @ifcheck,
  1196. ifuse = @ifuse,
  1197. ifnum = @ifnum,
  1198. decnum = @decnum,
  1199. ifpack = @ifpack
  1200. WHERE pzid = @pzid";
  1201. cmd.Parameters.Clear();
  1202. cmd.Parameters.AddWithValue("@typeid", typeId);
  1203. cmd.Parameters.AddWithValue("@pzcode", pzcode ?? string.Empty);
  1204. cmd.Parameters.AddWithValue("@name", codeName);
  1205. cmd.Parameters.AddWithValue("@inputtype", 0);
  1206. cmd.Parameters.AddWithValue("@configtype", 0);
  1207. cmd.Parameters.AddWithValue("@ifcross", 0);
  1208. cmd.Parameters.AddWithValue("@ifcheck", 0);
  1209. cmd.Parameters.AddWithValue("@ifuse", 1);
  1210. cmd.Parameters.AddWithValue("@ifnum", 0);
  1211. cmd.Parameters.AddWithValue("@decnum", 0);
  1212. cmd.Parameters.AddWithValue("@ifpack", 0);
  1213. cmd.Parameters.AddWithValue("@pzid", currentPzid);
  1214. cmd.ExecuteNonQuery();
  1215. }
  1216. return currentPzid;
  1217. }
  1218. }
  1219. cmd.CommandText = @"SELECT TOP 1 pzid
  1220. FROM u_configure_code
  1221. WHERE typeid = @typeid
  1222. AND LTRIM(RTRIM(name)) = LTRIM(RTRIM(@name))";
  1223. cmd.Parameters.Clear();
  1224. cmd.Parameters.AddWithValue("@typeid", typeId);
  1225. cmd.Parameters.AddWithValue("@name", codeName);
  1226. object result = cmd.ExecuteScalar();
  1227. if (result != null && result != DBNull.Value)
  1228. {
  1229. return Convert.ToInt32(result);
  1230. }
  1231. int nextPzid = BllHelper.GetID(cmd, "u_configure_code");
  1232. pzcode = GetNextSoftBedTemplateCodeNoV2(typeId);
  1233. var insertCode = new u_configure_code()
  1234. {
  1235. typeid = typeId,
  1236. pzid = nextPzid,
  1237. pzcode = pzcode,
  1238. name = codeName,
  1239. inputtype = 0,
  1240. configtype = 0,
  1241. ifcross = 0,
  1242. ifcheck = 0,
  1243. ifuse = 1,
  1244. ifnum = 0,
  1245. decnum = 0,
  1246. ifpack = 0
  1247. };
  1248. DbSqlHelper.Insert(cmd, "u_configure_code", null, insertCode, "typeid,pzid,pzcode,inputtype,name,configtype,ifcross,ifcheck,ifuse,ifnum,decnum,ifpack");
  1249. return nextPzid;
  1250. }
  1251. private int EnsureSoftBedTemplateCodeMxV2(int pzid, int currentPrintId, string codeMxName)
  1252. {
  1253. if (string.IsNullOrWhiteSpace(codeMxName)) throw new LJCommonException("模板核价保存失败,配置项明细名称不能为空");
  1254. string pzcode = GetSoftBedTemplateCodeNoV2(pzid);
  1255. if (currentPrintId > 0)
  1256. {
  1257. bool isExists = false;
  1258. string dbName = string.Empty;
  1259. cmd.CommandText = @"SELECT TOP 1 namemx
  1260. FROM u_configure_codemx
  1261. WHERE pzid = @pzid AND printid = @printid";
  1262. cmd.Parameters.Clear();
  1263. cmd.Parameters.AddWithValue("@pzid", pzid);
  1264. cmd.Parameters.AddWithValue("@printid", currentPrintId);
  1265. using (var reader = cmd.ExecuteReader())
  1266. {
  1267. if (reader.Read())
  1268. {
  1269. isExists = true;
  1270. dbName = Convert.ToString(reader["namemx"]);
  1271. }
  1272. }
  1273. if (isExists)
  1274. {
  1275. if (!string.Equals((dbName ?? string.Empty).Trim(), codeMxName.Trim(), StringComparison.Ordinal))
  1276. {
  1277. cmd.CommandText = @"UPDATE u_configure_codemx
  1278. SET pzcodemx = @pzcodemx,
  1279. namemx = @namemx,
  1280. ifuse = @ifuse
  1281. WHERE pzid = @pzid
  1282. AND printid = @printid";
  1283. cmd.Parameters.Clear();
  1284. cmd.Parameters.AddWithValue("@pzcodemx", $"{pzcode}_{FormatCode(currentPrintId, 3)}");
  1285. cmd.Parameters.AddWithValue("@namemx", codeMxName);
  1286. cmd.Parameters.AddWithValue("@ifuse", 1);
  1287. cmd.Parameters.AddWithValue("@pzid", pzid);
  1288. cmd.Parameters.AddWithValue("@printid", currentPrintId);
  1289. cmd.ExecuteNonQuery();
  1290. }
  1291. return currentPrintId;
  1292. }
  1293. }
  1294. cmd.CommandText = @"SELECT TOP 1 printid
  1295. FROM u_configure_codemx
  1296. WHERE pzid = @pzid
  1297. AND LTRIM(RTRIM(namemx)) = LTRIM(RTRIM(@namemx))";
  1298. cmd.Parameters.Clear();
  1299. cmd.Parameters.AddWithValue("@pzid", pzid);
  1300. cmd.Parameters.AddWithValue("@namemx", codeMxName);
  1301. object result = cmd.ExecuteScalar();
  1302. if (result != null && result != DBNull.Value)
  1303. {
  1304. return Convert.ToInt32(result);
  1305. }
  1306. int nextPrintId = GetNextSoftBedTemplateCodeMxIdV2(pzid);
  1307. byte defaultFlag = HasSoftBedTemplateDefaultCodeMxV2(pzid) ? (byte)0 : (byte)1;
  1308. var insertMx = new u_configure_codemx()
  1309. {
  1310. pzid = pzid,
  1311. printid = nextPrintId,
  1312. pzcodemx = $"{pzcode}_{FormatCode(nextPrintId, 3)}",
  1313. namemx = codeMxName,
  1314. ifdft = defaultFlag,
  1315. ifuse = 1
  1316. };
  1317. DbSqlHelper.Insert(cmd, "u_configure_codemx", null, insertMx, "pzid,printid,pzcodemx,namemx,ifdft,ifuse");
  1318. return nextPrintId;
  1319. }
  1320. private void ReplaceSoftBedTemplateBomV2(int pzid, int printid, List<u_softbed_mx> mxList)
  1321. {
  1322. var dbBomMap = new Dictionary<int, u_configure_codemxbom>();
  1323. cmd.CommandText = @"SELECT pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,sonloss,sonloss_formula,
  1324. sondecloss,sondecloss_formula,default_length,default_width,default_qty
  1325. FROM u_configure_codemxbom
  1326. WHERE pzid = @pzid AND printid = @printid
  1327. ORDER BY pid";
  1328. cmd.Parameters.Clear();
  1329. cmd.Parameters.AddWithValue("@pzid", pzid);
  1330. cmd.Parameters.AddWithValue("@printid", printid);
  1331. using (var reader = cmd.ExecuteReader())
  1332. {
  1333. while (reader.Read())
  1334. {
  1335. int mtrlid = Convert.ToInt32(reader["mtrlid"]);
  1336. if (dbBomMap.ContainsKey(mtrlid)) continue;
  1337. dbBomMap.Add(mtrlid, new u_configure_codemxbom()
  1338. {
  1339. pzid = Convert.ToInt32(reader["pzid"]),
  1340. printid = Convert.ToInt32(reader["printid"]),
  1341. pid = Convert.ToInt32(reader["pid"]),
  1342. mtrlid = mtrlid,
  1343. sonscale = Convert.ToDecimal(reader["sonscale"]),
  1344. sonscale_formula = Convert.ToString(reader["sonscale_formula"]),
  1345. mng_cost_rate = Convert.ToDecimal(reader["mng_cost_rate"]),
  1346. profit_rate = Convert.ToDecimal(reader["profit_rate"]),
  1347. realqty = Convert.ToDecimal(reader["realqty"]),
  1348. sonloss = Convert.ToDecimal(reader["sonloss"]),
  1349. sonloss_formula = Convert.ToString(reader["sonloss_formula"]),
  1350. sondecloss = Convert.ToDecimal(reader["sondecloss"]),
  1351. sondecloss_formula = Convert.ToString(reader["sondecloss_formula"]),
  1352. default_length = Convert.ToDecimal(reader["default_length"]),
  1353. default_width = Convert.ToDecimal(reader["default_width"]),
  1354. default_qty = Convert.ToDecimal(reader["default_qty"])
  1355. });
  1356. }
  1357. }
  1358. int nextPid = GetNextSoftBedTemplateBomPidV2(pzid, printid);
  1359. foreach (var mx in mxList.OrderBy(t => t.printid))
  1360. {
  1361. if (dbBomMap.TryGetValue(mx.mtrlid, out var dbBom))
  1362. {
  1363. cmd.CommandText = @"UPDATE u_configure_codemxbom
  1364. SET sonscale = @sonscale,
  1365. sonscale_formula = @sonscale_formula,
  1366. sonloss = @sonloss,
  1367. default_length = @default_length,
  1368. default_width = @default_width,
  1369. default_qty = @default_qty
  1370. WHERE pzid = @pzid
  1371. AND printid = @printid
  1372. AND pid = @pid";
  1373. cmd.Parameters.Clear();
  1374. cmd.Parameters.AddWithValue("@sonscale", mx.useqty);
  1375. cmd.Parameters.AddWithValue("@sonscale_formula", mx.use_formula ?? string.Empty);
  1376. cmd.Parameters.AddWithValue("@sonloss", mx.loss_rate);
  1377. cmd.Parameters.AddWithValue("@default_length", mx.cutting_length);
  1378. cmd.Parameters.AddWithValue("@default_width", mx.cutting_width);
  1379. cmd.Parameters.AddWithValue("@default_qty", mx.cutting_qty);
  1380. cmd.Parameters.AddWithValue("@pzid", pzid);
  1381. cmd.Parameters.AddWithValue("@printid", printid);
  1382. cmd.Parameters.AddWithValue("@pid", dbBom.pid);
  1383. cmd.ExecuteNonQuery();
  1384. continue;
  1385. }
  1386. var bom = new u_configure_codemxbom()
  1387. {
  1388. pzid = pzid,
  1389. printid = printid,
  1390. pid = nextPid++,
  1391. mtrlid = mx.mtrlid,
  1392. sonscale = mx.useqty,
  1393. sonscale_formula = mx.use_formula,
  1394. mng_cost_rate = 0,
  1395. profit_rate = 0,
  1396. realqty = 0,
  1397. sonloss = mx.loss_rate,
  1398. sonloss_formula = string.Empty,
  1399. sondecloss = 0,
  1400. sondecloss_formula = string.Empty,
  1401. price = mx.price,
  1402. price_formula = mx.price_formula,
  1403. default_length = mx.cutting_length,
  1404. default_width = mx.cutting_width,
  1405. default_qty = mx.cutting_qty
  1406. };
  1407. DbSqlHelper.Insert(cmd, "u_configure_codemxbom", null, bom, "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");
  1408. }
  1409. }
  1410. private void BackWriteSoftBedTemplateMxIdsV2(u_softbed softbed)
  1411. {
  1412. if (softbed.softbed_id <= 0 || softbed.mxList == null || softbed.mxList.Count <= 0) return;
  1413. foreach (var mx in softbed.mxList.Where(t => (t.has_type == 1 || t.has_type == 2 || t.has_type == 4) && t.if_no_config != 1))
  1414. {
  1415. cmd.CommandText = @"UPDATE u_softbed_mx
  1416. SET pzid = @pzid,
  1417. pzmxid = @pzmxid,
  1418. pzname = @pzname
  1419. WHERE softbed_id = @softbed_id
  1420. AND printid = @printid";
  1421. cmd.Parameters.Clear();
  1422. cmd.Parameters.AddWithValue("@pzid", mx.pzid);
  1423. cmd.Parameters.AddWithValue("@pzmxid", mx.pzmxid);
  1424. cmd.Parameters.AddWithValue("@pzname", mx.pzname ?? string.Empty);
  1425. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  1426. cmd.Parameters.AddWithValue("@printid", mx.printid);
  1427. cmd.ExecuteNonQuery();
  1428. }
  1429. }
  1430. private string GetNextSoftBedTemplateCodeNoV2(int typeId)
  1431. {
  1432. int maxNum = 0;
  1433. cmd.CommandText = @"SELECT pzcode FROM u_configure_code WHERE typeid = @typeid";
  1434. cmd.Parameters.Clear();
  1435. cmd.Parameters.AddWithValue("@typeid", typeId);
  1436. using (var reader = cmd.ExecuteReader())
  1437. {
  1438. while (reader.Read())
  1439. {
  1440. string pzcode = Convert.ToString(reader["pzcode"]).Trim();
  1441. if (int.TryParse(pzcode, out int currentNum) && currentNum > maxNum)
  1442. {
  1443. maxNum = currentNum;
  1444. }
  1445. }
  1446. }
  1447. return FormatCode(maxNum + 1, 4);
  1448. }
  1449. private string GetSoftBedTemplateCodeNoV2(int pzid)
  1450. {
  1451. cmd.CommandText = @"SELECT TOP 1 pzcode FROM u_configure_code WHERE pzid = @pzid";
  1452. cmd.Parameters.Clear();
  1453. cmd.Parameters.AddWithValue("@pzid", pzid);
  1454. object result = cmd.ExecuteScalar();
  1455. return result == null || result == DBNull.Value ? string.Empty : Convert.ToString(result).Trim();
  1456. }
  1457. private int GetNextSoftBedTemplateCodeMxIdV2(int pzid)
  1458. {
  1459. cmd.CommandText = @"SELECT ISNULL(MAX(printid),0) FROM u_configure_codemx WHERE pzid = @pzid";
  1460. cmd.Parameters.Clear();
  1461. cmd.Parameters.AddWithValue("@pzid", pzid);
  1462. object result = cmd.ExecuteScalar();
  1463. return Convert.ToInt32(result) + 1;
  1464. }
  1465. private bool HasSoftBedTemplateDefaultCodeMxV2(int pzid)
  1466. {
  1467. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND ifdft = 1";
  1468. cmd.Parameters.Clear();
  1469. cmd.Parameters.AddWithValue("@pzid", pzid);
  1470. return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
  1471. }
  1472. private int GetNextSoftBedTemplateBomPidV2(int pzid, int printid)
  1473. {
  1474. cmd.CommandText = @"SELECT ISNULL(MAX(pid),0) FROM u_configure_codemxbom WHERE pzid = @pzid AND printid = @printid";
  1475. cmd.Parameters.Clear();
  1476. cmd.Parameters.AddWithValue("@pzid", pzid);
  1477. cmd.Parameters.AddWithValue("@printid", printid);
  1478. return Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  1479. }
  1480. #region 通用公式
  1481. private CalculateFormula formula = new CalculateFormula();
  1482. private string BillKeyWord = "u_softbed";
  1483. #endregion
  1484. }
  1485. }