public async Task <PaymentDTO> SaveAsync(PaymentDTO paymentDTO) { using (var model = new PaymentServicesEntities()) { int number = 0; var paymentType = model.PaymentType.FirstOrDefault(x => x.Code == paymentDTO.CodePaymentType); var lastPayment = model.Payment.OrderByDescending(x => x.IdPayment).FirstOrDefault(); if (lastPayment != null) { int.TryParse(lastPayment.PaymentNumber, out number); } var payment = new Payment() { OrderNumber = paymentDTO.OrderNumber, ClientCode = paymentDTO.ClientCode, TotalPayment = paymentDTO.TotalPayment, PaymentNumber = (++number).ToString(), PaymentDate = DateTime.Now }; payment.IdStatus = (int)ePaymentStatus.Created; payment.PaymentType = paymentType ?? throw new Exception("Payment Type Code is not Valid"); model.Payment.AddObject(payment); var result = model.SaveChanges(); if (result > 0) { paymentDTO.PaymentNum = payment.PaymentNumber; paymentDTO.StatusCode = payment.Status.Code; paymentDTO.StatusName = payment.Status.Name; paymentDTO.NamePaymentType = payment.PaymentType.Name; } return(paymentDTO); } }
public async Task <PaymentDTO> CancelPaymentAsync(int idPayment) { using (var model = new PaymentServicesEntities()) { var payment = model.Payment.FirstOrDefault(x => x.IdPayment == idPayment); if (payment == null) { throw new Exception("Payment not found"); } payment.IdStatus = (int)ePaymentStatus.Cancel; model.Payment.ApplyCurrentValues(payment); model.SaveChanges(); return(new PaymentDTO() { PaymentNum = payment.PaymentNumber, PaymentDate = payment.PaymentDate, StatusName = payment.Status.Name }); } }
public PaymentTypeDTO ChangeActivePaymentType(int idPaymentType, bool isActive) { using (var model = new PaymentServicesEntities()) { var paymentType = model.PaymentType.FirstOrDefault(x => x.IdPaymentType == idPaymentType); if (paymentType == null) { throw new Exception("Payment type not found"); } paymentType.IsActive = isActive; model.PaymentType.ApplyCurrentValues(paymentType); model.SaveChanges(); return(new PaymentTypeDTO { IsActive = paymentType.IsActive, Code = paymentType.Code, IdPaymentType = paymentType.IdPaymentType, Name = paymentType.Name }); } }