static void Main(string[] args) { System.Threading.Mutex mtx = null; //connect to D365 var context = ImportCommon.Connect(); //read config string strFilePath = ImportCommon.GetAppSetting(BusinessConst.CSV_FILE_PATH); string strSuccessPath = ImportCommon.GetAppSetting(BusinessConst.SUC_FILE_PATH); string strErrorPath = ImportCommon.GetAppSetting(BusinessConst.ERR_FILE_PATH); string strLogPath = ImportCommon.GetAppSetting(BusinessConst.LOG_FILE_PATH); //log file StringBuilder logMsg = new System.Text.StringBuilder(); string strLogName = ImportCommon.GetLogFileName(BusinessConst.REDAC_NAME_INVOICE); string strfileName = ""; int iFileCnt = 0; Console.WriteLine("logName : {0}", strLogName); Console.WriteLine("FilePath : {0}", strFilePath); //mail set Console.WriteLine("mail setting :::"); EmailConf emailConf = new EmailConf(); try { string mutextName = "SanImportAccountCsv"; mtx = new System.Threading.Mutex(false, mutextName); mtx.WaitOne(); //open file ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, BusinessConst.LOG_ST); IEnumerable <System.IO.FileInfo> targetCsvfiles = ImportCommon.GetCsvFile(strFilePath); Console.WriteLine("start loop "); //import data foreach (System.IO.FileInfo targetCsvfile in targetCsvfiles) { iFileCnt++; strfileName = targetCsvfile.Name; ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "File name:" + targetCsvfile.Name); Console.WriteLine("strfileName : {0}", strfileName); //write import data List <string> accRecords = ImportCommon.WriteCsvFile(targetCsvfile); var wkLine = ImportCommon.TrimDoubleQuotationMarks(accRecords[1].Replace("\",\"", "\t")).Split('\t'); Console.WriteLine("create customer : "); string comId = wkLine[25]; if (wkLine[27].Equals("Tenant")) { comId = comId.Replace(",", ""); } if (SanODataQuerys.CheckByCustomId(context, comId)) { SanODataChangesets.CreateCustomAdv(wkLine, context); } else { Console.WriteLine("customer aleady exist: "); SanODataChangesets.AddCustomAddress(wkLine, context); } Console.WriteLine("create journal : "); SanODataChangesets.CreateGeneralJournalAdv(accRecords, context, logMsg); Console.WriteLine("create done !!"); //move file ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "Done:" + strfileName); ImportCommon.MoveCsvFile(strFilePath + "/" + strfileName, strSuccessPath + "/" + strfileName); } ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "File count:" + iFileCnt); ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, BusinessConst.LOG_EN); } catch (System.Exception e) { Console.WriteLine(e.Message); Console.WriteLine("err somthing : {0}", e.InnerException); //move file ImportCommon.Writelog(logMsg, BusinessConst.LOG_ERROR, "Detail1:" + e.Message); ImportCommon.Writelog(logMsg, BusinessConst.LOG_ERROR, "Detail2:" + e.InnerException); ImportCommon.MoveCsvFile(strFilePath + "/" + strfileName, strErrorPath + "/" + strfileName); SendEmail email = new SendEmail(emailConf); email.SendException(e, strLogName); if (mtx != null) { mtx.ReleaseMutex(); mtx = null; } } finally { Console.WriteLine("check log : {0} ", logMsg.ToString()); if (iFileCnt > 0) { ImportCommon.CreatelogFile(strLogPath + "/" + strLogName, logMsg.ToString()); } Console.WriteLine("end "); if (mtx != null) { mtx.ReleaseMutex(); } } }
public static void CreateGeneralJournalAdv(List <string> accRecords, Resources context, StringBuilder logMsg) { for (int i = 1; i < accRecords.Count; i++) { ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "Line" + i + ":" + ImportCommon.TrimDoubleQuotationMarks(accRecords[i].Replace("\",\"", "\t"))); var wkLine = ImportCommon.TrimDoubleQuotationMarks(accRecords[i].Replace("\",\"", "\t")).Split('\t'); string str1 = wkLine[0]; //Order No string str2 = wkLine[1]; //Advanced Payment ID string str3 = wkLine[2]; //Submit Date string str4 = wkLine[3]; //Payment Date string str5 = wkLine[4]; //Payment Time string str6 = wkLine[5]; //Total Amount string str7 = wkLine[6]; //Redac Division string str8 = wkLine[7]; //Parent Redac Division string str9 = wkLine[8]; //Tenant Name string str10 = wkLine[9]; //Company Name string str11 = wkLine[10]; //Street string str12 = wkLine[11]; //Street2 string str13 = wkLine[12]; //Street3 string str14 = wkLine[13]; //City string str15 = wkLine[14]; //US State string str16 = wkLine[15]; //ZIP/Postal code string str17 = wkLine[16]; //Country string str18 = wkLine[17]; //Main Phone string str19 = wkLine[18]; //Fax string str20 = wkLine[19]; //Email string str21 = wkLine[20]; //Purpose string str22 = wkLine[21]; //Amount string str23 = wkLine[22]; //Advance/Expense string str24 = wkLine[23]; //Payable to string str25 = wkLine[24]; //Payment Method string str26 = wkLine[25]; //Company ID string str27 = wkLine[26]; //Type string str28 = wkLine[27]; //CustomerGroup string strvendNum = ""; string strInvoice = str2 + "-" + i; Console.WriteLine("create vendor : "); strvendNum = SanODataQuerys.CheckByVendorId(context, str2, str24); if (strvendNum.Length == 0) { strvendNum = CreateVendor(wkLine, context, i); } else { Console.WriteLine("vendor aleady exist: "); } //Import to AX DataServiceCollection <LedgerJournalHeader> generalJournalCollection = new DataServiceCollection <LedgerJournalHeader>(context); //create General Journal header LedgerJournalHeader generalJournal = new LedgerJournalHeader(); generalJournalCollection.Add(generalJournal); generalJournal.JournalName = ConfigurationManager.AppSettings["JournalName"]; //Description [Advanced Payment ID]+[Line#] / [Submit Date] / [Amount] / [Purpose] string strDescription = strInvoice + "/" + str3.Replace("/", "-") + "/" + decimal.Parse(str22).ToString("0.00") + "/" + str21; generalJournal.Description = strDescription; generalJournal.DataAreaId = ConfigurationManager.AppSettings["RedacCompany"]; DataServiceResponse res1 = null; res1 = context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); string strJournalbatch = ""; foreach (ChangeOperationResponse change in res1) { // Get the descriptor for the entity. EntityDescriptor descriptor = change.Descriptor as EntityDescriptor; if (descriptor != null) { LedgerJournalHeader LedgerJournalres = descriptor.Entity as LedgerJournalHeader; if (LedgerJournalres != null) { strJournalbatch = LedgerJournalres.JournalBatchNumber; Console.WriteLine("New JournalBatchNumber {0}.", strJournalbatch); } } } //create General Journal line DataServiceCollection <LedgerJournalLine> generalJournalLineCollection = new DataServiceCollection <LedgerJournalLine>(context); LedgerJournalLine generalJournalLine = new LedgerJournalLine(); LedgerJournalLine generalJournalLine2 = new LedgerJournalLine(); generalJournalLineCollection.Add(generalJournalLine); generalJournalLineCollection.Add(generalJournalLine2); generalJournalLine.TransDate = Convert.ToDateTime(str3); generalJournalLine.AccountType = LedgerJournalACType.Cust; generalJournalLine.AccountDisplayValue = ImportCommon.Get20CustomerNum(str26).Replace(",", "").Replace("-", "\\-"); generalJournalLine.Text = str21; generalJournalLine.JournalBatchNumber = strJournalbatch; generalJournalLine.Company = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine.DebitAmount = Convert.ToDecimal(str22); generalJournalLine.OffsetAccountType = LedgerJournalACType.Ledger; generalJournalLine.OffsetAccountDisplayValue = "----"; generalJournalLine.OffsetCompany = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine.Invoice = strInvoice; generalJournalLine.CurrencyCode = ConfigurationManager.AppSettings["Currency"]; generalJournalLine.DataAreaId = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine.PostingProfile = ConfigurationManager.AppSettings["AdvancedPostingCustomer"]; Console.WriteLine(string.Format("LedgerJournalACType.Vend {0} - !", (int)(LedgerJournalACType.Vend))); generalJournalLine2.AccountType = LedgerJournalACType.Vend; generalJournalLine2.AccountDisplayValue = strvendNum.Replace("-", "\\-"); generalJournalLine2.JournalBatchNumber = strJournalbatch; generalJournalLine2.Company = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine2.CreditAmount = Convert.ToDecimal(str22); generalJournalLine2.OffsetAccountType = LedgerJournalACType.Ledger; generalJournalLine2.OffsetAccountDisplayValue = "----"; generalJournalLine2.PaymentMethod = ConfigurationManager.AppSettings[str25]; generalJournalLine2.DueDate = Convert.ToDateTime(str4); generalJournalLine2.OffsetCompany = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine2.Invoice = strInvoice; generalJournalLine2.CurrencyCode = ConfigurationManager.AppSettings["Currency"]; generalJournalLine2.DataAreaId = ConfigurationManager.AppSettings["RedacCompany"]; generalJournalLine2.PostingProfile = ConfigurationManager.AppSettings["AdvancedPostingVendor"]; context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Batch with Single Changeset ensure the saved changed runs in all-or-nothing mode. Console.WriteLine(string.Format("Invoice {0} - Saved !", strJournalbatch)); //SimpleCRUDExamples.GjUpd(context, strJournalbatch); //create General Journal line /* * DataServiceCollection<GeneralLedgerCustInvoiceJournalLine> generalInvoiceCollection = new DataServiceCollection<GeneralLedgerCustInvoiceJournalLine>(context); * GeneralLedgerCustInvoiceJournalLine generalInvoiceLine = new GeneralLedgerCustInvoiceJournalLine(); * generalInvoiceCollection.Add(generalInvoiceLine); * * Console.WriteLine(string.Format("approve Line start - !")); * generalInvoiceLine.JournalBatchNumber = strJournalbatch; * generalInvoiceLine.ApprovedBy = "000728"; * generalInvoiceLine.Approved = NoYes.Yes; * * context.SaveChanges(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset); // Batch with Single Changeset ensure the saved changed runs in all-or-nothing mode. * Console.WriteLine(string.Format("VendInvoice {0} - Saved !", strJournalbatch)); */ } }