MattressHelper.cs 87 KB

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