protected void AddNewArticleButton_Click(Object sender, EventArgs e)
        {
            Article  article = Article.LoadSingle(this.NewArticleList.SelectedValue.ToInt32());
            SaleItem newItem = new SaleItem(this.RequestAddOn.Query.Sale, article, ShopperConfiguration.Default.CurrentTaxRate);

            newItem.DecreaseStock();
            MyDataContext.Default.SaleItems.Add(newItem);
            MyDataContext.Default.SaveChanges();

            this.ResponseAddOn.Redirect <Sales.Items.Edit>(new Sales.Items.Edit.Query()
            {
                SaleItem   = newItem,
                SearchTerm = this.RequestAddOn.Query.SearchTerm
            });
        }
        protected void SaveButton_Click(Object sender, EventArgs e)
        {
            SaleItem current = this.RequestAddOn.Query.SaleItem;

            current.IncreaseStock();

            current.ExternalArticleNumber = current.Article.ArticleNumber;
            current.InternalArticleNumber = current.Article.ArticleNumber;
            current.Article          = current.Article;
            current.Amount           = this.AmountTextBox.Text.ToDecimal();
            current.SinglePriceGross = this.SinglePriceTextBox.Text.ToDecimal();
            current.TaxRate          = this.TaxRateTextBox.Text.ToDecimal();

            current.DecreaseStock();
            current.Sale.ManuallyChanged = true;

            MyDataContext.Default.SaveChanges();

            this.ResponseAddOn.Redirect <Sales.Edit>(new Sales.Edit.Query()
            {
                Sale = current.Sale, SearchTerm = this.RequestAddOn.Query.SearchTerm
            });
        }
