public bool EditCreditRequestForOnlinePayment(CreditRequestDTO creditRequestDTO, bool IsSuccess)
 {
     try
     {
         if (creditRequestDTO.PaymentId == null)
         {
             return false;
         }
         return CreditRequestService.EditCreditRequestForOnlinePayment(creditRequestDTO, IsSuccess);
     }
     catch (TimeoutException)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.RequestTimeout)
         {
             Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
 public CreditRequestDTO GenerateBill(CreditRequestDTO CreditRequestDTO)
 {
     try
     {
         return CreditRequestService.GenerateBill(CreditRequestDTO);
     }
     catch (TimeoutException)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.RequestTimeout)
         {
             Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
     catch (Exception)
     {
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
             ReasonPhrase = "Critical Exception"
         });
     }
 }
        public PaymentDTO GetPaymentDetailsURLByPaimentId(string paymentId)
        {
            try
            {
                payment OnlinePaymentDTO = new payment();  //OnlinePaymentDTO

                var queryString = HttpContext.Current.Request.QueryString;
                string payment_id = paymentId;
                string PaymentDetailUrl = ConfigurationManager.AppSettings["PaymentDetailsUrl"].ToString();//  "https://test.instamojo.com/api/1.1/payments/[payment_id]?api_key=4709c5655f99d3799ccc22d8da23a137&auth_token=820cb88637df64e703782970c729a5fb";
                PaymentDetailUrl = PaymentDetailUrl.Replace("%26", "&");
                PaymentDetailUrl = PaymentDetailUrl.Replace("[payment_id]", payment_id);

                WebRequest request = HttpWebRequest.Create(PaymentDetailUrl);
                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string responseText = reader.ReadToEnd();
                //responseText = responseText.Replace("\n","");

                PaymentDTO PaymentDTO = JsonConvert.DeserializeObject<PaymentDTO>(responseText);
                if (PaymentDTO != null)
                {
                    if (PaymentDTO.payment.custom_fields != null)
                    {
                        int CreditRequestId = Convert.ToInt32( PaymentDTO.payment.custom_fields.Field_52722.value);
                        CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                        CreditRequestDTO = CreditRequestService.GetById(CreditRequestId);
                        if (PaymentDTO.success == true)
                        {
                            if (CreditRequestDTO.IsPaid != true)
                            {
                                CreditRequestDTO.PaymentId = PaymentDTO.payment.payment_id;
                                CreditRequestDTO.ProvidedCredit = CreditRequestDTO.RequestedCredit;
                                CreditRequestService.EditCreditRequestForOnlinePayment(CreditRequestDTO, true);
                            }
                        }
                        else
                        {
                            CreditRequestDTO.PaymentId = PaymentDTO.payment.payment_id;
                            CreditRequestDTO.IsPaid = false;
                            CreditRequestService.EditCreditRequestForOnlinePayment(CreditRequestDTO, false);
                        }

                    }
                }
               // PaymentInfoDTO PaymentInfoDTO = JsonConvert.DeserializeObject<PaymentInfoDTO>(responseText);

                // OnlinePaymentDTO = urlpaymentId;
                //return PaymentDetailUrl;
                return PaymentDTO;// OnlinePaymentDTO;

            }
            catch (TimeoutException)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.RequestTimeout)
                {
                    Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
                    ReasonPhrase = "Critical Exception"
                });
            }
            catch (Exception)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("An error occurred, please try again or CreditRequest the administrator."),
                    ReasonPhrase = "Critical Exception"
                });
            }
        }
        //Get credit request list by  client id
        public static List<CreditRequestDTO> GetCreditRequestsbyClientId(int ClientId, PagingInfo pagingInfo)
        {
            List<CreditRequestDTO> CreditRequestDTOList = new List<CreditRequestDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                int take = pagingInfo.ItemsPerPage;

                IQueryable<CreditRequest> CreditRequest = uow.CreditRequestRepo.GetAll(skip, take).Where(e => e.ClientId == ClientId).AsQueryable();//.OrderByDescending(e => e.Date).ToList();
                CreditRequest = PagingService.Sorting<CreditRequest>(CreditRequest, pagingInfo.SortBy, pagingInfo.Reverse);
                CreditRequest = CreditRequest.Skip(skip).Take(take);

                if (CreditRequest != null)
                {
                    foreach (var item in CreditRequest)
                    {
                        CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                        CreditRequestDTO = GetById(item.Id);

                        //ClientDTO ClientDTO = new ClientDTO();
                        //ClientDTO = ClientService.GetById(item.ClientId);
                        //CreditRequestDTO.ClientName = ClientDTO.Company;

                        //UserDTO UserDTO = new UserDTO();
                        //UserDTO = UserService.GetById(item.RequestedBy);
                        //CreditRequestDTO.UserName = UserDTO.Name;

                        CreditRequestDTOList.Add(CreditRequestDTO);

                    }
                }

                if (CreditRequestDTOList.Count > 0)
                {
                    if (pagingInfo.Search != "" & pagingInfo.Search != null)
                    {

                        bool IsDate = CommonService.IsDate(pagingInfo.Search);
                        if (IsDate != true)
                        {
                            // string search
                            // var CreditRequestsearch = CreditRequest.Where(e => e.Amount.ToString().Contains(search.ToLower()) || e.ClientId.ToString().Contains(search) || e.GrandTotal.ToString().Contains(search) || e.IsProvided.ToString().Contains(search) || e.OldBalance.ToString().Contains(search) || e.PartnerId.ToString().Contains(search) || e.RatePerSMS.ToString().Contains(search) || e.RequestedCredit.ToString().Contains(search) || e.Tax.ToString().Contains(search));
                            IQueryable<CreditRequest> CreditRequestsearch = uow.CreditRequestRepo.GetAll().Where(e => (e.OldBalance.ToString() == (pagingInfo.Search) || e.RatePerSMS.ToString() == (pagingInfo.Search) || e.RequestedCredit.ToString() == (pagingInfo.Search) || (e.Date.ToString() != null ? (Convert.ToDateTime(e.Date).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || (e.ProvidedDate.ToString() != null ? (Convert.ToDateTime(e.ProvidedDate).ToString("dd-MMM-yyyy").ToLower().Contains(pagingInfo.Search.ToLower())) : false) || e.ProvidedCredit.ToString() == (pagingInfo.Search)) && e.ClientId == ClientId).AsQueryable();//.OrderByDescending(e => e.Date).Skip(skip).Take(take); //|| e.UserName.ToLower().Contains(search.ToLower()) || e.ClientName.ToLower().Contains(search.ToLower())
                            CreditRequestsearch = PagingService.Sorting<CreditRequest>(CreditRequestsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            CreditRequestsearch = CreditRequestsearch.Skip(skip).Take(take);

                            if (CreditRequestsearch != null)
                            {
                                List<CreditRequestDTO> CreditRequestDTOListNew = new List<CreditRequestDTO>();
                                foreach (var item in CreditRequestsearch)
                                {
                                    CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                                    CreditRequestDTO = GetById(item.Id);
                                    CreditRequestDTOListNew.Add(CreditRequestDTO);

                                }

                                return CreditRequestDTOListNew;
                            }

                        }
                        else
                        {
                            //date wise search
                            DateTime date = Convert.ToDateTime(pagingInfo.Search);
                            IQueryable<CreditRequest> CreditRequestsearch = uow.CreditRequestRepo.GetAll().Where(e => (e.Date >= date && e.Date < date.AddDays(1) || e.ProvidedDate >= date && e.ProvidedDate < date.AddDays(1)) && e.ClientId == ClientId).OrderByDescending(e => e.Date).AsQueryable();//.Skip(skip).Take(take);
                            CreditRequestsearch = PagingService.Sorting<CreditRequest>(CreditRequestsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            CreditRequestsearch = CreditRequestsearch.Skip(skip).Take(take);
                            if (CreditRequestsearch != null)
                            {
                                List<CreditRequestDTO> CreditRequestDTOListNew = new List<CreditRequestDTO>();
                                foreach (var item in CreditRequestsearch)
                                {
                                    //if (item.IsProvided == IsProvided)
                                    //{
                                    CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                                    CreditRequestDTO = GetById(item.Id);
                                    CreditRequestDTOListNew.Add(CreditRequestDTO);
                                    //}
                                }

                                return CreditRequestDTOListNew;
                            }

                        }

                    }
                    else
                    {

                        return CreditRequestDTOList;

                    }
                }
                return CreditRequestDTOList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Create credit request
        public static CreditRequestDTO Create(CreditRequestDTO CreditRequestDTO)
        {
            try
            {
                CreditRequestDTO CreditRequestDTONew = new CreditRequestDTO();

                PartnerDTO PartnerDTO = new PartnerDTO();
                ClientDTO ClientDTO = new ClientDTO();
                UserDTO UserDTO = new UserDTO();

                PartnerDTO = PartnerService.GetById(CreditRequestDTO.PartnerId);
                ClientDTO = ClientService.GetById(CreditRequestDTO.ClientId);
                UserDTO = UserService.GetById(CreditRequestDTO.RequestedBy);

                GlobalSettings.LoggedInClientId = ClientDTO.Id;
                GlobalSettings.LoggedInUserId = UserDTO.Id;
                int PartnerId = PartnerDTO.Id;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                if (CreditRequestDTO.RequestedCredit <= 0)
                {
                    return null; //0
                }
                var CreditRequest = new CreditRequest();
                using (var uow = new UnitOfWork())
                {
                    CreditRequest = Transform.CreditRequestToDomain(CreditRequestDTO);
                    CreditRequest.Date = System.DateTime.Now;
                    CreditRequest.OldBalance = ClientDTO.SMSCredit;
                    CreditRequest.IsProvided = false;
                    //CreditRequest.ProvidedCredit = CreditRequest.RequestedCredit;
                    CreditRequest.Amount = CreditRequest.RequestedCredit * CreditRequest.RatePerSMS;
                    double tax = (CreditRequest.Amount * Convert.ToDouble(CreditRequest.Tax)) / 100;
                    CreditRequest.TaxAmount = tax;
                    CreditRequest.GrandTotal = CreditRequest.Amount + tax;

                    bool IsOnlinepaymentSuccess = Onlinepayment(CreditRequest.GrandTotal);

                    if (IsOnlinepaymentSuccess == true)
                    {
                        //CreditRequest.IsPaymentSuccessful = true;
                        //CreditRequest.PaymentDate = System.DateTime.Now;
                        CreditRequest.ProvidedDate = System.DateTime.Now;
                        CreditRequest.IsProvided = true;
                        CreditRequest.IsBillGenerated = true;
                        CreditRequest.PaymentMode = PaymentMode.Card;
                        CreditRequest.PaymentDate = System.DateTime.Now;
                        CreditRequest.IsPaid = true;
                        CreditRequest.ProvidedCredit = CreditRequest.RequestedCredit;

                        uow.CreditRequestRepo.Insert(CreditRequest);
                        uow.SaveChanges();

                        ClientDTO.SMSCredit = ClientDTO.SMSCredit + CreditRequest.RequestedCredit;
                        ClientService.Edit(ClientDTO);

                    }
                    else
                    {
                        //CreditRequest.IsPaymentSuccessful = false;
                        CreditRequest.IsPaid = false;
                        uow.CreditRequestRepo.Insert(CreditRequest);
                        uow.SaveChanges();
                    }

                    //Update TotalAppliedCredit
                    ClientDTO.TotalAppliedCredit = ClientDTO.TotalAppliedCredit + CreditRequest.RequestedCredit;
                    ClientService.Edit(ClientDTO);

                    //Generate Link to provide mail trhough mail
                    string APILink = ConfigurationManager.AppSettings["APILink"].ToString() + "api/CreditRequest/ProvideCreditByMailLink?CreditRequestId=" + CreditRequest.Id;

                    // Send Email To Partner
                    bool IsMailSent = CommonService.SendEmail("SMS Credit Request", "Hello " + PartnerDTO.Name + ", <br/><br/> The New request of " + CreditRequest.RequestedCredit + " credits applied by " + ClientDTO.Company + "<br/><br/> <a href=" + APILink + ">Approve</a>", PartnerDTO.Email, "", false);

                    CreditRequestDTONew = GetById(CreditRequest.Id);
                    if (CreditRequest.PaymentMode == PaymentMode.Card)
                    {
                        CreditRequestDTONew.OnlinePaymentURL = OnlinePaymentLinkWithTemperproofData(UserDTO.Email, CreditRequest.GrandTotal, UserDTO.Mobile, UserDTO.Name, CreditRequest.Id);
                    }
                    return (CreditRequestDTONew); //.Id);

                }

            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {
                throw;
            }
        }
        //Get credit request list by  client id
        public static List<CreditRequestDTO> GetCreditRequestsbyClientId(int ClientId, string search)
        {
            List<CreditRequestDTO> CreditRequestDTOList = new List<CreditRequestDTO>();
            try
            {
                UnitOfWork uow = new UnitOfWork();
                IEnumerable<CreditRequest> CreditRequest = uow.CreditRequestRepo.GetAll().Where(e => e.ClientId == ClientId).OrderByDescending(e => e.Date).ToList();
                if (CreditRequest != null)
                {
                    if (search != "" & search != null)
                    {

                        bool IsDate = CommonService.IsDate(search);
                        if (IsDate != true)
                        {
                            // string search
                            // var CreditRequestsearch = CreditRequest.Where(e => e.Amount.ToString().Contains(search.ToLower()) || e.ClientId.ToString().Contains(search) || e.GrandTotal.ToString().Contains(search) || e.IsProvided.ToString().Contains(search) || e.OldBalance.ToString().Contains(search) || e.PartnerId.ToString().Contains(search) || e.RatePerSMS.ToString().Contains(search) || e.RequestedCredit.ToString().Contains(search) || e.Tax.ToString().Contains(search));
                            var CreditRequestsearch = CreditRequest.Where(e => e.OldBalance.ToString() == (search) || e.RatePerSMS.ToString() == (search) || e.RequestedCredit.ToString() == (search)).OrderByDescending(e => e.Date);

                            if (CreditRequestsearch != null)
                            {
                                foreach (var item in CreditRequestsearch)
                                {
                                    CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                                    CreditRequestDTO = Transform.CreditRequestToDTO(item);

                                    ClientDTO ClientDTO = new ClientDTO();
                                    ClientDTO = ClientService.GetById(item.ClientId);
                                    CreditRequestDTO.ClientName = ClientDTO.Company;

                                    UserDTO UserDTO = new UserDTO();
                                    UserDTO = UserService.GetById(item.RequestedBy);
                                    CreditRequestDTO.UserName = UserDTO.Name;

                                    CreditRequestDTOList.Add(CreditRequestDTO);// (Transform.CreditRequestToDTO(item));
                                }
                            }
                            return CreditRequestDTOList;
                        }
                        else
                        {
                            //date wise search
                            DateTime date = Convert.ToDateTime(search);
                            var CreditRequestsearch = CreditRequest.Where(e => e.Date >= date && e.Date < date.AddDays(1) || e.ProvidedDate >= date && e.ProvidedDate < date.AddDays(1)).OrderByDescending(e => e.Date);

                            if (CreditRequestsearch != null)
                            {
                                foreach (var item in CreditRequestsearch)
                                {
                                    CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                                    CreditRequestDTO = Transform.CreditRequestToDTO(item);

                                    ClientDTO ClientDTO = new ClientDTO();
                                    ClientDTO = ClientService.GetById(item.ClientId);
                                    CreditRequestDTO.ClientName = ClientDTO.Company;

                                    UserDTO UserDTO = new UserDTO();
                                    UserDTO = UserService.GetById(item.RequestedBy);
                                    CreditRequestDTO.UserName = UserDTO.Name;

                                    CreditRequestDTOList.Add(CreditRequestDTO);// (Transform.CreditRequestToDTO(item));
                                }
                            }
                            return CreditRequestDTOList;
                        }

                    }
                    else
                    {
                        foreach (var item in CreditRequest)
                        {
                            CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
                            CreditRequestDTO = Transform.CreditRequestToDTO(item);

                            ClientDTO ClientDTO = new ClientDTO();
                            ClientDTO = ClientService.GetById(item.ClientId);
                            CreditRequestDTO.ClientName = ClientDTO.Company;

                            UserDTO UserDTO = new UserDTO();
                            UserDTO = UserService.GetById(item.RequestedBy);
                            CreditRequestDTO.UserName = UserDTO.Name;

                            CreditRequestDTOList.Add(CreditRequestDTO);// (Transform.CreditRequestToDTO(item));
                        }
                    }
                }

                return CreditRequestDTOList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Send bill to client by credit request id
        public static bool SendBillToClient(int CreditRequestId)
        {
            bool IsSend = false;

            CreditRequestDTO CreditRequestDTO = new CreditRequestDTO();
            CreditRequestDTO = GetById(CreditRequestId);
            if (CreditRequestDTO.ProvidedDate != null)
            {
                ClientDTO ClientDTO = new ClientDTO();
                ClientDTO = ClientService.GetById(CreditRequestDTO.ClientId);

                UserDTO UserDTO = new UserDTO();
                UserDTO = UserService.GetById(CreditRequestDTO.RequestedBy);

                DateTime date = (DateTime)CreditRequestDTO.ProvidedDate;
                string DateFormat = date.ToString("dd-MMM-yyyy HH:mm");

                IsSend = CommonService.SendEmail("msgBlaster SMS credit provided bill", "<html><body><p>Hello " + ClientDTO.Company + ",</p> <br/><p>Your mesgblaster Bill details are as follows.<p/> <table><tr><td> Bill Date</td> <td> " + DateFormat + " </td> </tr> <tr><td> Requested Credits</td> <td> " + CreditRequestDTO.RequestedCredit + " </td> </tr> <tr><td> Provided Credits</td> <td> " + CreditRequestDTO.ProvidedCredit + " </td> </tr> <tr><td> Rate Per SMS</td> <td> " + String.Format("{0:0.00}", CreditRequestDTO.RatePerSMS) + " </td> </tr> <tr><td> Amount </td> <td> " + String.Format("{0:0.00}", CreditRequestDTO.Amount) + " </td> </tr><tr><td> Tax</td> <td> " + String.Format("{0:0.00}", CreditRequestDTO.Tax) + "% </td> </tr> <tr><td> <strong>Grand Total</strong></td> <td> <strong>" + String.Format("{0:0.00}", CreditRequestDTO.GrandTotal) + "</strong> </td> </tr>  </table>  </body></html>", UserDTO.Email, "", false);
                IsSend = true;
            }
            else { IsSend = false; }
            return IsSend;
        }
        //Generate bill by credit request details
        public static CreditRequestDTO GenerateBill(CreditRequestDTO CreditRequestDTO)
        {
            CreditRequestDTO CreditRequestDTONew = new CreditRequestDTO();
            try
            {
                //if (CreditRequestDTO.Tax == null)
                //{
                //    CreditRequestDTO.Tax = 0;
                //}

                if (CreditRequestDTO.IsProvided == true)
                {
                    CreditRequestDTONew = CreditRequestDTO;
                    CreditRequestDTONew.IsBillGenerated = true;
                    CreditRequestDTONew.IsProvided = true;
                    //CreditRequestDTONew.ProvidedDate = System.DateTime.Now;
                    CreditRequestDTONew.RatePerSMS = CreditRequestDTO.RatePerSMS;
                    CreditRequestDTONew.Tax = CreditRequestDTO.Tax;
                    CreditRequestDTONew.ProvidedCredit = CreditRequestDTO.ProvidedCredit;
                    CreditRequestDTONew.Amount = CreditRequestDTO.ProvidedCredit * CreditRequestDTO.RatePerSMS;
                    double tax = (CreditRequestDTONew.Amount * Convert.ToDouble(CreditRequestDTONew.Tax)) / 100;
                    CreditRequestDTONew.TaxAmount = tax;
                    CreditRequestDTONew.GrandTotal = CreditRequestDTONew.Amount + tax;

                    //if (CreditRequestDTO.GrandTotal != CreditRequestDTONew.GrandTotal)
                    //{
                    //    CreditRequestDTONew.GrandTotal = CreditRequestDTO.GrandTotal;
                    //}

                    GlobalSettings.LoggedInClientId = CreditRequestDTO.ClientId;
                    GlobalSettings.LoggedInUserId = CreditRequestDTO.RequestedBy;
                    int PartnerId = ClientService.GetById(CreditRequestDTO.ClientId).PartnerId;
                    GlobalSettings.LoggedInPartnerId = PartnerId;

                    UnitOfWork uow = new UnitOfWork();
                    CreditRequest CreditRequest = Transform.CreditRequestToDomain(CreditRequestDTONew);
                    uow.CreditRequestRepo.Update(CreditRequest);
                    uow.SaveChanges();

                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(CreditRequest.ClientId);

                    UserDTO UserDTO = new UserDTO();
                    UserDTO = UserService.GetById(CreditRequest.RequestedBy);

                    DateTime date = (DateTime)CreditRequestDTONew.ProvidedDate;
                    string DateFormat = date.ToString("dd-MMM-yyyy HH:mm");

                    CommonService.SendEmail("msgBlaster Bill", "<html><body><p>Hello " + ClientDTO.Company + ",</p> <br/><p>Your mesgblaster bill details are as follows.<p/> <table><tr><td> Date</td> <td> " + DateFormat + " </td> </tr>  <tr><td> Provided Credits</td> <td> " + CreditRequest.ProvidedCredit + " </td> </tr> <td> Total Balance</td> <td> " + ClientDTO.SMSCredit + " </td> </tr> <tr><td> Rate Per SMS</td> <td> " + String.Format("{0:0.00}", CreditRequest.RatePerSMS) + " </td> </tr> <tr><td> Amount </td> <td> " + String.Format("{0:0.00}", CreditRequest.Amount) + " </td> </tr> <tr><td> Tax</td> <td> " + String.Format("{0:0.00}", CreditRequest.Tax) + "% </td> </tr> <tr><td> <strong>Grand Total</strong></td> <td> <strong>" + String.Format("{0:0.00}", CreditRequest.GrandTotal) + "</strong> </td> </tr>  </table>  </body></html>", UserDTO.Email, "", false);  //<tr><td> Provided Credits</td> <td> " + CreditRequest.ProvidedCredit + " </td> </tr> <tr><td> Rate Per SMS</td> <td> " + CreditRequest.RatePerSMS + " </td> </tr> <tr><td> Amount </td> <td> " + CreditRequest.Amount + " </td> </tr> <tr><td> Tax</td> <td> " + CreditRequest.Tax + " </td> </tr> <tr><td> <strong>Grand Total</strong></td> <td> <strong>" + CreditRequest.GrandTotal + "</strong> </td> </tr>

                    CreditRequestDTONew = Transform.CreditRequestToDTO(CreditRequest);
                    return CreditRequestDTONew;
                }
                else
                {
                    return CreditRequestDTO;
                }

            }
            catch (Exception)
            {
                //return CreditRequestDTO;
                throw;
            }
        }
        //Edit credit request from online payment status
        public static bool EditCreditRequestForOnlinePayment(CreditRequestDTO CreditRequestDTO, bool IsSuccess)
        {
            try
            {
                GlobalSettings.LoggedInClientId = CreditRequestDTO.ClientId;
                GlobalSettings.LoggedInUserId = CreditRequestDTO.RequestedBy;
                int PartnerId = ClientService.GetById(CreditRequestDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                UnitOfWork uow = new UnitOfWork();
                if (IsSuccess == true)
                {
                    //CreditRequest.IsPaymentSuccessful = true;
                    //CreditRequest.PaymentDate = System.DateTime.Now;
                    CreditRequestDTO.ProvidedDate = System.DateTime.Now;
                    CreditRequestDTO.IsProvided = true;
                    CreditRequestDTO.IsBillGenerated = true;
                    CreditRequestDTO.PaymentMode = PaymentMode.Card.ToString();
                    CreditRequestDTO.PaymentDate = System.DateTime.Now;
                    CreditRequestDTO.IsPaid = true;

                    CreditRequest CreditRequest = Transform.CreditRequestToDomain(CreditRequestDTO);
                    uow.CreditRequestRepo.Update(CreditRequest);
                    uow.SaveChanges();

                    ClientDTO ClientDTO = new ClientDTO();
                    ClientDTO = ClientService.GetById(CreditRequestDTO.ClientId);
                    ClientDTO.TotalProvidedCredit = ClientDTO.TotalProvidedCredit + CreditRequestDTO.RequestedCredit;
                    ClientDTO.SMSCredit = ClientDTO.SMSCredit + CreditRequestDTO.RequestedCredit;
                    ClientService.Edit(ClientDTO);

                    return true;

                }
                else
                {
                    CreditRequestDTO.IsPaid = false;
                    CreditRequest CreditRequest = Transform.CreditRequestToDomain(CreditRequestDTO);
                    uow.CreditRequestRepo.Update(CreditRequest);
                    uow.SaveChanges();
                    return false;
                }

            }
            catch
            {
                throw;
            }
        }
        public static void PayOnlineEdit(CreditRequestDTO creditRequestDTO)
        {
            try
            {
                //bool IsPaymentSuccessful = false;
                //IsPaymentSuccessful = GetById(creditRequestDTO.Id).IsPaymentSuccessful;
                //UnitOfWork uow = new UnitOfWork();

                //if (creditRequestDTO.IsPaymentSuccessful != true)
                //{
                //    bool IsOnlinepaymentSuccess = Onlinepayment(creditRequestDTO.PayableAmount);
                //    if (IsOnlinepaymentSuccess == true)
                //    {
                //        creditRequestDTO.IsPaymentSuccessful = true;
                //        creditRequestDTO.PaymentDate = System.DateTime.Now;
                //        creditRequestDTO.IsProvided = true;
                //        creditRequestDTO.IsBillGenerated = true;

                //        ClientDTO ClientDTO = new ClientDTO();
                //        ClientDTO = ClientService.GetById(creditRequestDTO.ClientId);
                //        ClientDTO.SMSCredit = ClientDTO.SMSCredit + creditRequestDTO.RequestedCredit;
                //        ClientService.Edit(ClientDTO);
                //    }
                //    else
                //    {
                //        creditRequestDTO.IsPaymentSuccessful = false;
                //    }
                //}
                //CreditRequest CreditRequest = Transform.CreditRequestToDomain(creditRequestDTO);
                //uow.CreditRequestRepo.Update(CreditRequest);
                //uow.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
        //Get credit request list by  partner id with provided flag
        public static List<CreditRequestDTO> GetRequestedCreditRequestsbyPartnerIdWithProvidedflag(int PartnerId, string search, bool IsProvided, PagingInfo pagingInfo)
        {
            List<CreditRequestDTO> CreditRequestDTO = new List<CreditRequestDTO>();

            //List<PartnerDTO> PartnerDTO = new List<PartnerDTO>();

            try
            {
                UnitOfWork uow = new UnitOfWork();
                int skip = (pagingInfo.Page - 1) * pagingInfo.ItemsPerPage;
                int take = pagingInfo.ItemsPerPage;
                //IEnumerable<Client> Client = uow.ClientRepo.GetAll().Where(e => e.PartnerId == PartnerId).ToList();
                List<CreditRequestDTO> CreditRequestDTOList = new List<CreditRequestDTO>();
                //GetAll(skip, take)
                IQueryable<CreditRequest> CreditRequest = uow.CreditRequestRepo.GetAll().Where(e => e.IsProvided == IsProvided).AsQueryable();//.OrderByDescending(e => e.Date).ToList();
                CreditRequest = PagingService.Sorting<CreditRequest>(CreditRequest, pagingInfo.SortBy, pagingInfo.Reverse);
                CreditRequest = CreditRequest.Skip(skip).Take(take);

                if (CreditRequest != null)
                {
                    foreach (var item in CreditRequest)
                    {
                        //IEnumerable<CreditRequest> CreditRequest = uow.CreditRequestRepo.GetAll(skip,take).Where(e => e.ClientId == item.Id && e.IsProvided == IsProvided).OrderByDescending(e => e.Date).ToList();

                        CreditRequestDTO CreditRequestDTONew = new CreditRequestDTO();
                        CreditRequestDTONew = GetById(item.Id);

                        CreditRequestDTOList.Add(CreditRequestDTONew);// (Transform.CreditRequestToDTO(CreditRequestDTONew));

                    }
                }

                if (CreditRequestDTOList != null)
                {
                    if (search != "" && search != null)
                    {

                        bool IsDate = CommonService.IsDate(search);
                        if (IsDate != true)
                        {
                            //int ClientId =ClientService.GetByClientName(search, PartnerId);
                            String ClientIdString = ClientService.GetClientIdarrayByCompany(search, PartnerId);
                            // string search
                            // var CreditRequestsearch = CreditRequest.Where(e => e.Amount.ToString().Contains(search.ToLower()) || e.ClientId.ToString().Contains(search) || e.GrandTotal.ToString().Contains(search) || e.IsProvided.ToString().Contains(search) || e.OldBalance.ToString().Contains(search) || e.PartnerId.ToString().Contains(search) || e.RatePerSMS.ToString().Contains(search) || e.RequestedCredit.ToString().Contains(search) || e.Tax.ToString().Contains(search));
                            IQueryable<CreditRequest> CreditRequestsearch = uow.CreditRequestRepo.GetAll().Where(e => (e.OldBalance.ToString() == (search) || e.RatePerSMS.ToString() == (search) || e.RequestedCredit.ToString() == (search) || e.ProvidedCredit.ToString() == (search) || (e.Date.ToString() != null ? (Convert.ToDateTime(e.Date).ToString("dd-MMM-yyyy").ToLower().Contains(search.ToLower())) : false) || (e.ProvidedDate.ToString() != null ? (Convert.ToDateTime(e.ProvidedDate).ToString("dd-MMM-yyyy").ToLower().Contains(search.ToLower())) : false) || (ClientIdString != null ? (e.ClientId.ToString().Split(',').Any(ClientId => ClientIdString.Contains(ClientId))) : false)) && e.PartnerId == PartnerId && e.IsProvided == IsProvided).AsQueryable();//.OrderByDescending(e => e.Date).Skip(skip).Take(take); //|| e.UserName.ToString().ToLower().Contains(search.ToLower()) || e.ClientName.ToString().ToLower().Contains(search.ToLower())
                            CreditRequestsearch = PagingService.Sorting<CreditRequest>(CreditRequestsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            CreditRequestsearch = CreditRequestsearch.Skip(skip).Take(take);

                            if (CreditRequestsearch != null)
                            {
                                foreach (var item in CreditRequestsearch)
                                {
                                    CreditRequestDTO CreditRequestDTONew = new CreditRequestDTO();
                                    CreditRequestDTONew = GetById(item.Id);
                                    CreditRequestDTO.Add(CreditRequestDTONew);
                                }
                            }
                            return CreditRequestDTO;
                        }
                        else
                        {
                            //date wise search
                            DateTime date = Convert.ToDateTime(search);
                            IQueryable<CreditRequest> CreditRequestsearch = uow.CreditRequestRepo.GetAll().Where(e => (e.Date >= date && e.Date < date.AddDays(1) || e.ProvidedDate >= date && e.ProvidedDate < date.AddDays(1)) && e.IsProvided == IsProvided).AsQueryable();//.OrderByDescending(e => e.Date).Skip(skip).Take(take);
                            CreditRequestsearch = PagingService.Sorting<CreditRequest>(CreditRequestsearch, pagingInfo.SortBy, pagingInfo.Reverse);
                            CreditRequestsearch = CreditRequestsearch.Skip(skip).Take(take);

                            if (CreditRequestsearch != null)
                            {
                                foreach (var item in CreditRequestsearch)
                                {
                                    if (item.IsProvided == IsProvided)
                                    {
                                        CreditRequestDTO CreditRequestDTONew = new CreditRequestDTO();
                                        CreditRequestDTONew = GetById(item.Id);
                                        CreditRequestDTO.Add(CreditRequestDTONew);
                                    }
                                }
                            }
                            return CreditRequestDTO;

                        }

                    }
                    else
                    {
                        return CreditRequestDTOList;
                    }
                }

                return CreditRequestDTO;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //Edit credit request
        public static void Edit(CreditRequestDTO creditRequestDTO)
        {
            try
            {
                GlobalSettings.LoggedInClientId = creditRequestDTO.ClientId;
                GlobalSettings.LoggedInUserId = creditRequestDTO.RequestedBy;
                int PartnerId = ClientService.GetById(creditRequestDTO.ClientId).PartnerId;
                GlobalSettings.LoggedInPartnerId = PartnerId;

                UnitOfWork uow = new UnitOfWork();
                CreditRequest CreditRequest = Transform.CreditRequestToDomain(creditRequestDTO);
                uow.CreditRequestRepo.Update(CreditRequest);
                uow.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 13
0
 public static CreditRequest CreditRequestToDomain(CreditRequestDTO CreditRequestDTO)
 {
     if (CreditRequestDTO == null) return null;
     Mapper.CreateMap<CreditRequestDTO, CreditRequest>();
     CreditRequest CreditRequest = Mapper.Map<CreditRequest>(CreditRequestDTO);
     CreditRequest._clientName = ClientService.GetById(CreditRequest.ClientId).Company;
     CreditRequest._userName = UserService.GetById(CreditRequest.RequestedBy).Name;
     return CreditRequest;
 }