MattressHelper.cs 148 KB

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