MattressHelper.cs 127 KB

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