MattressHelper.cs 141 KB

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