MattressHelper.cs 108 KB

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