-
Notifications
You must be signed in to change notification settings - Fork 0
/
CebelcaAPISharp.cs
344 lines (299 loc) · 12.2 KB
/
CebelcaAPISharp.cs
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace CebelcaAPI
{
public class CebelcaPartner
{
public string Id { get; set; }
public string Name { get; set; }
}
public class CebelcaSalesLocation
{
public string Id { get; set; }
public string LocationId { get; set; }
public string RegisterId { get; set; }
public string Name { get {
return $"{LocationId}-{RegisterId}";
} }
}
public class CebInvoice
{
public string id { get; set; }
public string title { get; set; }
public DateTime date_served { get; set; }
public DateTime date_sent { get; set; }
public DateTime date_to_pay { get; set; }
public decimal amount { get; set; }
}
public class CebelcaAPISharp
{
private string _key = "";
private readonly ILogger<CebelcaAPISharp> _logger;
public CebelcaAPISharp(string key, ILogger<CebelcaAPISharp> logger = null)
{
_key = key;
if (logger != null)
{
_logger = logger;
}
else
{
_logger = new NullLogger<CebelcaAPISharp>();
}
}
private async Task<string> APICall(string region, string method, Dictionary<string, string> postvalues)
{
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes($"{_key}:x");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var url = $"https://www.cebelca.biz/API?_r={region}&_m={method}";
var content = new FormUrlEncodedContent(postvalues);
_logger.LogInformation("calling {url}. data: {data}", url, await content.ReadAsStringAsync());
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
_logger.LogInformation("response: {response}", responseString);
return responseString;
}
}
public async Task<byte[]> GetPDF(string id)
{
using (var client = new HttpClient())
{
var byteArray = Encoding.ASCII.GetBytes($"{_key}:x");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var url = $"https://www.cebelca.biz/API-pdf?id={id}&format=pdf&doctitle=Ra%C4%8Dun%20%C5%A1t.&lang=si&res=invoice-sent";
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var responseByteArr = await response.Content.ReadAsByteArrayAsync();
return responseByteArr;
} else
{
throw new Exception("Cebelca error!");
}
}
}
public async Task<string> AddInvoiceHead(string partnerId, string idDocumentExt, DateTime dateSent, DateTime dateServed, DateTime dateToPay, bool paid = false)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "date_sent",dateSent.ToShortDateString() },
{ "date_served",dateServed.ToShortDateString() },
{ "date_to_pay", dateToPay.ToShortDateString() },
{ "id_partner", partnerId },
{ "id_document_ext", idDocumentExt }
};
if (paid)
{
values.Add("payment", "paid");
values.Add("payment_act", "1");
}
var ret = await APICall("invoice-sent", "insert-smart-2", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
return id;
}
public async Task<string> GetNextInvoiceNo()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "doctype", "0"},
{ "year", DateTime.Now.Year.ToString() },
};
var ret = await APICall("invoice-sent", "select-next-title", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "proposed_title")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["proposed_title"].Value<string>();
return id;
}
public async Task<CebInvoice> GetInvoice(int id)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "id", id.ToString()},
};
var ret = await APICall("invoice-sent", "select-one", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var inv = json[0].ToObject<CebInvoice[]>();
return inv[0];
//var id = json[0][0]["proposed_title"].Value<string>();
//return id;
}
public async Task<IEnumerable<CebelcaPartner>> GetPartners()
{
var values = new Dictionary<string, string>();
var ret = await APICall("partner", "select-all", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
//var l = new List<CebelcaPartner>();
var l = json[0].Select(x => new CebelcaPartner
{
Id = x["id"].Value<string>(),
Name = x["name"].Value<string>()
}).ToList();
return l;
}
public async Task<IEnumerable<CebelcaSalesLocation>> GetSalesLocations()
{
var values = new Dictionary<string, string>();
var ret = await APICall("sales-location", "select-all", values);
var json = JArray.Parse(ret);
if (!json.Any() || !json[0].Any())
throw new Exception("Error from api (no data): " + ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
//var l = new List<CebelcaPartner>();
var l = json[0].Select(x => new CebelcaSalesLocation
{
Id = x["id"].Value<string>(),
LocationId = x["location_id"].Value<string>(),
RegisterId = x["register_id"].Value<string>()
}).ToList();
return l;
}
public async Task SendInvoiceByEmail(string invoiceId, string to, string subject, string content)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "id_invoice_sent", invoiceId},
{ "mto", to},
{ "msubj", subject },
{ "docformat", "pdf"},
{ "doctitle", "Račun št."},
{ "lang", "si"},
{ "content", content},
{ "format", "pdf"}
};
var ret = await APICall("mailer", "push-invoice-sent-doc", values);
}
public async Task<string> AddInvoiceLine(string invoiceId, string title, string measuringUnit, string qty, decimal price, string vat, string discount)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var cultureInfo = new CultureInfo("sl-SI"); // Slovenian culture
var customFormat = "#,0.00;-#,0.00";
var priceString = price.ToString(customFormat, cultureInfo);
var values = new Dictionary<string, string>
{
{ "title",title },
{ "mu",measuringUnit },
{ "qty",qty },
{ "id_invoice_sent", invoiceId },
{ "price", priceString },
{ "vat", vat },
{ "discount", discount },
};
var ret = await APICall("invoice-sent-b", "insert-into", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
return id;
}
public async Task<string> AddPayment(string invoiceId, DateTime dateOfPayment, decimal amount, string paymentMethod)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "date_of",dateOfPayment.ToShortDateString() },
{ "amount",amount.ToString() },
{ "id_invoice_sent", invoiceId },
{ "id_payment_method", paymentMethod},
};
var ret = await APICall("invoice-sent-p", "insert-into", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
return id;
}
public async Task<string> IssueInvoiceNoFiscalization(string invoiceId, string no="", string docType = "0")
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "id", invoiceId },
{ "doctype", docType},
{ "title", no},
};
var ret = await APICall("invoice-sent", "finalize-invoice-2015", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "new_title")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["new_title"].Value<string>();
return id;
}
public async Task<string> IssueInvoiceFiscalization(string invoiceId, string idLocation, string opTaxId, string opName, string invoiceNo = "", bool test_mode = false)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "id", invoiceId },
{ "id_location", idLocation },
{ "op-tax-id", opTaxId },
{ "op-name", opName },
{ "fiscalize", "1" },
{ "test_mode", test_mode ? "1" : "0" },
{ "title", invoiceNo},
};
var ret = await APICall("invoice-sent", "finalize-invoice", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "docnum")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["docnum"].Value<string>();
var eor = json[0][0]["eor"].Value<string>();
return id;
}
public async Task<string> AddPartner(string name, string email, string street, string city,
string postal)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("sl-SI");
var values = new Dictionary<string, string>
{
{ "name", name },
{ "email", email },
{ "street", street },
{ "city", city },
{ "postal", postal },
};
var ret = await APICall("partner", "assure", values);
var json = JArray.Parse(ret);
var retname = (json[0][0] as JObject).Properties().First().Name;
if (retname != "id")
throw new Exception("Error from api: " + ret);
var id = json[0][0]["id"].Value<string>();
return id;
}
}
}