MattressHelper.cs 167 KB

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