SoftBedHelper.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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. softbed_copy.softbed_code = string.Empty;
  758. SaveSoftBed(softbed_copy);
  759. //if(softbed_copy.is_template == 1 && softbed.copy_id > 0)
  760. //{
  761. // // 复制配置到新配置
  762. // var _softbed = GetSoftBed(softbed.copy_id, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  763. // if(_softbed.has_headboard == 1 && softbed_copy.has_headboard == 1)
  764. // {
  765. // CopyConfigureData($"{_softbed.softbed_code}|床头",$"{softbed_copy.softbed_code}|床头");
  766. // }
  767. // if (_softbed.has_nightstand == 1 && softbed_copy.has_nightstand == 1)
  768. // {
  769. // CopyConfigureData($"{_softbed.softbed_code}|床头柜", $"{softbed_copy.softbed_code}|床头柜");
  770. // }
  771. // if (_softbed.has_bedframe == 1 && softbed_copy.has_bedframe == 1)
  772. // {
  773. // CopyConfigureData($"{_softbed.softbed_code}|床架", $"{softbed_copy.softbed_code}|床架");
  774. // }
  775. //}
  776. return softbed_copy;
  777. }
  778. /// <summary>
  779. /// 删除软床报价
  780. /// </summary>
  781. /// <param name="billid"></param>
  782. /// <exception cref="LJCommonException"></exception>
  783. public void DeleteSoftBed(int billid)
  784. {
  785. var softbed = GetSoftBed(billid, "softbed_code,flag");
  786. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  787. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  788. DbSqlHelper.Delete(cmd, softbed);
  789. }
  790. /// <summary>
  791. /// 审核软床报价
  792. /// </summary>
  793. /// <param name="billid"></param>
  794. /// <exception cref="LJCommonException"></exception>
  795. public void AuditSoftBed(int billid)
  796. {
  797. var softbed = GetSoftBed(billid, "softbed_code,flag");
  798. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  799. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  800. softbed.flag = 1;
  801. softbed.audit_date = context.opdate;
  802. softbed.audit_emp = context.tokendata.username;
  803. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  804. }
  805. /// <summary>
  806. /// 撤审软床报价
  807. /// </summary>
  808. /// <param name="billid"></param>
  809. /// <exception cref="LJCommonException"></exception>
  810. public void CAuditSoftBed(int billid)
  811. {
  812. var softbed = GetSoftBed(billid, "softbed_code,flag");
  813. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  814. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  815. softbed.flag = 0;
  816. softbed.audit_date = null;
  817. softbed.audit_emp = string.Empty;
  818. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  819. }
  820. public void CalCulateFormula(u_softbed softbed)
  821. {
  822. InitSoftBed(softbed);
  823. InitReplaceMents(softbed);
  824. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  825. foreach (var mx in softbed.mxList)
  826. {
  827. InitMxReplaceMents(softbed, mx);
  828. formula.CalculateAll();
  829. // 转换后的文本公式
  830. mx.use_formula_str = formula.GetFormulaItem("【实际用量】").formula_transform;
  831. mx.price_formula_str = formula.GetFormulaItem("【成本单价】").formula_transform;
  832. // 成本单价
  833. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  834. // 实际用量
  835. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  836. // 成本金额
  837. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  838. // 总材料成本
  839. total_mtrl_cost += mx.cost_amt;
  840. // 总人力成本
  841. total_hr_cost += 0;
  842. }
  843. if(softbed.mxList != null && softbed.mxList.Count > 0)
  844. {
  845. softbed.total_mtrl_cost = total_mtrl_cost;
  846. softbed.total_hr_cost = total_hr_cost;
  847. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  848. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  849. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  850. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  851. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  852. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  853. }
  854. //formula.CalculateAll();
  855. }
  856. private void InitSoftBed(u_softbed softbed)
  857. {
  858. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  859. //
  860. softbed.dept_profitrate = dept.profitrate;
  861. //
  862. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  863. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  864. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  865. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  866. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  867. // 检查佣金是否小于1
  868. if (softbed.commission < 1)
  869. {
  870. throw new LJCommonException("佣金点数不能小于1!");
  871. }
  872. // 检查税率是否小于1
  873. if (softbed.taxrate < 1)
  874. {
  875. throw new LJCommonException("税率不能小于1!");
  876. }
  877. // 检查额外点数是否小于1
  878. if (softbed.other_rate < 1)
  879. {
  880. throw new LJCommonException("税率不能小于1!");
  881. }
  882. // 检查部门利润率是否为0
  883. if (softbed.dept_profitrate == 0)
  884. {
  885. throw new LJCommonException("部门利润率不能为0!");
  886. }
  887. if(softbed.is_template == 1)
  888. {
  889. // 使用模板报价
  890. softbed.template_id = 0;
  891. softbed.template_code = string.Empty;
  892. softbed.template_name = string.Empty;
  893. }
  894. }
  895. private void InitReplaceMents(u_softbed softbed)
  896. {
  897. // 常量变量
  898. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  899. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  900. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  901. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  902. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  903. formula.AddFormulaItem("【税率】", softbed.taxrate);
  904. formula.AddFormulaItem("【工厂利润率】", 1);
  905. formula.AddFormulaItem("【FOB】", 0);
  906. formula.AddFormulaItem("【外销加点】", 0);
  907. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  908. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  909. // 公式变量
  910. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  911. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  912. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  913. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  914. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  915. }
  916. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  917. {
  918. // 默认公式变量
  919. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  920. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  921. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  922. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  923. // 常量变量
  924. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  925. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  926. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  927. formula.AddFormulaItem("【用料量】", mx.useqty);
  928. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  929. formula.AddFormulaItem("【材料单价】", mx.price);
  930. if(mx.formulaid > 0)
  931. {
  932. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  933. ,u_softbed_formula.formulaname
  934. ,u_softbed_formula.use_formula
  935. ,u_softbed_formula.price_formula
  936. FROM u_softbed_formula
  937. WHERE formulaid = @formulaid";
  938. cmd.Parameters.Clear();
  939. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  940. using(var reader = cmd.ExecuteReader())
  941. {
  942. if(reader.Read())
  943. {
  944. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  945. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  946. }
  947. }
  948. }
  949. }
  950. private List<string> GetConfigureTypeNameFromBillId(int billid)
  951. {
  952. var _softbed = GetSoftBed(billid, "softbed_code,has_headboard,has_nightstand,has_bedframe");
  953. var nameList = new List<string>();
  954. string prefix = $"{_softbed.softbed_code}|";
  955. if (_softbed.has_headboard == 1)
  956. {
  957. nameList.Add($"{_softbed.softbed_code}|床头");
  958. }
  959. if (_softbed.has_nightstand == 1)
  960. {
  961. nameList.Add($"{_softbed.softbed_code}|床头柜");
  962. }
  963. if (_softbed.has_bedframe == 1)
  964. {
  965. nameList.Add($"{_softbed.softbed_code}|床架");
  966. }
  967. return nameList;
  968. }
  969. private void CopyConfigureData(string SourceName, string TargetName)
  970. {
  971. var commonHelper = GetHelper<CommonHelper>(cmd, context);
  972. var basicInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  973. // 入参非空验证
  974. if (string.IsNullOrWhiteSpace(SourceName))
  975. {
  976. throw new LJCommonException("源配置名称不能为空");
  977. }
  978. if (string.IsNullOrWhiteSpace(TargetName))
  979. {
  980. throw new LJCommonException("目标配置名称不能为空");
  981. }
  982. int SourceId = 0, TargetId = 0;
  983. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@SourceName))";
  984. cmd.Parameters.Clear();
  985. cmd.Parameters.AddWithValue("@SourceName", SourceName);
  986. using(var reader = cmd.ExecuteReader())
  987. {
  988. if(reader.Read())
  989. {
  990. SourceId = Convert.ToInt32(reader["contfigtypeid"]);
  991. }
  992. }
  993. cmd.CommandText = @"SELECT contfigtypeid FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@TargetName))";
  994. cmd.Parameters.Clear();
  995. cmd.Parameters.AddWithValue("@TargetName", TargetName);
  996. using (var reader = cmd.ExecuteReader())
  997. {
  998. if (reader.Read())
  999. {
  1000. TargetId = Convert.ToInt32(reader["contfigtypeid"]);
  1001. }
  1002. }
  1003. if (SourceId == 0) throw new LJCommonException($"源配置名称「{SourceName.Trim()}」未找到对应的结果");
  1004. if (TargetId == 0) throw new LJCommonException($"目标配置名称「{TargetName.Trim()}」未找到对应的结果");
  1005. // 部件选配项
  1006. var codeList = commonHelper.ExecuteDynamicSelectToList<u_configure_code>("web_configure_codelist",new Newtonsoft.Json.Linq.JObject() { { "typeid",SourceId} },0,0);
  1007. var code_copyList = new List<u_configure_code>();
  1008. var codeIdMap = new Dictionary<int, int>();
  1009. foreach(var code in codeList)
  1010. {
  1011. var code_copy = ObjectHelper.DeepCopy(code);
  1012. code_copy.pzid = BllHelper.GetID(cmd, "u_configure_code");
  1013. code_copy.typeid = TargetId;
  1014. code_copyList.Add(code_copy);
  1015. if (codeIdMap.ContainsKey(code.pzid.Value)) continue;
  1016. codeIdMap.Add(code.pzid.Value, code_copy.pzid.Value);
  1017. //basicInfoHelper.SaveConfigureCode(code_copy);
  1018. }
  1019. 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");
  1020. // 部件选配值
  1021. var codeMxList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemx>("web_configure_codemxlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  1022. var codeMx_copyList = new List<u_configure_codemx>();
  1023. foreach (var codeMx in codeMxList)
  1024. {
  1025. if (!codeIdMap.TryGetValue(codeMx.pzid.Value, out int newId)) continue;
  1026. var codeMx_copy = ObjectHelper.DeepCopy(codeMx);
  1027. codeMx_copy.pzid = newId;
  1028. codeMx_copyList.Add(codeMx_copy);
  1029. }
  1030. 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");
  1031. // 部件选配物料清单
  1032. var codeMxBomList = commonHelper.ExecuteDynamicSelectToList<u_configure_codemxbom>("web_configure_codemxbomlist", new Newtonsoft.Json.Linq.JObject() { { "typeid", SourceId } }, 0, 0);
  1033. var codeMxBom_copyList = new List<u_configure_codemxbom>();
  1034. foreach (var codeMxBom in codeMxBomList)
  1035. {
  1036. if (!codeIdMap.TryGetValue(codeMxBom.pzid, out int newId)) continue;
  1037. var codeMxBom_copy = ObjectHelper.DeepCopy(codeMxBom);
  1038. codeMxBom_copy.pzid = newId;
  1039. codeMxBom_copyList.Add(codeMxBom_copy);
  1040. }
  1041. 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");
  1042. }
  1043. /// <summary>
  1044. /// 新版模板核价资料保存。
  1045. /// 仅处理模板报价,将报价明细同步到核价配置资料,并将生成的 pzid/pzmxid 回写到报价明细。
  1046. /// </summary>
  1047. /// <param name="softbed"></param>
  1048. public void SaveSoftBedTemplateV2(u_softbed softbed)
  1049. {
  1050. if (softbed == null) throw new LJCommonException("模板核价保存失败,数据异常!");
  1051. if (softbed.is_template == 0) return;
  1052. if (string.IsNullOrWhiteSpace(softbed.softbed_code)) throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  1053. if (softbed.mxList == null || softbed.mxList.Count <= 0) return;
  1054. var typeNameMap = GetSoftBedTemplateTypeNameMapV2(softbed);
  1055. foreach (var item in typeNameMap)
  1056. {
  1057. var typeMxList = softbed.mxList
  1058. .Where(t => t.has_type == item.Key)
  1059. .OrderBy(t => t.printid)
  1060. .ToList();
  1061. if (typeMxList.Count <= 0) continue;
  1062. ValidateSoftBedTemplateMxListV2(item.Value, typeMxList);
  1063. int typeId = EnsureSoftBedTemplateTypeV2(item.Value);
  1064. var codeMxGroups = typeMxList
  1065. .GroupBy(t => new
  1066. {
  1067. PzName = (t.pzname ?? string.Empty).Trim(),
  1068. PzNameMx = (t.pznamemx ?? string.Empty).Trim()
  1069. })
  1070. .ToList();
  1071. foreach (var group in codeMxGroups)
  1072. {
  1073. var currentMxList = group.OrderBy(t => t.printid).ToList();
  1074. var firstMx = currentMxList.First();
  1075. int pzid = EnsureSoftBedTemplateCodeV2(typeId, firstMx.pzid, group.Key.PzName);
  1076. int pzmxid = EnsureSoftBedTemplateCodeMxV2(pzid, firstMx.pzmxid, group.Key.PzNameMx);
  1077. foreach (var mx in currentMxList)
  1078. {
  1079. mx.pzid = pzid;
  1080. mx.pzmxid = pzmxid;
  1081. mx.pzname = group.Key.PzName;
  1082. mx.pznamemx = group.Key.PzNameMx;
  1083. }
  1084. ReplaceSoftBedTemplateBomV2(pzid, pzmxid, currentMxList);
  1085. }
  1086. }
  1087. BackWriteSoftBedTemplateMxIdsV2(softbed);
  1088. }
  1089. private Dictionary<byte, string> GetSoftBedTemplateTypeNameMapV2(u_softbed softbed)
  1090. {
  1091. var dict = new Dictionary<byte, string>();
  1092. string prefix = $"{softbed.softbed_code}|";
  1093. if (softbed.has_headboard == 1) dict[1] = $"{prefix}床头";
  1094. if (softbed.has_nightstand == 1) dict[2] = $"{prefix}床头柜";
  1095. if (softbed.has_bedframe == 1) dict[4] = $"{prefix}床架";
  1096. return dict;
  1097. }
  1098. private void ValidateSoftBedTemplateMxListV2(string typeName, List<u_softbed_mx> mxList)
  1099. {
  1100. foreach (var mx in mxList)
  1101. {
  1102. if (string.IsNullOrWhiteSpace(mx.pzname))
  1103. {
  1104. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 未设置部件选配项名称(pzname)");
  1105. }
  1106. if (string.IsNullOrWhiteSpace(mx.pznamemx))
  1107. {
  1108. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 未设置部件选配明细名称(pznamemx)");
  1109. }
  1110. if (mx.mtrlid <= 0)
  1111. {
  1112. throw new LJCommonException($"模板核价保存失败,类型:{typeName},明细行:{mx.printid} 物料ID无效");
  1113. }
  1114. }
  1115. }
  1116. private int EnsureSoftBedTemplateTypeV2(string typeName)
  1117. {
  1118. cmd.CommandText = @"SELECT TOP 1 contfigtypeid
  1119. FROM u_configure_type
  1120. WHERE LTRIM(RTRIM(contfigtypename)) = LTRIM(RTRIM(@contfigtypename))";
  1121. cmd.Parameters.Clear();
  1122. cmd.Parameters.AddWithValue("@contfigtypename", typeName);
  1123. object result = cmd.ExecuteScalar();
  1124. if (result != null && result != DBNull.Value)
  1125. {
  1126. return Convert.ToInt32(result);
  1127. }
  1128. var configure = new u_configure_type()
  1129. {
  1130. contfigtypeid = BllHelper.GetID(cmd, "u_configure_type"),
  1131. contfigtypename = typeName,
  1132. contfigtype = 0,
  1133. usechflag = 1,
  1134. flag = 0
  1135. };
  1136. DbSqlHelper.Insert(cmd, "u_configure_type", null, configure, "contfigtypeid,contfigtype,contfigtypename,usechflag,flag");
  1137. return configure.contfigtypeid.Value;
  1138. }
  1139. private int EnsureSoftBedTemplateCodeV2(int typeId, int currentPzid, string codeName)
  1140. {
  1141. if (string.IsNullOrWhiteSpace(codeName)) throw new LJCommonException("模板核价保存失败,配置项名称不能为空");
  1142. string pzcode = string.Empty;
  1143. if (currentPzid > 0)
  1144. {
  1145. bool isExists = false;
  1146. int dbTypeId = 0;
  1147. string dbName = string.Empty;
  1148. cmd.CommandText = @"SELECT TOP 1 typeid,pzcode,name
  1149. FROM u_configure_code
  1150. WHERE pzid = @pzid";
  1151. cmd.Parameters.Clear();
  1152. cmd.Parameters.AddWithValue("@pzid", currentPzid);
  1153. using (var reader = cmd.ExecuteReader())
  1154. {
  1155. if (reader.Read())
  1156. {
  1157. isExists = true;
  1158. pzcode = Convert.ToString(reader["pzcode"]);
  1159. dbTypeId = Convert.ToInt32(reader["typeid"]);
  1160. dbName = Convert.ToString(reader["name"]);
  1161. }
  1162. }
  1163. if (isExists)
  1164. {
  1165. if (dbTypeId != typeId || !string.Equals((dbName ?? string.Empty).Trim(), codeName.Trim(), StringComparison.Ordinal))
  1166. {
  1167. cmd.CommandText = @"UPDATE u_configure_code
  1168. SET typeid = @typeid,
  1169. pzcode = @pzcode,
  1170. name = @name,
  1171. inputtype = @inputtype,
  1172. configtype = @configtype,
  1173. ifcross = @ifcross,
  1174. ifcheck = @ifcheck,
  1175. ifuse = @ifuse,
  1176. ifnum = @ifnum,
  1177. decnum = @decnum,
  1178. ifpack = @ifpack
  1179. WHERE pzid = @pzid";
  1180. cmd.Parameters.Clear();
  1181. cmd.Parameters.AddWithValue("@typeid", typeId);
  1182. cmd.Parameters.AddWithValue("@pzcode", pzcode ?? string.Empty);
  1183. cmd.Parameters.AddWithValue("@name", codeName);
  1184. cmd.Parameters.AddWithValue("@inputtype", 0);
  1185. cmd.Parameters.AddWithValue("@configtype", 0);
  1186. cmd.Parameters.AddWithValue("@ifcross", 0);
  1187. cmd.Parameters.AddWithValue("@ifcheck", 0);
  1188. cmd.Parameters.AddWithValue("@ifuse", 1);
  1189. cmd.Parameters.AddWithValue("@ifnum", 0);
  1190. cmd.Parameters.AddWithValue("@decnum", 0);
  1191. cmd.Parameters.AddWithValue("@ifpack", 0);
  1192. cmd.Parameters.AddWithValue("@pzid", currentPzid);
  1193. cmd.ExecuteNonQuery();
  1194. }
  1195. return currentPzid;
  1196. }
  1197. }
  1198. cmd.CommandText = @"SELECT TOP 1 pzid
  1199. FROM u_configure_code
  1200. WHERE typeid = @typeid
  1201. AND LTRIM(RTRIM(name)) = LTRIM(RTRIM(@name))";
  1202. cmd.Parameters.Clear();
  1203. cmd.Parameters.AddWithValue("@typeid", typeId);
  1204. cmd.Parameters.AddWithValue("@name", codeName);
  1205. object result = cmd.ExecuteScalar();
  1206. if (result != null && result != DBNull.Value)
  1207. {
  1208. return Convert.ToInt32(result);
  1209. }
  1210. int nextPzid = BllHelper.GetID(cmd, "u_configure_code");
  1211. pzcode = GetNextSoftBedTemplateCodeNoV2(typeId);
  1212. var insertCode = new u_configure_code()
  1213. {
  1214. typeid = typeId,
  1215. pzid = nextPzid,
  1216. pzcode = pzcode,
  1217. name = codeName,
  1218. inputtype = 0,
  1219. configtype = 0,
  1220. ifcross = 0,
  1221. ifcheck = 0,
  1222. ifuse = 1,
  1223. ifnum = 0,
  1224. decnum = 0,
  1225. ifpack = 0
  1226. };
  1227. DbSqlHelper.Insert(cmd, "u_configure_code", null, insertCode, "typeid,pzid,pzcode,inputtype,name,configtype,ifcross,ifcheck,ifuse,ifnum,decnum,ifpack");
  1228. return nextPzid;
  1229. }
  1230. private int EnsureSoftBedTemplateCodeMxV2(int pzid, int currentPrintId, string codeMxName)
  1231. {
  1232. if (string.IsNullOrWhiteSpace(codeMxName)) throw new LJCommonException("模板核价保存失败,配置项明细名称不能为空");
  1233. string pzcode = GetSoftBedTemplateCodeNoV2(pzid);
  1234. if (currentPrintId > 0)
  1235. {
  1236. bool isExists = false;
  1237. string dbName = string.Empty;
  1238. cmd.CommandText = @"SELECT TOP 1 namemx
  1239. FROM u_configure_codemx
  1240. WHERE pzid = @pzid AND printid = @printid";
  1241. cmd.Parameters.Clear();
  1242. cmd.Parameters.AddWithValue("@pzid", pzid);
  1243. cmd.Parameters.AddWithValue("@printid", currentPrintId);
  1244. using (var reader = cmd.ExecuteReader())
  1245. {
  1246. if (reader.Read())
  1247. {
  1248. isExists = true;
  1249. dbName = Convert.ToString(reader["namemx"]);
  1250. }
  1251. }
  1252. if (isExists)
  1253. {
  1254. if (!string.Equals((dbName ?? string.Empty).Trim(), codeMxName.Trim(), StringComparison.Ordinal))
  1255. {
  1256. cmd.CommandText = @"UPDATE u_configure_codemx
  1257. SET pzcodemx = @pzcodemx,
  1258. namemx = @namemx,
  1259. ifuse = @ifuse
  1260. WHERE pzid = @pzid
  1261. AND printid = @printid";
  1262. cmd.Parameters.Clear();
  1263. cmd.Parameters.AddWithValue("@pzcodemx", $"{pzcode}_{FormatCode(currentPrintId, 3)}");
  1264. cmd.Parameters.AddWithValue("@namemx", codeMxName);
  1265. cmd.Parameters.AddWithValue("@ifuse", 1);
  1266. cmd.Parameters.AddWithValue("@pzid", pzid);
  1267. cmd.Parameters.AddWithValue("@printid", currentPrintId);
  1268. cmd.ExecuteNonQuery();
  1269. }
  1270. return currentPrintId;
  1271. }
  1272. }
  1273. cmd.CommandText = @"SELECT TOP 1 printid
  1274. FROM u_configure_codemx
  1275. WHERE pzid = @pzid
  1276. AND LTRIM(RTRIM(namemx)) = LTRIM(RTRIM(@namemx))";
  1277. cmd.Parameters.Clear();
  1278. cmd.Parameters.AddWithValue("@pzid", pzid);
  1279. cmd.Parameters.AddWithValue("@namemx", codeMxName);
  1280. object result = cmd.ExecuteScalar();
  1281. if (result != null && result != DBNull.Value)
  1282. {
  1283. return Convert.ToInt32(result);
  1284. }
  1285. int nextPrintId = GetNextSoftBedTemplateCodeMxIdV2(pzid);
  1286. byte defaultFlag = HasSoftBedTemplateDefaultCodeMxV2(pzid) ? (byte)0 : (byte)1;
  1287. var insertMx = new u_configure_codemx()
  1288. {
  1289. pzid = pzid,
  1290. printid = nextPrintId,
  1291. pzcodemx = $"{pzcode}_{FormatCode(nextPrintId, 3)}",
  1292. namemx = codeMxName,
  1293. ifdft = defaultFlag,
  1294. ifuse = 1
  1295. };
  1296. DbSqlHelper.Insert(cmd, "u_configure_codemx", null, insertMx, "pzid,printid,pzcodemx,namemx,ifdft,ifuse");
  1297. return nextPrintId;
  1298. }
  1299. private void ReplaceSoftBedTemplateBomV2(int pzid, int printid, List<u_softbed_mx> mxList)
  1300. {
  1301. var dbBomMap = new Dictionary<int, u_configure_codemxbom>();
  1302. cmd.CommandText = @"SELECT pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,sonloss,sonloss_formula,
  1303. sondecloss,sondecloss_formula,default_length,default_width,default_qty
  1304. FROM u_configure_codemxbom
  1305. WHERE pzid = @pzid AND printid = @printid
  1306. ORDER BY pid";
  1307. cmd.Parameters.Clear();
  1308. cmd.Parameters.AddWithValue("@pzid", pzid);
  1309. cmd.Parameters.AddWithValue("@printid", printid);
  1310. using (var reader = cmd.ExecuteReader())
  1311. {
  1312. while (reader.Read())
  1313. {
  1314. int mtrlid = Convert.ToInt32(reader["mtrlid"]);
  1315. if (dbBomMap.ContainsKey(mtrlid)) continue;
  1316. dbBomMap.Add(mtrlid, new u_configure_codemxbom()
  1317. {
  1318. pzid = Convert.ToInt32(reader["pzid"]),
  1319. printid = Convert.ToInt32(reader["printid"]),
  1320. pid = Convert.ToInt32(reader["pid"]),
  1321. mtrlid = mtrlid,
  1322. sonscale = Convert.ToDecimal(reader["sonscale"]),
  1323. sonscale_formula = Convert.ToString(reader["sonscale_formula"]),
  1324. mng_cost_rate = Convert.ToDecimal(reader["mng_cost_rate"]),
  1325. profit_rate = Convert.ToDecimal(reader["profit_rate"]),
  1326. realqty = Convert.ToDecimal(reader["realqty"]),
  1327. sonloss = Convert.ToDecimal(reader["sonloss"]),
  1328. sonloss_formula = Convert.ToString(reader["sonloss_formula"]),
  1329. sondecloss = Convert.ToDecimal(reader["sondecloss"]),
  1330. sondecloss_formula = Convert.ToString(reader["sondecloss_formula"]),
  1331. default_length = Convert.ToDecimal(reader["default_length"]),
  1332. default_width = Convert.ToDecimal(reader["default_width"]),
  1333. default_qty = Convert.ToDecimal(reader["default_qty"])
  1334. });
  1335. }
  1336. }
  1337. int nextPid = GetNextSoftBedTemplateBomPidV2(pzid, printid);
  1338. foreach (var mx in mxList.OrderBy(t => t.printid))
  1339. {
  1340. if (dbBomMap.TryGetValue(mx.mtrlid, out var dbBom))
  1341. {
  1342. cmd.CommandText = @"UPDATE u_configure_codemxbom
  1343. SET sonscale = @sonscale,
  1344. sonscale_formula = @sonscale_formula,
  1345. sonloss = @sonloss,
  1346. default_length = @default_length,
  1347. default_width = @default_width,
  1348. default_qty = @default_qty
  1349. WHERE pzid = @pzid
  1350. AND printid = @printid
  1351. AND pid = @pid";
  1352. cmd.Parameters.Clear();
  1353. cmd.Parameters.AddWithValue("@sonscale", mx.useqty);
  1354. cmd.Parameters.AddWithValue("@sonscale_formula", mx.use_formula ?? string.Empty);
  1355. cmd.Parameters.AddWithValue("@sonloss", mx.loss_rate);
  1356. cmd.Parameters.AddWithValue("@default_length", mx.cutting_length);
  1357. cmd.Parameters.AddWithValue("@default_width", mx.cutting_width);
  1358. cmd.Parameters.AddWithValue("@default_qty", mx.cutting_qty);
  1359. cmd.Parameters.AddWithValue("@pzid", pzid);
  1360. cmd.Parameters.AddWithValue("@printid", printid);
  1361. cmd.Parameters.AddWithValue("@pid", dbBom.pid);
  1362. cmd.ExecuteNonQuery();
  1363. continue;
  1364. }
  1365. var bom = new u_configure_codemxbom()
  1366. {
  1367. pzid = pzid,
  1368. printid = printid,
  1369. pid = nextPid++,
  1370. mtrlid = mx.mtrlid,
  1371. sonscale = mx.useqty,
  1372. sonscale_formula = mx.use_formula,
  1373. mng_cost_rate = 0,
  1374. profit_rate = 0,
  1375. realqty = 0,
  1376. sonloss = mx.loss_rate,
  1377. sonloss_formula = string.Empty,
  1378. sondecloss = 0,
  1379. sondecloss_formula = string.Empty,
  1380. price = mx.price,
  1381. price_formula = mx.price_formula,
  1382. default_length = mx.cutting_length,
  1383. default_width = mx.cutting_width,
  1384. default_qty = mx.cutting_qty
  1385. };
  1386. 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");
  1387. }
  1388. }
  1389. private void BackWriteSoftBedTemplateMxIdsV2(u_softbed softbed)
  1390. {
  1391. if (softbed.softbed_id <= 0 || softbed.mxList == null || softbed.mxList.Count <= 0) return;
  1392. foreach (var mx in softbed.mxList.Where(t => t.has_type == 1 || t.has_type == 2 || t.has_type == 4))
  1393. {
  1394. cmd.CommandText = @"UPDATE u_softbed_mx
  1395. SET pzid = @pzid,
  1396. pzmxid = @pzmxid,
  1397. pzname = @pzname
  1398. WHERE softbed_id = @softbed_id
  1399. AND printid = @printid";
  1400. cmd.Parameters.Clear();
  1401. cmd.Parameters.AddWithValue("@pzid", mx.pzid);
  1402. cmd.Parameters.AddWithValue("@pzmxid", mx.pzmxid);
  1403. cmd.Parameters.AddWithValue("@pzname", mx.pzname ?? string.Empty);
  1404. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  1405. cmd.Parameters.AddWithValue("@printid", mx.printid);
  1406. cmd.ExecuteNonQuery();
  1407. }
  1408. }
  1409. private string GetNextSoftBedTemplateCodeNoV2(int typeId)
  1410. {
  1411. int maxNum = 0;
  1412. cmd.CommandText = @"SELECT pzcode FROM u_configure_code WHERE typeid = @typeid";
  1413. cmd.Parameters.Clear();
  1414. cmd.Parameters.AddWithValue("@typeid", typeId);
  1415. using (var reader = cmd.ExecuteReader())
  1416. {
  1417. while (reader.Read())
  1418. {
  1419. string pzcode = Convert.ToString(reader["pzcode"]).Trim();
  1420. if (int.TryParse(pzcode, out int currentNum) && currentNum > maxNum)
  1421. {
  1422. maxNum = currentNum;
  1423. }
  1424. }
  1425. }
  1426. return FormatCode(maxNum + 1, 4);
  1427. }
  1428. private string GetSoftBedTemplateCodeNoV2(int pzid)
  1429. {
  1430. cmd.CommandText = @"SELECT TOP 1 pzcode FROM u_configure_code WHERE pzid = @pzid";
  1431. cmd.Parameters.Clear();
  1432. cmd.Parameters.AddWithValue("@pzid", pzid);
  1433. object result = cmd.ExecuteScalar();
  1434. return result == null || result == DBNull.Value ? string.Empty : Convert.ToString(result).Trim();
  1435. }
  1436. private int GetNextSoftBedTemplateCodeMxIdV2(int pzid)
  1437. {
  1438. cmd.CommandText = @"SELECT ISNULL(MAX(printid),0) FROM u_configure_codemx WHERE pzid = @pzid";
  1439. cmd.Parameters.Clear();
  1440. cmd.Parameters.AddWithValue("@pzid", pzid);
  1441. object result = cmd.ExecuteScalar();
  1442. return Convert.ToInt32(result) + 1;
  1443. }
  1444. private bool HasSoftBedTemplateDefaultCodeMxV2(int pzid)
  1445. {
  1446. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND ifdft = 1";
  1447. cmd.Parameters.Clear();
  1448. cmd.Parameters.AddWithValue("@pzid", pzid);
  1449. return Convert.ToInt32(cmd.ExecuteScalar()) > 0;
  1450. }
  1451. private int GetNextSoftBedTemplateBomPidV2(int pzid, int printid)
  1452. {
  1453. cmd.CommandText = @"SELECT ISNULL(MAX(pid),0) FROM u_configure_codemxbom WHERE pzid = @pzid AND printid = @printid";
  1454. cmd.Parameters.Clear();
  1455. cmd.Parameters.AddWithValue("@pzid", pzid);
  1456. cmd.Parameters.AddWithValue("@printid", printid);
  1457. return Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  1458. }
  1459. #region 通用公式
  1460. private CalculateFormula formula = new CalculateFormula();
  1461. private string BillKeyWord = "u_softbed";
  1462. #endregion
  1463. }
  1464. }