MattressHelper.cs 129 KB

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