You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
34 KiB

3 years ago
  1. using EasyBL.WebApi.Message;
  2. using Entity;
  3. using Entity.Sugar;
  4. using Entity.ViewModels;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using SqlSugar;
  8. using SqlSugar.Base;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Linq;
  13. namespace EasyBL.WEBAPP.OPM
  14. {
  15. public class OpmComService : ServiceBase
  16. {
  17. #region 驗證發票號碼是否重複key入
  18. /// <summary>
  19. /// 驗證發票號碼是否重複key入
  20. /// </summary>
  21. /// <param name="i_crm">todo: describe i_crm parameter on CheckInvoiceNumber</param>
  22. /// <returns></returns>
  23. public ResponseMessage CheckInvoiceNumber(RequestMessage i_crm)
  24. {
  25. ResponseMessage rm = null;
  26. string sMsg = null;
  27. var db = SugarBase.DB;
  28. try
  29. {
  30. do
  31. {
  32. var bExsit = false;
  33. var sInvoiceNumber = _fetchString(i_crm, @"InvoiceNumber");
  34. bExsit = db.Queryable<OTB_OPM_BillInfo>().Any(it => it.InvoiceNumber == sInvoiceNumber && it.OrgID == i_crm.ORIGID);
  35. rm = new SuccessResponseMessage(null, i_crm);
  36. rm.DATA.Add(BLWording.REL, bExsit);
  37. } while (false);
  38. }
  39. catch (Exception ex)
  40. {
  41. sMsg = Util.GetLastExceptionMsg(ex);
  42. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"CheckInvoiceNumber(驗證發票號碼是否重複key入)", @"", @"", @"");
  43. }
  44. finally
  45. {
  46. if (null != sMsg)
  47. {
  48. rm = new ErrorResponseMessage(sMsg, i_crm);
  49. }
  50. }
  51. return rm;
  52. }
  53. #endregion 驗證發票號碼是否重複key入
  54. #region 獲取賬單資料
  55. /// <summary>
  56. /// 獲取賬單資料
  57. /// </summary>
  58. /// <param name="i_crm">todo: describe i_crm parameter on GetBills</param>
  59. /// <returns></returns>
  60. public ResponseMessage GetBills(RequestMessage i_crm)
  61. {
  62. ResponseMessage rm = null;
  63. string sMsg = null;
  64. var db = SugarBase.DB;
  65. try
  66. {
  67. do
  68. {
  69. var sBillNO = _fetchString(i_crm, @"BillNO");
  70. //20200721 調整抓取資料回推由一年改為兩年
  71. var rDate = DateTime.Now.AddYears(-2);
  72. var saBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  73. .OrderBy(x => x.CreateDate)
  74. .Where(x => x.OrgID == i_crm.ORIGID && x.CreateDate > rDate)
  75. .WhereIF(!string.IsNullOrEmpty(sBillNO), x => x.BillNO == sBillNO)
  76. .Select(x => new { x.BillNO })
  77. .ToList();
  78. rm = new SuccessResponseMessage(null, i_crm);
  79. rm.DATA.Add(BLWording.REL, saBillInfo);
  80. } while (false);
  81. }
  82. catch (Exception ex)
  83. {
  84. sMsg = Util.GetLastExceptionMsg(ex);
  85. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetBills(獲取賬單資料)", @"", @"", @"");
  86. }
  87. finally
  88. {
  89. if (null != sMsg)
  90. {
  91. rm = new ErrorResponseMessage(sMsg, i_crm);
  92. }
  93. }
  94. return rm;
  95. }
  96. #endregion 獲取賬單資料
  97. #region 預設預約單資料
  98. /// <summary>
  99. /// 預設預約單資料
  100. /// </summary>
  101. /// <param name="i_crm">todo: describe i_crm parameter on InitAppoint</param>
  102. /// <returns></returns>
  103. public ResponseMessage InitAppoint(RequestMessage i_crm)
  104. {
  105. ResponseMessage rm = null;
  106. string sMsg = null;
  107. var db = SugarBase.GetIntance();
  108. try
  109. {
  110. do
  111. {
  112. var sAppointNO = _fetchString(i_crm, @"AppointNO");
  113. //預約單基本資料
  114. var oTempl = db.Queryable<OTB_WSM_PackingOrder, OTB_OPM_Exhibition>(
  115. (t1, t2) => new object[]
  116. {
  117. JoinType.Inner, t1.ExhibitionNO == t2.SN.ToString()
  118. }
  119. )
  120. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.AppointNO == sAppointNO)
  121. .Select((t1, t2) => new OTB_OPM_OtherExhibition
  122. {
  123. OrgID = t1.OrgID,
  124. ExhibitionNO = t2.SN.ToString(),
  125. ImportBillName = t2.Exhibitioname_TW,
  126. ImportBillEName = t2.Exhibitioname_EN,
  127. ExhibitionDateStart = t2.ExhibitionDateStart,
  128. ExhibitionDateEnd = t2.ExhibitionDateEnd,
  129. Hall = t2.ExhibitionAddress,
  130. MuseumMumber = t1.MuseumMumber,
  131. Supplier = @"",
  132. Contactor = t1.AppointUser,
  133. Telephone = t1.AppointTel,
  134. SitiContactor = t1.Contactor,
  135. SitiTelephone = t1.ContactTel,
  136. ApproachTime = t1.ApproachTime,
  137. ExitTime = t1.ExitTime
  138. })
  139. .Single();
  140. var oImportCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_Customers>(
  141. (t1, t2) => new object[]
  142. {
  143. JoinType.Inner, t1.CustomerId == t2.guid.ToString()
  144. }
  145. )
  146. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.AppointNO == sAppointNO && t2.IsAudit == "Y" && t2.Effective == "Y")
  147. .Select((t1, t2) => new
  148. {
  149. t2.guid,
  150. })
  151. .Single();
  152. oTempl.Supplier = oImportCustomers.guid;
  153. //該展覽所有廠商(排除已經產生賬單的資料)
  154. var saImportCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_Customers>(
  155. (t1, t2) => new object[]
  156. {
  157. JoinType.Inner, t1.CustomerId == t2.guid.ToString()
  158. }
  159. )
  160. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.ExhibitionNO == oTempl.ExhibitionNO && t2.IsAudit == "Y" && t2.Effective == "Y" && SqlFunc.IsNullOrEmpty(t1.OtherId))
  161. .Select((t1, t2) => new
  162. {
  163. t1.AppointNO,
  164. t2.guid,
  165. })
  166. .ToList();
  167. var saKeys = saImportCustomers.Select(x => x.guid).ToList();
  168. //抓去正式客戶資料
  169. var saCustomers = db.Queryable<OTB_CRM_Customers>()
  170. .Where(x => x.OrgID == i_crm.ORIGID && saKeys.Contains(x.guid))
  171. .Select(x => new
  172. {
  173. AppointNO = "",
  174. x.guid,
  175. x.CustomerNO,
  176. x.UniCode,
  177. x.CustomerCName,
  178. x.CustomerEName,
  179. x.Telephone,
  180. x.Email
  181. })
  182. .ToList();
  183. //添加預約單號碼
  184. var saLast = (from e in saCustomers
  185. join f in saImportCustomers on e.guid equals f.guid
  186. select new
  187. {
  188. f.AppointNO,
  189. e.guid,
  190. e.CustomerNO,
  191. e.UniCode,
  192. e.CustomerCName,
  193. e.CustomerEName,
  194. e.Telephone,
  195. e.Email
  196. }).Distinct();
  197. var map = new Map
  198. {
  199. { "Base", oTempl },
  200. { "Customers", saLast }
  201. };
  202. rm = new SuccessResponseMessage(null, i_crm);
  203. rm.DATA.Add(BLWording.REL, map);
  204. } while (false);
  205. }
  206. catch (Exception ex)
  207. {
  208. sMsg = Util.GetLastExceptionMsg(ex);
  209. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"InitAppoint(預設預約單資料)", @"", @"", @"");
  210. }
  211. finally
  212. {
  213. if (null != sMsg)
  214. {
  215. rm = new ErrorResponseMessage(sMsg, i_crm);
  216. }
  217. }
  218. return rm;
  219. }
  220. #endregion 預設預約單資料
  221. #region 獲取賬單關聯
  222. /// <summary>
  223. /// 獲取賬單關聯
  224. /// </summary>
  225. /// <param name="i_crm">todo: describe i_crm parameter on InitAppoint</param>
  226. /// <returns></returns>
  227. public ResponseMessage GetBillAssociated(RequestMessage i_crm)
  228. {
  229. ResponseMessage rm = null;
  230. string sMsg = null;
  231. var db = SugarBase.GetIntance();
  232. try
  233. {
  234. do
  235. {
  236. var sAppointNO = _fetchString(i_crm, @"AppointNO");
  237. var sOtherId = _fetchString(i_crm, @"OtherId");
  238. var sSupplierID = _fetchString(i_crm, @"SupplierID");
  239. var map = new Map();
  240. var tb_FeeItems = new DataTable(@"tb");
  241. var oPackingOrder = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_Customers>(
  242. (t1, t2) => new object[]
  243. {
  244. JoinType.Inner, t1.CustomerId == t2.guid
  245. }
  246. )
  247. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t2.IsAudit == "Y" && t2.Effective == "Y")
  248. .WhereIF(!string.IsNullOrEmpty(sAppointNO), (t1, t2) => t1.AppointNO == sAppointNO)
  249. .WhereIF(!string.IsNullOrEmpty(sOtherId), (t1, t2) => t1.OtherId == sOtherId)
  250. .WhereIF(!string.IsNullOrEmpty(sSupplierID), (t1, t2) => t2.guid == sSupplierID)
  251. .Select((t1, t2) => t1).Single();
  252. if (oPackingOrder != null)
  253. {
  254. var jaPackingInfo = (JArray)JsonConvert.DeserializeObject(oPackingOrder.PackingInfo);
  255. var iAllNumber = 0;
  256. var iAllWeight = 0;
  257. Decimal iAllVolume = 0;
  258. var sContactorId = "";
  259. var sContactorName = "";
  260. if (jaPackingInfo.Count > 0)
  261. {
  262. foreach (var jo in jaPackingInfo)
  263. {
  264. var iNumber = int.Parse(jo["ExpoNumber"].ToString());
  265. var iWeight = int.Parse(jo["ExpoWeight"].ToString()) * iNumber;
  266. var iVolume = Math.Round(Convert.ToDecimal(jo["ExpoLen"].ToString()) * Convert.ToDecimal(jo["ExpoWidth"].ToString()) * Convert.ToDecimal(jo["ExpoHeight"].ToString()) * iNumber / Convert.ToDecimal("1000000"), 2);
  267. iAllNumber += iNumber;
  268. iAllWeight += iWeight;
  269. iAllVolume = iAllVolume + iVolume;
  270. }
  271. var oCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_Customers>(
  272. (t1, t2) => new object[]
  273. {
  274. JoinType.Inner, t1.CustomerId == t2.guid
  275. }
  276. )
  277. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t2.IsAudit == "Y" && t2.Effective == "Y")
  278. .WhereIF(!string.IsNullOrEmpty(sAppointNO), (t1, t2) => t1.AppointNO == sAppointNO)
  279. .WhereIF(!string.IsNullOrEmpty(sOtherId), (t1, t2) => t1.OtherId == sOtherId)
  280. .WhereIF(!string.IsNullOrEmpty(sSupplierID), (t1, t2) => t2.guid == sSupplierID)
  281. .Select((t1, t2) => t2).Single();
  282. var oContactors = db.Queryable<OTB_CRM_Contactors>()
  283. .Where(t1 => t1.CustomerId == oCustomers.guid && t1.ContactorName == oPackingOrder.Contactor).Single();
  284. if (!string.IsNullOrEmpty(oCustomers.guid))
  285. {
  286. sContactorId = oContactors.guid;
  287. sContactorName = oContactors.ContactorName;
  288. }
  289. map = new Map
  290. {
  291. {"CustomerId",oCustomers.guid },
  292. {"CustomerCode",oCustomers.CustomerNO },
  293. {"UniCode",oCustomers.UniCode },
  294. {"ContactorId",sContactorId },
  295. {"ContactorName",sContactorName },
  296. {"ContactTel",oPackingOrder.ContactTel },
  297. {"Number",iAllNumber },
  298. {"Unit","PKG" },
  299. {"Weight",iAllWeight },
  300. {"Volume",iAllVolume }
  301. };
  302. }
  303. var saPackingInfo = (JArray)JsonConvert.DeserializeObject(oPackingOrder.PackingInfo);
  304. tb_FeeItems.Columns.Add(@"guid", typeof(String));//費用代號
  305. tb_FeeItems.Columns.Add(@"FinancialCode", typeof(String));//費用代號
  306. tb_FeeItems.Columns.Add(@"FinancialCostStatement", typeof(String));//費用說明
  307. tb_FeeItems.Columns.Add(@"FinancialCurrency", typeof(String));//幣別
  308. tb_FeeItems.Columns.Add(@"FinancialUnitPrice", typeof(String));//單價
  309. tb_FeeItems.Columns.Add(@"FinancialNumber", typeof(String));//FinancialNumber
  310. tb_FeeItems.Columns.Add(@"FinancialUnit", typeof(String));//單位
  311. tb_FeeItems.Columns.Add(@"FinancialAmount", typeof(String));//金額
  312. tb_FeeItems.Columns.Add(@"FinancialExchangeRate", typeof(String));//匯率
  313. tb_FeeItems.Columns.Add(@"FinancialTWAmount", typeof(String));//台幣金額
  314. tb_FeeItems.Columns.Add(@"FinancialTaxRate", typeof(String));//稅率
  315. tb_FeeItems.Columns.Add(@"FinancialTax", typeof(String));//稅額
  316. tb_FeeItems.Columns.Add(@"Memo", typeof(String));//稅額
  317. tb_FeeItems.Columns.Add(@"CreateUser", typeof(String));//創建人
  318. tb_FeeItems.Columns.Add(@"CreateDate", typeof(String));//創建時間
  319. var oExhibitionRules = db.Queryable<OTB_WSM_ExhibitionRules, OTB_OPM_Exhibition>(
  320. (t1, t2) => new object[]
  321. {
  322. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Guid == t2.CostRulesId
  323. }
  324. )
  325. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t2.SN.ToString() == oPackingOrder.ExhibitionNO)
  326. .Select((t1, t2) => t1).Single();
  327. foreach (JObject packingInfo in saPackingInfo)
  328. {
  329. try
  330. {
  331. var bExpoStack = (bool)(packingInfo["ExpoStack"] ?? "false");
  332. var bExpoStorage = (bool)(packingInfo["ExpoStorage"] ?? "false");
  333. var bExpoSplit = (bool)(packingInfo["ExpoSplit"] ?? "false");
  334. var bExpoPack = (bool)(packingInfo["ExpoPack"] ?? "false");
  335. var bExpoFeed = (bool)(packingInfo["ExpoFeed"] ?? "false");
  336. var tb_row = tb_FeeItems.NewRow();
  337. if (bExpoStack)
  338. {//堆高機
  339. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 1);
  340. tb_FeeItems.Rows.Add(tb_row);
  341. }
  342. if (bExpoSplit)
  343. {//進場服務費
  344. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 2);
  345. tb_FeeItems.Rows.Add(tb_row);
  346. }
  347. if (bExpoPack)
  348. {//退場服務費
  349. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 3);
  350. tb_FeeItems.Rows.Add(tb_row);
  351. }
  352. if (bExpoFeed)
  353. {//空箱收送與儲存(空箱收送)
  354. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 4);
  355. tb_FeeItems.Rows.Add(tb_row);
  356. }
  357. if (bExpoStorage)
  358. {//空箱收送與儲存(儲存)
  359. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 5);
  360. tb_FeeItems.Rows.Add(tb_row);
  361. }
  362. }
  363. catch { }
  364. }
  365. }
  366. var mapRel = new Map
  367. {
  368. { "Base", map },
  369. { "Fees", tb_FeeItems }
  370. };
  371. rm = new SuccessResponseMessage(null, i_crm);
  372. rm.DATA.Add(BLWording.REL, mapRel);
  373. } while (false);
  374. }
  375. catch (Exception ex)
  376. {
  377. sMsg = Util.GetLastExceptionMsg(ex);
  378. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetBillAssociated(獲取賬單關聯)", @"", @"", @"");
  379. }
  380. finally
  381. {
  382. if (null != sMsg)
  383. {
  384. rm = new ErrorResponseMessage(sMsg, i_crm);
  385. }
  386. }
  387. return rm;
  388. }
  389. /// <summary>
  390. /// </summary>
  391. /// <param name="i_crm">todo: describe i_crm parameter on GetFeeItemsInfo</param>
  392. /// <param name="tb">todo: describe tb parameter on GetFeeItemsInfo</param>
  393. /// <param name="joInfo">todo: describe joInfo parameter on GetFeeItemsInfo</param>
  394. /// <param name="rule">todo: describe rule parameter on GetFeeItemsInfo</param>
  395. /// <param name="flag">todo: describe flag parameter on GetFeeItemsInfo</param>
  396. /// <returns></returns>
  397. private static DataRow GetFeeItemsInfo(RequestMessage i_crm, DataTable tb, JObject joInfo, OTB_WSM_ExhibitionRules rule, int flag)
  398. {
  399. var tb_row = tb.NewRow();
  400. var dTransferRate = Math.Round(Convert.ToDecimal("100") / Convert.ToDecimal("105"), 5);
  401. var iExpoNumber = Convert.ToInt32(joInfo["ExpoNumber"].ToString());
  402. var dTotalCBM = Convert.ToDecimal(joInfo["TotalCBM"].ToString());
  403. var dExpoWeightTon = Convert.ToDecimal(joInfo["ExpoWeightTon"].ToString());
  404. var iExpoDays = Convert.ToInt32(joInfo["ExpoDays"].ToString());
  405. var iPackingPrice = Math.Round(Convert.ToDecimal(rule.PackingPrice) * dTransferRate, 2);
  406. var iFeedingPrice = Math.Round(Convert.ToDecimal(rule.FeedingPrice) * dTransferRate, 2);
  407. var iStoragePrice = Math.Round(Convert.ToDecimal(rule.StoragePrice) * dTransferRate, 2);
  408. dTotalCBM = dTotalCBM < 1 ? 1 : dTotalCBM;
  409. tb_row[@"guid"] = Guid.NewGuid();
  410. tb_row[@"Memo"] = @"";
  411. tb_row[@"FinancialCurrency"] = @"NTD";
  412. tb_row[@"FinancialNumber"] = iExpoNumber;
  413. switch (flag)
  414. {
  415. case 1://堆高機
  416. {
  417. tb_row[@"FinancialCode"] = @"TEC06";
  418. tb_row[@"FinancialCostStatement"] = @"堆高機";
  419. var jaCostRules = (JArray)JsonConvert.DeserializeObject(rule.CostRules);
  420. var oCurRule = GetCurRule(joInfo, jaCostRules);
  421. var iPrice = Math.Round(int.Parse(oCurRule["Price"].ToString()) * dTransferRate, 2);
  422. tb_row[@"FinancialUnitPrice"] = iPrice;
  423. if (oCurRule["PricingMode"].ToString() == "T")
  424. {
  425. tb_row[@"FinancialNumber"] = dExpoWeightTon * iExpoNumber;
  426. tb_row[@"FinancialUnit"] = @"TON";
  427. tb_row[@"FinancialAmount"] = iPrice * dExpoWeightTon * iExpoNumber;
  428. }
  429. else
  430. {
  431. tb_row[@"FinancialUnit"] = @"SHPT";
  432. tb_row[@"FinancialAmount"] = iPrice * iExpoNumber;
  433. }
  434. }
  435. break;
  436. case 2://進場服務費
  437. {
  438. tb_row[@"FinancialCode"] = @"TEC34";
  439. tb_row[@"FinancialCostStatement"] = @"進場服務費";
  440. tb_row[@"FinancialUnitPrice"] = iPackingPrice;
  441. tb_row[@"FinancialNumber"] = dTotalCBM * iExpoNumber;
  442. tb_row[@"FinancialUnit"] = @"CBM";
  443. tb_row[@"FinancialAmount"] = iPackingPrice * dTotalCBM * iExpoNumber;
  444. }
  445. break;
  446. case 3://退場服務費
  447. {
  448. tb_row[@"FinancialCode"] = @"TEC35";
  449. tb_row[@"FinancialCostStatement"] = @"退場服務費";
  450. tb_row[@"FinancialUnitPrice"] = iPackingPrice;
  451. tb_row[@"FinancialNumber"] = dTotalCBM * iExpoNumber;
  452. tb_row[@"FinancialUnit"] = @"CBM";
  453. tb_row[@"FinancialAmount"] = iPackingPrice * dTotalCBM * iExpoNumber;
  454. }
  455. break;
  456. case 4://空箱收送與儲存(空箱收送)
  457. {
  458. tb_row[@"FinancialCode"] = @"TEC05";
  459. tb_row[@"FinancialCostStatement"] = @"空箱收送與儲存";
  460. tb_row[@"FinancialUnitPrice"] = iFeedingPrice;
  461. tb_row[@"FinancialNumber"] = dTotalCBM * iExpoNumber;
  462. tb_row[@"FinancialUnit"] = @"CBM";
  463. tb_row[@"FinancialAmount"] = iFeedingPrice * dTotalCBM * iExpoNumber;
  464. }
  465. break;
  466. case 5://空箱收送與儲存(儲存)
  467. {
  468. tb_row[@"FinancialCode"] = @"TEC05";
  469. tb_row[@"FinancialCostStatement"] = @"空箱收送與儲存";
  470. tb_row[@"FinancialUnitPrice"] = iStoragePrice * dTotalCBM * iExpoNumber;
  471. tb_row[@"FinancialNumber"] = iExpoDays;
  472. tb_row[@"FinancialUnit"] = @"DAY";
  473. tb_row[@"FinancialAmount"] = iStoragePrice * dTotalCBM * iExpoNumber * iExpoDays;
  474. }
  475. break;
  476. default:
  477. break;
  478. }
  479. tb_row[@"FinancialTWAmount"] = tb_row[@"FinancialAmount"];
  480. tb_row[@"FinancialTaxRate"] = 0.05;
  481. tb_row[@"FinancialExchangeRate"] = 1;
  482. tb_row[@"FinancialTax"] = Convert.ToDouble(tb_row[@"FinancialTWAmount"].ToString()) * 0.05;
  483. tb_row[@"CreateUser"] = i_crm.USERID ?? @"";
  484. tb_row[@"CreateDate"] = DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss");
  485. return tb_row;
  486. }
  487. private static JObject GetCurRule(JObject joData, JArray rules)
  488. {
  489. var jo = new JObject();
  490. var weight = (Decimal)joData["ExpoWeightTon"];
  491. foreach (JObject rule in rules)
  492. {
  493. var rule_min = PackNum(rule["Weight_Min"]);
  494. var rule_max = PackNum(rule["Weight_Max"]);
  495. if (weight >= rule_min && weight < rule_max || (weight == 30 && rule_max == 30))
  496. {
  497. jo = rule;
  498. break;
  499. }
  500. }
  501. return jo;
  502. }
  503. private static Decimal PackNum(object str)
  504. {
  505. return Convert.ToDecimal((str == null ? "" : str.ToString()) == "" ? "0" : str);
  506. }
  507. #endregion 獲取賬單關聯
  508. #region 複製費用項目
  509. /// <summary>
  510. /// 複製費用項目
  511. /// </summary>
  512. /// <param name="i_crm">todo: describe i_crm parameter on InitAppoint</param>
  513. /// <returns></returns>
  514. public ResponseMessage GetBillInfos(RequestMessage i_crm)
  515. {
  516. ResponseMessage rm = null;
  517. string sMsg = null;
  518. var db = SugarBase.GetIntance();
  519. try
  520. {
  521. do
  522. {
  523. var sExhibitionNO = _fetchString(i_crm, @"ExhibitionNO");
  524. var sBillNO = _fetchString(i_crm, @"BillNO");
  525. var saBillInfo = db.Queryable<OTB_OPM_BillInfo, OTB_SYS_Members, OTB_SYS_Members, OTB_OPM_Exhibition, OTB_CRM_Customers>(
  526. (t1, t2, t3, t4, t5) => new object[]
  527. {
  528. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  529. JoinType.Inner, t1.OrgID == t3.OrgID && t1.ResponsiblePerson == t3.MemberID,
  530. JoinType.Inner, t1.ExhibitionNO == t4.SN.ToString(),
  531. JoinType.Left, t1.Payer == t5.guid
  532. }
  533. )
  534. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID)
  535. .WhereIF(!string.IsNullOrEmpty(sExhibitionNO), (t1, t2, t3, t4, t5) => t1.ExhibitionNO == sExhibitionNO)
  536. .WhereIF(!string.IsNullOrEmpty(sBillNO), (t1, t2, t3, t4, t5) => t1.BillNO == sBillNO)
  537. .Select((t1, t2, t3, t4, t5) => new View_OPM_BillInfo
  538. {
  539. SN = SqlFunc.GetSelfAndAutoFill(t1.SN),
  540. CreateUserName = t2.MemberName,
  541. ResponsiblePersonName = t3.MemberName,
  542. ExhibitioShotName = t4.ExhibitioShotName_CN,
  543. ExhibitioName = t4.Exhibitioname_TW,
  544. PayerName = SqlFunc.IIF(SqlFunc.HasValue(t5.CustomerCName), t5.CustomerCName, t5.CustomerEName)
  545. })
  546. .OrderBy("t1.CreateDate")
  547. .ToList();
  548. rm = new SuccessResponseMessage(null, i_crm);
  549. rm.DATA.Add(BLWording.REL, saBillInfo);
  550. } while (false);
  551. }
  552. catch (Exception ex)
  553. {
  554. sMsg = Util.GetLastExceptionMsg(ex);
  555. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetBillInfos(複製費用項目)", @"", @"", @"");
  556. }
  557. finally
  558. {
  559. if (null != sMsg)
  560. {
  561. rm = new ErrorResponseMessage(sMsg, i_crm);
  562. }
  563. }
  564. return rm;
  565. }
  566. #endregion 複製費用項目
  567. #region 取得帳單存取權限
  568. public ResponseMessage GetExhibitionBillAuthorize(RequestMessage i_crm)
  569. {
  570. ResponseMessage rm = null;
  571. string sMsg = null;
  572. var db = SugarBase.GetIntance();
  573. try
  574. {
  575. do
  576. {
  577. var MemberToAccess = new List<string>();
  578. // Account
  579. //select MemberIDs from OVW_SYS_Rules where RuleID in ('Account','Admin')
  580. var sRuleIDs = _fetchString(i_crm, @"RuleID");
  581. var sResponsiblePerson = _fetchString(i_crm, @"ResponsiblePerson");
  582. var sMemberdb = new SimpleClient<OTB_SYS_Members>(db);
  583. var spOrgID = new SugarParameter("@OrgID", i_crm.ORIGID);
  584. var spResponsiblePerson = new SugarParameter("@MemberID", sResponsiblePerson);
  585. var spRuleID = new SugarParameter("@RuleID", sRuleIDs);
  586. //直屬主管、部門主管
  587. var Supervisors = db.Ado.SqlQuery<string>(@"SELECT M.ImmediateSupervisor + ',' + D.ChiefOfDepartmentID FROM OTB_SYS_Members M
  588. LEFT JOIN OTB_SYS_Departments D ON D.DepartmentID = M.DepartmentID WHERE M.OrgID = @OrgID AND M.MemberID = @MemberID ", spOrgID, spResponsiblePerson);
  589. MemberToAccess.AddRange(Supervisors);
  590. //
  591. var oVW_SYS_Rules = db.Queryable<OVW_SYS_Rules>().Where(r => r.DelStatus == "N" && r.OrgID == i_crm.ORIGID)
  592. .WhereIF(!string.IsNullOrWhiteSpace(sRuleIDs), r => sRuleIDs.Contains(r.RuleID)).Select(c => c.MemberIDs).ToList();
  593. MemberToAccess.AddRange(oVW_SYS_Rules);
  594. var BillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, "BillAuditor");
  595. MemberToAccess.Add(BillAuditor);
  596. //var saBillInfo = db.Queryable<OTB_OPM_BillInfo, OTB_SYS_Members, OTB_SYS_Members, OTB_OPM_Exhibition, OTB_CRM_Customers>(
  597. // (t1, t2, t3, t4, t5) => new object[]
  598. // {
  599. // JoinType.Inner, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  600. // JoinType.Inner, t1.OrgID == t3.OrgID && t1.ResponsiblePerson == t3.MemberID,
  601. // JoinType.Inner, t1.OrgID == t4.OrgID && t1.ExhibitionNO == t4.SN.ToString(),
  602. // JoinType.Left, t1.OrgID == t5.OrgID && t1.Payer == t5.guid
  603. // }
  604. // )
  605. // .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID)
  606. // .WhereIF(!string.IsNullOrEmpty(sExhibitionNO), (t1, t2, t3, t4, t5) => t1.ExhibitionNO == sExhibitionNO)
  607. // .WhereIF(!string.IsNullOrEmpty(sBillNO), (t1, t2, t3, t4, t5) => t1.BillNO == sBillNO)
  608. // .Select((t1, t2, t3, t4, t5) => new View_OPM_BillInfo
  609. // {
  610. // SN = SqlFunc.GetSelfAndAutoFill(t1.SN),
  611. // CreateUserName = t2.MemberName,
  612. // ResponsiblePersonName = t3.MemberName,
  613. // ExhibitioShotName = t4.ExhibitioShotName_CN,
  614. // ExhibitioName = t4.Exhibitioname_TW,
  615. // PayerName = SqlFunc.IIF(SqlFunc.HasValue(t5.CustomerCName), t5.CustomerCName, t5.CustomerEName)
  616. // })
  617. // .OrderBy("t1.CreateDate")
  618. // .ToList();
  619. var AccessAccounts = String.Join(",", MemberToAccess);
  620. rm = new SuccessResponseMessage(null, i_crm);
  621. rm.DATA.Add(BLWording.REL, AccessAccounts);
  622. } while (false);
  623. }
  624. catch (Exception ex)
  625. {
  626. sMsg = Util.GetLastExceptionMsg(ex);
  627. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetExhibitionBillAuthorize(取得帳單存取權限)", @"", @"", @"");
  628. }
  629. finally
  630. {
  631. if (null != sMsg)
  632. {
  633. rm = new ErrorResponseMessage(sMsg, i_crm);
  634. }
  635. }
  636. return rm;
  637. }
  638. #endregion
  639. }
  640. }