private void UpdateExfactory(Guid productId,decimal exf)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                var prod = ctx.tblProduct.FirstOrDefault(p => p.id == productId);
                if(prod !=null && prod.ExFactoryPrice==0m)
                {
                    try
                    {
                       ctx.tblProduct.FirstOrDefault(p => p.id == productId).ExFactoryPrice=exf;
                        ctx.SaveChanges();
                       
                    }
                    catch
                    {
                        
                    }
                }
            }

        }
     Guid AddUserGroup(string name)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblUserGroup group=null;
                group = ctx.tblUserGroup.FirstOrDefault(p => p.Name.ToLower() == name.ToLower());
                if (group == null)
                {
                    group = new tblUserGroup
                                {
                                    Id = Guid.NewGuid(),
                                    Description = name,
                                    Name = name,
                                    IM_DateCreated = DateTime.Now,
                                    IM_DateLastUpdated = DateTime.Now,
                                    IM_Status = (int) EntityStatus.Active
                                };
                    ctx.tblUserGroup.AddObject(group);
                    ctx.SaveChanges();
                    AddUserGroupRoles(group.Id);
                }
                return group.Id;
            }

        }
 private void Insert(tblShipToAddress dto)
 {
     using (var ctx = new CokeDataContext(Con))
     {
         ctx.tblShipToAddress.AddObject(dto);
         ctx.SaveChanges();
     }
 }
        private tblVATClass GetVATClass(string vatNameCode)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblVATClass vatClass = null;
                if (!string.IsNullOrEmpty(vatNameCode))
                {
                    vatClass = ctx
                        .tblVATClass.FirstOrDefault(
                            p => p.Class != null &&
                                 p.Class.ToLower() == vatNameCode.ToLower() ||
                                 p.Name != null && p.Name.ToLower() == vatNameCode.ToLower());
                }
                //if (vatClass == null)
                //    vatClass = ctx.tblVATClass.FirstOrDefault(p => p.Class == "default");

                if (vatClass == null)
                {
                    return null;
                    var item = new tblVATClass()
                                   {
                                       Class = "default",
                                       Name = "default",
                                       id = Guid.NewGuid(),
                                       IM_DateCreated = DateTime.Now,

                                       IM_DateLastUpdated = DateTime.Now,
                                       IM_Status = (int) EntityStatus.Active,
                                       tblVATClassItem = new EntityCollection<tblVATClassItem>()
                                                             {
                                                                 new tblVATClassItem()
                                                                     {
                                                                         id = Guid.NewGuid(),
                                                                         Rate = 0m,
                                                                         EffectiveDate = DateTime.Now,
                                                                         IM_DateCreated = DateTime.Now,
                                                                         IM_DateLastUpdated = DateTime.Now,
                                                                         IM_Status = (int) EntityStatus.Active
                                                                     }
                                                             }
                                   };

                    ctx.tblVATClass.AddObject(item);
                    ctx.SaveChanges();
                }
                return vatClass;
            }
        }
        private tblOutletType GetOutletType(string outlettypeCode)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblOutletType outletType = null;
                if (!string.IsNullOrEmpty(outlettypeCode))
                {
                    outletType = ctx
                        .tblOutletType.FirstOrDefault(
                            p => p.Code != null &&
                                 p.Code.ToLower() == outlettypeCode.ToLower() ||
                                 p.Name != null && p.Name.ToLower() == outlettypeCode.ToLower());
                }
                if(outletType==null)
                outletType = ctx.tblOutletType.FirstOrDefault(p => p.Code == "default");

                if(outletType==null)
                {
                    var item = new tblOutletType()
                                   {
                                       Code = "default",
                                       Name = "default",
                                       id = Guid.NewGuid(),
                                       IM_DateCreated = DateTime.Now,
                                       IM_DateLastUpdated = DateTime.Now,
                                       IM_Status = (int)EntityStatus.Active
                                   };
                    ctx.tblOutletType.AddObject(item);
                    ctx.SaveChanges();
                }
                return outletType;
            }
        }
        private tblCommodityType GetCommodityType(string name)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblCommodityType productType = null;
                if (!string.IsNullOrEmpty(name))
                {
                    productType = ctx
                        .tblCommodityType.FirstOrDefault(
                            p =>
                            p.Name.ToLower() == name.ToLower() ||
                            p.Code != null &&
                            p.Code.ToLower() == name.ToLower());
                }

                if (productType == null)
                {
                    productType = ctx.tblCommodityType.FirstOrDefault(p => p.Name.ToLower() == "default");
                    if (productType == null)
                    {
                        productType = new tblCommodityType()
                                          {
                                              Id = Guid.NewGuid(),
                                              Name = string.IsNullOrEmpty(name) ? "default" : name,
                                              Description = string.IsNullOrEmpty(name) ? "default" : name,
                                              Code = string.IsNullOrEmpty(name) ? "default" : name,
                                              IM_DateCreated = DateTime.Now,
                                              IM_Status = (int) EntityStatus.Active,
                                              IM_DateLastUpdated = DateTime.Now
                                          };
                        ctx.tblCommodityType.AddObject(productType);
                        ctx.SaveChanges();
                    }
                }
                return productType;
            }
        }
        private tblProductPackagingType GetPackagingType(string itemName)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblProductPackagingType pitem = null;
                if (!string.IsNullOrEmpty(itemName))
                    pitem = ctx
                        .tblProductPackagingType.FirstOrDefault(
                            p =>
                            p.name.ToLower() == itemName.ToLower() ||
                            p.code != null &&
                            p.code.ToLower() == itemName.ToLower());

                if (pitem == null)
                {
                    pitem = ctx.tblProductPackagingType.FirstOrDefault(p => p.name.ToLower() == "default");
                    if (pitem == null)
                    {
                        pitem = new tblProductPackagingType()
                                    {
                                        id = Guid.NewGuid(),
                                        name = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                                        code = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                                        IM_DateCreated = DateTime.Now,
                                        IM_Status = (int) EntityStatus.Active,
                                        IM_DateLastUpdated = DateTime.Now,
                                    };
                        ctx.tblProductPackagingType.AddObject(pitem);
                        ctx.SaveChanges();

                    }

                }
                return pitem;
            }
        }
        private tblProductBrand GetProductBrand(string itemName)
        {
            using (var ctx = new CokeDataContext(Con))
            {
                tblProductBrand pitem = null;
                if (!string.IsNullOrEmpty(itemName))
                    pitem = ctx
                        .tblProductBrand.FirstOrDefault(
                            p =>
                            p.name.ToLower() == itemName.ToLower() ||
                            p.code != null &&
                            p.code.ToLower() == itemName.ToLower())??ctx
                        .tblProductBrand.FirstOrDefault(
                            p =>
                            p.name.ToLower()=="default");

                if(pitem==null)
                {
                    var date = DateTime.Now;
                    var supp = ctx.tblSupplier.FirstOrDefault(p => p.Code == "default");
                    if(supp==null)
                    {
                        supp=new tblSupplier()
                                 {
                                     Name = "default",
                                     Code = "default",
                                     Description = "default",
                                     IM_Status = (int)EntityStatus.Active,
                                     id = Guid.NewGuid(),
                                     IM_DateCreated = date,
                                     IM_DateLastUpdated = date
                                 };
                        ctx.tblSupplier.AddObject(supp);
                        ctx.SaveChanges();
                    }
                    pitem = new tblProductBrand()
                                {
                                    IM_Status = (int) EntityStatus.Active,
                                    id = Guid.NewGuid(),
                                    name = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                                    description = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                                    code = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                                    SupplierId = supp.id,
                                    IM_DateCreated = date,
                                    IM_DateLastUpdated = date

                                };
                    ctx.tblProductBrand.AddObject(pitem);
                    ctx.SaveChanges();
                }

                
                return pitem;
            }
        }
 private tblProductFlavour GetProductFlavour(string itemName,tblProductBrand brand)
 {
     using (var ctx = new CokeDataContext(Con))
     {
         tblProductFlavour pitem = null;
         if (!string.IsNullOrEmpty(itemName))
         {
             pitem = ctx
                         .tblProductFlavour.FirstOrDefault(
                             p =>
                             p.name.ToLower() == itemName.ToLower() ||
                             p.code != null &&
                             p.code.ToLower() == itemName.ToLower()) ?? ctx.tblProductFlavour.FirstOrDefault(
                                 p => p.name.ToLower() == "default");
         }
         if(pitem==null)
         {
             var date = DateTime.Now;
                 pitem = new tblProductFlavour()
                 {
                     BrandId = brand.id,
                     id = Guid.NewGuid(),
                     code = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                     description = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                     IM_DateCreated = date,
                     IM_DateLastUpdated = date,
                     name = string.IsNullOrEmpty(itemName) ? "default" : itemName,
                 };
                 ctx.tblProductFlavour.AddObject(pitem);
                 ctx.SaveChanges();
         }
         return pitem;
     }
 }
        private void AdjustInventory()
        {
            string conn = ConfigurationManager.AppSettings["pzcussons_inventory"];
            using (var ctx = new CokeDataContext(conn))
            {
                using (var c = NestedContainer)
                {
                    Using<IAdjustInventoryWindow>(c).ShowAdjustDialog();

                    if(AdjustQuantity>0m)
                    {
                        try
                        {
                            var products = ctx.tblProduct.Select(p => p.id).ToList();
                            if(products.Any())
                            {
                                DateTime date = DateTime.Now;
                                var wareHouse = ctx.tblCostCentre.FirstOrDefault(p => p.Cost_Centre_Code == "PZ Cussons EA");
                                if(wareHouse==null)
                                {
                                    const string error = "Something bad has happened while adjusting inventory..there is no warehouse";
                                    MessageBox.Show(error);
                                    FileUtility.LogError(error);
                                    return;
                                }

                                foreach (var productId in products)
                                {
                                    var inventory = ctx.tblInventory.FirstOrDefault(p => p.ProductId == productId);
                                    if(inventory==null)
                                    {
                                        inventory = new tblInventory()
                                                        {
                                                            id=Guid.NewGuid(),
                                                            IM_DateCreated = date,
                                                            IM_DateLastUpdated = date,
                                                            IM_Status = (int)EntityStatus.Active,
                                                        };
                                        ctx.tblInventory.AddObject(inventory);
                                    }
                                    inventory.Balance += AdjustQuantity;
                                    inventory.ProductId = productId;
                                    inventory.WareHouseId = wareHouse.Id;
                                    inventory.Value = 0;
                                }
                                ctx.SaveChanges();
                            }
                            MessageBox.Show(string.Format("Inventory adjustment success=>{0} product adjusted with{1}",products.Count,AdjustQuantity));

                        }
                        catch (Exception e)
                        {
                            var error = e.Message
                                        +
                                        (e.InnerException == null
                                             ? ""
                                             : "\n" + e.InnerException);
                            MessageBox.Show("Something bad has happened while adjusting inventory..see logs for details");
                            FileUtility.LogError(error);
                        }
                       
                        
                    }
                }
                
            }
        }