public SalesOrderHeader CopyTempDataToProduction(PepperiImportHeaderTemp piht, ICollection <PepperiImportDetailTemp> pidt, ScheduledTask task)
        {
            SalesOrderHeader salesOrderHeader = null;

            try {
                Customer customer = db.FindCustomer(piht.CompanyId, piht.AccountName);
                salesOrderHeader = MapTempToSalesHeader(piht, task, customer);
                salesOrderHeader.SalesOrderDetails = MapTempToSalesDetail(pidt, task, customer);
            } catch (Exception ex) {
                TaskService.WriteTaskLog(task, ex.Message, LogSeverity.Severe);
            }

            if (salesOrderHeader != null && salesOrderHeader.SalesOrderDetails.Count > 0)
            {
                try {
                    db.SavePepperiData(salesOrderHeader, salesOrderHeader.SalesOrderDetails);
                } catch (Exception ex) {
                    TaskService.WriteTaskLog(task, ex.Message, LogSeverity.Severe);
                }
            }
            else
            {
                TaskService.WriteTaskLog(task, $"Error. Couldn't save Pepperi data to production tables. No data in either table (Header or Details) to process - @CopyTempDataToProduction", LogSeverity.Severe);
            }
            return(salesOrderHeader);
        }
        public bool ProcessTransaction(string businessName, PepperiTransactionTempModel.SalesTransaction transaction, UserModel taskUser, ScheduledTask task)
        {
            PepperiImportHeaderTemp        transactionHeader  = null;
            List <PepperiImportDetailTemp> transactionDetails = new List <PepperiImportDetailTemp>();

            try {
                db.CleanPepperiImportTempTables();
                transactionHeader  = MapFileImportHeaderToTemp(businessName, transaction.TransactionHeader, taskUser, task);
                transactionDetails = MapFileImportDetailToTemp(businessName, transaction.TransactionLines, transaction.TransactionHeader, taskUser, task);
                transactionHeader.BrandCategoryId = transactionDetails[0].BrandCategoryId;
            } catch (Exception ex) {
                TaskService.WriteTaskLog(task, $"Error: @ProcessTransaction\r\n{ex.Message}", LogSeverity.Severe);
                return(false);
            }

            if (transactionHeader != null && transactionDetails.Count > 0)
            {
                db.InsertPepperiImportFile(transactionHeader, transactionDetails);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private SalesOrderHeader MapTempToSalesHeader(PepperiImportHeaderTemp piht, ScheduledTask task, Customer customer)
        {
            SalesOrderHeader salesOrderHeader = null;

            if (customer != null)
            {
                salesOrderHeader                     = new SalesOrderHeader();
                salesOrderHeader.CompanyId           = piht.CompanyId;
                salesOrderHeader.SourceId            = piht.SourceId;
                salesOrderHeader.CustPO              = piht.WrntyId.ToString();
                salesOrderHeader.CustomerId          = piht.CustomerId;
                salesOrderHeader.OrderNumber         = piht.OrderNumber;
                salesOrderHeader.OrderDate           = piht.CreationDateTime;
                salesOrderHeader.RequiredDate        = piht.DeliveryDate;
                salesOrderHeader.ShipAddress1        = piht.ShipToStreet;
                salesOrderHeader.ShipSuburb          = piht.ShipToCity;
                salesOrderHeader.ShipState           = piht.ShipToState;
                salesOrderHeader.ShipPostcode        = piht.ShipToZipCode;
                salesOrderHeader.ShipCountryId       = (int)piht.ShipToCountryId;
                salesOrderHeader.SOStatus            = piht.SOStatus;
                salesOrderHeader.SOSubstatus         = piht.SOSubStatus;
                salesOrderHeader.SalespersonId       = (int)piht.SalespersonId;
                salesOrderHeader.OrderComment        = piht.Remark;
                salesOrderHeader.LocationId          = piht.LocationId;
                salesOrderHeader.TermsId             = customer.PaymentTermId;
                salesOrderHeader.ShippingMethodId    = customer.ShippingMethodId;
                salesOrderHeader.DeliveryWindowOpen  = piht.TSADeliveryWindowOpen;
                salesOrderHeader.DeliveryWindowClose = piht.TSADeliveryWindowClose;
                salesOrderHeader.ManualDWSet         = false;
                salesOrderHeader.ShipMethodAccount   = customer.ShipMethodAccount;
                salesOrderHeader.NextActionId        = piht.NextActionId;
                salesOrderHeader.IsConfirmedAddress  = piht.IsConfirmedAddress;
                salesOrderHeader.IsManualFreight     = customer.IsManualFreight ?? false;
                salesOrderHeader.FreightRate         = customer.FreightRate;
                salesOrderHeader.MinFreightPerOrder  = customer.MinFreightPerOrder;
                salesOrderHeader.FreightCarrierId    = customer.FreightCarrierId;
                salesOrderHeader.DeliveryContact     = customer.DeliveryContact;
                salesOrderHeader.SignedBy            = piht.SignedBy;
                salesOrderHeader.DateSigned          = piht.CreationDateTime;
                salesOrderHeader.MethodSignedId      = piht.MethodSignedId;
                //salesOrderHeader.PrintedForm = customer.PrintedForm; // **
                salesOrderHeader.WarehouseInstructions = customer.WarehouseInstructions;
                salesOrderHeader.DeliveryInstructions  = customer.DeliveryInstructions;
                salesOrderHeader.IsMSQProblem          = piht.IsMSQProblem;
                salesOrderHeader.IsOverrideMSQ         = piht.IsOverrideMSQ;
                salesOrderHeader.FreightTermId         = customer.FreightTermId;
                salesOrderHeader.IsProcessed           = false;
                salesOrderHeader.IsRetailSale          = false;
                salesOrderHeader.IsRetailHoldingOrder  = false;
                salesOrderHeader.OrderTypeId           = customer.OrderTypeId;
                salesOrderHeader.BrandCategoryId       = piht.BrandCategoryId;
                salesOrderHeader.DateCreated           = DateTimeOffset.Now;
            }
            return(salesOrderHeader);
        }
        public PepperiImportHeaderTemp GetTempTableData(ScheduledTask task)
        {
            PepperiImportHeaderTemp piht     = new PepperiImportHeaderTemp();
            SalesOrderHeader        soHeader = new SalesOrderHeader();

            try {
                piht = db.FindPepperiImportHeaderTempRecord();
            } catch (Exception ex) {
                TaskService.WriteTaskLog(task, $"Error: There was an error retreiving data from the TEMP tables - @GetTempTableData" + ex.Message, LogSeverity.Severe);
            }
            return(piht);
        }
        public bool ProcessXml(string fileName, string businessName, UserModel taskUser, ScheduledTask task)
        {
            TaskService.WriteTaskLog(task, $"Success: Processing file '{fileName}'", LogSeverity.Normal);

            var transaction = ReadFile(fileName, task);

            // Check if the file has data
            if (transaction.TransactionHeader.TransactionHeaderFields != null && transaction.TransactionHeader != null && transaction != null)
            {
                if (!string.IsNullOrEmpty(transaction.TransactionHeader.AccountFields.AccountName))
                {
                    TaskService.WriteTaskLog(task, $"Success: Found customer: {transaction.TransactionHeader.AccountFields.AccountName}", LogSeverity.Normal);
                }

                if (ProcessTransaction(businessName, transaction, taskUser, task))
                {
                    TaskService.WriteTaskLog(task, $"Success: Saved to TEMP tables - {transaction.TransactionLines.Count()} lines processed", LogSeverity.Normal);

                    PepperiImportHeaderTemp piht = GetTempTableData(task);
                    if (piht != null && piht.PepperiImportDetailTemps.Count > 0)
                    {
                        CopyTempDataToProduction(piht, piht.PepperiImportDetailTemps, task);
                        TaskService.WriteTaskLog(task, $"Success: Order {piht.OrderNumber} successfully saved to SALES tables", LogSeverity.Normal);
                        return(true);
                    }
                    else
                    {
                        TaskService.WriteTaskLog(task, $"Error. One and or both TEMP tables contain no data", LogSeverity.Severe);
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                TaskService.WriteTaskLog(task, $"Error: The file '{fileName}' you are trying to import is empty / has no data", LogSeverity.Severe);
                return(false);
            }
        }
        public PepperiImportHeaderTemp MapFileImportHeaderToTemp(string businessName,
                                                                 PepperiTransactionTempModel.SalesTransactionTransactionHeader th,
                                                                 UserModel taskUser,
                                                                 ScheduledTask task)
        {
            PepperiImportHeaderTemp piht = new PepperiImportHeaderTemp();

            var company  = CompanyService.FindCompanyFriendlyNameModel(businessName);
            var customer = GetCustomer(company, th, taskUser);

            if (company != null)
            {
                piht.CompanyId                  = company.Id;
                piht.WrntyId                    = Convert.ToInt32(th.TransactionHeaderFields.WrntyID);
                piht.OrderType                  = th.TransactionHeaderFields.Type;
                piht.Status                     = th.TransactionHeaderFields.Status;
                piht.CreationDateTime           = DateTimeOffset.Parse(th.TransactionHeaderFields.CreationDateTime);
                piht.ModificationDateTime       = DateTimeOffset.Parse(th.TransactionHeaderFields.ModificationDateTime);
                piht.ActionDateTime             = DateTimeOffset.Parse(th.TransactionHeaderFields.ActionDateTime);
                piht.DeliveryDate               = DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(th.TransactionHeaderFields.DeliveryDate), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                piht.Remark                     = th.TransactionHeaderFields.Remark as string;
                piht.CatalogId                  = th.CatalogFields.CatalogID;
                piht.CatalogDescription         = th.CatalogFields.CatalogDescription as string;
                piht.CatalogPriceFactor         = th.CatalogFields.CatalogPriceFactor;
                piht.CatalogExpirationDate      = DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(th.CatalogFields.CatalogExpirationDate), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                piht.AgentName                  = th.SalesRepFields.AgentName;
                piht.AgentExternalId            = (th.SalesRepFields.AgentExternalID as string == null) ? 0 : Convert.ToInt64(th.SalesRepFields.AgentExternalID);
                piht.AgentEmail                 = th.SalesRepFields.AgentEmail;
                piht.AccountWrntyId             = th.AccountFields.AccountWrntyID;
                piht.AccountExternalId          = th.AccountFields.AccountExternalID;
                piht.AccountCreationDate        = DateTimeOffset.Parse(th.AccountFields.AccountCreationDate);
                piht.AccountName                = th.AccountFields.AccountName;
                piht.AccountPhone               = th.AccountFields.AccountPhone;
                piht.AccountMobile              = th.AccountFields.AccountMobile as string;
                piht.AccountFax                 = th.AccountFields.AccountFax as string;
                piht.AccountEmail               = th.AccountFields.AccountEmail;
                piht.CustomerId                 = customer.Id;
                piht.AccountStreet              = th.AccountFields.AccountStreet;
                piht.AccountCity                = th.AccountFields.AccountCity;
                piht.AccountState               = th.AccountFields.AccountState;
                piht.AccountCountryId           = LookupService.FindCountryModel(th.AccountFields.AccountCountry).Id;
                piht.AccountCountry             = th.AccountFields.AccountCountry;
                piht.AccountZipCode             = th.AccountFields.AccountZipCode;
                piht.AccountPriceLevelName      = th.AccountFields.AccountPriceLevelName as string;
                piht.BillToName                 = th.BillingFields.BillToName;
                piht.BillToStreet               = th.BillingFields.BillToStreet;
                piht.BillToCity                 = th.BillingFields.BillToCity;
                piht.BillToState                = th.BillingFields.BillToState;
                piht.BillToCountryId            = LookupService.FindCountryModel(th.BillingFields.BillToCountry).Id;
                piht.BillToCountry              = th.BillingFields.BillToCountry;
                piht.BillToZipCode              = th.BillingFields.BillToZipCode;
                piht.BillToPhone                = th.BillingFields.BillToPhone;
                piht.ShipToExternalId           = th.ShippingFields.ShipToExternalID;
                piht.ShipToName                 = th.ShippingFields.ShipToName;
                piht.ShipToStreet               = th.ShippingFields.ShipToStreet;
                piht.ShipToCity                 = th.ShippingFields.ShipToCity;
                piht.ShipToState                = th.ShippingFields.ShipToState;
                piht.ShipToCountryId            = LookupService.FindCountryModel(th.ShippingFields.ShipToCountry).Id;
                piht.ShipToCountry              = th.ShippingFields.ShipToCountry;
                piht.ShipToZipCode              = th.ShippingFields.ShipToZipCode;
                piht.ShipToPhone                = th.ShippingFields.ShipToPhone;
                piht.Currency                   = th.Totals.Currency;
                piht.TotalItemsCount            = th.Totals.TotalItemsCount;
                piht.SubTotal                   = th.Totals.SubTotal;
                piht.SubTotalAfterItemsDiscount = th.Totals.SubTotalAfterItemsDiscount;
                piht.GrandTotal                 = th.Totals.GrandTotal;
                piht.DiscountPercentage         = th.Totals.DiscountPercentage;
                piht.TaxPercentage              = th.Totals.TaxPercentage;
                piht.TSAGST                     = th.TransactionCustomFields.TSAGST;
                piht.TSADeliveryWindowOpen      = (th.TransactionCustomFields.TSADeliveryWindowOpen == "") ? DateTimeOffset.Parse(th.TransactionHeaderFields.CreationDateTime) : DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(th.TransactionCustomFields.TSADeliveryWindowOpen), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                piht.TSADeliveryWindowClose     = (th.TransactionCustomFields.TSADeliveryWindowClose == "") ? LookupService.GetDeliveryWindow(piht.TSADeliveryWindowOpen.Value) : DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(th.TransactionCustomFields.TSADeliveryWindowClose), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                piht.TSAOrderTakenBy            = (th.TransactionCustomFields.TSAOrderTakenBy as string == null) ? th.TransactionCustomFields.TSAOrderTakenBy as string : th.TransactionCustomFields.TSAOrderTakenBy.ToString();
                piht.TSATaxRate                 = th.TransactionCustomFields.TSATaxRate;
                piht.TSASubTotalBeforeTax       = th.TransactionCustomFields.TSASubTotalBeforeTax;
                piht.TSAGrandTotal              = th.TransactionCustomFields.TSAGrandTotal;
                piht.SalespersonId              = GetSalespersonId(th.SalesRepFields.AgentEmail);
                piht.Filespec                   = th.TransactionCustomFields.Filespec;
                piht.IsNewCustomer              = th.TransactionCustomFields.IsNewCustomer;
                piht.OrderNumber                = (int)LookupService.GetNextSequenceNumber(company, SequenceNumberType.SalesOrderNumber);
                piht.SOStatus                   = (int)SalesOrderHeaderStatus.ConfirmedOrder;
                piht.SOSubStatus                = (int)SalesOrderHeaderSubStatus.Unpicked;
                piht.LocationId                 = company.DefaultLocationID.Value;
                piht.IsConfirmedAddress         = (th.TransactionCustomFields.IsNewCustomer == true) ? false : true;
                piht.SignedBy                   = "Customer";
                piht.MethodSignedId             = LookupService.FindMethodSignedModel("Pepperi").Id;
                piht.IsMSQProblem               = false;
                piht.IsOverrideMSQ              = false;
                piht.SourceId                   = LookupService.FindLOVItemModel(LOVName.OrderSource, "Pepperi").Id;
                piht.NextActionId               = LookupService.FindSaleNextActionId(Enumerations.SaleNextAction.None);
            }
            else
            {
                TaskService.WriteTaskLog(task, $"Error: Failed to find company '{businessName}' - @MapFileImportHeaderToTemp", LogSeverity.Severe);
            }
            return(piht);
        }