Exemplo n.º 1
0
        public List <ShipmentBox> GetShipmentBoxes(string warehouseId, string sku)
        {
            List <ShipmentBox> shipmentBoxes = new List <ShipmentBox>();

            using (ERPContext context = new ERPContext(m_connectionString))
            {
                var q = from ebayShipmentBox in context.EbayShipmentbox
                        where ebayShipmentBox.WarehouseId == warehouseId
                        where ebayShipmentBox.Sku == sku
                        orderby ebayShipmentBox.ShipmentId, ebayShipmentBox.BoxId
                select ebayShipmentBox;
                foreach (var row in q)
                {
                    ShipmentBox shipmentBox = new ShipmentBox();
                    shipmentBox.WarehouseId = row.WarehouseId;
                    shipmentBox.ShipmentId  = row.ShipmentId;
                    shipmentBox.BoxId       = row.BoxId;
                    shipmentBox.Quantity    = row.Quantity;
                    shipmentBox.Type        = row.Type;
                    shipmentBoxes.Add(shipmentBox);
                }
            }

            return(shipmentBoxes);
        }
        /// <summary>
        /// Saves the shipment data to the Database. If it exists in DB, it is edited; else it's added.
        /// </summary>
        public void SaveShipmentToDB()
        {
            using (var db = new Models.AppContext())
            {
                //create shipment entity and set props
                Entities.Shipment entShipment = new Shipment()
                {
                    ShipmentId = Shipment.ShipmentID, Boxes = new List <ShipmentBox>(), ShipmentDate = Shipment.ShipmentDate
                };

                //add each box one to entShipment entity
                foreach (var box in Shipment.Boxes)
                {
                    ShipmentBox entBox = new ShipmentBox
                    {
                        BoxContentString = box.FBALabel(),
                        BoxNumber        = box.BoxNumber,
                        BoxId            = box.BoxID,
                        Shipment         = entShipment
                    };

                    entShipment.Boxes.Add(entBox);
                }//end foreach loop

                //get amazon warehouse entity and set to ShipmentEntity
                AmazonWarehouse amz = db.AmazonWarehouses.Where(a => a.Id == Shipment.FullfillmentShipTo.Id).FirstOrDefault();
                entShipment.ShipToCenter = amz;

                //get company address entity and set to shipmententity
                CompanyAddress comp = db.CompanyAddresses.Where(c => c.Id == Shipment.CompanyShipFrom.Id).FirstOrDefault();
                entShipment.ShipFromCenter = comp;

                //check if shipment exists in the database
                if (db.Shipments.Any(s => s.ShipmentId == Shipment.ShipmentID))
                {
                    Shipment shipmentDel = db.Shipments.Find(Shipment.ShipmentID);


                    var bx = db.Boxes.Where((b => b.Shipment.ShipmentId == shipmentDel.ShipmentId)).ToList();

                    for (int i = 0; i < bx.Count(); i++)
                    {
                        db.Boxes.Remove(bx[i]);
                    }


                    db.SaveChanges();

                    //then delete the shipment with shipmentID
                    db.Shipments.Remove(shipmentDel);

                    //finally add the shipment back into shipments table
                    db.Shipments.Add(entShipment);
                }
                else
                {
                    //add shipment to DB and save
                    db.Shipments.Add(entShipment);
                }


                db.SaveChanges();
            }
        }