Example #1
0
        public static ACG.Order MapAmazonOrder(Order order, long customerId, ref int?invoiceNo)
        {
            var nameSplit = order.ShippingAddress.Name.Split(' ');

            invoiceNo += 1;
            ACG.Order acgOrder = new ACG.Order()
            {
                BillingAddress  = order.ShippingAddress.AddressLine1,
                BillingAddress2 = (order.ShippingAddress.IsSetAddressLine2()
                    ? order.ShippingAddress.AddressLine2
                    : "") + (order.ShippingAddress.IsSetAddressLine3() ? order.ShippingAddress.AddressLine3 : ""),
                BillingCity            = order.ShippingAddress.City,
                BillingCountry         = order.ShippingAddress.CountryCode,
                BillingFirstName       = nameSplit[0],
                BillingLastName        = string.Join(" ", nameSplit.Except(new[] { nameSplit[0] })),
                BillingEmail           = order.BuyerEmail,
                BillingPhoneNumber     = order.ShippingAddress.Phone == null ? "111-111-1111" : order.ShippingAddress.Phone,
                BillingState           = order.ShippingAddress.StateOrRegion,
                BillingZipCode         = order.ShippingAddress.PostalCode,
                BillingOnLinePayment   = false,
                BillingPaymentMethodID = "50",
                BillingPaymentMethod   = "Purchase Order",
                UserID              = "storeadmin1",
                PONo                = order.AmazonOrderId,
                CustomerID          = customerId,
                OrderAmount         = Convert.ToDouble(order.OrderTotal.Amount),
                OrderDate           = order.PurchaseDate,
                InvoiceNumberPrefix = "ACGA-",
                InvoiceNumber       = invoiceNo,
                Referer             = "http://www.amazon.com",
                SalesPerson         = "",
                CardNumber          = "-1",
                CardName            = "Amazon Items",
                OrderStatusID       = 1,
                CustomerComments    = order.AmazonOrderId,
                OrderID             = 0,
                ShipmentList        = new List <ACG.Shipment>
                {
                    new ACG.Shipment()
                    {
                        ShipmentAddress  = order.ShippingAddress.AddressLine1,
                        ShipmentAddress2 = (order.ShippingAddress.IsSetAddressLine2()
                                              ? order.ShippingAddress.AddressLine2
                                              : "") + (order.ShippingAddress.IsSetAddressLine3() ? order.ShippingAddress.AddressLine3 :""),
                        ShipmentCity      = order.ShippingAddress.City,
                        ShipmentCountry   = order.ShippingAddress.CountryCode,
                        ShipmentEmail     = order.BuyerEmail,
                        ShipmentFirstName = nameSplit[0],
                        ShipmentLastName  = string.Join(" ", nameSplit.Except(new [] { nameSplit[0] })),
                        ShipmentPhone     = order.ShippingAddress.Phone ?? "111-111-1111",
                        // SAM: This is tobe shipped By date. we may need a field for this. ShipmentShippedDate = order.LatestShipDate.ToLongDateString(),
                        ShipmentState   = order.ShippingAddress.StateOrRegion,
                        ShipmentZipCode = order.ShippingAddress.PostalCode
                    }
                },
                OrderItemList = new List <ACG.OrderItem>()
            };
            if (string.IsNullOrEmpty(acgOrder.BillingLastName))
            {
                acgOrder.BillingLastName = acgOrder.BillingFirstName;
            }
            if (string.IsNullOrEmpty(acgOrder.ShipmentList[0].ShipmentLastName))
            {
                acgOrder.ShipmentList[0].ShipmentLastName = acgOrder.ShipmentList[0].ShipmentFirstName;
            }
            foreach (var item in order.OrderItem)
            {
                // ** SAM: Make sure we have AmazonOrderItemID stored in some table for every order item

                // ** SAM: From Amazon, find Manufacturer Part no for this ASIN. Then use that MPN to map our part no.
                // Search our ASIN table, if the needed, insert this row. Or update.
                // SAM: It will be better if we can capture the SKU (new) coming back from Amazon - Can put it in ASIN table.

                // OR - simply map ASIN with our ASIN table and get the part no.
                var orderItem = ProductDAL.FindOrderFromASIN(ConfigurationHelper.ConnectionString, item.ASIN, item.SellerSKU);
                if (orderItem == null || orderItem.catalogid == null ||
                    orderItem.ItemId == null)
                {
                    MandrillMail.SendEmail(ConfigurationHelper.MandrilAPIKey, "Order Has to be processed manually. ",
                                           "Order Has to be processed manually. OrderItem ASIN information is null. The order no is:" + order.AmazonOrderId,
                                           "*****@*****.**");
                    //todo: SAM: Query 3d cart API if catalogid is null. Map "CK_"+CK_ASIN.ItemId with SKU of 3D Cart
                    continue;
                }
                //** SAM Need to check failure condition that orderItem may be null
                // *** Also these set of SKU's are new and probably we need a new field in the product table for this.
                acgOrder.OrderItemList.Add(new ACG.OrderItem
                {
                    ItemQuantity  = Convert.ToDouble(item.QuantityOrdered),
                    ItemUnitPrice = Convert.ToDouble(item.ItemPrice.Amount) / Convert.ToDouble(item.QuantityOrdered),
                    //ItemOptionPrice = Convert.ToDouble(item.ItemPrice.Amount),
                    ItemID          = orderItem.ItemId,
                    CatalogID       = orderItem.catalogid,
                    ItemOptions     = orderItem.VariantId,
                    ItemDescription = item.Title,
                    // ItemWeight = Convert.ToDouble(item.,
                    ItemAdditionalField1 = orderItem.ResourceCode,
                    ChannelOrderItemId   = item.OrderItemId,
                    //
                    //Order_Items Table : Add extra field additionalField4 >
                    //ResourceCode and Message
                });
            }


            var orderRes = RestHelper.AddRecord(acgOrder, "Orders", ConfigurationHelper.PrivateKey, ConfigurationHelper.Token,
                                                ConfigurationHelper.Store);

            if (orderRes.Status == ACG.ActionStatus.Failed)
            {
                MandrillMail.SendEmail(ConfigurationHelper.MandrilAPIKey, "Order Has to be processed manually",
                                       "Order Has to be processed manually. The order no is:" + order.AmazonOrderId + Environment.NewLine +
                                       "Error: " + orderRes.Description,
                                       "*****@*****.**");
                return(null);
            }

            var orderId = Convert.ToInt32(orderRes.ResultSet);

            acgOrder.OrderID = orderId;
            var orderList = OrderDAL.Map_n_Add_ExtOrders(ConfigurationHelper.ConnectionString, "", new List <ACG.Order>()
            {
                acgOrder
            });
            // SAM: No need to sync orders - just place it to CK

            var autoCarOpConfig = new AutoCarOperations.Model.ConfigurationData()
            {
                ConnectionString = ConfigurationHelper.ConnectionString,
                CKOrderFolder    = ConfigurationHelper.CKOrderFolder,
                MandrilAPIKey    = ConfigurationHelper.MandrilAPIKey,
                FTPAddress       = ConfigurationHelper.FTPAddress,
                FTPUserName      = ConfigurationHelper.FTPUserName,
                FTPPassword      = ConfigurationHelper.FTPPassword,
            };

            if (order.OrderItem[0].ASIN == "B00JFEU78W" && order.OrderItem.Count == 1)   // Avoid uploading Drawstring bag orders
            {
                Console.WriteLine(string.Format("Drawstring Bag, M4I98, ASIN B00JFEU78W - handle manually"));
            }
            else
            {
                OrderDAL.UploadOrderToCK(autoCarOpConfig, true, true, orderList);
            }
            //mark the above orders as Submitted in local table
            OrderDAL.UpdateStatus(ConfigurationHelper.ConnectionString, orderList);

            // Sep 5, 2018 - Write new order in AmazonOpenOrders.csv file
            String currentAppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            String fileName       = currentAppPath + "\\..\\..\\..\\3dCartImportConsole\\bin\\Debug\\AmazonOpenOrders.csv";

            // @"D:\test\ACG\ACGConsole_2018\3dCartImportConsole\bin\Debug\"+"AmazonOpenOrders.csv";
            Console.WriteLine(String.Format("\r\n*** Appending new order to {0} ", fileName));
            // CSV structure: Order Date,Amazon Order,Ship By,ASIN,Name,ACG Order,CK Order No,Status,Ship Agent,Ship Service,Tracking No,Status Date
            DateTime dt = Convert.ToDateTime(acgOrder.OrderDate.ToString());
            string   strOrderLineforCSV = string.Format("{0},{1},{2},,{3} {4},{5}{6},,,,,,{7}",
                                                        dt.ToString("MM/dd/yyyy hh:mm"), acgOrder.PONo, order.LatestShipDate.ToString("MM/dd/yyyy hh:mm"), acgOrder.BillingFirstName, acgOrder.BillingLastName,
                                                        acgOrder.InvoiceNumberPrefix, acgOrder.InvoiceNumber, DateTime.Now.ToString("MM/dd/yyyy hh:mm"));

            File.AppendAllText(fileName, strOrderLineforCSV + Environment.NewLine);
            return(acgOrder);
        }
