public MODEL.ProductionMaterialModel Save(MODEL.ProductionMaterialModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addmodel = new ProductionMaterial()
                    {
                      MaterialName = model.MaterialName,
                      Description = model.Description,
                      MaterialType = model.MaterialType,
                      UnitOfMeasurementId = model.UnitOfMeasurementId

                    };
                    _context.Entry(addmodel).State = EntityState.Added;
                    _context.SaveChanges();
                    model.ProductionMaterialId = addmodel.ProductionMaterialId;
                    return model;
                }

            }
            catch (Exception aException)
            {
                return new ProductionMaterialModel();
            }
        }
        public int EditGoodReceivedNote(GoodReceivedNoteModel model)
        {
            try
            {

                using (_context= new HSSNInventoryEntities() )
                {
                    var editModel = _context.GoodReceivedNotes.FirstOrDefault(a => a.GoodReceivedNoteId == model.GoodReceivedNoteId);
                    if (editModel != null)
                    {

                        editModel.GoodReceivedBy = model.GoodRecievedBy;
                        editModel.GoodReceivedDate = model.GoodReceivedDate;
                        editModel.CreatedBy = model.CreatedBy;
                        editModel.CreatedDate = model.CreatedDate;

                    }
                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();
                    return model.GoodReceivedNoteId;
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return 0;

            }
        }
        public bool SaveFinanceDispatchDetail(List<FinanceDispatchDetailModel> financeDispatchDetailModelsList, int FinanceDispatchid)
        {
            using (_context=new HSSNInventoryEntities())
            {
                try
                {
                    foreach (var rowData in financeDispatchDetailModelsList)
                    {
                        var model = new FinanceDispatchDetail()
                        {
                            FinanceDispatchId = FinanceDispatchid,
                            ProductId = rowData.ProductId,
                            DispatchFromWareHouseId = rowData.DispatchFromWareHouseId,
                            DispatchOrderId = rowData.DispatchOrderId,
                            Quantity = rowData.Quantity,
                            Rate = rowData.Rate,
                            ExcessShortageQuantity = rowData.ExcessShortageQuantity,
                            UnitOfMeasurementId = rowData.UnitOfMeasurementId,

                        };
                        _context.Entry(model).State=EntityState.Added;

                    }
                    _context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public int EditIssueMaterial(IssueMaterialModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.IssueMaterials.FirstOrDefault(a => a.IssueProductMaterilId == model.IssueProductMaterilId);
                    if (editModel != null)
                    {
                        editModel.IssueProductMaterilId = model.IssueProductMaterilId;
                        editModel.Issuedcode = model.Issuedcode;
                        editModel.Issuedby = model.Issuedby;
                        editModel.BillOfMaterialId = model.BillOfMaterialId;
                        editModel.IssueRemarks = model.IssueRemarks;

                    }
                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();
                    return editModel.IssueProductMaterilId;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
                throw;
            }
        }
        public int editJobOrder(JobOrderModel model)
        {
            try
            {

                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.JobOrders.FirstOrDefault(a => a.JobOrderId == model.JobOrderId);
                    if (editModel != null)
                    {

                        //editModel.DealerId = model.DealerId;
                        //editModel.JobOrderDate = model.JobOrderDate;
                        //editModel.Description = model.Description;
                        //editModel.PreparedBy = model.PreparedBy;
                        editModel.IsApproved = model.IsApproved;
                        editModel.ApprovedBy = model.ApprovedBy;
                        //editModel.ModifiedDate = model.ModifiedDate;
                        //editModel.ApprovedDate = model.ApprovedDate;

                    }
                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();
                    return model.JobOrderId;
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return 0;

            }
        }
        public Boolean EditPurchaseRequisitionNoteSumamry(PurchaseRequisitionNoteModel prnModel)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {

                    var data =
                        _context.PurchaseRequisitionNotes.FirstOrDefault(
                            a => a.PurchaseRequisitionNoteId == prnModel.PurchaseRequisitionNoteId);

                    if (data != null)
                    {
                        data.PrnNumber = prnModel.PrnNumber;
                        data.CreatedBy = prnModel.PrnNumber;
                        data.Reason = prnModel.Reason;
                        data.IsApprovedByGM = prnModel.IsApprovedByGM;
                        data.ApproverId = prnModel.ApproverId;
                        data.DateOfApproved = prnModel.DateOfApproved;
                    }

                    _context.Entry(data).State = EntityState.Modified;
                    _context.SaveChanges();
                    //dispatchOrderModel.DispatchOrderId =data.DispatchOrderId;
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false; //new DispatchOrderModel();
                }
            }
        }
        public int EditUserDetail(UserDetailModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.UserDetails.FirstOrDefault(a => a.UserDetailId == model.UserDetailId);
                    if (editModel != null)
                    {

                        editModel.Name = model.Name;
                        editModel.Address = model.Address;
                        editModel.Telephone = model.Telephone;
                        editModel.EmailId = model.EmailId;
                        editModel.UserName = model.UserName;
                        editModel.Password = model.Password;
                        editModel.DisplayName = model.DisplayName;
                        editModel.OrganisationId = model.OrganisationId;
                        editModel.CreatedBy = model.CreatedBy;
                        editModel.CreatedDate = model.CreatedDate;
                        editModel.ProfileId = model.ProfileId;
                    }
                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();
                    return 1;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
                throw;
            }
        }
        public bool EditIssueproductionDetail(List<IssueMaterialDetailModel> modellist, int Issueproductionmaterialid)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    foreach (var issueproductiondetail in modellist)
                    {
                        var model = new IssueProductMaterialDetail()
                        {
                            IssueProductMaterialDetailId=issueproductiondetail.IssueProductMaterialDetailId,
                           IssueProductMaterialId = Issueproductionmaterialid,
                            ProductMaterialId = issueproductiondetail.ProductMaterialId,
                            OrderedQty = issueproductiondetail.OrderedQty,
                            ReturnQty = issueproductiondetail.ReturnQty,
                            IssuedQty = issueproductiondetail.IssuedQty,

                        };
                        _context.Entry(model).State = EntityState.Modified;
                        _context.SaveChanges();
                    }

                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public bool EditData(MODEL.FinanceDispatchModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {

                    var data =
                        _context.FinanceDispatches.FirstOrDefault(a => a.FinanceDispatchId == model.FinanceDispatchId);

                        data.DispatchOrderId = model.DispatchOrderId;
                         data.EntryDate = model.EntryDate;
                         data.CreatedDate = model.CreatedDate;
                         data.CreatedBy = model.CreatedBy;

                    _context.Entry(data).State = EntityState.Modified;
                    _context.SaveChanges();

                    return true;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return false;
            }
        }
        public void DeleteUserDetail(int UserDetailId)
        {
            using (_context= new HSSNInventoryEntities() )
            {
                var data = _context.UserDetails.FirstOrDefault(a => a.UserDetailId == UserDetailId);
                _context.Entry(data).State = EntityState.Deleted;
                _context.SaveChanges();

            }
        }
        public bool Delete(int brandId)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var deleteModel = _context.Flavours.FirstOrDefault(a => a.FlavourId == brandId);
                    _context.Entry(deleteModel).State = EntityState.Deleted;
                    _context.SaveChanges();

                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }
        public Boolean Delete(int unitId)
        {
            try
            {
                using (_context= new HSSNInventoryEntities() )
                {
                    var data = _context.UnitOfMeasurements.FirstOrDefault(a => a.UnitOfMeasurementId == unitId);
                    _context.Entry(data).State = EntityState.Deleted;
                    _context.SaveChanges();
                    return true;
                }

            }

            catch (Exception e)
            {

                return false;
            }
        }
        public Boolean EditDispatchOrderSumamry(DispatchOrderModel dispatchOrderModel)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {

                    var data =
                        _context.DispatchOrders.FirstOrDefault(
                            a => a.DispatchOrderId == dispatchOrderModel.DispatchOrderId);

                    if (data != null)
                    {
                        //data.DispatchOrderId = dispatchOrderModel.DispatchOrderId;
                        //data.DispatchOrderNumber = dispatchOrderModel.DispatchOrderNumber;
                        //data.DealerId = dispatchOrderModel.DealerId;
                        //data.DispatchOrderDate = dispatchOrderModel.DispatchOrderDate;
                        //data.BankGuaranteeAmount = dispatchOrderModel.BankGuaranteeAmount;
                        data.IsCheckedByManager =Convert.ToBoolean(   dispatchOrderModel.IsCheckedByManager);
                        //data.CheckedBy = dispatchOrderModel.CheckedBy;
                        data.WhetherDoApproved = dispatchOrderModel.WhetherDoApproved;
                        //data.ApprovedBy = dispatchOrderModel.ApprovedBy;
                        //data.NetDueAmount = dispatchOrderModel.NetDueAmount;
                        //data.OverDueAmount = dispatchOrderModel.OverDueAmount;
                        //data.DispatchOrderDate = dispatchOrderModel.DispatchOrderDate;
                        //data.OrderRequestedBy = dispatchOrderModel.OrderRequestedBy;
                    }

                    _context.Entry(data).State = EntityState.Modified;
                    _context.SaveChanges();
                    //dispatchOrderModel.DispatchOrderId =data.DispatchOrderId;
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false; //new DispatchOrderModel();
                }
            }
        }
        public bool DeleteDetail(int financeDispatchId)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {

                    var data =
                        _context.FinanceDispatches.FirstOrDefault(a => a.FinanceDispatchId == financeDispatchId);

                    _context.Entry(data).State = EntityState.Deleted;
                    _context.SaveChanges();

                    return true;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return false;
            }
        }
        public int Save(MODEL.FlavourModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addModel = new Flavour()
                    {
                    FlavourName  = model.FlavourName,
                    Description = model.Description
                    };
                    _context.Entry(addModel).State = EntityState.Added;
                    _context.SaveChanges();

                    return addModel.FlavourId;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return 0;
            }
        }
        public int Save(MODEL.WareHouseModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addModel = new WareHouse()
                    {
                    WareHouseName  = model.WareHouseName,
                    WareHouseType = model.WareHouseType,
                    Description = model.Description
                    };
                    _context.Entry(addModel).State = EntityState.Added;
                    _context.SaveChanges();

                    return addModel.WareHouseId;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return 0;
            }
        }
        private bool SaveProductNormTemplateDetail(IEnumerable<ProductNormTemplateDetailModel> productNormDetails, int productNormTemplateId)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {
                    foreach (var row in productNormDetails)
                    {
                        var data = new ProductNormTemplateDetail()
                        {
                            ProductNormTemplateId = productNormTemplateId,
                            ProductionMaterialId = row.ProductionMaterialId,
                            BatchQuantity = row.BatchQuantity,
                            CartoonQuantity = row.CartoonQuantity
                        };
                        _context.Entry(data).State = EntityState.Added;
                    }
                    _context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        private ProductModel SaveProduct(ProductModel productModel)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {

                    var data = new Product()
                    {
                        BrandId = productModel.BrandId,
                        ProductName = productModel.ProductName,
                        FlavourId = productModel.FlavourId,
                        SKUCode = productModel.SKUCode,
                        weight = productModel.weight,
                        UnitOfMeasureId = productModel.UnitOfMeasureId,
                        PacketPerCartoon = productModel.PacketPerCartoon,
                        PerCartoonRate = productModel.PerCartoonRate,
                        IsVat = productModel.IsVat,
                        ExerciseDuty = productModel.ExerciseDuty,
                        CreatedBy = productModel.CreatedBy,
                        CreatedDate = productModel.CreatedDate
                    };
                    _context.Entry(data).State = EntityState.Added;
                    _context.SaveChanges();
                    productModel.ProductId = data.ProductId;
                    return productModel;

                }
                catch (Exception e)
                {

                    throw;
                }
            }
        }
        private bool EditProduct(ProductModel productModel)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {

                    var data = _context.Products.FirstOrDefault(a => a.ProductId == productModel.ProductId);

                    if (data != null)
                    {
                        data.BrandId = productModel.BrandId;
                        data.ProductName = productModel.ProductName;
                        data.FlavourId = productModel.FlavourId;
                        data.SKUCode = productModel.SKUCode;
                        data.weight = productModel.weight;
                        data.UnitOfMeasureId = productModel.UnitOfMeasureId;
                        data.PacketPerCartoon = productModel.PacketPerCartoon;
                        data.PacketPerCartoon = productModel.PacketPerCartoon;
                        data.PerCartoonRate = productModel.PerCartoonRate;
                        data.IsVat = productModel.IsVat;
                        data.ExerciseDuty = productModel.ExerciseDuty;
                        data.CreatedBy = productModel.CreatedBy;
                        data.CreatedDate = productModel.CreatedDate;

                        _context.Entry(data).State = EntityState.Modified;
                        _context.SaveChanges();
                        productModel.ProductId = data.ProductId;
                    }
                    return true;

                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
        public bool UpdateSerialNumberVoucherType(string voucherType)
        {
            using (_context = new HSSNInventoryEntities())
            {
                try
                {
                    var data = _context.SerialNumbers.FirstOrDefault(b => b.VoucherType == voucherType);
                    if (data != null)
                    {
                        if (data.CurrentNumber != null)
                        {
                            data.CurrentNumber++;
                            _context.Entry(data).State = EntityState.Modified;
                            _context.SaveChanges();
                            return true;

                        }
                        else
                            return false;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception)
                {

                    return false;
                }
            }
        }
        public ProductStockModel UpdateProductStock(ProductStockModel _productStockModel)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var data =
                        _context.ProductStocks.FirstOrDefault(a => a.ProductId == _productStockModel.ProductId &&
                                                                   a.OrganisationId == _productStockModel.OrganisationId &&
                                                                   a.WareHouseId == _productStockModel.WareHouseId);
                    if (_productStockModel.InOutMode == "IN")
                    {
                        if (data != null) data.CurrentStock += _productStockModel.CurrentStock;
                    }
                    else
                    {
                        if (data != null) data.CurrentStock -= _productStockModel.CurrentStock;
                    }
                    _context.Entry(data).State = EntityState.Modified;
                    _context.SaveChanges();
                    return _productStockModel;
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e);
                throw;
            }
        }
        public int saveProductstockupdte(int ProductId, int WareHouseId, int organisationId, int quantity, string Mode)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var data = new ProductStock()
                    {
                        ProductId = ProductId,
                        WareHouseId = WareHouseId,
                        CurrentStock = quantity,
                        OrganisationId = organisationId,
                        InOutMode = Mode,
                    };

                    if (data.InOutMode == "In")
                    {
                        if (data != null) data.CurrentStock += quantity;
                    }
                    else
                    {
                        if (data != null) data.CurrentStock -= quantity;
                    }
                    _context.Entry(data).State = EntityState.Modified;
                    _context.SaveChanges();
                    return data.ProductStockId;

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool SaveProductStock(List<ProductStockModel> modellist)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    foreach (var productstock in modellist)
                    {
                        var data = GetInfo(productstock.ProductId, productstock.OrganisationId,
                   productstock.WareHouseId);
                        if (data == null)
                        {
                            var model = new ProductStock()
                            {
                                ProductId = productstock.ProductId,
                                OrganisationId = productstock.OrganisationId,
                                WareHouseId = productstock.WareHouseId,
                                CurrentStock = productstock.CurrentStock,
                                OpeningStock = productstock.OpeningStock,
                                InOutMode = productstock.InOutMode,

                            };
                            _context.Entry(model).State = EntityState.Added;
                            _context.SaveChanges();
                        }
                        else
                        {
                            var data1 =
                                _context.ProductStocks.FirstOrDefault(
                                    a =>
                                        a.ProductId == productstock.ProductId &&
                                        a.OrganisationId == productstock.OrganisationId &&
                                        a.WareHouseId == productstock.WareHouseId);
                            data1.CurrentStock = productstock.CurrentStock;
                            data1.OpeningStock = productstock.OpeningStock;
                            _context.Entry(data1).State = EntityState.Modified;
                            _context.SaveChanges();

                        }

                    }

                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public bool Update(MODEL.FlavourModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.Flavours.FirstOrDefault(a => a.FlavourId==model.FlavourId);

                    if (editModel != null)
                    {
                        editModel.FlavourName = model.FlavourName;
                        editModel.Description = model.Description;
                    }

                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();

                    return true;

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }
        public bool SaveBillofMaterialDetail(List<BillofMaterialDetailModel> modellist, int billofmaterialid)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    foreach (var BOMDetail in modellist)
                    {
                        var model = new BillOfMaterialDetail()
                        {
                            BillOfMaterialId = billofmaterialid,
                            ProductMaterialId = BOMDetail.ProductMaterialId,
                            UnitOfMeasurement = BOMDetail.UnitOfMeasuremetn,
                            ManufacturedProductId = BOMDetail.ManufacturedProductId,
                            Quantity = BOMDetail.Quantity,
                        };

                        _context.Entry(model).State = EntityState.Added;

                    }
                    _context.SaveChanges();
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                throw;
            }
        }
        public int SaveBillofMaterial(BillOFMaterialModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var data = new BillOfMaterial()
                    {
                        BillOfMaterialId = model.BillOfMaterialId,
                        BillOfmaterialCode = model.BillOfmaterialCode,
                        ProductId = model.ProductId,
                        BatchQuantity = model.BatchQuantity,
                        CartoonQuantity = model.CartoonQuantity,
                        IsIssued = model.IsIssued,
                        PreparedBy = model.PreparedBy,
                        PreparedDate = model.PreparedDate,
                    };
                    _context.Entry(data).State = EntityState.Added;
                    _context.SaveChanges();
                    return data.BillOfMaterialId;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DealerModel Save(DealerModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addModel = new Dealer()
                    {
                        DealerName = model.DealerName,
                       DealerAddress = model.DealerAddress,
                       PhoneNo = model.PhoneNo,
                       MobileNo = model.MobileNo,
                       Email = model.Email,
                       DelearIncharge = model.DealerAddress,
                       RegionId = model.RegionId
                    };
                    _context.Entry(addModel).State = EntityState.Added;
                    _context.SaveChanges();
                    model.DealerId = addModel.DealerId;

                    return model;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return new DealerModel();
            }
        }
        public bool Update(DealerModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.Dealers.FirstOrDefault(a => a.DealerId == model.DealerId);

                    if (editModel != null)
                    {
                        editModel.DealerName = model.DealerName;
                        editModel.DealerAddress = model.DealerAddress;
                        editModel.DelearIncharge = model.DelearIncharge;
                        editModel.PhoneNo = model.PhoneNo;
                        editModel.MobileNo = model.MobileNo;
                        editModel.Email = model.Email;
                        editModel.RegionId = model.RegionId;
                    }

                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();

                    return true;

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
        }
        public int saveData(FinanceDispatchModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var addModel = new FinanceDispatch()
                    {
                        DispatchOrderId = model.DispatchOrderId,
                        EntryDate = model.EntryDate,
                        CreatedDate = model.CreatedDate,
                        CreatedBy = model.CreatedBy,

                    };
                    _context.Entry(addModel).State = EntityState.Added;
                    _context.SaveChanges();
                    model.FinanceDispatchId = addModel.FinanceDispatchId;

                    return model.FinanceDispatchId;
                }
            }
            catch (Exception e)
            {
                //Console.WriteLine(e);
                return 0;
            }
        }
        public MODEL.ProductionMaterialModel Update(MODEL.ProductionMaterialModel model)
        {
            try
            {
                using (_context = new HSSNInventoryEntities())
                {
                    var editModel = _context.ProductionMaterials.FirstOrDefault(a => a.ProductionMaterialId == model.ProductionMaterialId);

                    if (editModel != null)
                    {
                        editModel.MaterialName = model.MaterialName;
                        editModel.Description = model.Description;
                        editModel.UnitOfMeasurementId = model.UnitOfMeasurementId;
                        editModel.Description = model.Description;
                    }

                    _context.Entry(editModel).State = EntityState.Modified;
                    _context.SaveChanges();

                    return model;

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return new ProductionMaterialModel();
            }
        }