-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEInvoiceCrawler.cs
More file actions
1065 lines (970 loc) · 46.4 KB
/
Copy pathEInvoiceCrawler.cs
File metadata and controls
1065 lines (970 loc) · 46.4 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
namespace WindowsFormsApp1.Models
{
public class EInvoiceCrawler
{
/// <summary>
/// Địa chỉ API hóa đơn điện tử
/// </summary>
private readonly static string EInvoice_API = "https://hoadondientu.gdt.gov.vn:30000";
/// <summary>
/// Mã số thuế của doanh nghiệp
/// </summary>
private string _taxcode { get; set; }
/// <summary>
/// Tên đăng nhập
/// </summary>
private string _username { get; set; }
/// <summary>
/// Mật khẩu
/// </summary>
private string _password { get; set; }
/// <summary>
/// Token xác thực
/// </summary>
private string _token { get; set; }
/// <summary>
/// Lưu trữ cookie
/// </summary>
private CookieContainer _cookieContainer { get; set; }
/// <summary>
/// HTTP Client
/// </summary>
private HttpClient _client { get; set; }
/// <summary>
/// Quản lý các yêu cầu HTTP
/// </summary>
private HttpClientHandler _handler { get; set; }
public EInvoiceCrawler(string taxcode, string username, string password)
{
_taxcode = taxcode;
_username = username;
_password = password;
}
public EInvoiceCrawler()
{
}
public ProgressBarManager ProgressBarManager { get; set; }
/// <summary>
/// Check token theo mst xem có còn thời hạn không
/// </summary>
public async Task CheckToken()
{
try
{
if (!Directory.Exists(FileUtil.TOKEN_PATH))
{
Directory.CreateDirectory(FileUtil.TOKEN_PATH);
}
string[] files = Directory.GetFiles(FileUtil.TOKEN_PATH, $"{_taxcode}_Token_*.txt");
bool forceGetToken = true; // biến này dùng để check xem có lấy token mới không
// nếu tìm thấy token thì check expired timestamp
if (files.Length > 0)
{
string tokenPath = files[0];
string fileName = Path.GetFileNameWithoutExtension(tokenPath);
string[] fileNameParts = fileName.Split('_');
string expirationUnixTimeStr = fileNameParts[fileNameParts.Length - 1];
int.TryParse(expirationUnixTimeStr, out int expirationUnixTime);
var unixTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
if (unixTimestamp < expirationUnixTime)
{
_token = File.ReadAllText(tokenPath);
forceGetToken = false;
}
else
{
File.Delete(tokenPath);
}
}
if (forceGetToken)
{
// nếu không tìm thấy token thì lấy token theo username và password
var loginResponse = await LogInAsync();
if (loginResponse != null && loginResponse.Token != null)
{
_token = loginResponse.Token;
var unixTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
// cộng thêm 12 tiếng để lấy thời gian hết hạn
var expirationUnixTime = unixTimestamp + 12 * 60 * 60;
File.WriteAllText(FileUtil.TOKEN_PATH + $"\\{_taxcode}_Token_{expirationUnixTime}.txt", loginResponse.Token);
}
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
}
/// <summary>
/// Hàm này dùng để reset lại header mỗi khi send request
/// </summary>
private void ResetHeaderContent()
{
_handler?.Dispose();
_client?.Dispose();
_cookieContainer = new CookieContainer();
_handler = new HttpClientHandler()
{
CookieContainer = _cookieContainer,
};
_client = new HttpClient(_handler) { BaseAddress = new Uri(EInvoice_API) };
_client.DefaultRequestHeaders.Clear();
_client.Timeout = TimeSpan.FromSeconds(30); // thời gian timeout tối đa là 30s
_client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
_client.DefaultRequestHeaders.Add("Accept-Language", "vi-VN,vi;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,en;q=0.5");
_client.DefaultRequestHeaders.Add("Connection", "keep-alive");
_client.DefaultRequestHeaders.Add("Host", "hoadondientu.gdt.gov.vn:30000");
_client.DefaultRequestHeaders.Add("sec-ch-ua", "\"Google Chrome\";v=\"105\", \"Not) A; Brand\"; v=\"8\", \"Chromium\"; v=\"105\"");
_client.DefaultRequestHeaders.Add("sec-ch-ua-mobile", "20");
_client.DefaultRequestHeaders.Add("sec-ch-ua-platform", RandomHeaderRequest.GetRandomSecChUaPlatform());
_client.DefaultRequestHeaders.Add("User-Agent", RandomHeaderRequest.GetRandomUserAgent());
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);
}
/// <summary>
/// Đăng nhập vào trang hóa đơn điện tử để lấy token
/// </summary>
/// <param name="captcha">Captcha dùng để đăng nhập</param>
/// <returns></returns>
public async Task<LogInResponse> LogInAsync()
{
ResetHeaderContent();
try
{
// lấy captcha từ website hóa đơn điện tử
var captcha = await GetCaptchaSvgFromWebsiteHDDT();
// build request đăng nhập
var request = new Dictionary<string, string>
{
{ "ckey", captcha.Key },
{ "cvalue", captcha.Content },
{ "username", _username },
{ "password", _password }
};
// gửi request đăng nhập
var byteContent = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)));
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage responseMessage = await _client.PostAsync($"{DownloadEinvoiceSetting.LinkLogIn}", byteContent);
var stringContent = await responseMessage.Content.ReadAsStringAsync();
var loginResponse = JsonConvert.DeserializeObject<LogInResponse>(stringContent);
if (!responseMessage.IsSuccessStatusCode)
{
if (!string.IsNullOrEmpty(stringContent))
{
var errorMessage = JsonConvert.DeserializeObject<BaseTaxaxionResponse>(stringContent);
if (!string.IsNullOrEmpty(errorMessage.Message))
{
string messageLower = errorMessage.Message.Trim().ToLower();
if (messageLower.Contains("mã captcha không đúng"))
{
// TODO: Xử lý khi phản hồi trả về là mã captcha không đúng
}
else if (messageLower.Contains("tên đăng nhập hoặc mật khẩu không đúng"))
{
// TODO: Xử lý khi phản hồi trả về là tên đăng nhập hoặc mật khẩu không đúng
}
}
}
}
// những lỗi khác mà chưa case ở trên thì cứ EnsureSuccessStatusCode để nó throw exception ra nếu có lỗi
responseMessage.EnsureSuccessStatusCode();
return loginResponse;
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
}
/// <summary>
/// Lấy mã captcha từ website hóa đơn điện tử
/// </summary>
/// <returns></returns>
public async Task<CaptchaResponse> GetCaptchaSvgFromWebsiteHDDT()
{
ResetHeaderContent();
var response = await _client.GetAsync("/captcha");
response.EnsureSuccessStatusCode();
string res = await response.Content.ReadAsStringAsync();
var captcha = JsonConvert.DeserializeObject<CaptchaResponse>(res);
MisaDecaptcha misaDecaptcha = new MisaDecaptcha();
byte[] jpegBytes = misaDecaptcha.ConvertSvgToJpegByte(captcha.Content);
var decaptcha = await misaDecaptcha.DecodeCaptchaWithApi(jpegBytes);
captcha.Content = decaptcha.CaptchaText;
return captcha;
}
#region Đồng bộ từ cơ quan thuế
/// <summary>
/// Lấy chi tiết hóa đơn
/// </summary>
/// <param name="requests"></param>
/// <param name="progressBarManager"></param>
/// <returns></returns>
public async Task<HoaDon> GetInvoiceDetail(Dictionary<string, string> queryParams, string linkGetDetail)
{
ResetHeaderContent();
try
{
LogUtil.LogInfo($"GetInvoiceDetail_{JsonConvert.SerializeObject(queryParams)} has no xml. Get details");
// build param
string queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={kvp.Value}"));
string urlWithParams = linkGetDetail + "?" + queryString;
var response = await CommonPattern.RetryOperationAsync(async () =>
{
var httpResponse = await _client.GetAsync(urlWithParams);
httpResponse.EnsureSuccessStatusCode();
return httpResponse;
});
string stringContent = await response.Content.ReadAsStringAsync();
LogUtil.LogInfo($"GetInvoiceDetail_{JsonConvert.SerializeObject(queryParams)} get detail success");
return JsonConvert.DeserializeObject<HoaDon>(stringContent);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
}
/// <summary>
/// Lấy hóa đơn theo khoảng thời gian
/// </summary>
/// <param name="baseUrl"></param>
/// <param name="request"></param>
/// <param name="currentDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
private async Task<List<InvoicesResponse>> FetchInvoiceList(string baseUrl, InvoiceInfoRequest request, DateTime currentDate, DateTime endDate)
{
ResetHeaderContent();
_client.Timeout = TimeSpan.FromMilliseconds(2000);
List<InvoicesResponse> invoicesInRange = new List<InvoicesResponse>();
// Tạo biểu thức tìm kiếm cho ngày hiện tại
var textSearch = $"tdlap=ge={currentDate:dd/MM/yyyyT00:00:00};tdlap=le={endDate:dd/MM/yyyyT23:59:59}";
// Trạng thái hóa đơn
if (request.InvoiceStatus != 0)
{
textSearch += $";tthai=={request.InvoiceStatus}";
}
// Kết quả kiểm tra hóa đơn
if (request.InvoiceCheckResult != -1)
{
// nếu khác tất cả
textSearch += $";ttxly=={request.InvoiceCheckResult}";
}
else
{
// Nếu khác tất cả và là hóa đơn mua vào thì lấy cả 3 trạng thái
// 5: "Đã cấp mã hóa đơn",
// 6: "Tổng cục thuế đã nhận không mã",
// 8: "Tổng cục thuế đã nhận hóa đơn có mã khởi tạo từ máy tính tiền"
if (request.EinvoiceTab == 1)
{
textSearch += $";ttxly=in=(5,6,8)";
}
}
if (request.IsPaymentOrder && (baseUrl == DownloadEinvoiceSetting.LinkSoldInvoices || baseUrl == DownloadEinvoiceSetting.LinkPurchaseInvoices))
{
textSearch += $";unhiem==1";
}
var queryParams = new Dictionary<string, string>
{
{ "sort", "tdlap:desc,khmshdon:asc,shdon:desc" },
{ "size", $"{request.Take}" },
{ "search", textSearch }
};
// Build parameter
string urlWithParams = BuildQueryParam(baseUrl, queryParams);
// check xem có trang tiếp theo hay không
bool hasNextState = true;
// state của kết quả trả về hiện tại là param của trang sau
string currentState = null;
int totalPages = 0;
int currentPage = 0;
while (hasNextState)
{
try
{
// build lại param lấy trang tiếp theo dựa trên state
if (!string.IsNullOrEmpty(currentState))
{
queryParams["state"] = currentState;
urlWithParams = BuildQueryParam(baseUrl, queryParams);
}
string stringContent = "";
var response = await CommonPattern.RetryOperationAsync(async () =>
{
var httpResponse = await _client.GetAsync(urlWithParams);
stringContent = await httpResponse.Content.ReadAsStringAsync();
if (!httpResponse.IsSuccessStatusCode)
{
throw new Exception($"{urlWithParams} has error {stringContent}");
}
httpResponse.EnsureSuccessStatusCode();
return httpResponse;
});
var invoiceResponse = JsonConvert.DeserializeObject<InvoicesResponse>(stringContent);
if (totalPages == 0)
{
totalPages = (int)Math.Ceiling((double)invoiceResponse.Total / request.Take);
}
currentState = invoiceResponse.State;
// Thêm hóa đơn vào danh sách
invoicesInRange.Add(invoiceResponse);
hasNextState = !string.IsNullOrEmpty(currentState) && request.Take != 1;
currentPage++;
LogUtil.LogInfo($"FetchInvoiceList {currentPage}/ {totalPages} Page - TotalCrawl: {invoicesInRange.Sum(x => x.Datas.Count())}/ Total: {invoiceResponse.Total}");
if (currentPage % 10 == 0)
{
await Task.Delay(300);
}
}
catch (Exception ex)
{
hasNextState = false;
LogUtil.LogError(ex);
throw ex;
}
}
return invoicesInRange;
}
/// <summary>
/// Build query param từ dictionary và base url
/// </summary>
/// <param name="url"></param>
/// <param name="queryParams"></param>
/// <returns></returns>
private string BuildQueryParam(string url, Dictionary<string, string> queryParams)
{
string queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={kvp.Value}"));
return url + "?" + queryString;
}
/// <summary>
/// Tải XML
/// </summary>
/// <param name="requests"></param>
/// <param name="progressBarManager"></param>
/// <returns></returns>
public async Task<List<DownloadXMLResult>> DownloadXmlAsync(List<InvoiceBotResponse> invoices, RequestSyncTaxAuthority syncRequest)
{
// tạo folder lưu file xml, pdf phục vụ sau này
string folderPath = $"{Application.StartupPath}/HDDT/{_username}/Invoices";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", syncRequest.Authorization);
httpClient.DefaultRequestHeaders.Add("X-Misa-Context", syncRequest.XMISAContext);
// số lượng tải thành công
int totalSuccess = 0;
// số lượng tải thất bại
int totalError = 0;
// Create a list to store the tasks for downloading XML files
var xmlResults = new List<DownloadXMLResult>();
for (int i = 0; i < invoices.Count; i++)
{
try
{
var invoice = invoices[i];
var queryParams = new Dictionary<string, string> {
{ "nbmst", invoice.SellerTaxCode }, // MSTNBan
{ "khhdon", invoice.Series }, // Series
{ "shdon", invoice.InvoiceNo }, // InvoiceNo
{ "khmshdon", invoice.TemplateNo } // TemplateNo
};
// check xem nếu trong ký hiệu hóa đơn ký tự thứ 3 là M thì thay đổi link tải
var linkExportXml = invoice.Series.IndexOf("M") == 3 ? DownloadEinvoiceSetting.LinkExportXmlFromCashRegister : DownloadEinvoiceSetting.LinkExportXml;
// build query param
string urlWithParams = BuildQueryParam(linkExportXml, queryParams);
bool hasXML = false;
// tải xml, nếu sau 10 lần tải thất bại sẽ trả về exception do lỗi
// trả về task là string
var response = await CommonPattern.RetryOperationAsync(async () =>
{
ResetHeaderContent();
// thời gian tải xml timeout tối đa là 1.5s
_client.Timeout = TimeSpan.FromSeconds(1.5);
var httpResponse = await _client.GetAsync(urlWithParams);
if (!httpResponse.IsSuccessStatusCode)
{
string errorContent = await httpResponse.Content.ReadAsStringAsync();
var errorMessage = JsonConvert.DeserializeObject<BaseTaxaxionResponse>(errorContent);
if (!string.IsNullOrEmpty(errorMessage.Message))
{
if (errorMessage.Message.ToLower().Contains("không tồn tại hồ sơ gốc của hóa đơn"))
{
return null;
}
}
throw new Exception($"DownloadXmlAsync_Request failed {JsonConvert.SerializeObject(httpResponse)}");
}
httpResponse.EnsureSuccessStatusCode();
hasXML = true;
return httpResponse;
});
string xmlContent = "";
InvoiceBotResponse invoiceBotDetail = null;
if (hasXML && response != null)
{
byte[] responseBodyBytes = await response.Content.ReadAsByteArrayAsync();
xmlContent = ExtractToXml(responseBodyBytes, invoice, folderPath, httpClient, syncRequest);
}
else
{
var linkGetInvoiceDetail = invoice.Series.IndexOf("M") == 3 ? DownloadEinvoiceSetting.LinkGetDetailFromCashRegister : DownloadEinvoiceSetting.LinkGetDetail;
var invoiceDetail = await GetInvoiceDetail(queryParams, linkGetInvoiceDetail);
invoiceBotDetail = MappingTaxaxionToInbot(new List<HoaDon> { invoiceDetail }, syncRequest).FirstOrDefault();
}
xmlResults.Add(new DownloadXMLResult
{
InvoiceId = invoice.InvoiceId,
XMLContent = xmlContent,
DetailContent = invoiceBotDetail
});
totalSuccess++;
LogUtil.LogInfo($"DownloadXmlAsync_{urlWithParams} - success");
// tải 5 cái delay 0.5s
// sau vài chục lần chạy cấu hình thì thấy config như vậy là hợp lý để không bị thuế chặn
if ((i + 1) % 5 == 0)
{
await Task.Delay(300);
}
}
catch (Exception ex)
{
totalError++;
LogUtil.LogError(ex);
}
}
LogUtil.LogInfo($"DownloadXmlAsync_Finish to download xml: {totalSuccess}/{invoices.Count()} - Failed: {totalError}");
return xmlResults.ToList();
}
/// <summary>
/// Giải nén folder và tìm ra file xml để gửi sang kế toán
/// </summary>
/// <param name="zipData"></param>
/// <param name="request"></param>
/// <returns></returns>
private string ExtractToXml(byte[] zipData, InvoiceBotResponse request, string folderPath, HttpClient httpClient, RequestSyncTaxAuthority syncRequest)
{
try
{
using (var zipStream = new MemoryStream(zipData))
using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Read))
{
// Duyệt qua các file trong file zip để tìm html và xml
foreach (var entry in archive.Entries)
{
// lấy ra extention
var extension = Path.GetExtension(entry.Name);
// lấy ra tên file
var fileName = Path.GetFileName(entry.Name);
var tmpXmlFolder = $"{folderPath}/{request.InvoiceDate:dd-MM-yyyy}";
if (!Directory.Exists(tmpXmlFolder))
{
Directory.CreateDirectory(tmpXmlFolder);
}
string filePath = $"{tmpXmlFolder}/{request.TemplateNo}_{request.Series}_{request.InvoiceNo}_{fileName}";
if (extension.ToLower() == ".xml")
{
using (var fileStream = entry.Open())
using (var reader = new StreamReader(fileStream, Encoding.UTF8))
{
string xmlContent = reader.ReadToEnd();
return xmlContent;
}
}
}
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
return "";
}
/// <summary>
/// Đồng bộ danh sách hóa đơn từ CQT
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task<List<HoaDon>> SyncInvoiceInRange(RequestSyncTaxAuthority syncRequest)
{
try
{
LogUtil.LogInfo($"SyncInvoiceInRange_Start");
// Xử lý đăng nhập và lấy token
// trong hàm này đã retry rồi nên thôi bỏ qua bước retry
// đã xử lý exception sai captcha, sai mật khẩu.
await CheckToken();
ResetHeaderContent();
// tham số để lấy hóa đơn
InvoiceInfoRequest request = new InvoiceInfoRequest()
{
StartDate = syncRequest.StartDate,
EndDate = syncRequest.EndDate,
EinvoiceTab = syncRequest.IsSyncFromInputInvoice ? 1 : 0,
IsPaymentOrder = syncRequest.IsPaymentOrder
};
LogUtil.LogInfo($"SyncInvoiceInRange_Request param {JsonConvert.SerializeObject(request)}");
string linkInvoice = $"{DownloadEinvoiceSetting.LinkPurchaseInvoices}";
string linkInvoiceFromCashRegister = $"{DownloadEinvoiceSetting.LinkPurchaseInvoicesFromCashRegister}";
// nếu là hóa đơn bán ra thì sửa lại link lấy trên danh sách
if (request.EinvoiceTab == 0)
{
linkInvoice = $"{DownloadEinvoiceSetting.LinkSoldInvoices}";
linkInvoiceFromCashRegister = $"{DownloadEinvoiceSetting.LinkSoldInvoicesFromCashRegister}";
}
// Tổng số hóa đơn lấy về trong khoảng thời gian
var invoicesInRange = new List<HoaDon>();
// hóa đơn thường
List<InvoicesResponse> invoices = new List<InvoicesResponse>();
// hóa đơn khởi tạo từ máy tính tiền
List<InvoicesResponse> invoicesFromCashRegister = new List<InvoicesResponse>();
DateTime currentDate = request.StartDate.Date;
DateTime endDate = request.EndDate.Date;
var lstXMLDownload = new List<HoaDon>();
// Lấy Hóa đơn điện tử
invoices = await FetchInvoiceList(linkInvoice, request, currentDate, endDate);
if (invoices != null && invoices.Count > 0)
{
invoices.ForEach(x =>
{
lstXMLDownload.AddRange(x.Datas);
});
}
// Nếu không phải hóa đơn ủy nhiệm và có hóa đơn khởi tạo từ máy tính tiền
if (!request.IsPaymentOrder)
{
invoicesFromCashRegister = await FetchInvoiceList(linkInvoiceFromCashRegister, request, currentDate, endDate);
if (invoicesFromCashRegister != null && invoicesFromCashRegister.Count > 0)
{
invoicesFromCashRegister.ForEach(x =>
{
lstXMLDownload.AddRange(x.Datas);
});
}
}
// mapping list inv crwal được sang bên inbot
var invoiceBotMapping = MappingTaxaxionToInbot(lstXMLDownload, syncRequest);
//LogUtil.LogInfo($"MASTER DATA: {JsonConvert.SerializeObject(invoiceBotMapping)}");
LogUtil.LogInfo($"CRAWL MASTER SUCCESS {invoiceBotMapping.Count}/{invoices.FirstOrDefault().Total + invoicesFromCashRegister.FirstOrDefault().Total}");
if (lstXMLDownload.Count > 0)
{
var xmlResults = await DownloadXmlAsync(invoiceBotMapping, syncRequest);
//LogUtil.LogInfo($"DETAIL DATA: {JsonConvert.SerializeObject(xmlResults.ToList())}");
LogUtil.LogInfo($"CRAWL DETAIL SUCCESS {xmlResults.Count}");
}
return lstXMLDownload;
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
}
#endregion
/// <summary>
/// Lấy thông tin hóa đơn khi không kết nối
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
private async Task<HoaDon> GetInvoiceNoAuthorizationAsync(TaxaxionRequest request)
{
Dictionary<string, string> queryParams = new Dictionary<string, string>
{
{ "khmshdon", request.TemplateNo },
{ "nbmst", request.MSTNBan },
{ "shdon", request.InvoiceNo },
{ "cvalue", request.CaptchaText },
{ "ckey", request.CaptchaKey}
};
var linkSearch = DownloadEinvoiceSetting.LinkSearch;
if (!request.Series.StartsWith("C") && !request.Series.StartsWith("K"))
{
request.Series = request.Series.Substring(1);
}
if (request.Series.IndexOf("M") == 3)
{
linkSearch = DownloadEinvoiceSetting.LinkSearchFromCashRegister;
}
if (request.TemplateNo == "6")
{
queryParams.Add("hdon", $"06_0{(request.Series.IndexOf("N") == 3 ? 1 : 2)}");
queryParams.Add("tdlap", request.InvoiceDate.Value.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
}
else
{
queryParams.Add("hdon", $"0{request.TemplateNo}");
queryParams.Add("tgtttbso", request.Amount.ToString());
}
queryParams.Add("khhdon", request.Series);
// Build the query string
string queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={kvp.Value}"));
string urlWithParams = $"{linkSearch}?{queryString}";
LogUtil.LogInfo($"Build xong query cho hóa đơn {request.InvoiceNo} - {urlWithParams}");
LogUtil.LogInfo($"Bắt đầu Gọi API lên thuế Kiểm tra hóa đơn {request.InvoiceNo}");
HttpResponseMessage response = await _client.GetAsync(urlWithParams);
response.EnsureSuccessStatusCode();
string stringContent = await response.Content.ReadAsStringAsync();
LogUtil.LogInfo($"Gọi API lên thuế Kiểm tra hóa đơn {request.InvoiceNo} hoàn tất");
return JsonConvert.DeserializeObject<HoaDon>(stringContent);
}
public async Task<List<HoaDon>> GetInvoicesStatusAsync(List<TaxaxionRequest> requests, ProgressBarManager progressBarManager)
{
List<HoaDon> invoices = new List<HoaDon>();
int total = requests.Count;
int completed = 0;
HoaDon invoice = null;
try
{
// Use SemaphoreSlim to limit the number of concurrent tasks to 2
var semaphore = new SemaphoreSlim(2);
var tasks = requests.Select(async request =>
{
await semaphore.WaitAsync(); // Wait until a slot is available (max 2 concurrent tasks)
try
{
LogUtil.LogInfo($"Bắt đầu lấy captcha HDDT: {request.InvoiceNo}");
CaptchaResponse captcha = await GetCaptchaSvgFromWebsiteHDDT();
LogUtil.LogInfo($"Lấy captcha từ HDDT thành công: {request.InvoiceNo}");
request.CaptchaKey = captcha.Key;
request.CaptchaText = captcha.Content;
invoice = await GetInvoiceNoAuthorizationAsync(request);
}
catch (Exception ex)
{
LogUtil.LogInfo($"Lỗi {ex.Message}");
}
finally
{
lock (invoices) // Synchronize access to the list
{
if (invoice != null)
{
invoices.Add(invoice);
}
completed++;
progressBarManager.UpdateProgress(completed, total);
}
LogUtil.LogInfo($"-----Hoàn thành {request.InvoiceNo} - {completed}");
semaphore.Release(); // Release the slot after processing
}
}).ToArray();
// Wait for all worker tasks to complete
await Task.WhenAll(tasks);
}
catch (Exception ex)
{
LogUtil.LogError(ex);
throw ex;
}
return invoices;
}
#region Mapping object từ HDDT sang inbot
private List<InvoiceBotResponse> MappingTaxaxionToInbot(List<HoaDon> invoices, RequestSyncTaxAuthority syncRequest)
{
try
{
var invoiceBotMapping = new List<InvoiceBotResponse>();
invoices.ForEach(data =>
{
//Map thông tin cơ bản của hóa đơn
var result = new InvoiceBotResponse()
{
//Thông tin người bán
SellerTaxCode = data.MSTNBan,
SellerName = data.TenNBan,
SellerAddress = data.DChiNBan,
SellerPhoneNumber = data.SDTNBan,
BuyerTaxCode = data.MSTNMua,
BuyerName = data.TenNNT,
BuyerAddress = data.DChiNMua,
BuyerPhoneNumber = data.SDTNMua,
TemplateNo = data.KyHieuMSoHDon == null ? "" : ((int)data.KyHieuMSoHDon).ToString(),
Series = data.KyHieuHDon,
InvoiceNo = data.SoHDon,
InvoiceDate = data.ThoiDiemLap.Value.AddHours(7),
PaymentMethod = data.TenHThucTT,
CcyCode = data.DonViTienTe,
ExchangeRate = Convert.ToDecimal(data.TyGia),
TotalAmountWithoutVat = data.TTienChuaThue == null ? 0 : (decimal)data.TTienChuaThue,
TotalVATAmount = data.TTienThue == null ? 0 : (decimal)data.TTienThue,
TotalDiscountAmount = data.TienCKhauTMai == null ? 0 : (decimal)data.TienCKhauTMai,
TotalAmount = data.TTienTToan == null ? 0 : (decimal)data.TTienTToan,
//Cập nhật lại tiền thanh toán để mang đi kiểm tra tồn tại trên TCT
InfoND123 = MappingND123InfoTaxaxionToInbot(data),
Items = MappingItemTaxaxionToInbot(data.ChiTiets),
MCCQT = data.MaHDonDTu,
StatusInvoice = data.TThai,
ProcessingStatus = data.TThaiXuLy,
CreatedDate = data.NgayTao.Value,
ModifiedDate = data.NgayCapNhat.Value,
IsRegisterCash = data.KyHieuHDon.IndexOf("M") == 3,
SubscriberId = syncRequest.SubscriberId,
OrgId = syncRequest.OrganizationId,
};
result.TransId = BuildInvoiceId(result.InvoiceNo, result.TemplateNo, result.Series, result.InvoiceDate, result.SellerTaxCode);
result.InvoiceId = BuildInvoiceId(result.InvoiceNo, result.TemplateNo, result.Series, result.InvoiceDate, result.SellerTaxCode);
//Cập nhật thuế suất
if (result.InfoND123.ListVat.Count > 1) { result.VatRate = null; }
else if (result.InfoND123.ListVat.Count == 1) { result.VatRate = result.InfoND123.ListVat[0].VatRate; }
//Cập nhật thông tin CommonOthers
result.CommonOthers = MappingOtherInfors(data.TTinKhac);
invoiceBotMapping.Add(result);
});
return invoiceBotMapping;
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
return null;
}
private List<InvoiceItemInfo> MappingItemTaxaxionToInbot(List<HangHoaDVu> details)
{
var result = new List<InvoiceItemInfo>();
try
{
if (details == null || details.Count() == 0) return null;
int line = 1;
foreach (var item in details)
{
if (item == null) continue;
var newItem = new InvoiceItemInfo()
{
LineNumber = line,
Kind = item.TChat == null ? -1 : item.TChat.Value,
ItemCode = item.MaHHoaDVu,
ItemName = item.TenHHoaDVu,
UnitName = item.DViTinh,
Quantity = (decimal?)item.SoLuong,
UnitPrice = (decimal?)item.DGia,
AmountWithoutVat = (item.ThTien == null ? 0 : (decimal)item.ThTien) + (item.TienCKhau == null ? 0 : (decimal)item.TienCKhau),
VatRate = (decimal?)(item.TSuat != null ? item.TSuat * 100 : item.TSuat),
VatAmount = item.TThue == null ? 0 : (decimal)item.TThue,
DiscountRate = (decimal?)item.TyLeCKhau,
DiscountAmount = item.TienCKhau == null ? 0 : (decimal)item.TienCKhau,
Amount = item.ThTienSThue == null ? 0 : (decimal)item.ThTienSThue,
IsPromotion = item.TChat != null && (int)item.TChat == 2
};
//Nếu có tiền hàng và thuế suất, tính lại tiền thanh toán
if (newItem.AmountWithoutVat != 0 && newItem.VatAmount == 0 && newItem.VatRate > 0)
{
newItem.VatAmount = (newItem.AmountWithoutVat - newItem.DiscountAmount) * newItem.VatRate.Value / 100;
}
line++;
result.Add(newItem);
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
return result;
}
private ND123Info MappingND123InfoTaxaxionToInbot(HoaDon data)
{
var result = new ND123Info();
try
{
//Map danh sách tổng hợp thuế
var vatInfos = new List<VatInfo>();
if (data.TongHopThueSuat != null && data.TongHopThueSuat.Count > 0)
{
foreach (var item in data.TongHopThueSuat)
{
if (item == null) continue;
var isVatOtherTemp = false;
var newVat = new VatInfo
{
VatRate = GetVatRate(item.TSuat, isVatOther => isVatOtherTemp = isVatOther),
TotalVATAmount = item.TThue == null ? 0 : (decimal)item.TThue,
TotalAmountWithoutVat = item.ThTien == null ? 0 : (decimal)item.ThTien,
IsVatOther = isVatOtherTemp
};
vatInfos.Add(newVat);
}
}
//Map thông tin hóa đơn liên quan
var invConnect = new InvConnect()
{
TemplateNo = data.KyHieuMSoHDonGoc,
Series = data.KyHieuHDonGoc,
InvoiceNo = data.SoHDonGoc,
InvoiceDate = data.NgayHDonGoc,
Noted = data.GhiChuHDonGoc
};
result.Nature = data.TChat;
result.Status = data.TThai;
result.ProcessingStatus = data.TThaiXuLy;
result.ListVat = vatInfos;
result.InvConnect = invConnect;
//Thông tin vận chuyển
result.ShipperName = data.NgVanChuyen;
result.VehicleType = data.PhuongTienVanChuyen;
result.ExportPerson = data.TenNgXuatHang;
result.NumberAgreement = data.HopDongVanChuyen;
result.InternalTransferOrder = data.LenhDieuDong;
//Hợp đồng kinh tế số
result.CommandOrder = data.HopDongKinhTe;
result.CommandOrderDate = data.NgayHopDongKinhTe;
//Thông tin người mua
result.BuyerFullName = data.TenNMua;
result.BankAccountBuyer = data.SoTKNMua;
result.BankNameBuyer = data.NganHangNMua;
//Thông tin người bán
result.BankAccountSeller = data.SoTKNBan;
result.BankNameSeller = data.NganHangNBan;
//Thông tin bảng kê
result.DeclarationNumber = data.SoBangKe;
result.DeclarationDate = data.NgayBangKe;
result.ReasonRejects = data.PdNdungs;
//Map thêm danh sách các thông tin liên quan
if (data.HoaDonLienQuans != null && data.HoaDonLienQuans.Count > 0)
{
result.InvConnects = data.HoaDonLienQuans.Select(x => new InvConnect
{
TemplateNo = x.KyHieuMSoHDon != null ? ((int)x.KyHieuMSoHDon).ToString() : "",
Series = x.KyHieuHDon,
InvoiceNo = x.SoHDon,
Noted = x.GhiChuHDonGoc
}).ToList();
}
//Map thêm danh sách hóa đơn thông báo sai sót
if (data.HoaDonThongBaoSaiSots != null && data.HoaDonThongBaoSaiSots.Count > 0)
{
result.InvNotificationWrongs = data.HoaDonThongBaoSaiSots.Select(x => new InvConnect
{
TemplateNo = x.KyHieuMSoHDonGoc,
Series = x.KyHieuHDonGoc,
InvoiceNo = x.SoHDonGoc,
Noted = x.GhiChuHDonGoc,
ChangeDate = x.Ngay,
Type = x.Loai,
NotifyName = x.Ten,
NotifyNature = x.TinhChatTB.Value,
Reason = x.LyDo,
StatusInvoice = x.TThai,
StatusInvoiceProcess = x.TThaiXuLy
}).OrderBy(x => x.ChangeDate).ToList();
}
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
return result;
}
private List<OtherInfo> MappingOtherInfors(List<TTinKhac> tTinKhacs)
{
try
{
if (tTinKhacs == null || tTinKhacs.Count <= 0) return null;
var otherInfos = new List<OtherInfo>();
foreach (var tTinKhac in tTinKhacs)
{
if (tTinKhac == null) continue;
var otherInfo = new OtherInfo { TTruong = tTinKhac.TenTruong, KDLieu = tTinKhac.KieuDLieu, DLieu = tTinKhac.DLieu };
otherInfos.Add(otherInfo);
}
return otherInfos;
}
catch (Exception ex)
{
LogUtil.LogError(ex);
}
return null;
}
private decimal? GetVatRate(string stringVatRate, Action<bool> vatOtherCallback)
{
try
{
//Kiểm tra có phải thuế khác không
vatOtherCallback?.Invoke(!string.IsNullOrEmpty(stringVatRate) && stringVatRate.Contains("khac"));
if (string.IsNullOrEmpty(stringVatRate)) { return null; }
if (stringVatRate.Equals("xxx"))
{
return -4;
}