Ejemplo n.º 1
0
        public async Task <IActionResult> InvoiceDetail(InvoiceDetailModel model, IFormFile upload, string generate, string draft, string delete)
        {
            try
            {
                if (model.Data.Status == "Unpaid")
                {
                    if (string.IsNullOrEmpty(model.Data.InvoiceNumber) || string.IsNullOrEmpty(model.Data.ClientEmail) || model.Data.Amount < .1)
                    {
                        // TODO: Need to change invoice process model
                        return(RedirectToAction("Profile"));
                    }

                    await _invoicesServiceClient.GenerateInvoiceFromDraftAsync(new UpdateInvoiceModel
                    {
                        MerchantId    = MerchantId,
                        InvoiceId     = model.Data.InvoiceId,
                        InvoiceNumber = model.Data.InvoiceNumber,
                        ClientName    = model.Data.ClientName,
                        ClientEmail   = model.Data.ClientEmail,
                        Amount        = model.Data.Amount,
                        Currency      = model.Data.Currency,
                        DueDate       = DateTime.Parse(model.Data.DueDate, CultureInfo.InvariantCulture)
                                        .RepoDateStr()
                    });
                }
                else if (model.Data.Status == "Draft")
                {
                    await _invoicesServiceClient.UpdateDraftInvoiceAsync(new UpdateDraftInvoiceModel
                    {
                        MerchantId    = MerchantId,
                        InvoiceId     = model.Data.InvoiceId,
                        InvoiceNumber = model.Data.InvoiceNumber,
                        ClientName    = model.Data.ClientName,
                        ClientEmail   = model.Data.ClientEmail,
                        Amount        = model.Data.Amount,
                        Currency      = model.Data.Currency,
                        DueDate       = DateTime.Parse(model.Data.DueDate, CultureInfo.InvariantCulture)
                                        .RepoDateStr()
                    });
                }
                else if (model.Data.Status == "Remove")
                {
                    await _invoicesServiceClient.DeleteInvoiceAsync(MerchantId, model.Data.InvoiceId);

                    return(RedirectToAction("Profile"));
                }
                else
                {
                    throw new InvalidOperationException("Unknown action");
                }

                if (upload != null)
                {
                    byte[] buffer = new byte[16 * 1024];
                    using (var ms = new MemoryStream())
                    {
                        await upload.CopyToAsync(ms);

                        buffer = ms.ToArray();
                    }
                    await _invoicesServiceClient.UploadFileAsync(model.Data.InvoiceId, buffer, upload.FileName, upload.ContentType);
                }
            }
            catch (Exception exception)
            {
                await _log.WriteErrorAsync(nameof(HomeController), nameof(InvoiceDetail), model.ToJson(), exception);

                return(BadRequest("Error processing invoce!"));
            }

            return(RedirectToAction("InvoiceDetail", new
            {
                invoiceId = model.Data.InvoiceId
            }));
        }