Ejemplo n.º 1
0
        private void CancelItems(sales_flat_order order, Connector connector, OrderResponse ediResponse, int cancelledLines, int totalLines, OrderHelper helper)
        {
            log.Info("For order " + order.increment_id + " , trying to call the cancel shipment url ");
            if (((ConnectorSystemType)connector.ConnectorSystemType).Has(ConnectorSystemType.CallShipmentLink))
            {
                JsonOrder orderJson = new JsonOrder();
                orderJson.id             = order.increment_id;
                orderJson.tracking_codes = new string[0];

                int ediOrderLines = ediResponse.OrderDetails.Count();

                orderJson.lines = new JsonOrderLine[ediOrderLines];

                var isIgnorable = (ediOrderLines == 1 && (ediResponse.OrderDetails.First().ProductIdentifier.ProductNumber.ToLower() == "ShippingCostProductID_IDEAL".ToLower() || ediResponse.OrderDetails.First().ProductIdentifier.ProductNumber.ToLower() == "ShippingCostProductID_CREDITCARD".ToLower()));
                if (!isIgnorable)
                {
                    for (int i = 0; i < ediOrderLines; i++)
                    {
                        var line = ediResponse.OrderDetails[i];

                        orderJson.lines[i] = new JsonOrderLine()
                        {
                            lineid = line.ProductIdentifier.ManufacturerItemID,
                            qty    = line.Quantity.QuantityCancelled,
                            status = "C"
                        };
                    }
                }
                SendGenericUpdate(orderJson, connector, helper);
            }
        }
Ejemplo n.º 2
0
        public static List <PurchaseRecord> boughtCartJsonToPuchaseRecordList(string boughtJson)
        {
            ResponseClass response = JsonConvert.DeserializeObject <ResponseClass>(boughtJson);

            if (response.success)
            {
                JsonOrder             jsonOrder = JsonConvert.DeserializeObject <JsonOrder>(response.answerOrExceptionJson);
                List <PurchaseRecord> retList   = new List <PurchaseRecord>();
                foreach (JsonStoreOrder jsonStoreOrder in jsonOrder.StoreOrders)
                {
                    foreach (JsonStoreOrderItem jsonItem in jsonStoreOrder.StoreOrderItems)
                    {
                        Item           item     = new Item(jsonItem);
                        PurchaseRecord purchase = new PurchaseRecord(jsonOrder.OrderId, item.Name, item.Id, item.Price, jsonItem.Amount);
                        purchase.DiscountedPriceOnPurchase = jsonItem.DiscountedPricePerItem;
                        item.storeID     = jsonStoreOrder.StoreId;
                        purchase.ItemObj = item;
                        purchase.storeID = jsonStoreOrder.StoreId;
                        retList.Add(purchase);
                    }
                }
                return(retList);
            }
            return(null);
        }
Ejemplo n.º 3
0
        public void LoadAll(string fileUsers = "users.json", string fileOrders = "orders.json", string fileDrinks = "drinks.json")
        {
            if (File.Exists(fileDrinks))
            {
                List <JsonDrink> jsonDrinks = JsonConvert.DeserializeObject <List <JsonDrink> >(ReadFromFile(fileDrinks));
                this.drinks = new Dictionary <string, Drink>();
                foreach (var drink in jsonDrinks)
                {
                    this.drinks[drink.Name] = JsonDrink.importJsonDrink(drink, this);
                }
            }

            if (File.Exists(fileUsers))
            {
                List <JsonUser> jsonUsers = JsonConvert.DeserializeObject <List <JsonUser> >(ReadFromFile(fileUsers));
                this.users = new Dictionary <string, User>();
                foreach (var user in jsonUsers)
                {
                    this.users[user.Name] = JsonUser.importJsonUser(user, this);
                }
            }

            if (File.Exists(fileOrders))
            {
                List <JsonOrder> jsonOrders = JsonConvert.DeserializeObject <List <JsonOrder> >(ReadFromFile(fileOrders));
                this.orders = new List <Order>();
                foreach (var order in jsonOrders)
                {
                    this.orders.Add(JsonOrder.importJsonOrder(order, this));
                }
            }
        }
Ejemplo n.º 4
0
        public void ShouldReadPaymentMethod()
        {
            var doc = new JsonOrder(_json);

            var paymentMethod = doc.paymentMethod;

            Assert.That(paymentMethod, Is.EqualTo("card"));
        }
