public ActionResult Save(SlsSalesReturnViewModel objT)
        {
            int userId = Convert.ToInt32(Session["userId"]);
            int companyId = Convert.ToInt32(Session["companyId"]);
            Operation objOperation = new Operation { Success = false };

            if (ModelState.IsValid)
            {

                //Get store id of logged in user.
                var dbfactory = new DatabaseFactory();
                IOfficeService offservice = new OfficeService(new OfficeRepository(dbfactory), new UnitOfWork(dbfactory));
                SlsOffice off = offservice.GetUserOffice(userId);
                int officeId = off.Id;
                IStoreService storeservice = new StoreService(new InvStoreRepository(dbfactory), new UnitOfWork(dbfactory));
                InvStore store = storeservice.GetStoresForOffice(officeId);
                int storeId = store.Id;

                if (objT.Id == 0)
                {
                    if ((bool)Session["Add"])
                    {
                        objT.CreatedBy = userId;
                        objT.CreatedDate = DateTime.Now.Date;
                        objT.SecCompanyId = companyId;
                        objOperation = _SalesReturnService.Save(objT, storeId);
                    }
                    else { objOperation.OperationId = -1; }

                }
                else
                {
                    if ((bool)Session["Edit"])
                    {
                        objT.ModifiedBy = userId;
                        objT.ModifiedDate = DateTime.Now.Date;
                        objOperation = _SalesReturnService.Update(objT, storeId);
                    }
                    else { objOperation.OperationId = -2; }
                }
            }
            return Json(objOperation, JsonRequestBehavior.DenyGet);
        }
        public ActionResult Save(InvProductReceive ProductRecievedobj, List<InvProductReceiveDetail> ProductRecievedDetailList)
        {
            int userId = Convert.ToInt32(Session["userId"]);
            int companyId = Convert.ToInt32(Session["companyId"]);
            var dbfactory = new DatabaseFactory();

            IOfficeService offservice = new OfficeService(new OfficeRepository(dbfactory), new UnitOfWork(dbfactory));
            SlsOffice off = offservice.GetUserOffice(userId);

            int officeId = off.Id;
            IStoreService storeservice = new StoreService(new InvStoreRepository(dbfactory), new UnitOfWork(dbfactory));
            InvStore store = storeservice.GetStoresForOffice(officeId);

            int storeId = store.Id;
            Operation objOperation = new Operation { Success = false };

            if (ModelState.IsValid && ProductRecievedDetailList != null)
            {
                if (ProductRecievedobj.Id == 0)
                {
                    if ((bool)Session["Add"])
                    {
                        ProductRecievedobj.InvStoreId = storeId;
                        ProductRecievedobj.CreatedBy = userId;
                        ProductRecievedobj.CreatedDate = DateTime.Now;
                        objOperation = _ProductReceiveService.Save(ProductRecievedobj);

                        int ProductReceiveId = Convert.ToInt32(objOperation.OperationId);

                        foreach (var item in ProductRecievedDetailList)
                        {

                            InvProductReceiveDetail objInvProductReceiveDetail = _ProductReceiveDetailService.GetById(item.Id);

                            if (objInvProductReceiveDetail != null)
                            {
                                objInvProductReceiveDetail.InvProductReceiveId = ProductRecievedobj.Id;
                                objInvProductReceiveDetail.SlsProductId = item.SlsProductId;
                                objInvProductReceiveDetail.ReceivedQuantity = item.ReceivedQuantity;
                                objInvProductReceiveDetail.IssuedQuantity = item.IssuedQuantity;
                                objInvProductReceiveDetail.SlsUnitId = item.SlsUnitId;
                                objInvProductReceiveDetail.Remarks = item.Remarks;
                                _ProductReceiveDetailService.Update(objInvProductReceiveDetail);
                            }
                            else
                            {
                                objInvProductReceiveDetail = new InvProductReceiveDetail();
                                objInvProductReceiveDetail.InvProductReceiveId = ProductReceiveId;
                                objInvProductReceiveDetail.SlsProductId = item.SlsProductId;
                                objInvProductReceiveDetail.ReceivedQuantity = item.ReceivedQuantity;
                                objInvProductReceiveDetail.IssuedQuantity = item.IssuedQuantity;
                                objInvProductReceiveDetail.SlsUnitId = item.SlsUnitId;
                                objInvProductReceiveDetail.Remarks = item.Remarks;
                                _ProductReceiveDetailService.Save(objInvProductReceiveDetail);

                                //InvStockInOut objStockOut = new InvStockInOut();
                                //objStockOut.TransactionType = 1;
                                //objStockOut.RefId = ProductReceiveId;
                                //objStockOut.Status = 0;
                                //objStockOut.SlsProductId = item.SlsProductId.Value;
                                //objStockOut.Quantity = item.ReceivedQuantity.Value;
                                //objStockOut.SlsUnitId = item.SlsUnitId.Value;

                            }

                        }

                    }

                }
                else
                {
                    if ((bool)Session["Edit"])
                    {
                        ProductRecievedobj.InvStoreId = storeId;
                        ProductRecievedobj.ModifiedBy = userId;
                        ProductRecievedobj.ModifiedDate = DateTime.Now;
                        objOperation = _ProductReceiveService.Update(ProductRecievedobj);

                        foreach (var item in ProductRecievedDetailList)
                        {
                            InvProductReceiveDetail objInvProductReceiveDetail = _ProductReceiveDetailService.GetById(item.Id);
                            if (objInvProductReceiveDetail != null)
                            {
                                objInvProductReceiveDetail.InvProductReceiveId = ProductRecievedobj.Id;
                                objInvProductReceiveDetail.SlsProductId = item.SlsProductId;
                                objInvProductReceiveDetail.ReceivedQuantity = item.ReceivedQuantity;
                                objInvProductReceiveDetail.IssuedQuantity = item.IssuedQuantity;
                                objInvProductReceiveDetail.SlsUnitId = item.SlsUnitId;
                                objInvProductReceiveDetail.Remarks = item.Remarks;
                                _ProductReceiveDetailService.Update(objInvProductReceiveDetail);
                            }
                            else
                            {
                                objInvProductReceiveDetail = new InvProductReceiveDetail();
                                objInvProductReceiveDetail.InvProductReceiveId = ProductRecievedobj.Id;
                                objInvProductReceiveDetail.SlsProductId = item.SlsProductId;
                                objInvProductReceiveDetail.ReceivedQuantity = item.ReceivedQuantity;
                                objInvProductReceiveDetail.IssuedQuantity = item.IssuedQuantity;
                                objInvProductReceiveDetail.SlsUnitId = item.SlsUnitId;
                                objInvProductReceiveDetail.Remarks = item.Remarks;
                                _ProductReceiveDetailService.Save(objInvProductReceiveDetail);
                            }

                        }

                    }
                }
                objOperation = _ProductReceiveService.Commit();

            }

            return Json(objOperation, JsonRequestBehavior.DenyGet);
        }
        public ActionResult GetAreaForConfiguration(int basedOn = 0, int selemployeeId = 0, int selRegionId = 0,
            int selOfficeId = 0, int selDistrictId = 0, int selThanaId = 0)
        {
            //var basedOn = 1;
            //var employeeId = 2;
            //var selRegionId = 1;
            //var selOfficeId = 1;
            //var selDistrictId = 1;
            //var selThanaId = 1;

            var dbfactory = new DatabaseFactory();
            IRegionService _RegionService = new RegionService(new RegionRepository(dbfactory), new UnitOfWork(dbfactory));
            IOfficeService _OfficeService = new OfficeService(new OfficeRepository(dbfactory), new UnitOfWork(dbfactory));
            IDistrictService _DistrictService = new DistrictService(new DistrictRepository(dbfactory), new UnitOfWork(dbfactory));
            IThanaService _ThanaService = new ThanaService(new ThanaRepository(dbfactory), new UnitOfWork(dbfactory));
            IAreaService _AreaService = new AreaService(new AreaRepository(dbfactory), new UnitOfWork(dbfactory));

            IList<int> areaConfiguredForEmp = new List<int>();
            var allAreaConfigurations = _areaConfigurationService.GetAll();
            if (selemployeeId > 0)
            {
                if (allAreaConfigurations != null && allAreaConfigurations.Count() > 0)
                    allAreaConfigurations = allAreaConfigurations.Where(i => i.HrmEmployeeId == selemployeeId).ToList();
                if (allAreaConfigurations != null && allAreaConfigurations.Count() > 0)
                    areaConfiguredForEmp = allAreaConfigurations.Select(i => i.Id).ToList();
            }
            var allAreaConfigurationDetailss = _areaConfigurationDetailService.GetAll();
            if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                allAreaConfigurationDetailss = allAreaConfigurationDetailss.Where(i => areaConfiguredForEmp.Contains(i.SlsAreaConfigurationId)).ToList();
            var allRegions = _RegionService.GetAll();
            var allOffices = _OfficeService.GetAll();
            var allDistricts = _DistrictService.GetAll();
            var allThanas = _ThanaService.GetAll();
            var allAreas = _AreaService.GetAll();

            IList<AreaConfigurationViewModel> areaConfigurations = null;

            switch (basedOn)
            {
                case 1:
                    IList<int> regionsConfigured = new List<int>();
                    if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                        regionsConfigured = allAreaConfigurationDetailss.Where(i => i.BasedOn == "Region").Select(j => j.RefId).ToList();

                    areaConfigurations = allRegions.Select(i => new AreaConfigurationViewModel()
                    {
                        Id = i.Id,
                        Code = i.Code,
                        Name = i.Name,
                        IsRegionBased = true,
                        Status = regionsConfigured.Contains(i.Id) ? true : false
                    }).Distinct().ToList();
                    break;
                case 2:
                    IList<int> officesConfigured = new List<int>();
                    if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                        officesConfigured = allAreaConfigurationDetailss.Where(i => i.BasedOn == "Office").Select(j => j.RefId).ToList();
                    if (selRegionId > 0)
                        allOffices = allOffices.Where(i => i.SlsRegionId == selRegionId).ToList();

                    areaConfigurations = allOffices.Select(i => new AreaConfigurationViewModel()
                    {
                        Id = i.Id,
                        Code = i.Code,
                        Name = i.Name,
                        IsOfficeBased = true,
                        Status = officesConfigured.Contains(i.Id) ? true : false
                    }).Distinct().ToList();
                    break;
                case 3:
                    IList<int> districtsConfigured = new List<int>();
                    if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                        districtsConfigured = allAreaConfigurationDetailss.Where(i => i.BasedOn == "District").Select(j => j.RefId).ToList();
                    if (selOfficeId > 0)
                        allDistricts = allDistricts.Where(i => i.SlsOfficeId == selOfficeId).ToList();
                    else if (selRegionId > 0)
                    {
                        IList<int> selOffices = allOffices.Where(i => i.SlsRegionId == selRegionId).Select(j => j.Id).ToList();
                        IList<SlsDistrict> districts = new List<SlsDistrict>();
                        foreach (var item in allDistricts)
                        {
                            if (item.SlsOfficeId != null)
                                districts.Add(item);
                        }
                        allDistricts = districts;
                        allDistricts = allDistricts.Where(i => selOffices.Contains((int)i.SlsOfficeId)).ToList();
                    }

                    areaConfigurations = allDistricts.Select(i => new AreaConfigurationViewModel()
                    {
                        Id = i.Id,
                        Code = i.Code,
                        Name = i.Name,
                        IsOfficeBased = true,
                        Status = districtsConfigured.Contains(i.Id) ? true : false
                    }).Distinct().ToList();
                    break;
                case 4:
                    IList<int> thanasConfigured = new List<int>();
                    if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                        thanasConfigured = allAreaConfigurationDetailss.Where(i => i.BasedOn == "Thana").Select(j => j.RefId).ToList();
                    if (selDistrictId > 0)
                        allThanas = allThanas.Where(i => i.SlsDistrictId == selDistrictId).ToList();
                    else if (selOfficeId > 0)
                    {
                        IList<int> selDistricts = allDistricts.Where(i => i.SlsOfficeId == selOfficeId).Select(j => j.Id).ToList();
                        allThanas = allThanas.Where(i => selDistricts.Contains((int)i.SlsDistrictId)).ToList();
                    }
                    else if (selRegionId > 0)
                    {
                        IList<int> selOffices = allOffices.Where(i => i.SlsRegionId == selRegionId).Select(j => j.Id).ToList();
                        IList<SlsDistrict> districts = new List<SlsDistrict>();
                        foreach (var item in allDistricts)
                        {
                            if (item.SlsOfficeId != null)
                                districts.Add(item);
                        }
                        allDistricts = districts;
                        allDistricts = allDistricts.Where(i => selOffices.Contains((int)i.SlsOfficeId)).ToList();
                        if (allDistricts != null)
                        {
                            IList<int> selDistricts = allDistricts.Select(i => i.Id).ToList();
                            allThanas = allThanas.Where(i => selDistricts.Contains((int)i.SlsDistrictId)).ToList();
                        }
                    }

                    areaConfigurations = allThanas.Select(i => new AreaConfigurationViewModel()
                    {
                        Id = i.Id,
                        Code = i.Code,
                        Name = i.Name,
                        IsOfficeBased = true,
                        Status = thanasConfigured.Contains(i.Id) ? true : false
                    }).Distinct().ToList();
                    break;
                case 5:
                    IList<int> areasConfigured = new List<int>();
                    if (allAreaConfigurationDetailss != null && allAreaConfigurationDetailss.Count() > 0)
                        areasConfigured = allAreaConfigurationDetailss.Where(i => i.BasedOn == "Area").Select(j => j.RefId).ToList();

                    if (selThanaId > 0)
                        allAreas = allAreas.Where(i => i.SlsThanaId == selThanaId).ToList();
                    else if (selDistrictId > 0)
                    {
                        IList<int> selThanas = allThanas.Where(i => i.SlsDistrictId == selDistrictId).Select(j => j.Id).ToList();
                        allAreas = allAreas.Where(i => selThanas.Contains((int)i.SlsThanaId)).ToList();
                    }
                    else if (selOfficeId > 0)
                    {
                        IList<int> selDistricts = allDistricts.Where(i => i.SlsOfficeId == selOfficeId).Select(j => j.Id).ToList();
                        allThanas = allThanas.Where(i => selDistricts.Contains((int)i.SlsDistrictId)).ToList();
                        if (allThanas != null)
                        {
                            IList<int> selThanas = allThanas.Select(j => j.Id).ToList();
                            allAreas = allAreas.Where(i => selThanas.Contains((int)i.SlsThanaId)).ToList();
                        }
                    }
                    else if (selRegionId > 0)
                    {
                        IList<int> selOffices = allOffices.Where(i => i.SlsRegionId == selRegionId).Select(j => j.Id).ToList();
                        IList<SlsDistrict> districts = new List<SlsDistrict>();
                        foreach (var item in allDistricts)
                        {
                            if (item.SlsOfficeId != null)
                                districts.Add(item);
                        }
                        allDistricts = districts;
                        allDistricts = allDistricts.Where(i => selOffices.Contains((int)i.SlsOfficeId)).ToList();
                        if (allDistricts != null)
                        {
                            IList<int> selDistricts = allDistricts.Select(i => i.Id).ToList();
                            allThanas = allThanas.Where(i => selDistricts.Contains((int)i.SlsDistrictId)).ToList();
                            if (allThanas != null)
                            {
                                IList<int> selThanas = allThanas.Select(j => j.Id).ToList();
                                allAreas = allAreas.Where(i => selThanas.Contains((int)i.SlsThanaId)).ToList();
                            }
                        }
                    }

                    areaConfigurations = allAreas.Select(i => new AreaConfigurationViewModel()
                    {
                        Id = i.Id,
                        Code = i.Code,
                        Name = i.Name,
                        IsOfficeBased = true,
                        Status = areasConfigured.Contains(i.Id) ? true : false
                    }).Distinct().ToList();
                    break;
                default:
                    areaConfigurations = new Collection<AreaConfigurationViewModel>();
                    break;
            }

            return Json(areaConfigurations, JsonRequestBehavior.AllowGet);
        }