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.

2024 lines
109 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. namespace EasyBL.WEBAPP.OPM
  16. {
  17. public class OtherExhibitionTG_UpdService : ServiceBase
  18. {
  19. #region 其他(駒驛)報價和預估成本提交審核
  20. /// <summary>
  21. /// 其他(駒驛)報價和預估成本提交審核
  22. /// </summary>
  23. /// <param name="i_crm">todo: describe i_crm parameter on ToAuditForQuote</param>
  24. /// <returns></returns>
  25. public ResponseMessage ToAuditForQuote(RequestMessage i_crm)
  26. {
  27. ResponseMessage rm = null;
  28. string sMsg = null;
  29. try
  30. {
  31. rm = SugarBase.ExecTran(db =>
  32. {
  33. do
  34. {
  35. var data = i_crm.DATA as Dictionary<string, object>;
  36. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  37. var sSourceID = ((JObject)data[OTB_OPM_OtherExhibitionTG.CN_QUOTE])[EasyNetGlobalConstWord.GUID].ToString();
  38. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  39. var oOpm = sdb.GetById(sId);
  40. if (oOpm == null)
  41. {
  42. sMsg = @"系統找不到對應的基本資料,請核查!";
  43. break;
  44. }
  45. var sTitle = @"「其他(駒驛)」" + oOpm.ImportBillName + @"「報價單/預估成本審核」";
  46. if (i_crm.LANG == @"zh")
  47. {
  48. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  49. }
  50. //更新基本資料
  51. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  52. {
  53. Quote = data[OTB_OPM_OtherExhibitionTG.CN_QUOTE].ToString(),
  54. EstimatedCost = data[OTB_OPM_OtherExhibitionTG.CN_ESTIMATEDCOST].ToString(),
  55. ModifyUser = i_crm.USERID,
  56. ModifyDate = DateTime.Now
  57. };
  58. db.Updateable(oOpmUpd)
  59. .UpdateColumns(it => new { it.Quote, it.EstimatedCost, it.ModifyUser, it.ModifyDate })
  60. .Where(it => it.Guid == sId).ExecuteCommand();
  61. //更新代辦
  62. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  63. var saSupervisor = new List<string>();
  64. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  65. if (member.MemberID != null)
  66. {
  67. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  68. {
  69. saSupervisor.Add(member.ImmediateSupervisor);
  70. }
  71. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  72. if (oDept != null)
  73. {
  74. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  75. }
  76. }
  77. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  78. if (sBillAuditor != @"")
  79. {
  80. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  81. foreach (string auditor in saBillAuditor)
  82. {
  83. saSupervisor.Add(auditor);
  84. }
  85. }
  86. saSupervisor = saSupervisor.Distinct<string>().ToList();
  87. var listTips = new List<OTB_SYS_Tips>();
  88. var listTask = new List<OTB_SYS_Task>();
  89. if (saSupervisor.Count > 0)
  90. {
  91. foreach (string supervisor in saSupervisor)
  92. {
  93. //添加提醒消息
  94. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid, WebAppGlobalConstWord.BELL);
  95. listTips.Add(oTipsAdd);
  96. //添加代辦
  97. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"OtherExhibitionTG_Qry", @"?Action=Upd&GoTab=3&Guid=" + oOpm.Guid);
  98. listTask.Add(oTaskAdd);
  99. }
  100. }
  101. else
  102. {
  103. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  104. break;
  105. }
  106. if (listTips.Count > 0)
  107. {
  108. db.Insertable(listTips).ExecuteCommand();
  109. }
  110. if (listTask.Count > 0)
  111. {
  112. db.Insertable(listTask).ExecuteCommand();
  113. }
  114. rm = new SuccessResponseMessage(null, i_crm);
  115. rm.DATA.Add(BLWording.REL, saSupervisor);
  116. } while (false);
  117. return rm;
  118. });
  119. }
  120. catch (Exception ex)
  121. {
  122. sMsg = Util.GetLastExceptionMsg(ex);
  123. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"ToAuditForQuote(其他(駒驛)報價和預估成本提交審核)", @"", @"", @"");
  124. }
  125. finally
  126. {
  127. if (null != sMsg)
  128. {
  129. rm = new ErrorResponseMessage(sMsg, i_crm);
  130. }
  131. Logger.Debug(@"OtherExhibitionTG_UpdService.ToAuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  132. }
  133. return rm;
  134. }
  135. #endregion 其他(駒驛)報價和預估成本提交審核
  136. #region 其他(駒驛)報價和預估成本(主管)審核
  137. /// <summary>
  138. /// 其他(駒驛)報價和預估成本(主管)審核
  139. /// </summary>
  140. /// <param name="i_crm">todo: describe i_crm parameter on AuditForQuote</param>
  141. /// <returns></returns>
  142. public ResponseMessage AuditForQuote(RequestMessage i_crm)
  143. {
  144. ResponseMessage rm = null;
  145. string sMsg = null;
  146. try
  147. {
  148. rm = SugarBase.ExecTran(db =>
  149. {
  150. do
  151. {
  152. var data = i_crm.DATA as Dictionary<string, object>;
  153. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  154. var sSourceID = ((JObject)data[OTB_OPM_OtherExhibitionTG.CN_QUOTE])[EasyNetGlobalConstWord.GUID].ToString();
  155. var TipsType = WebAppGlobalConstWord.CHECK;
  156. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  157. var oOpm = sdb.GetById(sId);
  158. if (oOpm == null)
  159. {
  160. sMsg = @"系統找不到對應的基本資料,請核查!";
  161. break;
  162. }
  163. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  164. var sTitle = member.MemberName + @"審核了您的「其他(駒驛)」活動名稱:" + oOpm.ImportBillName + @")的「報價單/預估成本審核」";
  165. //更新報價和預估成本
  166. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  167. {
  168. Quote = data[OTB_OPM_OtherExhibitionTG.CN_QUOTE].ToString(),
  169. EstimatedCost = data[OTB_OPM_OtherExhibitionTG.CN_ESTIMATEDCOST].ToString(),
  170. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  171. ModifyUser = i_crm.USERID,
  172. ModifyDate = DateTime.Now
  173. };
  174. var sAuditVal = ((JObject)data[OTB_OPM_OtherExhibitionTG.CN_QUOTE])[@"AuditVal"].ToString();
  175. if (sAuditVal == @"2")
  176. {
  177. sTitle += @",審核結果:通過";
  178. }
  179. else
  180. {
  181. sTitle += @",審核結果:不通過";
  182. TipsType = WebAppGlobalConstWord.FAIL;
  183. }
  184. if (i_crm.LANG == @"zh")
  185. {
  186. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  187. }
  188. db.Updateable(oOpmUpd)
  189. .UpdateColumns(it => new { it.Quote, it.EstimatedCost, it.Bills, it.ModifyUser, it.ModifyDate })
  190. .Where(it => it.Guid == sId).ExecuteCommand();
  191. //更新代辦
  192. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  193. //添加提醒消息
  194. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid, TipsType);
  195. db.Insertable(oTipsAdd).ExecuteCommand();
  196. if (sAuditVal != @"2")
  197. {
  198. //添加代辦
  199. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"OtherExhibitionTG_Qry", @"?Action=Upd&GoTab=3&Guid=" + oOpm.Guid);
  200. db.Insertable(oTaskAdd).ExecuteCommand();
  201. }
  202. rm = new SuccessResponseMessage(null, i_crm);
  203. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  204. } while (false);
  205. return rm;
  206. });
  207. }
  208. catch (Exception ex)
  209. {
  210. sMsg = Util.GetLastExceptionMsg(ex);
  211. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"AuditForQuote(其他(駒驛)報價和預估成本(主管)審核)", @"", @"", @"");
  212. }
  213. finally
  214. {
  215. if (null != sMsg)
  216. {
  217. rm = new ErrorResponseMessage(sMsg, i_crm);
  218. }
  219. Logger.Debug(@"OtherExhibitionTG_UpdService.AuditForQuote Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  220. }
  221. return rm;
  222. }
  223. #endregion 其他(駒驛)報價和預估成本(主管)審核
  224. #region 其他(駒驛)帳單提交審核
  225. /// <summary>
  226. /// 其他(駒驛)帳單提交審核
  227. /// </summary>
  228. /// <param name="i_crm">todo: describe i_crm parameter on ToAuditForBill</param>
  229. /// <returns></returns>
  230. public ResponseMessage ToAuditForBill(RequestMessage i_crm)
  231. {
  232. ResponseMessage rm = null;
  233. string sMsg = null;
  234. try
  235. {
  236. rm = SugarBase.ExecTran(db =>
  237. {
  238. do
  239. {
  240. var data = i_crm.DATA as Dictionary<string, object>;
  241. var oBill = (JObject)data[@"Bill"];
  242. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  243. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  244. var sBillNO = oBill[@"BillNO"].ToString();
  245. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  246. var oOpm = sdb.GetById(sId);
  247. if (oOpm == null)
  248. {
  249. sMsg = @"系統找不到對應的基本資料,請核查!";
  250. break;
  251. }
  252. var sTitle = @"「其他(駒驛)」" + oOpm.ImportBillName + @"帳單(" + sBillNO + @")" + @"審核";
  253. if (i_crm.LANG == @"zh")
  254. {
  255. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  256. }
  257. //更新帳單
  258. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  259. {
  260. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  261. Exhibitors = data[OTB_OPM_OtherExhibitionTG.CN_EXHIBITORS].ToString(),
  262. ModifyUser = i_crm.USERID,
  263. ModifyDate = DateTime.Now
  264. };
  265. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.Exhibitors, it.ModifyUser, it.ModifyDate })
  266. .Where(it => it.Guid == sId).ExecuteCommand();
  267. //更新代辦
  268. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  269. var saSupervisor = new List<string>();
  270. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  271. if (member.MemberID != null)
  272. {
  273. if (!string.IsNullOrEmpty(member.ImmediateSupervisor))
  274. {
  275. saSupervisor.Add(member.ImmediateSupervisor);
  276. }
  277. var oDept = db.Queryable<OTB_SYS_Departments>().Single(it => it.OrgID == i_crm.ORIGID && it.DepartmentID == member.DepartmentID);
  278. if (oDept != null)
  279. {
  280. saSupervisor.Add(oDept.ChiefOfDepartmentID);
  281. }
  282. }
  283. var sBillAuditor = Common.GetSystemSetting(db, i_crm.ORIGID, WebAppGlobalConstWord.BILLAUDITOR);
  284. if (sBillAuditor != @"")
  285. {
  286. var saBillAuditor = sBillAuditor.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  287. foreach (string auditor in saBillAuditor)
  288. {
  289. saSupervisor.Add(auditor);
  290. }
  291. }
  292. saSupervisor = saSupervisor.Distinct<string>().ToList();
  293. var listTips = new List<OTB_SYS_Tips>();
  294. var listTask = new List<OTB_SYS_Task>();
  295. if (saSupervisor.Count > 0)
  296. {
  297. foreach (string supervisor in saSupervisor)
  298. {
  299. //添加提醒消息
  300. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, supervisor, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  301. listTips.Add(oTipsAdd);
  302. //添加代辦
  303. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, supervisor, sTitle, @"OtherExhibitionTG_Qry", @"?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO);
  304. listTask.Add(oTaskAdd);
  305. }
  306. }
  307. else
  308. {
  309. sMsg = @"您沒有對應的直屬主管或部門主管,請核查!";
  310. break;
  311. }
  312. if (listTips.Count > 0)
  313. {
  314. db.Insertable(listTips).ExecuteCommand();
  315. }
  316. if (listTask.Count > 0)
  317. {
  318. db.Insertable(listTask).ExecuteCommand();
  319. }
  320. rm = new SuccessResponseMessage(null, i_crm);
  321. rm.DATA.Add(BLWording.REL, saSupervisor);
  322. } while (false);
  323. return rm;
  324. });
  325. }
  326. catch (Exception ex)
  327. {
  328. sMsg = Util.GetLastExceptionMsg(ex);
  329. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"ToAuditForBill(其他(駒驛)帳單提交審核)", @"", @"", @"");
  330. }
  331. finally
  332. {
  333. if (null != sMsg)
  334. {
  335. rm = new ErrorResponseMessage(sMsg, i_crm);
  336. }
  337. Logger.Debug(@"OtherExhibitionTG_UpdService.ToAuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  338. }
  339. return rm;
  340. }
  341. #endregion 其他(駒驛)帳單提交審核
  342. #region 其他(駒驛)帳單(主管)審核
  343. /// <summary>
  344. /// 其他(駒驛)帳單(主管)審核
  345. /// </summary>
  346. /// <param name="i_crm">todo: describe i_crm parameter on AuditForBill</param>
  347. /// <returns></returns>
  348. public ResponseMessage AuditForBill(RequestMessage i_crm)
  349. {
  350. ResponseMessage rm = null;
  351. string sMsg = null;
  352. try
  353. {
  354. rm = SugarBase.ExecTran(db =>
  355. {
  356. do
  357. {
  358. var data = i_crm.DATA as Dictionary<string, object>;
  359. var oBill = (JObject)data[@"Bill"];
  360. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  361. var sBillNO = oBill[@"BillNO"].ToString();
  362. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  363. var TipsType = WebAppGlobalConstWord.CHECK;
  364. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  365. var oOpm = sdb.GetById(sId);
  366. if (oOpm == null)
  367. {
  368. sMsg = @"系統找不到對應的基本資料,請核查!";
  369. break;
  370. }
  371. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  372. var sTitle = member.MemberName + @"審核了您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的「帳單(" + sBillNO + @")」";
  373. //更新報價和預估成本
  374. var sAuditVal = oBill[@"AuditVal"].ToString();
  375. if (sAuditVal == @"2")
  376. {
  377. sTitle += @",審核結果:通過";
  378. }
  379. else
  380. {
  381. sTitle += @",審核結果:不通過";
  382. TipsType = WebAppGlobalConstWord.FAIL;
  383. }
  384. if (i_crm.LANG == @"zh")
  385. {
  386. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  387. }
  388. var oPayer = new OTB_CRM_Customers();
  389. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  390. {
  391. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  392. if (oPayer == null)
  393. {
  394. sMsg = @"系統找不到付款人資訊";
  395. break;
  396. }
  397. }
  398. //更新帳單
  399. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  400. {
  401. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  402. ModifyUser = i_crm.USERID,
  403. ModifyDate = DateTime.Now
  404. };
  405. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  406. .Where(it => it.Guid == sId).ExecuteCommand();
  407. //更新代辦
  408. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  409. //添加提醒消息
  410. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, TipsType);
  411. db.Insertable(oTipsAdd).ExecuteCommand();
  412. if (sAuditVal != @"2")
  413. {
  414. //添加代辦
  415. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"OtherExhibitionTG_Qry", @"?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO);
  416. db.Insertable(oTaskAdd).ExecuteCommand();
  417. }
  418. if (sAuditVal == @"2")
  419. {
  420. var oBillsAdd = new OTB_OPM_Bills
  421. {
  422. OrgID = i_crm.ORIGID,
  423. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  424. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  425. BillType = @"20",//帳別(收付)(3)
  426. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  427. };
  428. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  429. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  430. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  431. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  432. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  433. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  434. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  435. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  436. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  437. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  438. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  439. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  440. oBillsAdd.Settle = @"N"; //結清否(15)
  441. oBillsAdd.InvoiceStartNumber = @""; //發票號碼(起)(16)
  442. oBillsAdd.InvoiceEndNumber = @"";//發票號碼(迄)(17)
  443. oBillsAdd.Category = @"";//傳票類別(18)
  444. oBillsAdd.OrderNo = @"";//訂單單號(19)
  445. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  446. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  447. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  448. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  449. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  450. oBillsAdd.UpdateDate = @""; //更新日期(25)
  451. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  452. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  453. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  454. {
  455. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  456. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  457. }
  458. else
  459. {
  460. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  461. }
  462. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  463. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  464. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  465. var sCurrency = oBill[@"Currency"].ToString();
  466. if (i_crm.ORIGID == "SG")
  467. {
  468. if (sCurrency == "RMB")
  469. {
  470. sCurrency = "";
  471. }
  472. }
  473. else
  474. {
  475. if (sCurrency == "NTD")
  476. {
  477. sCurrency = "";
  478. }
  479. }
  480. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  481. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  482. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  483. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  484. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  485. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  486. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  487. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  488. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  489. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  490. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  491. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  492. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  493. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  494. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  495. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  496. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  497. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  498. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  499. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  500. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  501. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  502. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  503. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  504. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  505. oBillsAdd.CreateUser = i_crm.USERID;
  506. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  507. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  508. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  509. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  510. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  511. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  512. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO; //Url
  513. db.Insertable(oBillsAdd).ExecuteCommand();
  514. }
  515. var oLogInfo = new OTB_SYS_LogInfo
  516. {
  517. OrgID = i_crm.ORIGID,
  518. SouseId = sBillNO,
  519. LogType = "billnoupdate",
  520. LogInfo = data[@"Bill"].ToString(),
  521. CreateUser = i_crm.USERID,
  522. CreateDate = DateTime.Now,
  523. Memo = "「其他(駒驛)」(主管)審核"
  524. };
  525. db.Insertable(oLogInfo).ExecuteCommand();
  526. rm = new SuccessResponseMessage(null, i_crm);
  527. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  528. } while (false);
  529. return rm;
  530. });
  531. }
  532. catch (Exception ex)
  533. {
  534. sMsg = Util.GetLastExceptionMsg(ex);
  535. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"AuditForBill(其他(駒驛)帳單(主管)審核)", @"", @"", @"");
  536. }
  537. finally
  538. {
  539. if (null != sMsg)
  540. {
  541. rm = new ErrorResponseMessage(sMsg, i_crm);
  542. }
  543. Logger.Debug(@"OtherExhibitionTG_UpdService.AuditForBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  544. }
  545. return rm;
  546. }
  547. #endregion 其他(駒驛)帳單(主管)審核
  548. #region (會計)取消審核
  549. /// <summary>
  550. /// (會計)取消審核
  551. /// </summary>
  552. /// <param name="i_crm">todo: describe i_crm parameter on CancelAudit</param>
  553. /// <returns></returns>
  554. public ResponseMessage CancelAudit(RequestMessage i_crm)
  555. {
  556. ResponseMessage rm = null;
  557. string sMsg = null;
  558. try
  559. {
  560. rm = SugarBase.ExecTran(db =>
  561. {
  562. do
  563. {
  564. var data = i_crm.DATA as Dictionary<string, object>;
  565. var oBill = (JObject)data[@"Bill"];
  566. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  567. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  568. var sBillNO = oBill[@"BillNO"].ToString();
  569. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  570. var oOpm = sdb.GetById(sId);
  571. if (oOpm == null)
  572. {
  573. sMsg = @"系統找不到對應的基本資料,請核查!";
  574. break;
  575. }
  576. var LogData = _fetchString(i_crm, @"LogData");
  577. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  578. if (!LogResult.Item1)
  579. {
  580. sMsg = LogResult.Item2;
  581. break;
  582. }
  583. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  584. var sTitle = @"會計(" + member.MemberName + @")取消審核了您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")";
  585. if (i_crm.LANG == @"zh")
  586. {
  587. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  588. }
  589. //更新帳單
  590. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  591. {
  592. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  593. ModifyUser = i_crm.USERID,
  594. ModifyDate = DateTime.Now
  595. };
  596. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  597. .Where(it => it.Guid == sId).ExecuteCommand();
  598. //更新代辦
  599. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sSourceID);
  600. //添加提醒消息
  601. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  602. db.Insertable(oTipsAdd).ExecuteCommand();
  603. //添加代辦
  604. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, sSourceID, oOpm.ResponsiblePerson, sTitle, @"OtherExhibitionTG_Qry", @"?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO);
  605. db.Insertable(oTaskAdd).ExecuteCommand();
  606. rm = new SuccessResponseMessage(null, i_crm);
  607. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  608. } while (false);
  609. return rm;
  610. });
  611. }
  612. catch (Exception ex)
  613. {
  614. sMsg = Util.GetLastExceptionMsg(ex);
  615. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"CancelAudit((會計)取消審核)", @"", @"", @"");
  616. }
  617. finally
  618. {
  619. if (null != sMsg)
  620. {
  621. rm = new ErrorResponseMessage(sMsg, i_crm);
  622. }
  623. Logger.Debug(@"OtherExhibitionTG_UpdService.CancelAudit Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  624. }
  625. return rm;
  626. }
  627. #endregion (會計)取消審核
  628. #region (會計)銷帳
  629. /// <summary>
  630. /// (會計)銷帳
  631. /// </summary>
  632. /// <param name="i_crm">todo: describe i_crm parameter on WriteOff</param>
  633. /// <returns></returns>
  634. public ResponseMessage WriteOff(RequestMessage i_crm)
  635. {
  636. ResponseMessage rm = null;
  637. string sMsg = null;
  638. try
  639. {
  640. rm = SugarBase.ExecTran(db =>
  641. {
  642. do
  643. {
  644. var data = i_crm.DATA as Dictionary<string, object>;
  645. var oBill = (JObject)data[@"Bill"];
  646. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  647. var sBillNO = oBill[@"BillNO"].ToString();
  648. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  649. var oOpm = sdb.GetById(sId);
  650. if (oOpm == null)
  651. {
  652. sMsg = @"系統找不到對應的基本資料,請核查!";
  653. break;
  654. }
  655. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  656. var sTitle = @"會計(" + member.MemberName + @")對您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")進行了銷帳";
  657. if (i_crm.LANG == @"zh")
  658. {
  659. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  660. }
  661. //更新帳單
  662. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  663. {
  664. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  665. ModifyUser = i_crm.USERID,
  666. ModifyDate = DateTime.Now
  667. };
  668. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  669. .Where(it => it.Guid == sId).ExecuteCommand();
  670. //添加提醒消息
  671. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  672. db.Insertable(oTipsAdd).ExecuteCommand();
  673. rm = new SuccessResponseMessage(null, i_crm);
  674. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  675. } while (false);
  676. return rm;
  677. });
  678. }
  679. catch (Exception ex)
  680. {
  681. sMsg = Util.GetLastExceptionMsg(ex);
  682. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"WriteOff((會計)銷帳)", @"", @"", @"");
  683. }
  684. finally
  685. {
  686. if (null != sMsg)
  687. {
  688. rm = new ErrorResponseMessage(sMsg, i_crm);
  689. }
  690. Logger.Debug(@"OtherExhibitionTG_UpdService.WriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  691. }
  692. return rm;
  693. }
  694. #endregion (會計)銷帳
  695. #region (會計)取消銷帳
  696. /// <summary>
  697. /// (會計)取消銷帳
  698. /// </summary>
  699. /// <param name="i_crm">todo: describe i_crm parameter on CancelWriteOff</param>
  700. /// <returns></returns>
  701. public ResponseMessage CancelWriteOff(RequestMessage i_crm)
  702. {
  703. ResponseMessage rm = null;
  704. string sMsg = null;
  705. var db = SugarBase.GetIntance();
  706. try
  707. {
  708. do
  709. {
  710. var data = i_crm.DATA as Dictionary<string, object>;
  711. var oBill = (JObject)data[@"Bill"];
  712. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  713. var sBillNO = oBill[@"BillNO"].ToString();
  714. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  715. var oOpm = sdb.GetById(sId);
  716. if (oOpm == null)
  717. {
  718. sMsg = @"系統找不到對應的基本資料,請核查!";
  719. break;
  720. }
  721. var LogData = _fetchString(i_crm, @"LogData");
  722. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  723. if (!LogResult.Item1)
  724. {
  725. sMsg = LogResult.Item2;
  726. break;
  727. }
  728. //更新帳單
  729. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  730. {
  731. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  732. ModifyUser = i_crm.USERID,
  733. ModifyDate = DateTime.Now
  734. };
  735. var iResult = db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  736. .Where(it => it.Guid == sId).ExecuteCommand();
  737. if (iResult > 0)
  738. {
  739. rm = new SuccessResponseMessage(null, i_crm);
  740. }
  741. } while (false);
  742. }
  743. catch (Exception ex)
  744. {
  745. sMsg = Util.GetLastExceptionMsg(ex);
  746. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"CancelWriteOff((會計)取消銷帳)", @"", @"", @"");
  747. }
  748. finally
  749. {
  750. if (null != sMsg)
  751. {
  752. rm = new ErrorResponseMessage(sMsg, i_crm);
  753. }
  754. Logger.Debug(@"OtherExhibitionTG_UpdService.CancelWriteOff Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  755. }
  756. return rm;
  757. }
  758. #endregion (會計)取消銷帳
  759. #region 過帳
  760. /// <summary>
  761. /// 過帳
  762. /// </summary>
  763. /// <param name="i_crm">todo: describe i_crm parameter on BillPost</param>
  764. /// <returns></returns>
  765. public ResponseMessage BillPost(RequestMessage i_crm)
  766. {
  767. ResponseMessage rm = null;
  768. string sMsg = null;
  769. try
  770. {
  771. rm = SugarBase.ExecTran(db =>
  772. {
  773. do
  774. {
  775. var data = i_crm.DATA as Dictionary<string, object>;
  776. var oBill = (JObject)data[@"Bill"];
  777. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  778. var sBillNO = oBill[@"BillNO"].ToString();
  779. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  780. var oOpm = sdb.GetById(sId);
  781. if (oOpm == null)
  782. {
  783. sMsg = @"系統找不到對應的基本資料,請核查!";
  784. break;
  785. }
  786. var oPayer = new OTB_CRM_Customers();
  787. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  788. {
  789. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  790. if (oPayer == null)
  791. {
  792. sMsg = @"系統找不到付款人資訊";
  793. break;
  794. }
  795. }
  796. var sTitle = @"「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")已過帳";
  797. if (i_crm.LANG == @"zh")
  798. {
  799. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  800. }
  801. //更新帳單
  802. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  803. {
  804. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  805. ModifyUser = i_crm.USERID,
  806. ModifyDate = DateTime.Now
  807. };
  808. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  809. .Where(it => it.Guid == sId).ExecuteCommand();
  810. var listAccounts = db.Queryable<OTB_SYS_MembersToRule>()
  811. .Where(it => it.OrgID == i_crm.ORIGID && it.RuleID == @"Account").ToList();
  812. var listTips = new List<OTB_SYS_Tips>();
  813. if (listAccounts.Count > 0)
  814. {
  815. foreach (OTB_SYS_MembersToRule account in listAccounts)
  816. {
  817. //添加提醒消息
  818. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, account.MemberID, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  819. listTips.Add(oTipsAdd);
  820. }
  821. }
  822. var oBillsAdd = new OTB_OPM_Bills
  823. {
  824. OrgID = i_crm.ORIGID,
  825. BillNO = oBill[@"BillNO"].ToString(), //帳款號碼(1)
  826. CheckDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]),//對帳日期(2)
  827. BillType = @"20",//帳別(收付)(3)
  828. CustomerCode = oPayer.CustomerNO //客戶供應商代號(4)
  829. };
  830. var sResponsiblePerson = oOpm.ResponsiblePerson.Split('.')[0];
  831. oBillsAdd.ResponsiblePersonCode = Common.CutByteString(sResponsiblePerson, 11); //業務員代號(5)
  832. oBillsAdd.ResponsiblePersonFullCode = oOpm.ResponsiblePerson; //業務員全代號
  833. oBillsAdd.LastGetBillDate = @""; //最近收付日(6)
  834. oBillsAdd.LastGetBillNO = @""; //最近收付單號(7)
  835. oBillsAdd.TaxType = decimal.Parse(oBill[@"TaxSum"].ToString().Replace(@",", @"")) > 0 ? @"5" : @"6";//稅別(8)
  836. oBillsAdd.NOTaxAmount = oBill[@"AmountSum"].ToString(); //未稅金額(9)
  837. oBillsAdd.BillAmount = oBill[@"AmountTaxSum"].ToString(); //帳款金額(10)
  838. oBillsAdd.PaymentAmount = @"0"; //收付金額(11)
  839. oBillsAdd.Allowance = @"0"; //折讓金額(12)
  840. oBillsAdd.DebtAmount = @"0"; //呆帳金額(13)
  841. oBillsAdd.ExchangeAmount = @"0"; //匯兌損益金額(14)
  842. oBillsAdd.Settle = @"N"; //結清否(15)
  843. oBillsAdd.InvoiceStartNumber = oBill[@"InvoiceNumber"].ToString(); //發票號碼(起)(16)
  844. oBillsAdd.InvoiceEndNumber = oBill[@"InvoiceNumber"].ToString();//發票號碼(迄)(17)
  845. oBillsAdd.Category = @"";//傳票類別(18)
  846. oBillsAdd.OrderNo = @"";//訂單單號(19)
  847. oBillsAdd.ClosingNote = @"N"; //結帳註記(20)
  848. oBillsAdd.GeneralInvoiceNumber = @""; //立帳總帳傳票號碼(21)
  849. oBillsAdd.GeneralSerialNumber = @""; //立帳總帳傳票序號(22)
  850. oBillsAdd.Remark1 = @""; //備註一(30C)(23)
  851. oBillsAdd.AccountSource = @"0"; //帳款來源(24)
  852. oBillsAdd.UpdateDate = @""; //更新日期(25)
  853. oBillsAdd.UpdatePersonnel = @""; //更新人員(26)
  854. oBillsAdd.DepartmentSiteNumber = oOpm.DepartmentID; //部門\ 工地編號(27)
  855. if (!string.IsNullOrWhiteSpace(oOpm.ExhibitionNO))
  856. {
  857. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  858. oBillsAdd.ProjectNumber = oExhibition == null ? @"" : oExhibition.ExhibitionCode; //專案\ 項目編號(28)
  859. }
  860. else
  861. {
  862. oBillsAdd.ProjectNumber = @""; //專案\ 項目編號(28)
  863. }
  864. oBillsAdd.TransferBNotes = @""; //轉B 帳註記(29)
  865. oBillsAdd.ABNumber = @""; //A|B 帳唯一流水號(30)
  866. oBillsAdd.EnterNumber = @""; //進銷單號(31)
  867. var sCurrency = oBill[@"Currency"].ToString();
  868. if (i_crm.ORIGID == "SG")
  869. {
  870. if (sCurrency == "RMB")
  871. {
  872. sCurrency = "";
  873. }
  874. }
  875. else
  876. {
  877. if (sCurrency == "NTD")
  878. {
  879. sCurrency = "";
  880. }
  881. }
  882. oBillsAdd.ForeignCurrencyCode = sCurrency; //外幣代號(32)
  883. var sExchangeRate = oBill[@"ExchangeRate"].ToString();
  884. oBillsAdd.ExchangeRate = sExchangeRate; //匯率(33)
  885. oBillsAdd.ForeignAmount = (decimal.Parse(oBill[@"AmountTaxSum"].ToString()) * decimal.Parse(sExchangeRate == @"" ? @"1" : sExchangeRate)).ToString(); //外幣金額(34)
  886. oBillsAdd.PayAmount = @"0"; //收付沖抵金額(35)
  887. oBillsAdd.RefundAmount = @"0"; //退款金額(36)
  888. oBillsAdd.PaymentTerms = @""; //收付條件(37)
  889. oBillsAdd.AccountDate = Common.FnToTWDate(oBill[@"BillFirstCheckDate"]); //帳款日期(38)
  890. oBillsAdd.DCreditCardNumber = @""; //預設信用卡號(39)
  891. oBillsAdd.ClosingDate = @""; //結帳日期(40)
  892. oBillsAdd.CusField1 = @""; //自定義欄位一(41)
  893. oBillsAdd.CusField2 = @""; //自定義欄位二(42)
  894. oBillsAdd.CusField3 = @""; //自定義欄位三(43)
  895. oBillsAdd.CusField4 = @""; //自定義欄位四(44)
  896. oBillsAdd.CusField5 = @""; //自定義欄位五(45)
  897. oBillsAdd.CusField6 = @"0"; //自定義欄位六(46)
  898. oBillsAdd.CusField7 = @"0"; //自定義欄位七(47)
  899. oBillsAdd.CusField8 = @"0"; //自定義欄位八(48)
  900. oBillsAdd.CusField9 = @"0"; //自定義欄位九(49)
  901. oBillsAdd.CusField10 = @"0"; //自定義欄位十(50)
  902. oBillsAdd.CusField11 = @""; //自定義欄位十一(51)
  903. oBillsAdd.CusField12 = @""; //自定義欄位十二(52)
  904. oBillsAdd.Remark2 = @""; //備註二(M)(53)
  905. oBillsAdd.TWNOTaxAmount = oBill[@"AmountSum"].ToString(); //台幣未稅金額(54)
  906. oBillsAdd.TWAmount = oBill[@"AmountTaxSum"].ToString(); //台幣帳款金額(55)
  907. oBillsAdd.CreateUser = i_crm.USERID;
  908. oBillsAdd.CreateDate = oBill[@"CreateDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"CreateDate"].ToString();
  909. oBillsAdd.BillFirstCheckDate = oBill[@"BillFirstCheckDate"] == null ? DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss") : oBill[@"BillFirstCheckDate"].ToString();
  910. oBillsAdd.Advance = oBill[@"Advance"].ToString(); //預收
  911. oBillsAdd.TaxSum = oBill[@"TaxSum"].ToString(); //稅額
  912. oBillsAdd.TotalReceivable = oBill[@"TotalReceivable"].ToString(); //總應收
  913. oBillsAdd.IsRetn = oBill[@"IsRetn"] == null ? @"N" : oBill[@"IsRetn"].ToString(); //是否為退運
  914. oBillsAdd.Url = i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO; //Url
  915. db.Insertable(oBillsAdd).ExecuteCommand();
  916. var oLogInfo = new OTB_SYS_LogInfo
  917. {
  918. OrgID = i_crm.ORIGID,
  919. SouseId = sBillNO,
  920. LogType = "billnoupdate",
  921. LogInfo = data[@"Bill"].ToString(),
  922. CreateUser = i_crm.USERID,
  923. CreateDate = DateTime.Now,
  924. Memo = "「其他(駒驛)」(會計)過帳"
  925. };
  926. db.Insertable(oLogInfo).ExecuteCommand();
  927. rm = new SuccessResponseMessage(null, i_crm);
  928. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  929. } while (false);
  930. return rm;
  931. });
  932. }
  933. catch (Exception ex)
  934. {
  935. sMsg = Util.GetLastExceptionMsg(ex);
  936. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"BillPost(過帳)", @"", @"", @"");
  937. }
  938. finally
  939. {
  940. if (null != sMsg)
  941. {
  942. rm = new ErrorResponseMessage(sMsg, i_crm);
  943. }
  944. Logger.Debug(@"OtherExhibitionTG_UpdService.BillPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  945. }
  946. return rm;
  947. }
  948. #endregion 過帳
  949. #region 取消過帳
  950. /// <summary>
  951. /// 取消過帳
  952. /// </summary>
  953. /// <param name="i_crm">todo: describe i_crm parameter on BillCancelPost</param>
  954. /// <returns></returns>
  955. public ResponseMessage BillCancelPost(RequestMessage i_crm)
  956. {
  957. ResponseMessage rm = null;
  958. string sMsg = null;
  959. try
  960. {
  961. rm = SugarBase.ExecTran(db =>
  962. {
  963. do
  964. {
  965. var data = i_crm.DATA as Dictionary<string, object>;
  966. var oBill = (JObject)data[@"Bill"];
  967. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  968. var sBillNO = oBill[@"BillNO"].ToString();
  969. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  970. var oOpm = sdb.GetById(sId);
  971. if (oOpm == null)
  972. {
  973. sMsg = @"系統找不到對應的基本資料,請核查!";
  974. break;
  975. }
  976. var LogData = _fetchString(i_crm, @"LogData");
  977. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  978. if (!LogResult.Item1)
  979. {
  980. sMsg = LogResult.Item2;
  981. break;
  982. }
  983. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  984. var sTitle = @"會計(" + member.MemberName + @")對您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")取消了過帳";
  985. if (i_crm.LANG == @"zh")
  986. {
  987. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  988. }
  989. //更新帳單
  990. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  991. {
  992. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  993. ModifyUser = i_crm.USERID,
  994. ModifyDate = DateTime.Now
  995. };
  996. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  997. .Where(it => it.Guid == sId).ExecuteCommand();
  998. //添加提醒消息
  999. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=3&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1000. db.Insertable(oTipsAdd).ExecuteCommand();
  1001. rm = new SuccessResponseMessage(null, i_crm);
  1002. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1003. } while (false);
  1004. return rm;
  1005. });
  1006. }
  1007. catch (Exception ex)
  1008. {
  1009. sMsg = Util.GetLastExceptionMsg(ex);
  1010. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"BillCancelPost(取消過帳)", @"", @"", @"");
  1011. }
  1012. finally
  1013. {
  1014. if (null != sMsg)
  1015. {
  1016. rm = new ErrorResponseMessage(sMsg, i_crm);
  1017. }
  1018. Logger.Debug(@"OtherExhibitionTG_UpdService.BillCancelPost Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1019. }
  1020. return rm;
  1021. }
  1022. #endregion 取消過帳
  1023. #region 作廢帳單
  1024. /// <summary>
  1025. /// 作廢帳單
  1026. /// </summary>
  1027. /// <param name="i_crm">todo: describe i_crm parameter on BillVoid</param>
  1028. /// <returns></returns>
  1029. public ResponseMessage BillVoid(RequestMessage i_crm)
  1030. {
  1031. ResponseMessage rm = null;
  1032. string sMsg = null;
  1033. try
  1034. {
  1035. rm = SugarBase.ExecTran(db =>
  1036. {
  1037. do
  1038. {
  1039. var data = i_crm.DATA as Dictionary<string, object>;
  1040. var oBill = (JObject)data[@"Bill"];
  1041. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  1042. var sBillNO = oBill[@"BillNO"].ToString();
  1043. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  1044. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  1045. var oOpm = sdb.GetById(sId);
  1046. if (oOpm == null)
  1047. {
  1048. sMsg = @"系統找不到對應的基本資料,請核查!";
  1049. break;
  1050. }
  1051. var LogData = _fetchString(i_crm, @"LogData");
  1052. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  1053. if (!LogResult.Item1)
  1054. {
  1055. sMsg = LogResult.Item2;
  1056. break;
  1057. }
  1058. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1059. var sTitle = @"會計(" + member.MemberName + @")作廢了您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")";
  1060. if (i_crm.LANG == @"zh")
  1061. {
  1062. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  1063. }
  1064. //更新帳單
  1065. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  1066. {
  1067. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  1068. ModifyUser = i_crm.USERID,
  1069. ModifyDate = DateTime.Now
  1070. };
  1071. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  1072. .Where(it => it.Guid == sId).ExecuteCommand();
  1073. //添加提醒消息
  1074. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=2&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1075. db.Insertable(oTipsAdd).ExecuteCommand();
  1076. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  1077. rm = new SuccessResponseMessage(null, i_crm);
  1078. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1079. } while (false);
  1080. return rm;
  1081. });
  1082. }
  1083. catch (Exception ex)
  1084. {
  1085. sMsg = Util.GetLastExceptionMsg(ex);
  1086. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"BillVoid(作廢帳單)", @"", @"", @"");
  1087. }
  1088. finally
  1089. {
  1090. if (null != sMsg)
  1091. {
  1092. rm = new ErrorResponseMessage(sMsg, i_crm);
  1093. }
  1094. Logger.Debug(@"OtherExhibitionTG_UpdService.BillVoid Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1095. }
  1096. return rm;
  1097. }
  1098. #endregion 作廢帳單
  1099. #region 刪除帳單
  1100. /// <summary>
  1101. /// 刪除帳單
  1102. /// </summary>
  1103. /// <param name="i_crm">todo: describe i_crm parameter on BillDelete</param>
  1104. /// <returns></returns>
  1105. public ResponseMessage BillDelete(RequestMessage i_crm)
  1106. {
  1107. ResponseMessage rm = null;
  1108. string sMsg = null;
  1109. try
  1110. {
  1111. rm = SugarBase.ExecTran(db =>
  1112. {
  1113. do
  1114. {
  1115. var data = i_crm.DATA as Dictionary<string, object>;
  1116. var oBill = (JObject)data[@"Bill"];
  1117. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  1118. var sBillNO = oBill[@"BillNO"].ToString();
  1119. var sSourceID = oBill[EasyNetGlobalConstWord.GUID].ToString();
  1120. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  1121. var oOpm = sdb.GetById(sId);
  1122. if (oOpm == null)
  1123. {
  1124. sMsg = @"系統找不到對應的基本資料,請核查!";
  1125. break;
  1126. }
  1127. var LogData = _fetchString(i_crm, @"LogData");
  1128. var LogResult = BillLogs.InsertBillChangeLog(db, LogData, ActionType., i_crm.ORIGID, i_crm.USERID);
  1129. if (!LogResult.Item1)
  1130. {
  1131. sMsg = LogResult.Item2;
  1132. break;
  1133. }
  1134. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1135. var sTitle = @"管理員(" + member.MemberName + @")刪除了您的「其他(駒驛)」(活動名稱:" + oOpm.ImportBillName + @")的帳單(" + sBillNO + @")";
  1136. if (i_crm.LANG == @"zh")
  1137. {
  1138. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  1139. }
  1140. //更新帳單
  1141. var oOpmUpd = new OTB_OPM_OtherExhibitionTG
  1142. {
  1143. Bills = data[OTB_OPM_OtherExhibitionTG.CN_BILLS].ToString(),
  1144. ModifyUser = i_crm.USERID,
  1145. ModifyDate = DateTime.Now
  1146. };
  1147. db.Updateable(oOpmUpd).UpdateColumns(it => new { it.Bills, it.ModifyUser, it.ModifyDate })
  1148. .Where(it => it.Guid == sId).ExecuteCommand();
  1149. //添加提醒消息
  1150. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, oOpm.ResponsiblePerson, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&GoTab=2&Guid=" + oOpm.Guid + @"&BillNO=" + sBillNO, WebAppGlobalConstWord.BELL);
  1151. db.Insertable(oTipsAdd).ExecuteCommand();
  1152. db.Deleteable<OTB_SYS_Task>().Where(it => it.OrgID == i_crm.ORIGID && it.SourceID == sSourceID).ExecuteCommand();
  1153. rm = new SuccessResponseMessage(null, i_crm);
  1154. rm.DATA.Add(BLWording.REL, oOpm.ResponsiblePerson);
  1155. } while (false);
  1156. return rm;
  1157. });
  1158. }
  1159. catch (Exception ex)
  1160. {
  1161. sMsg = Util.GetLastExceptionMsg(ex);
  1162. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"BillDelete(刪除帳單)", @"", @"", @"");
  1163. }
  1164. finally
  1165. {
  1166. if (null != sMsg)
  1167. {
  1168. rm = new ErrorResponseMessage(sMsg, i_crm);
  1169. }
  1170. Logger.Debug(@"OtherExhibitionTG_UpdService.BillDelete Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1171. }
  1172. return rm;
  1173. }
  1174. #endregion 刪除帳單
  1175. #region 其他(駒驛)帳單列印下載
  1176. /// <summary>
  1177. /// 其他(駒驛)帳單列印下載
  1178. /// </summary>
  1179. /// <param name="i_crm">todo: describe i_crm parameter on PrintBill</param>
  1180. /// <returns></returns>
  1181. public ResponseMessage PrintBill(RequestMessage i_crm)
  1182. {
  1183. ResponseMessage rm = null;
  1184. string sMsg = null;
  1185. var db = SugarBase.GetIntance();
  1186. var sOutPut = Common.ConfigGetValue(@"", @"OutFilesPath");
  1187. try
  1188. {
  1189. do
  1190. {
  1191. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  1192. var sTemplID = _fetchString(i_crm, OTB_SYS_OfficeTemplate.CN_TEMPLID);
  1193. var sBill = _fetchString(i_crm, @"Bill");
  1194. var sAction = _fetchString(i_crm, @"Action");
  1195. var sPayDateText = _fetchString(i_crm, @"PayDateText");
  1196. var bTW = sTemplID.EndsWith(@"TW");
  1197. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  1198. var oOpm = sdb.GetById(sId);
  1199. if (oOpm == null)
  1200. {
  1201. sMsg = @"系統找不到對應的基本資料,請核查!";
  1202. break;
  1203. }
  1204. var oTempl = db.Queryable<OTB_SYS_OfficeTemplate>().Single(it => it.OrgID == i_crm.ORIGID && it.TemplID == sTemplID);
  1205. if (oTempl == null)
  1206. {
  1207. sMsg = @"請檢查模版設定";
  1208. break;
  1209. }
  1210. var oFile = db.Queryable<OTB_SYS_Files>()
  1211. .Single(it => it.OrgID == i_crm.ORIGID && it.ParentID == oTempl.FileID);
  1212. if (oFile == null)
  1213. {
  1214. sMsg = @"系統找不到對應的帳單模版";
  1215. break;
  1216. }
  1217. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1218. string sCurrency = oBill[@"Currency"] != null ? oBill[@"Currency"].ToString() : string.Empty;
  1219. string sPayer = oBill[@"Payer"] != null ? oBill[@"Payer"].ToString() : string.Empty;
  1220. string sBillNO = oBill[@"BillNO"] != null ? oBill[@"BillNO"].ToString() : string.Empty;
  1221. string sAuditVal = oBill[@"AuditVal"] != null ? oBill[@"AuditVal"].ToString() : string.Empty;
  1222. string sFeeItems = oBill[@"FeeItems"] != null ? oBill[@"FeeItems"].ToString() : string.Empty;
  1223. var bTWCurrency = sCurrency == @"NTD";
  1224. var oPayer = new OTB_CRM_Customers();
  1225. if (!string.IsNullOrEmpty(sPayer))
  1226. {
  1227. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == sPayer);
  1228. if (oPayer == null)
  1229. {
  1230. sMsg = @"系統找不到付款人資訊";
  1231. break;
  1232. }
  1233. }
  1234. var sTempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, oFile.FilePath);//Word模版路徑
  1235. var sBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"");
  1236. sOutPut = sBase + sOutPut;
  1237. Common.FnCreateDir(sOutPut);//如果不存在就創建文件夾
  1238. var sDocxName = @"Invoice-" + sBillNO;
  1239. //建立臨時文件
  1240. var sTempFile = Path.GetTempPath() + sDocxName + @".docx";
  1241. sOutPut += sDocxName + @".docx";
  1242. if (File.Exists(sTempFile))
  1243. {
  1244. File.Delete(sTempFile);
  1245. }
  1246. File.Copy(sTempPath, sTempFile);
  1247. var JTmpKeys = (JArray)JsonConvert.DeserializeObject(oTempl.TemplKeys);
  1248. double iPARM17 = 0;
  1249. var WatermarkInfo = Common.GetWatermarkInfo(sAuditVal, sTemplID, i_crm.ORIGID);
  1250. using (WordprocessingDocument doc = WordprocessingDocument.Open(sTempFile, true))//顯示Word
  1251. {
  1252. var body = doc.MainDocumentPart.Document.Body;
  1253. var jaFeeItems = (JArray)JsonConvert.DeserializeObject(sFeeItems);
  1254. //查找:獲取第三個表格
  1255. var table = body.Elements<Table>().ElementAt(2);
  1256. if (jaFeeItems.Count > 0)
  1257. {
  1258. var trow_Amount = table.Elements<TableRow>().ElementAt(4).Clone() as TableRow;//小計行
  1259. var trow_Tax = table.Elements<TableRow>().ElementAt(5).Clone() as TableRow;//營業稅
  1260. var trow_Total = table.Elements<TableRow>().ElementAt(7).Clone() as TableRow;//總應收
  1261. var trow_PrePay = table.Elements<TableRow>().ElementAt(8).Clone() as TableRow;//預收
  1262. var jaFeeItems_Tax = new JArray();
  1263. var jaFeeItems_NoTax = new JArray();
  1264. var iDefultRow = 10;
  1265. var iInsertRow = 1;
  1266. var iFeeItemsRow = 1;
  1267. foreach (JObject jo in jaFeeItems)
  1268. {
  1269. string sFinancialTaxRate = jo[@"FinancialTaxRate"] != null ? jo[@"FinancialTaxRate"].ToString() : string.Empty;
  1270. if (sFinancialTaxRate.Replace(@"%", @"").Trim() == @"0")
  1271. {
  1272. jaFeeItems_NoTax.Add(jo);
  1273. }
  1274. else
  1275. {
  1276. jaFeeItems_Tax.Add(jo);
  1277. }
  1278. }
  1279. if (jaFeeItems_Tax.Count > 0)
  1280. {
  1281. var jaFeeItems_TaxSorted = new JArray(jaFeeItems_Tax.OrderBy(p => p[@"OrderBy"]));
  1282. foreach (JObject jo in jaFeeItems_TaxSorted)
  1283. {
  1284. string sFinancialTWAmount = jo[@"FinancialTWAmount"] != null ? jo[@"FinancialTWAmount"].ToString() : string.Empty;
  1285. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1286. var iCellCount = trow_Item.Elements<TableCell>().Count();
  1287. if (iCellCount == 9)
  1288. {
  1289. ExhibitionHelper.RenderFeeItemsTW(trow_Item, jo, iCellCount, iFeeItemsRow);
  1290. }
  1291. else
  1292. {
  1293. ExhibitionHelper.RenderFeeItemsFR(trow_Item, jo, iCellCount, iFeeItemsRow);
  1294. }
  1295. table.InsertAt<TableRow>(trow_Item, iInsertRow + iDefultRow); //插入一行費用
  1296. iInsertRow++;
  1297. iFeeItemsRow++;
  1298. iPARM17 += double.Parse(sFinancialTWAmount);
  1299. }
  1300. table.InsertAt<TableRow>(trow_Amount, iInsertRow + iDefultRow);//插入一行小計行
  1301. iInsertRow++;
  1302. //營業稅
  1303. table.InsertAt<TableRow>(trow_Tax, iInsertRow + iDefultRow);//插入一行營業稅
  1304. iInsertRow++;
  1305. }
  1306. if (jaFeeItems_NoTax.Count > 0)
  1307. {
  1308. var jaFeeItems_NoTaxSorted = new JArray(jaFeeItems_NoTax.OrderBy(p => p[@"OrderBy"]));
  1309. var iCellCount = 0;
  1310. foreach (JObject jo in jaFeeItems_NoTaxSorted)
  1311. {
  1312. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1313. iCellCount = trow_Item.Elements<TableCell>().Count();
  1314. if (iCellCount == 9)
  1315. {
  1316. ExhibitionHelper.RenderFeeItemsTW(trow_Item, jo, iCellCount, iFeeItemsRow);
  1317. }
  1318. else
  1319. {
  1320. ExhibitionHelper.RenderFeeItemsFR(trow_Item, jo, iCellCount, iFeeItemsRow);
  1321. }
  1322. //插入一行
  1323. table.InsertAt<TableRow>(trow_Item, iInsertRow + iDefultRow);
  1324. iInsertRow++;
  1325. iFeeItemsRow++;
  1326. }
  1327. if (iCellCount == 9)
  1328. {
  1329. table.InsertAt<TableRow>(trow_Total, iInsertRow + iDefultRow);//插入一行總應收
  1330. iInsertRow++;
  1331. table.InsertAt<TableRow>(trow_PrePay, iInsertRow + iDefultRow);//插入一行預收
  1332. }
  1333. }
  1334. //刪除模板行
  1335. for (int i = 0; i < 8; i++)
  1336. {
  1337. table.Elements<TableRow>().ElementAt(1).Remove();
  1338. }
  1339. }
  1340. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  1341. if (oExhibition == null)
  1342. {
  1343. oExhibition = new OTB_OPM_Exhibition();
  1344. }
  1345. string sTaxSum = oBill["TaxSum"] != null ? oBill["TaxSum"].ToString() : string.Empty;
  1346. string sAmountTaxSum = oBill["AmountTaxSum"] != null ? oBill["AmountTaxSum"].ToString() : string.Empty;
  1347. string sAdvance = oBill["Advance"] != null ? oBill["Advance"].ToString() : string.Empty;
  1348. string sTotalReceivable = oBill["TotalReceivable"] != null ? oBill["TotalReceivable"].ToString() : string.Empty;
  1349. string sWeight = oBill["Weight"] != null ? oBill["Weight"].ToString() : string.Empty;
  1350. string sVolume = oBill["Volume"] != null ? oBill["Volume"].ToString() : string.Empty;
  1351. string sNumber = oBill["Number"] != null ? oBill["Number"].ToString() : string.Empty;
  1352. string sUnit = oBill["Unit"] != null ? oBill["Unit"].ToString() : string.Empty;
  1353. string sContactorName = oBill["ContactorName"] != null ? oBill["ContactorName"].ToString() : string.Empty;
  1354. string sTelephone = oBill["Telephone"] != null ? oBill["Telephone"].ToString() : string.Empty;
  1355. string sMemo = oBill["Memo"] != null ? oBill["Memo"].ToString() : string.Empty;
  1356. var sPARM3 = @"";
  1357. var sPARM4 = @"";
  1358. var sPARM7 = oExhibition.Exhibitioname_TW;
  1359. var sPARM14 = @"";
  1360. var sPARM16 = @"";
  1361. var iPARM18 = double.Parse(sTaxSum);
  1362. var iPARM19 = double.Parse(sAmountTaxSum);
  1363. var iPARM20 = double.Parse(sAdvance);
  1364. var iPARM21 = double.Parse(sTotalReceivable);
  1365. var sPARM27 = @"";
  1366. var sPARM29 = ExhibitionHelper.GetEnglishName(i_crm.USERID); ;
  1367. var sPARM30 = sCurrency;
  1368. if (!string.IsNullOrEmpty(sWeight))
  1369. {
  1370. sPARM16 = sWeight + @"KG";
  1371. }
  1372. if (!string.IsNullOrEmpty(sVolume))
  1373. {
  1374. sPARM16 = sPARM16 == @"" ? sVolume + @"CBM" : sPARM16 + @" / " + sVolume + @"CBM";
  1375. }
  1376. if (!string.IsNullOrEmpty(sNumber))
  1377. {
  1378. sPARM14 = sNumber + sUnit;
  1379. }
  1380. if (!string.IsNullOrEmpty(sContactorName))
  1381. {
  1382. sPARM3 = sContactorName;
  1383. }
  1384. if (!string.IsNullOrEmpty(sTelephone))
  1385. {
  1386. sPARM4 = sTelephone;
  1387. }
  1388. if (!string.IsNullOrEmpty(sMemo))
  1389. {
  1390. sPARM27 = System.Security.SecurityElement.Escape(sMemo);
  1391. }
  1392. if (bTWCurrency)
  1393. {
  1394. iPARM17 = ExhibitionHelper.Round(iPARM17, 0);
  1395. iPARM18 = ExhibitionHelper.Round(iPARM18, 0);
  1396. iPARM19 = ExhibitionHelper.Round(iPARM19, 0);
  1397. iPARM20 = ExhibitionHelper.Round(iPARM20, 0);
  1398. iPARM21 = ExhibitionHelper.Round(iPARM21, 0);
  1399. }
  1400. else
  1401. {
  1402. sPARM7 = oExhibition.Exhibitioname_EN;
  1403. }
  1404. if (bTW)
  1405. {
  1406. var member = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  1407. sPARM29 = member.MemberName;
  1408. }
  1409. ExhibitionHelper.RenderMemo(body, sPARM27);
  1410. var sShipAndVoyage = oOpm.ShipAndVoyage ?? @"";
  1411. var sBillLadNO = oOpm.BillLadNO ?? @"";
  1412. var sBillLadNOSub = oOpm.BillLadNOSub ?? @"";
  1413. var sShipmentPort = oOpm.ShipmentPort ?? @"";
  1414. var sDestinationPort = oOpm.DestinationPort ?? @"";
  1415. string sIsRetn = oBill["IsRetn"] != null ? oBill["IsRetn"].ToString() : string.Empty;
  1416. string sReFlow = oBill["ReFlow"] != null ? oBill["ReFlow"].ToString() : string.Empty;
  1417. if (sIsRetn == "Y" && oBill[@"SupplierGuid"] != null && !string.IsNullOrEmpty(sReFlow))
  1418. {
  1419. var sCustomerGuid = oBill[@"CustomerGuid"] != null ? oBill[@"CustomerGuid"].ToString() : string.Empty;
  1420. var sSupplierGuid = oBill[@"SupplierGuid"] != null ? oBill[@"SupplierGuid"].ToString() : string.Empty;
  1421. var saExhibitors = (JArray)JsonConvert.DeserializeObject(oOpm.Exhibitors);
  1422. foreach (JObject jo in saExhibitors)
  1423. {
  1424. string sSupplierID = jo[@"SupplierID"] != null ? jo[@"SupplierID"].ToString() : string.Empty;
  1425. string sGUID = jo[@"guid"] != null ? jo[@"guid"].ToString() : string.Empty;
  1426. if (sCustomerGuid == sSupplierID && sSupplierGuid == sGUID && jo[sReFlow] != null)
  1427. {
  1428. var oReFlow = (JObject)jo[sReFlow];
  1429. sShipAndVoyage = oReFlow[@"ShipAndVoyage"] == null ? @"" : oReFlow[@"ShipAndVoyage"].ToString();
  1430. sBillLadNO = oReFlow[@"BillLadNO"] == null ? @"" : oReFlow[@"BillLadNO"].ToString();
  1431. sBillLadNOSub = oReFlow[@"BillLadNOSub"] == null ? @"" : oReFlow[@"BillLadNOSub"].ToString();
  1432. sShipmentPort = oReFlow[@"ShipmentPort"] == null ? @"" : oReFlow[@"ShipmentPort"].ToString();
  1433. sDestinationPort = oReFlow[@"Destination"] == null ? @"" : oReFlow[@"Destination"].ToString();
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. string sBillCheckDate = oBill["BillCheckDate"] != null ? oBill["BillCheckDate"].ToString() : string.Empty;
  1439. var BillCheckDate = string.IsNullOrEmpty(sBillCheckDate) ? string.Empty : Convert.ToDateTime(sBillCheckDate).ToString(@"yyyy/MM/dd");
  1440. var sBody = body.InnerXml
  1441. .Replace(@"[PARM1]", string.IsNullOrEmpty(oPayer.CustomerCName) ? Common.EncodeEscapeChar(oPayer.CustomerEName) : Common.EncodeEscapeChar(oPayer.CustomerCName))
  1442. .Replace(@"[PARM2]", ExhibitionHelper.GetBillAddress(oPayer))
  1443. .Replace(@"[PARM3]", Common.EncodeEscapeChar(sPARM3))
  1444. .Replace(@"[PARM4]", Common.EncodeEscapeChar(sPARM4))
  1445. .Replace(@"[PARM5]", oPayer.FAX ?? @"")
  1446. .Replace(@"[PARM6]", oPayer.UniCode ?? @"")
  1447. .Replace(@"[PARM7]", Common.EncodeEscapeChar(sPARM7))
  1448. .Replace(@"[PARM8]", sBillNO)
  1449. .Replace(@"[PARM9]", BillCheckDate)
  1450. .Replace(@"[PARM10]", Common.EncodeEscapeChar(sShipAndVoyage))
  1451. .Replace(@"[PARM11]", Common.EncodeEscapeChar(sBillLadNO))
  1452. .Replace(@"[PARM12]", Common.EncodeEscapeChar(sShipmentPort))
  1453. .Replace(@"[PARM13]", Common.EncodeEscapeChar(sDestinationPort))
  1454. .Replace(@"[PARM14]", Common.EncodeEscapeChar(sPARM14))
  1455. .Replace(@"[PARM16]", Common.EncodeEscapeChar(sPARM16))
  1456. .Replace(@"[PARM17]", bTWCurrency ? $@"{iPARM17:N0}" : $@"{iPARM17:N2}")
  1457. .Replace(@"[PARM18]", bTWCurrency ? $@"{iPARM18:N0}" : $@"{iPARM18:N2}")
  1458. .Replace(@"[PARM19]", bTWCurrency ? $@"{iPARM19:N0}" : $@"{iPARM19:N2}")
  1459. .Replace(@"[PARM20]", bTWCurrency ? $@"{iPARM20:N0}" : $@"{iPARM20:N2}")
  1460. .Replace(@"[PARM21]", bTWCurrency ? $@"{iPARM21:N0}" : $@"{iPARM21:N2}")
  1461. .Replace(@"[PARM22]", Common.EncodeEscapeChar(sPayDateText))
  1462. .Replace(@"[PARM27]", Common.EncodeEscapeChar(sPARM27))
  1463. .Replace(@"[PARM28]", Common.EncodeEscapeChar(sBillLadNOSub))
  1464. .Replace(@"[PARM29]", Common.EncodeEscapeChar(sPARM29))
  1465. .Replace(@"[PARM30]", Common.EncodeEscapeChar(sPARM30));
  1466. foreach (JObject jo in JTmpKeys)
  1467. {
  1468. string sTempKey = jo["TemplKey"] != null ? jo["TemplKey"].ToString() : string.Empty;
  1469. string sTemplKeyValue = jo["TemplKeyValue"] != null ? jo["TemplKeyValue"].ToString() : string.Empty;
  1470. if (!string.IsNullOrEmpty(sTempKey))
  1471. {
  1472. var Value = Common.EncodeEscapeChar(sTemplKeyValue);
  1473. var Key = sTempKey;
  1474. if (Key.Contains("PARM") && WatermarkInfo.Item1)
  1475. {
  1476. Value = "";
  1477. }
  1478. sBody = sBody.Replace(Key, Value);
  1479. }
  1480. }
  1481. doc.MainDocumentPart.Document.Body.InnerXml = sBody;
  1482. doc.MainDocumentPart.Document.Save();
  1483. if (WatermarkInfo.Item1)
  1484. {
  1485. Common.WordAddWatermartText(doc, WatermarkInfo.Item2);
  1486. }
  1487. }
  1488. if (File.Exists(sOutPut))
  1489. {
  1490. File.Delete(sOutPut);
  1491. }
  1492. if (sAction.StartsWith(@"Print"))
  1493. {
  1494. sOutPut = sOutPut.Replace(@"docx", @"pdf");
  1495. var bOk = Common.WordToPDF(sTempFile, sOutPut);
  1496. if (!bOk)
  1497. {
  1498. sOutPut = @"";
  1499. }
  1500. }
  1501. else
  1502. {
  1503. File.Copy(sTempFile, sOutPut);
  1504. }
  1505. Thread.Sleep(500);
  1506. File.Delete(sTempFile); //刪除臨時文件
  1507. sOutPut = sOutPut.Replace(sBase, @"");
  1508. rm = new SuccessResponseMessage(null, i_crm);
  1509. rm.DATA.Add(BLWording.REL, sOutPut);
  1510. } while (false);
  1511. }
  1512. catch (Exception ex)
  1513. {
  1514. sMsg = Util.GetLastExceptionMsg(ex);
  1515. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm) + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他(駒驛)", @"PrintBill(其他(駒驛)帳單列印下載)", @"", @"", @"");
  1516. }
  1517. finally
  1518. {
  1519. if (null != sMsg)
  1520. {
  1521. rm = new ErrorResponseMessage(sMsg, i_crm);
  1522. }
  1523. Logger.Debug(@"OtherExhibitionTG_UpdService.PrintBill Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1524. }
  1525. return rm;
  1526. }
  1527. #endregion 其他(駒驛)帳單列印下載
  1528. #region 其他(駒驛)收據列印下載
  1529. /// <summary>
  1530. /// 其他(駒驛)收據列印下載
  1531. /// </summary>
  1532. /// <param name="i_crm">todo: describe i_crm parameter on PrintReceipt</param>
  1533. /// <returns></returns>
  1534. public ResponseMessage PrintReceipt(RequestMessage i_crm)
  1535. {
  1536. ResponseMessage rm = null;
  1537. string sMsg = null;
  1538. var db = SugarBase.GetIntance();
  1539. var sOutPut = Common.ConfigGetValue(@"", @"OutFilesPath");
  1540. try
  1541. {
  1542. do
  1543. {
  1544. var sId = _fetchString(i_crm, OTB_OPM_OtherExhibitionTG.CN_GUID);
  1545. var sTemplID = _fetchString(i_crm, OTB_SYS_OfficeTemplate.CN_TEMPLID);
  1546. var sBill = _fetchString(i_crm, @"Bill");
  1547. var sAction = _fetchString(i_crm, @"Action");
  1548. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  1549. var oOpm = sdb.GetById(sId);
  1550. if (oOpm == null)
  1551. {
  1552. sMsg = @"系統找不到對應的基本資料,請核查!";
  1553. break;
  1554. }
  1555. var oTempl = db.Queryable<OTB_SYS_OfficeTemplate>().Single(it => it.OrgID == i_crm.ORIGID && it.TemplID == sTemplID);
  1556. if (oTempl == null)
  1557. {
  1558. sMsg = @"請檢查模版設定";
  1559. break;
  1560. }
  1561. var oFile = db.Queryable<OTB_SYS_Files>()
  1562. .Single(it => it.OrgID == i_crm.ORIGID && it.ParentID == oTempl.FileID);
  1563. if (oFile == null)
  1564. {
  1565. sMsg = @"系統找不到對應的帳單模版";
  1566. break;
  1567. }
  1568. var oBill = (JObject)JsonConvert.DeserializeObject(sBill);
  1569. var bTWCurrency = oBill[@"Currency"].ToString() == @"NTD";
  1570. var oPayer = new OTB_CRM_Customers();
  1571. if (!string.IsNullOrEmpty(oBill[@"Payer"].ToString()))
  1572. {
  1573. oPayer = db.Queryable<OTB_CRM_Customers>().Single(it => it.guid == oBill[@"Payer"].ToString());
  1574. if (oPayer == null)
  1575. {
  1576. sMsg = @"系統找不到付款人資訊";
  1577. break;
  1578. }
  1579. }
  1580. var sTempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, oFile.FilePath);//Word模版路徑
  1581. var sBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"");
  1582. sOutPut = sBase + sOutPut;
  1583. Common.FnCreateDir(sOutPut);//如果不存在就創建文件夾
  1584. var sDocxName = @"Receipt-" + oBill[@"BillNO"];
  1585. //建立臨時文件
  1586. var sTempFile = Path.GetTempPath() + sDocxName + @".docx";
  1587. sOutPut += sDocxName + @".docx";
  1588. if (File.Exists(sTempFile))
  1589. {
  1590. File.Delete(sTempFile);
  1591. }
  1592. File.Copy(sTempPath, sTempFile);
  1593. var JTmpKeys = (JArray)JsonConvert.DeserializeObject(oTempl.TemplKeys);
  1594. double iAcountAll = 0;
  1595. var WatermarkInfo = Common.GetWatermarkInfo(oBill[@"AuditVal"]?.ToString(), sTemplID, i_crm.ORIGID);
  1596. using (WordprocessingDocument doc = WordprocessingDocument.Open(sTempFile, true))//顯示Word
  1597. {
  1598. var body = doc.MainDocumentPart.Document.Body;
  1599. var jaFeeItems = (JArray)JsonConvert.DeserializeObject(oBill[@"FeeItems"].ToString());
  1600. //查找:獲取第三個表格
  1601. var table = body.Elements<Table>().ElementAt(3);
  1602. if (jaFeeItems.Count > 0)
  1603. {
  1604. var jaFeeItems_NoTax = new JArray();
  1605. var iInsertRow = 5;
  1606. foreach (JObject jo in jaFeeItems)
  1607. {
  1608. if (jo[@"FinancialTaxRate"].ToString().Replace(@"%", @"").Trim() == @"0")
  1609. {
  1610. jaFeeItems_NoTax.Add(jo);
  1611. }
  1612. }
  1613. if (jaFeeItems_NoTax.Count > 0)
  1614. {
  1615. var jaFeeItems_NoTaxSorted = new JArray(jaFeeItems_NoTax.OrderBy(p => p[@"OrderBy"]));
  1616. foreach (JObject jo in jaFeeItems_NoTaxSorted)
  1617. {
  1618. var trow_First = table.Elements<TableRow>().ElementAt(1);//費用明細第一行
  1619. var trow_Item = table.Elements<TableRow>().ElementAt(2).Clone() as TableRow;//費用明細行
  1620. if (iInsertRow == 5)
  1621. {
  1622. for (int i = 0; i < 5; i++)
  1623. {
  1624. var tcell = trow_First.Elements<TableCell>().ElementAt(i);
  1625. var tmpPa = tcell.Elements<Paragraph>().FirstOrDefault();
  1626. var tmpRun = tmpPa.Elements<Run>().FirstOrDefault();
  1627. var tmpText = tmpRun.Elements<Text>().FirstOrDefault();
  1628. switch (i)
  1629. {
  1630. case 0:
  1631. var sRemark = jo[@"Memo"] == null ? @"" : jo[@"Memo"].ToString();
  1632. tmpText.Text = jo[@"FinancialCode"].ToString() == @"TE001" ? (sRemark == @"" ? jo[@"FinancialCostStatement"].ToString() : sRemark) : jo[@"FinancialCostStatement"] + (sRemark == @"" ? @"" : @"(" + sRemark + @")");
  1633. break;
  1634. case 1:
  1635. tmpText.Text = jo[@"FinancialCurrency"].ToString();
  1636. break;
  1637. case 2:
  1638. tmpText.Text = jo[@"FinancialExchangeRate"].ToString();
  1639. break;
  1640. case 3:
  1641. var sFinancialAmount = jo[@"FinancialTWAmount"].ToString();
  1642. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount.Trim() == @"" ? @"0" : sFinancialAmount):N}";
  1643. break;
  1644. case 4:
  1645. var sFinancialAmount4 = jo[@"FinancialTWAmount"].ToString();
  1646. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount4.Trim() == @"" ? @"0" : sFinancialAmount4):N}";
  1647. break;
  1648. }
  1649. }
  1650. }
  1651. else
  1652. {
  1653. for (int i = 0; i < 5; i++)
  1654. {
  1655. var tcell = trow_Item.Elements<TableCell>().ElementAt(i);
  1656. var tmpPa = tcell.Elements<Paragraph>().FirstOrDefault();
  1657. var tmpRun = tmpPa.Elements<Run>().FirstOrDefault();
  1658. var tmpText = tmpRun.Elements<Text>().FirstOrDefault();
  1659. switch (i)
  1660. {
  1661. case 0:
  1662. var sRemark = jo[@"Memo"] == null ? @"" : jo[@"Memo"].ToString();
  1663. tmpText.Text = jo[@"FinancialCode"].ToString() == @"TE001" ? (sRemark == @"" ? jo[@"FinancialCostStatement"].ToString() : sRemark) : jo[@"FinancialCostStatement"] + (sRemark == @"" ? @"" : @"(" + sRemark + @")");
  1664. break;
  1665. case 1:
  1666. tmpText.Text = jo[@"FinancialCurrency"].ToString();
  1667. break;
  1668. case 2:
  1669. tmpText.Text = jo[@"FinancialExchangeRate"].ToString();
  1670. break;
  1671. case 3:
  1672. var sFinancialAmount = jo[@"FinancialTWAmount"].ToString();
  1673. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount.Trim() == @"" ? @"0" : sFinancialAmount):N}";
  1674. break;
  1675. case 4:
  1676. var sFinancialAmount4 = jo[@"FinancialTWAmount"].ToString();
  1677. tmpText.Text = $@"{Convert.ToDecimal(sFinancialAmount4.Trim() == @"" ? @"0" : sFinancialAmount4):N}";
  1678. break;
  1679. default:
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. iAcountAll += double.Parse(jo[@"FinancialTWAmount"].ToString());
  1685. //插入一行
  1686. table.InsertAt<TableRow>(trow_Item, iInsertRow);
  1687. iInsertRow++;
  1688. }
  1689. table.Elements<TableRow>().ElementAt(2).Remove();
  1690. table.Elements<TableRow>().ElementAt(2).Remove();
  1691. }
  1692. else
  1693. {
  1694. table.Elements<TableRow>().ElementAt(1).Remove();
  1695. table.Elements<TableRow>().ElementAt(1).Remove();
  1696. }
  1697. //刪除模板行
  1698. }
  1699. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == oOpm.ExhibitionNO.ObjToInt());
  1700. if (oExhibition == null)
  1701. {
  1702. oExhibition = new OTB_OPM_Exhibition();
  1703. }
  1704. var sPARM1 = string.IsNullOrEmpty(oPayer.CustomerCName) ? oPayer.CustomerEName : oPayer.CustomerCName;
  1705. var sPARM2 = oBill[@"ReceiptDate"].ToString();
  1706. var sPARM3 = oPayer.UniCode;
  1707. var sPARM4 = oBill[@"ReceiptNumber"].ToString();
  1708. var sPARM5 = ExhibitionHelper.GetBillAddress(oPayer);
  1709. var sPARM6 = oExhibition.Exhibitioname_TW;
  1710. var sPARM7 = oBill[@"TaxSum"].ToString();
  1711. var sPARM8 = @"";
  1712. var sPARM9 = oBill[@"BillNO"].ToString();
  1713. var iPARM7 = iAcountAll;
  1714. if (sPARM2 != @"")
  1715. {
  1716. sPARM2 = Common.DateToTw(sPARM2);
  1717. var saPARM2 = sPARM2.Split('/');
  1718. sPARM2 = saPARM2[0] + @"年" + saPARM2[1] + @"月" + saPARM2[2] + @"日";
  1719. }
  1720. iPARM7 = ExhibitionHelper.Round(iPARM7, 0);
  1721. sPARM8 = Common.MoneyToUpper(iPARM7.ToString());
  1722. sPARM7 = $@"{iPARM7:N0}";
  1723. var sBody = body.InnerXml
  1724. .Replace(@"[PARM1]", Common.EncodeEscapeChar(sPARM1))
  1725. .Replace(@"[PARM2]", Common.EncodeEscapeChar(sPARM2))
  1726. .Replace(@"[PARM3]", Common.EncodeEscapeChar(sPARM3))
  1727. .Replace(@"[PARM4]", Common.EncodeEscapeChar(sPARM4))
  1728. .Replace(@"[PARM5]", Common.EncodeEscapeChar(sPARM5))
  1729. .Replace(@"[PARM6]", Common.EncodeEscapeChar(sPARM6))
  1730. .Replace(@"[PARM7]", Common.EncodeEscapeChar(sPARM7))
  1731. .Replace(@"[PARM8]", Common.EncodeEscapeChar(sPARM8))
  1732. .Replace(@"[PARM9]", Common.EncodeEscapeChar(sPARM9));
  1733. foreach (JObject jo in JTmpKeys)
  1734. {
  1735. if (jo.Property(@"TemplKey") != null && jo[@"TemplKey"].ToString() != @"")
  1736. {
  1737. sBody = sBody.Replace(jo[@"TemplKey"].ToString(), Common.EncodeEscapeChar(jo[@"TemplKeyValue"].ToString()));
  1738. }
  1739. }
  1740. doc.MainDocumentPart.Document.Body.InnerXml = sBody + sBody;
  1741. doc.MainDocumentPart.Document.Save();
  1742. if (WatermarkInfo.Item1)
  1743. {
  1744. Common.WordAddWatermartText(doc, WatermarkInfo.Item2);
  1745. }
  1746. }
  1747. if (File.Exists(sOutPut))
  1748. {
  1749. File.Delete(sOutPut);
  1750. }
  1751. if (sAction.StartsWith(@"Print"))
  1752. {
  1753. sOutPut = sOutPut.Replace(@"docx", @"pdf");
  1754. var bOk = Common.WordToPDF(sTempFile, sOutPut);
  1755. if (!bOk)
  1756. {
  1757. sOutPut = @"";
  1758. }
  1759. }
  1760. else
  1761. {
  1762. File.Copy(sTempFile, sOutPut);
  1763. }
  1764. Thread.Sleep(500);
  1765. File.Delete(sTempFile); //刪除臨時文件
  1766. sOutPut = sOutPut.Replace(sBase, @"");
  1767. rm = new SuccessResponseMessage(null, i_crm);
  1768. rm.DATA.Add(BLWording.REL, sOutPut);
  1769. } while (false);
  1770. }
  1771. catch (Exception ex)
  1772. {
  1773. sMsg = Util.GetLastExceptionMsg(ex);
  1774. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherBusiness_UpdService), @"其他(駒驛)", @"PrintReceipt(其他(駒驛)收據列印下載)", @"", @"", @"");
  1775. }
  1776. finally
  1777. {
  1778. if (null != sMsg)
  1779. {
  1780. rm = new ErrorResponseMessage(sMsg, i_crm);
  1781. }
  1782. Logger.Debug(@"OtherExhibitionTG_UpdService.PrintReceipt Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1783. }
  1784. return rm;
  1785. }
  1786. #endregion 其他(駒驛)收據列印下載
  1787. #region 更新帳單明細
  1788. /// <summary>
  1789. /// 更新帳單明細
  1790. /// </summary>
  1791. /// <param name="i_crm">帳單資料</param>
  1792. /// <returns></returns>
  1793. public ResponseMessage UpdateBillInfo(RequestMessage i_crm)
  1794. {
  1795. ResponseMessage rm = null;
  1796. string sMsg = null;
  1797. var db = SugarBase.GetIntance();
  1798. try
  1799. {
  1800. do
  1801. {
  1802. var sId = _fetchString(i_crm, @"Guid");
  1803. var sBillNO = _fetchString(i_crm, @"BillNO");
  1804. var sdb = new SimpleClient<OTB_OPM_OtherExhibitionTG>(db);
  1805. var oOpm = sdb.GetById(sId);
  1806. if (oOpm == null)
  1807. {
  1808. break;
  1809. }
  1810. var jaBills = new JArray();
  1811. jaBills = (JArray)JsonConvert.DeserializeObject(oOpm.Bills);
  1812. if (jaBills.Count > 0)
  1813. {
  1814. foreach (JObject jo in jaBills)
  1815. {
  1816. var oBillInfo_New = ExhibitionService.GetNewBillInfo(jo);
  1817. oBillInfo_New.OrgID = i_crm.ORIGID;
  1818. oBillInfo_New.BillType = i_crm.MODULE;
  1819. oBillInfo_New.ParentId = sId;
  1820. oBillInfo_New.RefNumber = @"";
  1821. oBillInfo_New.ExhibitionNO = oOpm.ExhibitionNO;
  1822. oBillInfo_New.ResponsiblePerson = oOpm.ResponsiblePerson;
  1823. oBillInfo_New.ModifyDate = DateTime.Now;
  1824. oBillInfo_New.ModifyUser = i_crm.USERID;
  1825. oBillInfo_New.AuditVal = jo[@"AuditVal"].ToString();
  1826. if (string.IsNullOrEmpty(sBillNO))
  1827. {
  1828. var s_BillNO = jo[@"BillNO"].ToString();
  1829. var oBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  1830. .Single(it => it.OrgID == i_crm.ORIGID && it.BillNO == s_BillNO);
  1831. if (oBillInfo == null)
  1832. {
  1833. oBillInfo_New.CreateDate = DateTime.Now;
  1834. oBillInfo_New.CreateUser = i_crm.USERID;
  1835. db.Insertable(oBillInfo_New).ExecuteCommand();
  1836. }
  1837. else
  1838. {
  1839. oBillInfo_New.CreateDate = oBillInfo.CreateDate;
  1840. oBillInfo_New.CreateUser = oBillInfo.CreateUser;
  1841. db.Updateable(oBillInfo_New).Where(it => it.SN == oBillInfo.SN).ExecuteCommand();
  1842. }
  1843. }
  1844. else
  1845. {
  1846. if (sBillNO == jo[@"BillNO"].ToString())
  1847. {
  1848. var oBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  1849. .Single(it => it.OrgID == i_crm.ORIGID && it.BillNO == sBillNO);
  1850. if (oBillInfo == null)
  1851. {
  1852. oBillInfo_New.CreateDate = DateTime.Now;
  1853. oBillInfo_New.CreateUser = i_crm.USERID;
  1854. db.Insertable(oBillInfo_New).ExecuteCommand();
  1855. }
  1856. else
  1857. {
  1858. oBillInfo_New.CreateDate = oBillInfo.CreateDate;
  1859. oBillInfo_New.CreateUser = oBillInfo.CreateUser;
  1860. db.Updateable(oBillInfo_New).Where(it => it.SN == oBillInfo.SN).ExecuteCommand();
  1861. }
  1862. break;
  1863. }
  1864. }
  1865. }
  1866. }
  1867. rm = new SuccessResponseMessage(null, i_crm);
  1868. } while (false);
  1869. }
  1870. catch (Exception ex)
  1871. {
  1872. sMsg = Util.GetLastExceptionMsg(ex);
  1873. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherExhibitionTG_UpdService), @"其他", @"UpdateBillInfo(更新帳單明細)", @"", @"", @"");
  1874. }
  1875. finally
  1876. {
  1877. if (null != sMsg)
  1878. {
  1879. rm = new ErrorResponseMessage(sMsg, i_crm);
  1880. }
  1881. Logger.Debug(@"OtherExhibitionTG_UpdService.UpdateBillInfo Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  1882. }
  1883. return rm;
  1884. }
  1885. #endregion 更新帳單明細
  1886. }
  1887. }