Ejemplo n.º 5
0
        public void ShouldReadTax()
        {
            var doc = new JsonOrder(_json);

            var tax = doc.tax;

            Assert.That(tax, Is.EqualTo(1.99));
        }
Ejemplo n.º 6
0
        public void ShouldSetTax()
        {
            var doc = new JsonOrder(_json)
            {
                tax = 10
            };

            Assert.That(doc.tax, Is.EqualTo(10));
        }
 // GET api/Order
 public IQueryable<JsonOrder> GetOrders()
 {
     IQueryable<Order> orders = ent.Orders.AsQueryable();
     List<JsonOrder> jsonOrders = new List<JsonOrder>();
     foreach(var order in orders)
     {
         JsonOrder jsonOrder = new JsonOrder();
         jsonOrder.color = ent.Colors.FirstOrDefault(c => c.Id == order.Color_Id).Name;
         var prod = ent.Products.FirstOrDefault(p => p.Id == order.Product_Id);
         jsonOrder.product = prod.Name;
         jsonOrder.cost = prod.Cost;
         jsonOrder.size = ent.Sizes.FirstOrDefault(s => s.Id == order.Size_Id).Category;
         jsonOrder.orderId = order.Id;
         jsonOrders.Add(jsonOrder);
     }
     return jsonOrders.AsQueryable();
 }
Ejemplo n.º 8
0
        public static List <StoreOrder> BoughtCartJsonToStoreOrderList(string boughtJson)
        {
            ResponseClass response = JsonConvert.DeserializeObject <ResponseClass>(boughtJson);

            if (response.success)
            {
                JsonOrder jsonOrder = JsonConvert.DeserializeObject <JsonOrder>(response.answerOrExceptionJson);

                List <StoreOrder> storeOrders = new List <StoreOrder>();
                foreach (JsonStoreOrder jsonStoreOrder in jsonOrder.StoreOrders)
                {
                    storeOrders.Add(new StoreOrder(jsonStoreOrder));
                }
                return(storeOrders);
            }
            return(null);
        }
        // GET api/Order
        public IQueryable <JsonOrder> GetOrders()
        {
            IQueryable <Order> orders     = ent.Orders.AsQueryable();
            List <JsonOrder>   jsonOrders = new List <JsonOrder>();

            foreach (var order in orders)
            {
                JsonOrder jsonOrder = new JsonOrder();
                jsonOrder.color = ent.Colors.FirstOrDefault(c => c.Id == order.Color_Id).Name;
                var prod = ent.Products.FirstOrDefault(p => p.Id == order.Product_Id);
                jsonOrder.product = prod.Name;
                jsonOrder.cost    = prod.Cost;
                jsonOrder.size    = ent.Sizes.FirstOrDefault(s => s.Id == order.Size_Id).Category;
                jsonOrder.orderId = order.Id;
                jsonOrders.Add(jsonOrder);
            }
            return(jsonOrders.AsQueryable());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Call the generic
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        private bool SendGenericUpdate(JsonOrder order, Connector conn, OrderHelper helper, string url = "")
        {
            bool success = false;

            if (url == string.Empty)
            {
                url = conn.ConnectorSettings.GetValueByKey <string>("SCBUrl", string.Empty);
            }

            url.ThrowIfNullOrEmpty();

            if (url.Contains("{StoreCodeWillBeDetermined}"))
            {
                var orderStoreCode = helper.GetOrderStoreCode(order.id);

                if (conn.ConnectorID == 7) //nastyyyyyy fixx!!!!
                {
                    url = url.Replace("{StoreCodeWillBeDetermined}", orderStoreCode.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries)[1]);
                }
                else
                {
                    url = url.Replace("{StoreCodeWillBeDetermined}", orderStoreCode);
                }
            }

            string jsonToPass = string.Empty;

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            jsonToPass = serializer.Serialize(order);

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method      = "POST";
            request.ContentType = "text/json";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(jsonToPass);
            }
            log.Info(order.id + ": Calling url " + url);
            try
            {
                using (WebResponse resp = request.GetResponse())
                {
                    var response = ((HttpWebResponse)resp);

                    log.Info(order.id + ": Url called returned status: " + (response.StatusCode + " " + response.StatusDescription));

                    string returnedMessage = string.Empty;

                    using (var reader = new StreamReader(resp.GetResponseStream()))
                    {
                        returnedMessage = reader.ReadToEnd();

                        log.Info(order.id + ": Url returned message  " + returnedMessage);
                    }
                }
            }
            catch (WebException e)
            {
                var response = ((HttpWebResponse)e.Response);
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var returnedMessage = reader.ReadToEnd();

                    if (response.StatusCode == HttpStatusCode.InternalServerError) //log as an error
                    {
                        log.AuditError(string.Format("Processing {0} failed : code {1} | message {2} | url: {3} | jsonPassed: {4}. Order is marked as processed in the Concentrator", order.id, (int)response.StatusCode + "  " + response.StatusCode.ToString(), returnedMessage, url, jsonToPass));
                    }

                    throw e;
                }
            }
            catch (Exception e)
            {
                log.AuditError(string.Format("Status update failed for {0}", order.id));
                throw e;
            }
            return(success);
        }
