public long SavePurchaseInformation(PurchaseReceive model, int userId)
        {
            long CurrentPurchaseID = 0;

            try
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    #region New_Purchase_Insert
                    using (_context)
                    {
                        Prq_Purchase objPurchase = new Prq_Purchase();
                        //Random r = new Random();

                        //objPurchase.PurchaseNo = r.Next().ToString();
                        objPurchase.PurchaseNo        = DalCommon.GetPreDefineNextCodeByUrl("Purchase/Purchase");
                        objPurchase.SupplierID        = Convert.ToInt16(model.SupplierID);
                        objPurchase.SupplierAddressID = Convert.ToInt16(model.SupplierAddressID);
                        objPurchase.PurchaseCategory  = model.PurchaseCategory;
                        objPurchase.PurchaseType      = model.PurchaseType;
                        objPurchase.PurchaseYear      = model.PurchaseYear;
                        objPurchase.PurchaseDate      = DalCommon.SetDate(model.PurchaseDate);
                        objPurchase.PurchaseNote      = model.PurchaseNote;
                        objPurchase.RecordStatus      = "NCF";
                        objPurchase.SetOn             = DateTime.Now;
                        objPurchase.SetBy             = userId;

                        _context.Prq_Purchase.Add(objPurchase);
                        _context.SaveChanges();

                        CurrentPurchaseID = objPurchase.PurchaseID;

                        if (model.ChallanList != null)
                        {
                            Prq_PurchaseChallan objChallan = new Prq_PurchaseChallan();

                            objChallan.ChallanNo       = model.ChallanList.FirstOrDefault().ChallanNo;
                            objChallan.PurchaseID      = CurrentPurchaseID;
                            objChallan.SourceID        = model.ChallanList.FirstOrDefault().SourceID;
                            objChallan.LocationID      = model.ChallanList.FirstOrDefault().LocationID;
                            objChallan.ReceiveStore    = DalCommon.GetStoreCode(model.ChallanList.FirstOrDefault().ReceiveStore);
                            objChallan.ChallanCategory = model.ChallanList.FirstOrDefault().ChallanCategory;
                            objChallan.ChallanNote     = model.ChallanList.FirstOrDefault().ChallanNote;



                            objChallan.ChallanDate = Convert.ToDateTime(model.ChallanList.FirstOrDefault().ChallanDate);


                            objChallan.RecordStatus = "NCF";
                            objChallan.SetOn        = DateTime.Now;
                            objChallan.SetBy        = userId;

                            _context.Prq_PurchaseChallan.Add(objChallan);
                            _context.SaveChanges();

                            var CurrentChallanNo = objChallan.ChallanID;

                            if (model.ChallanItemList != null)
                            {
                                foreach (var ChallanItem in model.ChallanItemList)
                                {
                                    Prq_PurchaseChallanItem objPurchaseChallanItem = new Prq_PurchaseChallanItem();

                                    objPurchaseChallanItem.ChallanID    = CurrentChallanNo;
                                    objPurchaseChallanItem.ItemCategory = "Leather";
                                    objPurchaseChallanItem.ItemTypeID   = DalCommon.GetItemTypeCode(ChallanItem.ItemTypeID);
                                    objPurchaseChallanItem.ItemSizeID   = DalCommon.GetSizeCode(ChallanItem.ItemSizeID);
                                    objPurchaseChallanItem.Description  = ChallanItem.Description;
                                    objPurchaseChallanItem.UnitID       = DalCommon.GetUnitCode(ChallanItem.UnitID);
                                    objPurchaseChallanItem.ChallanQty   = ChallanItem.ChallanQty;
                                    objPurchaseChallanItem.ReceiveQty   = ChallanItem.ReceiveQty;
                                    objPurchaseChallanItem.Remark       = ChallanItem.Remark;
                                    objPurchaseChallanItem.RecordStatus = "NCF";
                                    objPurchaseChallanItem.SetBy        = userId;
                                    objPurchaseChallanItem.SetOn        = DateTime.Now;

                                    _context.Prq_PurchaseChallanItem.Add(objPurchaseChallanItem);
                                    _context.SaveChanges();
                                }
                            }
                        }
                    }
                    #endregion

                    transaction.Complete();
                }
                return(CurrentPurchaseID);
            }
            catch (Exception e)
            {
                return(CurrentPurchaseID);
            }
        }
        public int UpdatePurchaseInformation(PurchaseReceive model, int userId)
        {
            long CurrentChallanNo = 0;

            try
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    using (_context)
                    {
                        #region Purchase_Informaiton_Update
                        var currentPurchase = (from p in _context.Prq_Purchase.AsEnumerable()
                                               where p.PurchaseID == Convert.ToInt64(model.PurchaseID)
                                               select p).FirstOrDefault();

                        if (currentPurchase.RecordStatus != "CNF")
                        {
                            currentPurchase.SupplierID        = Convert.ToInt32(model.SupplierID);
                            currentPurchase.SupplierAddressID = Convert.ToInt32(model.SupplierAddressID);
                            currentPurchase.PurchaseCategory  = model.PurchaseCategory;
                            currentPurchase.PurchaseType      = model.PurchaseType;
                            currentPurchase.PurchaseYear      = model.PurchaseYear;
                            currentPurchase.PurchaseNote      = model.PurchaseNote;
                            currentPurchase.RecordStatus      = "NCF";

                            currentPurchase.PurchaseDate = DalCommon.SetDate(model.PurchaseDate);
                            currentPurchase.SetBy        = userId;
                            currentPurchase.SetOn        = DateTime.Now;
                            _context.SaveChanges();
                        }

                        #endregion

                        #region Update_Challan_Information
                        if (model.ChallanList != null)
                        {
                            foreach (var challan in model.ChallanList)
                            {
                                #region New_Challan_Insertion
                                if (challan.ChallanID == 0)
                                {
                                    Prq_PurchaseChallan objChallan = new Prq_PurchaseChallan();

                                    objChallan.ChallanNo       = challan.ChallanNo;
                                    objChallan.PurchaseID      = Convert.ToInt32(model.PurchaseID);
                                    objChallan.SourceID        = challan.SourceID;
                                    objChallan.LocationID      = challan.LocationID;
                                    objChallan.ReceiveStore    = DalCommon.GetStoreCode(challan.ReceiveStore);
                                    objChallan.ChallanCategory = challan.ChallanCategory;
                                    objChallan.ChallanNote     = challan.ChallanNote;
                                    objChallan.ChallanDate     = Convert.ToDateTime(challan.ChallanDate);
                                    objChallan.RecordStatus    = "NCF";
                                    objChallan.SetOn           = DateTime.Now;
                                    objChallan.SetBy           = userId;;

                                    _context.Prq_PurchaseChallan.Add(objChallan);
                                    _context.SaveChanges();

                                    CurrentChallanNo = objChallan.ChallanID;
                                }
                                #endregion

                                #region Existing_Challan_Update
                                else if (challan.ChallanID != 0 && challan.RecordStatus != "CNF" && challan.RecordStatus != "Confirmed")
                                {
                                    var currentChallan = (from c in _context.Prq_PurchaseChallan.AsEnumerable()
                                                          where c.ChallanID == challan.ChallanID
                                                          select c).FirstOrDefault();

                                    currentChallan.ChallanNo = challan.ChallanNo;
                                    //currentChallan.PurchaseID = Convert.ToInt32(model.PurchaseID);
                                    currentChallan.SourceID        = challan.SourceID;
                                    currentChallan.LocationID      = challan.LocationID;
                                    currentChallan.ReceiveStore    = DalCommon.GetStoreCode(challan.ReceiveStore);
                                    currentChallan.ChallanCategory = challan.ChallanCategory;
                                    currentChallan.ChallanNote     = challan.ChallanNote;

                                    try
                                    {
                                        //var GridChallanDate = Convert.ToDateTime(challan.ChallanDate).Date.ToString("dd/MM/yyyy");
                                        //currentChallan.ChallanDate = DalCommon.SetDate(GridChallanDate);

                                        var GridChallanDate = challan.ChallanDate.Contains("/") ? challan.ChallanDate : Convert.ToDateTime(challan.ChallanDate).ToString("dd/MM/yyyy");
                                        //var GridChallanDate = Convert.ToDateTime(challan.ChallanDate).ToString().Contains("/") ? challan.ChallanDate : Convert.ToDateTime(challan.ChallanDate).ToString("dd/MM/yyyy");
                                        currentChallan.ChallanDate = DalCommon.SetDate(GridChallanDate);
                                    }
                                    catch
                                    {
                                        var GridChallanDate = Convert.ToDateTime(challan.ChallanDate).Date.ToString("dd/MM/yyyy");
                                        currentChallan.ChallanDate = DalCommon.SetDate(GridChallanDate);
                                        //currentChallan.ChallanDate = DalCommon.SetDate(challan.ChallanDate);
                                    }



                                    currentChallan.RecordStatus = "NCF";
                                    currentChallan.SetOn        = DateTime.Now;
                                    currentChallan.SetBy        = userId;

                                    _context.SaveChanges();
                                }
                                #endregion
                            }
                        }

                        #endregion

                        #region To_Find_ChallanID_For_Items_If_Any

                        if (model.ChallanItemList != null)
                        {
                            foreach (var challanItem in model.ChallanItemList)
                            {
                                if (challanItem.ChallanID != 0)
                                {
                                    CurrentChallanNo = challanItem.ChallanID;
                                    break;
                                }
                            }
                        }
                        #endregion

                        #region Update_Challan_Item_Information
                        if (model.ChallanItemList != null)
                        {
                            foreach (var challanItem in model.ChallanItemList)
                            {
                                #region New_Challan_Item_Insertion
                                if (challanItem.ChallanItemID == 0)
                                {
                                    Prq_PurchaseChallanItem objPurchaseChallanItem = new Prq_PurchaseChallanItem();

                                    objPurchaseChallanItem.ChallanID    = CurrentChallanNo;
                                    objPurchaseChallanItem.ItemCategory = "Leather";
                                    objPurchaseChallanItem.ItemTypeID   = DalCommon.GetItemTypeCode(challanItem.ItemTypeID);
                                    objPurchaseChallanItem.ItemSizeID   = DalCommon.GetSizeCode(challanItem.ItemSizeID);
                                    objPurchaseChallanItem.Description  = challanItem.Description;
                                    objPurchaseChallanItem.UnitID       = DalCommon.GetUnitCode(challanItem.UnitID);
                                    objPurchaseChallanItem.ChallanQty   = challanItem.ChallanQty;
                                    objPurchaseChallanItem.ReceiveQty   = challanItem.ReceiveQty;
                                    objPurchaseChallanItem.Remark       = challanItem.Remark;
                                    objPurchaseChallanItem.RecordStatus = "NCF";
                                    objPurchaseChallanItem.SetBy        = userId;;
                                    objPurchaseChallanItem.SetOn        = DateTime.Now;

                                    _context.Prq_PurchaseChallanItem.Add(objPurchaseChallanItem);
                                    _context.SaveChanges();
                                }
                                #endregion

                                #region Update_Existing_Challan_Item
                                else if (challanItem.ChallanItemID != 0 && challanItem.RecordStatus != "CNF" && challanItem.RecordStatus != "Confirmed")
                                {
                                    var currentChallanItem = (from c in _context.Prq_PurchaseChallanItem.AsEnumerable()
                                                              where c.ChallanItemID == challanItem.ChallanItemID
                                                              select c).FirstOrDefault();

                                    currentChallanItem.ItemSizeID  = DalCommon.GetSizeCode(challanItem.ItemSizeID);
                                    currentChallanItem.ItemTypeID  = DalCommon.GetItemTypeCode(challanItem.ItemTypeID);
                                    currentChallanItem.UnitID      = DalCommon.GetUnitCode(challanItem.UnitID);
                                    currentChallanItem.Description = challanItem.Description;
                                    currentChallanItem.ChallanQty  = challanItem.ChallanQty;
                                    currentChallanItem.ReceiveQty  = challanItem.ReceiveQty;
                                    currentChallanItem.Remark      = challanItem.Remark;
                                    _context.SaveChanges();
                                }
                                #endregion
                            }
                        }
                        #endregion
                    }

                    transaction.Complete();
                }
                return(1);
            }
            catch (Exception e)
            {
                return(0);
            }
        }