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.

307 lines
17 KiB

2 years ago
  1. using Aspose.Cells;
  2. using EasyBL.WebApi.Message;
  3. using Entity.Sugar;
  4. using Newtonsoft.Json.Linq;
  5. using SqlSugar;
  6. using SqlSugar.Base;
  7. using System;
  8. using System.Data;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. namespace EasyBL.WEBAPP.OPM
  13. {
  14. public class ExhibitionService : ServiceBase
  15. {
  16. #region 匯入報價費用項目
  17. /// <summary>
  18. /// 匯入報價費用項目
  19. /// </summary>
  20. /// <param name="i_crm">todo: describe i_crm parameter on GetImportFeeitems</param>
  21. /// <returns></returns>
  22. public ResponseMessage GetImportFeeitems(RequestMessage i_crm)
  23. {
  24. ResponseMessage rm = null;
  25. string sMsg = null;
  26. var db = SugarBase.DB;
  27. var tb_FeeItems = new DataTable(@"tb");
  28. try
  29. {
  30. do
  31. {
  32. var sFileId = _fetchString(i_crm, @"FileId");
  33. var sFileName = _fetchString(i_crm, @"FileName");
  34. var sRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"OutFiles\Temporary\");//Word模版路徑
  35. var sfileName = sFileName.Split(new string[] { @"." }, StringSplitOptions.RemoveEmptyEntries);
  36. var sSubFileName = sfileName.LastOrDefault(); //副檔名
  37. sFileName = sRoot + sFileId + @"." + sSubFileName;
  38. tb_FeeItems.Columns.Add(@"guid", typeof(String));//費用代號
  39. tb_FeeItems.Columns.Add(@"FinancialCode", typeof(String));//費用代號
  40. tb_FeeItems.Columns.Add(@"FinancialCostStatement", typeof(String));//費用說明
  41. tb_FeeItems.Columns.Add(@"FinancialCurrency", typeof(String));//幣別
  42. tb_FeeItems.Columns.Add(@"FinancialUnitPrice", typeof(String));//單價
  43. tb_FeeItems.Columns.Add(@"FinancialNumber", typeof(String));//FinancialNumber
  44. tb_FeeItems.Columns.Add(@"FinancialUnit", typeof(String));//單位
  45. tb_FeeItems.Columns.Add(@"FinancialAmount", typeof(String));//金額
  46. tb_FeeItems.Columns.Add(@"FinancialExchangeRate", typeof(String));//匯率
  47. tb_FeeItems.Columns.Add(@"FinancialTWAmount", typeof(String));//台幣金額
  48. tb_FeeItems.Columns.Add(@"FinancialTaxRate", typeof(String));//稅率
  49. tb_FeeItems.Columns.Add(@"FinancialTax", typeof(String));//稅額
  50. tb_FeeItems.Columns.Add(@"Memo", typeof(String));//稅額
  51. tb_FeeItems.Columns.Add(@"CreateUser", typeof(String));//創建人
  52. tb_FeeItems.Columns.Add(@"CreateDate", typeof(String));//創建時間
  53. var book = new Workbook(sFileName);
  54. //book.Open(sFileName);
  55. var sheet = book.Worksheets[0];
  56. var cells = sheet.Cells;
  57. var tbFeeItems = cells.ExportDataTableAsString(1, 0, cells.MaxDataRow - 2, cells.MaxDataColumn + 1, false);
  58. if (tbFeeItems.Rows.Count > 0)
  59. {
  60. foreach (DataRow row in tbFeeItems.Rows)
  61. {
  62. try
  63. {
  64. var sFinancialCode = row[@"Column2"].ToString();
  65. if (IsInt(row[@"Column1"].ToString()) && sFinancialCode != @"")
  66. {
  67. var bDefult = sFinancialCode == @"TE001";
  68. var iCount = 0;
  69. if (!bDefult)
  70. {
  71. iCount = db.Queryable<OTB_SYS_Arguments>()
  72. .Count(it => it.ArgumentClassID == @"FeeClass" && it.ArgumentID == sFinancialCode && it.OrgID == i_crm.ORIGID);
  73. }
  74. if (bDefult || iCount > 0)
  75. {
  76. var tb_row = tb_FeeItems.NewRow();
  77. tb_row[@"guid"] = Guid.NewGuid();
  78. tb_row[@"FinancialCode"] = bDefult ? @"TE001" : row[@"Column2"].ToString();
  79. tb_row[@"FinancialCostStatement"] = row[@"Column3"].ToString();
  80. tb_row[@"Memo"] = row[@"Column4"].ToString();
  81. tb_row[@"FinancialCurrency"] = row[@"Column5"].ToString();
  82. tb_row[@"FinancialUnitPrice"] = row[@"Column6"].ToString().Replace(@",", @"");
  83. tb_row[@"FinancialUnitPrice"] = tb_row[@"FinancialUnitPrice"].ToString() == @"" ? @"0" : tb_row[@"FinancialUnitPrice"].ToString().Replace(@",", @"");
  84. tb_row[@"FinancialNumber"] = row[@"Column7"].ToString();
  85. tb_row[@"FinancialUnit"] = row[@"Column8"].ToString();
  86. tb_row[@"FinancialAmount"] = row[@"Column10"].ToString().Replace(@",", @"");
  87. tb_row[@"FinancialAmount"] = tb_row[@"FinancialAmount"].ToString() == @"" ? @"0" : tb_row[@"FinancialAmount"].ToString().Replace(@",", @"");
  88. tb_row[@"FinancialExchangeRate"] = row[@"Column11"].ToString();
  89. tb_row[@"FinancialTWAmount"] = row[@"Column13"].ToString().Replace(@",", @"");
  90. tb_row[@"FinancialTWAmount"] = tb_row[@"FinancialTWAmount"].ToString() == @"" ? @"0" : tb_row[@"FinancialTWAmount"].ToString().Replace(@",", @"");
  91. tb_row[@"FinancialTaxRate"] = row[@"Column14"].ToString();// row["Column1"].ToString();
  92. tb_row[@"FinancialTax"] = decimal.Parse(row[@"Column13"].ToString().Replace(@",", @"")) * decimal.Parse(row[@"Column14"].ToString().Replace(@"%", @"")) / 100;// row["Column1"].ToString();
  93. tb_row[@"FinancialTax"] = tb_row[@"FinancialTax"].ToString() == @"" ? @"0" : tb_row[@"FinancialTax"];
  94. tb_row[@"CreateUser"] = i_crm.USERID ?? @"";
  95. tb_row[@"CreateDate"] = DateTime.Now.ToString(@"yyyy/MM/dd HH:mm:ss");
  96. tb_FeeItems.Rows.Add(tb_row);
  97. }
  98. }
  99. }
  100. catch { }
  101. }
  102. }
  103. rm = new SuccessResponseMessage(null, i_crm);
  104. rm.DATA.Add(BLWording.REL, tb_FeeItems);
  105. } while (false);
  106. }
  107. catch (Exception ex)
  108. {
  109. sMsg = Util.GetLastExceptionMsg(ex);
  110. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionService), @"展覽管理", @"GetImportFeeitems(匯入報價費用項目)", @"", @"", @"");
  111. }
  112. finally
  113. {
  114. if (null != sMsg)
  115. {
  116. rm = new ErrorResponseMessage(sMsg, i_crm);
  117. }
  118. }
  119. return rm;
  120. }
  121. #endregion 匯入報價費用項目
  122. #region 拋轉專案
  123. /// <summary>
  124. /// 拋轉專案
  125. /// </summary>
  126. /// <param name="i_crm">todo: describe i_crm parameter on Transfer</param>
  127. /// <returns></returns>
  128. public ResponseMessage Transfer(RequestMessage i_crm)
  129. {
  130. ResponseMessage rm = null;
  131. string sMsg = null;
  132. try
  133. {
  134. rm = SugarBase.ExecTran(db =>
  135. {
  136. do
  137. {
  138. var sId = _fetchString(i_crm, @"SN");
  139. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(it => it.SN == sId.ObjToInt());
  140. if (oExhibition == null)
  141. {
  142. sMsg = @"系統找不到對應的專案(展覽)資料,請核查!";
  143. break;
  144. }
  145. //更新客戶資料
  146. var oExhibitionUpd = new OTB_OPM_Exhibition
  147. {
  148. IsTransfer = @"Y",
  149. LastTransfer_Time = DateTime.Now,
  150. ModifyUser = i_crm.USERID,
  151. ModifyDate = DateTime.Now
  152. };
  153. db.Updateable(oExhibitionUpd)
  154. .UpdateColumns(it => new { it.IsTransfer, it.LastTransfer_Time, it.ModifyUser, it.ModifyDate })
  155. .Where(it => it.SN == sId.ObjToInt()).ExecuteCommand();
  156. var oExhibitionsTransferUpd = new OTB_OPM_ExhibitionsTransfer
  157. {
  158. OrgID = i_crm.ORIGID,
  159. PrjNO = oExhibition.ExhibitionCode,
  160. PrjName = Common.CutByteString(oExhibition.ExhibitioShotName_TW, 60)
  161. };
  162. var sCreateUser = oExhibition.CreateUser.Split('.')[0];
  163. oExhibitionsTransferUpd.PrjCharger = Common.CutByteString(sCreateUser, 11);
  164. oExhibitionsTransferUpd.EndDate = @"";
  165. db.Insertable(oExhibitionsTransferUpd).ExecuteCommand();
  166. rm = new SuccessResponseMessage(null, i_crm);
  167. } while (false);
  168. return rm;
  169. });
  170. }
  171. catch (Exception ex)
  172. {
  173. sMsg = Util.GetLastExceptionMsg(ex);
  174. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(ExhibitionService), @"展覽管理", @"Transfer(拋轉專案)", @"", @"", @"");
  175. }
  176. finally
  177. {
  178. if (null != sMsg)
  179. {
  180. rm = new ErrorResponseMessage(sMsg, i_crm);
  181. }
  182. }
  183. return rm;
  184. }
  185. #endregion 拋轉專案
  186. #region 刪除帳單明細
  187. /// <summary>
  188. /// 刪除帳單明細
  189. /// </summary>
  190. /// <param name="i_crm">帳單資料</param>
  191. /// <returns></returns>
  192. public ResponseMessage DeleteBillInfo(RequestMessage i_crm)
  193. {
  194. ResponseMessage rm = null;
  195. string sMsg = null;
  196. var db = SugarBase.GetIntance();
  197. try
  198. {
  199. do
  200. {
  201. var sBillNO = _fetchString(i_crm, @"BillNO");
  202. var oBillInfo = db.Queryable<OTB_OPM_BillInfo>()
  203. .Single(it => it.OrgID == i_crm.ORIGID && it.BillNO == sBillNO);
  204. if (oBillInfo != null)
  205. {
  206. var sdb = new SimpleClient<OTB_OPM_BillInfo>(SugarBase.DB);
  207. var bRes = sdb.DeleteById(oBillInfo.SN);
  208. }
  209. rm = new SuccessResponseMessage(null, i_crm);
  210. } while (false);
  211. }
  212. catch (Exception ex)
  213. {
  214. sMsg = Util.GetLastExceptionMsg(ex);
  215. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(OtherBusiness_UpdService), @"其他", @"UpdateBillInfo(刪除帳單明細)", @"", @"", @"");
  216. }
  217. finally
  218. {
  219. if (null != sMsg)
  220. {
  221. rm = new ErrorResponseMessage(sMsg, i_crm);
  222. }
  223. Logger.Debug(@"OtherBusiness_UpdService.UpdateBillInfo Debug(Param:" + JsonToString(i_crm) + @";Response:" + JsonToString(rm) + @")------------------");
  224. }
  225. return rm;
  226. }
  227. #endregion 刪除帳單明細
  228. /// <summary>
  229. /// </summary>
  230. /// <param name="sNumber"></param>
  231. /// <returns></returns>
  232. public static bool IsInt(string sNumber)
  233. {
  234. var objNotNumberPattern = new Regex(@"[^0-9.-]");
  235. var objTwoDotPattern = new Regex(@"[0-9]*[.][0-9]*[.][0-9]*");
  236. var objTwoMinusPattern = new Regex(@"[0-9]*[-][0-9]*[-][0-9]*");
  237. var strValidRealPattern = @"^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
  238. var strValidIntegerPattern = @"^([-]|[0-9])[0-9]*$";
  239. var objNumberPattern = new Regex(@"(" + strValidRealPattern + @")|(" + strValidIntegerPattern + @")");
  240. return !objNotNumberPattern.IsMatch(sNumber) &&
  241. !objTwoDotPattern.IsMatch(sNumber) &&
  242. !objTwoMinusPattern.IsMatch(sNumber) &&
  243. objNumberPattern.IsMatch(sNumber);
  244. }
  245. /// <summary>
  246. /// </summary>
  247. /// <param name="bill"></param>
  248. /// <returns></returns>
  249. public static OTB_OPM_BillInfo GetNewBillInfo(JObject bill)
  250. {
  251. const string CN_ORGANIZER = "BillOrganizer";
  252. var oBill = new OTB_OPM_BillInfo
  253. {
  254. BillGuid = bill[@"guid"].ToString(),
  255. BillNO = bill[OTB_OPM_BillInfo.CN_BILLNO].ToString(),
  256. AuditVal = bill[OTB_OPM_BillInfo.CN_AUDITVAL].ToString(),
  257. BillCreateDate = bill[OTB_OPM_BillInfo.CN_BILLCREATEDATE].ToString(),
  258. BillFirstCheckDate = bill[OTB_OPM_BillInfo.CN_BILLFIRSTCHECKDATE] == null ? bill[OTB_OPM_BillInfo.CN_BILLCHECKDATE].ToString() : bill[OTB_OPM_BillInfo.CN_BILLFIRSTCHECKDATE].ToString(),
  259. BillCheckDate = bill[OTB_OPM_BillInfo.CN_BILLCHECKDATE].ToString(),
  260. Currency = bill[OTB_OPM_BillInfo.CN_CURRENCY].ToString(),
  261. ExchangeRate = bill[OTB_OPM_BillInfo.CN_EXCHANGERATE].ToString(),
  262. Advance = bill[OTB_OPM_BillInfo.CN_ADVANCE].ToString(),
  263. FeeItems = bill[OTB_OPM_BillInfo.CN_FEEITEMS].ToString(),
  264. Memo = bill[OTB_OPM_BillInfo.CN_MEMO] == null ? @"" : bill[OTB_OPM_BillInfo.CN_MEMO].ToString(),
  265. InvoiceNumber = bill[OTB_OPM_BillInfo.CN_INVOICENUMBER].ToString(),
  266. InvoiceDate = bill[OTB_OPM_BillInfo.CN_INVOICEDATE].ToString(),
  267. ReceiptNumber = bill[OTB_OPM_BillInfo.CN_RECEIPTNUMBER].ToString(),
  268. ReceiptDate = bill[OTB_OPM_BillInfo.CN_RECEIPTDATE].ToString(),
  269. Payer = bill[OTB_OPM_BillInfo.CN_PAYER].ToString(),
  270. IsRetn = bill[OTB_OPM_BillInfo.CN_ISRETN] == null ? @"N" : bill[OTB_OPM_BillInfo.CN_ISRETN].ToString(),
  271. Number = bill[OTB_OPM_BillInfo.CN_NUMBER] == null ? @"" : bill[OTB_OPM_BillInfo.CN_NUMBER].ToString(),
  272. Unit = bill[OTB_OPM_BillInfo.CN_UNIT] == null ? @"" : bill[OTB_OPM_BillInfo.CN_UNIT].ToString(),
  273. Weight = bill[OTB_OPM_BillInfo.CN_WEIGHT] == null ? @"" : bill[OTB_OPM_BillInfo.CN_WEIGHT].ToString(),
  274. Volume = bill[OTB_OPM_BillInfo.CN_VOLUME] == null ? @"" : bill[OTB_OPM_BillInfo.CN_VOLUME].ToString(),
  275. ContactorName = bill[OTB_OPM_BillInfo.CN_CONTACTORNAME] == null ? @"" : bill[OTB_OPM_BillInfo.CN_CONTACTORNAME].ToString(),
  276. Telephone = bill[OTB_OPM_BillInfo.CN_TELEPHONE] == null ? @"" : bill[OTB_OPM_BillInfo.CN_TELEPHONE].ToString(),
  277. ReFlow = bill[OTB_OPM_BillInfo.CN_REFLOW] == null ? @"" : bill[OTB_OPM_BillInfo.CN_REFLOW].ToString(),
  278. Index = bill[OTB_OPM_BillInfo.CN_INDEX] == null ? @"" : bill[OTB_OPM_BillInfo.CN_INDEX].ToString(),
  279. AmountSum = bill[OTB_OPM_BillInfo.CN_AMOUNTSUM] == null ? @"0" : bill[OTB_OPM_BillInfo.CN_AMOUNTSUM].ToString(),
  280. TaxSum = bill[OTB_OPM_BillInfo.CN_TAXSUM] == null ? @"0" : bill[OTB_OPM_BillInfo.CN_TAXSUM].ToString(),
  281. AmountTaxSum = bill[OTB_OPM_BillInfo.CN_AMOUNTTAXSUM] == null ? @"0" : bill[OTB_OPM_BillInfo.CN_AMOUNTTAXSUM].ToString(),
  282. TotalReceivable = bill[OTB_OPM_BillInfo.CN_TOTALRECEIVABLE] == null ? @"0" : bill[OTB_OPM_BillInfo.CN_TOTALRECEIVABLE].ToString(),
  283. NotPassReason = bill[OTB_OPM_BillInfo.CN_NOTPASSREASON] == null ? @"" : bill[OTB_OPM_BillInfo.CN_NOTPASSREASON].ToString(),
  284. BillWriteOffDate = bill[OTB_OPM_BillInfo.CN_BILLWRITEOFFDATE] == null ? @"" : bill[OTB_OPM_BillInfo.CN_BILLWRITEOFFDATE].ToString(),
  285. Organizer = bill[CN_ORGANIZER] == null ? @"" : bill[CN_ORGANIZER].ToString()
  286. };
  287. return oBill;
  288. }
  289. }
  290. }