SoftBedHelper.cs 70 KB

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