MattressHelper.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 LJLib.DAL.SQL;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data.SqlClient;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. namespace JLHHJSvr.Helper
  16. {
  17. class MattressHelper : HelperBase
  18. {
  19. private Dictionary<string, object> replacements = new Dictionary<string, object>();
  20. private Dictionary<string, string> formula_replacements = new Dictionary<string, string>();
  21. public void CalCulateFormula(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist)
  22. {
  23. InitMattress(mattress);
  24. InitMattressMx(mattress,mxlist);
  25. // 重算床网价格
  26. CalCulateBedNet(mxlist);
  27. InitReplaceMents(mattress);
  28. var diancengList = mxlist.Where(t => t.formulatype == 1).ToList();
  29. var mattersstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  30. DbSqlHelper.SelectOne(cmd, mattersstype, "formula_big_side,formula_small_side,formula_v_side");
  31. foreach (var mx in mxlist)
  32. {
  33. if (mx.mtrlid == 0 && mx.formulakind != 31) continue;
  34. if (string.IsNullOrEmpty(mx.formula) && string.IsNullOrEmpty(mx.useformula)) continue;
  35. InitMxReplaceMents(mx);
  36. string expression = ConvertToEnglishSymbols(mx.formula), use_expression = ConvertToEnglishSymbols(mx.useformula);
  37. expression = InitMxSideReplaceMents(mx, diancengList, mattersstype, expression);
  38. use_expression = InitMxSideReplaceMents(mx, diancengList, mattersstype,use_expression);
  39. #region 替换布料幅宽
  40. //大侧
  41. if (new List<int>() { 2,42,52,62,72,82 }.Contains(mx.formulakind.Value))
  42. {
  43. InitMxClothWidthReplaceMents(mx, mxlist,2);
  44. }
  45. //小侧
  46. if (new List<int>() { 3, 43, 53, 63, 73, 83 }.Contains(mx.formulakind.Value))
  47. {
  48. InitMxClothWidthReplaceMents(mx, mxlist,3);
  49. }
  50. //大侧
  51. if (new List<int>() { 4, 44, 54, 64, 74, 84 }.Contains(mx.formulakind.Value))
  52. {
  53. InitMxClothWidthReplaceMents(mx, mxlist,4);
  54. }
  55. //面裥绵
  56. if (new List<int>() { 0, 40, 50, 60, 70, 80, 203 }.Contains(mx.formulakind.Value))
  57. {
  58. InitMxClothWidthReplaceMents(mx, mxlist,0);
  59. }
  60. //底裥绵
  61. if (new List<int>() { 1, 41, 51, 61, 71, 81 }.Contains(mx.formulakind.Value))
  62. {
  63. InitMxClothWidthReplaceMents(mx, mxlist,1);
  64. }
  65. #endregion
  66. #region 替换布套高
  67. //内布套
  68. if (new List<int>() { 101,102,103,104,105,106 }.Contains(mx.formulakind.Value))
  69. {
  70. InitMxClothHeightReplaceMents(mxlist, 101);
  71. }
  72. //外布套
  73. if (new List<int>() { 111, 112, 113, 114, 115, 116 }.Contains(mx.formulakind.Value))
  74. {
  75. InitMxClothHeightReplaceMents(mxlist, 111);
  76. }
  77. #endregion
  78. #region 裥绵收缩率
  79. InitMxShrinkageReplaceMents(mx,mxlist);
  80. #endregion
  81. foreach (var replacement in replacements)
  82. {
  83. expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  84. use_expression = use_expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  85. }
  86. #region 计算成本金额
  87. // 判断expression前两个字符是否为@@
  88. bool sqlCal = !string.IsNullOrEmpty(expression) && expression.Substring(0, 2) == "@@";
  89. expression = expression.Replace("@@", "");
  90. mx.replace_formula = expression;
  91. if (sqlCal)
  92. {
  93. mx.costamt = Convert.ToDecimal(SqlCalculate(expression, "成本金额"));
  94. }
  95. else
  96. {
  97. mx.costamt = Calculate(expression, "成本金额").DecimalValue;
  98. }
  99. #endregion
  100. #region 计算清单用量
  101. sqlCal = !string.IsNullOrEmpty(use_expression) && use_expression.Substring(0, 2) == "@@";
  102. use_expression = use_expression.Replace("@@", "");
  103. mx.replace_useformula = use_expression;
  104. if (sqlCal)
  105. {
  106. mx.useqty = Convert.ToDecimal(SqlCalculate(use_expression, "清单用量"));
  107. }
  108. else
  109. {
  110. mx.useqty = Calculate(use_expression, "清单用量").DecimalValue;
  111. }
  112. #endregion
  113. }
  114. InitMattressMxListReplaceMents(mattress, mxlist);
  115. }
  116. private void CalCulateBedNet(List<u_mattress_mx_mtrl> mxlist)
  117. {
  118. var bedNetHelper = HelperBase.GetHelper<BedNetHelper>(cmd);
  119. var bednetList = mxlist.Where(t => t.formulatype == 99);
  120. foreach(var mx in bednetList)
  121. {
  122. if (mx.mtrlid <= 0) continue;
  123. var bednet = new u_bednet() { bednetid = mx.mtrlid };
  124. DbSqlHelper.SelectOne(cmd, bednet, "flag");
  125. if (bednet.flag == 1) continue;
  126. bedNetHelper.GetBedNet(bednet);
  127. var bednetMxlist = bedNetHelper.GetBedNetMxList(bednet.bednetid.Value);
  128. var bednetSpringList = bedNetHelper.GetBedNetSpringList(bednet.bednetid.Value);
  129. bedNetHelper.CalCulateFormula(bednet, bednetMxlist, bednetSpringList);
  130. bedNetHelper.SaveBedNet(bednet, bednetMxlist, bednetSpringList);
  131. mx.price = bednet.nottax_factory_cost;
  132. }
  133. }
  134. private void InitMattress(u_mattress mattress)
  135. {
  136. var dept = new u_dept() { deptid = mattress.deptid.Value };
  137. DbSqlHelper.SelectOne(cmd, dept, "pricelistid,profitrate,moneyrate,discount");
  138. var profirate = new u_factory_profitrate() { deptid = mattress.deptid, bednettypeid_mattresstypeid = mattress.mattresstypeid, bednet_or_mattress = 1 };
  139. DbSqlHelper.SelectOne(cmd, profirate, "profitrate");
  140. mattress.pricelistid = dept.pricelistid;
  141. mattress.dept_profitrate = mattress.profitrate;
  142. mattress.profitrate = profirate.profitrate;
  143. mattress.moneyrate = mattress.if_moneyrate == 1 ? dept.moneyrate ?? 1 : 1;
  144. mattress.discount = dept.discount ?? 1 ;
  145. mattress.commission = mattress.commission ?? 1;
  146. mattress.taxrate = mattress.taxrate ?? 1;
  147. mattress.other_rate = mattress.other_rate ?? 1;
  148. mattress.dept_profitrate = mattress.dept_profitrate ?? 0;
  149. // 检查佣金是否小于1
  150. if (mattress.commission < 1)
  151. {
  152. throw new LJCommonException("佣金点数不能小于1!");
  153. }
  154. // 检查税率是否小于1
  155. if (mattress.taxrate < 1)
  156. {
  157. throw new LJCommonException("税率不能小于1!");
  158. }
  159. // 检查额外点数是否小于1
  160. if (mattress.other_rate < 1)
  161. {
  162. throw new LJCommonException("税率不能小于1!");
  163. }
  164. // 检查部门利润率是否为0
  165. if (mattress.dept_profitrate == 0)
  166. {
  167. throw new LJCommonException("部门利润率不能为0!");
  168. }
  169. }
  170. private void InitMattressMx(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  171. {
  172. foreach(var mx in mxlist)
  173. {
  174. var formula = new u_mattress_formula() { formulaid = mx.formulaid, };
  175. DbSqlHelper.SelectOne(cmd, formula, "formulakind,formulatype,sortcode");
  176. var mtrldef = new u_mtrl_price() { mtrlid = mx.mtrlid };
  177. DbSqlHelper.SelectOne(cmd, mtrldef, "name,price,gram_weight,cloth_width,if_inputqty,priceunit,shrinkage,if_areaprice");
  178. mx.formulakind = formula.formulakind;
  179. mx.formulatype = formula.formulatype;
  180. mx.sortcode = formula.sortcode.Trim();
  181. mx.mtrlname = mtrldef.name ?? "";
  182. var formula2 = new u_mattress_formula();
  183. DbSqlHelper.SelectOne(cmd, "u_mattress_formula","formulakind = @formulakind",
  184. new Dictionary<string, object>() { { "@formulakind",mx.formulakind } },formula2, "formula,useformula,gydscrp");
  185. mx.formula = formula2.formula;
  186. mx.useformula = formula2.useformula;
  187. mx.gydscrp = formula2.gydscrp;
  188. cmd.CommandText = @"SELECT isnull(u_mtrl_price_pricelist.price, 0) AS pricelistprice
  189. ,u_mtrl_price.gram_weight
  190. ,u_mtrl_price.cloth_width
  191. ,u_mtrl_price.if_inputqty
  192. ,u_mtrl_price.priceunit
  193. ,u_mtrl_price.shrinkage
  194. ,isnull(u_mtrl_price_pricelist.price_formula, 0) AS price_formula
  195. ,isnull(u_mtrl_price_pricelist.qty_formula, 0) AS qty_formula
  196. ,u_mtrl_price.if_areaprice
  197. ,u_mtrl_price.thickness
  198. FROM u_mtrl_price
  199. INNER JOIN u_mtrl_price_pricelist ON u_mtrl_price.mtrlid = u_mtrl_price_pricelist.mtrlid
  200. WHERE (u_mtrl_price.mtrlid = @mtrlid)
  201. AND (u_mtrl_price_pricelist.pricelistid = @pricelistid)";
  202. cmd.Parameters.Clear();
  203. cmd.Parameters.AddWithValue("@mtrlid", mx.mtrlid);
  204. cmd.Parameters.AddWithValue("@pricelistid", mattress.pricelistid);
  205. using(var reader = cmd.ExecuteReader())
  206. {
  207. if (reader.Read())
  208. {
  209. mx.price = Convert.ToDecimal(reader["pricelistprice"]);
  210. mx.gram_weight = Convert.ToDecimal(reader["gram_weight"]);
  211. mx.cloth_width = Convert.ToDecimal(reader["cloth_width"]);
  212. mx.if_inputqty = Convert.ToInt32(reader["if_inputqty"]);
  213. mx.priceunit = reader["priceunit"].ToString();
  214. mx.shrinkage = Convert.ToDecimal(reader["shrinkage"]);
  215. mx.if_areaprice = Convert.ToByte(reader["if_areaprice"]);
  216. if(mx.if_inputqty == 1) mx.thickness = Convert.ToDecimal(reader["thickness"]);
  217. string price_formula = Convert.ToString(reader["price_formula"]);
  218. string qty_formula = Convert.ToString(reader["qty_formula"]);
  219. mx.formula = string.IsNullOrEmpty(price_formula) ? "" : price_formula;
  220. mx.useformula = string.IsNullOrEmpty(qty_formula) ? "" : qty_formula;
  221. }
  222. }
  223. }
  224. }
  225. private void InitReplaceMents(u_mattress mattress)
  226. {
  227. replacements.Clear();
  228. AddKeyValue("规格宽", mattress.mattress_width);
  229. AddKeyValue("规格长", mattress.mattress_length);
  230. AddKeyValue("规格高", mattress.mattress_height);
  231. AddKeyValue("压包数量", mattress.packqty);
  232. AddKeyValue("卷包直径", mattress.diameter);
  233. AddKeyValue("顶布裥棉外布套做法", mattress.if_db_wbutao_way);
  234. AddKeyValue("面料外布套做法", mattress.if_m_wbutao_way);
  235. AddKeyValue("内布套上覆", mattress.s_cover_qty);
  236. AddKeyValue("内布套侧覆", mattress.z_cover_qty);
  237. AddKeyValue("内布套下覆", mattress.x_cover_qty);
  238. AddKeyValue("面料上覆", mattress.s_m_cover_qty);
  239. AddKeyValue("面料侧覆", mattress.z_m_cover_qty);
  240. AddKeyValue("面料下覆", mattress.x_m_cover_qty);
  241. AddKeyValue("面拆", mattress.if_m_chai);
  242. AddKeyValue("中拆", mattress.if_z_chai);
  243. AddKeyValue("底拆", mattress.if_d_chai);
  244. AddKeyValue("工厂利润率", mattress.profitrate);
  245. AddKeyValue("部门利润率", mattress.dept_profitrate);
  246. AddKeyValue("部门让利点数", mattress.dept_profitrate_rangli);
  247. AddKeyValue("佣金点数", mattress.commission);
  248. AddKeyValue("额外点数", mattress.other_rate);
  249. AddKeyValue("额外费用", mattress.extras_cost);
  250. AddKeyValue("汇率", mattress.moneyrate);
  251. AddKeyValue("税率", mattress.taxrate);
  252. AddKeyValue("折扣率", mattress.discount);
  253. // 地区FOB费用,如果存在变量表,会替代
  254. AddKeyValue("大柜-普通-地区FOB费用", 6500);
  255. AddKeyValue("大柜-特定-地区FOB费用", 8500);
  256. AddKeyValue("小柜-普通-地区FOB费用", 6000);
  257. AddKeyValue("小柜-特定-地区FOB费用", 7000);
  258. // 柜型立方数
  259. AddKeyValue("大柜-柜型立方数", 64);
  260. AddKeyValue("小柜-柜型立方数", 28);
  261. // 柜型米数
  262. AddKeyValue("大柜-柜型米数", 12);
  263. AddKeyValue("小柜-柜型米数", 5.9);
  264. // 公式
  265. AddFormulaKeyValue("【总成本】", "【总材料成本】*【大小单】+【款式费用】+【边带费用】+【额外费用】+【制造费用】");
  266. AddFormulaKeyValue("【不含税出厂价】", "【总成本】*(【工厂利润率】+【工艺点数】+【内布套点数】+【拆装点数】+【海绵扣点】)*【管理费点】");
  267. AddFormulaKeyValue("【部门不含税价】", "【不含税出厂价】/(【部门利润率】+【部门让利点数】/100)/(1-(【佣金点数】-1))*【额外点数】+【FOB】");
  268. AddFormulaKeyValue("【税金】", "【部门不含税价】*(【税率】-1)+【佣金】 * 0.05");
  269. AddFormulaKeyValue("【部门含税价】", "(【部门不含税价】+【佣金】 * 0.06)*【税率】*【折扣率】");
  270. AddFormulaKeyValue("【外币价】", "【部门含税价】/【汇率】");
  271. AddFormulaKeyValue("【底价】", "【底价】/(1-(【佣金点数】-1)-【底价】");
  272. AddFormulaKeyValue("【佣金】", "if((【不含税出厂价】+【部门利润】/100)<> 0,【不含税出厂价】/(【部门利润】+【部门让利点数】/ 100),0)");
  273. if (mattress.woodpallettype == 0)
  274. {
  275. AddKeyValue("木托方式", "'普通木托'");
  276. }else if (mattress.woodpallettype == 1)
  277. {
  278. AddKeyValue("木托方式", " '夹板木托'");
  279. }else
  280. {
  281. AddKeyValue("木托方式", "'铁管木托' ");
  282. }
  283. var dept = new u_dept() { deptid = mattress.deptid.Value };
  284. DbSqlHelper.SelectOne(cmd, dept, "pricelistid");
  285. var bednetVarList = new List<u_bednet_var>();
  286. var selectStr = @"SELECT u_bednet_varmx.varid AS varid
  287. ,varkind
  288. ,varcode
  289. ,varname
  290. ,varclass
  291. ,vartype
  292. ,varunit
  293. ,u_bednet_varmx.varvalue AS varvalue
  294. ,formula
  295. FROM u_bednet_var
  296. INNER JOIN u_bednet_varmx ON u_bednet_varmx.varid = u_bednet_var.varid
  297. INNER JOIN u_dept ON u_dept.pricelistid = u_bednet_varmx.pricelistid";
  298. var whereList = new List<string>();
  299. whereList.Add("u_bednet_var.varclass < 2");
  300. whereList.Add("u_bednet_var.varkind = @varkind");
  301. whereList.Add("u_dept.deptid = @deptid");
  302. var param = new Dictionary<string, object>();
  303. param.Add("@varkind", 1);
  304. param.Add("@deptid", mattress.deptid);
  305. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "varid", "varid,varkind,varcode,varname,varclass,vartype,varunit,varvalue", 0, 0, bednetVarList);
  306. foreach (var netvar in bednetVarList)
  307. {
  308. AddKeyValue(netvar.varname, netvar.varvalue);
  309. }
  310. selectStr = @"SELECT varid
  311. ,varkind
  312. ,varcode
  313. ,varname
  314. ,varclass
  315. ,vartype
  316. ,varunit
  317. ,formula
  318. FROM u_bednet_var";
  319. bednetVarList = new List<u_bednet_var>();
  320. whereList = new List<string>();
  321. whereList.Add("u_bednet_var.varkind = @varkind");
  322. whereList.Add("u_bednet_var.varclass = @varclass");
  323. param = new Dictionary<string, object>();
  324. param.Add("@varkind", 1);
  325. param.Add("@varclass", 2);
  326. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), param, "varid", "varid,varkind,varcode,varname,varclass,vartype,varunit,formula", 0, 0, bednetVarList);
  327. // 添加替换公式变量
  328. foreach (var netvar in bednetVarList)
  329. {
  330. AddFormulaKeyValue(netvar.varname, netvar.formula);
  331. }
  332. }
  333. private void InitMxReplaceMents(u_mattress_mx_mtrl mx)
  334. {
  335. mx.mtrlname = !string.IsNullOrEmpty(mx.mtrlname) ? mx.mtrlname.Trim() : "null";
  336. mx.shrinkage = mx.shrinkage ?? 1;
  337. //取mtrldef.name的前两位
  338. AddKeyValue("名称前2位", mx.mtrlname.Substring(0, 2));
  339. AddKeyValue("物料单价", mx.price);
  340. AddKeyValue("物料克重", mx.gram_weight);
  341. AddKeyValue("幅宽", mx.cloth_width);
  342. AddKeyValue("数量", mx.qty);
  343. AddKeyValue("物料厚度", mx.if_inputqty);
  344. AddKeyValue("固定厚度", mx.if_inputqty);
  345. AddKeyValue("厚度", mx.thickness);
  346. AddKeyValue("按面积单价", mx.if_areaprice);
  347. AddKeyValue("物料名称", mx.mtrlname);
  348. AddKeyValue("物料收缩率", mx.shrinkage);
  349. }
  350. /// <summary>
  351. /// 添加大侧,小侧,V侧高度公式替换
  352. /// </summary>
  353. /// <param name="mx"></param>
  354. /// <param name="diancengList"></param>
  355. /// <param name="mattersstype"></param>
  356. /// <param name="expression"></param>
  357. /// <returns></returns>
  358. private string InitMxSideReplaceMents(u_mattress_mx_mtrl mx,List<u_mattress_mx_mtrl> diancengList, u_mattress_type mattersstype, string expression)
  359. {
  360. // 获取垫层的数据
  361. decimal bigSum = 0, smallSum = 0, vSum = 0;
  362. string bigChaStr = mx.chastr ?? "大侧";
  363. string smallChaStr = mx.chastr ?? "小侧1";
  364. string vChaStr = mx.chastr ?? "V侧1";
  365. foreach (var dianceng in diancengList)
  366. {
  367. if (dianceng.chastr.IndexOf(bigChaStr) > -1) bigSum += dianceng.qty.Value * dianceng.thickness.Value;
  368. if (dianceng.chastr.IndexOf(smallChaStr) > -1) smallSum += dianceng.qty.Value * dianceng.thickness.Value;
  369. if (dianceng.chastr.IndexOf(vChaStr) > -1) vSum += dianceng.qty.Value * dianceng.thickness.Value;
  370. }
  371. if (bigSum > 0)
  372. {
  373. AddKeyValue("大侧高度", bigSum);
  374. }
  375. if (smallSum > 0)
  376. {
  377. AddKeyValue("小侧高度", smallSum);
  378. }
  379. if (vSum > 0)
  380. {
  381. AddKeyValue("V侧高度", vSum);
  382. }
  383. #region 替换大侧高度
  384. if (expression.IndexOf("【大侧高度】") > -1)
  385. {
  386. if (!string.IsNullOrEmpty(mattersstype.formula_big_side)) expression = expression.Replace("【大侧高度】", $"({mattersstype.formula_big_side})");
  387. expression = expression.Replace("【位置】", $"'{bigChaStr}'");
  388. }
  389. #endregion
  390. #region 替换小侧高度
  391. if (expression.IndexOf("【小侧高度】") > -1)
  392. {
  393. if (!string.IsNullOrEmpty(mattersstype.formula_small_side)) expression = expression.Replace("【小侧高度】", $"({mattersstype.formula_small_side})");
  394. expression = expression.Replace("【位置】", $"'{smallChaStr}'");
  395. }
  396. #endregion
  397. #region 替换V侧高度
  398. if (expression.IndexOf("【V侧高度】") > -1)
  399. {
  400. if(!string.IsNullOrEmpty(mattersstype.formula_v_side)) expression = expression.Replace("【V侧高度】", $"({mattersstype.formula_v_side})");
  401. expression = expression.Replace("【位置】", $"'{vChaStr}'");
  402. }
  403. #endregion
  404. return expression;
  405. }
  406. /// <summary>
  407. /// 添加布料幅宽公式替换
  408. /// </summary>
  409. /// <param name="mx"></param>
  410. /// <param name="mxlist"></param>
  411. /// <param name="formulakind"></param>
  412. private void InitMxClothWidthReplaceMents(u_mattress_mx_mtrl mx, List<u_mattress_mx_mtrl> mxlist,int formulakind)
  413. {
  414. decimal cloth_width = 0;
  415. if (string.IsNullOrEmpty(mx.chastr))
  416. {
  417. cloth_width = mxlist.First(t => t.formulakind.Value == formulakind).cloth_width.Value;
  418. }
  419. else
  420. {
  421. cloth_width = mxlist.First(t => t.formulakind.Value == formulakind && t.chastr.Equals(mx.chastr)).cloth_width.Value;
  422. }
  423. AddKeyValue("布料幅宽", cloth_width);
  424. }
  425. /// <summary>
  426. /// 添加布套高公式替换
  427. /// </summary>
  428. /// <param name="mxlist"></param>
  429. /// <param name="formulakind"></param>
  430. private void InitMxClothHeightReplaceMents(List<u_mattress_mx_mtrl> mxlist, int formulakind)
  431. {
  432. decimal cloth_height = 0;
  433. cloth_height = mxlist.First(t => t.formulakind.Value == formulakind).thickness.Value;
  434. AddKeyValue("布套高", cloth_height);
  435. }
  436. /// <summary>
  437. /// 添加裥绵收缩率公式替换
  438. /// </summary>
  439. /// <param name="mx"></param>
  440. /// <param name="mxlist"></param>
  441. private void InitMxShrinkageReplaceMents(u_mattress_mx_mtrl mx, List<u_mattress_mx_mtrl> mxlist)
  442. {
  443. decimal thickness = 0, shrinkage = 0;
  444. if(new List<int>() { 0,40,50,60,70,80,1,41, 51,61,71,81, 2,42,52,62,72,82,3,43, 53,63,73,83,4,44, 54,64,74,84 }.Contains(mx.formulakind.Value))
  445. {
  446. foreach (var row in mxlist) // mxList.Rows等价于arg_dw.RowCount()
  447. {
  448. if (new List<int>() { 50, 51, 52, 53, 54 }.Contains(row.formulakind.Value))
  449. {
  450. // 如果当前行的公式种类匹配
  451. thickness += row.thickness.Value * row.qty.Value; // 简化计算
  452. }
  453. }
  454. }
  455. if(thickness > 0)
  456. {
  457. cmd.CommandText = @"SELECT TOP 1 shrinkage
  458. FROM u_shrinkage
  459. WHERE u_shrinkage.minvalue <= @thickness
  460. AND u_shrinkage.maxvalue >= @thickness";
  461. cmd.Parameters.Clear();
  462. cmd.Parameters.AddWithValue("@thickness", thickness);
  463. using(var reader = cmd.ExecuteReader())
  464. {
  465. if (reader.Read())
  466. {
  467. shrinkage = Convert.ToDecimal(reader["shrinkage"]);
  468. }
  469. }
  470. }
  471. if(shrinkage > 0)
  472. {
  473. AddKeyValue("收缩率", shrinkage);
  474. }
  475. }
  476. /// <summary>
  477. /// 添加主表与明细表相关公式替换
  478. /// </summary>
  479. /// <param name="mattress"></param>
  480. /// <param name="mxlist"></param>
  481. private void InitMattressMxListReplaceMents(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  482. {
  483. string fob_expression = string.Empty,cubage_expression = string.Empty;
  484. cubage_expression = "(【规格长】*【规格宽】* 【规格高】)/100000";
  485. #region 判断装柜方式 是否进箱
  486. if (mattress.packtype == 0)
  487. {
  488. mattress.loading_type = "平压";
  489. fob_expression = "【地区FOB费用】/(【柜型米数】 /(【规格宽】/ 100 +0.1)*(【压包数量】*2))";
  490. if (mattress.packqty == 0) throw new LJCommonException("请填写压包数量!");
  491. }
  492. else if (mattress.packtype == 1)
  493. {
  494. mattress.loading_type = "卷包(非进箱)";
  495. fob_expression = "【地区FOB费用】 /(【柜型立方数】 /( 3.14*( 【卷包直径】 /100/2+0.03)*( 【卷包直径】 /100/2+0.03)*(【规格宽】/ 100 +0.05)))";
  496. cubage_expression = "( 3.14*( 【卷包直径】 /100/2+0.03)*( 【卷包直径】 /100/2+0.03)*(【规格宽】/ 100 +0.05))";
  497. if (mattress.packqty == 0) throw new LJCommonException("请填写卷包直径!");
  498. var _bzList = mxlist.Where(t => t.formulatype == 3 && t.formulakind == 19).ToList();
  499. foreach(var bz in _bzList)
  500. {
  501. if (bz.mtrlname.IndexOf("箱") > -1) {
  502. mattress.loading_type = "卷包(进箱)";
  503. fob_expression = "【地区FOB费用】/(【柜型立方数】 / (( 【纸箱宽】 /100+0.03)*(【纸箱宽】/100+0.03)*(【规格宽】/ 100 +0.1)))";
  504. cubage_expression = "( 【纸箱宽】 /100+0.03)*(【纸箱宽】/100+0.03)*(【规格宽】/ 100 +0.1)";
  505. AddKeyValue("【纸箱宽】", bz.mtrlname.Substring(0, 2));
  506. break;
  507. }
  508. }
  509. }
  510. else if (mattress.packtype == 2)
  511. {
  512. mattress.loading_type = "国内";
  513. fob_expression = "【地区FOB费用】/(【柜型立方数】/((【规格宽】/100+0.03)*(【规格长】/100+0.03)*(【规格高】/100+0.03)))";
  514. }
  515. #endregion
  516. #region 计算FOB
  517. if (string.IsNullOrEmpty(mattress.area) || string.IsNullOrEmpty(mattress.cabinet_type) || string.IsNullOrEmpty(mattress.loading_type)) return;
  518. //地区FOB费用
  519. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type) && new List<string>() { "普通地区", "特定地区" }.Contains(mattress.area))
  520. {
  521. fob_expression = fob_expression.Replace("【地区FOB费用】", $"【{mattress.cabinet_type.Trim()}-{mattress.area.Trim()}-地区FOB费用】");
  522. }
  523. //柜型立方数
  524. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type))
  525. {
  526. fob_expression = fob_expression.Replace("【柜型立方数】", $"【{mattress.cabinet_type.Trim()}-柜型立方数】");
  527. }
  528. //纸箱宽 见上面
  529. //柜型米数
  530. if (new List<string>() { "大柜", "小柜" }.Contains(mattress.cabinet_type))
  531. {
  532. fob_expression = fob_expression.Replace("【柜型米数】", $"【{mattress.cabinet_type.Trim()}-柜型米数】");
  533. }
  534. mattress.fob_replace_formula = fob_expression;
  535. mattress.fob = Calculate(fob_expression, "FOB").DecimalValue;
  536. //半成品:如果PE袋物料是含 半成品专用 的编码,FOB需要除以数量
  537. var bzList = mxlist.Where(t => t.formulatype == 3).ToList();
  538. foreach (var bz in bzList)
  539. {
  540. if (bz.qty.Value > 0 && bz.mtrlname.IndexOf("半成品专用") > -1)
  541. {
  542. mattress.fob /= bz.qty.Value;
  543. mattress.fob_replace_formula = $"{mattress.fob_replace_formula} / {bz.qty.Value}";
  544. }
  545. }
  546. AddKeyValue("FOB", mattress.fob);
  547. #endregion
  548. #region 计算体积
  549. if (mattress.fob > 0)
  550. {
  551. mattress.cubage = Calculate(cubage_expression, "体积").DecimalValue;
  552. }
  553. #endregion
  554. #region 计算人工费用
  555. var mattersstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  556. DbSqlHelper.SelectOne(cmd, mattersstype, "hrcost_formula,biandaiqty,zhizao_amt,guanli_rate");
  557. if(mattress.biandai_qty == 0 && mattress.if_m_chai == 0 && mattress.if_z_chai == 0 && mattress.if_d_chai == 0)
  558. {
  559. mattress.biandai_qty = mattersstype.biandaiqty;
  560. }
  561. mattress.hrcost_replace_formula = mattersstype.hrcost_formula;
  562. mattress.zhizao_amt = mattersstype.zhizao_amt;
  563. mattress.guanli_rate = mattersstype.guanli_rate;
  564. var diancengList = mxlist.Where(t => t.formulatype == 1).ToList();
  565. int diancengCount = 0;
  566. foreach(var dianceng in diancengList)
  567. {
  568. if (dianceng.formulatype == 32) diancengCount++;
  569. }
  570. AddKeyValue("垫层数量", diancengCount);
  571. AddKeyValue("制造费用", mattress.zhizao_amt);
  572. AddKeyValue("管理费点", mattress.guanli_rate);
  573. mattress.hrcost = Calculate(mattress.hrcost_replace_formula, "款式费用").DecimalValue;
  574. AddKeyValue("款式费用", mattress.hrcost);
  575. #endregion
  576. #region 计算边带费用
  577. mattress.biandaicost_replace_formula = "【边带条数】*【边带单价】*((【规格宽】+【规格长】)*2+30)/100";
  578. AddKeyValue("边带条数", mattress.biandai_qty);
  579. mattress.biandaicost = Calculate(mattress.biandaicost_replace_formula, "边带费用").DecimalValue;
  580. AddKeyValue("边带费用", mattress.biandaicost);
  581. #endregion
  582. #region 辅料 有打钮纽扣 加0.13 工厂利润率
  583. var fuliaoList = mxlist.Where(t => t.formulatype == 2 && t.mtrlname.IndexOf("打钮") > -1).ToList();
  584. if (fuliaoList.Any()) mattress.profitrate += 0.13M;
  585. #endregion
  586. #region 拼侧 最大金额的侧 和 工厂利润率加点
  587. CalCulatePince(mattress, mxlist);
  588. #endregion
  589. #region 内布套特殊加点
  590. if(mattress.if_n_butao == 1) CalCulateNetBuTao(mattress, mxlist);
  591. #endregion
  592. #region 拆装特殊加点
  593. CalCulateChaiZhuang(mattress);
  594. #endregion
  595. #region 海绵款特殊扣点
  596. if (mattress.if_haimian_type == 1) CalCulateHaiMian(mattress);
  597. #endregion
  598. #region 计算主表 总材料成本
  599. mattress.total_material_cost = mxlist.Sum(t => t.costamt);
  600. AddKeyValue("总材料成本", mattress.total_material_cost);
  601. #endregion
  602. CalCulateCostNew(mattress, "标准");
  603. CalCulateCostNew(mattress, "大单");
  604. CalCulateCostNew(mattress, "散单");
  605. }
  606. /// <summary>
  607. /// 计算拼侧最大金额的侧和工厂利润率加点
  608. /// </summary>
  609. /// <param name="mattress"></param>
  610. /// <param name="mxlist"></param>
  611. private void CalCulatePince(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist)
  612. {
  613. var mianliaoList = mxlist.Where(t => t.formulatype == 0).ToList();
  614. var costAmounts = new Dictionary<string, decimal>
  615. {
  616. { "普通大侧", 0m },
  617. { "上下拼侧1", 0m },
  618. { "上下拼侧2", 0m },
  619. { "上下拼侧3", 0m },
  620. { "左右拼侧1", 0m },
  621. { "左右拼侧2", 0m },
  622. { "左右拼侧3", 0m },
  623. { "左右拼侧4", 0m }
  624. };
  625. var costAmounts2 = new Dictionary<string, decimal>
  626. {
  627. { "小侧1-上下拼侧1", 0m },
  628. { "小侧1-上下拼侧2", 0m },
  629. { "小侧1-上下拼侧3", 0m }
  630. };
  631. foreach (var mianliao in mianliaoList)
  632. {
  633. if (costAmounts.ContainsKey(mianliao.sortcode))
  634. {
  635. costAmounts[mianliao.sortcode] += mianliao.costamt.Value;
  636. }else if (costAmounts2.ContainsKey(mianliao.sortcode))
  637. {
  638. costAmounts2[mianliao.sortcode] += mianliao.costamt.Value;
  639. }
  640. }
  641. // 找出最大拼侧金额及对应的拼侧名称
  642. string lsTempMaxStr = costAmounts.OrderByDescending(x => x.Value).First().Key;
  643. decimal ldeTempMax = costAmounts[lsTempMaxStr];
  644. // 清空不是最大金额的拼侧
  645. foreach (var mianliao in mianliaoList)
  646. {
  647. if (mianliao.sortcode != lsTempMaxStr && new List<int>() { 2, 82, 42, 52, 62, 72 }.Contains(mianliao.formulakind.Value))
  648. {
  649. mianliao.costamt = 0;
  650. }
  651. }
  652. // 找出最大小侧拼侧金额及对应的拼侧名称
  653. string lsTempMaxStr2 = costAmounts2.OrderByDescending(x => x.Value).First().Key;
  654. decimal ldeTempMax2 = costAmounts2[lsTempMaxStr2];
  655. // 清空不是最大金额的拼侧
  656. foreach (var mianliao in mianliaoList)
  657. {
  658. if (mianliao.sortcode != lsTempMaxStr2 && new List<int>() { 3, 83, 43, 53, 63, 73 }.Contains(mianliao.formulakind.Value))
  659. {
  660. mianliao.costamt = 0;
  661. }
  662. }
  663. // 计算拼侧数量
  664. int pinceCnt = costAmounts.Values.Count(value => value > 0);
  665. // 判断是否有大侧
  666. bool hasBigSide = mianliaoList.Any(row => new[] { 7, 8, 9, 10 }.Contains(row.formulakind.Value) && row.mtrlid.Value > 0);
  667. string field = pinceCnt > 3 || (hasBigSide && (pinceCnt == 2 || pinceCnt == 3)) ? "num_3" : hasBigSide ? "num_1" : pinceCnt == 2 || pinceCnt == 3 ? "num_2" : string.Empty;
  668. // 查找点数
  669. var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  670. mattress.profitrate_point = workmanship.rate;
  671. AddKeyValue("工艺点数", mattress.profitrate_point);
  672. }
  673. /// <summary>
  674. /// 计算内布套特殊加点
  675. /// </summary>
  676. /// <param name="mattress"></param>
  677. /// <param name="mxlist"></param>
  678. private void CalCulateNetBuTao(u_mattress mattress, List<u_mattress_mx_mtrl> mxlist)
  679. {
  680. var butaoList = mxlist.Where(t => t.formulatype == 101 || t.formulatype == 102);
  681. bool butaoFz = false, butaoJd = false;
  682. // 检查内布套是否为复杂款
  683. foreach (var butao in butaoList) {
  684. if (butao.formulakind == 70 && butao.mtrlid > 0) { butaoFz = true; break; }
  685. }
  686. if (butaoFz)
  687. {
  688. foreach (var butao in butaoList)
  689. {
  690. if (butao.formulakind == 203 && butao.mtrlid > 0) { butaoFz = true; break; }
  691. }
  692. }
  693. else
  694. {
  695. butaoJd = true; // 没有找到复杂款即为简单款
  696. }
  697. // 根据款式类型判断查询的字段
  698. string field = butaoJd ? "num_4" : (butaoFz ? "num_5" : "");
  699. // 查找点数
  700. var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  701. mattress.butao_point = workmanship.rate;
  702. AddKeyValue("内布套点数", mattress.butao_point);
  703. }
  704. /// <summary>
  705. /// 拆装特殊加点
  706. /// </summary>
  707. /// <param name="mattress"></param>
  708. private void CalCulateChaiZhuang(u_mattress mattress)
  709. {
  710. int cnt = 0;
  711. // 判断 if_m_chai, if_z_chai, if_d_chai 是否为 1
  712. if (Convert.ToInt32(mattress.if_m_chai) == 1) cnt++;
  713. if (Convert.ToInt32(mattress.if_z_chai) == 1) cnt++;
  714. if (Convert.ToInt32(mattress.if_d_chai) == 1) cnt++;
  715. // 根据款式类型判断查询的字段
  716. string field = cnt == 1 ? "num_6" : (cnt > 1 ? "num_7" : "");
  717. // 查找点数
  718. var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  719. mattress.chaizhuang_point = workmanship.rate;
  720. AddKeyValue("拆装点数", mattress.chaizhuang_point);
  721. }
  722. /// <summary>
  723. /// 计算海绵款特殊扣点
  724. /// </summary>
  725. /// <param name="mattress"></param>
  726. private void CalCulateHaiMian(u_mattress mattress)
  727. {
  728. // 根据款式类型判断查询的字段
  729. string field = "num_8";
  730. // 查找点数
  731. var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  732. mattress.haimian_point = workmanship.rate;
  733. AddKeyValue("海绵扣点", mattress.haimian_point);
  734. }
  735. /// <summary>
  736. /// 计算大小单加点
  737. /// </summary>
  738. /// <param name="mattress"></param>
  739. /// <param name="typename"></param>
  740. private void CalCulateDanNum(u_mattress mattress,string typename)
  741. {
  742. // 根据款式类型判断查询的字段
  743. string field = "散单".Equals(typename) ? "num_9" : "标准".Equals(typename) ? "num_10" : "大单".Equals(typename) ? "num_11" : "";
  744. // 查找点数
  745. var workmanship = new u_workmanship_add() { deptid = mattress.deptid, mattresstypeid = mattress.mattresstypeid, workmanshipname = field };
  746. mattress.dannum_rate = workmanship.rate;
  747. AddKeyValue("大小单", mattress.dannum_rate);
  748. }
  749. /// <summary>
  750. /// 计算成本
  751. /// </summary>
  752. /// <param name="mattress"></param>
  753. /// <param name="typename"></param>
  754. private void CalCulateCostNew(u_mattress mattress, string typename)
  755. {
  756. #region 总成本
  757. CalCulateDanNum(mattress, typename);
  758. mattress.total_cost = CalculateVarFormula("总成本").DecimalValue;
  759. if (mattress.if_zhedie_type == 1) mattress.total_cost *= 2;
  760. AddKeyValue("总成本", mattress.total_cost);
  761. #endregion
  762. #region 不含税出厂价
  763. mattress.nottax_factory_cost = CalculateVarFormula("不含税出厂价").DecimalValue;
  764. AddKeyValue("不含税出厂价", mattress.nottax_factory_cost);
  765. #endregion
  766. #region 部门不含税价
  767. mattress.nottax_dept_cost = CalculateVarFormula("部门不含税价").DecimalValue;
  768. AddKeyValue("部门不含税价", mattress.nottax_dept_cost);
  769. #endregion
  770. #region 税金
  771. mattress.taxes = CalculateVarFormula("税金").DecimalValue;
  772. AddKeyValue("税金", mattress.taxes);
  773. #endregion
  774. #region 部门含税价=(部门不含税价+佣金*0.06)*税率*折扣率(fob>0)
  775. if (mattress.fob == 0) {
  776. mattress.discount = 1;
  777. AddKeyValue("折扣率", mattress.discount);
  778. }
  779. mattress.taxes = CalculateVarFormula("部门含税价").DecimalValue;
  780. AddKeyValue("部门含税价", mattress.dept_cost);
  781. #endregion
  782. #region 外币价= 部门含税价/汇率
  783. if (mattress.moneyrate > 0)
  784. {
  785. if ("标准".Equals(typename)) mattress.foreign_cost_bz = mattress.dept_cost / mattress.moneyrate;
  786. else if ("大单".Equals(typename)) mattress.foreign_cost_dd = mattress.dept_cost / mattress.moneyrate;
  787. else if ("散单".Equals(typename)) mattress.foreign_cost = mattress.dept_cost / mattress.moneyrate;
  788. }
  789. #endregion
  790. }
  791. /// <summary>
  792. /// 计算成本
  793. /// </summary>
  794. /// <param name="mattress"></param>
  795. /// <param name="typename"></param>
  796. private void CalCulateCost(u_mattress mattress, string typename)
  797. {
  798. #region 计算主表 总成本
  799. CalCulateDanNum(mattress, typename);
  800. mattress.total_cost = mattress.total_material_cost * mattress.dannum_rate + mattress.hrcost + mattress.biandaicost + mattress.extras_cost + mattress.zhizao_amt;
  801. if (mattress.if_zhedie_type == 1) mattress.total_cost *= 2;
  802. #endregion
  803. #region 不含税出厂价=总成本*工厂利润率
  804. mattress.nottax_factory_cost = mattress.total_cost * (mattress.profitrate + mattress.profitrate_point + mattress.butao_point + mattress.chaizhuang_point + mattress.haimian_point) * mattress.guanli_rate;
  805. #endregion
  806. #region 部门不含税价=不含税出厂价*部门利润率*佣金点数 * 额外点数 +FOB
  807. mattress.nottax_dept_cost = mattress.nottax_factory_cost /
  808. (mattress.dept_profitrate
  809. + mattress.dept_profitrate_rangli / 100)
  810. / (1 - (mattress.commission - 1))
  811. * mattress.other_rate
  812. + mattress.fob;
  813. #endregion
  814. #region 税金 = 部门不含税价 * 税率
  815. mattress.taxes = mattress.nottax_dept_cost * (mattress.taxrate - 1);
  816. //问题1,税金增加需加上佣金*0.05 #TODO
  817. decimal lde_rj = 0;
  818. mattress.taxes += lde_rj * 0.05M;
  819. #endregion
  820. #region 部门含税价=(部门不含税价+佣金*0.06)*税率*折扣率(fob>0)
  821. if (mattress.fob == 0) mattress.discount = 1;
  822. mattress.dept_cost = (mattress.nottax_dept_cost + lde_rj * 0.06m)
  823. * mattress.taxrate
  824. * mattress.discount;
  825. #endregion
  826. #region 外币价= 部门含税价/汇率
  827. if(mattress.moneyrate > 0)
  828. {
  829. if ("标准".Equals(typename)) mattress.foreign_cost_bz = mattress.dept_cost / mattress.moneyrate;
  830. else if ("大单".Equals(typename)) mattress.foreign_cost_dd = mattress.dept_cost / mattress.moneyrate;
  831. else if ("散单".Equals(typename)) mattress.foreign_cost = mattress.dept_cost / mattress.moneyrate;
  832. }
  833. #endregion
  834. }
  835. private void AddKeyValue(string key, object value, bool isReplace = true)
  836. {
  837. key = $"【{key}】";
  838. if (!replacements.ContainsKey(key))
  839. {
  840. replacements.Add(key, value);
  841. }
  842. else if (isReplace)
  843. {
  844. replacements[key] = value;
  845. }
  846. }
  847. private void AddFormulaKeyValue(string key, string value, bool isReplace = true)
  848. {
  849. if (!formula_replacements.ContainsKey(key))
  850. {
  851. formula_replacements.Add(key, value);
  852. }
  853. else if (isReplace)
  854. {
  855. formula_replacements[key] = value;
  856. }
  857. }
  858. private string ConvertToEnglishSymbols(string input)
  859. {
  860. input = input.Replace("(", "(")
  861. .Replace(")", ")")
  862. .Replace(",", ",")
  863. .Replace("。", ".")
  864. .Replace(":", ":")
  865. .Replace(";", ";")
  866. .Replace("“", "\"")
  867. .Replace("”", "\"")
  868. .Replace("‘", "'")
  869. .Replace("’", "'");
  870. return input;
  871. }
  872. private TData CalculateVarFormula(string varname)
  873. {
  874. varname = $"【{varname}】";
  875. if (!formula_replacements.ContainsKey(varname)) return null;
  876. return Calculate(varname,varname);
  877. }
  878. private TData Calculate(string expression,string name="")
  879. {
  880. ConvertToEnglishSymbols(expression);
  881. // 定义正则表达式模式,匹配包含【】的内容
  882. string pattern = @"【(.*?)】";
  883. // 创建正则表达式对象
  884. Regex regex = new Regex(pattern);
  885. foreach (var replacement in formula_replacements)
  886. {
  887. expression = expression.Replace(replacement.Key, replacement.Value);
  888. }
  889. // 使用正则表达式匹配输入字符串
  890. MatchCollection matches = regex.Matches(expression);
  891. var new_replacements = new Dictionary<string, object>();
  892. foreach (Match match in matches)
  893. {
  894. if (replacements.TryGetValue(match.Value, out object value2))
  895. {
  896. new_replacements[match.Value] = value2;
  897. }
  898. }
  899. foreach (var replacement in new_replacements)
  900. {
  901. expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  902. }
  903. try
  904. {
  905. return LJExprParser.Parse(expression).Result;
  906. }
  907. catch (Exception ex)
  908. {
  909. throw new LJCommonException($"计算{name}公式错误!expression: {expression},exception: {ex.Message}");
  910. }
  911. }
  912. private object SqlCalculate(string expression, string name = "")
  913. {
  914. ConvertToEnglishSymbols(expression);
  915. expression = expression.Replace("@@", "");
  916. // 定义正则表达式模式,匹配包含【】的内容
  917. string pattern = @"【(.*?)】";
  918. // 创建正则表达式对象
  919. Regex regex = new Regex(pattern);
  920. // 使用正则表达式匹配输入字符串
  921. MatchCollection matches = regex.Matches(expression);
  922. var new_formula_replacements = new Dictionary<string, string>();
  923. var new_replacements = new Dictionary<string, object>();
  924. foreach (Match match in matches)
  925. {
  926. // 检查原始字典中是否存在该键,并添加到新字典中
  927. if (formula_replacements.TryGetValue(match.Value, out string value))
  928. {
  929. new_formula_replacements[match.Value] = value;
  930. }
  931. if (replacements.TryGetValue(match.Value, out object value2))
  932. {
  933. new_replacements[match.Value] = value2;
  934. }
  935. }
  936. foreach (var replacement in new_formula_replacements)
  937. {
  938. expression = expression.Replace(replacement.Key, replacement.Value);
  939. }
  940. foreach (var replacement in new_replacements)
  941. {
  942. expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
  943. }
  944. try
  945. {
  946. cmd.CommandText = $@"{expression}";
  947. cmd.Parameters.Clear();
  948. return cmd.ExecuteScalar();
  949. }
  950. catch (Exception ex)
  951. {
  952. throw new LJCommonException($"计算{name}公式错误!expression: {expression},exception: {ex.Message}");
  953. }
  954. }
  955. public void SaveMattress(u_mattress mattress,List<u_mattress_mx_mtrl> mxlist)
  956. {
  957. 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";
  958. 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";
  959. if (mattress.mattressid <= 0)
  960. {
  961. //新建
  962. mattress.createtime = context.opdate;
  963. mattress.createby = context.tokendata.username;
  964. mattress.mattressid = BllHelper.GetID(cmd, "u_mattress");
  965. fields = "mattressid," + fields;
  966. var mattresstype = new u_mattress_type() { mattresstypeid = mattress.mattresstypeid };
  967. DbSqlHelper.SelectOne(cmd, mattresstype, "typecode");
  968. mattress.mattresscode = $"{mattresstype.typecode}-{context.opdate.ToString("yyyyMMdd")}{(mattress.mattressid % 10000).ToString("D4")}";
  969. DbSqlHelper.Insert(cmd, "u_mattress", null, mattress, fields);
  970. }
  971. else
  972. {
  973. // 删除所有明细
  974. cmd.CommandText = @"DELETE u_mattress_mx_mtrl WHERE mattressid = @mattressid";
  975. cmd.Parameters.Clear();
  976. cmd.Parameters.AddWithValue("@mattressid", mattress.mattressid);
  977. cmd.ExecuteNonQuery();
  978. DbSqlHelper.Update(cmd, "u_mattress", null, mattress, "mattressid", fields);
  979. }
  980. if (mxlist != null && mxlist.Any())
  981. {
  982. foreach (var item in mxlist)
  983. {
  984. item.mattressid = mattress.mattressid;
  985. if (item.mattressmxid == null || item.mattressmxid <= 0)
  986. {
  987. item.mattressmxid = BllHelper.GetID(cmd, "u_mattress_mx_mtrl");
  988. }
  989. DbSqlHelper.Insert(cmd, "u_mattress_mx_mtrl", null, item, fieldsMx);
  990. }
  991. }
  992. // 更新计价历史
  993. var hisprice = new u_his_price
  994. {
  995. bednetid_mattressid = mattress.mattressid,
  996. typeid = 1,
  997. cmpdate = context.opdate,
  998. cmpemp = context.tokendata.username,
  999. nottax_dept_cost = mattress.nottax_dept_cost,
  1000. dept_cost = mattress.dept_cost,
  1001. foreign_cost = mattress.foreign_cost
  1002. };
  1003. var fieldsHs = "bednetid_mattressid, typeid, cmpdate, cmpemp, nottax_dept_cost, dept_cost, foreign_cost";
  1004. DbSqlHelper.Insert(cmd, "u_his_price", null, hisprice, fieldsHs);
  1005. }
  1006. }
  1007. }