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);
        }