-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInvoices.cs
More file actions
249 lines (223 loc) · 10.7 KB
/
Invoices.cs
File metadata and controls
249 lines (223 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using Birko.SuperFaktura.Request.Client;
using Birko.SuperFaktura.Request.Invoice;
using Birko.SuperFaktura.Response;
using Birko.SuperFaktura.Response.Invoice;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Birko.SuperFaktura
{
public class Invoices
{
private readonly AbstractSuperFaktura superFaktura;
public Invoices(AbstractSuperFaktura superFaktura)
{
this.superFaktura = superFaktura;
}
public async Task<PagedResponse> List(Request.Invoice.Filter filter, bool listInfo = true)
{
var result = await superFaktura.Get(string.Format("invoices/index.json{0}", filter.ToParameters(listInfo))).ConfigureAwait(false);
if (listInfo)
{
return superFaktura.DeserializeResult<PagedResponse>(result);
}
else
{
return new PagedResponse { Items = superFaktura.DeserializeResult<IEnumerable<Detail>>(result) };
}
}
public async Task<Detail> Add(Request.Invoice.Invoice invoice, Client client, Request.Invoice.Item[] items, int[] tags = null, InvoiceSettings setting = null, Extra extra = null, Request.Invoice.MyData myData = null)
{
var data = new InvoiceData
{
Invoice = invoice,
InvoiceItem = items,
Tag = tags,
Client = client,
InvoiceSetting = setting,
InvoiceExtra = extra,
MyData = myData
};
var result = await superFaktura.Post("invoices/create", data).ConfigureAwait(false);
var response = superFaktura.DeserializeResult<StatusResponse<Detail>>(result);
return response.Data;
}
public async Task<Detail> View(int ID)
{
var result = await superFaktura.Get(string.Format("invoices/view/{0}.json", ID)).ConfigureAwait(false);
return superFaktura.DeserializeResult<Detail>(result);
}
public async Task<Dictionary<int, Detail>> ListDetails(IEnumerable<int> IDS)
{
var result = await superFaktura.Get(string.Format("invoices/getInvoiceDetails/{0}", string.Join(",", IDS))).ConfigureAwait(false);
return superFaktura.DeserializeResult<Dictionary<int, Detail>>(result);
}
public async Task<DetailData> Edit(Request.Invoice.Invoice invoice, Client client, Request.Invoice.Item[] items, int[] tags = null, InvoiceSettings setting = null, Extra extra = null, Request.Invoice.MyData myData = null)
{
var data = new InvoiceData
{
Invoice = invoice,
InvoiceItem = items,
Tag = tags,
Client = client,
InvoiceSetting = setting,
InvoiceExtra = extra,
MyData = myData
};
var result = await superFaktura.Post("invoices/edit", data).ConfigureAwait(false);
var response = superFaktura.DeserializeResult<Response<DetailData>>(result);
return response.Data;
}
public async Task<StringMessageResponse> SetInvoiceLanguage(int ID, string language = Request.ValueLists.LanguageType.Slovak)
{
if (!Request.ValueLists.LanguageType.Languages.Contains(language))
{
language = Request.ValueLists.LanguageType.Slovak;
}
// error response fix
var result = await superFaktura.Get(string.Format("invoices/setinvoicelanguage/{0}/lang:{1}", ID, language)).ConfigureAwait(false);
return superFaktura.DeserializeResult<StringMessageResponse>(result);
}
public async Task<byte[]> Download(int invoiceId, string token, string language = Request.ValueLists.LanguageType.Slovak, bool? signature = null, bool? bySquare = null, bool? paypal = null)
{
if (!Request.ValueLists.LanguageType.Languages.Contains(language))
{
language = Request.ValueLists.LanguageType.Slovak;
}
var url = $"{language}/invoices/pdf/{invoiceId}/token:{token}";
if (signature != null)
{
url += $"/no-signature:{(signature.Value ? 0 : 1)}";
}
if (bySquare != null)
{
url += $"/bysquare:{(bySquare.Value ? 1 : 0)}";
}
if (paypal != null)
{
url += $"/paypal:{(paypal.Value ? 1 : 0)}";
}
var result = await superFaktura.GetByte(url).ConfigureAwait(false);
//Code below tests if response is a SuperFaktura error response or PDF File
try
{
string testResult = Encoding.UTF8.GetString(result);
superFaktura.DeserializeResult<ErrorMessageResponse>(testResult);
}
catch (Exceptions.ParseException) when (result != null && result.Length != 0)
{
//test deserialization failed. it is a pdf file
}
catch (Exception)
{
throw;
}
return result;
}
public async Task<StringMessageResponse> Delete(int id)
{
var result = await superFaktura.Get(string.Format("invoices/delete/{0}", id)).ConfigureAwait(false);
return superFaktura.DeserializeResult<StringMessageResponse>(result);
}
public async Task<DetailBasic> WillNotBePaid(int id)
{
var result = await superFaktura.Get(string.Format("invoices/will_not_be_paid/{0}", id)).ConfigureAwait(false);
var detail = superFaktura.DeserializeResult<Response<DetailBasic>>(result);
return detail.Data;
}
public async Task<ResponseEmailInvoice> SendEmail(Request.Invoice.Email email)
{
var result = await superFaktura.Post("invoices/send", new EmailData { Email = email }).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<Response<ResponseEmail>>(result);
return data.Data.Invoice;
}
public async Task<ErrorMessageResponse> MarkAsSentViaMail(MarkEmail email)
{
var result = await superFaktura.Post("invoices/mark_as_sent", new MarkEmailData { InvoiceEmail = email }).ConfigureAwait(false);
return superFaktura.DeserializeResult<ErrorMessageResponse>(result);
}
public async Task<DetailBasic> SendPost(Post post)
{
var result = await superFaktura.Post("invoices/post", new PostData { Post = post }).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<Response<DetailBasic>>(result);
return data.Data;
}
public async Task<Mark> MarkAsSent(int invoiceID)
{
var result = await superFaktura.Get(string.Format("invoices/mark_sent/{0}", invoiceID)).ConfigureAwait(false);
return superFaktura.DeserializeResult<Mark>(result);
}
public async Task<DetailInvoice> DeleteItem(int invoiceID, int itemID)
{
return await DeleteItem(invoiceID, new int[] { itemID }).ConfigureAwait(false);
}
public async Task<DetailInvoice> DeleteItem(int invoiceID, int[] itemID)
{
if (!(itemID?.Any() ?? false))
{
return null;
}
var result = await superFaktura.Get(string.Format("invoice_items/delete/{1}/invoice_id:{0}", invoiceID, string.Join(",", itemID))).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<Response<DetailInvoice>>(result);
return data.Data;
}
public async Task<ResponsePayment> AddPayment(Request.Invoice.Payment payment)
{
var result = await superFaktura.Post("invoice_payments/add/ajax:1/api:1", new InvoicePaymentData { InvoicePayment = payment }).ConfigureAwait(false);
return superFaktura.DeserializeResult<ResponsePayment>(result);
}
public async Task<DeletePayment> DeletePayment(int invoicePaymentID)
{
var result = await superFaktura.Get(string.Format("invoice_payments/delete/{0}", invoicePaymentID)).ConfigureAwait(false);
return superFaktura.DeserializeResult<DeletePayment>(result);
}
public async Task<RelatedItemResponse> AddRelatedItem(Request.RelatedItem related)
{
var result = await superFaktura.Post("invoices/addRelatedItem", related).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<Response<RelatedItemResponse>>(result);
return data.Data;
}
public async Task<StringMessageResponse> DeleteRelatedItem(int relationID)
{
var result = await superFaktura.Get(string.Format("invoices/deleteRelatedItem/{0}", relationID)).ConfigureAwait(false);
return superFaktura.DeserializeResult<StringMessageResponse>(result);
}
public async Task<byte[]> DownloadReceipt(int invoiceID)
{
var result = await superFaktura.GetByte(string.Format("invoices/receipt/{0}", invoiceID)).ConfigureAwait(false);
try
{
string testResult = Encoding.UTF8.GetString(result);
superFaktura.DeserializeResult<ErrorMessageResponse>(testResult);
}
catch (Exceptions.ParseException) when (result != null && result.Length != 0)
{
//test deserialization failed. it is a pdf file
}
catch (Exception)
{
throw;
}
return result;
}
[Obsolete("Not found in API documentation")]
public async Task<Detail> CreateFromProforma(int proformaID)
{
var proforma = await superFaktura.Get(string.Format("invoices/regular.json/{0}", proformaID)).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<ExpandoObject>(proforma);
var result = await superFaktura.Post("/invoices/create", new Request.DataData { Data = data}).ConfigureAwait(false);
var resultdata = superFaktura.DeserializeResult<Response<Detail>>(result);
return resultdata.Data;
}
[Obsolete("Not found in API documentation")]
public async Task<ExpandoObject> SetEstimateStatus(int estimateID, int status)
{
var result = await superFaktura.Get(string.Format("invoices/set_estimate_status/{0}/{1}/ajax:1", estimateID, status)).ConfigureAwait(false);
var data = superFaktura.DeserializeResult<Response<ExpandoObject>>(result);
return data.Data;
}
}
}