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.

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