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.

3410 lines
182 KiB

2 years ago
  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Wordprocessing;
  3. using EasyBL.WebApi.Message;
  4. using EasyNet;
  5. using Entity.Sugar;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using SqlSugar;
  9. using SqlSugar.Base;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Xml;
  16. namespace EasyBL.WEBAPP.OPM
  17. {
  18. public class ExhibitionExport_UpdService : ServiceBase
  19. {
  20. #region 出口報價和預估成本提交審核
  21. /// <summary>
  22. /// 出口報價和預估成本提交審核
  23. /// </summary>
  24. /// <param name="i_crm">todo: describe i_crm parameter on ToAuditForQuote</param>
  25. /// <returns></returns>
  26. public ResponseMessage ToAuditForQuote(RequestMessage i_crm)
  27. {
  28. ResponseMessage rm = null;
  29. string sMsg = null;
  30. try
  31. {
  32. rm = SugarBase.ExecTran(db =>
  33. {
  34. do
  35. {
  36. var data = i_crm.DATA as Dictionary<string, object>;
  37. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  38. var sSourceID = ((JObject)data[OTB_OPM_ExportExhibition.CN_QUOTE])[EasyNetGlobalConstWord.GUID].ToString();
  39. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  40. var oOpm = sdb.GetById(sExportBillNO);
  41. if (oOpm == null)
  42. {
  43. sMsg = @"系統找不到對應的基本資料,請核查!";
  44. break;
  45. }
  46. var sTitle = @"「出口」" + oOpm.ExportBillName + @"(" + oOpm.RefNumber + @")" + @"「報價單/預估成本審核」";
  47. if (i_crm.LANG == @"zh")
  48. {
  49. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  50. }
  51. //更新基本資料
  52. var oOpmUpd = new OTB_OPM_ExportExhibition
  53. {
  54. Quote = data[OTB_OPM_ExportExhibition.CN_QUOTE].ToString(),
  55. EstimatedCost = data[OTB_OPM_ExportExhibition.CN_ESTIMATEDCOST].ToString(),
  56. ModifyUser = i_crm.USERID,
  57. ModifyDate = DateTime.Now
  58. };
  59. db.Updateable(oOpmUpd)
  60. .UpdateColumns(it => new { it.Quote, it.EstimatedCost, it.ModifyUser, it.ModifyDate })
  61. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  62. //更新代辦
  63. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  64. var saSupervisor = new List<string>();
  65. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  66. if (member.MemberID != null)
  67. {
  68. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  69. {
  70. saSupervisor.Add(member.ImmediateSupervisor);
  71. }
  72. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  73. if (oDept != null)
  74. {
  75. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  76. }
  77. }
  78. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  79. if (sBillAuditor != @"")
  80. {
  81. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  82. foreach (string auditor in saBillAuditor)
  83. {
  84. saSupervisor.Add(auditor);
  85. }
  86. }
  87. saSupervisor = saSupervisor.Distinct<string>().ToList();
  88. var listTips = new List<OTB_SYS_Tips>();
  89. var listTask = new List<OTB_SYS_Task>();
  90. if (saSupervisor.Count > 0)
  91. {
  92. foreach (string supervisor in saSupervisor)
  93. {
  94. //添加提醒消息
  95. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO, WebAppGlobalConstWord.BELL);
  96. listTips.Add(oTipsAdd);
  97. //添加代辦
  98. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO);
  99. listTask.Add(oTaskAdd);
  100. }
  101. }
  102. else
  103. {
  104. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  105. break;
  106. }
  107. if (listTips.Count > 0)
  108. {
  109. db.Insertable(listTips).ExecuteCommand();
  110. }
  111. if (listTask.Count > 0)
  112. {
  113. db.Insertable(listTask).ExecuteCommand();
  114. }
  115. rm = new SuccessResponseMessage(null, i_crm);
  116. rm.DATA.Add(BLWording.REL, saSupervisor);
  117. } while (false);
  118. return rm;
  119. });
  120. }
  121. catch (Exception ex)
  122. {
  123. sMsg = Util.GetLastExceptionMsg(ex);
  124. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ToAuditForQuote(出口報價和預估成本提交審核)", @"", @"", @"");
  125. }
  126. finally
  127. {
  128. if (null != sMsg)
  129. {
  130. rm = new ErrorResponseMessage(sMsg, i_crm);
  131. }
  132. Logger.Debug(@"ExhibitionExport_UpdService.ToAuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  133. }
  134. return rm;
  135. }
  136. #endregion 出口報價和預估成本提交審核
  137. #region 出口報價和預估成本(主管)審核
  138. /// <summary>
  139. /// 出口報價和預估成本(主管)審核
  140. /// </summary>
  141. /// <param name="i_crm">todo: describe i_crm parameter on AuditForQuote</param>
  142. /// <returns></returns>
  143. public ResponseMessage AuditForQuote(RequestMessage i_crm)
  144. {
  145. ResponseMessage rm = null;
  146. string sMsg = null;
  147. try
  148. {
  149. rm = SugarBase.ExecTran(db =>
  150. {
  151. do
  152. {
  153. var data = i_crm.DATA as Dictionary<string, object>;
  154. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  155. var sSourceID = ((JObject)data[OTB_OPM_ExportExhibition.CN_QUOTE])[EasyNetGlobalConstWord.GUID].ToString();
  156. var TipsType = WebAppGlobalConstWord.CHECK;
  157. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  158. var oOpm = sdb.GetById(sExportBillNO);
  159. if (oOpm == null)
  160. {
  161. sMsg = @"系統找不到對應的基本資料,請核查!";
  162. break;
  163. }
  164. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  165. var sTitle = member.MemberName + @"審核了您的「出口」活動名稱:" + oOpm.ExportBillName + @")的「報價單/預估成本審核」";
  166. //更新報價和預估成本
  167. var oOpmUpd = new OTB_OPM_ExportExhibition
  168. {
  169. Quote = data[OTB_OPM_ExportExhibition.CN_QUOTE].ToString(),
  170. EstimatedCost = data[OTB_OPM_ExportExhibition.CN_ESTIMATEDCOST].ToString(),
  171. Bills = data[OTB_OPM_ExportExhibition.CN_BILLS].ToString(),
  172. ModifyUser = i_crm.USERID,
  173. ModifyDate = DateTime.Now
  174. };
  175. var sAuditVal = ((JObject)data[OTB_OPM_ExportExhibition.CN_QUOTE])[@"AuditVal"].ToString();
  176. if (sAuditVal == @"2")
  177. {
  178. sTitle += @",審核結果:通過";
  179. }
  180. else
  181. {
  182. sTitle += @",審核結果:不通過";
  183. TipsType = WebAppGlobalConstWord.FAIL;
  184. }
  185. if (i_crm.LANG == @"zh")
  186. {
  187. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  188. }
  189. db.Updateable(oOpmUpd)
  190. .UpdateColumns(it => new { it.Quote, it.EstimatedCost, it.Bills, it.ModifyUser, it.ModifyDate })
  191. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  192. //更新代辦
  193. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  194. //添加提醒消息
  195. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO, TipsType);
  196. db.Insertable(oTipsAdd).ExecuteCommand();
  197. if (sAuditVal != @"2")
  198. {
  199. //添加代辦
  200. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO);
  201. db.Insertable(oTaskAdd).ExecuteCommand();
  202. }
  203. rm = new SuccessResponseMessage(null, i_crm);
  204. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  205. } while (false);
  206. return rm;
  207. });
  208. }
  209. catch (Exception ex)
  210. {
  211. sMsg = Util.GetLastExceptionMsg(ex);
  212. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"AuditForQuote(出口報價和預估成本(主管)審核)", @"", @"", @"");
  213. }
  214. finally
  215. {
  216. if (null != sMsg)
  217. {
  218. rm = new ErrorResponseMessage(sMsg, i_crm);
  219. }
  220. Logger.Debug(@"ExhibitionExport_UpdService.AuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  221. }
  222. return rm;
  223. }
  224. #endregion 出口報價和預估成本(主管)審核
  225. #region 出口帳單提交審核
  226. /// <summary>
  227. /// 出口帳單提交審核
  228. /// </summary>
  229. /// <param name="i_crm">todo: describe i_crm parameter on ToAuditForBill</param>
  230. /// <returns></returns>
  231. public ResponseMessage ToAuditForBill(RequestMessage i_crm)
  232. {
  233. ResponseMessage rm = null;
  234. string sMsg = null;
  235. try
  236. {
  237. rm = SugarBase.ExecTran(db =>
  238. {
  239. do
  240. {
  241. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  242. var sExhibitors = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXHIBITORS);
  243. var sBills = _fetchString(i_crm, @"Bills");
  244. var sBill = _fetchString(i_crm, @"Bill");
  245. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  246. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  247. var sBillNO = oBill[@"BillNO"].ToString();
  248. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  249. var oOpm = sdb.GetById(sExportBillNO);
  250. if (oOpm == null)
  251. {
  252. sMsg = @"系統找不到對應的基本資料,請核查!";
  253. break;
  254. }
  255. var sTitle = @"「出口」" + oOpm.ExportBillName + @"帳單(" + sBillNO + @")" + @"審核";
  256. if (i_crm.LANG == @"zh")
  257. {
  258. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  259. }
  260. //更新帳單
  261. var oOpmUpd = new OTB_OPM_ExportExhibition
  262. {
  263. Bills = sBills,
  264. Exhibitors = sExhibitors,
  265. ModifyUser = i_crm.USERID,
  266. ModifyDate = DateTime.Now
  267. };
  268. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.Exhibitors, it.ModifyUser, it.ModifyDate })
  269. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  270. //更新代辦
  271. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  272. var saSupervisor = new List<string>();
  273. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  274. if (member.MemberID != null)
  275. {
  276. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  277. {
  278. saSupervisor.Add(member.ImmediateSupervisor);
  279. }
  280. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  281. if (oDept != null)
  282. {
  283. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  284. }
  285. }
  286. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  287. if (sBillAuditor != @"")
  288. {
  289. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  290. foreach (string auditor in saBillAuditor)
  291. {
  292. saSupervisor.Add(auditor);
  293. }
  294. }
  295. saSupervisor = saSupervisor.Distinct<string>().ToList();
  296. var listTips = new List<OTB_SYS_Tips>();
  297. var listTask = new List<OTB_SYS_Task>();
  298. if (saSupervisor.Count > 0)
  299. {
  300. foreach (string supervisor in saSupervisor)
  301. {
  302. //添加提醒消息
  303. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  304. listTips.Add(oTipsAdd);
  305. //添加代辦
  306. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO);
  307. listTask.Add(oTaskAdd);
  308. }
  309. }
  310. else
  311. {
  312. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  313. break;
  314. }
  315. if (listTips.Count > 0)
  316. {
  317. db.Insertable(listTips).ExecuteCommand();
  318. }
  319. if (listTask.Count > 0)
  320. {
  321. db.Insertable(listTask).ExecuteCommand();
  322. }
  323. rm = new SuccessResponseMessage(null, i_crm);
  324. rm.DATA.Add(BLWording.REL, saSupervisor);
  325. } while (false);
  326. return rm;
  327. });
  328. }
  329. catch (Exception ex)
  330. {
  331. sMsg = Util.GetLastExceptionMsg(ex);
  332. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ToAuditForBill(出口帳單提交審核)", @"", @"", @"");
  333. }
  334. finally
  335. {
  336. if (null != sMsg)
  337. {
  338. rm = new ErrorResponseMessage(sMsg, i_crm);
  339. }
  340. Logger.Debug(@"ExhibitionExport_UpdService.ToAuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  341. }
  342. return rm;
  343. }
  344. #endregion 出口帳單提交審核
  345. #region 出口帳單(主管)審核
  346. /// <summary>
  347. /// 出口帳單(主管)審核
  348. /// </summary>
  349. /// <param name="i_crm">todo: describe i_crm parameter on AuditForBill</param>
  350. /// <returns></returns>
  351. public ResponseMessage AuditForBill(RequestMessage i_crm)
  352. {
  353. ResponseMessage rm = null;
  354. string sMsg = null;
  355. try
  356. {
  357. rm = SugarBase.ExecTran(db =>
  358. {
  359. do
  360. {
  361. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  362. var sBills = _fetchString(i_crm, @"Bills");
  363. var sBill = _fetchString(i_crm, @"Bill");
  364. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  365. var sBillNO = oBill[@"BillNO"].ToString();
  366. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  367. var TipsType = WebAppGlobalConstWord.CHECK;
  368. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  369. var oOpm = sdb.GetById(sExportBillNO);
  370. if (oOpm == null)
  371. {
  372. sMsg = @"系統找不到對應的基本資料,請核查!";
  373. break;
  374. }
  375. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  376. var sTitle = member.MemberName + @"審核了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的「帳單(" + sBillNO + @")」";
  377. //更新報價和預估成本
  378. var sAuditVal = oBill[@"AuditVal"].ToString();
  379. if (sAuditVal == @"2")
  380. {
  381. sTitle += @",審核結果:通過";
  382. }
  383. else
  384. {
  385. sTitle += @",審核結果:不通過";
  386. TipsType = WebAppGlobalConstWord.FAIL;
  387. }
  388. if (i_crm.LANG == @"zh")
  389. {
  390. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  391. }
  392. var oPayer = new OTB_CRM_Customers();
  393. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  394. {
  395. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  396. if (oPayer == null)
  397. {
  398. sMsg = @"系統找不到付款人資訊";
  399. break;
  400. }
  401. }
  402. //更新帳單
  403. var oOpmUpd = new OTB_OPM_ExportExhibition
  404. {
  405. Bills = sBills,
  406. ModifyUser = i_crm.USERID,
  407. ModifyDate = DateTime.Now
  408. };
  409. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  410. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  411. //更新代辦
  412. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  413. //添加提醒消息
  414. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, TipsType);
  415. db.Insertable(oTipsAdd).ExecuteCommand();
  416. if (sAuditVal != @"2")
  417. {
  418. //添加代辦
  419. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO);
  420. db.Insertable(oTaskAdd).ExecuteCommand();
  421. }
  422. if (sAuditVal == @"2")
  423. {
  424. var oBillsAdd = new OTB_OPM_Bills
  425. {
  426. OrgID = i_crm.ORIGID,
  427. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  428. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  429. BillType = @"20",//帳別(收付)(3)
  430. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  431. };
  432. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  433. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  434. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  435. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  436. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  437. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  438. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  439. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  440. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  441. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  442. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  443. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  444. oBillsAdd.Settle = @"N"; //結清否(15)
  445. oBillsAdd.InvoiceStartNumber = @""; //發票號碼(起)(16)
  446. oBillsAdd.InvoiceEndNumber = @"";//發票號碼(迄)(17)
  447. oBillsAdd.Category = @"";//傳票類別(18)
  448. oBillsAdd.OrderNo = @"";//訂單單號(19)
  449. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  450. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  451. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  452. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  453. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  454. oBillsAdd.UpdateDate = @""; //更新日期(25)
  455. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  456. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  457. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  458. {
  459. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  460. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  461. }
  462. else
  463. {
  464. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  465. }
  466. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  467. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  468. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  469. var sCurrency = oBill[@"Currency"].ToString();
  470. if (i_crm.ORIGID == "SG")
  471. {
  472. if (sCurrency == "RMB")
  473. {
  474. sCurrency = "";
  475. }
  476. }
  477. else
  478. {
  479. if (sCurrency == "NTD")
  480. {
  481. sCurrency = "";
  482. }
  483. }
  484. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  485. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  486. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  487. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  488. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  489. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  490. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  491. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  492. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  493. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  494. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  495. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  496. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  497. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  498. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  499. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  500. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  501. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  502. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  503. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  504. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  505. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  506. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  507. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  508. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  509. oBillsAdd.CreateUser = i_crm.USERID;
  510. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  511. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  512. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  513. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  514. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  515. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  516. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO; //Url
  517. db.Insertable(oBillsAdd).ExecuteCommand();
  518. }
  519. var oLogInfo = new OTB_SYS_LogInfo
  520. {
  521. OrgID = i_crm.ORIGID,
  522. SouseId = sBillNO,
  523. LogType = "billnoupdate",
  524. LogInfo = sBill,
  525. CreateUser = i_crm.USERID,
  526. CreateDate = DateTime.Now,
  527. Memo = "「出口帳單」(主管)審核"
  528. };
  529. db.Insertable(oLogInfo).ExecuteCommand();
  530. rm = new SuccessResponseMessage(null, i_crm);
  531. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  532. } while (false);
  533. return rm;
  534. });
  535. }
  536. catch (Exception ex)
  537. {
  538. sMsg = Util.GetLastExceptionMsg(ex);
  539. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"AuditForBill(出口帳單(主管)審核)", @"", @"", @"");
  540. }
  541. finally
  542. {
  543. if (null != sMsg)
  544. {
  545. rm = new ErrorResponseMessage(sMsg, i_crm);
  546. }
  547. Logger.Debug(@"ExhibitionExport_UpdService.AuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  548. }
  549. return rm;
  550. }
  551. #endregion 出口帳單(主管)審核
  552. #region (會計)取消審核
  553. /// <summary>
  554. /// (會計)取消審核
  555. /// </summary>
  556. /// <param name="i_crm">todo: describe i_crm parameter on CancelAudit</param>
  557. /// <returns></returns>
  558. public ResponseMessage CancelAudit(RequestMessage i_crm)
  559. {
  560. ResponseMessage rm = null;
  561. string sMsg = null;
  562. try
  563. {
  564. rm = SugarBase.ExecTran(db =>
  565. {
  566. do
  567. {
  568. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  569. var sBills = _fetchString(i_crm, @"Bills");
  570. var sBill = _fetchString(i_crm, @"Bill");
  571. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  572. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  573. var sBillNO = oBill[@"BillNO"].ToString();
  574. var LogData = _fetchString(i_crm, @"LogData");
  575. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  576. if (!LogResult.Item1)
  577. {
  578. sMsg = LogResult.Item2;
  579. break;
  580. }
  581. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  582. var oOpm = sdb.GetById(sExportBillNO);
  583. if (oOpm == null)
  584. {
  585. sMsg = @"系統找不到對應的基本資料,請核查!";
  586. break;
  587. }
  588. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  589. var sTitle = @"會計(" + member.MemberName + @")取消審核了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")";
  590. if (i_crm.LANG == @"zh")
  591. {
  592. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  593. }
  594. //更新帳單
  595. var oOpmUpd = new OTB_OPM_ExportExhibition
  596. {
  597. Bills = sBills,
  598. ModifyUser = i_crm.USERID,
  599. ModifyDate = DateTime.Now
  600. };
  601. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  602. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  603. //更新代辦
  604. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  605. //添加提醒消息
  606. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  607. db.Insertable(oTipsAdd).ExecuteCommand();
  608. //添加代辦
  609. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO);
  610. db.Insertable(oTaskAdd).ExecuteCommand();
  611. rm = new SuccessResponseMessage(null, i_crm);
  612. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  613. } while (false);
  614. return rm;
  615. });
  616. }
  617. catch (Exception ex)
  618. {
  619. sMsg = Util.GetLastExceptionMsg(ex);
  620. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"CancelAudit((會計)取消審核)", @"", @"", @"");
  621. }
  622. finally
  623. {
  624. if (null != sMsg)
  625. {
  626. rm = new ErrorResponseMessage(sMsg, i_crm);
  627. }
  628. Logger.Debug(@"ExhibitionExport_UpdService.CancelAudit Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  629. }
  630. return rm;
  631. }
  632. #endregion (會計)取消審核
  633. #region (會計)銷帳
  634. /// <summary>
  635. /// (會計)銷帳
  636. /// </summary>
  637. /// <param name="i_crm">todo: describe i_crm parameter on WriteOff</param>
  638. /// <returns></returns>
  639. public ResponseMessage WriteOff(RequestMessage i_crm)
  640. {
  641. ResponseMessage rm = null;
  642. string sMsg = null;
  643. try
  644. {
  645. rm = SugarBase.ExecTran(db =>
  646. {
  647. do
  648. {
  649. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  650. var sBills = _fetchString(i_crm, @"Bills");
  651. var sBill = _fetchString(i_crm, @"Bill");
  652. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  653. var sBillNO = oBill[@"BillNO"].ToString();
  654. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  655. var oOpm = sdb.GetById(sExportBillNO);
  656. if (oOpm == null)
  657. {
  658. sMsg = @"系統找不到對應的基本資料,請核查!";
  659. break;
  660. }
  661. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  662. var sTitle = @"會計(" + member.MemberName + @")對您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")進行了銷帳";
  663. if (i_crm.LANG == @"zh")
  664. {
  665. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  666. }
  667. //更新帳單
  668. var oOpmUpd = new OTB_OPM_ExportExhibition
  669. {
  670. Bills = sBills,
  671. ModifyUser = i_crm.USERID,
  672. ModifyDate = DateTime.Now
  673. };
  674. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  675. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  676. //添加提醒消息
  677. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  678. db.Insertable(oTipsAdd).ExecuteCommand();
  679. rm = new SuccessResponseMessage(null, i_crm);
  680. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  681. } while (false);
  682. return rm;
  683. });
  684. }
  685. catch (Exception ex)
  686. {
  687. sMsg = Util.GetLastExceptionMsg(ex);
  688. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"WriteOff((會計)銷帳)", @"", @"", @"");
  689. }
  690. finally
  691. {
  692. if (null != sMsg)
  693. {
  694. rm = new ErrorResponseMessage(sMsg, i_crm);
  695. }
  696. Logger.Debug(@"ExhibitionExport_UpdService.WriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  697. }
  698. return rm;
  699. }
  700. #endregion (會計)銷帳
  701. #region (會計)取消銷帳
  702. /// <summary>
  703. /// (會計)取消銷帳
  704. /// </summary>
  705. /// <param name="i_crm">todo: describe i_crm parameter on CancelWriteOff</param>
  706. /// <returns></returns>
  707. public ResponseMessage CancelWriteOff(RequestMessage i_crm)
  708. {
  709. ResponseMessage rm = null;
  710. string sMsg = null;
  711. var db = SugarBase.GetIntance();
  712. try
  713. {
  714. do
  715. {
  716. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  717. var sBills = _fetchString(i_crm, @"Bills");
  718. var sBill = _fetchString(i_crm, @"Bill");
  719. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  720. var sBillNO = oBill[@"BillNO"].ToString();
  721. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  722. var oOpm = sdb.GetById(sExportBillNO);
  723. if (oOpm == null)
  724. {
  725. sMsg = @"系統找不到對應的基本資料,請核查!";
  726. break;
  727. }
  728. var LogData = _fetchString(i_crm, @"LogData");
  729. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  730. if (!LogResult.Item1)
  731. {
  732. sMsg = LogResult.Item2;
  733. break;
  734. }
  735. //更新帳單
  736. var oOpmUpd = new OTB_OPM_ExportExhibition
  737. {
  738. Bills = sBills,
  739. ModifyUser = i_crm.USERID,
  740. ModifyDate = DateTime.Now
  741. };
  742. var iResult = db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  743. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  744. if (iResult > 0)
  745. {
  746. rm = new SuccessResponseMessage(null, i_crm);
  747. }
  748. } while (false);
  749. }
  750. catch (Exception ex)
  751. {
  752. sMsg = Util.GetLastExceptionMsg(ex);
  753. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"CancelWriteOff((會計)取消銷帳)", @"", @"", @"");
  754. }
  755. finally
  756. {
  757. if (null != sMsg)
  758. {
  759. rm = new ErrorResponseMessage(sMsg, i_crm);
  760. }
  761. Logger.Debug(@"ExhibitionExport_UpdService.CancelWriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  762. }
  763. return rm;
  764. }
  765. #endregion (會計)取消銷帳
  766. #region 過帳
  767. /// <summary>
  768. /// 過帳
  769. /// </summary>
  770. /// <param name="i_crm">todo: describe i_crm parameter on BillPost</param>
  771. /// <returns></returns>
  772. public ResponseMessage BillPost(RequestMessage i_crm)
  773. {
  774. ResponseMessage rm = null;
  775. string sMsg = null;
  776. try
  777. {
  778. rm = SugarBase.ExecTran(db =>
  779. {
  780. do
  781. {
  782. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  783. var sBills = _fetchString(i_crm, @"Bills");
  784. var sBill = _fetchString(i_crm, @"Bill");
  785. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  786. var sBillNO = oBill[@"BillNO"].ToString();
  787. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  788. var oOpm = sdb.GetById(sExportBillNO);
  789. if (oOpm == null)
  790. {
  791. sMsg = @"系統找不到對應的基本資料,請核查!";
  792. break;
  793. }
  794. var oPayer = new OTB_CRM_Customers();
  795. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  796. {
  797. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  798. if (oPayer == null)
  799. {
  800. sMsg = @"系統找不到付款人資訊";
  801. break;
  802. }
  803. }
  804. var sTitle = @"「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")已過帳";
  805. if (i_crm.LANG == @"zh")
  806. {
  807. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  808. }
  809. //更新帳單
  810. var oOpmUpd = new OTB_OPM_ExportExhibition
  811. {
  812. Bills = sBills,
  813. ModifyUser = i_crm.USERID,
  814. ModifyDate = DateTime.Now
  815. };
  816. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  817. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  818. var listAccounts = db.Queryable<OTB_SYS_MembersToRule>()
  819. .Where(it => it.OrgID == i_crm.ORIGID && it.RuleID == @"Account").ToList();
  820. var listTips = new List<OTB_SYS_Tips>();
  821. if (listAccounts.Count > 0)
  822. {
  823. foreach (OTB_SYS_MembersToRule account in listAccounts)
  824. {
  825. //添加提醒消息
  826. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, account.MemberID, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  827. listTips.Add(oTipsAdd);
  828. }
  829. }
  830. var oBillsAdd = new OTB_OPM_Bills
  831. {
  832. OrgID = i_crm.ORIGID,
  833. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  834. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  835. BillType = @"20",//帳別(收付)(3)
  836. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  837. };
  838. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  839. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  840. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  841. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  842. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  843. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  844. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  845. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  846. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  847. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  848. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  849. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  850. oBillsAdd.Settle = @"N"; //結清否(15)
  851. oBillsAdd.InvoiceStartNumber = oBill[@"InvoiceNumber"].ToString(); //發票號碼(起)(16)
  852. oBillsAdd.InvoiceEndNumber = oBill[@"InvoiceNumber"].ToString();//發票號碼(迄)(17)
  853. oBillsAdd.Category = @"";//傳票類別(18)
  854. oBillsAdd.OrderNo = @"";//訂單單號(19)
  855. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  856. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  857. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  858. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  859. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  860. oBillsAdd.UpdateDate = @""; //更新日期(25)
  861. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  862. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  863. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  864. {
  865. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  866. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  867. }
  868. else
  869. {
  870. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  871. }
  872. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  873. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  874. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  875. var sCurrency = oBill[@"Currency"].ToString();
  876. if (i_crm.ORIGID == "SG")
  877. {
  878. if (sCurrency == "RMB")
  879. {
  880. sCurrency = "";
  881. }
  882. }
  883. else
  884. {
  885. if (sCurrency == "NTD")
  886. {
  887. sCurrency = "";
  888. }
  889. }
  890. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  891. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  892. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  893. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  894. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  895. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  896. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  897. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  898. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  899. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  900. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  901. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  902. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  903. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  904. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  905. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  906. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  907. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  908. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  909. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  910. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  911. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  912. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  913. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  914. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  915. oBillsAdd.CreateUser = i_crm.USERID;
  916. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  917. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  918. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  919. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  920. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  921. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  922. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO; //Url
  923. db.Insertable(oBillsAdd).ExecuteCommand();
  924. var oLogInfo = new OTB_SYS_LogInfo
  925. {
  926. OrgID = i_crm.ORIGID,
  927. SouseId = sBillNO,
  928. LogType = "billnoupdate",
  929. LogInfo = sBill,
  930. CreateUser = i_crm.USERID,
  931. CreateDate = DateTime.Now,
  932. Memo = "「出口帳單」(會計)過帳"
  933. };
  934. db.Insertable(oLogInfo).ExecuteCommand();
  935. rm = new SuccessResponseMessage(null, i_crm);
  936. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  937. } while (false);
  938. return rm;
  939. });
  940. }
  941. catch (Exception ex)
  942. {
  943. sMsg = Util.GetLastExceptionMsg(ex);
  944. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"BillPost(過帳)", @"", @"", @"");
  945. }
  946. finally
  947. {
  948. if (null != sMsg)
  949. {
  950. rm = new ErrorResponseMessage(sMsg, i_crm);
  951. }
  952. Logger.Debug(@"ExhibitionExport_UpdService.BillPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  953. }
  954. return rm;
  955. }
  956. #endregion 過帳
  957. #region 取消過帳
  958. /// <summary>
  959. /// 取消過帳
  960. /// </summary>
  961. /// <param name="i_crm">todo: describe i_crm parameter on BillCancelPost</param>
  962. /// <returns></returns>
  963. public ResponseMessage BillCancelPost(RequestMessage i_crm)
  964. {
  965. ResponseMessage rm = null;
  966. string sMsg = null;
  967. try
  968. {
  969. rm = SugarBase.ExecTran(db =>
  970. {
  971. do
  972. {
  973. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  974. var sBills = _fetchString(i_crm, @"Bills");
  975. var sBill = _fetchString(i_crm, @"Bill");
  976. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  977. var sBillNO = oBill[@"BillNO"].ToString();
  978. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  979. var oOpm = sdb.GetById(sExportBillNO);
  980. if (oOpm == null)
  981. {
  982. sMsg = @"系統找不到對應的基本資料,請核查!";
  983. break;
  984. }
  985. var LogData = _fetchString(i_crm, @"LogData");
  986. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  987. if (!LogResult.Item1)
  988. {
  989. sMsg = LogResult.Item2;
  990. break;
  991. }
  992. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  993. var sTitle = @"會計(" + member.MemberName + @")對您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")取消了過帳";
  994. if (i_crm.LANG == @"zh")
  995. {
  996. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  997. }
  998. //更新帳單
  999. var oOpmUpd = new OTB_OPM_ExportExhibition
  1000. {
  1001. Bills = sBills,
  1002. ModifyUser = i_crm.USERID,
  1003. ModifyDate = DateTime.Now
  1004. };
  1005. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  1006. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  1007. //添加提醒消息
  1008. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1009. db.Insertable(oTipsAdd).ExecuteCommand();
  1010. rm = new SuccessResponseMessage(null, i_crm);
  1011. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1012. } while (false);
  1013. return rm;
  1014. });
  1015. }
  1016. catch (Exception ex)
  1017. {
  1018. sMsg = Util.GetLastExceptionMsg(ex);
  1019. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"BillCancelPost(取消過帳)", @"", @"", @"");
  1020. }
  1021. finally
  1022. {
  1023. if (null != sMsg)
  1024. {
  1025. rm = new ErrorResponseMessage(sMsg, i_crm);
  1026. }
  1027. Logger.Debug(@"ExhibitionExport_UpdService.BillCancelPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1028. }
  1029. return rm;
  1030. }
  1031. #endregion 取消過帳
  1032. #region 作廢帳單
  1033. /// <summary>
  1034. /// 作廢帳單
  1035. /// </summary>
  1036. /// <param name="i_crm">todo: describe i_crm parameter on BillVoid</param>
  1037. /// <returns></returns>
  1038. public ResponseMessage BillVoid(RequestMessage i_crm)
  1039. {
  1040. ResponseMessage rm = null;
  1041. string sMsg = null;
  1042. try
  1043. {
  1044. rm = SugarBase.ExecTran(db =>
  1045. {
  1046. do
  1047. {
  1048. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1049. var sBills = _fetchString(i_crm, @"Bills");
  1050. var sBill = _fetchString(i_crm, @"Bill");
  1051. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1052. var sBillNO = oBill[@"BillNO"].ToString();
  1053. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  1054. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1055. var oOpm = sdb.GetById(sExportBillNO);
  1056. if (oOpm == null)
  1057. {
  1058. sMsg = @"系統找不到對應的基本資料,請核查!";
  1059. break;
  1060. }
  1061. var LogData = _fetchString(i_crm, @"LogData");
  1062. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  1063. if (!LogResult.Item1)
  1064. {
  1065. sMsg = LogResult.Item2;
  1066. break;
  1067. }
  1068. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1069. var sTitle = @"會計(" + member.MemberName + @")作廢了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")";
  1070. if (i_crm.LANG == @"zh")
  1071. {
  1072. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  1073. }
  1074. //更新帳單
  1075. var oOpmUpd = new OTB_OPM_ExportExhibition
  1076. {
  1077. Bills = sBills,
  1078. ModifyUser = i_crm.USERID,
  1079. ModifyDate = DateTime.Now
  1080. };
  1081. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  1082. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  1083. //添加提醒消息
  1084. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=2&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1085. db.Insertable(oTipsAdd).ExecuteCommand();
  1086. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  1087. rm = new SuccessResponseMessage(null, i_crm);
  1088. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1089. } while (false);
  1090. return rm;
  1091. });
  1092. }
  1093. catch (Exception ex)
  1094. {
  1095. sMsg = Util.GetLastExceptionMsg(ex);
  1096. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"BillVoid(作廢帳單)", @"", @"", @"");
  1097. }
  1098. finally
  1099. {
  1100. if (null != sMsg)
  1101. {
  1102. rm = new ErrorResponseMessage(sMsg, i_crm);
  1103. }
  1104. Logger.Debug(@"ExhibitionExport_UpdService.BillVoid Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1105. }
  1106. return rm;
  1107. }
  1108. #endregion 作廢帳單
  1109. #region 刪除帳單
  1110. /// <summary>
  1111. /// 刪除帳單
  1112. /// </summary>
  1113. /// <param name="i_crm">todo: describe i_crm parameter on BillDelete</param>
  1114. /// <returns></returns>
  1115. public ResponseMessage BillDelete(RequestMessage i_crm)
  1116. {
  1117. ResponseMessage rm = null;
  1118. string sMsg = null;
  1119. try
  1120. {
  1121. rm = SugarBase.ExecTran(db =>
  1122. {
  1123. do
  1124. {
  1125. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1126. var sBills = _fetchString(i_crm, @"Bills");
  1127. var sBill = _fetchString(i_crm, @"Bill");
  1128. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1129. var sBillNO = oBill[@"BillNO"].ToString();
  1130. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  1131. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1132. var oOpm = sdb.GetById(sExportBillNO);
  1133. if (oOpm == null)
  1134. {
  1135. sMsg = @"系統找不到對應的基本資料,請核查!";
  1136. break;
  1137. }
  1138. var LogData = _fetchString(i_crm, @"LogData");
  1139. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  1140. if (!LogResult.Item1)
  1141. {
  1142. sMsg = LogResult.Item2;
  1143. break;
  1144. }
  1145. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1146. var sTitle = @"管理員(" + member.MemberName + @")刪除了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")";
  1147. if (i_crm.LANG == @"zh")
  1148. {
  1149. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  1150. }
  1151. //更新帳單
  1152. var oOpmUpd = new OTB_OPM_ExportExhibition
  1153. {
  1154. Bills = sBills,
  1155. ModifyUser = i_crm.USERID,
  1156. ModifyDate = DateTime.Now
  1157. };
  1158. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  1159. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  1160. //添加提醒消息
  1161. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=2&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1162. db.Insertable(oTipsAdd).ExecuteCommand();
  1163. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  1164. rm = new SuccessResponseMessage(null, i_crm);
  1165. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1166. } while (false);
  1167. return rm;
  1168. });
  1169. }
  1170. catch (Exception ex)
  1171. {
  1172. sMsg = Util.GetLastExceptionMsg(ex);
  1173. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"BillDelete(刪除帳單)", @"", @"", @"");
  1174. }
  1175. finally
  1176. {
  1177. if (null != sMsg)
  1178. {
  1179. rm = new ErrorResponseMessage(sMsg, i_crm);
  1180. }
  1181. Logger.Debug(@"ExhibitionExport_UpdService.BillDelete Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1182. }
  1183. return rm;
  1184. }
  1185. #endregion 刪除帳單
  1186. #region 出口帳單列印下載
  1187. /// <summary>
  1188. /// 出口帳單列印下載
  1189. /// </summary>
  1190. /// <param name="i_crm">要審核的資料</param>
  1191. /// <returns></returns>
  1192. public ResponseMessage PrintBill(RequestMessage i_crm)
  1193. {
  1194. ResponseMessage rm = null;
  1195. string sMsg = null;
  1196. var db = SugarBase.GetIntance();
  1197. var sOutPut = Common.ConfigGetValue(@"", @"OutFilesPath");
  1198. var saOutPut = new List<string>();
  1199. try
  1200. {
  1201. do
  1202. {
  1203. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1204. var sTemplID = _fetchString(i_crm, OTB_SYS_OfficeTemplate.CN_TEMPLID);
  1205. var sBill = _fetchString(i_crm, @"Bill");
  1206. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1207. var sAction = _fetchString(i_crm, @"Action");
  1208. var sPayDateText = _fetchString(i_crm, @"PayDateText");
  1209. var bTW = sTemplID.EndsWith(@"TW");
  1210. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1211. var oOpm = sdb.GetById(sExportBillNO);
  1212. if (oOpm == null)
  1213. {
  1214. sMsg = @"系統找不到對應的基本資料,請核查!";
  1215. break;
  1216. }
  1217. var oTempl = db.Queryable<OTB_SYS_OfficeTemplate>().Single(it => it.OrgID == i_crm.ORIGID && it.TemplID == sTemplID);
  1218. if (oTempl == null)
  1219. {
  1220. sMsg = @"請檢查模版設定";
  1221. break;
  1222. }
  1223. var oFile = db.Queryable<OTB_SYS_Files>()
  1224. .Single(it => it.OrgID == i_crm.ORIGID && it.ParentID == oTempl.FileID);
  1225. if (oFile == null)
  1226. {
  1227. sMsg = @"系統找不到對應的帳單模版";
  1228. break;
  1229. }
  1230. var sIsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString();
  1231. var WatermarkInfo = Common.GetWatermarkInfo(oBill[@"AuditVal"]?.ToString(), sTemplID, i_crm.ORIGID);
  1232. var jaBills = new JArray();
  1233. if (sAction.EndsWith(@"_BatchBill"))
  1234. {
  1235. if (sIsRetn == @"N")
  1236. {
  1237. jaBills = (JArray)JsonConvert.DeserializeObject(oOpm.Bills);
  1238. }
  1239. else
  1240. {
  1241. var jaReturn = (JArray)JsonConvert.DeserializeObject(oOpm.ReturnBills);
  1242. foreach (JObject retn in jaReturn)
  1243. {
  1244. if (retn[@"Bills"] != null)
  1245. {
  1246. var jaReturnBills = (JArray)JsonConvert.DeserializeObject(retn[@"Bills"].ToString());
  1247. foreach (JObject rebill in jaReturnBills)
  1248. {
  1249. jaBills.Add(rebill);
  1250. }
  1251. }
  1252. }
  1253. }
  1254. }
  1255. else
  1256. {
  1257. jaBills.Add(oBill);
  1258. }
  1259. foreach (JObject joBill in jaBills)
  1260. {
  1261. string sCurrency = joBill["Currency"] != null ? joBill["Currency"].ToString() : string.Empty;
  1262. string sPayer = joBill["Payer"] != null ? joBill["Payer"].ToString() : string.Empty;
  1263. string sBillNO = joBill["BillNO"] != null ? joBill["BillNO"].ToString() : string.Empty;
  1264. var sAuditVal = joBill["AuditVal"] != null ? joBill["AuditVal"].ToString() : string.Empty;
  1265. if (@"0,1,2,4,5".IndexOf(sAuditVal) == -1)
  1266. {
  1267. continue;
  1268. }
  1269. var bTWCurrency = sCurrency == @"NTD";
  1270. var oPayer = new OTB_CRM_Customers();
  1271. if (!string.IsNullOrEmpty(sPayer))
  1272. {
  1273. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == sPayer);
  1274. if (oPayer == null)
  1275. {
  1276. sMsg = @"系統找不到付款人資訊";
  1277. break;
  1278. }
  1279. }
  1280. var sTempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, oFile.FilePath);//Word模版路徑
  1281. var sBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"");
  1282. var sCurPath = @"";
  1283. sCurPath = sBase + sOutPut;
  1284. Common.FnCreateDir(sCurPath);//如果不存在就創建文件夾
  1285. var sDocxName = @"Invoice-" + sBillNO;
  1286. //建立臨時文件
  1287. var sTempFile = Path.GetTempPath() + sDocxName + @".docx";
  1288. sCurPath += sDocxName + @".docx";
  1289. if (File.Exists(sTempFile))
  1290. {
  1291. File.Delete(sTempFile);
  1292. }
  1293. File.Copy(sTempPath, sTempFile);
  1294. var JTmpKeys = (JArray)JsonConvert.DeserializeObject(oTempl.TemplKeys);
  1295. double iPARM17 = 0;
  1296. using (WordprocessingDocument doc = WordprocessingDocument.Open(sTempFile, true))//顯示Word
  1297. {
  1298. var body = doc.MainDocumentPart.Document.Body;
  1299. var jaFeeItems = (JArray)JsonConvert.DeserializeObject(joBill[@"FeeItems"].ToString());
  1300. //查找:獲取第三個表格
  1301. var table = body.Elements<Table>().ElementAt(2);
  1302. if (jaFeeItems.Count > 0)
  1303. {
  1304. var trow_Amount = table.Elements<TableRow>().ElementAt(4).Clone() as TableRow;//小計行
  1305. var trow_Tax = table.Elements<TableRow>().ElementAt(5).Clone() as TableRow;//營業稅
  1306. var trow_Total = table.Elements<TableRow>().ElementAt(7).Clone() as TableRow;//總應收
  1307. var trow_PrePay = table.Elements<TableRow>().ElementAt(8).Clone() as TableRow;//預收
  1308. var jaFeeItems_Tax = new JArray();
  1309. var jaFeeItems_NoTax = new JArray();
  1310. var iDefultRow = 10;
  1311. var iInsertRow = 1;
  1312. var iFeeItemsRow = 1;
  1313. foreach (JObject jo in jaFeeItems)
  1314. {
  1315. string sFinancialTaxRate = jo[@"FinancialTaxRate"] != null ? jo[@"FinancialTaxRate"].ToString() : string.Empty;
  1316. if (sFinancialTaxRate.Replace(@"%", @"").Trim() == @"0")
  1317. {
  1318. jaFeeItems_NoTax.Add(jo);
  1319. }
  1320. else
  1321. {
  1322. jaFeeItems_Tax.Add(jo);
  1323. }
  1324. }
  1325. if (jaFeeItems_Tax.Count > 0)
  1326. {
  1327. var jaFeeItems_TaxSorted = new JArray(jaFeeItems_Tax.OrderBy(p => p[@"OrderBy"]));
  1328. foreach (JObject jo in jaFeeItems_TaxSorted)
  1329. {
  1330. string sFinancialTWAmount = jo["FinancialTWAmount"] != null ? jo["FinancialTWAmount"].ToString() : string.Empty;
  1331. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1332. var iCellCount = trow_Item.Elements<TableCell>().Count();
  1333. if (iCellCount == 9)
  1334. {
  1335. ExhibitionHelper.RenderFeeItemsTW(trow_Item, jo, iCellCount, iFeeItemsRow);
  1336. }
  1337. else
  1338. {
  1339. ExhibitionHelper.RenderFeeItemsFR(trow_Item, jo, iCellCount, iFeeItemsRow);
  1340. }
  1341. table.InsertAt<TableRow>(trow_Item, iInsertRow + iDefultRow); //插入一行費用
  1342. iInsertRow++;
  1343. iFeeItemsRow++;
  1344. iPARM17 += double.Parse(sFinancialTWAmount);
  1345. }
  1346. table.InsertAt<TableRow>(trow_Amount, iInsertRow + iDefultRow);//插入一行小計行
  1347. iInsertRow++;
  1348. //營業稅
  1349. table.InsertAt<TableRow>(trow_Tax, iInsertRow + iDefultRow);//插入一行營業稅
  1350. iInsertRow++;
  1351. }
  1352. if (jaFeeItems_NoTax.Count > 0)
  1353. {
  1354. var jaFeeItems_NoTaxSorted = new JArray(jaFeeItems_NoTax.OrderBy(p => p[@"OrderBy"]));
  1355. var iCellCount = 0;
  1356. foreach (JObject jo in jaFeeItems_NoTaxSorted)
  1357. {
  1358. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1359. iCellCount = trow_Item.Elements<TableCell>().Count();
  1360. if (iCellCount == 9)
  1361. {
  1362. ExhibitionHelper.RenderFeeItemsTW(trow_Item, jo, iCellCount, iFeeItemsRow);
  1363. }
  1364. else
  1365. {
  1366. ExhibitionHelper.RenderFeeItemsFR(trow_Item, jo, iCellCount, iFeeItemsRow);
  1367. }
  1368. //插入一行
  1369. table.InsertAt<TableRow>(trow_Item, iInsertRow + iDefultRow);
  1370. iInsertRow++;
  1371. iFeeItemsRow++;
  1372. }
  1373. if (iCellCount == 9)
  1374. {
  1375. table.InsertAt<TableRow>(trow_Total, iInsertRow + iDefultRow);//插入一行總應收
  1376. iInsertRow++;
  1377. //table.InsertAt<TableRow>(trow_PrePay, iInsertRow + iDefultRow);//插入一行預收
  1378. }
  1379. }
  1380. string sStrAdvance = oBill[@"Advance"] != null ? oBill[@"Advance"].ToString() : string.Empty;
  1381. //判斷是否有預收款項
  1382. if (sStrAdvance != "0")
  1383. {
  1384. table.InsertAt<TableRow>(trow_PrePay, iInsertRow + iDefultRow);//插入一行預收
  1385. iInsertRow++;
  1386. }
  1387. //刪除模板行
  1388. for (int i = 0; i < 8; i++)
  1389. {
  1390. table.Elements<TableRow>().ElementAt(1).Remove();
  1391. }
  1392. }
  1393. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  1394. if (oExhibition == null)
  1395. {
  1396. oExhibition = new OTB_OPM_Exhibition();
  1397. }
  1398. string sStrIsRetn = joBill[@"IsRetn"] != null ? joBill[@"IsRetn"].ToString() : string.Empty;
  1399. string sTaxSum = joBill[@"TaxSum"] != null ? joBill[@"TaxSum"].ToString() : string.Empty;
  1400. string sAmountTaxSum = joBill[@"AmountTaxSum"] != null ? joBill[@"AmountTaxSum"].ToString() : string.Empty;
  1401. string sAdvance = joBill[@"Advance"] != null ? joBill[@"Advance"].ToString() : string.Empty;
  1402. string sTotalReceivable = joBill[@"TotalReceivable"] != null ? joBill[@"TotalReceivable"].ToString() : string.Empty;
  1403. string sWeight = joBill["Weight"] != null ? joBill["Weight"].ToString() : string.Empty;
  1404. string sVolume = joBill["Volume"] != null ? joBill["Volume"].ToString() : string.Empty;
  1405. string sNumber = joBill["Number"] != null ? joBill["Number"].ToString() : string.Empty;
  1406. string sUnit = joBill["Unit"] != null ? joBill["Unit"].ToString() : string.Empty;
  1407. string sContactorName = joBill["ContactorName"] != null ? joBill["ContactorName"].ToString() : string.Empty;
  1408. string sTelephone = joBill["Telephone"] != null ? joBill["Telephone"].ToString() : string.Empty;
  1409. string sMemo = joBill["Memo"] != null ? joBill["Memo"].ToString() : string.Empty;
  1410. var ReturnType = sStrIsRetn == "Y";
  1411. var sPARM3 = @"";
  1412. var sPARM4 = @"";
  1413. var sPARM7 = oExhibition.Exhibitioname_TW + ExhibitionHelper.GetExhibitioExportComment(false, ReturnType);
  1414. var sPARM14 = @"";
  1415. var sPARM16 = @"";
  1416. double.TryParse(sTaxSum, out var dTaxSum);
  1417. double.TryParse(sAmountTaxSum, out var dAmountTaxSum);
  1418. double.TryParse(sAdvance, out var dAdvance);
  1419. double.TryParse(sTotalReceivable, out var dTotalReceivable);
  1420. var iPARM18 = dTaxSum;
  1421. var iPARM19 = dAmountTaxSum;
  1422. var iPARM20 = dAdvance;
  1423. var iPARM21 = dTotalReceivable;
  1424. var sPARM27 = @"";
  1425. var sPARM29 = ExhibitionHelper.GetEnglishName(i_crm.USERID);
  1426. var sPARM30 = sCurrency;
  1427. if (!string.IsNullOrEmpty(sWeight))
  1428. {
  1429. sPARM16 = sWeight + @"KG";
  1430. }
  1431. if (!string.IsNullOrEmpty(sVolume))
  1432. {
  1433. sPARM16 = sPARM16 == @"" ? sVolume + @"CBM" : sPARM16 + @" / " + sVolume + @"CBM";
  1434. }
  1435. if (!string.IsNullOrEmpty(sNumber))
  1436. {
  1437. sPARM14 = sNumber + sUnit;
  1438. }
  1439. if (!string.IsNullOrEmpty(sContactorName))
  1440. {
  1441. sPARM3 = sContactorName;
  1442. }
  1443. if (!string.IsNullOrEmpty(sTelephone))
  1444. {
  1445. sPARM4 = sTelephone;
  1446. }
  1447. if (!string.IsNullOrEmpty(sMemo))
  1448. {
  1449. sPARM27 = System.Security.SecurityElement.Escape(sMemo);
  1450. }
  1451. if (bTWCurrency)
  1452. {
  1453. iPARM17 = ExhibitionHelper.Round(iPARM17, 0);
  1454. iPARM18 = ExhibitionHelper.Round(iPARM18, 0);
  1455. iPARM19 = ExhibitionHelper.Round(iPARM19, 0);
  1456. iPARM20 = ExhibitionHelper.Round(iPARM20, 0);
  1457. iPARM21 = ExhibitionHelper.Round(iPARM21, 0);
  1458. }
  1459. else
  1460. {
  1461. sPARM7 = oExhibition.Exhibitioname_EN + ExhibitionHelper.GetExhibitioExportComment(true, ReturnType);
  1462. }
  1463. if (bTW)
  1464. {
  1465. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1466. sPARM29 = member.MemberName;
  1467. }
  1468. ExhibitionHelper.RenderMemo(body, sPARM27);
  1469. var sShipAndVoyage = oOpm.ShipAndVoyage ?? @"";
  1470. var sBillLadNO = oOpm.BillLadNO ?? @"";
  1471. var sBillLadNOSub = oOpm.BillLadNOSub ?? @"";
  1472. var sShipmentPort = oOpm.ShipmentPort ?? @"";
  1473. var sDestinationPort = oOpm.Destination ?? @"";
  1474. string sStrReFlow = joBill["ReFlow"] != null ? joBill["ReFlow"].ToString() : string.Empty;
  1475. if (sStrIsRetn == @"Y" && joBill[@"SupplierGuid"] != null && !string.IsNullOrEmpty(sStrReFlow))
  1476. {
  1477. var sCustomerGuid = joBill[@"CustomerGuid"] != null ? joBill[@"CustomerGuid"].ToString() : string.Empty;
  1478. var sSupplierGuid = joBill[@"SupplierGuid"] != null ? joBill[@"SupplierGuid"].ToString() : string.Empty;
  1479. var sReFlow = joBill[@"ReFlow"] != null ? joBill[@"ReFlow"].ToString() : string.Empty;
  1480. var saExhibitors = (JArray)JsonConvert.DeserializeObject(oOpm.Exhibitors);
  1481. foreach (JObject jo in saExhibitors)
  1482. {
  1483. string sSupplierID = jo[@"SupplierID"] != null ? jo[@"SupplierID"].ToString() : string.Empty;
  1484. string sGUID = jo[@"guid"] != null ? jo[@"guid"].ToString() : string.Empty;
  1485. if (sCustomerGuid == sSupplierID && sSupplierGuid == sGUID && jo[sReFlow] != null)
  1486. {
  1487. var oReFlow = (JObject)jo[sReFlow];
  1488. sShipAndVoyage = oReFlow[@"ShipAndVoyage"] == null ? @"" : oReFlow[@"ShipAndVoyage"].ToString();
  1489. sBillLadNO = oReFlow[@"BillLadNO"] == null ? @"" : oReFlow[@"BillLadNO"].ToString();
  1490. sBillLadNOSub = oReFlow[@"BillLadNOSub"] == null ? @"" : oReFlow[@"BillLadNOSub"].ToString();
  1491. sShipmentPort = oReFlow[@"ShipmentPort"] == null ? @"" : oReFlow[@"ShipmentPort"].ToString();
  1492. sDestinationPort = oReFlow[@"Destination"] == null ? @"" : oReFlow[@"Destination"].ToString();
  1493. break;
  1494. }
  1495. }
  1496. }
  1497. var BillCheckDate = string.IsNullOrWhiteSpace(joBill[@"BillCheckDate"]?.ToString()) ? "" : Convert.ToDateTime(joBill[@"BillCheckDate"].ToString()).ToString(@"yyyy/MM/dd");
  1498. var sBody = body.InnerXml
  1499. .Replace(@"[PARM1]", string.IsNullOrEmpty(oPayer.CustomerCName) ? Common.EncodeEscapeChar(oPayer.CustomerEName) : Common.EncodeEscapeChar(oPayer.CustomerCName))
  1500. .Replace(@"[PARM2]", Common.EncodeEscapeChar(ExhibitionHelper.GetBillAddress(oPayer)))
  1501. .Replace(@"[PARM3]", Common.EncodeEscapeChar(sPARM3))
  1502. .Replace(@"[PARM4]", Common.EncodeEscapeChar(sPARM4))
  1503. .Replace(@"[PARM5]", oPayer.FAX ?? @"")
  1504. .Replace(@"[PARM6]", oPayer.UniCode ?? @"")
  1505. .Replace(@"[PARM7]", Common.EncodeEscapeChar(sPARM7))
  1506. .Replace(@"[PARM8]", sBillNO)
  1507. .Replace(@"[PARM9]", BillCheckDate)
  1508. .Replace(@"[PARM10]", Common.EncodeEscapeChar(sShipAndVoyage))
  1509. .Replace(@"[PARM11]", Common.EncodeEscapeChar(sBillLadNO))
  1510. .Replace(@"[PARM12]", Common.EncodeEscapeChar(sShipmentPort))
  1511. .Replace(@"[PARM13]", Common.EncodeEscapeChar(sDestinationPort))
  1512. .Replace(@"[PARM14]", Common.EncodeEscapeChar(sPARM14))
  1513. .Replace(@"[PARM16]", Common.EncodeEscapeChar(sPARM16))
  1514. .Replace(@"[PARM17]", bTWCurrency ? $@"{iPARM17:N0}" : $@"{iPARM17:N2}")
  1515. .Replace(@"[PARM18]", bTWCurrency ? $@"{iPARM18:N0}" : $@"{iPARM18:N2}")
  1516. .Replace(@"[PARM19]", bTWCurrency ? $@"{iPARM19:N0}" : $@"{iPARM19:N2}")
  1517. .Replace(@"[PARM20]", bTWCurrency ? $@"{iPARM20:N0}" : $@"{iPARM20:N2}")
  1518. .Replace(@"[PARM21]", bTWCurrency ? $@"{iPARM21:N0}" : $@"{iPARM21:N2}")
  1519. .Replace(@"[PARM22]", sPayDateText)
  1520. .Replace(@"[PARM27]", Common.EncodeEscapeChar(sPARM27))
  1521. .Replace(@"[PARM28]", Common.EncodeEscapeChar(sBillLadNOSub))
  1522. .Replace(@"[PARM29]", Common.EncodeEscapeChar(sPARM29))
  1523. .Replace(@"[PARM30]", Common.EncodeEscapeChar(sPARM30));
  1524. //若新增PARM變數時候,word也要新增摘要資訊參數(資訊 => 摘要資訊 => 進階摘要資訊 => 新增變數)
  1525. foreach (JObject jo in JTmpKeys)
  1526. {
  1527. string sTemplKey = jo["TemplKey"] != null ? jo["TemplKey"].ToString() : string.Empty;
  1528. string sTemplKeyValue = jo["TemplKeyValue"] != null ? jo["TemplKeyValue"].ToString() : string.Empty;
  1529. if (!string.IsNullOrEmpty(sTemplKey))
  1530. {
  1531. var Value = Common.EncodeEscapeChar(sTemplKeyValue);
  1532. var Key = sTemplKey;
  1533. if (Key.Contains("PARM") && WatermarkInfo.Item1)
  1534. {
  1535. Value = "";
  1536. }
  1537. sBody = sBody.Replace(Key, Value);
  1538. }
  1539. }
  1540. doc.MainDocumentPart.Document.Body.InnerXml = sBody;
  1541. doc.MainDocumentPart.Document.Save();
  1542. if (WatermarkInfo.Item1)
  1543. {
  1544. Common.WordAddWatermartText(doc, WatermarkInfo.Item2);
  1545. }
  1546. }
  1547. if (File.Exists(sCurPath))
  1548. {
  1549. File.Delete(sCurPath);
  1550. }
  1551. if (sAction.StartsWith(@"Print"))
  1552. {
  1553. sCurPath = sCurPath.Replace(@"docx", @"pdf");
  1554. var bOk = Common.WordToPDF(sTempFile, sCurPath);
  1555. if (!bOk)
  1556. {
  1557. sCurPath = @"";
  1558. }
  1559. }
  1560. else
  1561. {
  1562. File.Copy(sTempFile, sCurPath);
  1563. }
  1564. Thread.Sleep(500);
  1565. File.Delete(sTempFile); //刪除臨時文件
  1566. sCurPath = sCurPath.Replace(sBase, @"");
  1567. saOutPut.Add(sCurPath);
  1568. }
  1569. rm = new SuccessResponseMessage(null, i_crm);
  1570. rm.DATA.Add(BLWording.REL, saOutPut);
  1571. } while (false);
  1572. }
  1573. catch (Exception ex)
  1574. {
  1575. sMsg = Util.GetLastExceptionMsg(ex);
  1576. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm) + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"PrintBill(出口帳單列印下載)", @"", @"", @"");
  1577. //sMsg += ex.StackTrace; //測試機列印pdf錯誤訊息
  1578. }
  1579. finally
  1580. {
  1581. if (null != sMsg)
  1582. {
  1583. rm = new ErrorResponseMessage(sMsg, i_crm);
  1584. }
  1585. Logger.Debug(@"ExhibitionExport_UpdService.PrintBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1586. }
  1587. return rm;
  1588. }
  1589. #endregion 出口帳單列印下載
  1590. #region 出口收據列印下載
  1591. /// <summary>
  1592. /// 出口收據列印下載
  1593. /// </summary>
  1594. /// <param name="i_crm">todo: describe i_crm parameter on PrintReceipt</param>
  1595. /// <returns></returns>
  1596. public ResponseMessage PrintReceipt(RequestMessage i_crm)
  1597. {
  1598. ResponseMessage rm = null;
  1599. string sMsg = null;
  1600. var db = SugarBase.GetIntance();
  1601. var sOutPut = Common.ConfigGetValue(@"", @"OutFilesPath");
  1602. try
  1603. {
  1604. do
  1605. {
  1606. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1607. var sTemplID = _fetchString(i_crm, OTB_SYS_OfficeTemplate.CN_TEMPLID);
  1608. var sBill = _fetchString(i_crm, @"Bill");
  1609. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1610. var sAction = _fetchString(i_crm, @"Action");
  1611. var bTW = sTemplID.EndsWith(@"TW");
  1612. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1613. var oOpm = sdb.GetById(sExportBillNO);
  1614. if (oOpm == null)
  1615. {
  1616. sMsg = @"系統找不到對應的基本資料,請核查!";
  1617. break;
  1618. }
  1619. var oTempl = db.Queryable<OTB_SYS_OfficeTemplate>().Single(it => it.OrgID == i_crm.ORIGID && it.TemplID == sTemplID);
  1620. if (oTempl == null)
  1621. {
  1622. sMsg = @"請檢查模版設定";
  1623. break;
  1624. }
  1625. var oFile = db.Queryable<OTB_SYS_Files>()
  1626. .Single(it => it.OrgID == i_crm.ORIGID && it.ParentID == oTempl.FileID);
  1627. if (oFile == null)
  1628. {
  1629. sMsg = @"系統找不到對應的帳單模版";
  1630. break;
  1631. }
  1632. var WatermarkInfo = Common.GetWatermarkInfo(oBill[@"AuditVal"]?.ToString(), sTemplID, i_crm.ORIGID);
  1633. var bTWCurrency = oBill[@"Currency"].ToString() == @"NTD";
  1634. var oPayer = new OTB_CRM_Customers();
  1635. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  1636. {
  1637. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  1638. if (oPayer == null)
  1639. {
  1640. sMsg = @"系統找不到付款人資訊";
  1641. break;
  1642. }
  1643. }
  1644. var sTempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, oFile.FilePath);//Word模版路徑
  1645. var sBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"");
  1646. sOutPut = sBase + sOutPut;
  1647. Common.FnCreateDir(sOutPut);//如果不存在就創建文件夾
  1648. var sDocxName = @"Receipt-" + oBill[@"BillNO"];
  1649. //建立臨時文件
  1650. var sTempFile = Path.GetTempPath() + sDocxName + @".docx";
  1651. sOutPut += sDocxName + @".docx";
  1652. if (File.Exists(sTempFile))
  1653. {
  1654. File.Delete(sTempFile);
  1655. }
  1656. File.Copy(sTempPath, sTempFile);
  1657. var JTmpKeys = (JArray)JsonConvert.DeserializeObject(oTempl.TemplKeys);
  1658. double iAcountAll = 0;
  1659. using (WordprocessingDocument doc = WordprocessingDocument.Open(sTempFile, true))//顯示Word
  1660. {
  1661. var body = doc.MainDocumentPart.Document.Body;
  1662. var jaFeeItems = (JArray)JsonConvert.DeserializeObject(oBill[@"FeeItems"].ToString());
  1663. //查找:獲取第三個表格
  1664. var table = body.Elements<Table>().ElementAt(3);
  1665. if (jaFeeItems.Count > 0)
  1666. {
  1667. var jaFeeItems_NoTax = new JArray();
  1668. var iInsertRow = 5;
  1669. foreach (JObject jo in jaFeeItems)
  1670. {
  1671. if (jo[@"FinancialTaxRate"].ToString().Replace(@"%", @"").Trim() == @"0")
  1672. {
  1673. jaFeeItems_NoTax.Add(jo);
  1674. }
  1675. }
  1676. if (jaFeeItems_NoTax.Count > 0)
  1677. {
  1678. var jaFeeItems_NoTaxSorted = new JArray(jaFeeItems_NoTax.OrderBy(p => p[@"OrderBy"]));
  1679. foreach (JObject jo in jaFeeItems_NoTaxSorted)
  1680. {
  1681. var trow_First = table.Elements<TableRow>().ElementAt(1);//費用明細第一行
  1682. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1683. if (iInsertRow == 5)
  1684. {
  1685. for (int i = 0; i < 5; i++)
  1686. {
  1687. var tcell = trow_First.Elements<TableCell>().ElementAt(i);
  1688. var tmpPa = tcell.Elements<Paragraph>().FirstOrDefault();
  1689. var tmpRun = tmpPa.Elements<Run>().FirstOrDefault();
  1690. var tmpText = tmpRun.Elements<Text>().FirstOrDefault();
  1691. switch (i)
  1692. {
  1693. case 0:
  1694. var sRemark = jo[@"Memo"] == null ? @"" : jo[@"Memo"].ToString();
  1695. tmpText.Text = jo[@"FinancialCode"].ToString() == @"TE001" ? (sRemark == @"" ? jo[@"FinancialCostStatement"].ToString() : sRemark) : jo[@"FinancialCostStatement"] + (sRemark == @"" ? @"" : @"(" + sRemark + @")");
  1696. break;
  1697. case 1:
  1698. tmpText.Text = jo[@"FinancialCurrency"].ToString();
  1699. break;
  1700. case 2:
  1701. tmpText.Text = jo[@"FinancialExchangeRate"].ToString();
  1702. break;
  1703. case 3:
  1704. var sFinancialAmount = jo[@"FinancialTWAmount"].ToString();
  1705. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount.Trim() == @"" ? @"0" : sFinancialAmount):N}";
  1706. break;
  1707. case 4:
  1708. var sFinancialAmount4 = jo[@"FinancialTWAmount"].ToString();
  1709. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount4.Trim() == @"" ? @"0" : sFinancialAmount4):N}";
  1710. break;
  1711. default:
  1712. break;
  1713. }
  1714. }
  1715. }
  1716. else
  1717. {
  1718. for (int i = 0; i < 5; i++)
  1719. {
  1720. var tcell = trow_Item.Elements<TableCell>().ElementAt(i);
  1721. var tmpPa = tcell.Elements<Paragraph>().FirstOrDefault();
  1722. var tmpRun = tmpPa.Elements<Run>().FirstOrDefault();
  1723. var tmpText = tmpRun.Elements<Text>().FirstOrDefault();
  1724. switch (i)
  1725. {
  1726. case 0:
  1727. var sRemark = jo[@"Memo"] == null ? @"" : jo[@"Memo"].ToString();
  1728. tmpText.Text = jo[@"FinancialCode"].ToString() == @"TE001" ? (sRemark == @"" ? jo[@"FinancialCostStatement"].ToString() : sRemark) : jo[@"FinancialCostStatement"] + (sRemark == @"" ? @"" : @"(" + sRemark + @")");
  1729. break;
  1730. case 1:
  1731. tmpText.Text = jo[@"FinancialCurrency"].ToString();
  1732. break;
  1733. case 2:
  1734. tmpText.Text = jo[@"FinancialExchangeRate"].ToString();
  1735. break;
  1736. case 3:
  1737. var sFinancialAmount = jo[@"FinancialTWAmount"].ToString();
  1738. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount.Trim() == @"" ? @"0" : sFinancialAmount):N}";
  1739. break;
  1740. case 4:
  1741. var sFinancialAmount4 = jo[@"FinancialTWAmount"].ToString();
  1742. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount4.Trim() == @"" ? @"0" : sFinancialAmount4):N}";
  1743. break;
  1744. default:
  1745. break;
  1746. }
  1747. }
  1748. }
  1749. iAcountAll += double.Parse(jo[@"FinancialTWAmount"].ToString());
  1750. //插入一行
  1751. table.InsertAt<TableRow>(trow_Item, iInsertRow);
  1752. iInsertRow++;
  1753. }
  1754. table.Elements<TableRow>().ElementAt(2).Remove();
  1755. table.Elements<TableRow>().ElementAt(2).Remove();
  1756. }
  1757. else
  1758. {
  1759. table.Elements<TableRow>().ElementAt(1).Remove();
  1760. table.Elements<TableRow>().ElementAt(1).Remove();
  1761. }
  1762. //刪除模板行
  1763. }
  1764. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  1765. if (oExhibition == null)
  1766. {
  1767. oExhibition = new OTB_OPM_Exhibition();
  1768. }
  1769. var sPARM1 = string.IsNullOrEmpty(oPayer.CustomerCName) ? oPayer.CustomerEName : oPayer.CustomerCName;
  1770. var sPARM2 = oBill[@"ReceiptDate"].ToString();
  1771. var sPARM3 = oPayer.UniCode;
  1772. var sPARM4 = oBill[@"ReceiptNumber"].ToString();
  1773. var sPARM5 = ExhibitionHelper.GetBillAddress(oPayer);
  1774. var sPARM6 = oExhibition.Exhibitioname_TW;
  1775. var sPARM7 = oBill[@"TaxSum"].ToString();
  1776. var sPARM8 = @"";
  1777. var sPARM9 = oBill[@"BillNO"].ToString();
  1778. var iPARM7 = iAcountAll;
  1779. if (sPARM2 != @"")
  1780. {
  1781. sPARM2 = Common.DateToTw(sPARM2);
  1782. var saPARM2 = sPARM2.Split('/');
  1783. sPARM2 = saPARM2[0] + @"年" + saPARM2[1] + @"月" + saPARM2[2] + @"日";
  1784. }
  1785. iPARM7 = ExhibitionHelper.Round(iPARM7, 0);
  1786. sPARM8 = Common.MoneyToUpper(iPARM7.ToString());
  1787. sPARM7 = $@"{iPARM7:N0}";
  1788. var sBody = body.InnerXml
  1789. .Replace(@"[PARM1]", Common.EncodeEscapeChar(sPARM1))
  1790. .Replace(@"[PARM2]", Common.EncodeEscapeChar(sPARM2))
  1791. .Replace(@"[PARM3]", Common.EncodeEscapeChar(sPARM3))
  1792. .Replace(@"[PARM4]", Common.EncodeEscapeChar(sPARM4))
  1793. .Replace(@"[PARM5]", Common.EncodeEscapeChar(sPARM5))
  1794. .Replace(@"[PARM6]", Common.EncodeEscapeChar(sPARM6))
  1795. .Replace(@"[PARM7]", Common.EncodeEscapeChar(sPARM7))
  1796. .Replace(@"[PARM8]", Common.EncodeEscapeChar(sPARM8))
  1797. .Replace(@"[PARM9]", Common.EncodeEscapeChar(sPARM9));
  1798. foreach (JObject jo in JTmpKeys)
  1799. {
  1800. if (jo.Property(@"TemplKey") != null && jo[@"TemplKey"].ToString() != @"")
  1801. {
  1802. sBody = sBody.Replace(jo[@"TemplKey"].ToString(), Common.EncodeEscapeChar(jo[@"TemplKeyValue"].ToString()));
  1803. }
  1804. }
  1805. doc.MainDocumentPart.Document.Body.InnerXml = sBody + sBody;
  1806. doc.MainDocumentPart.Document.Save();
  1807. if (WatermarkInfo.Item1)
  1808. {
  1809. Common.WordAddWatermartText(doc, WatermarkInfo.Item2);
  1810. }
  1811. }
  1812. if (File.Exists(sOutPut))
  1813. {
  1814. File.Delete(sOutPut);
  1815. }
  1816. if (sAction.StartsWith(@"Print"))
  1817. {
  1818. sOutPut = sOutPut.Replace(@"docx", @"pdf");
  1819. var bOk = Common.WordToPDF(sTempFile, sOutPut);
  1820. if (!bOk)
  1821. {
  1822. sOutPut = @"";
  1823. }
  1824. }
  1825. else
  1826. {
  1827. File.Copy(sTempFile, sOutPut);
  1828. }
  1829. Thread.Sleep(500);
  1830. File.Delete(sTempFile); //刪除臨時文件
  1831. sOutPut = sOutPut.Replace(sBase, @"");
  1832. rm = new SuccessResponseMessage(null, i_crm);
  1833. rm.DATA.Add(BLWording.REL, sOutPut);
  1834. } while (false);
  1835. }
  1836. catch (Exception ex)
  1837. {
  1838. sMsg = Util.GetLastExceptionMsg(ex);
  1839. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"PrintReceipt(出口收據列印下載)", @"", @"", @"");
  1840. }
  1841. finally
  1842. {
  1843. if (null != sMsg)
  1844. {
  1845. rm = new ErrorResponseMessage(sMsg, i_crm);
  1846. }
  1847. Logger.Debug(@"ExhibitionExport_UpdService.PrintReceipt Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1848. }
  1849. return rm;
  1850. }
  1851. #endregion 出口收據列印下載
  1852. #region 出口(退運)報價和預估成本提交審核
  1853. /// <summary>
  1854. /// 出口(退運)報價和預估成本提交審核
  1855. /// </summary>
  1856. /// <param name="i_crm">todo: describe i_crm parameter on ReturnToAuditForQuote</param>
  1857. /// <returns></returns>
  1858. public ResponseMessage ReturnToAuditForQuote(RequestMessage i_crm)
  1859. {
  1860. ResponseMessage rm = null;
  1861. string sMsg = null;
  1862. try
  1863. {
  1864. rm = SugarBase.ExecTran(db =>
  1865. {
  1866. do
  1867. {
  1868. var data = i_crm.DATA as Dictionary<string, object>;
  1869. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1870. var sSourceID = _fetchString(i_crm, @"SourceID");
  1871. var sIndex = _fetchString(i_crm, @"Index");
  1872. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1873. var oOpm = sdb.GetById(sExportBillNO);
  1874. if (oOpm == null)
  1875. {
  1876. sMsg = @"系統找不到對應的基本資料,請核查!";
  1877. break;
  1878. }
  1879. var sTitle = @"「出口」" + oOpm.ExportBillName + @"(" + oOpm.RefNumber + @")" + @"「退運" + sIndex + @"報價單/預估成本審核」";
  1880. if (i_crm.LANG == @"zh")
  1881. {
  1882. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  1883. }
  1884. //更新基本資料
  1885. var oOpmUpd = new OTB_OPM_ExportExhibition
  1886. {
  1887. ReturnBills = data[OTB_OPM_ExportExhibition.CN_RETURNBILLS].ToString(),
  1888. ModifyUser = i_crm.USERID,
  1889. ModifyDate = DateTime.Now
  1890. };
  1891. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  1892. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  1893. //更新代辦
  1894. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  1895. var saSupervisor = new List<string>();
  1896. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1897. if (member.MemberID != null)
  1898. {
  1899. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  1900. {
  1901. saSupervisor.Add(member.ImmediateSupervisor);
  1902. }
  1903. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  1904. if (oDept != null)
  1905. {
  1906. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  1907. }
  1908. }
  1909. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  1910. if (sBillAuditor != @"")
  1911. {
  1912. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  1913. foreach (string auditor in saBillAuditor)
  1914. {
  1915. saSupervisor.Add(auditor);
  1916. }
  1917. }
  1918. saSupervisor = saSupervisor.Distinct<string>().ToList();
  1919. var listTips = new List<OTB_SYS_Tips>();
  1920. var listTask = new List<OTB_SYS_Task>();
  1921. if (saSupervisor.Count > 0)
  1922. {
  1923. foreach (string supervisor in saSupervisor)
  1924. {
  1925. //添加提醒消息
  1926. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO, WebAppGlobalConstWord.BELL);
  1927. listTips.Add(oTipsAdd);
  1928. //添加代辦
  1929. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO);
  1930. listTask.Add(oTaskAdd);
  1931. }
  1932. }
  1933. else
  1934. {
  1935. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  1936. break;
  1937. }
  1938. if (listTips.Count > 0)
  1939. {
  1940. db.Insertable(listTips).ExecuteCommand();
  1941. }
  1942. if (listTask.Count > 0)
  1943. {
  1944. db.Insertable(listTask).ExecuteCommand();
  1945. }
  1946. rm = new SuccessResponseMessage(null, i_crm);
  1947. rm.DATA.Add(BLWording.REL, saSupervisor);
  1948. } while (false);
  1949. return rm;
  1950. });
  1951. }
  1952. catch (Exception ex)
  1953. {
  1954. sMsg = Util.GetLastExceptionMsg(ex);
  1955. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnToAuditForQuote(出口(退運)報價和預估成本提交審核)", @"", @"", @"");
  1956. }
  1957. finally
  1958. {
  1959. if (null != sMsg)
  1960. {
  1961. rm = new ErrorResponseMessage(sMsg, i_crm);
  1962. }
  1963. Logger.Debug(@"ExhibitionExport_UpdService.ReturnToAuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1964. }
  1965. return rm;
  1966. }
  1967. #endregion 出口(退運)報價和預估成本提交審核
  1968. #region 出口(退運)報價和預估成本(主管)審核
  1969. /// <summary>
  1970. /// 出口(退運)報價和預估成本(主管)審核
  1971. /// </summary>
  1972. /// <param name="i_crm">todo: describe i_crm parameter on ReturnAuditForQuote</param>
  1973. /// <returns></returns>
  1974. public ResponseMessage ReturnAuditForQuote(RequestMessage i_crm)
  1975. {
  1976. ResponseMessage rm = null;
  1977. string sMsg = null;
  1978. try
  1979. {
  1980. rm = SugarBase.ExecTran(db =>
  1981. {
  1982. do
  1983. {
  1984. var data = i_crm.DATA as Dictionary<string, object>;
  1985. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  1986. var sAuditVal = _fetchString(i_crm, @"AuditVal");
  1987. var sSourceID = _fetchString(i_crm, @"SourceID");
  1988. var sIndex = _fetchString(i_crm, @"Index");
  1989. var TipsType = WebAppGlobalConstWord.CHECK;
  1990. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  1991. var oOpm = sdb.GetById(sExportBillNO);
  1992. if (oOpm == null)
  1993. {
  1994. sMsg = @"系統找不到對應的基本資料,請核查!";
  1995. break;
  1996. }
  1997. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1998. var sTitle = member.MemberName + @"審核了您的「出口」活動名稱:" + oOpm.ExportBillName + @")的「退運" + sIndex + @"報價單/預估成本審核」";
  1999. if (sAuditVal == @"2")
  2000. {
  2001. sTitle += @",審核結果:通過";
  2002. }
  2003. else
  2004. {
  2005. sTitle += @",審核結果:不通過";
  2006. TipsType = WebAppGlobalConstWord.FAIL;
  2007. }
  2008. if (i_crm.LANG == @"zh")
  2009. {
  2010. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2011. }
  2012. //更新基本資料
  2013. var oOpmUpd = new OTB_OPM_ExportExhibition
  2014. {
  2015. ReturnBills = data[OTB_OPM_ExportExhibition.CN_RETURNBILLS].ToString(),
  2016. ModifyUser = i_crm.USERID,
  2017. ModifyDate = DateTime.Now
  2018. };
  2019. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2020. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2021. //更新代辦
  2022. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  2023. //添加提醒消息
  2024. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO, TipsType);
  2025. db.Insertable(oTipsAdd).ExecuteCommand();
  2026. if (sAuditVal != @"2")
  2027. {
  2028. //添加代辦
  2029. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO);
  2030. db.Insertable(oTaskAdd).ExecuteCommand();
  2031. }
  2032. rm = new SuccessResponseMessage(null, i_crm);
  2033. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2034. } while (false);
  2035. return rm;
  2036. });
  2037. }
  2038. catch (Exception ex)
  2039. {
  2040. sMsg = Util.GetLastExceptionMsg(ex);
  2041. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnAuditForQuote(出口(退運)報價和預估成本(主管)審核)", @"", @"", @"");
  2042. }
  2043. finally
  2044. {
  2045. if (null != sMsg)
  2046. {
  2047. rm = new ErrorResponseMessage(sMsg, i_crm);
  2048. }
  2049. Logger.Debug(@"ExhibitionExport_UpdService.ReturnAuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2050. }
  2051. return rm;
  2052. }
  2053. #endregion 出口(退運)報價和預估成本(主管)審核
  2054. #region 出口(退運)帳單提交審核
  2055. /// <summary>
  2056. /// 出口(退運)帳單提交審核
  2057. /// </summary>
  2058. /// <param name="i_crm">todo: describe i_crm parameter on ReturnToAuditForBill</param>
  2059. /// <returns></returns>
  2060. public ResponseMessage ReturnToAuditForBill(RequestMessage i_crm)
  2061. {
  2062. ResponseMessage rm = null;
  2063. string sMsg = null;
  2064. try
  2065. {
  2066. rm = SugarBase.ExecTran(db =>
  2067. {
  2068. do
  2069. {
  2070. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2071. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2072. var sExhibitors = _fetchString(i_crm, @"Exhibitors");
  2073. var sBill = _fetchString(i_crm, @"Bill");
  2074. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2075. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  2076. var sBillNO = oBill[@"BillNO"].ToString();
  2077. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2078. var oOpm = sdb.GetById(sExportBillNO);
  2079. if (oOpm == null)
  2080. {
  2081. sMsg = @"系統找不到對應的基本資料,請核查!";
  2082. break;
  2083. }
  2084. var sTitle = @"「出口」" + oOpm.ExportBillName + @"退運帳單(" + sBillNO + @")" + @"審核";
  2085. if (i_crm.LANG == @"zh")
  2086. {
  2087. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2088. }
  2089. //更新基本資料
  2090. var oOpmUpd = new OTB_OPM_ExportExhibition
  2091. {
  2092. ReturnBills = sReturnBills,
  2093. Exhibitors = sExhibitors,
  2094. ModifyUser = i_crm.USERID,
  2095. ModifyDate = DateTime.Now
  2096. };
  2097. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.Exhibitors, it.ModifyUser, it.ModifyDate })
  2098. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2099. //更新代辦
  2100. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  2101. var saSupervisor = new List<string>();
  2102. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2103. if (member.MemberID != null)
  2104. {
  2105. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  2106. {
  2107. saSupervisor.Add(member.ImmediateSupervisor);
  2108. }
  2109. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  2110. if (oDept != null)
  2111. {
  2112. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  2113. }
  2114. }
  2115. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  2116. if (sBillAuditor != @"")
  2117. {
  2118. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  2119. foreach (string auditor in saBillAuditor)
  2120. {
  2121. saSupervisor.Add(auditor);
  2122. }
  2123. }
  2124. saSupervisor = saSupervisor.Distinct<string>().ToList();
  2125. var listTips = new List<OTB_SYS_Tips>();
  2126. var listTask = new List<OTB_SYS_Task>();
  2127. if (saSupervisor.Count > 0)
  2128. {
  2129. foreach (string supervisor in saSupervisor)
  2130. {
  2131. //添加提醒消息
  2132. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2133. listTips.Add(oTipsAdd);
  2134. //添加代辦
  2135. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO);
  2136. listTask.Add(oTaskAdd);
  2137. }
  2138. }
  2139. else
  2140. {
  2141. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  2142. break;
  2143. }
  2144. if (listTips.Count > 0)
  2145. {
  2146. db.Insertable(listTips).ExecuteCommand();
  2147. }
  2148. if (listTask.Count > 0)
  2149. {
  2150. db.Insertable(listTask).ExecuteCommand();
  2151. }
  2152. rm = new SuccessResponseMessage(null, i_crm);
  2153. rm.DATA.Add(BLWording.REL, saSupervisor);
  2154. } while (false);
  2155. return rm;
  2156. });
  2157. }
  2158. catch (Exception ex)
  2159. {
  2160. sMsg = Util.GetLastExceptionMsg(ex);
  2161. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnToAuditForBill(出口(退運)帳單提交審核)", @"", @"", @"");
  2162. }
  2163. finally
  2164. {
  2165. if (null != sMsg)
  2166. {
  2167. rm = new ErrorResponseMessage(sMsg, i_crm);
  2168. }
  2169. Logger.Debug(@"ExhibitionExport_UpdService.ReturnToAuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2170. }
  2171. return rm;
  2172. }
  2173. #endregion 出口(退運)帳單提交審核
  2174. #region 出口退運帳單(主管)審核
  2175. /// <summary>
  2176. /// 出口退運帳單(主管)審核
  2177. /// </summary>
  2178. /// <param name="i_crm">todo: describe i_crm parameter on ReturnAuditForBill</param>
  2179. /// <returns></returns>
  2180. public ResponseMessage ReturnAuditForBill(RequestMessage i_crm)
  2181. {
  2182. ResponseMessage rm = null;
  2183. string sMsg = null;
  2184. try
  2185. {
  2186. rm = SugarBase.ExecTran(db =>
  2187. {
  2188. do
  2189. {
  2190. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2191. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2192. var sBill = _fetchString(i_crm, @"Bill");
  2193. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2194. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  2195. var sBillNO = oBill[@"BillNO"].ToString();
  2196. var TipsType = WebAppGlobalConstWord.CHECK;
  2197. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2198. var oOpm = sdb.GetById(sExportBillNO);
  2199. if (oOpm == null)
  2200. {
  2201. sMsg = @"系統找不到對應的基本資料,請核查!";
  2202. break;
  2203. }
  2204. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2205. var sTitle = member.MemberName + @"審核了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的「退運帳單(" + sBillNO + @")」";
  2206. //更新報價和預估成本
  2207. var sAuditVal = oBill[@"AuditVal"].ToString();
  2208. if (sAuditVal == @"2")
  2209. {
  2210. sTitle += @",審核結果:通過";
  2211. }
  2212. else
  2213. {
  2214. sTitle += @",審核結果:不通過";
  2215. TipsType = WebAppGlobalConstWord.FAIL;
  2216. }
  2217. if (i_crm.LANG == @"zh")
  2218. {
  2219. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2220. }
  2221. var oPayer = new OTB_CRM_Customers();
  2222. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  2223. {
  2224. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  2225. if (oPayer == null)
  2226. {
  2227. sMsg = @"系統找不到付款人資訊";
  2228. break;
  2229. }
  2230. }
  2231. //更新基本資料
  2232. var oOpmUpd = new OTB_OPM_ExportExhibition
  2233. {
  2234. ReturnBills = sReturnBills,
  2235. ModifyUser = i_crm.USERID,
  2236. ModifyDate = DateTime.Now
  2237. };
  2238. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2239. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2240. //更新代辦
  2241. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  2242. //添加提醒消息
  2243. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, TipsType);
  2244. db.Insertable(oTipsAdd).ExecuteCommand();
  2245. if (sAuditVal != @"2")
  2246. {
  2247. //添加代辦
  2248. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"ExhibitionExport_Qry", @"?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO);
  2249. db.Insertable(oTaskAdd).ExecuteCommand();
  2250. }
  2251. if (sAuditVal == @"2")
  2252. {
  2253. var oBillsAdd = new OTB_OPM_Bills
  2254. {
  2255. OrgID = i_crm.ORIGID,
  2256. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  2257. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  2258. BillType = @"20",//帳別(收付)(3)
  2259. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  2260. };
  2261. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  2262. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  2263. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  2264. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  2265. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  2266. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  2267. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  2268. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  2269. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  2270. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  2271. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  2272. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  2273. oBillsAdd.Settle = @"N"; //結清否(15)
  2274. oBillsAdd.InvoiceStartNumber = @""; //發票號碼(起)(16)
  2275. oBillsAdd.InvoiceEndNumber = @"";//發票號碼(迄)(17)
  2276. oBillsAdd.Category = @"";//傳票類別(18)
  2277. oBillsAdd.OrderNo = @"";//訂單單號(19)
  2278. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  2279. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  2280. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  2281. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  2282. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  2283. oBillsAdd.UpdateDate = @""; //更新日期(25)
  2284. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  2285. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  2286. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  2287. {
  2288. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  2289. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  2290. }
  2291. else
  2292. {
  2293. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  2294. }
  2295. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  2296. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  2297. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  2298. var sCurrency = oBill[@"Currency"].ToString();
  2299. if (i_crm.ORIGID == "SG")
  2300. {
  2301. if (sCurrency == "RMB")
  2302. {
  2303. sCurrency = "";
  2304. }
  2305. }
  2306. else
  2307. {
  2308. if (sCurrency == "NTD")
  2309. {
  2310. sCurrency = "";
  2311. }
  2312. }
  2313. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  2314. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  2315. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  2316. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  2317. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  2318. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  2319. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  2320. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  2321. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  2322. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  2323. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  2324. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  2325. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  2326. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  2327. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  2328. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  2329. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  2330. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  2331. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  2332. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  2333. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  2334. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  2335. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  2336. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  2337. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  2338. oBillsAdd.CreateUser = i_crm.USERID;
  2339. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  2340. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  2341. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  2342. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  2343. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  2344. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  2345. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO; //Url
  2346. db.Insertable(oBillsAdd).ExecuteCommand();
  2347. }
  2348. var oLogInfo = new OTB_SYS_LogInfo
  2349. {
  2350. OrgID = i_crm.ORIGID,
  2351. SouseId = sBillNO,
  2352. LogType = "billnoupdate",
  2353. LogInfo = sBill,
  2354. CreateUser = i_crm.USERID,
  2355. CreateDate = DateTime.Now,
  2356. Memo = "「出口(退運)帳單」(主管)審核"
  2357. };
  2358. db.Insertable(oLogInfo).ExecuteCommand();
  2359. rm = new SuccessResponseMessage(null, i_crm);
  2360. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2361. } while (false);
  2362. return rm;
  2363. });
  2364. }
  2365. catch (Exception ex)
  2366. {
  2367. sMsg = Util.GetLastExceptionMsg(ex);
  2368. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnAuditForBill(出口退運帳單(主管)審核)", @"", @"", @"");
  2369. }
  2370. finally
  2371. {
  2372. if (null != sMsg)
  2373. {
  2374. rm = new ErrorResponseMessage(sMsg, i_crm);
  2375. }
  2376. Logger.Debug(@"ExhibitionExport_UpdService.ReturnAuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2377. }
  2378. return rm;
  2379. }
  2380. #endregion 出口退運帳單(主管)審核
  2381. #region (會計)取消審核(退運)
  2382. /// <summary>
  2383. /// (會計)取消審核(退運)
  2384. /// </summary>
  2385. /// <param name="i_crm">todo: describe i_crm parameter on ReturnCancelAudit</param>
  2386. /// <returns></returns>
  2387. public ResponseMessage ReturnCancelAudit(RequestMessage i_crm)
  2388. {
  2389. ResponseMessage rm = null;
  2390. string sMsg = null;
  2391. try
  2392. {
  2393. rm = SugarBase.ExecTran(db =>
  2394. {
  2395. do
  2396. {
  2397. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2398. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2399. var sBill = _fetchString(i_crm, @"Bill");
  2400. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2401. var sBillNO = oBill[@"BillNO"].ToString();
  2402. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  2403. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2404. var oOpm = sdb.GetById(sExportBillNO);
  2405. if (oOpm == null)
  2406. {
  2407. sMsg = @"系統找不到對應的基本資料,請核查!";
  2408. break;
  2409. }
  2410. var LogData = _fetchString(i_crm, @"LogData");
  2411. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType.退, i_crm.ORIGID, i_crm.USERID);
  2412. if (!LogResult.Item1)
  2413. {
  2414. sMsg = LogResult.Item2;
  2415. break;
  2416. }
  2417. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2418. var sTitle = @"會計(" + member.MemberName + @")取消審核了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的退運帳單(" + sBillNO + @")」";
  2419. if (i_crm.LANG == @"zh")
  2420. {
  2421. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2422. }
  2423. //更新基本資料
  2424. var oOpmUpd = new OTB_OPM_ExportExhibition
  2425. {
  2426. ReturnBills = sReturnBills,
  2427. ModifyUser = i_crm.USERID,
  2428. ModifyDate = DateTime.Now
  2429. };
  2430. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2431. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2432. //更新代辦
  2433. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  2434. //添加提醒消息
  2435. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2436. db.Insertable(oTipsAdd).ExecuteCommand();
  2437. rm = new SuccessResponseMessage(null, i_crm);
  2438. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2439. } while (false);
  2440. return rm;
  2441. });
  2442. }
  2443. catch (Exception ex)
  2444. {
  2445. sMsg = Util.GetLastExceptionMsg(ex);
  2446. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnCancelAudit((會計)取消審核(退運))", @"", @"", @"");
  2447. }
  2448. finally
  2449. {
  2450. if (null != sMsg)
  2451. {
  2452. rm = new ErrorResponseMessage(sMsg, i_crm);
  2453. }
  2454. Logger.Debug(@"ExhibitionExport_UpdService.ReturnCancelAudit Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2455. }
  2456. return rm;
  2457. }
  2458. #endregion (會計)取消審核(退運)
  2459. #region (會計)銷帳(退運)
  2460. /// <summary>
  2461. /// (會計)銷帳(退運)
  2462. /// </summary>
  2463. /// <param name="i_crm">todo: describe i_crm parameter on ReturnWriteOff</param>
  2464. /// <returns></returns>
  2465. public ResponseMessage ReturnWriteOff(RequestMessage i_crm)
  2466. {
  2467. ResponseMessage rm = null;
  2468. string sMsg = null;
  2469. try
  2470. {
  2471. rm = SugarBase.ExecTran(db =>
  2472. {
  2473. do
  2474. {
  2475. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2476. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2477. var sBill = _fetchString(i_crm, @"Bill");
  2478. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2479. var sBillNO = oBill[@"BillNO"].ToString();
  2480. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2481. var oOpm = sdb.GetById(sExportBillNO);
  2482. if (oOpm == null)
  2483. {
  2484. sMsg = @"系統找不到對應的基本資料,請核查!";
  2485. break;
  2486. }
  2487. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2488. var sTitle = @"會計(" + member.MemberName + @")對您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的退運帳單(" + sBillNO + @")進行了銷帳";
  2489. if (i_crm.LANG == @"zh")
  2490. {
  2491. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2492. }
  2493. //更新基本資料
  2494. var oOpmUpd = new OTB_OPM_ExportExhibition
  2495. {
  2496. ReturnBills = sReturnBills,
  2497. ModifyUser = i_crm.USERID,
  2498. ModifyDate = DateTime.Now
  2499. };
  2500. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2501. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2502. //添加提醒消息
  2503. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2504. db.Insertable(oTipsAdd).ExecuteCommand();
  2505. rm = new SuccessResponseMessage(null, i_crm);
  2506. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2507. } while (false);
  2508. return rm;
  2509. });
  2510. }
  2511. catch (Exception ex)
  2512. {
  2513. sMsg = Util.GetLastExceptionMsg(ex);
  2514. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnWriteOff((會計)銷帳(退運))", @"", @"", @"");
  2515. }
  2516. finally
  2517. {
  2518. if (null != sMsg)
  2519. {
  2520. rm = new ErrorResponseMessage(sMsg, i_crm);
  2521. }
  2522. Logger.Debug(@"ExhibitionExport_UpdService.ReturnWriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2523. }
  2524. return rm;
  2525. }
  2526. #endregion (會計)銷帳(退運)
  2527. #region (會計)取消銷帳(退運)
  2528. /// <summary>
  2529. /// (會計)取消銷帳
  2530. /// </summary>
  2531. /// <param name="i_crm">todo: describe i_crm parameter on ReturnCancelWriteOff</param>
  2532. /// <returns></returns>
  2533. public ResponseMessage ReturnCancelWriteOff(RequestMessage i_crm)
  2534. {
  2535. ResponseMessage rm = null;
  2536. string sMsg = null;
  2537. var db = SugarBase.GetIntance();
  2538. try
  2539. {
  2540. do
  2541. {
  2542. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2543. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2544. var sBill = _fetchString(i_crm, @"Bill");
  2545. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2546. var sBillNO = oBill[@"BillNO"].ToString();
  2547. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2548. var oOpm = sdb.GetById(sExportBillNO);
  2549. if (oOpm == null)
  2550. {
  2551. sMsg = @"系統找不到對應的基本資料,請核查!";
  2552. break;
  2553. }
  2554. var LogData = _fetchString(i_crm, @"LogData");
  2555. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType.退, i_crm.ORIGID, i_crm.USERID);
  2556. if (!LogResult.Item1)
  2557. {
  2558. sMsg = LogResult.Item2;
  2559. break;
  2560. }
  2561. //更新報價和預估成本
  2562. var oOpmUpd = new OTB_OPM_ExportExhibition
  2563. {
  2564. ReturnBills = sReturnBills,
  2565. ModifyUser = i_crm.USERID,
  2566. ModifyDate = DateTime.Now
  2567. };
  2568. var iResult = db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2569. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2570. if (iResult > 0)
  2571. {
  2572. rm = new SuccessResponseMessage(null, i_crm);
  2573. }
  2574. } while (false);
  2575. }
  2576. catch (Exception ex)
  2577. {
  2578. sMsg = Util.GetLastExceptionMsg(ex);
  2579. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnCancelWriteOff((會計)取消銷帳(退運))", @"", @"", @"");
  2580. }
  2581. finally
  2582. {
  2583. if (null != sMsg)
  2584. {
  2585. rm = new ErrorResponseMessage(sMsg, i_crm);
  2586. }
  2587. Logger.Debug(@"ExhibitionExport_UpdService.ReturnCancelWriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2588. }
  2589. return rm;
  2590. }
  2591. #endregion (會計)取消銷帳(退運)
  2592. #region 過帳(退運)
  2593. /// <summary>
  2594. /// 過帳(退運)
  2595. /// </summary>
  2596. /// <param name="i_crm">todo: describe i_crm parameter on ReturnBillPost</param>
  2597. /// <returns></returns>
  2598. public ResponseMessage ReturnBillPost(RequestMessage i_crm)
  2599. {
  2600. ResponseMessage rm = null;
  2601. string sMsg = null;
  2602. try
  2603. {
  2604. rm = SugarBase.ExecTran(db =>
  2605. {
  2606. do
  2607. {
  2608. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2609. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2610. var sBill = _fetchString(i_crm, @"Bill");
  2611. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2612. var sBillNO = oBill[@"BillNO"].ToString();
  2613. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2614. var oOpm = sdb.GetById(sExportBillNO);
  2615. if (oOpm == null)
  2616. {
  2617. sMsg = @"系統找不到對應的基本資料,請核查!";
  2618. break;
  2619. }
  2620. var oPayer = new OTB_CRM_Customers();
  2621. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  2622. {
  2623. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  2624. if (oPayer == null)
  2625. {
  2626. sMsg = @"系統找不到付款人資訊";
  2627. break;
  2628. }
  2629. }
  2630. var sTitle = @"「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")已過帳";
  2631. if (i_crm.LANG == @"zh")
  2632. {
  2633. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2634. }
  2635. //更新基本資料
  2636. var oOpmUpd = new OTB_OPM_ExportExhibition
  2637. {
  2638. ReturnBills = sReturnBills,
  2639. ModifyUser = i_crm.USERID,
  2640. ModifyDate = DateTime.Now
  2641. };
  2642. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2643. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2644. var listAccounts = db.Queryable<OTB_SYS_MembersToRule>()
  2645. .Where(it => it.OrgID == i_crm.ORIGID && it.RuleID == @"Account").ToList();
  2646. var listTips = new List<OTB_SYS_Tips>();
  2647. if (listAccounts.Count > 0)
  2648. {
  2649. foreach (OTB_SYS_MembersToRule account in listAccounts)
  2650. {
  2651. //添加提醒消息
  2652. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, account.MemberID, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2653. listTips.Add(oTipsAdd);
  2654. }
  2655. }
  2656. var oBillsAdd = new OTB_OPM_Bills
  2657. {
  2658. OrgID = i_crm.ORIGID,
  2659. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  2660. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  2661. BillType = @"20",//帳別(收付)(3)
  2662. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  2663. };
  2664. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  2665. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  2666. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  2667. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  2668. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  2669. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  2670. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  2671. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  2672. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  2673. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  2674. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  2675. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  2676. oBillsAdd.Settle = @"N"; //結清否(15)
  2677. oBillsAdd.InvoiceStartNumber = oBill[@"InvoiceNumber"].ToString(); //發票號碼(起)(16)
  2678. oBillsAdd.InvoiceEndNumber = oBill[@"InvoiceNumber"].ToString();//發票號碼(迄)(17)
  2679. oBillsAdd.Category = @"";//傳票類別(18)
  2680. oBillsAdd.OrderNo = @"";//訂單單號(19)
  2681. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  2682. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  2683. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  2684. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  2685. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  2686. oBillsAdd.UpdateDate = @""; //更新日期(25)
  2687. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  2688. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  2689. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  2690. {
  2691. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  2692. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  2693. }
  2694. else
  2695. {
  2696. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  2697. }
  2698. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  2699. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  2700. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  2701. var sCurrency = oBill[@"Currency"].ToString();
  2702. if (i_crm.ORIGID == "SG")
  2703. {
  2704. if (sCurrency == "RMB")
  2705. {
  2706. sCurrency = "";
  2707. }
  2708. }
  2709. else
  2710. {
  2711. if (sCurrency == "NTD")
  2712. {
  2713. sCurrency = "";
  2714. }
  2715. }
  2716. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  2717. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  2718. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  2719. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  2720. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  2721. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  2722. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  2723. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  2724. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  2725. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  2726. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  2727. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  2728. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  2729. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  2730. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  2731. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  2732. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  2733. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  2734. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  2735. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  2736. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  2737. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  2738. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  2739. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  2740. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  2741. oBillsAdd.CreateUser = i_crm.USERID;
  2742. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  2743. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  2744. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  2745. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  2746. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  2747. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  2748. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO; //Url
  2749. db.Insertable(oBillsAdd).ExecuteCommand();
  2750. var oLogInfo = new OTB_SYS_LogInfo
  2751. {
  2752. OrgID = i_crm.ORIGID,
  2753. SouseId = sBillNO,
  2754. LogType = "billnoupdate",
  2755. LogInfo = sBill,
  2756. CreateUser = i_crm.USERID,
  2757. CreateDate = DateTime.Now,
  2758. Memo = "「出口(退運)帳單」(會計)過帳"
  2759. };
  2760. db.Insertable(oLogInfo).ExecuteCommand();
  2761. rm = new SuccessResponseMessage(null, i_crm);
  2762. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2763. } while (false);
  2764. return rm;
  2765. });
  2766. }
  2767. catch (Exception ex)
  2768. {
  2769. sMsg = Util.GetLastExceptionMsg(ex);
  2770. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnBillPost(過帳(退運))", @"", @"", @"");
  2771. }
  2772. finally
  2773. {
  2774. if (null != sMsg)
  2775. {
  2776. rm = new ErrorResponseMessage(sMsg, i_crm);
  2777. }
  2778. Logger.Debug(@"ExhibitionExport_UpdService.ReturnBillPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2779. }
  2780. return rm;
  2781. }
  2782. #endregion 過帳(退運)
  2783. #region 取消過帳(退運)
  2784. /// <summary>
  2785. /// 取消過帳(退運)
  2786. /// </summary>
  2787. /// <param name="i_crm">todo: describe i_crm parameter on ReturnBillCancelPost</param>
  2788. /// <returns></returns>
  2789. public ResponseMessage ReturnBillCancelPost(RequestMessage i_crm)
  2790. {
  2791. ResponseMessage rm = null;
  2792. string sMsg = null;
  2793. try
  2794. {
  2795. rm = SugarBase.ExecTran(db =>
  2796. {
  2797. do
  2798. {
  2799. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2800. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2801. var sBill = _fetchString(i_crm, @"Bill");
  2802. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2803. var sBillNO = oBill[@"BillNO"].ToString();
  2804. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2805. var oOpm = sdb.GetById(sExportBillNO);
  2806. if (oOpm == null)
  2807. {
  2808. sMsg = @"系統找不到對應的基本資料,請核查!";
  2809. break;
  2810. }
  2811. var LogData = _fetchString(i_crm, @"LogData");
  2812. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType.退, i_crm.ORIGID, i_crm.USERID);
  2813. if (!LogResult.Item1)
  2814. {
  2815. sMsg = LogResult.Item2;
  2816. break;
  2817. }
  2818. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2819. var sTitle = @"會計(" + member.MemberName + @")對您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的帳單(" + sBillNO + @")取消了過帳";
  2820. if (i_crm.LANG == @"zh")
  2821. {
  2822. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2823. }
  2824. //更新基本資料
  2825. var oOpmUpd = new OTB_OPM_ExportExhibition
  2826. {
  2827. ReturnBills = sReturnBills,
  2828. ModifyUser = i_crm.USERID,
  2829. ModifyDate = DateTime.Now
  2830. };
  2831. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2832. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2833. //添加提醒消息
  2834. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2835. db.Insertable(oTipsAdd).ExecuteCommand();
  2836. rm = new SuccessResponseMessage(null, i_crm);
  2837. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2838. } while (false);
  2839. return rm;
  2840. });
  2841. }
  2842. catch (Exception ex)
  2843. {
  2844. sMsg = Util.GetLastExceptionMsg(ex);
  2845. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnBillCancelPost(取消過帳(退運))", @"", @"", @"");
  2846. }
  2847. finally
  2848. {
  2849. if (null != sMsg)
  2850. {
  2851. rm = new ErrorResponseMessage(sMsg, i_crm);
  2852. }
  2853. Logger.Debug(@"ExhibitionExport_UpdService.ReturnBillCancelPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2854. }
  2855. return rm;
  2856. }
  2857. #endregion 取消過帳(退運)
  2858. #region 作廢(退運)帳單
  2859. /// <summary>
  2860. /// 作廢(退運)帳單
  2861. /// </summary>
  2862. /// <param name="i_crm">todo: describe i_crm parameter on ReturnBillVoid</param>
  2863. /// <returns></returns>
  2864. public ResponseMessage ReturnBillVoid(RequestMessage i_crm)
  2865. {
  2866. ResponseMessage rm = null;
  2867. string sMsg = null;
  2868. try
  2869. {
  2870. rm = SugarBase.ExecTran(db =>
  2871. {
  2872. do
  2873. {
  2874. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2875. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2876. var sBill = _fetchString(i_crm, @"Bill");
  2877. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2878. var sBillNO = oBill[@"BillNO"].ToString();
  2879. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  2880. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2881. var oOpm = sdb.GetById(sExportBillNO);
  2882. if (oOpm == null)
  2883. {
  2884. sMsg = @"系統找不到對應的基本資料,請核查!";
  2885. break;
  2886. }
  2887. var LogData = _fetchString(i_crm, @"LogData");
  2888. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType.退, i_crm.ORIGID, i_crm.USERID);
  2889. if (!LogResult.Item1)
  2890. {
  2891. sMsg = LogResult.Item2;
  2892. break;
  2893. }
  2894. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2895. var sTitle = @"會計(" + member.MemberName + @")作廢了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的退運帳單(" + sBillNO + @")";
  2896. if (i_crm.LANG == @"zh")
  2897. {
  2898. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2899. }
  2900. //更新基本資料
  2901. var oOpmUpd = new OTB_OPM_ExportExhibition
  2902. {
  2903. ReturnBills = sReturnBills,
  2904. ModifyUser = i_crm.USERID,
  2905. ModifyDate = DateTime.Now
  2906. };
  2907. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2908. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2909. //添加提醒消息
  2910. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2911. db.Insertable(oTipsAdd).ExecuteCommand();
  2912. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  2913. rm = new SuccessResponseMessage(null, i_crm);
  2914. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2915. } while (false);
  2916. return rm;
  2917. });
  2918. }
  2919. catch (Exception ex)
  2920. {
  2921. sMsg = Util.GetLastExceptionMsg(ex);
  2922. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnBillVoid(作廢(退運)帳單)", @"", @"", @"");
  2923. }
  2924. finally
  2925. {
  2926. if (null != sMsg)
  2927. {
  2928. rm = new ErrorResponseMessage(sMsg, i_crm);
  2929. }
  2930. Logger.Debug(@"ExhibitionExport_UpdService.ReturnBillVoid Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  2931. }
  2932. return rm;
  2933. }
  2934. #endregion 作廢(退運)帳單
  2935. #region 刪除(退運)帳單
  2936. /// <summary>
  2937. /// 刪除(退運)帳單
  2938. /// </summary>
  2939. /// <param name="i_crm">todo: describe i_crm parameter on ReturnBillDelete</param>
  2940. /// <returns></returns>
  2941. public ResponseMessage ReturnBillDelete(RequestMessage i_crm)
  2942. {
  2943. ResponseMessage rm = null;
  2944. string sMsg = null;
  2945. try
  2946. {
  2947. rm = SugarBase.ExecTran(db =>
  2948. {
  2949. do
  2950. {
  2951. var sExportBillNO = _fetchString(i_crm, OTB_OPM_ExportExhibition.CN_EXPORTBILLNO);
  2952. var sReturnBills = _fetchString(i_crm, @"ReturnBills");
  2953. var sBill = _fetchString(i_crm, @"Bill");
  2954. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  2955. var sBillNO = oBill[@"BillNO"].ToString();
  2956. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  2957. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  2958. var oOpm = sdb.GetById(sExportBillNO);
  2959. if (oOpm == null)
  2960. {
  2961. sMsg = @"系統找不到對應的基本資料,請核查!";
  2962. break;
  2963. }
  2964. var LogData = _fetchString(i_crm, @"LogData");
  2965. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType.退, i_crm.ORIGID, i_crm.USERID);
  2966. if (!LogResult.Item1)
  2967. {
  2968. sMsg = LogResult.Item2;
  2969. break;
  2970. }
  2971. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  2972. var sTitle = @"管理員(" + member.MemberName + @")刪除了您的「出口」(活動名稱:" + oOpm.ExportBillName + @")的退運帳單(" + sBillNO + @")";
  2973. if (i_crm.LANG == @"zh")
  2974. {
  2975. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  2976. }
  2977. //更新基本資料
  2978. var oOpmUpd = new OTB_OPM_ExportExhibition
  2979. {
  2980. ReturnBills = sReturnBills,
  2981. ModifyUser = i_crm.USERID,
  2982. ModifyDate = DateTime.Now
  2983. };
  2984. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.ReturnBills, it.ModifyUser, it.ModifyDate })
  2985. .Where(it => it.ExportBillNO == sExportBillNO).ExecuteCommand();
  2986. //添加提醒消息
  2987. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=5&ExportBillNO=" + oOpm.ExportBillNO + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  2988. db.Insertable(oTipsAdd).ExecuteCommand();
  2989. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  2990. rm = new SuccessResponseMessage(null, i_crm);
  2991. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  2992. } while (false);
  2993. return rm;
  2994. });
  2995. }
  2996. catch (Exception ex)
  2997. {
  2998. sMsg = Util.GetLastExceptionMsg(ex);
  2999. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"出口", @"ReturnBillDelete(刪除(退運)帳單)", @"", @"", @"");
  3000. }
  3001. finally
  3002. {
  3003. if (null != sMsg)
  3004. {
  3005. rm = new ErrorResponseMessage(sMsg, i_crm);
  3006. }
  3007. Logger.Debug(@"ExhibitionExport_UpdService.ReturnBillDelete Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  3008. }
  3009. return rm;
  3010. }
  3011. #endregion 刪除(退運)帳單
  3012. #region 更新帳單明細
  3013. /// <summary>
  3014. /// 更新帳單明細
  3015. /// </summary>
  3016. /// <param name="i_crm">帳單資料</param>
  3017. /// <returns></returns>
  3018. public ResponseMessage UpdateBillInfo(RequestMessage i_crm)
  3019. {
  3020. ResponseMessage rm = null;
  3021. string sMsg = null;
  3022. var db = SugarBase.GetIntance();
  3023. try
  3024. {
  3025. do
  3026. {
  3027. var sId = _fetchString(i_crm, @"Guid");
  3028. var sBillNO = _fetchString(i_crm, @"BillNO");
  3029. var sdb = new SimpleClient<OTB_OPM_ExportExhibition>(db);
  3030. var oOpm = sdb.GetById(sId);
  3031. if (oOpm == null)
  3032. {
  3033. break;
  3034. }
  3035. var jaBills = new JArray();
  3036. var jaReturns = new JArray();
  3037. jaBills = (JArray)JsonConvert.DeserializeObject(oOpm.Bills);
  3038. jaReturns = (JArray)JsonConvert.DeserializeObject(oOpm.ReturnBills);
  3039. foreach (JObject jo in jaReturns)
  3040. {
  3041. var jaReBills = new JArray();
  3042. jaReBills = (JArray)JsonConvert.DeserializeObject(jo[@"Bills"].ToString());
  3043. foreach (JObject jobill in jaReBills)
  3044. {
  3045. jaBills.Add(jobill);
  3046. }
  3047. }
  3048. if (jaBills.Count > 0)
  3049. {
  3050. foreach (JObject jo in jaBills)
  3051. {
  3052. var oBillInfo_New = ExhibitionService.GetNewBillInfo(jo);
  3053. oBillInfo_New.OrgID = i_crm.ORIGID;
  3054. oBillInfo_New.BillType = i_crm.MODULE;
  3055. oBillInfo_New.ParentId = sId;
  3056. oBillInfo_New.RefNumber = oOpm.RefNumber;
  3057. oBillInfo_New.ExhibitionNO = oOpm.ExhibitionNO;
  3058. oBillInfo_New.ResponsiblePerson = oOpm.ResponsiblePerson;
  3059. oBillInfo_New.ModifyDate = DateTime.Now;
  3060. oBillInfo_New.ModifyUser = i_crm.USERID;
  3061. oBillInfo_New.AuditVal = jo[@"AuditVal"].ToString();
  3062. if (string.IsNullOrEmpty(sBillNO))
  3063. {
  3064. var s_BillNO = jo[@"BillNO"].ToString();
  3065. var oBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  3066. .Single(it => it.OrgID == i_crm.ORIGID && it.BillNO == s_BillNO);
  3067. if (oBillInfo == null)
  3068. {
  3069. oBillInfo_New.CreateDate = DateTime.Now;
  3070. oBillInfo_New.CreateUser = i_crm.USERID;
  3071. db.Insertable(oBillInfo_New).ExecuteCommand();
  3072. }
  3073. else
  3074. {
  3075. oBillInfo_New.CreateDate = oBillInfo.CreateDate;
  3076. oBillInfo_New.CreateUser = oBillInfo.CreateUser;
  3077. db.Updateable(oBillInfo_New).Where(it => it.SN == oBillInfo.SN).ExecuteCommand();
  3078. }
  3079. }
  3080. else
  3081. {
  3082. if (sBillNO == jo[@"BillNO"].ToString())
  3083. {
  3084. var oBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  3085. .Single(it => it.OrgID == i_crm.ORIGID && it.BillNO == sBillNO);
  3086. if (oBillInfo == null)
  3087. {
  3088. oBillInfo_New.CreateDate = DateTime.Now;
  3089. oBillInfo_New.CreateUser = i_crm.USERID;
  3090. db.Insertable(oBillInfo_New).ExecuteCommand();
  3091. }
  3092. else
  3093. {
  3094. oBillInfo_New.CreateDate = oBillInfo.CreateDate;
  3095. oBillInfo_New.CreateUser = oBillInfo.CreateUser;
  3096. db.Updateable(oBillInfo_New).Where(it => it.SN == oBillInfo.SN).ExecuteCommand();
  3097. }
  3098. break;
  3099. }
  3100. }
  3101. }
  3102. }
  3103. rm = new SuccessResponseMessage(null, i_crm);
  3104. } while (false);
  3105. }
  3106. catch (Exception ex)
  3107. {
  3108. sMsg = Util.GetLastExceptionMsg(ex);
  3109. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"", @"UpdateBillInfo(更新帳單明細)", @"", @"", @"");
  3110. }
  3111. finally
  3112. {
  3113. if (null != sMsg)
  3114. {
  3115. rm = new ErrorResponseMessage(sMsg, i_crm);
  3116. }
  3117. Logger.Debug(@"ExhibitionExport_UpdService.UpdateBillInfo Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  3118. }
  3119. return rm;
  3120. }
  3121. #endregion 更新帳單明細
  3122. #region 賬單批次下載
  3123. /// <summary>
  3124. /// 賬單批次下載
  3125. /// </summary>
  3126. /// <param name="i_crm">帳單資料</param>
  3127. /// <returns></returns>
  3128. public ResponseMessage BatchBillsDownLoad(RequestMessage i_crm)
  3129. {
  3130. ResponseMessage rm = null;
  3131. string sMsg = null;
  3132. var db = SugarBase.GetIntance();
  3133. try
  3134. {
  3135. do
  3136. {
  3137. var saOutPut = new List<string>();
  3138. var bIsRetrun = _fetchBool(i_crm, @"IsRetrun");
  3139. if (bIsRetrun)
  3140. {
  3141. }
  3142. else
  3143. {
  3144. }
  3145. rm = new SuccessResponseMessage(null, i_crm);
  3146. rm.DATA.Add(BLWording.REL, saOutPut);
  3147. } while (false);
  3148. }
  3149. catch (Exception ex)
  3150. {
  3151. sMsg = Util.GetLastExceptionMsg(ex);
  3152. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionExport_UpdService), @"", @"BatchBillsDownLoad(賬單批次下載)", @"", @"", @"");
  3153. }
  3154. finally
  3155. {
  3156. if (null != sMsg)
  3157. {
  3158. rm = new ErrorResponseMessage(sMsg, i_crm);
  3159. }
  3160. }
  3161. return rm;
  3162. }
  3163. #endregion 賬單批次下載
  3164. }
  3165. }