Exemple #1
0
        private CustomerModel GetCustomer(CompanyModel company, string customerName, ShopifyImportTempModel.DataOrder order, UserModel taskUser)
        {
            CustomerModel customer = CustomerService.FindCustomerModel(company.Id, customerName);

            if (customer == null)
            {
                customer             = new CustomerModel();
                customer.CompanyId   = company.Id;
                customer.Name        = customerName;
                customer.CreatedDate = DateTime.Now;
                // NEED MORE DEFAULTS??
                //customer.CreatedById = ??

                CustomerService.SetCustomerDefaults(company, customer, order.shipping_address.country, order.shipping_address.postcode.ToString());
                CustomerService.InsertOrUpdateCustomer(customer, taskUser);
            }
            return(customer);
        }
Exemple #2
0
        public ShopifyImportHeaderTemp MapOrderToTemp(string businessName, Dictionary <string, string> configDetails, ShopifyImportTempModel.DataOrder order, UserModel taskUser)
        {
            CompanyModel company = CompanyService.FindCompanyFriendlyNameModel(businessName);

            if (order == null)
            {
                return(null);
            }
            else
            {
                if (company != null)
                {
                    CustomerModel customer = GetCustomer(company, configDetails["CustomerName"], order, taskUser);

                    ShopifyImportHeaderTemp temp = new ShopifyImportHeaderTemp();
                    temp.CompanyId   = company.Id;
                    temp.CustomerId  = customer.Id;
                    temp.SOStatus    = (int)SalesOrderHeaderStatus.ConfirmedOrder;
                    temp.SOSubstatus = (int)SalesOrderHeaderSubStatus.Unpicked;
                    temp.LocationId  = company.DefaultLocationID;
                    temp.TaxCodeId   = customer.TaxCodeId;

                    // Grab the first product tax and use, if its 0 use the customer card
                    decimal taxRate = 0;
                    if (order.SalesOrderItems[0].tax_percent != null && order.SalesOrderItems[0].tax_percent != 0)
                    {
                        taxRate = order.SalesOrderItems[0].tax_percent.Value;
                    }
                    else
                    {
                        taxRate = LookupService.FindTaxCodeModel(customer.TaxCodeId).TaxPercentageRate.Value;
                    }

                    temp.FreightCarrierId = customer.FreightCarrierId;
                    temp.MethodSignedId   = LookupService.FindMethodSignedModel("Shopify").Id;
                    temp.FreightTermId    = null;
                    var product = ProductService.FindProductModel(order.SalesOrderItems[0].sku, null, company, false);
                    temp.BrandCategoryId = ProductService.FindBrandCategoriesListModel(company.Id, 0, 1, 1, configDetails["BrandCategory"]).Items.FirstOrDefault().Id;
                    temp.SourceId        = LookupService.FindLOVItemModel(LOVName.OrderSource, configDetails["DataSource"]).Id;
                    temp.StoreId         = Convert.ToInt32(configDetails["StoreId"]);

                    var salesperson = CustomerService.FindBrandCategorySalesPersonsModel(company, customer, temp.BrandCategoryId.Value, SalesPersonType.AccountManager).FirstOrDefault();
                    if (salesperson != null)
                    {
                        temp.SalesPersonId = salesperson.UserId;
                    }
                    else
                    {
                        temp.SalesPersonId = company.DefaultImportUserId;
                    }

                    temp.TermsId              = customer.PaymentTermId;
                    temp.ShippingMethodId     = customer.ShippingMethodId;
                    temp.NextActionId         = LookupService.FindSaleNextActionId(Enumerations.SaleNextAction.None);
                    temp.OrderTypeId          = LookupService.FindLOVItemsModel(company, LOVName.OrderType).FirstOrDefault().Id;
                    temp.WebsiteTransactionId = order.ShopifyOrderId.ToString();
                    temp.OrderNumber          = null;
                    temp.CustomerPO           = order.OrderNumber.ToString();
                    string dateFormat = "yyyyMMddHHmmss";
                    temp.OrderDate    = DateTimeOffset.ParseExact(order.OrderDate.ToString(), dateFormat, CultureInfo.InvariantCulture);
                    temp.RequiredDate = DateTimeOffset.ParseExact(order.OrderDate.ToString(), dateFormat, CultureInfo.InvariantCulture);
                    temp.CompanyName  = order.shipping_address.company.ToString();
                    temp.EndUserName  = order.shipping_address.name;

                    // Shipping Address
                    temp.ShipAddress1  = order.shipping_address.address1;
                    temp.ShipAddress2  = order.shipping_address.address2;
                    temp.ShipAddress3  = null;
                    temp.ShipAddress4  = null;
                    temp.ShipSuburb    = order.shipping_address.city;
                    temp.ShipState     = order.shipping_address.state;
                    temp.ShipPostcode  = order.shipping_address.postcode;
                    temp.ShipCountryId = LookupService.FindCountryModel(order.shipping_address.country.ToString()).Id;
                    temp.Telephone     = order.shipping_address.telephone;
                    temp.EmailAddress  = order.shipping_address.email.ToString();

                    // SalesOrderItems
                    int lineNumber = 0;
                    foreach (var item in order.SalesOrderItems)
                    {
                        ShopifyImportDetailTemp detail = new ShopifyImportDetailTemp();
                        detail.CompanyId          = company.Id;
                        detail.WebsiteLineItemId  = item.id.ToString();
                        detail.LineNumber         = lineNumber;
                        detail.ProductDescription = item.name;
                        detail.ProductNumber      = item.sku;
                        product                = ProductService.FindProductModel(item.sku, null, company, false);
                        detail.ProductId       = product.Id;
                        detail.OrderQty        = item.quantity;
                        detail.TaxPercent      = (item.tax_percent == null) ? 0 : item.tax_percent;
                        detail.TaxCodeId       = customer.TaxCodeId;
                        detail.UnitPriceExTax  = Math.Round((item.price / (1 + (Convert.ToDecimal(detail.TaxPercent)))), 2); // CHECK - Wrong calculations?
                        detail.UnitPriceTax    = (item.tax == null) ? 0 : item.tax;
                        detail.Discount        = item.discount;
                        detail.DiscountPercent = item.discount_percent;

                        detail.TaxCodeId        = customer.TaxCodeId;
                        detail.AllocQty         = 0;
                        detail.PickQty          = 0;
                        detail.InvQty           = 0;
                        detail.LineStatusId     = (int)SalesOrderLineStatus.Unpicked;
                        detail.DateCreated      = DateTimeOffset.Now;
                        detail.DateModified     = DateTimeOffset.Now;
                        detail.ReallocationItem = true;

                        temp.ShopifyImportDetailTemps.Add(detail);
                        lineNumber += 100;
                    }

                    // Discount
                    if (order.Discount != null)
                    {
                        temp.WholeOrderDiscount = Math.Abs(order.Discount.price);
                        // add new line - lookup product table with "#DSPR" -> create a new product() -> lineNumber += 100
                    }

                    // Shipping
                    temp.FreightGST   = 0;
                    temp.FreightExGST = 0;
                    if (order.Shipping != null)
                    {
                        int index = order.Shipping.label.IndexOf("(");
                        if (taxRate != 0)
                        {
                            temp.FreightGST   = Math.Round((order.Shipping.price / (Convert.ToDecimal(1 + (taxRate / 100)) * 10)), 2);
                            temp.FreightExGST = Math.Round((order.Shipping.price - Convert.ToDecimal(temp.FreightGST)), 2);
                        }
                        if (index == -1)
                        {
                            temp.FreightLabel = order.Shipping.label;
                        }
                        else
                        {
                            temp.FreightLabel = order.Shipping.label.Substring(0, index).Trim();
                        }
                    }

                    // SalesOrderExtraItems
                    if (order.SalesOrderExtraItems != null)
                    {
                        foreach (var orderitem in order.SalesOrderExtraItems)
                        {
                            if (orderitem.label == "DISCOUNT")
                            {
                                temp.WholeOrderDiscount = orderitem.price;
                            }
                            else if (orderitem.label.Contains("Shipment") || orderitem.label.Contains("Shipping"))
                            {
                                int index = orderitem.label.IndexOf("(");

                                if (taxRate == 0)
                                {
                                    temp.FreightExGST = temp.FreightExGST + orderitem.price;
                                }
                                else
                                {
                                    temp.FreightGST   = Math.Round((orderitem.price / (Convert.ToDecimal(1 + (taxRate / 100)) * 10)), 2);
                                    temp.FreightExGST = Math.Round((orderitem.price - Convert.ToDecimal(temp.FreightGST)), 2);
                                }

                                if (index == -1)
                                {
                                    temp.FreightLabel = orderitem.label;
                                }
                                else
                                {
                                    temp.FreightLabel = orderitem.label.Substring(0, index).Trim();
                                }
                            }
                            else
                            {
                                temp.WholeOrderDiscount = 0;
                            }
                        }
                    }

                    // Comments
                    if (order.Comments != null)
                    {
                        if (order.Comments.ToString().Contains("eBay"))
                        {
                            temp.OrderComment = order.Comments.Comment.Replace(",", "").Replace("\"", "").Replace(System.Environment.NewLine, ""); // May need to truncate this string (nvarchar(255))
                            temp.Comments     = "";
                        }
                        else
                        {
                            temp.Comments     = order.Comments.Comment.Replace(",", "").Replace("\"", "").Replace(System.Environment.NewLine, ""); // May need to truncate this string (nvarchar(255))
                            temp.OrderComment = "";
                        }
                    }

                    temp.WebsiteOrderNo        = order.OrderNumber.ToString();
                    temp.Filespec              = null;
                    temp.IsError               = false;
                    temp.DeliveryWindowOpen    = temp.OrderDate;
                    temp.DeliveryWindowClose   = temp.DeliveryWindowOpen.Value.AddDays(1);
                    temp.ManualDWSet           = false;
                    temp.ShippingMethodAccount = null;
                    temp.IsConfirmedAddress    = true;
                    temp.IsManualFreight       = true;
                    temp.FreightRate           = 0;
                    temp.MinFreightPerOrder    = 0;
                    temp.DeliveryInstructions  = null;
                    temp.DeliveryContact       = order.shipping_address.name;
                    temp.SignedBy              = "Customer";
                    temp.DateSigned            = temp.OrderDate;
                    temp.WarehouseInstructions = null;
                    temp.IsMSQProblem          = false;
                    temp.EndUserName           = order.shipping_address.name;
                    temp.IsOverrideMSQ         = false;
                    temp.SiteName              = configDetails["DataSource"];
                    temp.IsProcessed           = false;
                    temp.IsRetailSale          = true;
                    temp.IsRetailHoldingOrder  = false;
                    return(temp);
                }
                return(null);
            }
        }