SoftBedHelper.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. using DirectService.Tools;
  2. using JLHHJSvr.BLL;
  3. using JLHHJSvr.Com.Model;
  4. using JLHHJSvr.Excutor;
  5. using JLHHJSvr.LJException;
  6. using JLHHJSvr.Tools;
  7. using LJLib.DAL.SQL;
  8. using NPOI.SS.Formula.Functions;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. using System.Web;
  17. using static JLHHJSvr.Helper.CacheHelper;
  18. namespace JLHHJSvr.Helper
  19. {
  20. internal class SoftBedHelper : HelperBase
  21. {
  22. /// <summary>
  23. /// 获取软床报价单据
  24. /// </summary>
  25. /// <param name="billid"></param>
  26. /// <param name="fields"></param>
  27. /// <param name="bill"></param>
  28. public u_softbed GetSoftBed(int billid,string fields = null)
  29. {
  30. fields = fields ?? @"softbed_id,softbed_code,softbed_relcode,softbed_name,deptid,create_date,create_emp,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  31. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  32. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version,flag,audit_date,audit_emp,update_date,update_emp";
  33. var bill = new u_softbed() { softbed_id = billid };
  34. if (DbSqlHelper.SelectOne(cmd, bill, fields) == 0) return null;
  35. return bill;
  36. }
  37. /// <summary>
  38. /// 获取软床报价单据明细
  39. /// </summary>
  40. /// <param name="billid"></param>
  41. /// <param name="fields"></param>
  42. /// <param name="mxlist"></param>
  43. public List<u_softbed_mx> GetSoftBedMxList(int billid,string fields = null)
  44. {
  45. fields = fields ?? @"softbed_id,printid,formulaid,pzid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  46. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt,pzname,formulaname";
  47. var mxlist = new List<u_softbed_mx>();
  48. var selectStr = @"SELECT u_softbed_mx.softbed_id
  49. ,u_softbed_mx.printid
  50. ,u_softbed_mx.formulaid
  51. ,u_softbed_mx.pzid
  52. ,u_softbed_mx.mtrlid
  53. ,u_softbed_mx.mtrlname
  54. ,u_softbed_mx.mtrlcode
  55. ,u_softbed_mx.mtrlmode
  56. ,u_softbed_mx.unit
  57. ,u_softbed_mx.has_type
  58. ,u_softbed_mx.allow_edit
  59. ,u_softbed_mx.cutting_length
  60. ,u_softbed_mx.cutting_width
  61. ,u_softbed_mx.cutting_qty
  62. ,u_softbed_mx.useqty
  63. ,u_softbed_mx.use_formula
  64. ,u_softbed_mx.use_formula_str
  65. ,u_softbed_mx.actual_useqty
  66. ,u_softbed_mx.loss_rate
  67. ,u_softbed_mx.price
  68. ,u_softbed_mx.price_formula
  69. ,u_softbed_mx.price_formula_str
  70. ,u_softbed_mx.cost_price
  71. ,u_softbed_mx.cost_amt
  72. ,ISNULL(u_configure_code.name,'') AS pzname
  73. ,ISNULL(u_softbed_formula.formulaname,'') AS formulaname
  74. FROM u_softbed_mx
  75. LEFT JOIN u_configure_code ON u_softbed_mx.pzid = u_configure_code.pzid
  76. LEFT JOIN u_softbed_formula ON u_softbed_mx.formulaid = u_softbed_formula.formulaid
  77. ";
  78. DbSqlHelper.SelectJoin(cmd, selectStr, "softbed_id = @billid", new Dictionary<string, object>() { { "@billid", billid } }, "has_type,printid", fields, 0, 0, mxlist);
  79. return mxlist;
  80. }
  81. public List<u_softbed_mx> GetChangeSoftBedMxList(int billid,List<u_configure_codemx> codeMxList)
  82. {
  83. var newList = new List<u_softbed_mx>();
  84. var softbed = GetSoftBed(billid, "billid,softbed_code,has_headboard,has_nightstand,has_bedframe");
  85. if (softbed == null) return newList;
  86. // 物料清单
  87. var mxList = GetSoftBedMxList(billid);
  88. // 换料清单
  89. var bomList = GetSoftBedMxBomList(codeMxList);
  90. // 排除不存在配置的物料
  91. //var skipList = new List<int>();
  92. //var mxMtrlIds = mxList.Select(t => t.mtrlid).ToList();
  93. //if (mxMtrlIds.Count > 0)
  94. //{
  95. // cmd.CommandText = $@"SELECT mtrlid FROM u_configure_codemxbom WHERE mtrlid NOT IN {ListEx.getString(mxMtrlIds)}";
  96. // cmd.Parameters.Clear();
  97. // using (var reader = cmd.ExecuteReader())
  98. // {
  99. // while (reader.Read())
  100. // {
  101. // skipList.Add(Convert.ToInt32(reader["mtrlid"]));
  102. // }
  103. // }
  104. //}
  105. foreach (var mx in mxList)
  106. {
  107. if (mx.pzid > 0)
  108. {
  109. // 部件
  110. var tmp_bomList = bomList.FindAll(t => t.has_type == mx.has_type && t.pzid == mx.pzid);
  111. if (tmp_bomList.Count > 0)
  112. {
  113. // add
  114. foreach (var temp in tmp_bomList)
  115. {
  116. if (temp.mtrlid == mx.mtrlid)
  117. {
  118. mx.printid = newList.Count + 1;
  119. newList.Add(mx);
  120. }
  121. else
  122. {
  123. temp.printid = newList.Count + 1;
  124. newList.Add(temp);
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. // 非部件
  132. string contfigtypename = string.Empty;
  133. string prefix = $"{softbed.softbed_code}|";
  134. if (mx.has_type == 1) contfigtypename = $"{prefix}床头";
  135. else if (mx.has_type == 2) contfigtypename = $"{prefix}床头柜";
  136. else contfigtypename = $"{prefix}床架";
  137. cmd.CommandText = @"SELECT u_configure_code.pzid
  138. FROM u_configure_codemxbom
  139. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  140. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  141. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  142. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid
  143. WHERE u_configure_codemxbom.mtrlid = @mtrlid AND LTRIM(RTRIM(u_configure_type.contfigtypename)) = @contfigtypename";
  144. cmd.Parameters.Clear();
  145. cmd.Parameters.AddWithValue("@mtrlid",mx.mtrlid);
  146. cmd.Parameters.AddWithValue("@contfigtypename", contfigtypename);
  147. int pzid = 0, cnt = 0;
  148. using(var reader = cmd.ExecuteReader())
  149. {
  150. while(reader.Read())
  151. {
  152. pzid = Convert.ToInt32(reader["pzid"]);
  153. cnt++;
  154. }
  155. }
  156. if (cnt > 1) throw new LJCommonException($"换料失败,原因:{contfigtypename}的{mx.mtrlname}存在多个清单,但没有部件!");
  157. var tmp_bomList = bomList.FindAll(t => t.pzid == pzid);
  158. if (tmp_bomList.Count > 0)
  159. {
  160. // add
  161. foreach (var temp in tmp_bomList)
  162. {
  163. if (temp.mtrlid == mx.mtrlid)
  164. {
  165. mx.printid = newList.Count + 1;
  166. newList.Add(mx);
  167. }
  168. else
  169. {
  170. temp.printid = newList.Count + 1;
  171. newList.Add(temp);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. if (newList.Count <= 0) newList.AddRange(bomList);
  178. return newList;
  179. }
  180. /// <summary>
  181. /// 根据部件配置项值获取对应BOM明细
  182. /// </summary>
  183. /// <param name="mxlist"></param>
  184. /// <param name="codeList"></param>
  185. public List<u_softbed_mx> GetSoftBedMxBomList(List<u_configure_codemx> codeList)
  186. {
  187. var mxlist = new List<u_softbed_mx>();
  188. foreach(var code in codeList)
  189. {
  190. var resultList = new List<u_configure_codemxbom>();
  191. var outputFields = @"pzid,printid,pid,mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,cost,sonloss,sonloss_formula,
  192. sondecloss,sondecloss_formula,default_length,default_width,default_qty,mtrlcode,mtrlname,mtrlmode,unit,
  193. mtrlsectype,zxmtrlmode,usermtrlmode,price";
  194. var selectStr = @"SELECT u_configure_codemxbom.pzid
  195. ,u_configure_codemxbom.printid
  196. ,u_configure_codemxbom.pid
  197. ,u_configure_codemxbom.mtrlid
  198. ,u_configure_codemxbom.sonscale
  199. ,u_configure_codemxbom.sonscale_formula
  200. ,u_configure_codemxbom.mng_cost_rate
  201. ,u_configure_codemxbom.profit_rate
  202. ,u_configure_codemxbom.realqty
  203. ,u_configure_codemxbom.cost
  204. ,u_configure_codemxbom.sonloss
  205. ,u_configure_codemxbom.sonloss_formula
  206. ,u_configure_codemxbom.sondecloss
  207. ,u_configure_codemxbom.sondecloss_formula
  208. ,u_configure_codemxbom.default_length
  209. ,u_configure_codemxbom.default_width
  210. ,u_configure_codemxbom.default_qty
  211. ,u_mtrldef.mtrlcode
  212. ,u_mtrldef.mtrlname
  213. ,u_mtrldef.mtrlmode
  214. ,u_mtrldef.unit
  215. ,u_mtrldef.mtrlsectype
  216. ,u_mtrldef.zxmtrlmode
  217. ,u_mtrldef.usermtrlmode
  218. ,u_erpmtrl_price.price
  219. FROM u_configure_codemxbom
  220. INNER JOIN u_mtrldef ON u_configure_codemxbom.mtrlid = u_mtrldef.mtrlid
  221. LEFT OUTER JOIN u_erpmtrl_price ON u_configure_codemxbom.mtrlid = u_erpmtrl_price.mtrlid";
  222. DbSqlHelper.SelectJoin(cmd,selectStr,
  223. "u_configure_codemxbom.pzid = @pzid AND u_configure_codemxbom.printid = @printid",
  224. new Dictionary<string, object>() { { "@pzid", code.pzid }, { "@printid",code.printid} },
  225. "u_configure_codemxbom.pid", outputFields, 0,0,resultList);
  226. foreach(var item in resultList)
  227. {
  228. mxlist.Add(InitSoftBedMxFromBom(item, code.has_type.Value));
  229. }
  230. }
  231. return mxlist;
  232. }
  233. /// <summary>
  234. /// 初始化配置项值带出的BOM清单列表
  235. /// </summary>
  236. /// <param name="bomItem"></param>
  237. /// <returns></returns>
  238. public u_softbed_mx InitSoftBedMxFromBom(u_configure_codemxbom bomItem,byte has_type)
  239. {
  240. var mx = new u_softbed_mx()
  241. {
  242. pzid = bomItem.pzid,
  243. mtrlid = bomItem.mtrlid,
  244. mtrlname = bomItem.mtrlname,
  245. mtrlcode = bomItem.mtrlcode,
  246. mtrlmode = bomItem.mtrlmode,
  247. unit = bomItem.unit,
  248. allow_edit = 0,
  249. has_type = has_type, // 1-床头、2-床头柜、4-床架
  250. cutting_length = bomItem.default_length,
  251. cutting_width = bomItem.default_width,
  252. cutting_qty = bomItem.default_qty,
  253. price = bomItem.price,
  254. price_formula = "",
  255. price_formula_str = "",
  256. useqty = bomItem.sonscale,
  257. use_formula = "",
  258. use_formula_str = "",
  259. actual_useqty = 0,
  260. loss_rate = bomItem.sonloss,
  261. cost_price = 0,
  262. cost_amt = 0
  263. };
  264. return mx;
  265. }
  266. /// <summary>
  267. /// 新建/修改 软床报价
  268. /// </summary>
  269. /// <param name="softbed"></param>
  270. /// <exception cref="LJCommonException"></exception>
  271. public void SaveSoftBed(u_softbed softbed)
  272. {
  273. SaveCheck(softbed);
  274. // 初始化属性
  275. AutoInit.AutoInitS(softbed);
  276. foreach(var mx in softbed.mxList)
  277. {
  278. AutoInit.AutoInitS(mx);
  279. }
  280. // 计算价格
  281. CalCulateFormula(softbed);
  282. //
  283. var dtNow = context.opdate;
  284. var fields = @"softbed_relcode,softbed_name,deptid,mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  285. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,money_type,moneyrate,dscrp,costamt,nottax_factory_cost,nottax_dept_cost,
  286. dept_cost,foreign_cost,total_mtrl_cost,total_hr_cost,total_cost,version";
  287. var mx_fields = @"softbed_id,printid,pzid,formulaid,mtrlid,mtrlname,mtrlcode,mtrlmode,unit,has_type,allow_edit,cutting_length,cutting_width,cutting_qty,
  288. useqty,use_formula,use_formula_str,actual_useqty,loss_rate,price,price_formula,price_formula_str,cost_price,cost_amt";
  289. if(softbed.softbed_id == 0)
  290. {
  291. // 新建
  292. fields += ",softbed_id,softbed_code,create_date,create_emp";
  293. softbed.softbed_id = BllHelper.GetID(cmd, "u_softbed");
  294. softbed.create_date = dtNow;
  295. softbed.create_emp = context.tokendata.username;
  296. softbed.version = 1;
  297. if (string.IsNullOrEmpty(softbed.softbed_code))
  298. {
  299. // 编号-年月日+流水
  300. softbed.softbed_code = $"RC-{dtNow.ToString("yyyyMMdd")}{(softbed.softbed_id % 10000).ToString("D4")}";
  301. }
  302. DbSqlHelper.Insert(cmd, "u_softbed", null, softbed, fields);
  303. } else
  304. {
  305. // 修改
  306. softbed.update_date = dtNow;
  307. softbed.update_emp = context.tokendata.username;
  308. softbed.version++;
  309. fields += ",update_date,update_emp";
  310. cmd.CommandText = @"DELETE FROM u_softbed_mx WHERE softbed_id = @softbed_id";
  311. cmd.Parameters.Clear();
  312. cmd.Parameters.AddWithValue("@softbed_id", softbed.softbed_id);
  313. cmd.ExecuteNonQuery();
  314. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", fields);
  315. }
  316. for (int i = 0; i < softbed.mxList.Count; i++)
  317. {
  318. var mx = softbed.mxList[i];
  319. mx.softbed_id = softbed.softbed_id;
  320. mx.printid = i + 1;
  321. DbSqlHelper.Insert(cmd, "u_softbed_mx", null, mx, mx_fields);
  322. }
  323. SaveSoftBedTemplate(softbed);
  324. // 解锁
  325. LockHelper.UnLockBill(cmd, BillKeyWord, softbed.softbed_id, context.tokendata.username, 0);
  326. }
  327. private void SaveCheck(u_softbed softbed)
  328. {
  329. if (softbed == null) throw new LJCommonException("软床报价保存失败,数据异常!");
  330. if (softbed.is_template == 0 && (softbed.mxList == null || softbed.mxList.Count <= 0)) throw new LJCommonException("软床报价保存失败,明细内容为空!");
  331. if(softbed.softbed_id > 0)
  332. {
  333. // 检查锁
  334. if (LockHelper.CheckIsLock(cmd, MattressHelper.BillKeyWord, softbed.softbed_id, context.tokendata.username, out string arg_msg)) throw new LJCommonException(arg_msg);
  335. // 检测版本控制
  336. CheckBillVersion<u_softbed>(cmd, softbed.softbed_id, softbed.version);
  337. //var _softbed = GetSoftBed(softbed.softbed_id, "softbed_id,softbed_code,version,update_emp");
  338. //if (_softbed.version != softbed.version) throw new LJCommonException($"单据:{_softbed.softbed_code}已被{_softbed.update_emp}修改,请重新加载最新数据再操作!");
  339. }
  340. if(softbed.softbed_id > 0 && softbed.is_template != 0)
  341. {
  342. //
  343. var configureList = GetSoftBedConfigureName(softbed);
  344. var bomSet = new Dictionary<int,List<u_configure_codemxbom>>();
  345. foreach(var configure in configureList)
  346. {
  347. cmd.CommandText = @"SELECT u_configure_code.pzid
  348. ,u_configure_codemxbom.mtrlid
  349. ,LTRIM(RTRIM(u_configure_type.contfigtypename))
  350. ,u_configure_type.contfigtypeid
  351. FROM u_configure_codemxbom
  352. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  353. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  354. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  355. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid
  356. WHERE LTRIM(RTRIM(u_configure_type.contfigtypename)) = @contfigtypename";
  357. cmd.Parameters.Clear();
  358. cmd.Parameters.AddWithValue("@contfigtypename", configure.Value.contfigtypename);
  359. using (var reader = cmd.ExecuteReader())
  360. {
  361. while (reader.Read())
  362. {
  363. if (!bomSet.ContainsKey(configure.Key)) bomSet.Add(configure.Key, new List<u_configure_codemxbom>());
  364. bomSet[configure.Key].Add(new u_configure_codemxbom()
  365. {
  366. pzid = Convert.ToInt32(reader["pzid"]),
  367. mtrlid = Convert.ToInt32(reader["mtrlid"]),
  368. typeid = Convert.ToInt32(reader["contfigtypeid"]),
  369. has_type = configure.Key
  370. });
  371. }
  372. }
  373. }
  374. foreach (var mx in softbed.mxList)
  375. {
  376. if(!bomSet.TryGetValue(mx.has_type, out var bomList))
  377. {
  378. throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}没有设置物料清单");
  379. }
  380. var cnt = bomList.Where(t => t.has_type == mx.has_type && t.mtrlid == mx.mtrlid).Count();
  381. if (cnt > 1) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}存在多个清单,但没有设置部件!");
  382. else if (cnt == 0) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}不存在于物料清单中,请检查!");
  383. }
  384. }
  385. }
  386. /// <summary>
  387. /// 新建软床报价模板
  388. /// </summary>
  389. /// <param name="softbed"></param>
  390. private void SaveSoftBedTemplate(u_softbed softbed)
  391. {
  392. if (softbed.is_template == 0) return;
  393. if(string.IsNullOrEmpty(softbed.softbed_code))
  394. {
  395. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  396. }
  397. string prefix = $"{softbed.softbed_code}|";
  398. var configureList = GetSoftBedConfigureName(softbed).Values.ToList();
  399. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  400. foreach(var configure in configureList)
  401. {
  402. // 判断是否已存在
  403. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  404. cmd.Parameters.Clear();
  405. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  406. var cnt = Convert.ToInt32(cmd.ExecuteScalar());
  407. if (cnt > 0) continue;
  408. baseInfoHelper.SaveConfigureType(configure);
  409. }
  410. // 生成标准选配项值ifdft
  411. if(softbed.codeMxList != null && softbed.codeMxList.Count > 0)
  412. {
  413. foreach (var codeMx in softbed.codeMxList)
  414. {
  415. if (codeMx.pzid == 0) continue;
  416. // 判断是否有标准
  417. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  418. cmd.Parameters.Clear();
  419. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  420. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  421. if(Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  422. {
  423. cmd.CommandText = @"UPDATE u_configure_codemx SET ifdft = 0 WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  424. cmd.Parameters.Clear();
  425. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  426. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  427. cmd.ExecuteNonQuery();
  428. }
  429. codeMx.ifdft = 1;
  430. DbSqlHelper.Update(cmd, "u_configure_codemx", null, codeMx, "pzid,printid", "ifdft");
  431. }
  432. }
  433. }
  434. /// <summary>
  435. /// 返回软床报价相关配置名
  436. /// </summary>
  437. /// <param name="softbed"></param>
  438. /// <returns></returns>
  439. private Dictionary<byte, u_configure_type> GetSoftBedConfigureName(u_softbed softbed)
  440. {
  441. var dict = new Dictionary<byte, u_configure_type>();
  442. string prefix = $"{softbed.softbed_code}|";
  443. if (softbed.has_headboard == 1)
  444. {
  445. dict.Add(1, new u_configure_type()
  446. {
  447. contfigtypeid = 0,
  448. contfigtypename = $"{prefix}床头",
  449. contfigtype = 0,
  450. usechflag = 1,
  451. flag = 0
  452. });
  453. }
  454. if (softbed.has_nightstand == 1)
  455. {
  456. dict.Add(2, new u_configure_type()
  457. {
  458. contfigtypeid = 0,
  459. contfigtypename = $"{prefix}床头柜",
  460. contfigtype = 0,
  461. usechflag = 1,
  462. flag = 0
  463. });
  464. }
  465. if (softbed.has_bedframe == 1)
  466. {
  467. dict.Add(4,new u_configure_type()
  468. {
  469. contfigtypeid = 0,
  470. contfigtypename = $"{prefix}床架",
  471. contfigtype = 0,
  472. usechflag = 1,
  473. flag = 0
  474. });
  475. }
  476. return dict;
  477. }
  478. /// <summary>
  479. /// 获取软床报价配置
  480. /// </summary>
  481. /// <param name="softbed"></param>
  482. /// <param name="extraWheres"></param>
  483. /// <returns></returns>
  484. public List<u_configure_type> GetSoftBedConfigureList(u_softbed softbed,List<string> extraWheres)
  485. {
  486. var codeMxList = new List<u_configure_codemx>();
  487. var selectStr = @"SELECT u_configure_codemx.pzid
  488. ,u_configure_codemx.printid
  489. ,u_configure_codemx.pzcodemx
  490. ,u_configure_codemx.namemx
  491. ,u_configure_code.name AS pzname
  492. ,u_configure_code.pzcode
  493. ,u_configure_code.inputtype
  494. ,LTRIM(RTRIM(u_configure_type.contfigtypename)) AS contfigtypename
  495. ,u_configure_code.typeid
  496. ,u_configure_codemx.ifdft
  497. FROM u_configure_codemx
  498. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  499. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid";
  500. var whereList = new List<string>();
  501. var nameList = new List<string>();
  502. if (softbed.softbed_id > 0 || softbed.template_id > 0)
  503. {
  504. // 模版配置获取
  505. int billid = softbed.softbed_id > 0 ? softbed.softbed_id : softbed.template_id;
  506. var _softbed = GetSoftBed(billid, "softbed_id,softbed_code,has_headboard,has_nightstand,has_bedframe");
  507. string prefix = $"{_softbed.softbed_code}|";
  508. if(_softbed.has_headboard == 1)
  509. {
  510. nameList.Add($"{prefix}床头");
  511. }
  512. if (_softbed.has_nightstand == 1)
  513. {
  514. nameList.Add($"{prefix}床头柜");
  515. }
  516. if (_softbed.has_bedframe == 1)
  517. {
  518. nameList.Add($"{prefix}床架");
  519. }
  520. }
  521. if(nameList.Count > 0)
  522. {
  523. whereList.Add($"LTRIM(RTRIM(u_configure_type.contfigtypename)) IN {ListEx.getString(nameList)}");
  524. }
  525. if(extraWheres != null && extraWheres.Count > 0)
  526. {
  527. whereList.AddRange(extraWheres);
  528. }
  529. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), null, "typeid,pzid,printid", "typeid,pzid,printid,pzcodemx,namemx,pzname,pzcode,contfigtypename,ifdft", 0, 0, codeMxList);
  530. // 部件选配项
  531. var codeMap = new Dictionary<int, u_configure_code>();
  532. foreach(var codeMx in codeMxList)
  533. {
  534. int pzid = codeMx.pzid.Value;
  535. if (!codeMap.TryGetValue(pzid, out var config))
  536. {
  537. config = new u_configure_code
  538. {
  539. pzid = pzid,
  540. name = codeMx.pzname,
  541. contfigtypename = codeMx.contfigtypename,
  542. pzcode = codeMx.pzcode,
  543. typeid = codeMx.typeid,
  544. codeMxList = new List<u_configure_codemx>()
  545. };
  546. codeMap.Add(pzid, config);
  547. }
  548. config.codeMxList.Add(codeMx);
  549. }
  550. // 部件选配类型
  551. var typeMap = new Dictionary<int, u_configure_type>();
  552. foreach(var code in codeMap.Values.ToList())
  553. {
  554. int typeid = code.typeid.Value;
  555. if (!typeMap.TryGetValue(typeid, out var config))
  556. {
  557. config = new u_configure_type
  558. {
  559. contfigtypeid = typeid,
  560. contfigtypename = code.contfigtypename,
  561. codeList = new List<u_configure_code>()
  562. };
  563. typeMap.Add(typeid, config);
  564. }
  565. config.codeList.Add(code);
  566. }
  567. return typeMap.Values.ToList();
  568. }
  569. /// <summary>
  570. /// 复制软床报价
  571. /// </summary>
  572. /// <param name="softbed"></param>
  573. /// <param name="newId"></param>
  574. public void CopySoftBed(u_softbed softbed, out int newId)
  575. {
  576. var fields = @"mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  577. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,moneyrate";
  578. var softbed_copy = GetSoftBed(softbed.softbed_id, fields);
  579. // 判断copy_bedNet.bednetcode是否存在@@字符串,如果存在,则删除@@后面的字符串,包括@@
  580. if (softbed_copy.softbed_code.IndexOf("@@") > -1)
  581. {
  582. softbed_copy.softbed_code = softbed_copy.softbed_code.Substring(softbed_copy.softbed_code.IndexOf("@@"));
  583. }
  584. softbed_copy.softbed_code += " @@";
  585. softbed_copy.softbed_code += DateTime.Now.ToString("yyyMMdd_mmhhss");
  586. softbed_copy.softbed_id = 0;
  587. SaveSoftBed(softbed_copy);
  588. newId = softbed_copy.softbed_id;
  589. }
  590. /// <summary>
  591. /// 删除软床报价
  592. /// </summary>
  593. /// <param name="billid"></param>
  594. /// <exception cref="LJCommonException"></exception>
  595. public void DeleteSoftBed(int billid)
  596. {
  597. var softbed = GetSoftBed(billid, "softbed_code,flag");
  598. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  599. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  600. DbSqlHelper.Delete(cmd, softbed);
  601. }
  602. /// <summary>
  603. /// 审核软床报价
  604. /// </summary>
  605. /// <param name="billid"></param>
  606. /// <exception cref="LJCommonException"></exception>
  607. public void AuditSoftBed(int billid)
  608. {
  609. var softbed = GetSoftBed(billid, "softbed_code,flag");
  610. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  611. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  612. softbed.flag = 1;
  613. softbed.audit_date = context.opdate;
  614. softbed.audit_emp = context.tokendata.username;
  615. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  616. }
  617. /// <summary>
  618. /// 撤审软床报价
  619. /// </summary>
  620. /// <param name="billid"></param>
  621. /// <exception cref="LJCommonException"></exception>
  622. public void CAuditSoftBed(int billid)
  623. {
  624. var softbed = GetSoftBed(billid, "softbed_code,flag");
  625. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  626. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  627. softbed.flag = 0;
  628. softbed.audit_date = null;
  629. softbed.audit_emp = string.Empty;
  630. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  631. }
  632. public void CalCulateFormula(u_softbed softbed)
  633. {
  634. InitSoftBed(softbed);
  635. InitReplaceMents(softbed);
  636. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  637. foreach (var mx in softbed.mxList)
  638. {
  639. InitMxReplaceMents(softbed, mx);
  640. formula.CalculateAll();
  641. // 转换后的文本公式
  642. mx.use_formula_str = formula.GetFormulaItem("【实际用量】").formula_transform;
  643. mx.price_formula_str = formula.GetFormulaItem("【成本单价】").formula_transform;
  644. // 成本单价
  645. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  646. // 实际用量
  647. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  648. // 成本金额
  649. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  650. // 总材料成本
  651. total_mtrl_cost += mx.cost_amt;
  652. // 总人力成本
  653. total_hr_cost += 0;
  654. }
  655. if(softbed.mxList != null && softbed.mxList.Count > 0)
  656. {
  657. softbed.total_mtrl_cost = total_mtrl_cost;
  658. softbed.total_hr_cost = total_hr_cost;
  659. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  660. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  661. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  662. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  663. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  664. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  665. }
  666. //formula.CalculateAll();
  667. }
  668. private void InitSoftBed(u_softbed softbed)
  669. {
  670. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  671. //
  672. softbed.dept_profitrate = dept.profitrate;
  673. //
  674. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  675. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  676. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  677. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  678. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  679. // 检查佣金是否小于1
  680. if (softbed.commission < 1)
  681. {
  682. throw new LJCommonException("佣金点数不能小于1!");
  683. }
  684. // 检查税率是否小于1
  685. if (softbed.taxrate < 1)
  686. {
  687. throw new LJCommonException("税率不能小于1!");
  688. }
  689. // 检查额外点数是否小于1
  690. if (softbed.other_rate < 1)
  691. {
  692. throw new LJCommonException("税率不能小于1!");
  693. }
  694. // 检查部门利润率是否为0
  695. if (softbed.dept_profitrate == 0)
  696. {
  697. throw new LJCommonException("部门利润率不能为0!");
  698. }
  699. if(softbed.is_template == 1)
  700. {
  701. // 使用模板报价
  702. softbed.template_id = 0;
  703. softbed.template_code = string.Empty;
  704. softbed.template_name = string.Empty;
  705. }
  706. }
  707. private void InitReplaceMents(u_softbed softbed)
  708. {
  709. // 常量变量
  710. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  711. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  712. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  713. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  714. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  715. formula.AddFormulaItem("【税率】", softbed.taxrate);
  716. formula.AddFormulaItem("【工厂利润率】", 1);
  717. formula.AddFormulaItem("【FOB】", 0);
  718. formula.AddFormulaItem("【外销加点】", 0);
  719. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  720. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  721. // 公式变量
  722. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  723. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  724. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  725. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  726. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  727. }
  728. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  729. {
  730. // 默认公式变量
  731. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  732. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  733. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  734. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  735. // 常量变量
  736. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  737. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  738. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  739. formula.AddFormulaItem("【用料量】", mx.useqty);
  740. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  741. formula.AddFormulaItem("【材料单价】", mx.price);
  742. if(mx.formulaid > 0)
  743. {
  744. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  745. ,u_softbed_formula.formulaname
  746. ,u_softbed_formula.use_formula
  747. ,u_softbed_formula.price_formula
  748. FROM u_softbed_formula
  749. WHERE formulaid = @formulaid";
  750. cmd.Parameters.Clear();
  751. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  752. using(var reader = cmd.ExecuteReader())
  753. {
  754. if(reader.Read())
  755. {
  756. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  757. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  758. }
  759. }
  760. }
  761. }
  762. #region 通用公式
  763. private CalculateFormula formula = new CalculateFormula();
  764. private string BillKeyWord = "u_softbed";
  765. #endregion
  766. }
  767. }