MattressHelper.cs 107 KB

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