Esempio n. 1
0
        private async Task <BankTransactionExplanation> ProcessBankTransaction(BankTransactionExplanation transaction)
        {
            try
            {
                await semaphore.WaitAsync().ConfigureAwait(false);

                var request = WebRequest.Create(new Uri(transaction.Attachment.ContentSrc));
                var id      = transaction.LocalId();

                var transactionId = transaction.BankTransaction.LocalId();
                var bankId        = transaction.BankAccount.LocalId();
                var dateTime      = transaction.DatedOn.FromModelDate();

                var dateStr = dateTime.ToString("yyyy MMMM dd");
                logger.LogDebug("Downloading: Id {0} Transaction: {1} (Bank: {2}) Date: {3}",
                                id,
                                transactionId,
                                bankId,
                                dateTime);

                using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false))
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        var fileName = $@"{config.Out}\{bankId}\{dateStr}_{id}_{transactionId}";
                        await transaction.SerializeJsonZip($"{fileName}_data.zip").ConfigureAwait(false);

                        using (var file = new FileStream($@"{fileName}_file.pdf", FileMode.Create, FileAccess.Write))
                        {
                            stream.CopyTo(file);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogError(e, "Download failed");
            }
            finally
            {
                semaphore.Release();
            }

            return(transaction);
        }
Esempio n. 2
0
        public async Task CanRaiseInvoiceAndBankExplanation()
        {
            // Create a random contact:
            var source  = Helper.NewContact();
            var contact = await this.Client.CreateContactAsync(source);

            Assert.NotNull(contact.Url);

            // Create an invoice for this contact
            var items = new List <InvoiceItem>
            {
                new InvoiceItem()
                {
                    ItemType     = InvoiceItemType.NoUnit,
                    Price        = 100,
                    Description  = "Some things",
                    Quantity     = 100,
                    SalesTaxRate = 20
                }
            };

            var invoice = new Invoice
            {
                Contact            = contact.Url,
                DatedOn            = DateTime.Now.AddDays(-1),
                DueOn              = DateTime.Now.AddDays(1),
                PaymentTermsInDays = 25,
                InvoiceItems       = items,
                Currency           = "GBP",
                Comments           = "Thanks, all done."
            };

            try
            {
                invoice = await this.Client.CreateInvoice(invoice);
            }
            catch (FreeAgentException e)
            {
                Console.WriteLine(e.InnerException.ToString());
            }
            Assert.NotNull(invoice.Url);

            // Now we need to create an email for the invoice
            var email = new InvoiceEmail
            {
                Email =
                    new InvoiceEmailDetail
                {
                    Body    = "Hello there, here you are.",
                    Subject = "Your invoice",
                }
            };

            await this.Client.CreateInvoiceEmail(invoice.GetId(), email);

            var bankAccount = await Client.GetBankAccountsAsync();

            // Create a bank account explanation for the new invoice now.
            var explanation = new BankTransactionExplanation
            {
                BankAccount          = bankAccount.First().Url,
                DatedOn              = DateTime.Now,
                PaidInvoice          = invoice.Url,
                GrossValue           = invoice.TotalValue,
                ForeignCurrencyValue = 0
            };

            explanation = await this.Client.CreateBankTransactionExplanationAsync(explanation);

            Assert.NotNull(explanation.Url);

            // And for my final trick, I shall now get the PDF of that invoice.
            var pdf = await this.Client.GetInvoicePdfAsync(invoice.GetId());

            Assert.NotNull(pdf?.Content);
        }