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.

701 lines
35 KiB

2 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.OrgID == t2.OrgID && 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_ImportCustomers>(
  141. (t1, t2) => new object[]
  142. {
  143. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CustomerId == t2.guid.ToString()
  144. }
  145. )
  146. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.AppointNO == sAppointNO)
  147. .Select((t1, t2) => new
  148. {
  149. t2.FormalGuid,
  150. })
  151. .Single();
  152. oTempl.Supplier = oImportCustomers.FormalGuid;
  153. //該展覽所有廠商(排除已經產生賬單的資料)
  154. var saImportCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_ImportCustomers>(
  155. (t1, t2) => new object[]
  156. {
  157. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CustomerId == t2.guid.ToString()
  158. }
  159. )
  160. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.ExhibitionNO == oTempl.ExhibitionNO && t2.IsFormal == true && SqlFunc.IsNullOrEmpty(t1.OtherId))
  161. .Select((t1, t2) => new
  162. {
  163. t1.AppointNO,
  164. t2.FormalGuid,
  165. })
  166. .ToList();
  167. var saKeys = saImportCustomers.Select(x => x.FormalGuid).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.FormalGuid
  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. if (i_crm.ORIGID == "TG")
  242. {
  243. var oPackingOrder = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_ImportCustomers, OTB_CRM_Customers>(
  244. (t1, t2, t3) => new object[]
  245. {
  246. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CustomerId == t2.guid.ToString(),
  247. JoinType.Inner, t2.OrgID == t3.OrgID && t2.FormalGuid == t3.guid
  248. }
  249. )
  250. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID)
  251. .WhereIF(!string.IsNullOrEmpty(sAppointNO), (t1, t2, t3) => t1.AppointNO == sAppointNO)
  252. .WhereIF(!string.IsNullOrEmpty(sOtherId), (t1, t2, t3) => t1.OtherId == sOtherId)
  253. .WhereIF(!string.IsNullOrEmpty(sSupplierID), (t1, t2, t3) => t2.FormalGuid == sSupplierID && t3.guid == sSupplierID)
  254. .Select((t1, t2, t3) => t1).Single();
  255. if (oPackingOrder != null)
  256. {
  257. var jaPackingInfo = (JArray)JsonConvert.DeserializeObject(oPackingOrder.PackingInfo);
  258. var iAllNumber = 0;
  259. var iAllWeight = 0;
  260. Decimal iAllVolume = 0;
  261. var sContactorId = "";
  262. var sContactorName = "";
  263. if (jaPackingInfo.Count > 0)
  264. {
  265. foreach (var jo in jaPackingInfo)
  266. {
  267. var iNumber = int.Parse(jo["ExpoNumber"].ToString());
  268. var iWeight = int.Parse(jo["ExpoWeight"].ToString()) * iNumber;
  269. var iVolume = Math.Round(Convert.ToDecimal(jo["ExpoLen"].ToString()) * Convert.ToDecimal(jo["ExpoWidth"].ToString()) * Convert.ToDecimal(jo["ExpoHeight"].ToString()) * iNumber / Convert.ToDecimal("1000000"), 2);
  270. iAllNumber += iNumber;
  271. iAllWeight += iWeight;
  272. iAllVolume = iAllVolume + iVolume;
  273. }
  274. var oCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_ImportCustomers, OTB_CRM_Customers>(
  275. (t1, t2, t3) => new object[]
  276. {
  277. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CustomerId == t2.guid.ToString(),
  278. JoinType.Inner, t2.OrgID == t3.OrgID && t2.FormalGuid == t3.guid
  279. }
  280. )
  281. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID)
  282. .WhereIF(!string.IsNullOrEmpty(sAppointNO), (t1, t2, t3) => t1.AppointNO == sAppointNO)
  283. .WhereIF(!string.IsNullOrEmpty(sOtherId), (t1, t2, t3) => t1.OtherId == sOtherId)
  284. .WhereIF(!string.IsNullOrEmpty(sSupplierID), (t1, t2, t3) => t2.FormalGuid == sSupplierID && t3.guid == sSupplierID)
  285. .Select((t1, t2, t3) => t3).Single();
  286. if (!string.IsNullOrEmpty(oCustomers.Contactors))
  287. {
  288. var jaContactors = (JArray)JsonConvert.DeserializeObject(oCustomers.Contactors);
  289. foreach (var joContacto in jaContactors)
  290. {
  291. if (joContacto["FullName"].ToString() == oPackingOrder.Contactor)
  292. {
  293. sContactorId = joContacto["guid"].ToString();
  294. sContactorName = joContacto["FullName"].ToString();
  295. }
  296. }
  297. }
  298. map = new Map
  299. {
  300. {"CustomerId",oCustomers.guid },
  301. {"CustomerCode",oCustomers.CustomerNO },
  302. {"UniCode",oCustomers.UniCode },
  303. {"ContactorId",sContactorId },
  304. {"ContactorName",sContactorName },
  305. {"ContactTel",oPackingOrder.ContactTel },
  306. {"Number",iAllNumber },
  307. {"Unit","PKG" },
  308. {"Weight",iAllWeight },
  309. {"Volume",iAllVolume }
  310. };
  311. }
  312. var saPackingInfo = (JArray)JsonConvert.DeserializeObject(oPackingOrder.PackingInfo);
  313. tb_FeeItems.Columns.Add(@"guid", typeof(String));//費用代號
  314. tb_FeeItems.Columns.Add(@"FinancialCode", typeof(String));//費用代號
  315. tb_FeeItems.Columns.Add(@"FinancialCostStatement", typeof(String));//費用說明
  316. tb_FeeItems.Columns.Add(@"FinancialCurrency", typeof(String));//幣別
  317. tb_FeeItems.Columns.Add(@"FinancialUnitPrice", typeof(String));//單價
  318. tb_FeeItems.Columns.Add(@"FinancialNumber", typeof(String));//FinancialNumber
  319. tb_FeeItems.Columns.Add(@"FinancialUnit", typeof(String));//單位
  320. tb_FeeItems.Columns.Add(@"FinancialAmount", typeof(String));//金額
  321. tb_FeeItems.Columns.Add(@"FinancialExchangeRate", typeof(String));//匯率
  322. tb_FeeItems.Columns.Add(@"FinancialTWAmount", typeof(String));//台幣金額
  323. tb_FeeItems.Columns.Add(@"FinancialTaxRate", typeof(String));//稅率
  324. tb_FeeItems.Columns.Add(@"FinancialTax", typeof(String));//稅額
  325. tb_FeeItems.Columns.Add(@"Memo", typeof(String));//稅額
  326. tb_FeeItems.Columns.Add(@"CreateUser", typeof(String));//創建人
  327. tb_FeeItems.Columns.Add(@"CreateDate", typeof(String));//創建時間
  328. var oExhibitionRules = db.Queryable<OTB_WSM_ExhibitionRules, OTB_OPM_Exhibition>(
  329. (t1, t2) => new object[]
  330. {
  331. JoinType.Inner, t1.OrgID == t2.OrgID && t1.Guid == t2.CostRulesId
  332. }
  333. )
  334. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t2.SN.ToString() == oPackingOrder.ExhibitionNO)
  335. .Select((t1, t2) => t1).Single();
  336. foreach (JObject packingInfo in saPackingInfo)
  337. {
  338. try
  339. {
  340. var bExpoStack = (bool)(packingInfo["ExpoStack"] ?? "false");
  341. var bExpoStorage = (bool)(packingInfo["ExpoStorage"] ?? "false");
  342. var bExpoSplit = (bool)(packingInfo["ExpoSplit"] ?? "false");
  343. var bExpoPack = (bool)(packingInfo["ExpoPack"] ?? "false");
  344. var bExpoFeed = (bool)(packingInfo["ExpoFeed"] ?? "false");
  345. var tb_row = tb_FeeItems.NewRow();
  346. if (bExpoStack)
  347. {//堆高機
  348. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 1);
  349. tb_FeeItems.Rows.Add(tb_row);
  350. }
  351. if (bExpoSplit)
  352. {//進場服務費
  353. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 2);
  354. tb_FeeItems.Rows.Add(tb_row);
  355. }
  356. if (bExpoPack)
  357. {//退場服務費
  358. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 3);
  359. tb_FeeItems.Rows.Add(tb_row);
  360. }
  361. if (bExpoFeed)
  362. {//空箱收送與儲存(空箱收送)
  363. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 4);
  364. tb_FeeItems.Rows.Add(tb_row);
  365. }
  366. if (bExpoStorage)
  367. {//空箱收送與儲存(儲存)
  368. tb_row = GetFeeItemsInfo(i_crm, tb_FeeItems, packingInfo, oExhibitionRules, 5);
  369. tb_FeeItems.Rows.Add(tb_row);
  370. }
  371. }
  372. catch { }
  373. }
  374. }
  375. }
  376. var mapRel = new Map
  377. {
  378. { "Base", map },
  379. { "Fees", tb_FeeItems }
  380. };
  381. rm = new SuccessResponseMessage(null, i_crm);
  382. rm.DATA.Add(BLWording.REL, mapRel);
  383. } while (false);
  384. }
  385. catch (Exception ex)
  386. {
  387. sMsg = Util.GetLastExceptionMsg(ex);
  388. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetBillAssociated(獲取賬單關聯)", @"", @"", @"");
  389. }
  390. finally
  391. {
  392. if (null != sMsg)
  393. {
  394. rm = new ErrorResponseMessage(sMsg, i_crm);
  395. }
  396. }
  397. return rm;
  398. }
  399. /// <summary>
  400. /// </summary>
  401. /// <param name="i_crm">todo: describe i_crm parameter on GetFeeItemsInfo</param>
  402. /// <param name="tb">todo: describe tb parameter on GetFeeItemsInfo</param>
  403. /// <param name="joInfo">todo: describe joInfo parameter on GetFeeItemsInfo</param>
  404. /// <param name="rule">todo: describe rule parameter on GetFeeItemsInfo</param>
  405. /// <param name="flag">todo: describe flag parameter on GetFeeItemsInfo</param>
  406. /// <returns></returns>
  407. private static DataRow GetFeeItemsInfo(RequestMessage i_crm, DataTable tb, JObject joInfo, OTB_WSM_ExhibitionRules rule, int flag)
  408. {
  409. var tb_row = tb.NewRow();
  410. var dTransferRate = Math.Round(Convert.ToDecimal("100") / Convert.ToDecimal("105"), 5);
  411. var iExpoNumber = Convert.ToInt32(joInfo["ExpoNumber"].ToString());
  412. var dTotalCBM = Convert.ToDecimal(joInfo["TotalCBM"].ToString());
  413. var dExpoWeightTon = Convert.ToDecimal(joInfo["ExpoWeightTon"].ToString());
  414. var iExpoDays = Convert.ToInt32(joInfo["ExpoDays"].ToString());
  415. var iPackingPrice = Math.Round(Convert.ToDecimal(rule.PackingPrice) * dTransferRate, 2);
  416. var iFeedingPrice = Math.Round(Convert.ToDecimal(rule.FeedingPrice) * dTransferRate, 2);
  417. var iStoragePrice = Math.Round(Convert.ToDecimal(rule.StoragePrice) * dTransferRate, 2);
  418. dTotalCBM = dTotalCBM < 1 ? 1 : dTotalCBM;
  419. tb_row[@"guid"] = Guid.NewGuid();
  420. tb_row[@"Memo"] = @"";
  421. tb_row[@"FinancialCurrency"] = @"NTD";
  422. tb_row[@"FinancialNumber"] = iExpoNumber;
  423. switch (flag)
  424. {
  425. case 1://堆高機
  426. {
  427. tb_row[@"FinancialCode"] = @"TEC06";
  428. tb_row[@"FinancialCostStatement"] = @"堆高機";
  429. var jaCostRules = (JArray)JsonConvert.DeserializeObject(rule.CostRules);
  430. var oCurRule = GetCurRule(joInfo, jaCostRules);
  431. var iPrice = Math.Round(int.Parse(oCurRule["Price"].ToString()) * dTransferRate, 2);
  432. tb_row[@"FinancialUnitPrice"] = iPrice;
  433. if (oCurRule["PricingMode"].ToString() == "T")
  434. {
  435. tb_row[@"FinancialNumber"] = dExpoWeightTon * iExpoNumber;
  436. tb_row[@"FinancialUnit"] = @"TON";
  437. tb_row[@"FinancialAmount"] = iPrice * dExpoWeightTon * iExpoNumber;
  438. }
  439. else
  440. {
  441. tb_row[@"FinancialUnit"] = @"SHPT";
  442. tb_row[@"FinancialAmount"] = iPrice * iExpoNumber;
  443. }
  444. }
  445. break;
  446. case 2://進場服務費
  447. {
  448. tb_row[@"FinancialCode"] = @"TEC34";
  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 3://退場服務費
  457. {
  458. tb_row[@"FinancialCode"] = @"TEC35";
  459. tb_row[@"FinancialCostStatement"] = @"退場服務費";
  460. tb_row[@"FinancialUnitPrice"] = iPackingPrice;
  461. tb_row[@"FinancialNumber"] = dTotalCBM * iExpoNumber;
  462. tb_row[@"FinancialUnit"] = @"CBM";
  463. tb_row[@"FinancialAmount"] = iPackingPrice * dTotalCBM * iExpoNumber;
  464. }
  465. break;
  466. case 4://空箱收送與儲存(空箱收送)
  467. {
  468. tb_row[@"FinancialCode"] = @"TEC05";
  469. tb_row[@"FinancialCostStatement"] = @"空箱收送與儲存";
  470. tb_row[@"FinancialUnitPrice"] = iFeedingPrice;
  471. tb_row[@"FinancialNumber"] = dTotalCBM * iExpoNumber;
  472. tb_row[@"FinancialUnit"] = @"CBM";
  473. tb_row[@"FinancialAmount"] = iFeedingPrice * dTotalCBM * iExpoNumber;
  474. }
  475. break;
  476. case 5://空箱收送與儲存(儲存)
  477. {
  478. tb_row[@"FinancialCode"] = @"TEC05";
  479. tb_row[@"FinancialCostStatement"] = @"空箱收送與儲存";
  480. tb_row[@"FinancialUnitPrice"] = iStoragePrice * dTotalCBM * iExpoNumber;
  481. tb_row[@"FinancialNumber"] = iExpoDays;
  482. tb_row[@"FinancialUnit"] = @"DAY";
  483. tb_row[@"FinancialAmount"] = iStoragePrice * dTotalCBM * iExpoNumber * iExpoDays;
  484. }
  485. break;
  486. default:
  487. break;
  488. }
  489. tb_row[@"FinancialTWAmount"] = tb_row[@"FinancialAmount"];
  490. tb_row[@"FinancialTaxRate"] = 0.05;
  491. tb_row[@"FinancialExchangeRate"] = 1;
  492. tb_row[@"FinancialTax"] = Convert.ToDouble(tb_row[@"FinancialTWAmount"].ToString()) * 0.05;
  493. tb_row[@"CreateUser"] = i_crm.USERID ?? @"";
  494. tb_row[@"CreateDate"] = DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss");
  495. return tb_row;
  496. }
  497. private static JObject GetCurRule(JObject joData, JArray rules)
  498. {
  499. var jo = new JObject();
  500. var weight = (Decimal)joData["ExpoWeightTon"];
  501. foreach (JObject rule in rules)
  502. {
  503. var rule_min = PackNum(rule["Weight_Min"]);
  504. var rule_max = PackNum(rule["Weight_Max"]);
  505. if (weight >= rule_min && weight < rule_max || (weight == 30 && rule_max == 30))
  506. {
  507. jo = rule;
  508. break;
  509. }
  510. }
  511. return jo;
  512. }
  513. private static Decimal PackNum(object str)
  514. {
  515. return Convert.ToDecimal((str == null ? "" : str.ToString()) == "" ? "0" : str);
  516. }
  517. #endregion 獲取賬單關聯
  518. #region 複製費用項目
  519. /// <summary>
  520. /// 複製費用項目
  521. /// </summary>
  522. /// <param name="i_crm">todo: describe i_crm parameter on InitAppoint</param>
  523. /// <returns></returns>
  524. public ResponseMessage GetBillInfos(RequestMessage i_crm)
  525. {
  526. ResponseMessage rm = null;
  527. string sMsg = null;
  528. var db = SugarBase.GetIntance();
  529. try
  530. {
  531. do
  532. {
  533. var sExhibitionNO = _fetchString(i_crm, @"ExhibitionNO");
  534. var sBillNO = _fetchString(i_crm, @"BillNO");
  535. var saBillInfo = db.Queryable<OTB_OPM_BillInfo, OTB_SYS_Members, OTB_SYS_Members, OTB_OPM_Exhibition, OTB_CRM_Customers>(
  536. (t1, t2, t3, t4, t5) => new object[]
  537. {
  538. JoinType.Inner, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  539. JoinType.Inner, t1.OrgID == t3.OrgID && t1.ResponsiblePerson == t3.MemberID,
  540. JoinType.Inner, t1.OrgID == t4.OrgID && t1.ExhibitionNO == t4.SN.ToString(),
  541. JoinType.Left, t1.OrgID == t5.OrgID && t1.Payer == t5.guid
  542. }
  543. )
  544. .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID)
  545. .WhereIF(!string.IsNullOrEmpty(sExhibitionNO), (t1, t2, t3, t4, t5) => t1.ExhibitionNO == sExhibitionNO)
  546. .WhereIF(!string.IsNullOrEmpty(sBillNO), (t1, t2, t3, t4, t5) => t1.BillNO == sBillNO)
  547. .Select((t1, t2, t3, t4, t5) => new View_OPM_BillInfo
  548. {
  549. SN = SqlFunc.GetSelfAndAutoFill(t1.SN),
  550. CreateUserName = t2.MemberName,
  551. ResponsiblePersonName = t3.MemberName,
  552. ExhibitioShotName = t4.ExhibitioShotName_CN,
  553. ExhibitioName = t4.Exhibitioname_TW,
  554. PayerName = SqlFunc.IIF(SqlFunc.HasValue(t5.CustomerCName), t5.CustomerCName, t5.CustomerEName)
  555. })
  556. .OrderBy("t1.CreateDate")
  557. .ToList();
  558. rm = new SuccessResponseMessage(null, i_crm);
  559. rm.DATA.Add(BLWording.REL, saBillInfo);
  560. } while (false);
  561. }
  562. catch (Exception ex)
  563. {
  564. sMsg = Util.GetLastExceptionMsg(ex);
  565. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetBillInfos(複製費用項目)", @"", @"", @"");
  566. }
  567. finally
  568. {
  569. if (null != sMsg)
  570. {
  571. rm = new ErrorResponseMessage(sMsg, i_crm);
  572. }
  573. }
  574. return rm;
  575. }
  576. #endregion 複製費用項目
  577. #region 取得帳單存取權限
  578. public ResponseMessage GetExhibitionBillAuthorize(RequestMessage i_crm)
  579. {
  580. ResponseMessage rm = null;
  581. string sMsg = null;
  582. var db = SugarBase.GetIntance();
  583. try
  584. {
  585. do
  586. {
  587. var MemberToAccess = new List<string>();
  588. // Account
  589. //select MemberIDs from OVW_SYS_Rules where RuleID in ('Account','Admin')
  590. var sRuleIDs = _fetchString(i_crm, @"RuleID");
  591. var sResponsiblePerson = _fetchString(i_crm, @"ResponsiblePerson");
  592. var sMemberdb = new SimpleClient<OTB_SYS_Members>(db);
  593. var spOrgID = new SugarParameter("@OrgID", i_crm.ORIGID);
  594. var spResponsiblePerson = new SugarParameter("@MemberID", sResponsiblePerson);
  595. var spRuleID = new SugarParameter("@RuleID", sRuleIDs);
  596. //直屬主管、部門主管
  597. var Supervisors = db.Ado.SqlQuery<string>(@"SELECT M.ImmediateSupervisor + ',' + D.ChiefOfDepartmentID FROM OTB_SYS_Members M
  598. LEFT JOIN OTB_SYS_Departments D ON D.DepartmentID = M.DepartmentID WHERE M.OrgID = @OrgID AND M.MemberID = @MemberID ", spOrgID, spResponsiblePerson);
  599. MemberToAccess.AddRange(Supervisors);
  600. //
  601. var oVW_SYS_Rules = db.Queryable<OVW_SYS_Rules>().Where(r => r.DelStatus == "N" && r.OrgID == i_crm.ORIGID)
  602. .WhereIF(!string.IsNullOrWhiteSpace(sRuleIDs), r => sRuleIDs.Contains(r.RuleID)).Select(c => c.MemberIDs).ToList();
  603. MemberToAccess.AddRange(oVW_SYS_Rules);
  604. var BillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, "BillAuditor");
  605. MemberToAccess.Add(BillAuditor);
  606. //var saBillInfo = db.Queryable<OTB_OPM_BillInfo, OTB_SYS_Members, OTB_SYS_Members, OTB_OPM_Exhibition, OTB_CRM_Customers>(
  607. // (t1, t2, t3, t4, t5) => new object[]
  608. // {
  609. // JoinType.Inner, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  610. // JoinType.Inner, t1.OrgID == t3.OrgID && t1.ResponsiblePerson == t3.MemberID,
  611. // JoinType.Inner, t1.OrgID == t4.OrgID && t1.ExhibitionNO == t4.SN.ToString(),
  612. // JoinType.Left, t1.OrgID == t5.OrgID && t1.Payer == t5.guid
  613. // }
  614. // )
  615. // .Where((t1, t2, t3, t4, t5) => t1.OrgID == i_crm.ORIGID)
  616. // .WhereIF(!string.IsNullOrEmpty(sExhibitionNO), (t1, t2, t3, t4, t5) => t1.ExhibitionNO == sExhibitionNO)
  617. // .WhereIF(!string.IsNullOrEmpty(sBillNO), (t1, t2, t3, t4, t5) => t1.BillNO == sBillNO)
  618. // .Select((t1, t2, t3, t4, t5) => new View_OPM_BillInfo
  619. // {
  620. // SN = SqlFunc.GetSelfAndAutoFill(t1.SN),
  621. // CreateUserName = t2.MemberName,
  622. // ResponsiblePersonName = t3.MemberName,
  623. // ExhibitioShotName = t4.ExhibitioShotName_CN,
  624. // ExhibitioName = t4.Exhibitioname_TW,
  625. // PayerName = SqlFunc.IIF(SqlFunc.HasValue(t5.CustomerCName), t5.CustomerCName, t5.CustomerEName)
  626. // })
  627. // .OrderBy("t1.CreateDate")
  628. // .ToList();
  629. var AccessAccounts = String.Join(",", MemberToAccess);
  630. rm = new SuccessResponseMessage(null, i_crm);
  631. rm.DATA.Add(BLWording.REL, AccessAccounts);
  632. } while (false);
  633. }
  634. catch (Exception ex)
  635. {
  636. sMsg = Util.GetLastExceptionMsg(ex);
  637. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OpmComService), @"進出口管理模組", @"GetExhibitionBillAuthorize(取得帳單存取權限)", @"", @"", @"");
  638. }
  639. finally
  640. {
  641. if (null != sMsg)
  642. {
  643. rm = new ErrorResponseMessage(sMsg, i_crm);
  644. }
  645. }
  646. return rm;
  647. }
  648. #endregion
  649. }
  650. }