MattressHelper.cs 141 KB

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