forked from LinuxCabal/admin-cfdi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
admincfdi.py
1617 lines (1473 loc) · 59.5 KB
/
admincfdi.py
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
#!/usr/bin/env python
#! coding: utf-8
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
import tkinter as tk
import pygubu
from selenium import webdriver
from pyutil import Util
from pyutil import Mail
from pyutil import LibO
from pyutil import CFDIPDF
from values import Global
class Application(pygubu.TkApplication):
def __init__(self, parent):
self.parent = parent
self.util = Util()
self.g = Global()
self._init_var()
self._create_ui()
def _init_var(self):
self.builder = pygubu.Builder()
self.config = self.util.load_config(self.g.FILES['config'])
self.users_sat = {}
self.mail_servers = {}
self.users_xml = {}
self.users_pdf = {}
self.users_report = {}
#~ self.reports = {}
return
def _create_ui(self):
self.builder.add_from_file(self.g.FILES['main'])
self.builder.add_resource_path(self.g.PATHS['img'])
self.main = self.builder.get_object(self.g.MAIN, self.parent)
# Send mail author, remove wend bug fix
for k, v in self.g.CONTROLS.items():
control = self.builder.get_object(k)
for p, v in self.g.CONTROLS[k].items():
control[p] = v
self.builder.connect_callbacks(self)
self.parent.title(self.g.TITLE)
self.parent.resizable(0, 0)
self._center_window(self.parent)
if self.config:
key = 'contribuyentes'
if key in self.config:
self.users_sat = self.config[key]
listbox = self._get_object('listbox_contribuyentes')
for k, _ in self.config[key].items():
self.util.listbox_insert(listbox, k)
if len(self.users_sat) == 1:
self._set('current_user', k)
key = 'mail_servers'
if key in self.config:
self.mail_servers = self.config[key]
listbox = self._get_object('listbox_mail_servers')
for k, _ in self.config[key].items():
self.util.listbox_insert(listbox, k)
key = 'users_xml'
if key in self.config:
self.users_xml = self.config[key]
listbox = self._get_object('listbox_users_xml')
for k, _ in self.config[key].items():
self.util.listbox_insert(listbox, k)
if len(self.users_xml) == 1:
self._set('current_user_xml', k)
key = 'users_pdf'
if key in self.config:
self.users_pdf = self.config[key]
listbox = self._get_object('listbox_users_pdf')
for k, _ in self.config[key].items():
self.util.listbox_insert(listbox, k)
if len(self.users_pdf) == 1:
self._set('current_user_pdf', k)
key = 'users_report'
if key in self.config:
self.users_report = self.config[key]
listbox = self._get_object('listbox_users_report')
for k, _ in self.config[key].items():
self.util.listbox_insert(listbox, k)
if len(self.users_report) == 1:
self._set('current_user_report', k)
listbox = self._get_object('listbox_reports')
reports = self.users_report[k]['reports']
for k, _ in reports.items():
self.util.listbox_insert(listbox, k)
options = (
'options_mail',
'options_xml',
'options_pdf',
'options_report',
)
for opt in options:
if opt in self.config:
data = self.config[opt]
for k, v in self.config[opt].items():
self._set(k, v)
now = self.util.now()
combo = self._get_object('combo_year')
years = range(self.g.YEAR_INIT, now.year + 1)
self.util.combo_values(combo, years)
combo = self._get_object('combo_month')
months = ['%02d' % x for x in range(1, 13)]
self.util.combo_values(combo, months, now.month - 1)
combo = self._get_object('combo_day')
days_month = self.util.get_days(now.year, now.month) + 1
days = ['%02d' % x for x in range(0, days_month)]
self.util.combo_values(combo, days, 0)
self._set('mail_port', 993)
self._get_object('check_ssl').invoke()
self._focus_set('text_rfc')
return
def _center_window(self, root):
w = 710
h = 560
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w) / 2
y = (sh - h) / 2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
return
def _get(self, name, var=False):
if var:
return self.builder.get_variable(name)
else:
return self.builder.get_variable(name).get()
def _set(self, name, value='', update=False):
self.builder.get_variable(name).set(value)
if update:
self.parent.update_idletasks()
return
def _get_object(self, name):
return self.builder.get_object(name)
def _focus_set(self, name):
control = name
if isinstance(name, str):
control = self._get_object(name)
control.focus_set()
return
def _config(self, name, properties):
control = name
if isinstance(name, str):
control = self._get_object(name)
control.config(**properties)
return
def _focus_in(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
if isinstance(w, tk.Entry):
w.selection_range(0, tk.END)
return
def _focus_out(self, event):
event.widget['bg'] = self.g.COLORS['FOCUS_OUT']
return
def listbox_contribuyentes_click(self, event):
w = event.widget
if w.curselection():
self._set('current_user', event.widget.get(w.curselection()[0]))
return
def listbox_contribuyentes_double_click(self, event):
w = event.widget
if w.curselection():
sel = w.get(w.curselection()[0])
info = 'Destino: ' + self.users_sat[sel]['target_sat']
self.util.msgbox(info, 2)
return
def listbox_mail_servers_click(self, event):
w = event.widget
sel = w.get(w.curselection()[0])
self.util.msgbox(self.mail_servers[sel]['mail_target'], 2)
return
def radio_type_query(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
opt = self._get('type_invoice')
if opt == 1:
radio = 'radio_invoice_receive'
msg = 'RFC Receptor '
else:
radio = 'radio_invoice_send'
msg = 'RFC Emisor '
self._config(radio, {'bg': self.g.COLORS['DEFAULT']})
self._set('info_rfc', msg)
return
def radio_type_search(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
opt = self._get('type_search')
if opt == 1:
self._focus_set('text_uuid')
radio = 'radio_search_date'
else:
self._focus_set('text_rfc')
radio = 'radio_search_uuid'
self._config(radio, {'bg': self.g.COLORS['DEFAULT']})
return
def radio_organizate_xml(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
opt = self._get('xml_organizate')
if opt == 1:
self._config('radio_emisor', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_receptor', {'bg': self.g.COLORS['DEFAULT']})
elif opt == 2:
self._config('radio_only_date', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_receptor', {'bg': self.g.COLORS['DEFAULT']})
elif opt == 3:
self._config('radio_only_date', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_emisor', {'bg': self.g.COLORS['DEFAULT']})
return
def radio_rename_xml(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
opt = self._get('xml_rename')
if opt == 1:
self._config('radio_uuid', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_template', {'bg': self.g.COLORS['DEFAULT']})
elif opt == 2:
self._config('radio_original', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_template', {'bg': self.g.COLORS['DEFAULT']})
elif opt == 3:
self._config('radio_original', {'bg': self.g.COLORS['DEFAULT']})
self._config('radio_uuid', {'bg': self.g.COLORS['DEFAULT']})
return
def radio_template_type(self, event):
w = event.widget
w['bg'] = self.g.COLORS['FOCUS_IN']
opt = self._get('template_type')
radio = 'radio_ods'
button = 'button_select_template_ods'
self._config(button, {'state': 'normal'})
self._config('button_select_template_json', {'state': 'normal'})
if opt == 1:
radio = 'radio_json'
button = 'button_select_template_json'
self._config(radio, {'bg': self.g.COLORS['DEFAULT']})
self._config(button, {'state': 'disabled'})
return
def button_save_emisor_click(self):
ok, data = self._validate_save_emisor()
if not ok:
return
self.users_sat[data['razon_social']] = data
listbox = self._get_object('listbox_contribuyentes')
self.util.listbox_insert(listbox, data['razon_social'])
var = ('razon_social', 'user_sat', 'password', 'target_sat')
for v in var:
self._set(v)
self.util.save_config(
self.g.FILES['config'], 'contribuyentes', self.users_sat)
self._set('current_user', data['razon_social'])
return
def _validate_save_emisor(self):
data = ()
razon_social = self._get('razon_social').upper().strip()
user_sat = self._get('user_sat').upper().strip()
password = self._get('password').strip()
target_sat = self._get('target_sat')
if not razon_social:
self._focus_set('text_razon_social')
msg = 'El campo RAZON SOCIAL es requerido'
self.util.msgbox(msg)
return False, data
if razon_social in self.users_sat:
self._focus_set('text_razon_social')
msg = 'Esta RAZON SOCIAL ya esta guardada'
self.util.msgbox(msg)
return False, data
if not user_sat:
self._focus_set('text_user_sat')
msg = 'El campo RFC es requerido'
self.util.msgbox(msg)
return False, data
if len(user_sat) < 12 or len(user_sat) > 13:
self._focus_set('text_user_sat')
msg = 'La longitud del RFC es incorrecta'
self.util.msgbox(msg)
return False, data
for k, v in self.users_sat.items():
if self.users_sat[k]['user_sat'] == user_sat:
self._focus_set('text_user_sat')
msg = 'Este RFC ya esta dado de alta'
self.util.msgbox(msg)
return False, data
if not password:
self._focus_set('text_password')
msg = 'El campo CLAVE CIEC es requerido'
self.util.msgbox(msg)
return False, data
if not target_sat:
self._focus_set('text_target_sat')
msg = 'El campo DIRECTORIO DESTINO es requerido'
self.util.msgbox(msg)
return False, data
data = {
'razon_social': razon_social,
'user_sat': user_sat,
'password': password,
'target_sat': self.util.path_config(target_sat)
}
return True, data
def button_delete_emisor_click(self):
listbox = self._get_object('listbox_contribuyentes')
sel = self.util.listbox_selection(listbox)
if sel:
var = ('razon_social', 'user_sat', 'password', 'target_sat')
for v in var:
self._set(v, self.users_sat[sel][v])
del self.users_sat[sel]
self.util.listbox_delete(listbox)
self.util.save_config(
self.g.FILES['config'], 'contribuyentes', self.users_sat)
self._set('current_user')
else:
self._focus_set(listbox)
msg = 'Selecciona el contribuyente a eliminar de la lista'
self.util.msgbox(msg)
return
def button_select_folder_click(self):
folder = self.util.get_folder(self.parent)
if folder:
self._set('target_sat', folder)
return
def combo_month_click(self, event):
combo = self._get_object('combo_day')
days_month = self.util.get_days(
self._get('search_year'), self._get('search_month')) + 1
days = ['%02d' % x for x in range(0, days_month)]
self.util.combo_values(combo, days, 0)
return
def button_download_sat_click(self):
ok, data = self._validate_download_sat()
if not ok:
return
self._download_sat(data)
return
def _download_sat(self, data):
self._set('msg_user', 'Abriendo Firefox...', True)
page_query = self.g.SAT['page_receptor']
if data['type_invoice'] == 1:
page_query = self.g.SAT['page_emisor']
# To prevent download dialog
profile = webdriver.FirefoxProfile()
profile.set_preference(
'browser.download.folderList', 2)
profile.set_preference(
'browser.download.manager.showWhenStarting', False)
profile.set_preference(
'browser.helperApps.alwaysAsk.force', False)
profile.set_preference(
'browser.helperApps.neverAsk.saveToDisk',
'text/xml, application/octet-stream, application/xml')
profile.set_preference(
'browser.download.dir', data['user_sat']['target_sat'])
# mrE - desactivar telemetry
profile.set_preference(
'toolkit.telemetry.prompted', 2)
profile.set_preference(
'toolkit.telemetry.rejected', True)
profile.set_preference(
'toolkit.telemetry.enabled', False)
profile.set_preference(
'datareporting.healthreport.service.enabled', False)
profile.set_preference(
'datareporting.healthreport.uploadEnabled', False)
profile.set_preference(
'datareporting.healthreport.service.firstRun', False)
profile.set_preference(
'datareporting.healthreport.logging.consoleEnabled', False)
profile.set_preference(
'datareporting.policy.dataSubmissionEnabled', False)
profile.set_preference(
'datareporting.policy.dataSubmissionPolicyResponseType', 'accepted-info-bar-dismissed')
#profile.set_preference(
# 'datareporting.policy.dataSubmissionPolicyAccepted'; False) # este me marca error, why?
#oculta la gran flecha animada al descargar
profile.set_preference(
'browser.download.animateNotifications', False)
try:
browser = webdriver.Firefox(profile)
self._set('msg_user', 'Conectando...', True)
browser.get(self.g.SAT['page_init'])
txt = browser.find_element_by_name(self.g.SAT['user'])
txt.send_keys(data['user_sat']['user_sat'])
txt = browser.find_element_by_name(self.g.SAT['password'])
txt.send_keys(data['user_sat']['password'])
txt.submit()
self.util.sleep(3)
self._set('msg_user', 'Conectado...', True)
browser.get(page_query)
self.util.sleep(3)
self._set('msg_user', 'Buscando...', True)
if data['type_search'] == 1:
txt = browser.find_element_by_id(self.g.SAT['uuid'])
txt.click()
txt.send_keys(data['search_uuid'])
else:
# Descargar por fecha
opt = browser.find_element_by_id(self.g.SAT['date'])
opt.click()
self.util.sleep()
if data['search_rfc']:
if data['type_search'] == 1:
txt = browser.find_element_by_id(self.g.SAT['receptor'])
else:
txt = browser.find_element_by_id(self.g.SAT['emisor'])
txt.send_keys(data['search_rfc'])
# Emitidas
if data['type_invoice'] == 1:
year = int(data['search_year'])
month = int(data['search_month'])
dates = self.util.get_dates(year, month)
txt = browser.find_element_by_id(self.g.SAT['date_from'])
arg = "document.getElementsByName('{}')[0]." \
"removeAttribute('disabled');".format(
self.g.SAT['date_from_name'])
browser.execute_script(arg)
txt.send_keys(dates[0])
txt = browser.find_element_by_id(self.g.SAT['date_to'])
arg = "document.getElementsByName('{}')[0]." \
"removeAttribute('disabled');".format(
self.g.SAT['date_to_name'])
browser.execute_script(arg)
txt.send_keys(dates[1])
# Recibidas
else:
#~ combos = browser.find_elements_by_class_name(
#~ self.g.SAT['combos'])
#~ combos[0].click()
combo = browser.find_element_by_id(self.g.SAT['year'])
combo = browser.find_element_by_id(
'sbToggle_{}'.format(combo.get_attribute('sb')))
combo.click()
self.util.sleep(2)
link = browser.find_element_by_link_text(
data['search_year'])
link.click()
self.util.sleep(2)
combo = browser.find_element_by_id(self.g.SAT['month'])
combo = browser.find_element_by_id(
'sbToggle_{}'.format(combo.get_attribute('sb')))
combo.click()
self.util.sleep(2)
link = browser.find_element_by_link_text(
data['search_month'])
link.click()
self.util.sleep(2)
if data['search_day'] != '00':
combo = browser.find_element_by_id(self.g.SAT['day'])
sb = combo.get_attribute('sb')
combo = browser.find_element_by_id(
'sbToggle_{}'.format(sb))
combo.click()
self.util.sleep()
if data['search_month'] == data['search_day']:
links = browser.find_elements_by_link_text(
data['search_day'])
for l in links:
p = l.find_element_by_xpath(
'..').find_element_by_xpath('..')
sb2 = p.get_attribute('id')
if sb in sb2:
link = l
break
else:
link = browser.find_element_by_link_text(
data['search_day'])
link.click()
self.util.sleep()
browser.find_element_by_id(self.g.SAT['submit']).click()
sec = 3
if data['type_invoice'] != 1 and data['search_day'] == '00':
sec = 15
self.util.sleep(sec)
# Bug del SAT
if data['type_invoice'] != 1 and data['search_day'] != '00':
combo = browser.find_element_by_id(self.g.SAT['day'])
sb = combo.get_attribute('sb')
combo = browser.find_element_by_id(
'sbToggle_{}'.format(sb))
combo.click()
self.util.sleep(2)
if data['search_month'] == data['search_day']:
links = browser.find_elements_by_link_text(
data['search_day'])
for l in links:
p = l.find_element_by_xpath(
'..').find_element_by_xpath('..')
sb2 = p.get_attribute('id')
if sb in sb2:
link = l
break
else:
link = browser.find_element_by_link_text(
data['search_day'])
link.click()
self.util.sleep(2)
browser.find_element_by_id(self.g.SAT['submit']).click()
self.util.sleep(sec)
elif data['type_invoice'] == 2 and data['sat_month']:
return self._download_sat_month(data, browser)
try:
found = True
content = browser.find_elements_by_class_name(
self.g.SAT['subtitle'])
for c in content:
if self.g.SAT['found'] in c.get_attribute('innerHTML') \
and c.is_displayed():
found = False
break
except Exception as e:
print (str(e))
if found:
docs = browser.find_elements_by_name(self.g.SAT['download'])
t = len(docs)
pb = self._get_object('progressbar')
pb['maximum'] = t
pb.start()
for i, v in enumerate(docs):
msg = 'Factura {} de {}'.format(i+1, t)
pb['value'] = i + 1
self._set('msg_user', msg, True)
download = self.g.SAT['page_cfdi'].format(
v.get_attribute('onclick').split("'")[1])
browser.get(download)
pb['value'] = 0
pb.stop()
self.util.sleep()
else:
self._set('msg_user', 'Sin facturas...', True)
except Exception as e:
print (e)
finally:
try:
self._set('msg_user', 'Desconectando...', True)
link = browser.find_element_by_partial_link_text('Cerrar Sesi')
link.click()
except:
pass
finally:
browser.close()
self._set('msg_user', 'Desconectado...')
return
def _download_sat_month(self, data, browser):
year = int(data['search_year'])
month = int(data['search_month'])
days_month = self.util.get_days(year, month) + 1
days = ['%02d' % x for x in range(1, days_month)]
for d in days:
combo = browser.find_element_by_id(self.g.SAT['day'])
sb = combo.get_attribute('sb')
combo = browser.find_element_by_id('sbToggle_{}'.format(sb))
combo.click()
self.util.sleep(2)
if data['search_month'] == d:
links = browser.find_elements_by_link_text(d)
for l in links:
p = l.find_element_by_xpath(
'..').find_element_by_xpath('..')
sb2 = p.get_attribute('id')
if sb in sb2:
link = l
break
else:
link = browser.find_element_by_link_text(d)
link.click()
self.util.sleep(2)
browser.find_element_by_id(self.g.SAT['submit']).click()
self.util.sleep(3)
docs = browser.find_elements_by_name(self.g.SAT['download'])
if docs:
t = len(docs)
pb = self._get_object('progressbar')
pb['maximum'] = t
pb.start()
for i, v in enumerate(docs):
msg = 'Factura {} de {}'.format(i+1, t)
pb['value'] = i + 1
self._set('msg_user', msg, True)
download = self.g.SAT['page_cfdi'].format(
v.get_attribute('onclick').split("'")[1])
browser.get(download)
self.util.sleep()
pb['value'] = 0
pb.stop()
self.util.sleep()
return
def _validate_download_sat(self):
data = {}
if not self.users_sat:
msg = 'Agrega un RFC y contraseña a consultar'
self.util.msgbox(msg)
return False, data
current_user = self._get('current_user')
if not current_user:
msg = 'Selecciona primero una Razón Social'
self.util.msgbox(msg)
return False, data
uuid = self._get('search_uuid')
opt = self._get('type_search')
rfc = ''
if opt == 1:
if not uuid:
self._focus_set('text_uuid')
msg = 'El campo UUID es requerido'
self.util.msgbox(msg)
return False, data
if len(uuid) != 36:
self._focus_set('text_uuid')
msg = 'La longitud del campo UUID es incorrecta'
self.util.msgbox(msg)
return False, data
else:
rfc = self._get('search_rfc').strip().upper()
if rfc:
if len(rfc) < 12 or len(rfc) > 13:
self._focus_set('text_rfc')
msg = 'La longitud del RFC es incorrecta'
self.util.msgbox(msg)
return False, data
sat_month = self._get('sat_month')
if sat_month:
search_day = '00'
else:
search_day = self._get('search_day')
data = {
'user_sat': self.users_sat[current_user],
'type_invoice': self._get('type_invoice'),
'type_search': opt,
'search_uuid': uuid,
'search_rfc': rfc,
'search_year': self._get('search_year'),
'search_month': self._get('search_month'),
'search_day': search_day,
'sat_month': sat_month,
}
return True, data
def check_ssl_click(self, event):
port = 143
mail_ssl = self._get('mail_ssl')
if mail_ssl == 1:
port = 993
self._set('mail_port', port)
return
def button_select_folder_mail_click(self):
folder = self.util.get_folder(self.parent)
if folder:
self._set('mail_target', folder)
return
def button_save_mail_server_click(self):
ok, data = self._validate_mail()
if not ok:
return
self.mail_servers[data['mail_user']] = data
listbox = self._get_object('listbox_mail_servers')
self.util.listbox_insert(listbox, data['mail_user'])
var = ('mail_server', 'mail_user', 'mail_password', 'mail_target')
for v in var:
self._set(v)
self._set('mail_port', 993)
self._set('mail_ssl', 1)
self.util.save_config(
self.g.FILES['config'], 'mail_servers', self.mail_servers)
return
def _validate_mail(self):
data = {}
mail_server = self._get('mail_server')
if not mail_server:
self._focus_set('text_mail_server')
msg = 'Captura el servidor IMAP'
self.util.msgbox(msg)
return False, data
mail_port = self._get('mail_port')
mail_ssl = self._get('mail_ssl')
mail_user = self._get('mail_user')
if not mail_user:
self._focus_set('text_mail_user')
msg = 'Captura el nombre de usuario'
self.util.msgbox(msg)
return False, data
if mail_user in self.mail_servers:
msg = 'Este servidor y usuario ya esta en la lista, si ' \
'quieres cambiar los datos, primero eliminalo.'
self.util.msgbox(msg)
return False, data
mail_password = self._get('mail_password')
if not mail_password:
self._focus_set('text_mail_password')
msg = 'Captura la contraseña de acceso'
self.util.msgbox(msg)
return False, data
mail_target = self._get('mail_target')
if not mail_target:
self._focus_set('text_mail_target')
msg = 'Selecciona el directorio destino de descarga'
self.util.msgbox(msg)
return False, data
if not self.util.validate_dir(mail_target, 'w'):
self._focus_set('text_mail_target')
msg = 'No tienes derechos de escritura en el directorio destino'
self.util.msgbox(msg)
return False, data
data['mail_server'] = mail_server
data['mail_port'] = mail_port
data['mail_ssl'] = mail_ssl
data['mail_user'] = mail_user
data['mail_password'] = mail_password
data['mail_target'] = self.util.path_config(mail_target)
mail = Mail(data)
if mail.error:
self.util.msgbox(mail.error)
del mail
return False, {}
return True, data
def button_delete_mail_server_click(self):
listbox = self._get_object('listbox_mail_servers')
sel = self.util.listbox_selection(listbox)
if sel:
var = (
'mail_server',
'mail_port',
'mail_ssl',
'mail_user',
'mail_password',
'mail_target')
for v in var:
self._set(v, self.mail_servers[sel][v])
del self.mail_servers[sel]
self.util.listbox_delete(listbox)
self.util.save_config(
self.g.FILES['config'], 'mail_servers', self.mail_servers)
else:
self._focus_set(listbox)
msg = 'Selecciona el contribuyente a eliminar de la lista'
self.util.msgbox(msg)
return
def button_download_mail_click(self):
ok, data = self._validate_download_mail()
if not ok:
return
self.util.save_config(
self.g.FILES['config'], 'options_mail', data)
pb = self._get_object('progressbar')
info = self._get('msg_user', True)
for k, v in self.mail_servers.items():
mail = Mail(v)
if mail.error:
msg = mail.error
del mail
self.util.msgbox(msg)
continue
#~ try:
mail.down_all_mails(pb, info, data)
#~ except Exception as e:
#~ print (e)
#~ finally:
del mail
pb['value'] = 0
pb.stop()
self._set('msg_user', 'Descarga completa...')
return
def _validate_download_mail(self):
data = {}
if not self.mail_servers:
msg = 'Agrega una cuenta de correo primero.'
self.util.msgbox(msg)
return False, data
data['mail_subfolder'] = self._get('mail_subfolder')
data['mail_name'] = self._get('mail_name')
data['mail_delete1'] = self._get('mail_delete1')
data['mail_delete2'] = self._get('mail_delete2')
return True, data
def button_select_folder_source_xml_click(self):
folder = self.util.get_folder(self.parent)
if folder:
self._set('xml_source', folder)
self._set('xml_target', folder)
return
def button_select_folder_target_xml_click(self):
folder = self.util.get_folder(self.parent)
if folder:
self._set('xml_target', folder)
return
def listbox_users_xml_double_click(self, event):
w = event.widget
if w.curselection():
sel = w.get(w.curselection()[0])
info = 'Origen: ' + self.users_xml[sel]['xml_source'] + '\n\n'
info += 'Destino: ' + self.users_xml[sel]['xml_target'] + '\n'
self.util.msgbox(info, 2)
return
def listbox_users_xml_click(self, event):
w = event.widget
if w.curselection():
self._set('current_user_xml', w.get(w.curselection()[0]))
return
def listbox_users_pdf_click(self, event):
w = event.widget
if w.curselection():
sel = w.get(w.curselection()[0])
info = 'Origen: ' + self.users_pdf[sel]['pdf_source'] + '\n\n'
info += 'Destino: ' + self.users_pdf[sel]['pdf_target'] + '\n\n'
self.util.msgbox(info, 2)
return
def button_save_xml_user_click(self):
ok, data = self._validate_xml_user()
if not ok:
return
self.users_xml[data['xml_user']] = data
listbox = self._get_object('listbox_users_xml')
self.util.listbox_insert(listbox, data['xml_user'])
var = ('xml_user', 'xml_source', 'xml_target')
for v in var:
self._set(v)
self._set('xml_copy', 0)
self.util.save_config(
self.g.FILES['config'], 'users_xml', self.users_xml)
self._set('current_user_xml', data['xml_user'])
return
def _validate_xml_user(self):
data = {}
xml_user = self._get('xml_user').strip().upper()
if not xml_user:
self._focus_set('text_xml_user')
msg = 'Captura el campo USUARIO'
self.util.msgbox(msg)
return False, data
if xml_user in self.users_xml:
msg = 'Este usuario ya esta en la lista, si ' \
'quieres cambiar los datos, primero eliminalo.'
self.util.msgbox(msg)
return False, data
xml_source = self._get('xml_source')
if not xml_source:
self._focus_set('text_xml_source')
msg = 'Selecciona el directorio origen para este usuario'
self.util.msgbox(msg)
return False, data
if not self.util.validate_dir(xml_source, 'r'):
self._focus_set('text_xml_source')
msg = 'No tienes derechos de lectura en el directorio origen'
self.util.msgbox(msg)
return False, data
xml_target = self._get('xml_target')
if not xml_target:
self._focus_set('text_xml_target')
msg = 'Selecciona el directorio destino para este usuario'
self.util.msgbox(msg)
return False, data
if not self.util.validate_dir(xml_target, 'w'):
self._focus_set('text_xml_target')
msg = 'No tienes derechos de escritura en el directorio destino'
self.util.msgbox(msg)
return False, data
data['xml_user'] = xml_user
data['xml_source'] = self.util.path_config(xml_source)
data['xml_target'] = self.util.path_config(xml_target)
data['xml_copy'] = self._get('xml_copy')
return True, data
def button_delete_xml_user_click(self):
listbox = self._get_object('listbox_users_xml')
sel = self.util.listbox_selection(listbox)
if sel:
var = (
'xml_user',
'xml_source',
'xml_target',
'xml_copy',
)
for v in var:
self._set(v, self.users_xml[sel][v])
del self.users_xml[sel]
self.util.listbox_delete(listbox)
self.util.save_config(
self.g.FILES['config'], 'users_xml', self.users_xml)
self._set('current_user_xml')
else:
self._focus_set(listbox)
msg = 'Selecciona el usuario a eliminar de la lista'
self.util.msgbox(msg)
return
def button_organizate_xml_click(self):
ok, options = self._validate_organizate_xml()
if not ok:
return
files = options['files']
del options['files']
total = len(files)
data = self.users_xml[options['xml_user']]
del options['xml_user']
self.util.save_config(self.g.FILES['config'], 'options_xml', options)
data.update(options)
pb = self._get_object('progressbar')
pb['maximum'] = total
pb.start()
j = 0
for i, f in enumerate(files):
pb['value'] = i + 1
pb.update_idletasks()
if self._organizate_xml(f, data):
j += 1
pb['value'] = 0
pb.stop()
msg = 'Archivos XML encontrados: {}\n'
msg += 'Archivos XML organizados: {}'
msg = msg.format(total, j)
self.util.msgbox(msg, 2)
return
def _organizate_xml(self, path, data):
pdf_ori = '{}_ORIGINAL.pdf'
name_file_uuid = ''
name_file_template = ''
info_pdf = self.util.get_path_info(path)
name_pdf_1 = pdf_ori.format(info_pdf[2])
name_pdf_2 = '{}.pdf'.format(info_pdf[2])
paths_pdf = (
(self.util.join(info_pdf[0], name_pdf_1), name_pdf_1),
(self.util.join(info_pdf[0], name_pdf_2), name_pdf_2),