MattressHelper.cs 147 KB

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