MattressHelper.cs 166 KB

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