Example #2
0
        public static long CreateCustomer(Order order)
        {
            //check if customer already exist
            var customerOld = CustomerDAL.FindCustomer(ConfigurationHelper.ConnectionString, cus => cus.billing_address == order.ShippingAddress.AddressLine1 && cus.billing_zip == order.ShippingAddress.PostalCode);

            if (customerOld != null)
            {
                return(customerOld.customer_id);
            }
            var nameSplit = order.ShippingAddress.Name.Split(' ');
            var customer  = new ACG.Customer
            {
                Email            = order.BuyerEmail,
                Password         = "******" + Guid.NewGuid().ToString("d").Substring(1, 8),
                BillingCompany   = "",
                BillingFirstName = nameSplit[0],
                BillingLastName  = string.Join(" ", nameSplit.Except(new[] { nameSplit[0] })),
                BillingAddress1  = order.ShippingAddress.AddressLine1,
                BillingAddress2  = (order.ShippingAddress.IsSetAddressLine2()
                ? order.ShippingAddress.AddressLine2
                : "") + (order.ShippingAddress.IsSetAddressLine3() ? order.ShippingAddress.AddressLine3 : ""),
                BillingCity        = order.ShippingAddress.City,
                BillingState       = order.ShippingAddress.StateOrRegion,
                BillingZipCode     = order.ShippingAddress.PostalCode,
                BillingCountry     = order.ShippingAddress.CountryCode, // order.BuyerCounty
                BillingPhoneNumber = order.ShippingAddress.Phone,
                //BillingTaxID= order.BuyerTaxInfo.,todo: Sam to check
                //ShippingCompany= "Amazon",
                ShippingFirstName = nameSplit[0],
                ShippingLastName  = string.Join(" ", nameSplit.Except(new[] { nameSplit[0] })),
                ShippingAddress1  = order.ShippingAddress.AddressLine1,
                ShippingAddress2  = (order.ShippingAddress.IsSetAddressLine2()
                                      ? order.ShippingAddress.AddressLine2
                                      : "") + (order.ShippingAddress.IsSetAddressLine3() ? order.ShippingAddress.AddressLine3 : ""),
                ShippingCity        = order.ShippingAddress.City,
                ShippingState       = order.ShippingAddress.StateOrRegion.Trim(),
                ShippingZipCode     = order.ShippingAddress.PostalCode,
                ShippingCountry     = order.ShippingAddress.CountryCode.ToUpper(),
                ShippingPhoneNumber = order.ShippingAddress.Phone,
                //ShippingAddressType= 0,
                Enabled    = true,
                MailList   = false,
                NonTaxable = true,
                DisableBillingSameAsShipping = true,
                CustomerGroupID = 13,  // Amazon
                //Comments= "string",
                //AdditionalField1= "string",
                //AdditionalField2= "string",
                //AdditionalField3= "string",
                TotalStoreCredit = ""
            };

            // check for US States
            if ((customer.ShippingCountry == "US" || customer.ShippingCountry == "USA") && customer.ShippingState.Length > 2)
            {
                customer.ShippingState = OrderDAL.GetStateCodeForUS(customer.ShippingState);
            }
            if ((customer.BillingCountry == "US" || customer.BillingCountry == "USA") && customer.BillingState.Length > 2)
            {
                customer.BillingState = OrderDAL.GetStateCodeForUS(customer.BillingState);
            }

            var recordInfo = RestHelper.AddRecord(customer, "Customers", ConfigurationHelper.PrivateKey,
                                                  ConfigurationHelper.Token, ConfigurationHelper.Store);

            if (recordInfo.Status == ACG.ActionStatus.Failed)
            {
                MandrillMail.SendEmail(ConfigurationHelper.MandrilAPIKey, "Order Has to be processed manually. ",
                                       "Order Has to be processed manually. Could not create customer. The order no is:" + order.AmazonOrderId,
                                       "*****@*****.**");
                return(0);
            }
            var customerId = Convert.ToInt32(recordInfo.ResultSet);

            customer.CustomerID = customerId;
            CustomerDAL.AddCustomers(ConfigurationHelper.ConnectionString, new List <ACG.Customer>()
            {
                customer
            }, new List <ACG.CustomerGroup>());
            return(customerId);
        }