public List <PepperiImportDetailTemp> MapFileImportDetailToTemp(string businessName,
                                                                        PepperiTransactionTempModel.SalesTransactionTransactionLine[] tls,
                                                                        PepperiTransactionTempModel.SalesTransactionTransactionHeader th,
                                                                        UserModel taskUser,
                                                                        ScheduledTask task)
        {
            List <PepperiImportDetailTemp> details = new List <PepperiImportDetailTemp>();
            var lineNumber = 0;

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

            if (company != null)
            {
                if (tls.Length != 0)
                {
                    foreach (PepperiTransactionTempModel.SalesTransactionTransactionLine tl in tls)
                    {
                        var pidt = new PepperiImportDetailTemp();
                        pidt.CompanyId = company.Id;

                        var product = ProductService.FindProductModel(tl.ItemFields.ItemExternalID, null, company, false);
                        // Stop import if product is false
                        pidt.ProductId                    = product.Id;
                        pidt.BrandCategoryId              = ProductService.FindProductBrandCategoryModel(company.Id, pidt.ProductId.Value).Id;
                        pidt.ItemWrntyId                  = tl.ItemFields.ItemWrntyID;
                        pidt.ItemExternalId               = tl.ItemFields.ItemExternalID;
                        pidt.ItemMainCategory             = tl.ItemFields.ItemMainCategory;
                        pidt.ItemMainCategoryCode         = tl.ItemFields.ItemMainCategoryCode;
                        pidt.ItemName                     = tl.ItemFields.ItemName.Replace(",", "").Replace("'", "");
                        pidt.ItemPrice                    = tl.ItemFields.ItemPrice;
                        pidt.ItemInStockQuantity          = tl.ItemFields.ItemInStockQuantity;
                        pidt.TSANextAvailableDate         = (tl.ItemFields.TSANextAvailableDate == "") ? (DateTimeOffset?)null : DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(tl.ItemFields.TSANextAvailableDate), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                        pidt.TSATotalAvailable            = (string.IsNullOrEmpty(tl.ItemFields.TSATotalAvailable)) ? 0 : Convert.ToInt32(tl.ItemFields.TSATotalAvailable);
                        pidt.TSADuePDF                    = tl.TransactionLineCustomFields.TSADuePDF as string;
                        pidt.TSALineAmount                = tl.TransactionLineCustomFields.TSALineAmount;
                        pidt.UnitsQuantity                = tl.TransactionLineFields.UnitsQuantity;
                        pidt.UnitPrice                    = tl.TransactionLineFields.UnitPrice;
                        pidt.UnitDiscountPercentage       = tl.TransactionLineFields.UnitDiscountPercentage;
                        pidt.UnitPriceAfterDiscount       = tl.TransactionLineFields.UnitPriceAfterDiscount;
                        pidt.TotalUnitsPriceAfterDiscount = tl.TransactionLineFields.TotalUnitsPriceAfterDiscount;
                        pidt.DeliveryDate                 = DateTimeOffsetExtensions.ParseDate(DateTimeOffset.Parse(tl.TransactionLineFields.DeliveryDate), (TimeZoneInfo.Local.BaseUtcOffset.Hours * 60));
                        pidt.TransactionWrntyId           = tl.TransactionLineFields.TransactionWrntyID;
                        pidt.TransactionExternalId        = (tl.TransactionLineFields.TransactionExternalID as string == null) ? (long?)null : Convert.ToInt64(tl.TransactionLineFields.TransactionExternalID);
                        pidt.LineNumber                   = lineNumber;
                        pidt.TaxCodeId                    = customer.TaxCodeId;
                        pidt.DiscountPercent              = tl.TransactionLineFields.UnitDiscountPercentage;
                        details.Add(pidt);

                        lineNumber += 100;
                    }
                }
            }
            else
            {
                TaskService.WriteTaskLog(task, $"Error: Failed to find company '{businessName}' - @MapFileImportDetailToTemp", LogSeverity.Severe);
            }

            return(details);
        }
        private CustomerModel GetCustomer(CompanyModel company, PepperiTransactionTempModel.SalesTransactionTransactionHeader transactionHeader, UserModel taskUser)
        {
            CustomerModel customer = CustomerService.FindCustomerModel(company.Id, transactionHeader.AccountFields.AccountName);

            if (customer == null)
            {
                customer             = new CustomerModel();
                customer.CompanyId   = company.Id;
                customer.Name        = transactionHeader.AccountFields.AccountName;
                customer.CreatedDate = DateTime.Now;
                customer.CreatedById = (int)GetSalespersonId(transactionHeader.SalesRepFields.AgentEmail);

                CustomerService.SetCustomerDefaults(company,
                                                    customer,
                                                    transactionHeader.AccountFields.AccountCountry,
                                                    transactionHeader.AccountFields.AccountZipCode);
                CustomerService.InsertOrUpdateCustomer(customer, taskUser);
            }
            return(customer);
        }
        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);
        }