/// <summary>
        /// This method is used for save new  supplier return request in database. - JJ
        /// </summary>
        /// <param name="SupplierReturnRequest">object of SupplierReturnRequest</param>
        /// <param name="company">object of CompanyDetail</param>
        /// <param name="user">object of UserDetail</param>
        /// <returns>object of SupplierReturnRequest</returns>

        public SupplierReturnRequest SaveSupplierReturnRequest(SupplierReturnRequest SupplierReturnRequest, UserDetail user, CompanyDetail company)
        {
            try
            {
                var status = CheckSupplierConditions(SupplierReturnRequest.SupplierId, SupplierReturnRequest.SupplierReturnItemAC);
                if (status == "ok")
                {
                    var ParentRecordId = 0;
                    var workFlowLog    = _IWorkFlowDetailsRepository.GetInitiationActionWorkFlow(StringConstants.SupplierReturnRequest, StringConstants.InitiateSupplierReturnRequest, user, company, null, SupplierReturnRequest.InitiationComment, null);
                    if (workFlowLog != null)
                    {
                        WorkFlowLog    log            = (WorkFlowLog)workFlowLog.Item1;
                        WorkFlowDetail workFlowDetail = (WorkFlowDetail)workFlowLog.Item2;
                        ParentRecordId = log.RecordId;
                        var requestNo            = RequestNumberGenerator(company.Id);
                        var supplierReturnDetail = new SupplierReturnDetail
                        {
                            CreatedDateTime   = DateTime.UtcNow,
                            IsRejected        = false,
                            IsDeleted         = false,
                            RecordId          = ParentRecordId,
                            LastActivityDate  = DateTime.UtcNow,
                            SupplierId        = SupplierReturnRequest.SupplierId,
                            RequestNo         = requestNo,
                            BranchId          = SupplierReturnRequest.BranchId,
                            InitiatorId       = user.Id,
                            InitiationComment = SupplierReturnRequest.InitiationComment
                        };
                        _supplierReturnDetailContext.Add(supplierReturnDetail);
                        _supplierReturnDetailContext.SaveChanges();

                        foreach (var item in SupplierReturnRequest.SupplierReturnItemAC)
                        {
                            var supplierReturnItem = new SupplierReturnItem
                            {
                                CostPrice        = item.CostPrice,
                                CreatedDateTime  = DateTime.UtcNow,
                                ItemId           = item.ItemId,
                                ReturnQuantity   = item.ReturnQuantity,
                                SupplierReturnId = supplierReturnDetail.Id,
                                ReturnCauseId    = item.ReturnCauseId
                            };
                            _supplierReturnItemContext.Add(supplierReturnItem);
                            _supplierReturnItemContext.SaveChanges();

                            var itemProfile = _itemProfileContext.Find(item.ItemId);
                            itemProfile.IsSupplierReturnRequestGenerated = true;
                            _itemProfileContext.Update(itemProfile);
                            _itemProfileContext.SaveChanges();
                        }
                        SupplierReturnRequest.SupplierReturnId = supplierReturnDetail.Id;
                        var companyid = _branchDetailContext.Find(supplierReturnDetail.BranchId).CompanyId;
                        if (workFlowDetail.NextActivity.IsClosed)
                        {
                            var outcome = _ISupReturnWorkListRepository.IssueCreditNote(supplierReturnDetail.Id, supplierReturnDetail.BranchId, companyid, supplierReturnDetail.InitiationComment);
                            if (outcome)
                            {
                                SupplierReturnRequest.Status = "print";
                            }
                            else
                            {
                                SupplierReturnRequest.Status = "Error in Issueing Credit Note";
                            }
                            return(SupplierReturnRequest);
                        }
                        else
                        {
                            SupplierReturnRequest.Status = "ok";
                            return(SupplierReturnRequest);
                        }
                    }
                    else
                    {
                        SupplierReturnRequest.Status = StringConstants.WorkFlowNotCreated;
                        return(SupplierReturnRequest);
                    }
                }
                else
                {
                    SupplierReturnRequest.Status = status;
                    return(SupplierReturnRequest);
                }
            }
            catch (Exception ex)
            {
                _errorLog.LogException(ex);
                throw;
            }
        }
        /// <summary>
        /// This method is used for update supplier return request in database. - JJ
        /// </summary>
        /// <param name="SupplierReturnRequest">object of SupplierReturnRequest</param>
        /// <param name="company">object of CompanyDetail</param>
        /// <param name="user">object of UserDetail</param>
        /// <returns>status</returns>
        public string UpdateSupplierReturnRequest(SupplierReturnRequest SupplierReturnRequest, UserDetail user, CompanyDetail company)
        {
            try
            {
                var workFlowLog    = new WorkFlowLog();
                var supplierReturn = _supplierReturnDetailContext.Fetch(x => x.RecordId == SupplierReturnRequest.RecordId).FirstOrDefault();
                if (supplierReturn.IsRejected || supplierReturn.IsDeleted)
                {
                    if (supplierReturn.IsRejected)
                    {
                        return("Supplier Return Request is Rejected. You cannot Resubmit or Edit it");
                    }
                    else
                    {
                        return("Supplier Return Request is Deleted. You cannot Resubmit or Edit it");
                    }
                }
                else
                {
                    var status = CheckSupplierConditions(SupplierReturnRequest.SupplierId, SupplierReturnRequest.SupplierReturnItemAC);
                    if (status == "ok")
                    {
                        if (SupplierReturnRequest.IsResubmit)
                        {
                            workFlowLog = _IWorkFlowDetailsRepository.GetResubmitActionWorkFlow(SupplierReturnRequest.RecordId, SupplierReturnRequest, SupplierReturnRequest, StringConstants.ReSubmitedAction, SupplierReturnRequest.Comment, user);
                        }
                        else
                        {
                            workFlowLog = _IWorkFlowDetailsRepository.GetResubmitActionWorkFlow(SupplierReturnRequest.RecordId, SupplierReturnRequest, SupplierReturnRequest, StringConstants.EditedAction, SupplierReturnRequest.InitiationComment, user);
                        }
                        if (workFlowLog != null)
                        {
                            supplierReturn.SupplierId       = SupplierReturnRequest.SupplierId;
                            supplierReturn.LastActivityDate = DateTime.UtcNow;
                            supplierReturn.ModifiedDateTime = DateTime.UtcNow;
                            _supplierReturnDetailContext.Update(supplierReturn);
                            _supplierReturnDetailContext.SaveChanges();

                            DeleteSupplierReturnItem(supplierReturn.Id);

                            foreach (var item in SupplierReturnRequest.SupplierReturnItemAC)
                            {
                                var supplierReturnItem = new SupplierReturnItem
                                {
                                    CostPrice        = item.CostPrice,
                                    CreatedDateTime  = DateTime.UtcNow,
                                    ItemId           = item.ItemId,
                                    ReturnQuantity   = item.ReturnQuantity,
                                    SupplierReturnId = SupplierReturnRequest.SupplierReturnId,
                                    ReturnCauseId    = item.ReturnCauseId
                                };
                                _supplierReturnItemContext.Add(supplierReturnItem);
                                _supplierReturnItemContext.SaveChanges();

                                var itemProfile = _itemProfileContext.Find(item.ItemId);
                                itemProfile.IsSupplierReturnRequestGenerated = true;
                                itemProfile.ModifiedDateTime = DateTime.UtcNow;
                                _itemProfileContext.Update(itemProfile);
                                _itemProfileContext.SaveChanges();
                            }
                            return("ok");
                        }
                        else
                        {
                            return(StringConstants.WorkFlowNotCreated);
                        }
                    }
                    else
                    {
                        return(status);
                    }
                }
            }
            catch (Exception ex)
            {
                _errorLog.LogException(ex);
                throw;
            }
        }