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.

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