示例#1
0
        public async Task <InvoiceResponse> AddInvoice(InvoiceReceipt receipt)
        {
            var result = await CheckExistingDocument(receipt);

            if (result == null)
            {
                result = await WithClient(async client =>
                {
                    using (var content = AsJson(receipt))
                    {
                        using (var response = await client.PostAsync("documents", content))
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                var receivedCustomer = await FromJson <InvoiceResponse>(response.Content);

                                return(receivedCustomer);
                            }
                            else
                            {
                                throw new InvalidOperationException("Unable to create invoice");
                            }
                        }
                    }
                });
            }

            return(result);
        }
示例#2
0
        private async Task <InvoiceResponse> CheckExistingDocument(InvoiceReceipt invoice)
        {
            var result = await WithClient(async client =>
            {
                using (var content = AsJson(new
                {
                    ClientId = invoice.Client.Id,
                    FromDate = invoice.Payment[0].Date,
                    ToDate = invoice.Payment[0].Date,
                    Description = invoice.Description
                }))
                {
                    using (var response = await client.PostAsync("documents/search", content))
                    {
                        var invoiceResults = default(InvoiceResponse);
                        if (response.IsSuccessStatusCode)
                        {
                            var searchResult = await FromJson <InvoiceSearchResult>(response.Content);
                            if (searchResult != null && searchResult.Total > 0)
                            {
                                invoiceResults = searchResult.Items.First();
                            }
                        }
                        return(invoiceResults);
                    }
                }
            });

            return(result);
        }