MattressHelper.cs 164 KB

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