SoftBedHelper.cs 29 KB

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