SoftBedHelper.cs 30 KB

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