SoftBedHelper.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. private void SaveCheck(u_softbed softbed)
  326. {
  327. if (softbed == null) throw new LJCommonException("软床报价保存失败,数据异常!");
  328. if (softbed.is_template == 0 && (softbed.mxList == null || softbed.mxList.Count <= 0)) throw new LJCommonException("软床报价保存失败,明细内容为空!");
  329. if(softbed.softbed_id > 0)
  330. {
  331. // 检测版本控制
  332. var _softbed = GetSoftBed(softbed.softbed_id, "softbed_id,softbed_code,version,update_emp");
  333. if (_softbed.version != softbed.version) throw new LJCommonException($"单据:{_softbed.softbed_code}已被{_softbed.update_emp}修改,请重新加载最新数据再操作!");
  334. }
  335. if(softbed.softbed_id > 0 && softbed.is_template != 0)
  336. {
  337. //
  338. var configureList = GetSoftBedConfigureName(softbed);
  339. var bomSet = new Dictionary<int,List<u_configure_codemxbom>>();
  340. foreach(var configure in configureList)
  341. {
  342. cmd.CommandText = @"SELECT u_configure_code.pzid
  343. ,u_configure_codemxbom.mtrlid
  344. ,LTRIM(RTRIM(u_configure_type.contfigtypename))
  345. ,u_configure_type.contfigtypeid
  346. FROM u_configure_codemxbom
  347. INNER JOIN u_configure_codemx ON u_configure_codemxbom.pzid = u_configure_codemx.pzid
  348. AND u_configure_codemxbom.printid = u_configure_codemx.printid
  349. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  350. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid
  351. WHERE LTRIM(RTRIM(u_configure_type.contfigtypename)) = @contfigtypename";
  352. cmd.Parameters.Clear();
  353. cmd.Parameters.AddWithValue("@contfigtypename", configure.Value.contfigtypename);
  354. using (var reader = cmd.ExecuteReader())
  355. {
  356. while (reader.Read())
  357. {
  358. if (!bomSet.ContainsKey(configure.Key)) bomSet.Add(configure.Key, new List<u_configure_codemxbom>());
  359. bomSet[configure.Key].Add(new u_configure_codemxbom()
  360. {
  361. pzid = Convert.ToInt32(reader["pzid"]),
  362. mtrlid = Convert.ToInt32(reader["mtrlid"]),
  363. typeid = Convert.ToInt32(reader["contfigtypeid"]),
  364. has_type = configure.Key
  365. });
  366. }
  367. }
  368. }
  369. foreach (var mx in softbed.mxList)
  370. {
  371. if(!bomSet.TryGetValue(mx.has_type, out var bomList))
  372. {
  373. throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}没有设置物料清单");
  374. }
  375. var cnt = bomList.Where(t => t.has_type == mx.has_type && t.mtrlid == mx.mtrlid).Count();
  376. if (cnt > 1) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}存在多个清单,但没有设置部件!");
  377. else if (cnt == 0) throw new LJCommonException($"保存失败,原因:{configureList[mx.has_type].contfigtypename}的{mx.mtrlname}不存在于物料清单中,请检查!");
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// 新建软床报价模板
  383. /// </summary>
  384. /// <param name="softbed"></param>
  385. private void SaveSoftBedTemplate(u_softbed softbed)
  386. {
  387. if (softbed.is_template == 0) return;
  388. if(string.IsNullOrEmpty(softbed.softbed_code))
  389. {
  390. throw new LJCommonException("报价未生成唯一码,无法生成模板配置");
  391. }
  392. string prefix = $"{softbed.softbed_code}|";
  393. var configureList = GetSoftBedConfigureName(softbed).Values.ToList();
  394. var baseInfoHelper = GetHelper<BasicInfoHelper>(cmd, context);
  395. foreach(var configure in configureList)
  396. {
  397. // 判断是否已存在
  398. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_type WHERE LTRIM(RTRIM(contfigtypename)) = @contfigtypename";
  399. cmd.Parameters.Clear();
  400. cmd.Parameters.AddWithValue("@contfigtypename", configure.contfigtypename);
  401. var cnt = Convert.ToInt32(cmd.ExecuteScalar());
  402. if (cnt > 0) continue;
  403. baseInfoHelper.SaveConfigureType(configure);
  404. }
  405. // 生成标准选配项值ifdft
  406. if(softbed.codeMxList != null && softbed.codeMxList.Count > 0)
  407. {
  408. foreach (var codeMx in softbed.codeMxList)
  409. {
  410. if (codeMx.pzid == 0) continue;
  411. // 判断是否有标准
  412. cmd.CommandText = @"SELECT COUNT(*) FROM u_configure_codemx WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  413. cmd.Parameters.Clear();
  414. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  415. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  416. if(Convert.ToInt32(cmd.ExecuteScalar()) > 0)
  417. {
  418. cmd.CommandText = @"UPDATE u_configure_codemx SET ifdft = 0 WHERE pzid = @pzid AND printid = @printid AND ifdft = 1";
  419. cmd.Parameters.Clear();
  420. cmd.Parameters.AddWithValue("@pzid", codeMx.pzid);
  421. cmd.Parameters.AddWithValue("@printid", codeMx.printid);
  422. cmd.ExecuteNonQuery();
  423. }
  424. codeMx.ifdft = 1;
  425. DbSqlHelper.Update(cmd, "u_configure_codemx", null, codeMx, "pzid,printid", "ifdft");
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// 返回软床报价相关配置名
  431. /// </summary>
  432. /// <param name="softbed"></param>
  433. /// <returns></returns>
  434. private Dictionary<byte, u_configure_type> GetSoftBedConfigureName(u_softbed softbed)
  435. {
  436. var dict = new Dictionary<byte, u_configure_type>();
  437. string prefix = $"{softbed.softbed_code}|";
  438. if (softbed.has_headboard == 1)
  439. {
  440. dict.Add(1, new u_configure_type()
  441. {
  442. contfigtypeid = 0,
  443. contfigtypename = $"{prefix}床头",
  444. contfigtype = 0,
  445. usechflag = 1,
  446. flag = 0
  447. });
  448. }
  449. if (softbed.has_nightstand == 1)
  450. {
  451. dict.Add(2, new u_configure_type()
  452. {
  453. contfigtypeid = 0,
  454. contfigtypename = $"{prefix}床头柜",
  455. contfigtype = 0,
  456. usechflag = 1,
  457. flag = 0
  458. });
  459. }
  460. if (softbed.has_bedframe == 1)
  461. {
  462. dict.Add(4,new u_configure_type()
  463. {
  464. contfigtypeid = 0,
  465. contfigtypename = $"{prefix}床架",
  466. contfigtype = 0,
  467. usechflag = 1,
  468. flag = 0
  469. });
  470. }
  471. return dict;
  472. }
  473. /// <summary>
  474. /// 获取软床报价配置
  475. /// </summary>
  476. /// <param name="softbed"></param>
  477. /// <param name="extraWheres"></param>
  478. /// <returns></returns>
  479. public List<u_configure_type> GetSoftBedConfigureList(u_softbed softbed,List<string> extraWheres)
  480. {
  481. var codeMxList = new List<u_configure_codemx>();
  482. var selectStr = @"SELECT u_configure_codemx.pzid
  483. ,u_configure_codemx.printid
  484. ,u_configure_codemx.pzcodemx
  485. ,u_configure_codemx.namemx
  486. ,u_configure_code.name AS pzname
  487. ,u_configure_code.pzcode
  488. ,u_configure_code.inputtype
  489. ,LTRIM(RTRIM(u_configure_type.contfigtypename)) AS contfigtypename
  490. ,u_configure_code.typeid
  491. ,u_configure_codemx.ifdft
  492. FROM u_configure_codemx
  493. INNER JOIN u_configure_code ON u_configure_codemx.pzid = u_configure_code.pzid
  494. INNER JOIN u_configure_type ON u_configure_code.typeid = u_configure_type.contfigtypeid";
  495. var whereList = new List<string>();
  496. var nameList = new List<string>();
  497. if (softbed.softbed_id > 0 || softbed.template_id > 0)
  498. {
  499. // 模版配置获取
  500. int billid = softbed.softbed_id > 0 ? softbed.softbed_id : softbed.template_id;
  501. var _softbed = GetSoftBed(billid, "softbed_id,softbed_code,has_headboard,has_nightstand,has_bedframe");
  502. string prefix = $"{_softbed.softbed_code}|";
  503. if(_softbed.has_headboard == 1)
  504. {
  505. nameList.Add($"{prefix}床头");
  506. }
  507. if (_softbed.has_nightstand == 1)
  508. {
  509. nameList.Add($"{prefix}床头柜");
  510. }
  511. if (_softbed.has_bedframe == 1)
  512. {
  513. nameList.Add($"{prefix}床架");
  514. }
  515. }
  516. if(nameList.Count > 0)
  517. {
  518. whereList.Add($"LTRIM(RTRIM(u_configure_type.contfigtypename)) IN {ListEx.getString(nameList)}");
  519. }
  520. if(extraWheres != null && extraWheres.Count > 0)
  521. {
  522. whereList.AddRange(extraWheres);
  523. }
  524. DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), null, "typeid,pzid,printid", "typeid,pzid,printid,pzcodemx,namemx,pzname,pzcode,contfigtypename,ifdft", 0, 0, codeMxList);
  525. // 部件选配项
  526. var codeMap = new Dictionary<int, u_configure_code>();
  527. foreach(var codeMx in codeMxList)
  528. {
  529. int pzid = codeMx.pzid.Value;
  530. if (!codeMap.TryGetValue(pzid, out var config))
  531. {
  532. config = new u_configure_code
  533. {
  534. pzid = pzid,
  535. name = codeMx.pzname,
  536. contfigtypename = codeMx.contfigtypename,
  537. pzcode = codeMx.pzcode,
  538. typeid = codeMx.typeid,
  539. codeMxList = new List<u_configure_codemx>()
  540. };
  541. codeMap.Add(pzid, config);
  542. }
  543. config.codeMxList.Add(codeMx);
  544. }
  545. // 部件选配类型
  546. var typeMap = new Dictionary<int, u_configure_type>();
  547. foreach(var code in codeMap.Values.ToList())
  548. {
  549. int typeid = code.typeid.Value;
  550. if (!typeMap.TryGetValue(typeid, out var config))
  551. {
  552. config = new u_configure_type
  553. {
  554. contfigtypeid = typeid,
  555. contfigtypename = code.contfigtypename,
  556. codeList = new List<u_configure_code>()
  557. };
  558. typeMap.Add(typeid, config);
  559. }
  560. config.codeList.Add(code);
  561. }
  562. return typeMap.Values.ToList();
  563. }
  564. /// <summary>
  565. /// 复制软床报价
  566. /// </summary>
  567. /// <param name="softbed"></param>
  568. /// <param name="newId"></param>
  569. public void CopySoftBed(u_softbed softbed, out int newId)
  570. {
  571. var fields = @"mtrlmode,mtrltype,has_headboard,has_nightstand,has_bedframe,is_template,
  572. template_id,template_code,template_name,commission,taxes,taxrate,other_rate,extras_cost,moneyrate";
  573. var softbed_copy = GetSoftBed(softbed.softbed_id, fields);
  574. // 判断copy_bedNet.bednetcode是否存在@@字符串,如果存在,则删除@@后面的字符串,包括@@
  575. if (softbed_copy.softbed_code.IndexOf("@@") > -1)
  576. {
  577. softbed_copy.softbed_code = softbed_copy.softbed_code.Substring(softbed_copy.softbed_code.IndexOf("@@"));
  578. }
  579. softbed_copy.softbed_code += " @@";
  580. softbed_copy.softbed_code += DateTime.Now.ToString("yyyMMdd_mmhhss");
  581. softbed_copy.softbed_id = 0;
  582. SaveSoftBed(softbed_copy);
  583. newId = softbed_copy.softbed_id;
  584. }
  585. /// <summary>
  586. /// 删除软床报价
  587. /// </summary>
  588. /// <param name="billid"></param>
  589. /// <exception cref="LJCommonException"></exception>
  590. public void DeleteSoftBed(int billid)
  591. {
  592. var softbed = GetSoftBed(billid, "softbed_code,flag");
  593. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  594. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}已审核,无法删除!");
  595. DbSqlHelper.Delete(cmd, softbed);
  596. }
  597. /// <summary>
  598. /// 审核软床报价
  599. /// </summary>
  600. /// <param name="billid"></param>
  601. /// <exception cref="LJCommonException"></exception>
  602. public void AuditSoftBed(int billid)
  603. {
  604. var softbed = GetSoftBed(billid, "softbed_code,flag");
  605. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  606. if (softbed.flag == 1) throw new LJCommonException($"单据:{softbed.softbed_code}未撤审,无法审核!");
  607. softbed.flag = 1;
  608. softbed.audit_date = context.opdate;
  609. softbed.audit_emp = context.tokendata.username;
  610. DbSqlHelper.Update(cmd, "u_softbed",null,softbed, "softbed_id", "flag,audit_date,audit_emp");
  611. }
  612. /// <summary>
  613. /// 撤审软床报价
  614. /// </summary>
  615. /// <param name="billid"></param>
  616. /// <exception cref="LJCommonException"></exception>
  617. public void CAuditSoftBed(int billid)
  618. {
  619. var softbed = GetSoftBed(billid, "softbed_code,flag");
  620. if (softbed == null) throw new LJCommonException($"单据不存在,请检查! billid:{billid}");
  621. if (softbed.flag == 0) throw new LJCommonException($"单据:{softbed.softbed_code}未审核,无法撤审!");
  622. softbed.flag = 0;
  623. softbed.audit_date = null;
  624. softbed.audit_emp = string.Empty;
  625. DbSqlHelper.Update(cmd, "u_softbed", null, softbed, "softbed_id", "flag,audit_date,audit_emp");
  626. }
  627. public void CalCulateFormula(u_softbed softbed)
  628. {
  629. InitSoftBed(softbed);
  630. InitReplaceMents(softbed);
  631. decimal total_mtrl_cost = 0, total_hr_cost = 0;
  632. foreach (var mx in softbed.mxList)
  633. {
  634. InitMxReplaceMents(softbed, mx);
  635. formula.CalculateAll();
  636. // 转换后的文本公式
  637. mx.use_formula_str = formula.GetFormulaItem("【实际用量】").formula_transform;
  638. mx.price_formula_str = formula.GetFormulaItem("【成本单价】").formula_transform;
  639. // 成本单价
  640. mx.cost_price = formula.GetFormulaItem("【成本单价】").value;
  641. // 实际用量
  642. mx.actual_useqty = formula.GetFormulaItem("【实际用量】").value;
  643. // 成本金额
  644. mx.cost_amt = formula.GetFormulaItem("【成本金额】").value;
  645. // 总材料成本
  646. total_mtrl_cost += mx.cost_amt;
  647. // 总人力成本
  648. total_hr_cost += 0;
  649. }
  650. if(softbed.mxList != null && softbed.mxList.Count > 0)
  651. {
  652. softbed.total_mtrl_cost = total_mtrl_cost;
  653. softbed.total_hr_cost = total_hr_cost;
  654. softbed.total_cost = formula.GetFormulaItem("【车间成本】").value;
  655. softbed.nottax_factory_cost = formula.GetFormulaItem("【不含税出厂价】").value;
  656. softbed.nottax_dept_cost = formula.GetFormulaItem("【部门不含税价】").value;
  657. softbed.dept_cost = formula.GetFormulaItem("【部门含税价】").value;
  658. softbed.taxes = formula.GetFormulaItem("【税金】").value;
  659. softbed.foreign_cost = formula.GetFormulaItem("【外币价】").value;
  660. }
  661. //formula.CalculateAll();
  662. }
  663. private void InitSoftBed(u_softbed softbed)
  664. {
  665. var dept = Cache.GetData<u_dept, DeptMapping>(softbed.deptid);
  666. //
  667. softbed.dept_profitrate = dept.profitrate;
  668. //
  669. softbed.moneyrate = softbed.moneyrate <= 0 ? 1 : softbed.moneyrate;
  670. softbed.commission = softbed.commission <= 0 ? 1 : softbed.commission;
  671. softbed.taxrate = softbed.taxrate <= 0 ? 1 : softbed.taxrate;
  672. softbed.other_rate = softbed.other_rate <= 0 ? 1 : softbed.other_rate;
  673. softbed.dept_profitrate = softbed.dept_profitrate <= 0 ? 1 : softbed.dept_profitrate;
  674. // 检查佣金是否小于1
  675. if (softbed.commission < 1)
  676. {
  677. throw new LJCommonException("佣金点数不能小于1!");
  678. }
  679. // 检查税率是否小于1
  680. if (softbed.taxrate < 1)
  681. {
  682. throw new LJCommonException("税率不能小于1!");
  683. }
  684. // 检查额外点数是否小于1
  685. if (softbed.other_rate < 1)
  686. {
  687. throw new LJCommonException("税率不能小于1!");
  688. }
  689. // 检查部门利润率是否为0
  690. if (softbed.dept_profitrate == 0)
  691. {
  692. throw new LJCommonException("部门利润率不能为0!");
  693. }
  694. if(softbed.is_template == 1)
  695. {
  696. // 使用模板报价
  697. softbed.template_id = 0;
  698. softbed.template_code = string.Empty;
  699. softbed.template_name = string.Empty;
  700. }
  701. }
  702. private void InitReplaceMents(u_softbed softbed)
  703. {
  704. // 常量变量
  705. formula.AddFormulaItem("【部门利润率】", softbed.dept_profitrate);
  706. formula.AddFormulaItem("【佣金点数】", softbed.commission);
  707. formula.AddFormulaItem("【额外点数】", softbed.other_rate);
  708. formula.AddFormulaItem("【额外费用】", softbed.extras_cost);
  709. formula.AddFormulaItem("【汇率】", softbed.moneyrate);
  710. formula.AddFormulaItem("【税率】", softbed.taxrate);
  711. formula.AddFormulaItem("【工厂利润率】", 1);
  712. formula.AddFormulaItem("【FOB】", 0);
  713. formula.AddFormulaItem("【外销加点】", 0);
  714. formula.AddFormulaItem("【总材料成本】", softbed.total_mtrl_cost);
  715. formula.AddFormulaItem("【总人力成本】", softbed.total_hr_cost);
  716. // 公式变量
  717. formula.AddFormulaItem("【不含税出厂价】", "【车间成本】*(【工厂利润率】+【外销加点】)");
  718. formula.AddFormulaItem("【部门不含税价】", "【不含税出厂价】/【部门利润率】/( 1 - (【佣金点数】- 1))*【额外点数】+【FOB】");
  719. formula.AddFormulaItem("【部门含税价】", "【部门不含税价】*【税率】");
  720. formula.AddFormulaItem("【税金】", "【部门不含税价】* (【税率】-1)");
  721. formula.AddFormulaItem("【外币价】", "【部门含税价】/【汇率】");
  722. }
  723. private void InitMxReplaceMents(u_softbed softbed,u_softbed_mx mx)
  724. {
  725. // 默认公式变量
  726. formula.AddFormulaItem("【车间成本】", "【总材料成本】 + 【总人力成本】 + 【额外费用】");
  727. formula.AddFormulaItem("【成本单价】", "【材料单价】*1");
  728. formula.AddFormulaItem("【实际用量】", "【用料量】*(1 + 【损耗率】)");
  729. formula.AddFormulaItem("【成本金额】", "【实际用量】 * 【成本单价】");
  730. // 常量变量
  731. formula.AddFormulaItem("【下料长】", mx.cutting_length);
  732. formula.AddFormulaItem("【下料宽】", mx.cutting_width);
  733. formula.AddFormulaItem("【下料数量】", mx.cutting_qty);
  734. formula.AddFormulaItem("【用料量】", mx.useqty);
  735. formula.AddFormulaItem("【损耗率】", mx.loss_rate);
  736. formula.AddFormulaItem("【材料单价】", mx.price);
  737. if(mx.formulaid > 0)
  738. {
  739. cmd.CommandText = @"SELECT u_softbed_formula.formulaid
  740. ,u_softbed_formula.formulaname
  741. ,u_softbed_formula.use_formula
  742. ,u_softbed_formula.price_formula
  743. FROM u_softbed_formula
  744. WHERE formulaid = @formulaid";
  745. cmd.Parameters.Clear();
  746. cmd.Parameters.AddWithValue("@formulaid", mx.formulaid);
  747. using(var reader = cmd.ExecuteReader())
  748. {
  749. if(reader.Read())
  750. {
  751. formula.AddFormulaItem("【实际用量】", Convert.ToString(reader["use_formula"]));
  752. formula.AddFormulaItem("【成本单价】", Convert.ToString(reader["price_formula"]));
  753. }
  754. }
  755. }
  756. }
  757. #region 通用公式
  758. private CalculateFormula formula = new CalculateFormula();
  759. #endregion
  760. }
  761. }