Esempio n. 1
0
        public async Task <ActionResult <IInvoiceView> > Update([FromRoute] int id, [FromBody] InvoicePay invoicePay)
        {
            try
            {
                var grain  = _orleansService.Client.GetGrain <InvoicePayCommand>(0);
                var result = await grain.InvoicePay(invoicePay);

                if (result.__CQRSSuccessful)
                {
                    return(new ActionResult <IInvoiceView>(result));
                }
                else
                {
                    return(new ContentResult()
                    {
                        StatusCode = result.__CQRSStatusCode, Content = result.__CQRSErrorMessage, ContentType = "text/plain"
                    });
                }
            }
            catch (Exception e)
            {
                LogFactory.GetLogger().Log(LogLevel.Error, e);
                return(new ContentResult()
                {
                    StatusCode = 500, Content = "Fatal API Error", ContentType = "text/plain"
                });
            }
        }
        public async Task <IActionResult> SavePaymentDetails([FromBody] InvoicePay details)
        {
            Result <string> result = new Result <string>();

            try
            {
                using (var dbContextTransaction = _ablemusicContext.Database.BeginTransaction())
                {
                    var invoiceItem =
                        await _ablemusicContext.Invoice.FirstOrDefaultAsync(s => s.InvoiceId == details.InvoiceId);

                    if (invoiceItem == null)
                    {
                        throw new Exception("Invoice does not found.");
                    }

                    //the owing fee for invoice cannot be negative.
                    if (invoiceItem.OwingFee - details.Amount < 0)
                    {
                        throw new Exception("You only need to pay " + invoiceItem.OwingFee + " dollar. No more than it");
                    }

                    invoiceItem.PaidFee  = invoiceItem.PaidFee + details.Amount;
                    invoiceItem.OwingFee = invoiceItem.OwingFee - details.Amount;
                    if (invoiceItem.OwingFee > 0)
                    {
                        invoiceItem.IsPaid = 0;
                    }

                    if (invoiceItem.OwingFee == 0)
                    {
                        invoiceItem.IsPaid = 1;
                    }
                    _ablemusicContext.Update(invoiceItem);
                    await _ablemusicContext.SaveChangesAsync();

                    //save the Invoice payment to Payment table
                    var paymentItem = new Payment();
                    _mapper.Map(details, paymentItem);
                    paymentItem.CreatedAt   = toNZTimezone(DateTime.UtcNow);
                    paymentItem.PaymentType = 1;
                    paymentItem.IsConfirmed = 0;
                    _ablemusicContext.Add(paymentItem);
                    await _ablemusicContext.SaveChangesAsync();

                    var fundItem =
                        await _ablemusicContext.Fund.FirstOrDefaultAsync(s => s.LearnerId == details.LearnerId);

                    if (fundItem == null)
                    {
                        var fundNewItem = new  Fund();
                        fundNewItem.LearnerId = details.LearnerId;
                        fundNewItem.Balance   = details.Amount;
                        fundNewItem.Balance   = details.Amount;
                        fundNewItem.UpdatedAt = toNZTimezone(DateTime.UtcNow);
                        _ablemusicContext.Add(fundNewItem);
                    }
                    else
                    {
                        fundItem.Balance   = fundItem.Balance + details.Amount;
                        fundItem.UpdatedAt = toNZTimezone(DateTime.UtcNow);
                        _ablemusicContext.Update(fundItem);
                    }
                    await _ablemusicContext.SaveChangesAsync();

                    if (invoiceItem.IsPaid == 1)
                    {
                        if (invoiceItem.CourseInstanceId != null) //if this is a one on one session
                        //
                        {
                            var invoiceLessonRemain =
                                _ablemusicContext.LessonRemain.FirstOrDefault(s =>
                                                                              s.CourseInstanceId == invoiceItem.CourseInstanceId);
                            if (invoiceLessonRemain != null)
                            {
                                invoiceLessonRemain.Quantity =
                                    invoiceLessonRemain.Quantity + invoiceItem.LessonQuantity;
                                _ablemusicContext.Update(invoiceLessonRemain);
                                await _ablemusicContext.SaveChangesAsync();
                            }
                            else
                            {
                                var lessonRemain = new LessonRemain
                                {
                                    Quantity         = invoiceItem.LessonQuantity,
                                    TermId           = invoiceItem.TermId,
                                    ExpiryDate       = invoiceItem.EndDate.Value.AddMonths(3),
                                    CourseInstanceId = invoiceItem.CourseInstanceId,
                                    LearnerId        = invoiceItem.LearnerId
                                };
                                _ablemusicContext.Add(lessonRemain);
                                await _ablemusicContext.SaveChangesAsync();
                            }
                        }
                        //if  (invoiceItem.CourseInstanceId != null)
                        //await SaveLesson(details.InvoiceId,0,1);
                    }
                    dbContextTransaction.Commit();
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.ErrorCode = ex.Message;
                return(BadRequest(result));
            }
            result.Data = "Success!";
            return(Ok(result));
        }