MattressHelper.cs 124 KB

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