Example #3
0
        public void SyncToSale(MyMagento.Models.Order order)
        {
            try
            {
                if (order.status != "canceled")
                {
                    Sale sale = Sale.LoadByMagentoIncrementId(order.increment_id.ToString());

                    MyMagento.Models.OrderAddress billingAddress  = order.addresses.Find(x => x.address_type == "billing");
                    MyMagento.Models.OrderAddress shippingAddress = order.addresses.Find(x => x.address_type == "shipping");

                    if (sale == null)
                    {
                        sale = Sale.Create();

                        System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
                        provider.NumberDecimalSeparator = ".";

                        sale.Source       = SaleSources.Magento;
                        sale.SourceId     = order.increment_id.ToString();
                        sale.EbayName     = String.Empty;
                        sale.NameOfBuyer  = billingAddress.firstname + " " + billingAddress.lastname;
                        sale.PhoneNumber  = shippingAddress.telephone ?? String.Empty;
                        sale.EMailAddress = billingAddress.email;
                        Models.MyDataContext.Default.Sales.Add(sale);

                        var itemsWithoutCategories = order.order_items.Where(runner => runner.parent_item_id == null).ToList();                         //Filters out doubles that come in with customizable products such as size or color
                        foreach (MyMagento.Models.OrderItem currentItem in itemsWithoutCategories)
                        {
                            Decimal amount         = Convert.ToDecimal(currentItem.qty_ordered, provider);
                            Decimal totalTax       = Convert.ToDecimal(currentItem.tax_amount, provider);
                            Decimal singleTax      = totalTax / amount;
                            Decimal singlePriceNet = Convert.ToDecimal(currentItem.price, provider);

                            SaleItem saleItem = new SaleItem();
                            saleItem.Sale = sale;
                            saleItem.EbaySalesRecordNumber = -1;
                            saleItem.ExternalArticleName   = currentItem.name;
                            saleItem.ExternalArticleNumber = currentItem.item_id.ToString();
                            saleItem.InternalArticleNumber = currentItem.sku;
                            saleItem.Article             = Article.LoadByArticleNumber(currentItem.sku);
                            saleItem.Amount              = amount;
                            saleItem.TaxRate             = Convert.ToDecimal(currentItem.tax_percent, provider);
                            saleItem.SinglePriceGross    = singleTax + singlePriceNet;
                            saleItem.EbayOrderLineItemID = String.Empty;
                            Models.MyDataContext.Default.SaleItems.Add(saleItem);
                            Models.MyDataContext.Default.SaveChanges();
                            saleItem.DecreaseStock();
                        }
                    }

                    if (sale.CanBeSynced)
                    {
                        System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
                        provider.NumberDecimalSeparator = ".";

                        sale.InvoiceName = billingAddress.firstname + " " + billingAddress.lastname;
                        String[] billingStreetParts = billingAddress.street.Split('\n');
                        sale.InvoiceStreet1  = billingStreetParts.Length >= 1 ? billingStreetParts[0] : String.Empty;
                        sale.InvoiceStreet2  = billingStreetParts.Length >= 2 ? billingStreetParts[1] : String.Empty;
                        sale.InvoiceCity     = billingAddress.city ?? String.Empty;
                        sale.InvoiceRegion   = billingAddress.region ?? String.Empty;
                        sale.InvoicePostcode = billingAddress.postcode ?? String.Empty;
                        sale.InvoiceCountry  = billingAddress.country_id ?? String.Empty;

                        sale.ShippingName = shippingAddress.firstname + " " + shippingAddress.lastname;
                        String[] shippingStreetParts = shippingAddress.street.Split('\n');
                        sale.ShippingStreet1  = shippingStreetParts.Length >= 1 ? shippingStreetParts[0] : String.Empty;
                        sale.ShippingStreet2  = shippingStreetParts.Length >= 2 ? shippingStreetParts[1] : String.Empty;
                        sale.ShippingCity     = shippingAddress.city ?? String.Empty;
                        sale.ShippingRegion   = shippingAddress.region ?? String.Empty;
                        sale.ShippingPostcode = shippingAddress.postcode ?? String.Empty;
                        sale.ShippingCountry  = shippingAddress.country_id ?? String.Empty;

                        // FIXME: There could be a better method to figure out the payment date
                        if (order.total_paid != null && order.order_comments != null && order.order_comments.Count > 0)
                        {
                            var lastComment = order.order_comments[order.order_comments.Count - 1];
                            sale.DateOfPayment = TimeZoneInfo.ConvertTimeToUtc(lastComment.created_at);
                        }
                        else
                        {
                            sale.DateOfPayment = null;
                        }

                        sale.DateOfSale    = TimeZoneInfo.ConvertTimeToUtc(order.created_at);
                        sale.ShippingPrice = Convert.ToDecimal(order.shipping_amount, provider);
                    }

                    MyDataContext.Default.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new SalesImportException(String.Format("Error at Magento-Order-Number: {0} ", order.increment_id),
                                               ex);
            }
        }
Example #4
0
        public void SyncToSale(TransactionType ebaySale)
        {
            Int32 ebayOrderSaleRecordNumber = 0;

            try
            {
                ebayOrderSaleRecordNumber =
                    (ebaySale.ContainingOrder == null || ebaySale.ContainingOrder.ShippingDetails == null) ?
                    ebaySale.ShippingDetails.SellingManagerSalesRecordNumber :
                    ebaySale.ContainingOrder.ShippingDetails.SellingManagerSalesRecordNumber;

                System.Diagnostics.Trace.WriteLine(String.Format("Begin sync of ebay sale {0}...", ebayOrderSaleRecordNumber));

                Sale sale = Sale.LoadByEbaySalesRecordNumber(ebayOrderSaleRecordNumber);
                if (sale == null)
                {
                    sale             = Sale.Create();
                    sale.Source      = SaleSources.Ebay;
                    sale.SourceId    = ebayOrderSaleRecordNumber.ToString();
                    sale.EbayName    = ebaySale.Buyer.UserID;
                    sale.NameOfBuyer = ebaySale.Buyer.BuyerInfo.ShippingAddress.Name ?? String.Empty;

                    var shippingPhone = GetStringFromTransaction(ebaySale.Buyer.BuyerInfo.ShippingAddress.Phone);
                    sale.PhoneNumber  = String.IsNullOrWhiteSpace(shippingPhone) ? "" : shippingPhone;
                    sale.EMailAddress = GetStringFromTransaction(ebaySale.Buyer.Email);
                    sale.DateOfSale   = ebaySale.CreatedDate;
                    Models.MyDataContext.Default.Sales.Add(sale);
                }

                // A sale item may occur in another (previously synced sale) and if so it must be deleted
                SaleItem oldSaleItem = SaleItem.LoadBySalesRecordNumber(ebaySale.ShippingDetails.SellingManagerSalesRecordNumber);
                if (oldSaleItem != null)
                {
                    if (oldSaleItem.Sale.Id != sale.Id)
                    {
                        Sale oldSale = null;

                        if (oldSaleItem.Sale.SaleItems.Count == 1)
                        {
                            oldSale = oldSaleItem.Sale;
                        }

                        oldSaleItem.IncreaseStock();
                        Models.MyDataContext.Default.SaleItems.Remove(oldSaleItem);

                        if (oldSale != null)
                        {
                            Models.MyDataContext.Default.Sales.Remove(oldSale);
                        }
                    }
                }

                SaleItem saleItem = SaleItem.LoadByEbaySalesRecordNumber(ebayOrderSaleRecordNumber, ebaySale.ShippingDetails.SellingManagerSalesRecordNumber);
                if (saleItem == null)
                {
                    saleItem = new SaleItem();
                    saleItem.EbaySalesRecordNumber = ebaySale.ShippingDetails.SellingManagerSalesRecordNumber;
                    saleItem.ExternalArticleName   = ebaySale.Item.Title;
                    saleItem.ExternalArticleNumber = ebaySale.Item.ItemID;
                    saleItem.InternalArticleNumber = ebaySale.Item.SKU;
                    saleItem.Article = Article.LoadByArticleNumber(ebaySale.Item.SKU);
                    if (saleItem.Article == null)
                    {
                        saleItem.Article = Article.LoadByEbayArticleNumber(ebaySale.Item.SKU);
                    }
                    saleItem.Amount              = ebaySale.QuantityPurchased;
                    saleItem.TaxRate             = ebaySale.VATPercentSpecified ? ebaySale.VATPercent : ShopperConfiguration.Default.CurrentTaxRate;
                    saleItem.SinglePriceGross    = Convert.ToDecimal(ebaySale.ConvertedTransactionPrice.Value);
                    saleItem.Sale                = sale;
                    saleItem.EbayOrderLineItemID = ebaySale.OrderLineItemID;
                    Models.MyDataContext.Default.SaleItems.Add(saleItem);
                    Models.MyDataContext.Default.SaveChanges();
                    saleItem.DecreaseStock();
                }

                if (sale.CanBeSynced)
                {
                    sale.InvoiceName     = ebaySale.Buyer.BuyerInfo.ShippingAddress.Name ?? String.Empty;
                    sale.InvoiceStreet1  = ebaySale.Buyer.BuyerInfo.ShippingAddress.Street1 ?? String.Empty;
                    sale.InvoiceStreet2  = ebaySale.Buyer.BuyerInfo.ShippingAddress.Street2 ?? String.Empty;
                    sale.InvoiceCity     = ebaySale.Buyer.BuyerInfo.ShippingAddress.CityName ?? String.Empty;
                    sale.InvoiceRegion   = ebaySale.Buyer.BuyerInfo.ShippingAddress.StateOrProvince ?? String.Empty;
                    sale.InvoicePostcode = ebaySale.Buyer.BuyerInfo.ShippingAddress.PostalCode ?? String.Empty;
                    sale.InvoiceCountry  = ebaySale.Buyer.BuyerInfo.ShippingAddress.CountryName ?? String.Empty;

                    sale.ShippingName     = ebaySale.Buyer.BuyerInfo.ShippingAddress.Name ?? String.Empty;
                    sale.ShippingStreet1  = ebaySale.Buyer.BuyerInfo.ShippingAddress.Street1 ?? String.Empty;
                    sale.ShippingStreet2  = ebaySale.Buyer.BuyerInfo.ShippingAddress.Street2 ?? String.Empty;
                    sale.ShippingCity     = ebaySale.Buyer.BuyerInfo.ShippingAddress.CityName ?? String.Empty;
                    sale.ShippingRegion   = ebaySale.Buyer.BuyerInfo.ShippingAddress.StateOrProvince ?? String.Empty;
                    sale.ShippingPostcode = ebaySale.Buyer.BuyerInfo.ShippingAddress.PostalCode ?? String.Empty;
                    sale.ShippingCountry  = ebaySale.Buyer.BuyerInfo.ShippingAddress.CountryName ?? String.Empty;

                    DateTime?ebayPayDate =
                        ebaySale.ContainingOrder == null ?
                        ebaySale.PaidTimeSpecified ? (DateTime?)ebaySale.PaidTime.Date : (DateTime?)null :
                        ebaySale.ContainingOrder.PaidTimeSpecified ? (DateTime?)ebaySale.ContainingOrder.PaidTime : (DateTime?)null;
                    sale.DateOfPayment = sale.DateOfPayment ?? ebayPayDate;
                    sale.ShippingPrice =
                        (ebaySale.ContainingOrder == null || ebaySale.ContainingOrder.Total == null || ebaySale.ContainingOrder.Subtotal == null) ?
                        GetShippingCosts(ebaySale) :
                        Convert.ToDecimal(ebaySale.ContainingOrder.Total.Value) - Convert.ToDecimal(ebaySale.ContainingOrder.Subtotal.Value);

                    DateTime?ebayCancelDate =
                        ebaySale.AdjustmentAmount.Value < 0.0 ?
                        (DateTime?)DateTime.Now :
                        (DateTime?)null;
                    saleItem.CancelDate = saleItem.CancelDate ?? ebayCancelDate;
                }

                Models.MyDataContext.Default.SaveChanges();
                System.Diagnostics.Trace.WriteLine(String.Format("Sync of ebay sale {0} successful!", ebayOrderSaleRecordNumber));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(String.Format("Sync of ebay sale {0} failed!", ebayOrderSaleRecordNumber));
                throw new SalesImportException(String.Format("Error at Ebay-Prot-Number: {0}.", ebayOrderSaleRecordNumber.ToString()),
                                               ex);
            }
        }