MattressHelper.cs 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. using CSUST.Data.Expr;
  2. using DirectService.Tools;
  3. using JLHHJSvr.BLL;
  4. using JLHHJSvr.Com.Model;
  5. using JLHHJSvr.LJException;
  6. using JLHHJSvr.LJFramework.Tools;
  7. using JLHHJSvr.Tools;
  8. using LJLib.DAL.SQL;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data.SqlClient;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. namespace JLHHJSvr.Helper
  17. {
  18. class MattressHelper : HelperBase
  19. {
  20. private Dictionary<string, object> replacements = new Dictionary<string, object>();
  21. /// <summary>
  22. /// 默认展示公式
  23. /// </summary>
  24. private Dictionary<string, string> formula_replacements = new Dictionary<string, string>();
  25. /// <summary>
  26. /// 隐藏公式
  27. /// </summary>
  28. private Dictionary<string, string> formula_replacements_hide = new Dictionary<string, string>();
  29. public List<replacement> Replacements
  30. {
  31. get
  32. {
  33. var res = new List<replacement>();
  34. foreach (var item in replacements)
  35. {
  36. res.Add(new replacement { label = item.Key, value = item.Value, type = 0 });
  37. }
  38. return res;
  39. }
  40. }
  41. public List<replacement> FormulaReplacements
  42. {
  43. get
  44. {
  45. var res = new List<replacement>();
  46. foreach (var item in formula_replacements)
  47. {
  48. res.Add(new replacement { label = item.Key, value = item.Value, type = 0 });
  49. }
  50. foreach (var item in formula_replacements_hide)
  51. {
  52. res.Add(new replacement { label = item.Key, value = item.Value, type = 1 });
  53. }
  54. return res;
  55. }
  56. }
  57. /// <summary>
  58. /// 公式计算
  59. /// </summary>
  60. /// <param name="mattress"></param>
  61. /// <param name="mxlist"></param>
  62. /// <param name="isCalBed">是否实时计算</param>
  63. public void CalCulateFormula(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist,Boolean isCalBed = true)
  64. {
  65. InitMattress(mattress);
  66. InitReplaceMents(mattress);
  67. // 实时计算价格
  68. if (isCalBed)
  69. {
  70. InitMattressMx(mattress, mxlist);
  71. CalCulateBedNet(mxlist);
  72. var diancengList = mxlist.Where(t => t.formulatype == 1).ToList();
  73. var mattersstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  74. DbSqlHelper.SelectOne(cmd, mattersstype, "formula_big_side,formula_small_side,formula_v_side");
  75. var thicknessDict = InitMxThicknessReplaceMents(mxlist);
  76. foreach (var mx in mxlist)
  77. {
  78. if (mx.mtrlid == 0 && mx.formulakind != 31) continue;
  79. if (string.IsNullOrEmpty(mx.formula) && string.IsNullOrEmpty(mx.useformula)) continue;
  80. InitMxReplaceMents(mx);
  81. // 顶替分组关键值:內布套、顶布裥棉
  82. if (thicknessDict.ContainsKey(mx.chastr))
  83. {
  84. foreach(var rp in thicknessDict[mx.chastr])
  85. {
  86. AddKeyValue(rp.Key, rp.Value);
  87. }
  88. }
  89. string expression = ConvertToEnglishSymbols(mx.formula), use_expression = ConvertToEnglishSymbols(mx.useformula);
  90. expression = InitMxSideReplaceMents(mattress, mx, diancengList, mattersstype, expression);
  91. use_expression = InitMxSideReplaceMents(mattress, mx, diancengList, mattersstype,use_expression);
  92. #region 替换布料幅宽
  93. //大侧
  94. if (new List<int>() { 2,42,52,62,72,82 }.Contains(mx.formulakind.Value))
  95. {
  96. InitMxClothWidthReplaceMents(mx, mxlist,2);
  97. }
  98. //小侧
  99. if (new List<int>() { 3, 43, 53, 63, 73, 83 }.Contains(mx.formulakind.Value))
  100. {
  101. InitMxClothWidthReplaceMents(mx, mxlist,3);
  102. }
  103. //大侧
  104. if (new List<int>() { 4, 44, 54, 64, 74, 84 }.Contains(mx.formulakind.Value))
  105. {
  106. InitMxClothWidthReplaceMents(mx, mxlist,4);
  107. }
  108. //面裥绵
  109. if (new List<int>() { 0, 40, 50, 60, 70, 80, 203 }.Contains(mx.formulakind.Value))
  110. {
  111. InitMxClothWidthReplaceMents(mx, mxlist,0);
  112. }
  113. //底裥绵
  114. if (new List<int>() { 1, 41, 51, 61, 71, 81 }.Contains(mx.formulakind.Value))
  115. {
  116. InitMxClothWidthReplaceMents(mx, mxlist,1);
  117. }
  118. #endregion
  119. #region 替换布套高
  120. //内布套
  121. if (new List<int>() { 101,102,103,104,105,106 }.Contains(mx.formulakind.Value))
  122. {
  123. expression = InitMxClothHeightReplaceMents(mattress, mx, expression, mxlist, 101);
  124. use_expression = InitMxClothHeightReplaceMents(mattress, mx, use_expression, mxlist, 101);
  125. }
  126. //外布套
  127. if (new List<int>() { 111, 112, 113, 114, 115, 116 }.Contains(mx.formulakind.Value))
  128. {
  129. expression = InitMxClothHeightReplaceMents(mattress, mx, expression, mxlist, 111);
  130. use_expression = InitMxClothHeightReplaceMents(mattress, mx, use_expression, mxlist, 111);
  131. }
  132. #endregion
  133. #region 裥绵收缩率
  134. InitMxShrinkageReplaceMents(mx,mxlist);
  135. #endregion
  136. foreach (var replacement in replacements)
  137. {
  138. expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  139. use_expression = use_expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  140. }
  141. #region 计算成本金额
  142. // 判断expression前两个字符是否为@@
  143. bool sqlCal = !string.IsNullOrEmpty(expression) && expression.Length > 2 && expression.Substring(0, 2) == "@@";
  144. expression = expression.Replace("@@", "");
  145. mx.replace_formula = expression;
  146. try
  147. {
  148. if (sqlCal)
  149. {
  150. mx.costamt = Convert.ToDecimal(SqlCalculate(expression, "成本金额"));
  151. }
  152. else
  153. {
  154. mx.costamt = Calculate(expression, "成本金额").DecimalValue;
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. mx.if_success = 1;
  160. mx.message = e.Message;
  161. }
  162. #endregion
  163. #region 计算清单用量
  164. sqlCal = !string.IsNullOrEmpty(use_expression) && use_expression.Length > 2 &&use_expression.Substring(0, 2) == "@@";
  165. use_expression = use_expression.Replace("@@", "");
  166. mx.replace_useformula = use_expression;
  167. try
  168. {
  169. if (sqlCal)
  170. {
  171. mx.useqty = Convert.ToDecimal(SqlCalculate(use_expression, "清单用量"));
  172. }
  173. else
  174. {
  175. mx.useqty = Calculate(use_expression, "清单用量").DecimalValue;
  176. }
  177. }
  178. catch (Exception e)
  179. {
  180. mx.if_success = 1;
  181. mx.message = e.Message;
  182. }
  183. #endregion
  184. }
  185. }
  186. InitMattressMxListReplaceMents(mattress, mxlist);
  187. }
  188. /// <summary>
  189. /// 记录明细按分组读取厚度变量
  190. /// </summary>
  191. /// <param name="mxlist"></param>
  192. private Dictionary<string, Dictionary<string, object>> InitMxThicknessReplaceMents(List<u_mattress_mx_mtrl> mxlist)
  193. {
  194. // 明细按分组读取厚度变量
  195. var thicknessDict = new Dictionary<string, Dictionary<string, object>> () { };
  196. foreach (var mx in mxlist)
  197. {
  198. // 只处理 内布套(101,102)、顶布裥棉(103)
  199. if (!new List<int>() { 101, 102, 103 }.Contains(mx.formulatype.Value)) continue;
  200. if (mx.chastr.Contains("顶布裥棉") || mx.chastr.Contains("内布套"))
  201. {
  202. if(!thicknessDict.ContainsKey(mx.chastr))
  203. {
  204. thicknessDict.Add(mx.chastr, new Dictionary<string, object>() {});
  205. }
  206. var mxDict = thicknessDict[mx.chastr];
  207. setMxThicknessValue(mx, ref mxDict);
  208. thicknessDict[mx.chastr] = mxDict;
  209. }
  210. }
  211. return thicknessDict;
  212. }
  213. private void setMxThicknessValue(u_mattress_mx_mtrl mx, ref Dictionary<string, object> mxDict)
  214. {
  215. var keyStr = "";
  216. if (new List<int>() { 101, 102 }.Contains(mx.formulatype.Value))
  217. {
  218. switch (mx.formulakind.Value)
  219. {
  220. case 0: // 面裥绵-布料
  221. keyStr = "内布套上覆";
  222. break;
  223. case 1: // 底裥绵-布料
  224. keyStr = "内布套下覆";
  225. break;
  226. case 2: // 大侧裥绵-布料
  227. keyStr = "内布套侧覆";
  228. break;
  229. }
  230. if (!string.IsNullOrEmpty(keyStr))
  231. {
  232. mxDict.Add(keyStr, mx.thickness);
  233. }
  234. }
  235. else if (new List<int>() { 103 }.Contains(mx.formulatype.Value))
  236. {
  237. switch (mx.formulakind.Value)
  238. {
  239. case 50: // 面裥绵-裥绵海绵
  240. keyStr = "物料厚度";
  241. break;
  242. }
  243. if (!string.IsNullOrEmpty(keyStr))
  244. {
  245. mxDict.Add(keyStr, mx.thickness.Value);
  246. }
  247. }
  248. }
  249. private void CalCulateBedNet(List<u_mattress_mx_mtrl> mxlist)
  250. {
  251. var bedNetHelper = HelperBase.GetHelper<BedNetHelper>(cmd, new HelperBase.Context() { tokendata = context.tokendata });
  252. var bednetList = mxlist.Where(t => t.formulatype == 99);
  253. foreach(var mx in bednetList)
  254. {
  255. if (mx.mtrlid <= 0) continue;
  256. var bednet = bedNetHelper.GetBedNet(mx.mtrlid.Value);
  257. if (bednet.flag == 1) continue;
  258. var bednetMxlist = bedNetHelper.GetBedNetMxList(bednet.bednetid.Value);
  259. var bednetSpringList = bedNetHelper.GetBedNetSpringList(bednet.bednetid.Value);
  260. bedNetHelper.CalCulateFormula(bednet, bednetMxlist, bednetSpringList);
  261. bedNetHelper.SaveBedNet(bednet, bednetMxlist, bednetSpringList);
  262. mx.price = bednet.nottax_factory_cost;
  263. }
  264. }
  265. private void InitMattress(u_mattress mattress)
  266. {
  267. var dept = new u_dept() { deptid = mattress.deptid.Value };
  268. DbSqlHelper.SelectOne(cmd, dept, "pricelistid,profitrate,moneyrate,discount");
  269. //var profirate = new u_factory_profitrate() { deptid = mattress.deptid, bednettypeid_mattresstypeid = mattress.mattresstypeid, bednet_or_mattress = 1 };
  270. //DbSqlHelper.SelectOne(cmd, profirate, "profitrate");
  271. decimal profitrate = 0;
  272. cmd.CommandText = @"SELECT profitrate FROM u_factory_profitrate
  273. WHERE deptid = @deptid
  274. AND bednettypeid_mattresstypeid = @bednettypeid_mattresstypeid
  275. AND bednet_or_mattress = 1";
  276. cmd.Parameters.Clear();
  277. cmd.Parameters.AddWithValue("@deptid", mattress.deptid);
  278. cmd.Parameters.AddWithValue("@bednettypeid_mattresstypeid", mattress.mattresstypeid);
  279. using (var reader = cmd.ExecuteReader())
  280. {
  281. if (reader.Read())
  282. {
  283. profitrate = Convert.ToDecimal(reader["profitrate"]);
  284. }
  285. }
  286. mattress.pricelistid = dept.pricelistid;
  287. mattress.dept_profitrate = dept.profitrate;
  288. mattress.profitrate = profitrate;
  289. mattress.moneyrate = mattress.if_moneyrate == 1 ? dept.moneyrate ?? 1 : 1;
  290. mattress.discount = dept.discount ?? 1 ;
  291. mattress.commission = mattress.commission ?? 1;
  292. mattress.taxrate = mattress.taxrate ?? 1;
  293. mattress.other_rate = mattress.other_rate ?? 1;
  294. mattress.dept_profitrate = mattress.dept_profitrate ?? 0;
  295. // 检查佣金是否小于1
  296. if (mattress.commission < 1)
  297. {
  298. throw new LJCommonException("佣金点数不能小于1!");
  299. }
  300. // 检查税率是否小于1
  301. if (mattress.taxrate < 1)
  302. {
  303. throw new LJCommonException("税率不能小于1!");
  304. }
  305. // 检查额外点数是否小于1
  306. if (mattress.other_rate < 1)
  307. {
  308. throw new LJCommonException("税率不能小于1!");
  309. }
  310. // 检查部门利润率是否为0
  311. if (mattress.dept_profitrate == 0)
  312. {
  313. throw new LJCommonException("部门利润率不能为0!");
  314. }
  315. }
  316. private void InitMattressMx(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  317. {
  318. foreach(var mx in mxlist)
  319. {
  320. if (!new List<int>() { 0, 1, 2, 3, 104 }.Contains(mx.formulatype.Value)) continue;
  321. var formula = new u_mattress_formula() { formulaid = mx.formulaid, };
  322. DbSqlHelper.SelectOne(cmd, formula, "formulakind,formulatype,sortcode");
  323. var mtrldef = new u_mtrl_price() { mtrlid = mx.mtrlid };
  324. DbSqlHelper.SelectOne(cmd, mtrldef, "name,gram_weight,cloth_width,if_inputqty,priceunit,shrinkage,if_areaprice");
  325. mx.costamt = 0;
  326. mx.useqty = 0;
  327. mx.if_success = 0;
  328. mx.replace_formula = "";
  329. mx.replace_useformula = "";
  330. mx.formulakind = formula.formulakind;
  331. mx.formulatype = formula.formulatype;
  332. //mx.sortcode = formula.sortcode.Trim();
  333. mx.mtrlname = mtrldef.name ?? "";
  334. var formula2 = new u_mattress_formula();
  335. DbSqlHelper.SelectOne(cmd, "u_mattress_formula","formulakind = @formulakind",
  336. new Dictionary<string, object>() { { "@formulakind",mx.formulakind } },formula2, "formula,useformula,gydscrp");
  337. mx.formula = formula2.formula;
  338. mx.useformula = formula2.useformula;
  339. mx.gydscrp = formula2.gydscrp;
  340. cmd.CommandText = @"SELECT isnull(u_mtrl_price_pricelist.price, 0) AS pricelistprice
  341. ,u_mtrl_price.gram_weight
  342. ,u_mtrl_price.cloth_width
  343. ,u_mtrl_price.if_inputqty
  344. ,u_mtrl_price.priceunit
  345. ,u_mtrl_price.shrinkage
  346. ,isnull(u_mtrl_price_pricelist.price_formula, 0) AS price_formula
  347. ,isnull(u_mtrl_price_pricelist.qty_formula, 0) AS qty_formula
  348. ,u_mtrl_price.if_areaprice
  349. ,u_mtrl_price.thickness
  350. FROM u_mtrl_price
  351. INNER JOIN u_mtrl_price_pricelist ON u_mtrl_price.mtrlid = u_mtrl_price_pricelist.mtrlid
  352. WHERE (u_mtrl_price.mtrlid = @mtrlid)
  353. AND (u_mtrl_price_pricelist.pricelistid = @pricelistid)";
  354. cmd.Parameters.Clear();
  355. cmd.Parameters.AddWithValue("@mtrlid", mx.mtrlid);
  356. cmd.Parameters.AddWithValue("@pricelistid", mattress.pricelistid);
  357. using(var reader = cmd.ExecuteReader())
  358. {
  359. if (reader.Read())
  360. {
  361. mx.price = Convert.ToDecimal(reader["pricelistprice"]);
  362. mx.gram_weight = Convert.ToDecimal(reader["gram_weight"]);
  363. mx.cloth_width = Convert.ToDecimal(reader["cloth_width"]);
  364. mx.if_inputqty = Convert.ToInt32(reader["if_inputqty"]);
  365. mx.priceunit = reader["priceunit"].ToString();
  366. mx.shrinkage = Convert.ToDecimal(reader["shrinkage"]);
  367. mx.if_areaprice = Convert.ToByte(reader["if_areaprice"]);
  368. if(mx.if_inputqty == 1) mx.thickness = Convert.ToDecimal(reader["thickness"]);
  369. string price_formula = Convert.ToString(reader["price_formula"]);
  370. string qty_formula = Convert.ToString(reader["qty_formula"]);
  371. mx.formula = string.IsNullOrEmpty(price_formula) ? mx.formula : price_formula;
  372. mx.useformula = string.IsNullOrEmpty(qty_formula) ? mx.useformula : qty_formula;
  373. }
  374. }
  375. }
  376. }
  377. private void InitReplaceMents(u_mattress mattress)
  378. {
  379. replacements.Clear();
  380. AddKeyValue("规格宽", mattress.mattress_width);
  381. AddKeyValue("规格长", mattress.mattress_length);
  382. AddKeyValue("规格高", mattress.mattress_height);
  383. AddKeyValue("压包数量", mattress.packqty);
  384. AddKeyValue("卷包直径", mattress.diameter);
  385. AddKeyValue("顶布裥棉外布套做法", mattress.if_db_wbutao_way);
  386. AddKeyValue("面料外布套做法", mattress.if_m_wbutao_way);
  387. AddKeyValue("内布套上覆", mattress.s_cover_qty);
  388. AddKeyValue("内布套侧覆", mattress.z_cover_qty);
  389. AddKeyValue("内布套下覆", mattress.x_cover_qty);
  390. AddKeyValue("面料上覆", mattress.s_m_cover_qty);
  391. AddKeyValue("面料侧覆", mattress.z_m_cover_qty);
  392. AddKeyValue("面料下覆", mattress.x_m_cover_qty);
  393. AddKeyValue("面拆", mattress.if_m_chai);
  394. AddKeyValue("中拆", mattress.if_z_chai);
  395. AddKeyValue("底拆", mattress.if_d_chai);
  396. AddKeyValue("工厂利润率", mattress.profitrate);
  397. AddKeyValue("部门利润率", mattress.dept_profitrate);
  398. AddKeyValue("部门让利点数", mattress.dept_profitrate_rangli);
  399. AddKeyValue("佣金点数", mattress.commission);
  400. AddKeyValue("额外点数", mattress.other_rate);
  401. AddKeyValue("额外费用", mattress.extras_cost);
  402. AddKeyValue("汇率", mattress.moneyrate);
  403. AddKeyValue("税率", mattress.taxrate);
  404. AddKeyValue("折扣率", mattress.discount);
  405. // 地区FOB费用,如果存在变量表,会替代
  406. AddKeyValue("大柜-普通地区-地区FOB费用", 6500);
  407. AddKeyValue("大柜-特定地区-地区FOB费用", 8500);
  408. AddKeyValue("小柜-普通地区-地区FOB费用", 6000);
  409. AddKeyValue("小柜-特定地区-地区FOB费用", 7000);
  410. // 柜型立方数
  411. AddKeyValue("大柜-柜型立方数", 64);
  412. AddKeyValue("小柜-柜型立方数", 28);
  413. // 柜型米数
  414. AddKeyValue("大柜-柜型米数", 12);
  415. AddKeyValue("小柜-柜型米数", 5.9);
  416. // 公式
  417. AddFormulaKeyValue("总成本", "【总材料成本】*【大小单】+【款式费用】+【边带费用】+【额外费用】+【制造费用】");
  418. AddFormulaKeyValue("不含税出厂价", "【总成本】*(【工厂利润率】+【工艺点数】+【内布套点数】+【拆装点数】+【海绵扣点】)*【管理费点】");
  419. AddFormulaKeyValue("部门不含税价", "【不含税出厂价】/(【部门利润率】+【部门让利点数】/100)/(1-(【佣金点数】-1))*【额外点数】+【FOB】");
  420. AddFormulaKeyValue("税金", "【部门不含税价】*(【税率】-1)+【佣金】 * 0.05");
  421. AddFormulaKeyValue("部门含税价", "(【部门不含税价】+【佣金】 * 0.06)*【税率】*【折扣率】");
  422. AddFormulaKeyValue("外币价", "【部门含税价】/【汇率】");
  423. AddFormulaKeyValue("底价", "if((【部门利润率】+【部门让利点数】/100)<> 0,【不含税出厂价】/(【部门利润率】+【部门让利点数】/ 100),0)");
  424. AddFormulaKeyValue("佣金", "【底价】/(1-(【佣金点数】-1))-【底价】");
  425. //AddFormulaHideKeyValue("【总材料成本】", "【弹簧材料总成本】+【蛇线材料成本】+【四周口袋弹簧成本】+【四周口袋无纺布成本】+【四周加硬材料成本】+【胶水材料成本】+【入袋无纺布材料成本】+【底面无纺布材料成本】+【边铁材料成本】+【C钉/夹码材料成本】+【海绵包边材料成本】+【填充海绵成本】+【封边材料成本】+【弹叉材料成本】+【胶条/包角材料成本】+【上垫层物料成本】+【下垫层物料成本】+【包装总成本】");
  426. if (mattress.woodpallettype == 0)
  427. {
  428. AddKeyValue("木托方式", "'普通木托'");
  429. }else if (mattress.woodpallettype == 1)
  430. {
  431. AddKeyValue("木托方式", " '夹板木托'");
  432. }else if (mattress.woodpallettype == 2)
  433. {
  434. AddKeyValue("木托方式", "'铁管木托' ");
  435. }
  436. var dept = new u_dept() { deptid = mattress.deptid.Value };
  437. DbSqlHelper.SelectOne(cmd, dept, "pricelistid");
  438. var bednetVarList = new List<u_bednet_var>();
  439. var selectStr = @"SELECT u_bednet_varmx.varid AS varid
  440. ,varkind
  441. ,varcode
  442. ,varname
  443. ,varclass
  444. ,vartype
  445. ,varunit
  446. ,u_bednet_varmx.varvalue AS varvalue
  447. ,formula
  448. FROM u_bednet_var
  449. INNER JOIN u_bednet_varmx ON u_bednet_varmx.varid = u_bednet_var.varid
  450. INNER JOIN u_dept ON u_dept.pricelistid = u_bednet_varmx.pricelistid";
  451. var whereList = new List<string>();
  452. whereList.Add("u_bednet_var.varclass < 2");
  453. whereList.Add("u_bednet_var.varkind = @varkind");
  454. whereList.Add("u_dept.deptid = @deptid");
  455. var param = new Dictionary<string, object>();
  456. param.Add("@varkind", 1);
  457. param.Add("@deptid", mattress.deptid);
  458. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "varid", "varid,varkind,varcode,varname,varclass,vartype,varunit,varvalue", 0, 0, bednetVarList);
  459. foreach (var netvar in bednetVarList)
  460. {
  461. AddKeyValue(netvar.varname, netvar.varvalue);
  462. }
  463. selectStr = @"SELECT varid
  464. ,varkind
  465. ,varcode
  466. ,varname
  467. ,varclass
  468. ,vartype
  469. ,varunit
  470. ,formula
  471. FROM u_bednet_var";
  472. bednetVarList = new List<u_bednet_var>();
  473. whereList = new List<string>();
  474. whereList.Add("u_bednet_var.varkind = @varkind");
  475. whereList.Add("u_bednet_var.varclass = @varclass");
  476. param = new Dictionary<string, object>();
  477. param.Add("@varkind", 1);
  478. param.Add("@varclass", 2);
  479. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "varid", "varid,varkind,varcode,varname,varclass,vartype,varunit,formula", 0, 0, bednetVarList);
  480. // 添加替换公式变量
  481. foreach (var netvar in bednetVarList)
  482. {
  483. AddFormulaKeyValue(netvar.varname, netvar.formula);
  484. }
  485. }
  486. private void InitMxReplaceMents(u_mattress_mx_mtrl mx)
  487. {
  488. mx.mtrlname = !string.IsNullOrEmpty(mx.mtrlname) ? mx.mtrlname.Trim() : "null";
  489. decimal _shrinkage = mx.shrinkage.Value == 0 ? (decimal)1 : mx.shrinkage.Value;
  490. //取mtrldef.name的前两位
  491. AddKeyValue("名称前2位", mx.mtrlname.Length > 2 ? mx.mtrlname.Substring(0, 2) : mx.mtrlname);
  492. AddKeyValue("物料单价", mx.price);
  493. AddKeyValue("物料克重", mx.gram_weight);
  494. AddKeyValue("幅宽", mx.cloth_width);
  495. AddKeyValue("数量", mx.qty);
  496. AddKeyValue("物料厚度", mx.if_inputqty);
  497. AddKeyValue("固定厚度", mx.if_inputqty);
  498. AddKeyValue("厚度", mx.thickness);
  499. AddKeyValue("按面积单价", mx.if_areaprice);
  500. AddKeyValue("物料名称", mx.mtrlname);
  501. if (_shrinkage > 0) AddKeyValue("物料收缩率", _shrinkage);
  502. }
  503. /// <summary>
  504. /// 添加大侧,小侧,V侧高度公式替换
  505. /// </summary>
  506. /// <param name="mx"></param>
  507. /// <param name="diancengList"></param>
  508. /// <param name="mattersstype"></param>
  509. /// <param name="expression"></param>
  510. /// <returns></returns>
  511. private string InitMxSideReplaceMents(u_mattress mattress, u_mattress_mx_mtrl mx,List<u_mattress_mx_mtrl> diancengList, u_mattress_type mattersstype, string expression)
  512. {
  513. // 获取垫层的数据
  514. decimal bigSum = 0, smallSum = 0, vSum = 0;
  515. string bigChaStr = "大侧";
  516. string smallChaStr = mx.chastr ?? "小侧1";
  517. string vChaStr = mx.chastr ?? "V侧1";
  518. foreach (var dianceng in diancengList)
  519. {
  520. if (dianceng.chastr.IndexOf(bigChaStr) > -1) bigSum += dianceng.qty.Value * dianceng.thickness.Value;
  521. if (dianceng.chastr.IndexOf(smallChaStr) > -1) smallSum += dianceng.qty.Value * dianceng.thickness.Value;
  522. if (dianceng.chastr.IndexOf(vChaStr) > -1) vSum += dianceng.qty.Value * dianceng.thickness.Value;
  523. }
  524. if (bigSum > 0)
  525. {
  526. AddKeyValue("垫层", bigSum);
  527. }
  528. if (smallSum > 0)
  529. {
  530. AddKeyValue("垫层", smallSum);
  531. }
  532. if (vSum > 0)
  533. {
  534. AddKeyValue("垫层", vSum);
  535. }
  536. #region 替换大侧高度
  537. if (expression.IndexOf("【大侧高度】") > -1)
  538. {
  539. if (!string.IsNullOrEmpty(mattersstype.formula_big_side)) expression = expression.Replace("【大侧高度】", $"({mattersstype.formula_big_side})");
  540. expression = expression.Replace("【位置】", $"'{bigChaStr}'");
  541. if (bigSum > 0) expression = expression.Replace("【大侧高度】", $"{bigSum}");
  542. }
  543. #endregion
  544. #region 替换小侧高度
  545. if (expression.IndexOf("【小侧高度】") > -1)
  546. {
  547. if (!string.IsNullOrEmpty(mattersstype.formula_small_side)) expression = expression.Replace("【小侧高度】", $"({mattersstype.formula_small_side})");
  548. expression = expression.Replace("【位置】", $"'{smallChaStr}'");
  549. expression = expression.Replace("【小侧高度】", $"{smallSum}");
  550. }
  551. #endregion
  552. #region 替换V侧高度
  553. if (expression.IndexOf("【V侧高度】") > -1)
  554. {
  555. if(!string.IsNullOrEmpty(mattersstype.formula_v_side)) expression = expression.Replace("【V侧高度】", $"({mattersstype.formula_v_side})");
  556. expression = expression.Replace("【位置】", $"'{vChaStr}'");
  557. expression = expression.Replace("【V侧高度】", $"18");
  558. }
  559. #endregion
  560. #region 若填写则顶替床垫尺寸
  561. if (mx.mattress_width != null && mx.mattress_width > 0 && mx.mattress_width <= mattress.mattress_width) expression = expression.Replace("【规格宽】", $"{mx.mattress_width}");
  562. if (mx.mattress_length != null && mx.mattress_length > 0 && mx.mattress_length <= mattress.mattress_length) expression = expression.Replace("【规格长】", $"{mx.mattress_length}");
  563. #endregion
  564. return expression;
  565. }
  566. /// <summary>
  567. /// 添加布料幅宽公式替换
  568. /// </summary>
  569. /// <param name="mx"></param>
  570. /// <param name="mxlist"></param>
  571. /// <param name="formulakind"></param>
  572. private void InitMxClothWidthReplaceMents(u_mattress_mx_mtrl mx, List<u_mattress_mx_mtrl> mxlist,int formulakind)
  573. {
  574. decimal cloth_width = 0;
  575. if (string.IsNullOrEmpty(mx.chastr))
  576. {
  577. cloth_width = mxlist.First(t => t.formulakind.Value == formulakind).cloth_width.Value;
  578. }
  579. else
  580. {
  581. cloth_width = mxlist.First(t => t.formulakind.Value == formulakind && t.chastr.Equals(mx.chastr)).cloth_width.Value;
  582. }
  583. AddKeyValue("布料幅宽", cloth_width);
  584. }
  585. /// <summary>
  586. /// 添加布套高公式替换
  587. /// </summary>
  588. /// <param name="mxlist"></param>
  589. /// <param name="formulakind"></param>
  590. private string InitMxClothHeightReplaceMents(u_mattress mattress, u_mattress_mx_mtrl mx, string expression, List<u_mattress_mx_mtrl> mxlist, int formulakind)
  591. {
  592. decimal cloth_height = 0;
  593. cloth_height = mxlist.First(t => t.formulakind.Value == formulakind).thickness.Value;
  594. AddKeyValue("布套高", cloth_height);
  595. if (mx.mattress_width != null && mx.mattress_width > 0 && mx.mattress_width <= mattress.mattress_width) expression = expression.Replace("【规格宽】", $"{mx.mattress_width}");
  596. if (mx.mattress_length != null && mx.mattress_length > 0 && mx.mattress_length <= mattress.mattress_length) expression = expression.Replace("【规格长】", $"{mx.mattress_length}");
  597. return expression;
  598. }
  599. /// <summary>
  600. /// 添加裥绵收缩率公式替换
  601. /// </summary>
  602. /// <param name="mx"></param>
  603. /// <param name="mxlist"></param>
  604. private void InitMxShrinkageReplaceMents(u_mattress_mx_mtrl mx, List<u_mattress_mx_mtrl> mxlist)
  605. {
  606. decimal thickness = 0, shrinkage = 0;
  607. int[][] formulaKinds = new int[][]
  608. {
  609. new int [] {50, 0, 40, 60, 70, 80},
  610. new int [] {51, 1, 41, 61, 71, 81},
  611. new int [] {52, 2, 42, 62, 72, 82},
  612. new int [] {53, 3, 43, 63, 73, 83},
  613. new int [] {54, 4, 44, 64, 74, 84},
  614. };
  615. foreach (int[] kindGroup in formulaKinds)
  616. {
  617. if (kindGroup.Contains(mx.formulakind.Value))
  618. {
  619. foreach (var row in mxlist) // mxList.Rows等价于arg_dw.RowCount()
  620. {
  621. if (row.formulakind.Value == kindGroup[0])
  622. {
  623. thickness += row.thickness.Value * row.qty.Value; // 简化计算
  624. }
  625. }
  626. }
  627. }
  628. if(thickness >= 0)
  629. {
  630. cmd.CommandText = @"SELECT TOP 1 shrinkage
  631. FROM u_shrinkage
  632. WHERE u_shrinkage.minvalue <= @thickness
  633. AND u_shrinkage.maxvalue >= @thickness";
  634. cmd.Parameters.Clear();
  635. cmd.Parameters.AddWithValue("@thickness", thickness);
  636. using(var reader = cmd.ExecuteReader())
  637. {
  638. if (reader.Read())
  639. {
  640. shrinkage = Convert.ToDecimal(reader["shrinkage"]);
  641. }
  642. }
  643. }
  644. if(shrinkage > 0)
  645. {
  646. AddKeyValue("收缩率", shrinkage);
  647. }
  648. }
  649. /// <summary>
  650. /// 添加主表与明细表相关公式替换
  651. /// </summary>
  652. /// <param name="mattress"></param>
  653. /// <param name="mxlist"></param>
  654. private void InitMattressMxListReplaceMents(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  655. {
  656. string fob_expression = string.Empty,cubage_expression = string.Empty;
  657. cubage_expression = "(【规格长】*【规格宽】* 【规格高】)/100000";
  658. #region 判断装柜方式 是否进箱
  659. if (mattress.packtype == 0)
  660. {
  661. mattress.loading_type = "平压";
  662. fob_expression = "【地区FOB费用】/(【柜型米数】 /(【规格宽】/ 100 +0.1)*(【压包数量】*2))";
  663. if (mattress.packqty == 0) throw new LJCommonException("请填写压包数量!");
  664. }
  665. else if (mattress.packtype == 1)
  666. {
  667. mattress.loading_type = "卷包(非进箱)";
  668. fob_expression = "【地区FOB费用】 /(【柜型立方数】 /( 3.14*( 【卷包直径】 /100/2+0.03)*( 【卷包直径】 /100/2+0.03)*(【规格宽】/ 100 +0.05)))";
  669. cubage_expression = "( 3.14*( 【卷包直径】 /100/2+0.03)*( 【卷包直径】 /100/2+0.03)*(【规格宽】/ 100 +0.05))";
  670. if (mattress.diameter == 0) throw new LJCommonException("请填写卷包直径!");
  671. var _bzList = mxlist.Where(t => t.formulatype == 3 && t.formulakind == 19).ToList();
  672. foreach(var bz in _bzList)
  673. {
  674. if (bz.mtrlname.IndexOf("箱") > -1) {
  675. mattress.loading_type = "卷包(进箱)";
  676. fob_expression = "【地区FOB费用】/(【柜型立方数】 / (( 【纸箱宽】 /100+0.03)*(【纸箱宽】/100+0.03)*(【规格宽】/ 100 +0.1)))";
  677. cubage_expression = "( 【纸箱宽】 /100+0.03)*(【纸箱宽】/100+0.03)*(【规格宽】/ 100 +0.1)";
  678. AddKeyValue("【纸箱宽】", bz.mtrlname.Length > 2 ? bz.mtrlname.Substring(0, 2) : bz.mtrlname);
  679. break;
  680. }
  681. }
  682. }
  683. else if (mattress.packtype == 2)
  684. {
  685. mattress.loading_type = "国内";
  686. fob_expression = "【地区FOB费用】/(【柜型立方数】/((【规格宽】/100+0.03)*(【规格长】/100+0.03)*(【规格高】/100+0.03)))";
  687. }
  688. #endregion
  689. #region 计算FOB
  690. if (!string.IsNullOrEmpty(mattress.area) && !string.IsNullOrEmpty(mattress.cabinet_type) && !string.IsNullOrEmpty(mattress.loading_type))
  691. {
  692. //地区FOB费用
  693. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type) && new List<string>() { "普通地区", "特定地区" }.Contains(mattress.area))
  694. {
  695. fob_expression = fob_expression.Replace("【地区FOB费用】", $"【{mattress.cabinet_type.Trim()}-{mattress.area.Trim()}-地区FOB费用】");
  696. }
  697. //柜型立方数
  698. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type))
  699. {
  700. fob_expression = fob_expression.Replace("【柜型立方数】", $"【{mattress.cabinet_type.Trim()}-柜型立方数】");
  701. }
  702. //纸箱宽 见上面
  703. //柜型米数
  704. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type))
  705. {
  706. fob_expression = fob_expression.Replace("【柜型米数】", $"【{mattress.cabinet_type.Trim()}-柜型米数】");
  707. }
  708. mattress.fob_replace_formula = fob_expression;
  709. mattress.fob = Calculate(fob_expression, "FOB").DecimalValue;
  710. //半成品:如果PE袋物料是含 半成品专用 的编码,FOB需要除以数量
  711. var bzList2 = mxlist.Where(t => t.formulatype == 3).ToList();
  712. foreach (var bz in bzList2)
  713. {
  714. if (bz.qty.Value > 0 && bz.mtrlname.IndexOf("半成品专用") > -1)
  715. {
  716. mattress.fob /= bz.qty.Value;
  717. mattress.fob_replace_formula = $"{mattress.fob_replace_formula} / {bz.qty.Value}";
  718. }
  719. }
  720. };
  721. AddKeyValue("FOB", mattress.fob);
  722. #endregion
  723. #region 计算体积
  724. if (mattress.fob > 0)
  725. {
  726. mattress.cubage = Calculate(cubage_expression, "体积").DecimalValue;
  727. }
  728. #endregion
  729. #region 计算人工费用
  730. var mattersstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  731. DbSqlHelper.SelectOne(cmd, mattersstype, "hrcost_formula,biandaiqty,zhizao_amt,guanli_rate");
  732. if(mattress.biandai_qty == 0 && mattress.if_m_chai == 0 && mattress.if_z_chai == 0 && mattress.if_d_chai == 0)
  733. {
  734. mattress.biandai_qty = mattersstype.biandaiqty;
  735. }
  736. mattress.hrcost_replace_formula = mattersstype.hrcost_formula;
  737. mattress.zhizao_amt = mattersstype.zhizao_amt;
  738. mattress.guanli_rate = mattersstype.guanli_rate;
  739. var diancengList = mxlist.Where(t => t.formulatype == 1).ToList();
  740. int diancengCount = 0;
  741. foreach(var dianceng in diancengList)
  742. {
  743. if (dianceng.formulakind == 32) diancengCount++;
  744. }
  745. AddKeyValue("垫层数量", diancengCount);
  746. AddKeyValue("制造费用", mattress.zhizao_amt);
  747. AddKeyValue("管理费点", mattress.guanli_rate);
  748. mattress.hrcost = Calculate(mattress.hrcost_replace_formula, "款式费用").DecimalValue;
  749. AddKeyValue("款式费用", mattress.hrcost);
  750. #endregion
  751. #region 计算边带费用
  752. mattress.biandaicost_replace_formula = "【边带条数】*【边带单价】*((【规格宽】+【规格长】)*2+30)/100";
  753. AddKeyValue("边带条数", mattress.biandai_qty);
  754. mattress.biandaicost = Calculate(mattress.biandaicost_replace_formula, "边带费用").DecimalValue;
  755. AddKeyValue("边带费用", mattress.biandaicost);
  756. #endregion
  757. #region 辅料 有打钮纽扣 加0.13 工厂利润率
  758. var fuliaoList = mxlist.Where(t => t.formulatype == 2 && t.mtrlname.IndexOf("打钮") > -1).ToList();
  759. if (fuliaoList.Any()) mattress.profitrate += 0.13M;
  760. #endregion
  761. #region 拼侧 最大金额的侧 和 工厂利润率加点
  762. CalCulatePince(mattress, mxlist);
  763. #endregion
  764. #region 内布套特殊加点
  765. CalCulateNetBuTao(mattress, mxlist);
  766. #endregion
  767. #region 拆装特殊加点
  768. CalCulateChaiZhuang(mattress);
  769. #endregion
  770. #region 海绵款特殊扣点
  771. CalCulateHaiMian(mattress);
  772. #endregion
  773. #region 计算主表 总材料成本
  774. var aa = new List<decimal>();
  775. mxlist.ForEach(t => {
  776. aa.Add(t.costamt.Value);
  777. });
  778. mattress.total_material_cost = mxlist.Sum(t => t.costamt);
  779. AddKeyValue("总材料成本", mattress.total_material_cost);
  780. #endregion
  781. #region 标准金额
  782. // 查找点数
  783. decimal _rate = GetWorkmanshipRate(mattress, "num_10", "标准");
  784. _rate = _rate == 0 ? 1 : _rate;
  785. mattress.foreign_cost_bz = mattress.dept_cost / _rate;
  786. AddKeyValue("标准金额", mattress.foreign_cost_bz);
  787. #endregion
  788. #region 大单金额
  789. // 查找点数
  790. decimal _rate2 = GetWorkmanshipRate(mattress, "num_11", "大单");
  791. _rate2 = _rate2 == 0 ? 1 : _rate2;
  792. mattress.foreign_cost_dd = mattress.dept_cost / _rate2;
  793. AddKeyValue("大单金额", mattress.foreign_cost_dd);
  794. #endregion
  795. CalCulateCostNew(mattress, "标准");
  796. CalCulateCostNew(mattress, "大单");
  797. CalCulateCostNew(mattress, "散单");
  798. }
  799. /// <summary>
  800. /// 计算拼侧最大金额的侧和工厂利润率加点
  801. /// </summary>
  802. /// <param name="mattress"></param>
  803. /// <param name="mxlist"></param>
  804. private void CalCulatePince(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist)
  805. {
  806. var mianliaoList = mxlist.Where(t => t.formulatype == 0).ToList();
  807. var costAmounts = new Dictionary<string, decimal>
  808. {
  809. { "普通大侧", 0m },
  810. { "上下拼侧1", 0m },
  811. { "上下拼侧2", 0m },
  812. { "上下拼侧3", 0m },
  813. { "左右拼侧1", 0m },
  814. { "左右拼侧2", 0m },
  815. { "左右拼侧3", 0m },
  816. { "左右拼侧4", 0m }
  817. };
  818. var costAmounts2 = new Dictionary<string, decimal>
  819. {
  820. { "小侧1-上下拼侧1", 0m },
  821. { "小侧1-上下拼侧2", 0m },
  822. { "小侧1-上下拼侧3", 0m }
  823. };
  824. foreach (var mianliao in mianliaoList)
  825. {
  826. if (costAmounts.ContainsKey(mianliao.sortcode))
  827. {
  828. costAmounts[mianliao.sortcode] += mianliao.costamt.Value;
  829. }else if (costAmounts2.ContainsKey(mianliao.sortcode))
  830. {
  831. costAmounts2[mianliao.sortcode] += mianliao.costamt.Value;
  832. }
  833. }
  834. // 找出最大拼侧金额及对应的拼侧名称
  835. string lsTempMaxStr = null;
  836. decimal ldeTempMax = decimal.MinValue;
  837. foreach (var item in costAmounts)
  838. {
  839. if (item.Value >= ldeTempMax)
  840. {
  841. ldeTempMax = item.Value;
  842. lsTempMaxStr = item.Key;
  843. }
  844. }
  845. // 清空不是最大金额的拼侧
  846. foreach (var mianliao in mianliaoList)
  847. {
  848. if (mianliao.sortcode != lsTempMaxStr && new List<int>() { 2, 82, 42, 52, 62, 72 }.Contains(mianliao.formulakind.Value))
  849. {
  850. mianliao.costamt = 0;
  851. }
  852. }
  853. // 找出最大小侧拼侧金额及对应的拼侧名称
  854. string lsTempMaxStr2 = null;
  855. decimal ldeTempMax2 = decimal.MinValue;
  856. foreach (var item in costAmounts)
  857. {
  858. if (item.Value >= ldeTempMax)
  859. {
  860. ldeTempMax = item.Value;
  861. lsTempMaxStr = item.Key;
  862. }
  863. }
  864. if (ldeTempMax2 > 0)
  865. {
  866. // 清空不是最大金额的拼侧
  867. foreach (var mianliao in mianliaoList)
  868. {
  869. if (mianliao.sortcode != lsTempMaxStr2 && new List<int>() { 3, 83, 43, 53, 63, 73 }.Contains(mianliao.formulakind.Value))
  870. {
  871. mianliao.costamt = 0;
  872. }
  873. }
  874. }
  875. // 计算拼侧数量
  876. int pinceCnt = costAmounts.Values.Count(value => value > 0);
  877. // 判断是否有大侧
  878. bool hasBigSide = mianliaoList.Any(row => new[] { 7, 8, 9, 10 }.Contains(row.formulakind.Value) && row.mtrlid.Value > 0);
  879. string field = pinceCnt > 3 || (hasBigSide && (pinceCnt == 2 || pinceCnt == 3)) ? "num_3" : hasBigSide ? "num_1" : pinceCnt == 2 || pinceCnt == 3 ? "num_2" : string.Empty;
  880. // 查找点数
  881. mattress.profitrate_point = GetWorkmanshipRate(mattress, field, "工艺点数");
  882. AddKeyValue("工艺点数", mattress.profitrate_point);
  883. }
  884. /// <summary>
  885. /// 计算内布套特殊加点
  886. /// </summary>
  887. /// <param name="mattress"></param>
  888. /// <param name="mxlist"></param>
  889. private void CalCulateNetBuTao(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist)
  890. {
  891. if(mattress.if_n_butao == 1)
  892. {
  893. var butaoList = mxlist.Where(t => t.formulatype == 101 || t.formulatype == 102);
  894. bool butaoFz = false, butaoJd = false;
  895. // 检查内布套是否为复杂款
  896. foreach (var butao in butaoList) {
  897. if (butao.formulakind == 70 && butao.mtrlid > 0) { butaoFz = true; break; }
  898. }
  899. if (butaoFz)
  900. {
  901. foreach (var butao in butaoList)
  902. {
  903. if (butao.formulakind == 203 && butao.mtrlid > 0) { butaoFz = true; break; }
  904. }
  905. }
  906. else
  907. {
  908. butaoJd = true; // 没有找到复杂款即为简单款
  909. }
  910. // 根据款式类型判断查询的字段
  911. string field = butaoJd ? "num_4" : (butaoFz ? "num_5" : "");
  912. // 查找点数
  913. //var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  914. mattress.butao_point = GetWorkmanshipRate(mattress, field, "内布套点数");
  915. }
  916. AddKeyValue("内布套点数", mattress.butao_point);
  917. }
  918. /// <summary>
  919. /// 拆装特殊加点
  920. /// </summary>
  921. /// <param name="mattress"></param>
  922. private void CalCulateChaiZhuang(u_mattress mattress)
  923. {
  924. int cnt = 0;
  925. // 判断 if_m_chai, if_z_chai, if_d_chai 是否为 1
  926. if (Convert.ToInt32(mattress.if_m_chai) == 1) cnt++;
  927. if (Convert.ToInt32(mattress.if_z_chai) == 1) cnt++;
  928. if (Convert.ToInt32(mattress.if_d_chai) == 1) cnt++;
  929. // 根据款式类型判断查询的字段
  930. string field = cnt == 1 ? "num_6" : (cnt > 1 ? "num_7" : "");
  931. // 查找点数
  932. mattress.chaizhuang_point = GetWorkmanshipRate(mattress, field, "拆装点数");
  933. AddKeyValue("拆装点数", mattress.chaizhuang_point);
  934. }
  935. /// <summary>
  936. /// 计算海绵款特殊扣点
  937. /// </summary>
  938. /// <param name="mattress"></param>
  939. private void CalCulateHaiMian(u_mattress mattress)
  940. {
  941. if (mattress.if_haimian_type == 1)
  942. {
  943. // 根据款式类型判断查询的字段
  944. string field = "num_8";
  945. // 查找点数
  946. mattress.haimian_point = GetWorkmanshipRate(mattress, field, "海绵扣点");
  947. }
  948. AddKeyValue("海绵扣点", mattress.haimian_point);
  949. }
  950. /// <summary>
  951. /// 获取工厂利润率
  952. /// </summary>
  953. /// <param name="mattress"></param>
  954. /// <param name="field">旧表字段名</paramD>
  955. /// <param name="label">新表变量名</paramD>
  956. /// <returns></returns>
  957. public decimal GetWorkmanshipRate(u_mattress mattress, string field, string label)
  958. {
  959. decimal rate = 0;
  960. if (field == "") return 0;
  961. cmd.CommandText = @"SELECT formula, rate FROM u_workmanship_add WHERE deptid = @deptid AND mattresstypeid = @mattresstypeid AND workmanshipname = @workmanshipname";
  962. cmd.Parameters.Clear();
  963. cmd.Parameters.AddWithValue("@deptid", mattress.deptid);
  964. cmd.Parameters.AddWithValue("@mattresstypeid", mattress.mattresstypeid);
  965. cmd.Parameters.AddWithValue("@workmanshipname", label);
  966. using (var reader = cmd.ExecuteReader())
  967. {
  968. if (reader.Read())
  969. {
  970. rate = reader[field] == null ? 0 : Convert.ToDecimal(reader[field]);
  971. var _formula = Convert.ToString(reader["formula"]);
  972. if (!string.IsNullOrEmpty(_formula))
  973. {
  974. AddKeyValue(label, rate);
  975. rate = Calculate(mattress.hrcost_replace_formula, label).DecimalValue;
  976. }
  977. }
  978. }
  979. if (rate == 0)
  980. {
  981. cmd.CommandText = @"SELECT " + field + " FROM u_workmanship_dianshu WHERE deptid = @deptid AND mattresstypeid = @mattresstypeid";
  982. cmd.Parameters.Clear();
  983. cmd.Parameters.AddWithValue("@deptid", mattress.deptid);
  984. cmd.Parameters.AddWithValue("@mattresstypeid", mattress.mattresstypeid);
  985. using (var reader = cmd.ExecuteReader())
  986. {
  987. if (reader.Read())
  988. {
  989. rate = reader[field] == null ? 0 : Convert.ToDecimal(reader[field]);
  990. }
  991. }
  992. }
  993. return rate;
  994. }
  995. /// <summary>
  996. /// 计算大小单加点
  997. /// </summary>
  998. /// <param name="mattress"></param>
  999. /// <param name="typename"></param>
  1000. private void CalCulateDanNum(u_mattress mattress,string typename)
  1001. {
  1002. // 根据款式类型判断查询的字段
  1003. string field = "散单".Equals(typename) ? "num_9" : "标准".Equals(typename) ? "num_10" : "大单".Equals(typename) ? "num_11" : "";
  1004. // 查找点数
  1005. decimal _rate = GetWorkmanshipRate(mattress, field, typename);
  1006. mattress.dannum_rate = _rate == 0 ? 1 : _rate;
  1007. AddKeyValue("大小单", mattress.dannum_rate);
  1008. }
  1009. /// <summary>
  1010. /// 计算成本
  1011. /// </summary>
  1012. /// <param name="mattress"></param>
  1013. /// <param name="typename"></param>
  1014. private void CalCulateCostNew(u_mattress mattress, string typename)
  1015. {
  1016. #region 总成本
  1017. CalCulateDanNum(mattress, typename);
  1018. mattress.total_cost = CalculateVarFormula("总成本").DecimalValue;
  1019. if (mattress.if_zhedie_type == 1) mattress.total_cost *= 2;
  1020. AddKeyValue("总成本", mattress.total_cost);
  1021. #endregion
  1022. #region 不含税出厂价
  1023. mattress.nottax_factory_cost = CalculateVarFormula("不含税出厂价").DecimalValue;
  1024. AddKeyValue("不含税出厂价", mattress.nottax_factory_cost);
  1025. #endregion
  1026. #region 部门不含税价
  1027. mattress.nottax_dept_cost = CalculateVarFormula("部门不含税价").DecimalValue;
  1028. AddKeyValue("部门不含税价", mattress.nottax_dept_cost);
  1029. #endregion
  1030. #region 底价
  1031. decimal _dijia = CalculateVarFormula("底价").DecimalValue;
  1032. AddKeyValue("底价", _dijia);
  1033. #endregion
  1034. #region 佣金
  1035. decimal _yongjin = CalculateVarFormula("佣金").DecimalValue;
  1036. AddKeyValue("佣金", _yongjin);
  1037. #endregion
  1038. #region 税金
  1039. mattress.taxes = CalculateVarFormula("税金").DecimalValue;
  1040. AddKeyValue("税金", mattress.taxes);
  1041. #endregion
  1042. #region 部门含税价=(部门不含税价+佣金*0.06)*税率*折扣率(fob>0)
  1043. if (mattress.fob == 0) {
  1044. mattress.discount = 1;
  1045. AddKeyValue("折扣率", mattress.discount);
  1046. }
  1047. mattress.dept_cost = CalculateVarFormula("部门含税价").DecimalValue;
  1048. AddKeyValue("部门含税价", mattress.dept_cost);
  1049. #endregion
  1050. #region 外币价= 部门含税价/汇率
  1051. if (mattress.moneyrate > 0)
  1052. {
  1053. if ("标准".Equals(typename)) mattress.foreign_cost_bz = mattress.dept_cost / mattress.moneyrate;
  1054. else if ("大单".Equals(typename)) mattress.foreign_cost_dd = mattress.dept_cost / mattress.moneyrate;
  1055. else if ("散单".Equals(typename)) mattress.foreign_cost = mattress.dept_cost / mattress.moneyrate;
  1056. }
  1057. decimal _waibi = CalculateVarFormula("外币价").DecimalValue;
  1058. AddKeyValue("外币价", _waibi);
  1059. #endregion
  1060. }
  1061. /// <summary>
  1062. /// 保存床垫报价
  1063. /// </summary>
  1064. /// <param name="mattress"></param>
  1065. /// <param name="mxlist"></param>
  1066. /// <param name="subspecs">副规格列表</param>
  1067. public void SaveMattress(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist, List<u_mattress> subspecs = null)
  1068. {
  1069. var fields = "mattressname, deptid, mattresscode, mattresstypeid, mattress_width, mattress_length, mattress_height, packtype, packqty, woodpallettype, total_hr_cost, total_material_cost, fees_dscrp, total_fees_cost, total_cost, taxrate, taxes, commissionrate, commission, fob, profitrate, dept_profitrate, moneyrate, nottax_factory_cost, nottax_dept_cost, foreign_cost, diameter, area, cabinet_type, hrcost, biandaicost, createtime, createby, mattressrelcode, other_rate, flag, dept_profitrate_rangli, profitrate_point, if_moneyrate, discount, if_m_chai, if_z_chai, if_d_chai, if_n_butao, if_w_butao, if_m_wbutao_way, s_cover_qty, z_cover_qty, x_cover_qty, biandai_qty, s_m_cover_qty, z_m_cover_qty, x_m_cover_qty, chaizhuang_point, haimian_point, if_zhedie_type, qr_auditingrep, qr_auditingdate, if_bcp_type, zhizao_amt, foreign_cost_bz, cubage, extras_cost, extras_cost_dscrp, parentid, flag, xd_flag";
  1070. var fieldsMx = "mattressmxid,mattressid,formulaid,formula,replace_formula,if_success,priceunit,shrinkage,mtrlid,price,gram_weight,cloth_width,if_inputqty,qty ,costamt,if_areaprice,thickness,chastr,xu,useqty,useformula,replace_useformula,gydscrp,mattress_width,mattress_length";
  1071. if (mattress.mattressid <= 0)
  1072. {
  1073. //新建
  1074. mattress.createtime = context.opdate;
  1075. mattress.createby = context.tokendata.username;
  1076. mattress.mattressid = BllHelper.GetID(cmd, "u_mattress");
  1077. fields = "mattressid," + fields;
  1078. if (string.IsNullOrEmpty(mattress.mattresscode))
  1079. {
  1080. var mattresstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  1081. DbSqlHelper.SelectOne(cmd, mattresstype, "typecode");
  1082. mattress.mattresscode = $"{mattresstype.typecode}-{context.opdate.ToString("yyyyMMdd")}{(mattress.mattressid % 10000).ToString("D4")}";
  1083. }
  1084. DbSqlHelper.Insert(cmd, "u_mattress", null, mattress, fields);
  1085. }
  1086. else
  1087. {
  1088. // 删除所有明细
  1089. cmd.CommandText = @"DELETE u_mattress_mx_mtrl WHERE mattressid = @mattressid";
  1090. cmd.Parameters.Clear();
  1091. cmd.Parameters.AddWithValue("@mattressid", mattress.mattressid);
  1092. cmd.ExecuteNonQuery();
  1093. DbSqlHelper.Update(cmd, "u_mattress", null, mattress, "mattressid", fields);
  1094. }
  1095. if (mxlist != null && mxlist.Any())
  1096. {
  1097. foreach (var item in mxlist)
  1098. {
  1099. item.mattressid = mattress.mattressid;
  1100. if (item.mattressmxid == null || item.mattressmxid <= 0)
  1101. {
  1102. item.mattressmxid = BllHelper.GetID(cmd, "u_mattress_mx_mtrl");
  1103. }
  1104. DbSqlHelper.Insert(cmd, "u_mattress_mx_mtrl", null, item, fieldsMx);
  1105. }
  1106. }
  1107. // 更新计价历史
  1108. var hisprice = new u_his_price
  1109. {
  1110. bednetid_mattressid = mattress.mattressid,
  1111. typeid = 1,
  1112. cmpdate = context.opdate,
  1113. cmpemp = context.tokendata.username,
  1114. nottax_dept_cost = mattress.nottax_dept_cost,
  1115. dept_cost = mattress.dept_cost,
  1116. foreign_cost = mattress.foreign_cost
  1117. };
  1118. var fieldsHs = "bednetid_mattressid, typeid, cmpdate, cmpemp, nottax_dept_cost, dept_cost, foreign_cost";
  1119. DbSqlHelper.Insert(cmd, "u_his_price", null, hisprice, fieldsHs);
  1120. }
  1121. public u_mattress GetMattress(int mattressid)
  1122. {
  1123. if(mattressid <= 0)
  1124. {
  1125. throw new LJCommonException("查找床垫报价失败,ID错误!");
  1126. }
  1127. var mattress = new u_mattress() { mattressid = mattressid };
  1128. var fields = @"mattressid,mattressname,deptid,mattresstypeid,mattresscode,mattress_width,mattress_length,mattress_height,packtype,packqty,woodpallettype,
  1129. total_hr_cost,total_material_cost,fees_dscrp,total_fees_cost,taxrate,commissionrate,commission,fob,profitrate,profitrate_point,butao_point,chaizhuang_point,
  1130. haimian_point,dannum_rate,hrcost,biandaicost,zhizao_amt,guanli_rate,discount,extras_cost,dept_profitrate,dept_profitrate_rangli,moneyrate,mattressrelcode,
  1131. flag,auditingrep,auditingdate,createtime,createby,total_cost,nottax_factory_cost,nottax_dept_cost,taxes,dept_cost,foreign_cost,foreign_cost_bz,
  1132. nottax_factory_cost/(dept_profitratedept_profitrate_rangli/100)asdijia_cost,xd_flag,xd_auditingrep,xd_auditingdate,qr_flag,qr_auditingrep,
  1133. qr_auditingdate,js1_flag,erp_mtrlcode,erp_cb_updatetime,if_bcp_type,if_zhedie_type,if_w_butao,biandai_qty,other_rate,if_moneyrate,
  1134. if_m_chai,if_z_chai,if_d_chai,if_n_butao,if_m_wbutao_way,s_cover_qty,z_cover_qty,x_cover_qty,s_m_cover_qty,z_m_cover_qty,x_m_cover_qty,cabinet_type,area,diameter,cubage";
  1135. DbSqlHelper.SelectOne(cmd, mattress, fields);
  1136. return mattress;
  1137. }
  1138. public List<u_mattress_mx_mtrl> GetMattressMxMtrl(int mattressid,List<string> extraWhere = null)
  1139. {
  1140. var mxList = new List<u_mattress_mx_mtrl>();
  1141. var outputFields = @"mattressmxid,mattressid,formulaid,formula,mtrlid,price,gram_weight,cloth_width,if_inputqty,qty,costamt,if_15strip,if_success,shrinkage,replace_formula,priceunit,if_areaprice,thickness,chastr,dv_dscrp,xu,useqty,useformula,replace_useformula,gydscrp,cw_erpmtrlcode,erp_mtrlid,formulakind,formulatype,mtrlname";
  1142. var selectStr = @"SELECT u_mattress_mx_mtrl.mattressmxid
  1143. ,u_mattress_mx_mtrl.mattressid
  1144. ,u_mattress_mx_mtrl.formulaid
  1145. ,u_mattress_mx_mtrl.formula
  1146. ,u_mattress_mx_mtrl.mtrlid
  1147. ,u_mattress_mx_mtrl.price
  1148. ,u_mattress_mx_mtrl.gram_weight
  1149. ,u_mattress_mx_mtrl.cloth_width
  1150. ,u_mattress_mx_mtrl.if_inputqty
  1151. ,u_mattress_mx_mtrl.qty
  1152. ,u_mattress_mx_mtrl.if_15strip
  1153. ,u_mattress_mx_mtrl.costamt
  1154. ,u_mattress_mx_mtrl.if_success
  1155. ,u_mattress_mx_mtrl.shrinkage
  1156. ,u_mattress_mx_mtrl.replace_formula
  1157. ,u_mattress_mx_mtrl.if_areaprice
  1158. ,u_mattress_mx_mtrl.priceunit
  1159. ,u_mattress_mx_mtrl.thickness
  1160. ,u_mattress_mx_mtrl.chastr
  1161. ,u_mattress_mx_mtrl.xu
  1162. ,u_mattress_mx_mtrl.dv_dscrp
  1163. ,u_mattress_mx_mtrl.useqty
  1164. ,u_mattress_mx_mtrl.useformula
  1165. ,u_mattress_mx_mtrl.replace_useformula
  1166. ,u_mattress_mx_mtrl.gydscrp
  1167. ,u_mattress_mx_mtrl.cw_erpmtrlcode
  1168. ,u_mattress_mx_mtrl.erp_mtrlid
  1169. ,u_mattress_formula.formulakind
  1170. ,u_mattress_formula.formulatype
  1171. ,u_mtrl_price.name AS mtrlname
  1172. FROM u_mattress_mx_mtrl
  1173. LEFT OUTER JOIN u_mattress_formula ON u_mattress_formula.formulaid = u_mattress_mx_mtrl.formulaid
  1174. LEFT OUTER JOIN u_mtrl_price ON u_mattress_mx_mtrl.mtrlid = u_mtrl_price.mtrlid";
  1175. var whereList = new List<string>();
  1176. whereList.Add("u_mattress_mx_mtrl.mattressid = @mattressid");
  1177. if(extraWhere != null && extraWhere.Any())
  1178. {
  1179. whereList = whereList.Concat(extraWhere).ToList();
  1180. }
  1181. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), new Dictionary<string, object>() { { "@mattressid", mattressid } }, "mattressmxid", outputFields, 0, 0, mxList);
  1182. return mxList;
  1183. }
  1184. /// <summary>
  1185. /// 获取副规格列表
  1186. /// </summary>
  1187. /// <param name="mattressid"></param>
  1188. /// <returns></returns>
  1189. public List<u_mattress> GetMattressSubspecs(int mattressid)
  1190. {
  1191. var list = new List<u_mattress>();
  1192. var outputFields = @"mattressid,mattress_width,mattress_length,mattress_height,parentid";
  1193. var selectStr = @"SELECT
  1194. mattressid,
  1195. mattress_width,
  1196. mattress_length,
  1197. mattress_height,
  1198. parentid
  1199. FROM u_mattress
  1200. ";
  1201. var whereList = new List<string>();
  1202. whereList.Add("u_mattress.parentid = @mattressid");
  1203. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), new Dictionary<string, object>() { { "@mattressid", mattressid } }, "mattressid", outputFields, 0, 0, list);
  1204. return list;
  1205. }
  1206. /// <summary>
  1207. /// 保存床垫复制清单
  1208. /// </summary>
  1209. /// <param name="mattressid"></param>
  1210. public void CopyMattressInterface(int cpoyid,int mattressid)
  1211. {
  1212. cmd.CommandText = @"DELETE u_mattress_interface WHERE mattressid = @mattressid";
  1213. cmd.Parameters.Clear();
  1214. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1215. cmd.ExecuteNonQuery();
  1216. cmd.CommandText = @"DELETE u_mattress_interface_qd WHERE mattressid = @mattressid";
  1217. cmd.Parameters.Clear();
  1218. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1219. cmd.ExecuteNonQuery();
  1220. cmd.CommandText = @"INSERT INTO u_mattress_interface (
  1221. mattressid
  1222. ,printid
  1223. ,itemname
  1224. ,bj_pzname
  1225. ,bj_namemx
  1226. ,bj_inputtype
  1227. ,actual_size
  1228. ,sb_craft
  1229. ,actual_size_sb
  1230. ,erp_pzid
  1231. ,ss_rate
  1232. ,ls_rate
  1233. )
  1234. SELECT @mattressid
  1235. ,printid
  1236. ,itemname
  1237. ,bj_pzname
  1238. ,bj_namemx
  1239. ,bj_inputtype
  1240. ,actual_size
  1241. ,sb_craft
  1242. ,actual_size_sb
  1243. ,erp_pzid
  1244. ,ss_rate
  1245. ,ls_rate
  1246. FROM u_mattress_interface
  1247. WHERE mattressid = @copy_id";
  1248. cmd.Parameters.Clear();
  1249. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1250. cmd.Parameters.AddWithValue("@copy_id", cpoyid);
  1251. cmd.ExecuteNonQuery();
  1252. cmd.CommandText = @"INSERT INTO u_mattress_interface_qd (
  1253. mattressid
  1254. ,printid
  1255. ,itemname
  1256. ,bj_pzname
  1257. ,bj_pzname_mx
  1258. ,mtrlid
  1259. ,erp_mtrlid
  1260. ,wrkgrpid
  1261. ,useqty
  1262. ,dscrp
  1263. ,actual_useqty
  1264. ,qd_actual_size
  1265. ,qd_pfgroupqty
  1266. ,formulaid
  1267. )
  1268. SELECT @mattressid
  1269. ,printid
  1270. ,itemname
  1271. ,bj_pzname
  1272. ,bj_pzname_mx
  1273. ,mtrlid
  1274. ,erp_mtrlid
  1275. ,wrkgrpid
  1276. ,useqty
  1277. ,dscrp
  1278. ,actual_useqty
  1279. ,qd_actual_size
  1280. ,qd_pfgroupqty
  1281. ,formulaid
  1282. FROM u_mattress_interface_qd
  1283. WHERE mattressid = @copy_id";
  1284. cmd.Parameters.Clear();
  1285. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1286. cmd.Parameters.AddWithValue("@copy_id", cpoyid);
  1287. cmd.ExecuteNonQuery();
  1288. }
  1289. public void CopyMattress(List<u_mattress> mattressList,int gwidth,int glength)
  1290. {
  1291. var bedNetHelpe = HelperBase.GetHelper<BedNetHelper>(cmd);
  1292. foreach(var mattress in mattressList)
  1293. {
  1294. var mattress_copy = GetMattress(mattress.mattressid);
  1295. var mxlist = new List<u_mattress_mx_mtrl>();
  1296. if (mattress.mattress_length > 0 && mattress.mattress_width > 0)
  1297. {
  1298. cmd.CommandText = @"SELECT u_mattress_mx_mtrl.mtrlid
  1299. FROM u_mattress_mx_mtrl
  1300. INNER JOIN u_mattress_formula ON u_mattress_formula.formulaid = u_mattress_mx_mtrl.formulaid
  1301. WHERE u_mattress_formula.formulakind = 99
  1302. AND u_mattress_mx_mtrl.mattressid = @mattressid
  1303. AND u_mattress_mx_mtrl.mtrlid > 0";
  1304. cmd.Parameters.Clear();
  1305. cmd.Parameters.AddWithValue("@mattressid", mattress.mattressid);
  1306. using(var reader = cmd.ExecuteReader())
  1307. {
  1308. while (reader.Read())
  1309. {
  1310. mxlist.Add(new u_mattress_mx_mtrl() { copty_mtrlid = Convert.ToInt32(reader["mtrlid"]) });
  1311. }
  1312. }
  1313. foreach(var mx in mxlist)
  1314. {
  1315. var bednet = new u_bednet() { bednetid = mx.copty_mtrlid, mattress_width = mattress.mattress_width ,mattress_length = mattress.mattress_length };
  1316. var bednetMx = new u_bednetmx() { spring_qty_width = gwidth, spring_qty_length = glength };
  1317. bedNetHelpe.CopyBedNet(bednet, bednetMx, out int newMtrlid);
  1318. mx.mtrlid = newMtrlid;
  1319. }
  1320. }
  1321. // 判断copy_bedNet.bednetcode是否存在@@字符串,如果存在,则删除@@后面的字符串,包括@@
  1322. if (mattress_copy.mattresscode.IndexOf("@@") > -1)
  1323. {
  1324. mattress_copy.mattresscode = mattress_copy.mattresscode.Substring(mattress_copy.mattresscode.IndexOf("@@"));
  1325. }
  1326. mattress_copy.mattresscode += " @@";
  1327. mattress_copy.mattresscode += DateTime.Now.ToString("yyyMMdd_mmhhss");
  1328. if (mattress_copy.mattressrelcode.IndexOf("@@") > -1)
  1329. {
  1330. mattress_copy.mattressrelcode = mattress_copy.mattressrelcode.Substring(mattress_copy.mattressrelcode.IndexOf("@@"));
  1331. }
  1332. mattress_copy.mattressrelcode += " @@";
  1333. mattress_copy.mattressrelcode += DateTime.Now.ToString("yyyMMdd_mmhhss");
  1334. mattress_copy.mattressid = 0;
  1335. mattress_copy.mattress_width = mattress.mattress_width > 0 ? mattress.mattress_width : mattress_copy.mattress_width;
  1336. mattress_copy.mattress_length = mattress.mattress_length > 0 ? mattress.mattress_length : mattress_copy.mattress_length;
  1337. if (!"维持原状".Equals(mattress.area)) mattress_copy.area = mattress.area;
  1338. if (!"维持原状".Equals(mattress.cabinet_type)) mattress_copy.cabinet_type = mattress.cabinet_type;
  1339. mattress_copy.js1_flag = 0;
  1340. mattress_copy.js1_auditingrep = string.Empty;
  1341. mattress_copy.js1_auditingdate = null;
  1342. mattress_copy.xd_flag = 0;
  1343. var mattressMxMtrlList = GetMattressMxMtrl(mattress.mattressid);
  1344. foreach(var mx in mattressMxMtrlList)
  1345. {
  1346. mx.mattressid = 0;
  1347. mx.mattressmxid = 0;
  1348. }
  1349. SaveMattress(mattress_copy, mattressMxMtrlList);
  1350. var mattressSubspecs = GetMattressSubspecs(mattress.mattressid);
  1351. foreach (var sub in mattressSubspecs)
  1352. {
  1353. sub.parentid = mattress_copy.mattressid;
  1354. sub.mattressid = 0;
  1355. sub.mattresscode = "";
  1356. var mattressMxList = GetMattressMxMtrl(sub.mattressid);
  1357. foreach (var submx in mattressMxList)
  1358. {
  1359. submx.mattressid = 0;
  1360. submx.mattressmxid = 0;
  1361. }
  1362. SaveMattress(sub, mattressMxList);
  1363. }
  1364. CopyMattressInterface(mattress_copy.mattressid, mattress.mattressid);
  1365. foreach (var mx in mxlist)
  1366. {
  1367. cmd.CommandText = @"UPDATE u_mattress_mx_mtrl
  1368. SET mtrlid = @new_mtrlid
  1369. FROM u_mattress_mx_mtrl
  1370. INNER JOIN u_mattress_formula ON u_mattress_formula.formulaid = u_mattress_mx_mtrl.formulaid
  1371. WHERE u_mattress_formula.formulakind = 99
  1372. AND u_mattress_mx_mtrl.mattressid = @new_mattressid
  1373. AND u_mattress_mx_mtrl.mtrlid = @old_mtrld";
  1374. cmd.Parameters.Clear();
  1375. cmd.Parameters.AddWithValue("@new_mtrlid", mx.mtrlid);
  1376. cmd.Parameters.AddWithValue("@new_mattressid", mattress_copy.mattressid);
  1377. cmd.Parameters.AddWithValue("@old_mtrld", mx.copty_mtrlid);
  1378. cmd.ExecuteNonQuery();
  1379. }
  1380. }
  1381. }
  1382. public void SaveMattressInterface(int mattressid, List<u_mattress_interface> mxlist,List<u_mattress_interface_qd> qdlist)
  1383. {
  1384. cmd.CommandText = @"DELETE u_mattress_interface WHERE mattressid = @mattressid";
  1385. cmd.Parameters.Clear();
  1386. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1387. cmd.ExecuteNonQuery();
  1388. cmd.CommandText = @"DELETE u_mattress_interface_qd WHERE mattressid = @mattressid";
  1389. cmd.Parameters.Clear();
  1390. cmd.Parameters.AddWithValue("@mattressid", mattressid);
  1391. cmd.ExecuteNonQuery();
  1392. foreach (var mx in mxlist) {
  1393. mx.mattressid = mattressid;
  1394. DbSqlHelper.Insert(cmd, mx, "mattressid,printid,itemname,bj_pzname,bj_namemx,actual_size,sb_craft,actual_size_sb,erp_pzid,bj_inputtype,ss_rate,ls_rate");
  1395. }
  1396. foreach (var mx in qdlist)
  1397. {
  1398. mx.mattressid = mattressid;
  1399. DbSqlHelper.Insert(cmd, mx, "mattressid,printid,itemname,bj_pzname,bj_pzname_mx,bj_pzname_mx_mx,mtrlid,erp_mtrlid,wrkgrpid,useqty,dscrp,actual_useqty,qd_actual_size,qd_pfgroupqty,ss_rate,ls_rate,sh_rate,formulaid");
  1400. }
  1401. AutoUpdateBcp(mattressid);
  1402. }
  1403. #region 床垫接口相关方法
  1404. /// <summary>
  1405. /// 业务审核
  1406. /// </summary>
  1407. /// <param name="mattressid"></param>
  1408. /// <exception cref="LJCommonException"></exception>
  1409. public void MattressYWAudit(int mattressid)
  1410. {
  1411. var mattress = new u_mattress() { mattressid = mattressid };
  1412. DbSqlHelper.SelectOne(cmd, mattress, "yw_flag");
  1413. if(mattress.yw_flag == 1)
  1414. {
  1415. throw new LJCommonException("床垫已业务审核,不能业务审核!");
  1416. }
  1417. mattress.yw_flag = 1;
  1418. mattress.yw_auditingdate = context.opdate;
  1419. mattress.yw_auditingrep = context.tokendata.username;
  1420. DbSqlHelper.Update(cmd, mattress, "yw_flag,yw_auditingdate,yw_auditingrep");
  1421. }
  1422. /// <summary>
  1423. /// 自动更新半成品部件
  1424. /// </summary>
  1425. /// <param name="mattressid"></param>
  1426. public void AutoUpdateBcp(int mattressid)
  1427. {
  1428. var qdList = new List<u_mattress_interface_qd>();
  1429. var selectStr = @"
  1430. SELECT u_mattress_interface_qd.mattressid
  1431. ,u_mattress_interface_qd.printid
  1432. ,u_mattress_interface_qd.itemname
  1433. ,u_mattress_interface_qd.bj_pzname
  1434. ,u_mattress_interface_qd.bj_pzname_mx
  1435. ,u_mattress_interface_qd.bj_pzname_mx_mx
  1436. ,u_mattress_interface_qd.mtrlid
  1437. ,u_mattress_interface_qd.erp_mtrlid
  1438. ,u_mattress_interface_qd.useqty
  1439. ,u_mattress_interface_qd.dscrp
  1440. ,u_mattress_interface_qd.actual_useqty
  1441. ,u_mattress_interface_qd.qd_actual_size
  1442. ,u_mattress_interface_qd.qd_pfgroupqty
  1443. ,u_mattress_interface_qd.wrkgrpid
  1444. ,u_mattress_interface_qd.ss_rate
  1445. ,u_mattress_interface_qd.ls_rate
  1446. ,u_mattress_interface_qd.sh_rate
  1447. ,u_mattress_interface_qd.formulaid
  1448. FROM u_mattress_interface_qd
  1449. ";
  1450. var whereList = new List<string>();
  1451. whereList.Add("mattressid = @mattressid");
  1452. var param = new Dictionary<string, object>();
  1453. param.Add("@mattressid", mattressid);
  1454. var outFields = "mattressid, printid, itemname, bj_pzname, bj_pzname_mx, bj_pzname_mx_mx, mtrlid, erp_mtrlid, useqty, dscrp, actual_useqty, qd_actual_size, qd_pfgroupqty, wrkgrpid, ss_rate, ls_rate, sh_rate, formulaid";
  1455. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "printid", outFields, 0, 0, qdList);
  1456. var chastrArr = new List<string>() { "裥面", "裥底", "顶布裥棉", "大侧", "小侧", "V侧" };
  1457. var chastrType = new Dictionary<string, int>();
  1458. Dictionary<string, List<u_mattress_interface_qd>> bcpQdlist = new Dictionary<string, List<u_mattress_interface_qd>>();
  1459. foreach (var mx in qdList)
  1460. {
  1461. if (chastrArr.Contains(mx.itemname) && !string.IsNullOrEmpty(mx.bj_pzname_mx_mx))
  1462. {
  1463. if (!chastrType.ContainsKey(mx.bj_pzname_mx_mx))
  1464. {
  1465. switch (mx.itemname)
  1466. {
  1467. case "裥面":
  1468. chastrType.Add(mx.bj_pzname_mx_mx, 901);
  1469. mx.billtype = 901;
  1470. break;
  1471. case "裥底":
  1472. chastrType.Add(mx.bj_pzname_mx_mx, 902);
  1473. mx.billtype = 902;
  1474. break;
  1475. case "大侧":
  1476. chastrType.Add(mx.bj_pzname_mx_mx, 903);
  1477. mx.billtype = 903;
  1478. break;
  1479. case "小侧":
  1480. chastrType.Add(mx.bj_pzname_mx_mx, 904);
  1481. mx.billtype = 904;
  1482. break;
  1483. case "V侧":
  1484. chastrType.Add(mx.bj_pzname_mx_mx, 905);
  1485. mx.billtype = 905;
  1486. break;
  1487. }
  1488. }
  1489. if (!bcpQdlist.ContainsKey(mx.bj_pzname_mx_mx))
  1490. {
  1491. List<u_mattress_interface_qd> list = new List<u_mattress_interface_qd>();
  1492. list.Add(mx);
  1493. bcpQdlist.Add(mx.bj_pzname_mx_mx, list);
  1494. }
  1495. else
  1496. {
  1497. bcpQdlist[mx.bj_pzname_mx_mx].Add(mx);
  1498. }
  1499. }
  1500. }
  1501. foreach (var mx in bcpQdlist)
  1502. {
  1503. var billid = 0;
  1504. cmd.CommandText = @"SELECT TOP 1 billid
  1505. FROM u_semi_finished_product
  1506. WHERE semi_finished_name = @semi_finished_name";
  1507. cmd.Parameters.Clear();
  1508. cmd.Parameters.AddWithValue("@semi_finished_name", mx.Key);
  1509. using (var reader = cmd.ExecuteReader())
  1510. {
  1511. if (reader.Read())
  1512. {
  1513. billid = Convert.ToInt32(reader["billid"]);
  1514. }
  1515. }
  1516. if (billid == 0)
  1517. {
  1518. //创建
  1519. var bcpMattress = new u_semi_finished_product() { billid = 0 };
  1520. AutoInit.AutoInitS(cmd, bcpMattress);
  1521. bcpMattress.billid = 0;
  1522. bcpMattress.billdate = DateTime.Now;
  1523. bcpMattress.dscrp = "自动生成,来自mattressid:" + mattressid;
  1524. //bcpMattress.production_size = DateTime.Now;
  1525. //bcpMattress.hemming_process = DateTime.Now;
  1526. //bcpMattress.hemmed_size = DateTime.Now;
  1527. //bcpMattress.shrinkage_rate = DateTime.Now;
  1528. //bcpMattress.elongation_rate = DateTime.Now;
  1529. bcpMattress.opemp = context.tokendata.username;
  1530. bcpMattress.opdate = DateTime.Now;
  1531. bcpMattress.semi_finished_type = chastrType[mx.Key];
  1532. bcpMattress.semi_finished_code = mx.Key;
  1533. bcpMattress.semi_finished_name = mx.Key;
  1534. var bcpMxList = new List<u_semi_finished_product_mx>();
  1535. foreach (var qdmx in mx.Value)
  1536. {
  1537. var bcpmx = new u_semi_finished_product_mx()
  1538. {
  1539. mtrlid = qdmx.mtrlid.Value,
  1540. erp_mtrlid = qdmx.erp_mtrlid,
  1541. formulaid = qdmx.formulaid
  1542. };
  1543. bcpMxList.Add(bcpmx);
  1544. }
  1545. bcpMattress.mxlist = bcpMxList;
  1546. SaveMattressBcp(bcpMattress);
  1547. }
  1548. else
  1549. {
  1550. // 判断不一样,就覆盖
  1551. selectStr = @"
  1552. SELECT u_semi_finished_product_mx.billid
  1553. ,u_semi_finished_product_mx.printid
  1554. ,u_semi_finished_product_mx.mtrlid
  1555. ,u_semi_finished_product_mx.formulaid
  1556. ,u_semi_finished_product_mx.chastr
  1557. ,u_semi_finished_product_mx.thickness
  1558. ,u_semi_finished_product_mx.qty
  1559. ,u_semi_finished_product_mx.erp_mtrlid
  1560. ,u_semi_finished_product_mx.usenum
  1561. ,u_semi_finished_product_mx.usedenom
  1562. FROM u_semi_finished_product_mx
  1563. ";
  1564. var bcpMxList = new List<u_semi_finished_product_mx>();
  1565. whereList = new List<string>();
  1566. whereList.Add("u_semi_finished_product_mx.billid = @billid");
  1567. param.Clear();
  1568. param.Add("@billid", billid);
  1569. outFields = "billid, printid, mtrlid, formulaid, chastr, thickness, qty, erp_mtrlid, usenum, usedenom, mtrlname, if_mtrl, formula, formulatype, formulakind";
  1570. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "formulatype, sortcode", outFields, 0, 0, bcpMxList);
  1571. var ErpMtrlids = "";
  1572. var BcpErpMtrlids = "";
  1573. foreach (var qdmx in mx.Value)
  1574. {
  1575. ErpMtrlids += qdmx.erp_mtrlid + "+";
  1576. }
  1577. foreach (var qdmx in bcpMxList)
  1578. {
  1579. BcpErpMtrlids += qdmx.erp_mtrlid + "+";
  1580. }
  1581. if (ErpMtrlids != BcpErpMtrlids)
  1582. {
  1583. var bcpMattress = new u_semi_finished_product() { billid = billid };
  1584. DbSqlHelper.SelectOne(cmd, bcpMattress, "billid, billdate, flag, semi_finished_type, semi_finished_code, semi_finished_name, dscrp, production_size, hemming_process, hemmed_size, shrinkage_rate, elongation_rate, opemp, opdate, auditemp, auditdate");
  1585. bcpMattress.billid = billid;
  1586. bcpMattress.auditemp = context.tokendata.username;
  1587. bcpMattress.auditdate = DateTime.Now;
  1588. foreach (var qdmx in bcpMxList)
  1589. {
  1590. var mtrlid = 0;
  1591. var arr = mx.Value.Where((itm) => itm.mtrlid == qdmx.mtrlid).ToList();
  1592. if (arr.Count > 0)
  1593. {
  1594. mtrlid = arr[0].erp_mtrlid.Value;
  1595. }
  1596. qdmx.erp_mtrlid = mtrlid;
  1597. qdmx.formulaid = arr[0].formulaid;
  1598. }
  1599. bcpMattress.mxlist = bcpMxList;
  1600. SaveMattressBcp(bcpMattress);
  1601. }
  1602. }
  1603. }
  1604. }
  1605. /// <summary>
  1606. /// 业务撤审
  1607. /// </summary>
  1608. /// <param name="mattressid"></param>
  1609. /// <exception cref="LJCommonException"></exception>
  1610. public void MattressYWCancelAudit(int mattressid)
  1611. {
  1612. var mattress = new u_mattress() { mattressid = mattressid };
  1613. DbSqlHelper.SelectOne(cmd, mattress, "yw_flag,js1_flag");
  1614. if (mattress.js1_flag == 1)
  1615. {
  1616. throw new LJCommonException("床垫已技术1审核,不能业务撤审!");
  1617. }
  1618. if (mattress.yw_flag == 0)
  1619. {
  1620. throw new LJCommonException("床垫未业务审核,不能业务撤审!");
  1621. }
  1622. mattress.yw_flag = 0;
  1623. mattress.yw_auditingdate = null;
  1624. mattress.yw_auditingrep = string.Empty;
  1625. DbSqlHelper.Update(cmd, mattress, "yw_flag,yw_auditingdate,yw_auditingrep");
  1626. }
  1627. /// <summary>
  1628. /// 产品补充审核
  1629. /// </summary>
  1630. /// <param name="mattressid"></param>
  1631. /// <exception cref="LJCommonException"></exception>
  1632. public void MattressJSAudit(int mattressid)
  1633. {
  1634. var mattress = new u_mattress() { mattressid = mattressid };
  1635. DbSqlHelper.SelectOne(cmd, mattress, "yw_flag,js1_flag");
  1636. if (mattress.js1_flag == 1)
  1637. {
  1638. throw new LJCommonException("床垫已技术1审核,不能技术1审核!");
  1639. }
  1640. if (mattress.yw_flag == 0)
  1641. {
  1642. throw new LJCommonException("床垫未业务审核,不能技术1审核!");
  1643. }
  1644. mattress.js1_flag = 1;
  1645. mattress.js1_auditingdate = context.opdate;
  1646. mattress.js1_auditingrep = context.tokendata.username;
  1647. DbSqlHelper.Update(cmd, mattress, "js1_flag,js1_auditingdate,js1_auditingrep");
  1648. }
  1649. /// <summary>
  1650. /// 产品补充撤审
  1651. /// </summary>
  1652. /// <param name="mattressid"></param>
  1653. /// <exception cref="LJCommonException"></exception>
  1654. public void MattressJSCancelAudit(int mattressid)
  1655. {
  1656. var mattress = new u_mattress() { mattressid = mattressid };
  1657. DbSqlHelper.SelectOne(cmd, mattress, "yw_flag,js1_flag,creatmtrl_flag");
  1658. if (mattress.creatmtrl_flag == 1)
  1659. {
  1660. throw new LJCommonException("床垫已生成产品,不能技术1撤审!");
  1661. }
  1662. if (mattress.js1_flag == 0)
  1663. {
  1664. throw new LJCommonException("床垫未技术1撤审,不能技术1撤审!");
  1665. }
  1666. mattress.js1_flag = 0;
  1667. mattress.js1_auditingdate = null;
  1668. mattress.js1_auditingrep = string.Empty;
  1669. DbSqlHelper.Update(cmd, mattress, "js1_flag,js1_auditingdate,js1_auditingrep");
  1670. }
  1671. /// <summary>
  1672. /// 清单补充审核
  1673. /// </summary>
  1674. /// <param name="mattressid"></param>
  1675. /// <exception cref="LJCommonException"></exception>
  1676. public void MattressJS2Audit(int mattressid)
  1677. {
  1678. var mattress = new u_mattress() { mattressid = mattressid };
  1679. DbSqlHelper.SelectOne(cmd, mattress, "js2_flag");
  1680. if (mattress.js2_flag == 1)
  1681. {
  1682. throw new LJCommonException("床垫已技术2审核,不能技术2审核!");
  1683. }
  1684. mattress.js2_flag = 1;
  1685. mattress.js2_auditingdate = context.opdate;
  1686. mattress.js2_auditingrep = context.tokendata.username;
  1687. DbSqlHelper.Update(cmd, mattress, "js2_flag,js2_auditingdate,js2_auditingrep");
  1688. }
  1689. /// <summary>
  1690. /// 清单补充撤审
  1691. /// </summary>
  1692. /// <param name="mattressid"></param>
  1693. /// <exception cref="LJCommonException"></exception>
  1694. public void MattressJS2CancelAudit(int mattressid)
  1695. {
  1696. var mattress = new u_mattress() { mattressid = mattressid };
  1697. DbSqlHelper.SelectOne(cmd, mattress, "js2_flag");
  1698. if (mattress.js2_flag == 0)
  1699. {
  1700. throw new LJCommonException("床垫未技术2审核,不能技术2撤审!");
  1701. }
  1702. mattress.js2_flag = 0;
  1703. mattress.js2_auditingdate = null;
  1704. mattress.js2_auditingrep = string.Empty;
  1705. DbSqlHelper.Update(cmd, mattress, "js2_flag,js2_auditingdate,js2_auditingrep");
  1706. }
  1707. /// <summary>
  1708. /// 配对ERP成品
  1709. /// </summary>
  1710. /// <param name="mattress"></param>
  1711. /// <param name="mxlist"></param>
  1712. /// <exception cref="LJCommonException"></exception>
  1713. public void MatchERPMtrldef(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  1714. {
  1715. if(mattress.mattressid <= 0)
  1716. {
  1717. throw new LJCommonException("床垫id有误,请检查!");
  1718. }
  1719. DbSqlHelper.Update(cmd, mattress, "erp_mtrlid,erp_mtrlcode");
  1720. foreach(var mx in mxlist)
  1721. {
  1722. DbSqlHelper.Update(cmd, mx, "erp_mtrlid");
  1723. }
  1724. }
  1725. #endregion
  1726. #region 公式计算通用方法
  1727. private void AddKeyValue(string key, object value, bool isReplace = true)
  1728. {
  1729. key = $"【{key}】";
  1730. if (!replacements.ContainsKey(key))
  1731. {
  1732. replacements.Add(key, value);
  1733. }
  1734. else if (isReplace)
  1735. {
  1736. replacements[key] = value;
  1737. }
  1738. }
  1739. private void AddFormulaKeyValue(string key, string value, bool isReplace = true)
  1740. {
  1741. key = $"【{key}】";
  1742. if (!formula_replacements.ContainsKey(key))
  1743. {
  1744. formula_replacements.Add(key, value);
  1745. }
  1746. else if (isReplace)
  1747. {
  1748. formula_replacements[key] = value;
  1749. }
  1750. }
  1751. /// <summary>
  1752. /// 记录隐藏公式
  1753. /// </summary>
  1754. /// <param name="key"></param>
  1755. /// <param name="value"></param>
  1756. /// <param name="isReplace"></param>
  1757. private void AddFormulaHideKeyValue(string key, string value, bool isReplace = true)
  1758. {
  1759. if (!formula_replacements_hide.ContainsKey(key))
  1760. {
  1761. formula_replacements_hide.Add(key, value);
  1762. }
  1763. else if (isReplace)
  1764. {
  1765. formula_replacements_hide[key] = value;
  1766. }
  1767. }
  1768. private string ConvertToEnglishSymbols(string input)
  1769. {
  1770. input = input.Replace("(", "(")
  1771. .Replace(")", ")")
  1772. .Replace(",", ",")
  1773. .Replace("。", ".")
  1774. .Replace(":", ":")
  1775. .Replace(";", ";")
  1776. .Replace("“", "\"")
  1777. .Replace("”", "\"")
  1778. .Replace("‘", "'")
  1779. .Replace("’", "'");
  1780. return input;
  1781. }
  1782. /// <summary>
  1783. /// 变量公式计算
  1784. /// </summary>
  1785. /// <param name="varname"></param>
  1786. /// <returns></returns>
  1787. private TData CalculateVarFormula(string varname)
  1788. {
  1789. varname = $"【{varname}】";
  1790. if (!formula_replacements.ContainsKey(varname)) return null;
  1791. return Calculate(formula_replacements[varname], varname);
  1792. }
  1793. private void replaceFormulas(ref string expression)
  1794. {
  1795. bool hasReplace = false;
  1796. foreach (var replacement in formula_replacements)
  1797. {
  1798. if (expression.IndexOf(replacement.Key) > -1)
  1799. {
  1800. expression = expression.Replace(replacement.Key, "(" + replacement.Value + ")");
  1801. hasReplace = true;
  1802. }
  1803. }
  1804. if (hasReplace)
  1805. {
  1806. replaceFormulas(ref expression);
  1807. }
  1808. }
  1809. private void FormatExpression(ref string expression)
  1810. {
  1811. // 替换相互嵌套的公式
  1812. replaceFormulas(ref expression);
  1813. expression = ConvertToEnglishSymbols(expression);
  1814. // 定义正则表达式模式,匹配包含【】的内容
  1815. string pattern = @"【(.*?)】";
  1816. // 创建正则表达式对象
  1817. Regex regex = new Regex(pattern);
  1818. // 使用正则表达式匹配输入字符串
  1819. MatchCollection matches = regex.Matches(expression);
  1820. var new_replacements = new Dictionary<string, object>();
  1821. foreach (Match match in matches)
  1822. {
  1823. if (replacements.TryGetValue(match.Value, out object value2))
  1824. {
  1825. new_replacements[match.Value] = value2;
  1826. }
  1827. }
  1828. foreach (var replacement in new_replacements)
  1829. {
  1830. expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  1831. }
  1832. }
  1833. /// <summary>
  1834. /// 公式计算
  1835. /// </summary>
  1836. /// <param name="expression"></param>
  1837. /// <param name="name"></param>
  1838. /// <returns></returns>
  1839. private TData Calculate(string expression, string name = "")
  1840. {
  1841. FormatExpression(ref expression);
  1842. try
  1843. {
  1844. return LJExprParser.Parse(expression).Result;
  1845. }
  1846. catch (Exception ex)
  1847. {
  1848. throw new LJCommonException($"计算{name}公式错误: {ex.Message}");
  1849. }
  1850. }
  1851. /// <summary>
  1852. /// Sql公式计算
  1853. /// </summary>
  1854. /// <param name="expression"></param>
  1855. /// <param name="name"></param>
  1856. /// <returns></returns>
  1857. private object SqlCalculate(string expression, string name = "")
  1858. {
  1859. FormatExpression(ref expression);
  1860. try
  1861. {
  1862. cmd.CommandText = $@"{expression}";
  1863. cmd.Parameters.Clear();
  1864. return cmd.ExecuteScalar();
  1865. }
  1866. catch (Exception ex)
  1867. {
  1868. throw new LJCommonException($"计算{name}公式错误: {ex.Message}");
  1869. }
  1870. }
  1871. #endregion
  1872. #region 导入包装明细
  1873. /// <summary>
  1874. /// 导入 2-按包装方式 的公式
  1875. /// </summary>
  1876. /// <param name="packtype">包装方式</param>
  1877. /// <param name="dw_2">结果导出</param>
  1878. public void wf_import_bz(int packtype, ref List<u_mattress_mx_mtrl> dw_2)
  1879. {
  1880. var selectStr = @"
  1881. SELECT formulaid
  1882. ,formulakind
  1883. ,formulatype
  1884. ,sortcode
  1885. ,if_mtrl
  1886. ,formula
  1887. ,usetype
  1888. ,if_packtype0
  1889. ,if_packtype1
  1890. ,if_packtype2
  1891. ,default_mtrlid
  1892. ,createtime
  1893. ,createby
  1894. ,useformula
  1895. ,gydscrp
  1896. ,mtrltype
  1897. FROM u_mattress_formula";
  1898. var outFields = "formulaid, formulakind, formulatype, sortcode, if_mtrl, formula, usetype, if_packtype0, if_packtype1, if_packtype2, default_mtrlid, createtime, createby, useformula, gydscrp, mtrltype";
  1899. var whereList = new List<string>();
  1900. whereList.Add("u_mattress_formula.formulatype IN (0, 1, 2, 3, 99, 104)");
  1901. whereList.Add("u_mattress_formula.usetype = @usetype");
  1902. var param = new Dictionary<string, object>();
  1903. // 导入 2-按包装方式 的公式
  1904. var mattressFormulaList = new List<u_mattress_formula>();
  1905. param.Clear();
  1906. param.Add("@usetype", 2);
  1907. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "formulatype, sortcode", outFields, 0, 0, mattressFormulaList);
  1908. foreach (var mx in mattressFormulaList)
  1909. {
  1910. if (packtype == 0 && mx.if_packtype0 != 1) continue;
  1911. if (packtype == 1 && mx.if_packtype1 != 1) continue;
  1912. if (packtype == 2 && mx.if_packtype2 != 1) continue;
  1913. var newDw2 = new u_mattress_mx_mtrl();
  1914. AutoInit.AutoInitS(newDw2);
  1915. if (mx.if_mtrl > 1)
  1916. {
  1917. newDw2.addmx = "+";
  1918. newDw2.delmx = "-";
  1919. }
  1920. newDw2.formulatype = mx.formulatype;
  1921. newDw2.if_mtrl = mx.if_mtrl;
  1922. newDw2.formulaid = mx.formulaid;
  1923. newDw2.sortcode = mx.sortcode;
  1924. newDw2.formulakind = mx.formulakind;
  1925. newDw2.formula = mx.formula;
  1926. newDw2.useformula = mx.useformula;
  1927. newDw2.gydscrp = mx.gydscrp;
  1928. dw_2.Add(newDw2);
  1929. }
  1930. }
  1931. #endregion
  1932. #region 明细排序
  1933. /// <summary>
  1934. /// 明细排序
  1935. /// </summary>
  1936. /// <param name="dw_2"></param>
  1937. public void wf_sort_mx(ref List<u_mattress_mx_mtrl> dw_2)
  1938. {
  1939. // u_mattress_formula_formulatype A xu A u_mattress_formula_sortcode A u_mattress_mx_mtrl_formulaid A
  1940. dw_2.Sort((a, b) =>
  1941. {
  1942. if (a.formulatype != b.formulatype)
  1943. {
  1944. return a.formulatype.Value.CompareTo(b.formulatype.Value);
  1945. }
  1946. if (a.xu != b.xu)
  1947. {
  1948. return a.xu.Value.CompareTo(b.xu.Value);
  1949. }
  1950. if (a.sortcode != b.sortcode)
  1951. {
  1952. return a.sortcode.CompareTo(b.sortcode);
  1953. }
  1954. return a.formulaid.Value.CompareTo(b.formulaid.Value);
  1955. });
  1956. }
  1957. #endregion
  1958. #region 获取物料信息
  1959. public void wf_mtrl_fine(List<int> arg_mtrlid, int arg_pricelistid, ref List<u_mtrl_price> mtrlList)
  1960. {
  1961. var selectStr = @"
  1962. SELECT
  1963. u_mtrl_price.mtrlid,
  1964. u_mtrl_price.name,
  1965. isnull(u_mtrl_price_pricelist.price,0) as price,
  1966. u_mtrl_price.gram_weight,
  1967. u_mtrl_price.cloth_width,
  1968. u_mtrl_price.if_inputqty,
  1969. u_mtrl_price.priceunit,
  1970. u_mtrl_price.shrinkage,
  1971. isnull(u_mtrl_price_pricelist.price_formula,0) as price_formula,
  1972. isnull(u_mtrl_price_pricelist.qty_formula,0) as qty_formula,
  1973. u_mtrl_price.if_areaprice,
  1974. u_mtrl_price.thickness
  1975. FROM u_mtrl_price inner join u_mtrl_price_pricelist on
  1976. u_mtrl_price.mtrlid = u_mtrl_price_pricelist.mtrlid";
  1977. var outFields = "mtrlid, name, price, gram_weight, cloth_width, if_inputqty, priceunit, shrinkage, price_formula, qty_formula, if_areaprice, thickness";
  1978. var whereList = new List<string>();
  1979. whereList.Add("u_mtrl_price.mtrlid IN (" + string.Join(",", arg_mtrlid) + ")");
  1980. whereList.Add("u_mtrl_price_pricelist.pricelistid = @arg_pricelistid");
  1981. var param = new Dictionary<string, object>();
  1982. param.Clear();
  1983. param.Add("@arg_pricelistid", arg_pricelistid);
  1984. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "", outFields, 0, 0, mtrlList);
  1985. }
  1986. #endregion
  1987. #region 获取包装默认物料
  1988. /// <summary>
  1989. /// 获取包装默认物料
  1990. /// </summary>
  1991. /// <param name="dw_2"></param>
  1992. public void wf_default_mtrlid(int pricelistid, ref List<u_mattress_mx_mtrl> dw_2)
  1993. {
  1994. // 导入 2-按包装方式 的公式
  1995. var mtrlPriceList = new List<u_mtrl_price>();
  1996. wf_mtrl_fine(new List<int> { 46804, 46809, 46811, 46817, 46872 }, pricelistid, ref mtrlPriceList);
  1997. foreach (var mx in dw_2)
  1998. {
  1999. var _mtrlid = 0;
  2000. if (mx.formulakind == 16)
  2001. {
  2002. //默认 pe kind = 16 默认 10C PE袋 mtrlid 46804
  2003. _mtrlid = 46804;
  2004. }
  2005. else if (mx.formulakind == 17)
  2006. {
  2007. //默认 外层pe kind = 17 默认 20C mtrlid 46809
  2008. _mtrlid = 46809;
  2009. }
  2010. else if (mx.formulakind == 18)
  2011. {
  2012. //默认 pvc kind = 18 默认 15C mtrlid 46811
  2013. _mtrlid = 46811;
  2014. }
  2015. else if (mx.formulakind == 19)
  2016. {
  2017. //默认 卷包外包装 kind = 19 默认 彩箱 32*32 46817
  2018. _mtrlid = 46817;
  2019. }
  2020. else if (mx.formulakind == 27)
  2021. {
  2022. //默认 拉手脚轮 kind = 27 默认 46872
  2023. _mtrlid = 46872;
  2024. }
  2025. var mtrlItm = mtrlPriceList.Where((itm) => itm.mtrlid == _mtrlid).ToList();
  2026. if (mtrlItm.Count > 0)
  2027. {
  2028. mx.mtrlid = mtrlItm[0].mtrlid;
  2029. mx.mtrlname = mtrlItm[0].name;
  2030. mx.price = mtrlItm[0].price;
  2031. mx.gram_weight = mtrlItm[0].gram_weight;
  2032. mx.cloth_width = mtrlItm[0].cloth_width;
  2033. mx.if_inputqty = mtrlItm[0].if_inputqty;
  2034. mx.priceunit = mtrlItm[0].priceunit;
  2035. mx.shrinkage = mtrlItm[0].shrinkage;
  2036. mx.qty = 1;
  2037. if (!string.IsNullOrEmpty(mtrlItm[0].price_formula)) mx.formula = mtrlItm[0].price_formula;
  2038. }
  2039. }
  2040. }
  2041. #endregion
  2042. #region 获取非包装默认物料
  2043. /// <summary>
  2044. /// 获取非包装默认物料
  2045. /// </summary>
  2046. /// <param name="dw_2"></param>
  2047. public void wf_default_mtrlid_notbz(int pricelistid, ref List<u_mattress_mx_mtrl> dw_2)
  2048. {
  2049. foreach (var mx in dw_2)
  2050. {
  2051. var matterssformula = new u_mattress_formula() { formulaid = mx.formulaid };
  2052. if (DbSqlHelper.SelectOne(cmd, matterssformula, "default_mtrlid") != 1 || matterssformula.default_mtrlid.Value == 0) continue;
  2053. // 导入 2-按包装方式 的公式
  2054. var mtrlItm = new List<u_mtrl_price>();
  2055. wf_mtrl_fine(new List<int> { matterssformula.default_mtrlid.Value }, pricelistid, ref mtrlItm);
  2056. if (mtrlItm.Count > 0)
  2057. {
  2058. mx.mtrlid = mtrlItm[0].mtrlid;
  2059. mx.mtrlname = mtrlItm[0].name;
  2060. mx.price = mtrlItm[0].price;
  2061. mx.gram_weight = mtrlItm[0].gram_weight;
  2062. mx.cloth_width = mtrlItm[0].cloth_width;
  2063. mx.if_inputqty = mtrlItm[0].if_inputqty;
  2064. mx.priceunit = mtrlItm[0].priceunit;
  2065. mx.shrinkage = mtrlItm[0].shrinkage;
  2066. mx.qty = 1;
  2067. if (!string.IsNullOrEmpty(mtrlItm[0].price_formula)) mx.formula = mtrlItm[0].price_formula;
  2068. }
  2069. }
  2070. }
  2071. #endregion
  2072. #region 保存半成品部件
  2073. public void SaveMattressBcp(u_semi_finished_product mattress)
  2074. {
  2075. var dtNow = DateTime.Now;
  2076. var fields = @"billdate,semi_finished_type,semi_finished_code,semi_finished_name,dscrp,production_size,hemming_process,
  2077. hemmed_size,shrinkage_rate,elongation_rate,opemp,opdate,auditemp,auditdate";
  2078. var fieldsMx = "billid,printid,mtrlid,formulaid,chastr,thickness,qty,erp_mtrlid,usenum,usedenom";
  2079. if (mattress.billid <= 0)
  2080. {
  2081. //新建
  2082. fields += ",billid,billcode";
  2083. mattress.opdate = dtNow;
  2084. mattress.opemp = context.tokendata.username;
  2085. mattress.billid = BllHelper.GetID(cmd, "u_semi_finished_product");
  2086. switch (mattress.semi_finished_type)
  2087. {
  2088. case 901:
  2089. mattress.billcode = "JM";
  2090. break;
  2091. case 902:
  2092. mattress.billcode = "JD";
  2093. break;
  2094. case 903:
  2095. mattress.billcode = "BC";
  2096. break;
  2097. case 904:
  2098. mattress.billcode = "XC";
  2099. break;
  2100. case 905:
  2101. mattress.billcode = "VC";
  2102. break;
  2103. }
  2104. // 编号-年月日+流水
  2105. mattress.billcode = $"{mattress.billcode}-{dtNow.ToString("yyyyMMdd")}{(mattress.billid.Value % 10000).ToString("D4")}";
  2106. DbSqlHelper.Insert(cmd, "u_semi_finished_product", null, mattress, fields);
  2107. }
  2108. else
  2109. {
  2110. //修改
  2111. cmd.CommandText = @"DELETE FROM u_semi_finished_product_mx WHERE billid = @billid";
  2112. cmd.Parameters.Clear();
  2113. cmd.Parameters.AddWithValue("@billid", mattress.billid);
  2114. cmd.ExecuteNonQuery();
  2115. DbSqlHelper.Update(cmd, "u_semi_finished_product", null, mattress, "billid", fields);
  2116. }
  2117. var cnt = 0;
  2118. foreach (var mx in mattress.mxlist)
  2119. {
  2120. AutoInit.AutoInitS(cmd, mx);
  2121. mx.billid = mattress.billid;
  2122. mx.printid = ++cnt;
  2123. DbSqlHelper.Insert(cmd, "u_semi_finished_product_mx", null, mx, fieldsMx);
  2124. }
  2125. var hisprice = new u_his_price
  2126. {
  2127. bednetid_mattressid = mattress.billid,
  2128. typeid = 2,
  2129. cmpdate = dtNow,
  2130. cmpemp = context.tokendata.username,
  2131. nottax_dept_cost = 0,
  2132. dept_cost = 0,
  2133. foreign_cost = 0
  2134. };
  2135. var fieldsHs = "bednetid_mattressid, typeid, cmpdate, cmpemp, nottax_dept_cost, dept_cost, foreign_cost";
  2136. DbSqlHelper.Insert(cmd, "u_his_price", null, hisprice, fieldsHs);
  2137. }
  2138. #endregion
  2139. }
  2140. }