Ejemplo n.º 11
0
        private void ProcessResponse(XmlDocument responseString, string prefix)
        {
            object ediDocument   = null;
            int    bskIdentifier = 0;

            switch (prefix)
            {
            case "OrderResponse":
                ediDocument   = ExtractResponseMessage <OrderResponse>(responseString);
                bskIdentifier = ((OrderResponse)ediDocument).OrderHeader.BSKIdentifier;
                break;

            case "ShippingNotification":
                ediDocument   = ExtractResponseMessage <ShippingNotification>(responseString);
                bskIdentifier = ((ShippingNotification)ediDocument).ShipmentOrderHeader.BSKIdentifier;

                break;

            case "InvoiceNotification":
                ediDocument   = ExtractResponseMessage <InvoiceNotification>(responseString);
                bskIdentifier = ((InvoiceNotification)ediDocument).InvoiceOrderHeader.BSKIdentifier;
                break;

            case "Statuses":
                ediDocument = XDocument.Parse(responseString.OuterXml);
                break;
            }

            Connector connector = null;

            using (var unit = GetUnitOfWork())
            {
                if (bskIdentifier != 0)
                {
                    connector = unit.Scope.Repository <Connector>().GetSingle(x => x.BSKIdentifier == bskIdentifier);
                }
                else
                {
                    if (ediDocument != null)
                    {
                        var connectorid = int.Parse(((XDocument)ediDocument).Root.Attribute("ConnectorID").Value);
                        connector = unit.Scope.Repository <Connector>().GetSingle(x => x.ConnectorID == connectorid);
                    }
                }

                if (connector == null)
                {
                    string bsk     = bskIdentifier.ToString();
                    var    setting = unit.Scope.Repository <ConnectorSetting>().GetSingle(x => x.SettingKey == "ShopOrderBSK" && x.Value == bsk);

                    if (setting != null)
                    {
                        connector = setting.Connector;
                    }

                    if (connector == null)
                    {
                        log.WarnFormat("Process response failed for {0} message", prefix);
                        return;
                    }
                }
            }

            OrderHelper      helper = new OrderHelper(connector.Connection);
            sales_flat_order order  = null;

            switch (prefix)
            {
            case "OrderResponse":
                OrderResponse ediResponse = ediDocument as OrderResponse;



                order = helper.GetSalesOrder(ediResponse.OrderHeader.WebSiteOrderNumber);

                if (order != null)
                {
                    order.state  = "processing";
                    order.status = "concentrator";


                    log.Info("For order " + order.increment_id + " updating the status to concentrator ");
                    helper.UpdateOrderStatus(ediResponse.OrderHeader.WebSiteOrderNumber, MagentoOrderState.Processing, MagentoOrderStatus.concentrator);

                    int cancelledlines = 0;
                    int backorderLines = 0;
                    int lines          = 0;

                    foreach (var line in ediResponse.OrderDetails)
                    {
                        //var responseLine = (from r in mctx.sales_flat_order_item
                        //                    where r.item_id == line.LineNumber
                        //                    select r).FirstOrDefault();



                        helper.UpdateOrderLine(order, line);
                        lines++;
                        var lineEntity = helper.GetOrderLine(order, line.ProductIdentifier.ManufacturerItemID);

                        //if (responseLine != null)
                        //{
                        //  lines++;

                        if (line.Quantity.QuantityCancelled > 0)
                        {
                            cancelledlines++;
                        }

                        if (line.Quantity.QuantityBackordered > 0)
                        {
                            backorderLines++;
                        }
                    }

                    #region REPLACED WITH FUNCTION : DiSCUSS WITH FREEK

                    //if (cancelledlines == lines)
                    //{
                    //  // helper.UpdateOrderStatus(order.increment_id, MagentoOrderState.canceled, MagentoOrderStatus.canceled);
                    //  log.Info("For order " + order.increment_id + " , trying to call the cancel shipment url ");
                    //  if (((ConnectorSystemType)connector.ConnectorSystemType).Has(ConnectorSystemType.CallShipmentLink))
                    //  {

                    //    int ediOrderLines = ediResponse.OrderDetails.Count();

                    //    var isIgnorable = (ediOrderLines == 1 && (ediResponse.OrderDetails.First().ProductIdentifier.ProductNumber.ToLower() == "ShippingCostProductID_IDEAL".ToLower() || ediResponse.OrderDetails.First().ProductIdentifier.ProductNumber.ToLower() == "ShippingCostProductID_CREDITCARD".ToLower()));
                    //    if (!isIgnorable)
                    //    {
                    //      string url = connector.ConnectorSettings.GetValueByKey("CancelShipmentUrl", string.Empty);
                    //      if (!string.IsNullOrEmpty(url))
                    //      {
                    //        var skus = new Dictionary<string, int>();
                    //        StringBuilder sb = new StringBuilder();


                    //        for (int i = 0; i < ediOrderLines; i++)
                    //        {
                    //          var line = ediResponse.OrderDetails[i];

                    //          sb.Append(HttpUtility.HtmlEncode(line.ProductIdentifier.ManufacturerItemID) + "," + line.Quantity.QuantityShipped);
                    //          if (i != ediOrderLines - 1) sb.Append(";");
                    //        }

                    //        ///basstock/shipment/shippartialorder/order_id/100000342/items/SKU1,5;SKU2,7/trackin_code/123,2345

                    //        url = string.Format(url, ediResponse.OrderHeader.WebSiteOrderNumber, HttpUtility.UrlEncode(sb.ToString()));
                    //        log.Info("For order " + order.increment_id + " , called cancel shipment url " + url);
                    //        WebRequest req = WebRequest.Create(url);
                    //        using (WebResponse resp = req.GetResponse())
                    //        {
                    //          log.AuditInfo("Url for partial shipment called for " + connector.Name + " returned:" + (((HttpWebResponse)resp).StatusDescription));
                    //        }
                    //      }
                    //    }
                    //  }
                    //}

                    #endregion

                    if (cancelledlines > 0)
                    {
                        int totalLines = 0;
                        totalLines = helper.GetOrderLinesCount(order, true);
                        CancelItems(order, connector, ediResponse, cancelledlines, totalLines, helper);
                    }

                    //if (backorderLines == lines)
                    //{
                    //  //helper.UpdateOrderStatus(order.increment_id, MagentoOrderState.Processing, MagentoOrderStatus.backorder);
                    //}
                }
                log.DebugFormat("Processed OrderResponse for order {0} (Connector : {1})", ediResponse.OrderHeader.WebSiteOrderNumber, connector.Name);

                break;

            case "ShippingNotification":
                ShippingNotification ediShipment = ediDocument as ShippingNotification;
                log.Info("Shipment order for " + ediShipment.ShipmentOrderHeader.WebSiteOrderNumber);

                bool shopOrder = false;
                order = helper.GetSalesOrder(ediShipment.ShipmentOrderHeader.WebSiteOrderNumber);


                if (order != null)
                {
                    if (ediShipment.ShipmentOrderHeader.CustomerOrder.Contains("Winkel#") ||
                        ediShipment.ShipmentOrderHeader.CustomerOrder.Contains("#winkel"))
                    {
                        shopOrder = true;
                    }

                    int cancelledlines = 0;
                    int lines          = 0;
                    foreach (var line in ediShipment.ShipmentOrderDetails)
                    {
                        long lineNumber = 0;

                        if (long.TryParse(line.LineNumber, out lineNumber))
                        {
                            var responseLine = helper.GetOrderLine(order, line.ProductIdentifier.ManufacturerItemID);

                            if (responseLine != null)
                            {
                                lines++;

                                if (line.Quantity.QuantityCancelled > 0)
                                {
                                    cancelledlines++;
                                }

                                helper.UpdateOrderLine(order, line);
                            }
                        }
                    }

                    log.Info("Number of cancelled lines " + cancelledlines);

                    log.Info("For order " + order.increment_id + " trying to call shipment url");
                    if (((ConnectorSystemType)connector.ConnectorSystemType).Has(ConnectorSystemType.CallShipmentLink))
                    {
                        int ediOrderLines = ediShipment.ShipmentOrderDetails.Count();

                        JsonOrder orderJson = new JsonOrder();
                        orderJson.id             = order.increment_id;
                        orderJson.tracking_codes = ediShipment.ShipmentOrderDetails.Select(x => x.ShipmentInformation.TrackAndTraceNumber).Distinct().ToArray();
                        orderJson.lines          = new JsonOrderLine[ediOrderLines];

                        var isIgnorable = (ediOrderLines == 1 && (ediShipment.ShipmentOrderDetails.First().ProductIdentifier.ManufacturerItemID.ToLower() == "ShippingCostProductID_IDEAL".ToLower() || ediShipment.ShipmentOrderDetails.First().ProductIdentifier.ManufacturerItemID.ToLower() == "ShippingCostProductID_CREDITCARD".ToLower()));
                        if (!isIgnorable)
                        {
                            string url = connector.ConnectorSettings.GetValueByKey("SCBUrl", string.Empty);
                            if (!string.IsNullOrEmpty(url))
                            {
                                var skus = new Dictionary <string, int>();

                                for (int i = 0; i < ediOrderLines; i++)
                                {
                                    var line = ediShipment.ShipmentOrderDetails[i];

                                    orderJson.lines[i] = new JsonOrderLine()
                                    {
                                        lineid = line.ProductIdentifier.ManufacturerItemID,
                                        qty    = line.Quantity.QuantityShipped,
                                        status = "S"
                                    };
                                }

                                SendGenericUpdate(orderJson, connector, helper);
                            }
                        }
                    }
                }

                log.DebugFormat("Processed ShippingNotification for order {0} (Connector : {1})", ediShipment.ShipmentOrderHeader.WebSiteOrderNumber, connector.Name);

                break;

            case "InvoiceNotification":
                InvoiceNotification ediInvoice = ediDocument as InvoiceNotification;

                order = helper.GetSalesOrder(ediInvoice.InvoiceOrderHeader.WebSiteOrderNumber);

                if (order != null)
                {
                    foreach (var line in ediInvoice.InvoiceOrderDetails)
                    {
                        long lineNumber = 0;

                        if (long.TryParse(line.LineNumber, out lineNumber))
                        {
                            var responseLine = helper.GetOrderLine(order, line.ProductIdentifier.ManufacturerItemID);

                            if (responseLine != null)
                            {
                                helper.UpdateOrderLine(order, line);
                            }
                        }
                    }
                }
                log.DebugFormat("Processed InvoiceNotification for order {0} (Connector : {1})", ediInvoice.InvoiceOrderHeader.WebSiteOrderNumber, connector.Name);

                break;

            case "Statuses":
                string websiteOrderNumber = ((XDocument)ediDocument).Root.Attribute("WebsiteOrderNumber").Value;

                Dictionary <string, string> urls = new Dictionary <string, string>();

                var statuses = ((XDocument)ediDocument).Root.Elements("Status").GroupBy(x => x.Element("StatusDescription").Value).ToArray();

                foreach (var statusGroup in statuses)
                {
                    var count     = statusGroup.Count();
                    var statusId  = statusGroup.Key;
                    var statusUrl = connector.ConnectorSettings.GetValueByKey(statusGroup.Key, string.Empty);

                    JsonOrder ord = new JsonOrder();
                    ord.id             = websiteOrderNumber;
                    ord.tracking_codes = new string[0];
                    ord.lines          = new JsonOrderLine[count];

                    int c = 0;

                    foreach (var stat in statusGroup)
                    {
                        ord.lines[c] = new JsonOrderLine()
                        {
                            lineid = stat.Element("VendorItemNumber").Value,
                            qty    = int.Parse(stat.Element("Quantity").Value),
                            status = statusId[0].ToString().ToUpper()
                        };
                        c++;
                    }
                    SendGenericUpdate(ord, connector, helper, statusUrl);
                }
                break;
            }
        }