private string createBagManifest(FrayteeCommerceBag bag)
        {
            eCommerceBagManifest bagManifest;

            string bagMnaifestName = string.Empty;

            if (bag != null)
            {
                if (string.IsNullOrEmpty(bag.BagManifest))
                {
                    // FIRST CREATE BAG MANIFEST
                    bagManifest           = new eCommerceBagManifest();
                    bagManifest.CreatedOn = DateTime.UtcNow;
                    bagManifest.IsClosed  = bag.BagClosed;
                    dbContext.eCommerceBagManifests.Add(bagManifest);
                    dbContext.SaveChanges();
                }
                else
                {
                    bagManifest = dbContext.eCommerceBagManifests.Where(p => p.Name == bag.BagManifest).FirstOrDefault();
                }

                // THEN UPDATE BAG MANIFEST
                if (bagManifest != null)
                {
                    bagManifest.Name     = "BM-UK-" + bagManifest.eCommerceBagManifestId;
                    bagManifest.IsClosed = bag.BagClosed;
                    dbContext.SaveChanges();
                    bagMnaifestName = bagManifest.Name;
                    dbContext.SaveChanges();
                }
            }
            return(bagMnaifestName);
        }
        public BagResult CreateBag(FrayteeCommerceBag bag)
        {
            BagResult result = new BagResult();

            result = new eCommerceAppRepository().CreateBag(bag);
            if (bag.BagClosed)
            {
                result.ManifestNumber = "";
            }
            return(result);
        }
        //public eCommerceAppResult CreateBag(eCommerceBag bagDetail)
        //{
        //    eCommerceAppResult Result = new eCommerceAppResult();
        //    Result.Messages = new List<string>();

        //    try
        //    {
        //        if (bagDetail != null)
        //        {

        //            CreateShipmentBag(bagDetail);
        //        }

        //        else
        //        {
        //            Result.Status = false;
        //            Result.Messages.Add(eCommerceAppErrorMessage.InvalidBarcode);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Result.Status = false;
        //        Result.Messages.Add(eCommerceAppErrorMessage.ErrorProcessing);
        //    }

        //    return Result;
        //}

        #region Private Methods CreateBag , Barcode ,Label  BagDetail

        private void addShipmentsInBag(FrayteeCommerceBag bag, int id)
        {
            if (bag != null && bag.ShipmentBarcodes != null && bag.ShipmentBarcodes.Count > 0)
            {
                foreach (var ship in bag.ShipmentBarcodes)
                {
                    var shipment = dbContext.eCommerceShipments.Where(p => p.BarCodeNumber == ship).FirstOrDefault();
                    if (shipment != null)
                    {
                        shipment.eCommerceBagId = id;
                        dbContext.SaveChanges();
                    }
                }
            }
        }
        public BagResult CreateBag(FrayteeCommerceBag bag)
        {
            // If bagmanifest is not available and BagClosed is false the create a new bag manifest and create a new  bag for shipments
            // If bagmanifest is available and BagClosed is false the create new bag
            // If bagmanifest is available and BagClosed is true the create new bag

            BagResult result = new BagResult();

            try
            {
                result.Messages = new List <ErrorMessage>();

                string bagManifestName = string.Empty;

                if (string.IsNullOrEmpty(bag.BagManifest) || bag.BagClosed)
                {
                    // create a new bag manifest
                    bagManifestName = createBagManifest(bag);
                    // create bag
                }
                else
                {
                    bagManifestName = bag.BagManifest;
                }
                int id = createBag(bagManifestName);
                // add shipments in the bag
                addShipmentsInBag(bag, id);
                result.Status         = true;
                result.ManifestNumber = bagManifestName;
            }
            catch (Exception ex)
            {
                result.Status = false;
            }
            return(result);
        }