MattressHelper.cs 105 KB

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