SoftBedHelper.cs 69 KB

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