-
Notifications
You must be signed in to change notification settings - Fork 7
/
purchase_invoice.py
896 lines (838 loc) · 38 KB
/
purchase_invoice.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
#!/usr/bin/python3
from purchase_invoice_google_parser import PurchaseInvoiceGoogleParser
from purchase_invoice_parser import PurchaseInvoiceParser, SupplierItem
from settings import STANDARD_PRICE_LIST, STANDARD_NAMING_SERIES_PINV, VAT_DESCRIPTION, DELIVERY_COST_ACCOUNT, \
DELIVERY_COST_DESCRIPTION, SOMIKO_ACCOUNTS, AGGREGATE_ITEMS
import os
import utils
import PySimpleGUI as sg
import easygui
import subprocess
import re
from api import Api, LIMIT
from api_wrapper import gui_api_wrapper
import settings
import doc
import company
import bank
import stock
from invoice import Invoice
from collections import defaultdict
from pprint import pprint
import jsondiff
from jsondiff.symbols import insert, delete
import jsoneditor
# extract amounts of form xxx,xx from string
def extract_amounts(s):
amounts = re.findall(r"([0-9]+,[0-9][0-9])", s)
return list(map(lambda s: float(s.replace(",", ".")), amounts))
# try to extract gross amount and vat from an invoice
def extract_amount_and_vat(lines, vat_rates):
amounts = extract_amounts(" ".join(lines))
if not amounts:
return (None, None)
amount = max(amounts)
vat_factors = [vr / 100.0 for vr in vat_rates]
for vat_factor in vat_factors:
vat = round(amount / (1 + vat_factor) * vat_factor, 2)
if vat in amounts:
return (amount, vat)
vat_lines = [l for l in lines if "mwst" in l.lower()]
for line in vat_lines:
v_amounts = extract_amounts(line)
for vat in v_amounts:
for vat_factor in vat_factors:
for amount in amounts:
if vat == round(amount / (1 + vat_factor) * vat_factor, 2):
return (amount, vat)
return (max(amounts), 0)
def extract_date(lines):
for line in lines:
for word in line.split():
d = utils.convert_date4(word)
if d:
return d
return None
def extract_no(lines):
nos = []
for line in lines:
lline = line.lower()
if ("Seite" in line and "von" in line) or "Verwendungszweck" in line \
or "in Rechnung" in line:
continue
if "rechnungsnr" in lline \
or "rechnungs-Nr" in lline \
or "rechnungsnummer" in lline \
or ("rechnung" in lline and "nr" in lline) \
or ("rechnung " in lline) \
or ("rechnung:" in lline) \
or ("deine rechnung" in lline) \
or ("belegnummer" in lline):
for pattern in ["[nN][rR]", "[rR]e.*nummer", "Rechnung",
"Belegnummer / Document Number",
"Belegnummer"]:
s1 = re.search(pattern + "[:.– ]*([A-Za-z0-9/_–-]+([0-9/_–-]| (?! ))+)", line)
if s1 and s1.group(1):
# print("line:",line)
# print("s1:",s1.group(1))
nos.append(s1.group(1))
continue
if "EXP-" in line: # ERPNext invoices
s = re.search(r"EXP-[0-9][0-9]-[0-9][0-9]-[0-9]+", line)
if s and s.group(0):
nos.append(s.group(0))
continue
if not nos:
return None
nos.sort(key=lambda s: len(s), reverse=True)
return nos[0].strip()
def extract_supplier(lines):
return " ".join(lines[0][0:80].split())
def decode_uft_8(bline):
try:
return bline.decode('utf_8')
except Exception:
return ""
def pdf_to_text(file, raw=False):
cmd = ["pdftotext", "-nopgbrk"]
if raw:
cmd.append("-raw")
else:
cmd.append("-table")
cmd += ["-enc", "UTF-8"]
cmd += [file, "-"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
return [decode_uft_8(bline) for bline in p.stdout]
def ask_if_to_continue(err, msg=""):
if err:
title = "Warnung"
return easygui.ccbox(err + msg, title) # show a Continue/Cancel dialog
return True
class PurchaseInvoice(Invoice):
suppliers = {}
def extract_order_id(self, str, line):
if str in line:
try:
lsplit = line.split()
i = lsplit.index(str.split()[-1])
self.order_id = lsplit[i + 1]
except:
pass
def complete_data_by_gui(self, account=None, paid_by_submitter=False, amount=""):
"""
This method is used to change some information about the purchase invoice object by GUI.
"""
suppliers = gui_api_wrapper(Api.api.get_list, "Supplier", limit_page_length=LIMIT)
supplier_names = [supp['name'] for supp in suppliers]
supplier_names.sort()
supplier_names += ['neu']
def_supp = self.supplier if self.supplier in supplier_names else "neu"
def_new_supp = "" if self.supplier in supplier_names else self.supplier
if not amount:
amount = self.totals[self.default_vat]+self.vat[self.default_vat]
layout = [
[sg.Text('Lieferant')],
[sg.OptionMenu(values=supplier_names, k='-supplier-',
default_value=def_supp)],
[sg.Text('ggf. neuer Lieferant')],
[sg.Input(default_text=def_new_supp,
k='-supplier-name-')],
[sg.Text('Rechnungsnr.')],
[sg.Input(default_text=self.no, k='-no-')],
[sg.Text('Datum')],
[sg.Input(key='-date-',
default_text=utils.show_date4(self.date)),
sg.CalendarButton('Kalender', target='-date-',
format='%d.%m.%Y',
begin_at_sunday_plus=1)],
[sg.Text('MWSt')],
[sg.Input(default_text=str(self.vat[self.default_vat]).replace(".",","),
k='-vat-')],
[sg.Text('Brutto')],
[sg.Input(default_text=str(amount).replace(".",","), k='-gross-')],
[sg.Text('Skonto')],
[sg.Input(k='-skonto-')],
[sg.Checkbox('Schon selbst bezahlt',
default=paid_by_submitter, k='-paid-')],
[sg.Text('Kommentar')],
[sg.Input(k='-remarks-')],
[sg.Button('Speichern')]
]
window1 = sg.Window("Einkaufsrechnung", layout, finalize=True)
window1.bring_to_front()
event, values = window1.read()
window1.close()
if values:
if '-supplier-' in values:
self.supplier = values['-supplier-']
if self.supplier == 'neu' and '-supplier-name-' in values:
self.supplier = values['-supplier-name-']
if '-no-' in values:
self.no = values['-no-']
if '-date-' in values:
date = utils.convert_date4(values['-date-'])
if date:
self.date = date
if '-vat-' in values:
self.vat[self.default_vat] = utils.read_float(values['-vat-'])
if '-gross-' in values:
self.totals[self.default_vat] = utils.read_float(values['-gross-']) - self.vat[self.default_vat]
if '-skonto-' in values and values['-skonto-']:
self.skonto = utils.read_float(values['-skonto-'])
if '-paid-' in values and values['-paid-']:
self.paid_by_submitter = True
if '-remarks-' in values:
self.remarks = values['-remarks-']
else:
return False
self.compute_total()
if not account:
pinvs = self.company.purchase_invoices[self.supplier]
paccs = [pi['expense_account'] for pi in pinvs if 'expense_account' in pi]
paccs = list(set(paccs))
accounts = self.company.leaf_accounts_for_credit
account_names = [acc['name'] for acc in accounts]
for acc in paccs:
try:
account_names.remove(acc)
except Exception:
pass
account_names = paccs + account_names
title = 'Buchungskonto wählen'
msg = 'Bitte ein Buchungskonto wählen\n'
account = easygui.choicebox(msg, title, account_names)
if not account:
return False
if not self.update_stock:
self.assign_default_e_items({self.default_vat: account})
return True
def parse_generic(self, lines, account=None, paid_by_submitter=False, is_test=False):
self.parser = "generic"
self.extract_items = False
amount = ""
self.vat[self.default_vat] = ""
self.totals[self.default_vat] = ""
self.shipping = 0.0
self.no = ""
if not self.supplier:
self.supplier = ""
if lines:
(amount, vat) = extract_amount_and_vat(lines, self.vat_rates)
self.date = extract_date(lines)
self.no = extract_no(lines)
if not self.supplier:
self.supplier = extract_supplier(lines)
if amount:
self.vat[self.default_vat] = vat
self.totals[self.default_vat] = amount - self.vat[self.default_vat]
self.shipping = 0.0
if not is_test:
if self.check_if_present():
return self
if is_test:
return self
if self.complete_data_by_gui(account, paid_by_submitter, amount):
return self
def complete_missing_data(self, account=None, paid_by_submitter=False, is_test=False, check_dup=True):
"""
This method is used to complete the missing information of the purchase invoice object
"""
if account and not self.update_stock:
self.assign_default_e_items({self.default_vat: account})
if not check_dup:
if not self.supplier:
self.supplier = "???"
if not self.date:
self.date = "1970-01-01"
if not self.no:
self.no = "???"
if not self.vat[self.default_vat]:
self.vat[self.default_vat] = 0.0
if not self.gross_total:
self.gross_total = 0.0
if not check_dup or (self.supplier and self.date and self.no and self.totals[self.default_vat]
and self.vat[self.default_vat] and self.gross_total):
self.compute_total()
return self
if is_test or self.check_if_present(check_dup):
self.compute_total()
return self
if not self.supplier:
print("Lieferant nicht erkannt")
if not self.date:
print("Datum nicht erkannt")
if not self.no:
print("Rechnungsnr. nicht erkannt")
if not self.vat[self.default_vat]:
print("MWSt nicht erkannt")
if not self.totals[self.default_vat]:
print("Nettobetrag nicht erkannt")
if not self.gross_total:
print("Bruttobetrag nicht erkannt")
print("Rückfall auf manuelle Eingabe")
if self.complete_data_by_gui(account, paid_by_submitter):
return self
def apply_info_changes(self, diff, new_data_model):
"""
Whenever we want to change the information obtained from parsers' purchase data,
it is necessary to call this method so that the changes are also applied to the purchase invoice object.
"""
for key in diff.keys():
value = diff[key]
if key == insert:
for new_key in value.keys():
if new_key == 'supplier':
self.supplier = value[new_key]
elif new_key == 'taxes':
for tax_info in value[new_key]:
self.vat[tax_info['rate']] = tax_info['tax_amount']
self.total_vat += tax_info['tax_amount']
if self.total_vat == 0 and self.default_vat:
self.vat[self.default_vat] = 0
elif new_key == 'items':
for item in value[new_key]:
s_item = SupplierItem(self)
s_item.description = item.get('description')
s_item.qty = item.get('qty')
s_item.qty_unit = item.get('uom')
s_item.rate = item.get('rate')
s_item.amount = item.get('amount')
self.items.append(s_item)
elif new_key == 'total':
self.totals[self.default_vat] = value[new_key]
elif new_key == 'grand_total':
self.gross_total = value[new_key]
elif new_key == 'bill_no':
self.bill_no = value[new_key]
elif new_key == 'order_id':
self.order_id = value[new_key]
elif new_key == 'posting_date':
self.date = value[new_key]
elif new_key == 'shipping':
self.shipping = value[new_key]
elif key == delete:
for deleted_key in value.keys():
if deleted_key == 'supplier':
self.supplier = None
elif deleted_key == 'taxes':
self.vat[self.default_vat] = 0
self.total_vat = 0
elif deleted_key == 'items':
self.items = []
if new_data_model and new_data_model.get('items'):
for item in new_data_model.get('items'):
s_item = SupplierItem(self)
s_item.description = item.get('description')
s_item.qty = item.get('qty')
s_item.qty_unit = item.get('uom')
s_item.rate = item.get('rate')
s_item.amount = item.get('amount')
self.items.append(s_item)
elif deleted_key == 'total':
self.totals[self.default_vat] = 0
elif deleted_key == 'grand_total':
self.gross_total = 0
elif deleted_key == 'bill_no':
self.bill_no = None
elif deleted_key == 'order_id':
self.order_id = None
elif deleted_key == 'posting_date':
self.date = None
elif deleted_key == 'shipping':
self.shipping = 0
else:
if key == 'supplier':
self.supplier = value[1]
elif key == 'taxes':
self.total_vat = 0
if type(value) is list:
for tax_info in value[1]:
self.vat[tax_info['rate']] = tax_info['tax_amount']
self.total_vat += tax_info['tax_amount']
elif new_data_model:
for tax_info in new_data_model['taxes']:
self.vat[tax_info['rate']] = tax_info['tax_amount']
self.total_vat += tax_info['tax_amount']
if self.total_vat == 0 and self.default_vat:
self.vat[self.default_vat] = 0
elif key == 'items':
self.items = []
if new_data_model and new_data_model.get('items'):
for item in new_data_model.get('items'):
s_item = SupplierItem(self)
s_item.description = item.get('description')
s_item.qty = item.get('qty')
s_item.qty_unit = item.get('uom')
s_item.rate = item.get('rate')
s_item.amount = item.get('amount')
self.items.append(s_item)
elif key == 'total':
self.totals[self.default_vat] = value[1]
elif key == 'grand_total':
self.gross_total = value[1]
elif key == 'bill_no':
self.bill_no = value[1]
elif key == 'order_id':
self.order_id = value[1]
elif key == 'posting_date':
self.date = value[1]
elif key == 'shipping':
self.shipping = value[1]
def edit_data_model_manually(self, data_model, infile):
"""
This method is used to manually change the information obtained from the parsers' purchase data.
"""
diff = None
new_data_model = None
if utils.running_linux():
os.system("evince " + infile + " &")
def store_json(json_data: dict):
nonlocal diff, new_data_model
new_data_model = json_data
diff = jsondiff.diff(data_model, json_data, syntax='symmetric')
jsoneditor.editjson(data_model, callback=store_json)
if diff:
self.apply_info_changes(diff, new_data_model)
return new_data_model
def merge_items(self, items1, items2):
if not items1:
return items2
dict1 = {item.get("item_code", 0): item for item in items1}
dict2 = {item.get("item_code", 0): item for item in items2}
for item_code, item in dict1.items():
if dict2.get(item_code):
description = dict2.get(item_code).get('description')
if description:
item['description'] = description
for item_code, item in dict2.items():
if not dict1.get(item_code):
dict1[item_code] = item
merged_list = []
all_items = [item for item in dict1.values() if item.get('item_code')]
for item_code, item in dict1.items():
if item_code == 0:
for data in all_items:
if item.get('description') and data.get('description') and item.get('description') in data.get('description'):
merged_list.append(data)
continue
merged_list.append(item)
print("Merged list of items .......", merged_list)
return merged_list
def parse_invoice(self, invoice_json, infile, account_abbrv=None, paid_by_submitter=False, given_supplier=None,
is_test=False, check_dup=True, manual_edit=False):
account = None
if account_abbrv:
accounts = self.company.leaf_accounts_for_credit
account_names = [acc['name'] for acc in accounts]
for acc in account_names:
if account_abbrv in acc:
account = acc
normal_purchase_data = None
google_purchase_data = None
final_data = None
if invoice_json:
print("Nutze Google invoice parser")
purchase_invoice_google_parser = PurchaseInvoiceGoogleParser(self, invoice_json, given_supplier, is_test)
purchase_invoice_google_parser.set_purchase_info()
google_purchase_data = purchase_invoice_google_parser.get_purchase_data()
print("Nutze internen Parser")
self.extract_items = self.update_stock
lines = pdf_to_text(infile)
try:
if lines:
head = lines[0][0:140]
if not head[0:10].split():
for line in lines[0:10]:
if self.company.name != 'Bremer SolidarStrom' and len(line) > 2 and (
line[-2] == '£' or line[-3] == '£'):
head = "Kornkraft Naturkost GmbH"
break
supps = dict(PurchaseInvoice.suppliers)
if self.company.name != 'Laden':
del supps['Rechnung']
for supplier, info in supps.items():
if supplier in head:
self.parser = supplier
self.extract_items = self.update_stock
if info['raw']:
self.raw = True
lines = pdf_to_text(infile, True)
if 'supplier' in info:
self.supplier = info['supplier']
else:
self.supplier = supplier
print("Verwende Rechnungsparser für ", self.supplier)
if given_supplier and self.supplier != given_supplier:
print("abweichend von PreRechnung: ", given_supplier)
self.multi = info['multi']
purchase_invoice_parser = PurchaseInvoiceParser(self, info['parser'], lines)
purchase_invoice_parser.set_purchase_info()
normal_purchase_data = purchase_invoice_parser.get_purchase_data()
except Exception as e:
if google_purchase_data:
print(e)
elif self.update_stock:
raise e
elif not is_test:
print(e)
print("Rückfall auf Standard-Rechnungsbehandlung")
print("Google parser data ...", google_purchase_data)
print("Internal parser data ...", normal_purchase_data)
if google_purchase_data:
if normal_purchase_data:
if google_purchase_data.get('items'):
normal_purchase_data['items'] = self.merge_items(normal_purchase_data.get('items'), google_purchase_data.get('items'))
google_purchase_data.update(normal_purchase_data)
diff = jsondiff.diff(normal_purchase_data, google_purchase_data, syntax='symmetric')
self.apply_info_changes(diff, normal_purchase_data)
final_data = google_purchase_data
elif normal_purchase_data:
final_data = normal_purchase_data
if final_data:
if manual_edit:
final_data = self.edit_data_model_manually(google_purchase_data, infile)
print("Final Data ...")
print(final_data)
return self.complete_missing_data(account, paid_by_submitter, is_test, check_dup)
print("Verwende generischen Rechnungsparser")
self.supplier = given_supplier
return self.parse_generic(lines, account, paid_by_submitter, is_test)
def compute_total(self):
self.total = sum([t for v, t in self.totals.items()])
self.total_vat = sum([t for v, t in self.vat.items()])
self.gross_total = round(self.total + self.total_vat, 2)
for vat in self.vat_rates:
if (round(self.totals[vat] * vat / 100.0 + 0.00001, 2) - self.vat[vat]):
print(self.no, " Abweichung bei MWSt! ",
vat, "% von", self.totals[vat], " = ",
round(self.totals[vat] * vat / 100.0 + 0.00001, 2),
". MWSt auf der Rechnung: ",
self.vat[vat])
def assign_default_e_items(self, accounts):
self.e_items = []
for vat in self.vat_rates:
if vat in accounts.keys() and self.totals[vat]:
self.e_items.append(
{'item_code': settings.DEFAULT_ITEM_CODE,
'qty': 1 if self.totals[vat] >= 0 else -1,
'rate': abs(self.totals[vat]),
'expense_account' : accounts[vat],
'cost_center': self.company.cost_center}
)
self.update_stock = False
# if not self.update_stock and self.vat_rates:
# self.e_items[0]['expense_account'] = accounts[self.vat_rates[0]]
def create_taxes(self):
self.taxes = []
for vat, account in self.company.taxes.items():
if self.vat[vat]:
self.taxes.append({'add_deduct_tax': 'Add',
'charge_type': 'Actual',
'account_head': account,
'cost_center': self.company.cost_center,
'description': VAT_DESCRIPTION,
'tax_amount': self.vat[vat]})
def create_doc(self):
self.doc = {
'doctype': 'Purchase Invoice',
'company': self.company.name,
'supplier': self.supplier,
'title': self.supplier.split()[0] + " " + self.no,
'project': self.project,
'bill_no': self.no,
'order_id': self.order_id,
'posting_date': self.date,
'remarks': self.remarks,
'paid_by_submitter': self.paid_by_submitter,
'set_posting_time': 1,
'credit_to': self.company.payable_account,
'naming_series': STANDARD_NAMING_SERIES_PINV,
'buying_price_list': STANDARD_PRICE_LIST,
'taxes': self.taxes,
'items': self.e_items,
'update_stock': 1 if self.update_stock else 0,
'cost_center': self.company.cost_center,
'is_return' : self.total < 0
}
if self.skonto:
self.doc['apply_discount_on'] = 'Grand Total'
self.doc['discount_amount'] = self.skonto
if self.shipping:
self.doc['taxes'].append({'add_deduct_tax': 'Add',
'charge_type': 'Actual',
'account_head': DELIVERY_COST_ACCOUNT,
'description': DELIVERY_COST_DESCRIPTION,
'tax_amount': self.shipping})
def check_total(self, check_dup=True):
err = ""
computed_total = self.shipping + sum([item.rate * item.qty for item in self.items])
if check_dup and abs(self.total - computed_total) > 0.005:
err = "Abweichung! Summe in Rechnung: {0}, Summe der Posten: {1}".format(self.total, computed_total)
err += "\nDies kann noch durch Preisanpassungen korrigiert werden.\n"
return err
def check_duplicates(self):
err = ""
items = defaultdict(list)
for item in self.e_items:
items[item['item_code']].append(item) # group items by item_code
for key in items.keys():
if not key in AGGREGATE_ITEMS.values() and len(items[key]) > 1: # if there is more than one item in a group
err += "Ein Artikel ist mehrfach in der Rechnung vorhanden:\n"
err += "\n".join(map(str, items[key]))
err += "\nVielleicht ist die Zuordnung falsch und dies sollten zwei verschiedene Artikel sein?"
if err:
err += "\n\nTrotzdem Rechnung erstellen?"
return err
def check_if_present(self, check_dup=True):
if not check_dup or not self.no or not self.no.strip():
return False
upload = None
invs = gui_api_wrapper(Api.api.get_list, "Purchase Invoice",
filters={'bill_no': self.no, 'status': ['!=', 'Cancelled']})
if not invs and self.order_id:
invs1 = gui_api_wrapper(Api.api.get_list, "Purchase Invoice",
filters={'order_id': self.order_id, 'status': ['!=', 'Cancelled']})
if invs1:
easygui.msgbox(
"Einkaufsrechnung {} ist möglicherweise schon als {} in ERPNext eingetragen worden. Möglicherweise ist der Auftrag aber auch in mehrere Rechnungen gesplittet worden.".format(
self.no, invs1[0]['name']))
if invs:
# attach the present PDF to invoice with same bill_no
upload = self.upload_pdfs(invs[0]['name'])
if upload:
upload = "Aktuelle Rechnung wurde dort angefügt."
easygui.msgbox("Einkaufsrechnung {} ist schon als {} in ERPNext eingetragen worden. {}".format(self.no,
invs[0][
'name'],
upload))
self.is_duplicate = True
self.doc = invs[0]
return True
return False
def __init__(self, update_stock=False):
# do not call super().__init__ here,
# because there is no doc in ERPNext yet
self.update_stock = update_stock
self.order_id = None
self.date = None
self.company_name = sg.UserSettings()['-company-']
# print("Company: ",self.company_name)
self.company = company.Company.get_company(self.company_name)
# print("Company: ",self.company.name)
self.remarks = None
self.project = None
self.paid_by_submitter = False
self.total = 0
self.gross_total = 0
self.default_vat = self.company.default_vat
self.vat_rates = list(self.company.taxes.keys())
self.vat = {}
self.totals = {}
for vat in self.vat_rates:
self.vat[vat] = 0.0
self.totals[vat] = 0.0
self.multi = False
self.infiles = []
self.is_duplicate = False
self.e_items = []
self.raw = False
self.skonto = 0
self.parser = None
# for testing
@classmethod
def parse_and_dump(cls, infile, update_stock, account_abbrv=None, paid_by_submitter=False):
inv = PurchaseInvoice(update_stock).parse_invoice(infile, account_abbrv, paid_by_submitter)
pprint(vars(inv))
pprint(list(map(lambda x: pprint(vars(x)), inv.items)))
@classmethod
def read_and_transfer(cls, invoice_json, infile, update_stock, account_abbrv=None, paid_by_submitter=False, project=None,
supplier=None, check_dup=True):
inv = PurchaseInvoice(update_stock).read_pdf(
invoice_json, infile, account_abbrv, paid_by_submitter, supplier, check_dup=check_dup)
if inv and inv.is_duplicate:
return inv
if inv and not inv.is_duplicate:
inv.project = project
inv = inv.send_to_erpnext(not check_dup)
if not inv:
print("Keine Einkaufsrechnung angelegt")
return inv
def read_pdf(self, invoice_json, infile, account_abbrv=None, paid_by_submitter=False, supplier=None, check_dup=True):
self.infiles = [infile]
if not self.parse_invoice(invoice_json, infile, account_abbrv, paid_by_submitter, supplier, check_dup=check_dup):
return None
print("Prüfe auf doppelte Rechung")
if self.check_if_present(check_dup):
return self
if self.extract_items:
Api.load_item_data()
print("Hole Lagerdaten")
yesterd = utils.yesterday(self.date)
self.e_items = [item.process_item(self.supplier, yesterd, check_dup) for item in self.items] # if item.description]
if None in self.e_items:
print(
"Nicht alle Artikel wurden eingetragen.\n Bitte Einkaufsrechnung in ERPNext weiter bearbeiten.")
self.e_items = [item for item in self.e_items if item]
if not ask_if_to_continue(self.check_total(check_dup), "Fortsetzen?"):
return None
if not ask_if_to_continue(self.check_duplicates()):
return None
elif not self.e_items:
self.assign_default_e_items(SOMIKO_ACCOUNTS)
self.create_taxes()
return self
def summary(self):
if not self.doc:
self.create_doc()
fields = [('Rechnungsnr.', 'bill_no'),
('Unternehmen', 'company'),
('Lieferant', 'supplier'),
('Datum', 'posting_date'),
('Bemerkungen', 'remarks'),
('schon bezahlt', 'paid_by_submitter'),
('Gegenkonto', 'credit_to'),
('Lagerhaltung', 'update_stock')]
lines = ["{}: {}".format(d, self.doc[f]) for (d, f) in fields]
lines.append('Artikel:')
total = 0.0
for item in self.doc['items']:
amount = item['qty'] * item['rate']
total += amount
if Api.items_by_code:
try:
item_name = Api.items_by_code[item['item_code']]['item_name']
except Exception:
item_name = ""
else:
item_name = ""
if 'expense_account' in item:
expense_account = item['expense_account']
else:
expense_account = ""
lines.append(" {}x {} {} à {:.2f}€ = {:.2f}€ auf {}".format(item['qty'],
item['item_code'],
item_name,
item['rate'],
amount,
expense_account))
lines.append('Steuern und Kosten:')
for tax in self.doc['taxes']:
total += tax['tax_amount']
lines.append(" {:.2f}€ auf {}".format(tax['tax_amount'],
tax['account_head']))
lines.append("Summe: {:.2f}€".format(total))
lines = [line[0:70] for line in lines]
return "\n".join(lines)
def upload_pdfs(self, inv_name=None):
print("Übertrage PDF der Rechnung")
if not inv_name:
inv_name = self.doc['name']
upload = None
for infile in self.infiles:
upload = gui_api_wrapper(Api.api.read_and_attach_file,
"Purchase Invoice", inv_name,
infile, True)
return upload
def send_to_erpnext(self, silent=False):
print("Stelle ERPNext-Rechnung zusammen")
self.create_doc()
Api.create_supplier(self.supplier)
# print(self.doc)
print("Übertrage ERPNext-Rechnung")
if not self.insert():
return None
# now we have a doc and can init class Invoice
super().__init__(self.doc, False)
# print(self.doc)
self.company.purchase_invoices[self.doc['supplier']].append(self.doc)
upload = self.upload_pdfs()
# currently, we can only link to the last PDF
self.doc['supplier_invoice'] = upload['file_url']
self.update()
# enter purchased material separately into stock, if needed
stock.purchase_invoice_into_stock(self.doc['name'])
# fallback on manual creation of invoice if necessary
if self.update_stock and self.parser == "generic":
easygui.msgbox(
"Einkaufsrechnung {0} wurde als Entwurf an ERPNext übertragen. Bitte Artikel in ERPNext manuell eintragen. Künftig könnte dies ggf. automatisiert werden.".format(
self.no))
return self
if silent:
return self
choices = ["Sofort buchen", "Später buchen"]
msg = "Einkaufsrechnung {0} wurde als Entwurf an ERPNext übertragen:\n{1}\n\n".format(self.doc['title'],
self.summary())
title = "Rechnung {}".format(self.no)
filters = {'company': self.company_name,
'party': self.supplier,
'docstatus': 1,
'unallocated_amount': self.gross_total}
py = gui_api_wrapper(Api.api.get_list,
"Payment Entry",
filters=filters,
limit_page_length=1)
if py:
py = doc.Doc(name=py[0]['name'], doctype='Payment Entry')
bt = None
else:
bt = bank.BankTransaction.find_bank_transaction(self.company_name,
-self.gross_total,
self.no)
if py:
msg += "\n\nZugehörige Anzahlung gefunden: {}\n". \
format(py.name)
choices[0] = "Sofort buchen und zahlen"
if bt:
msg += "\n\nZugehörige Bank-Transaktion gefunden: {}\n". \
format(bt.description)
choices[0] = "Sofort buchen und zahlen"
if easygui.buttonbox(msg, title, choices) in \
["Sofort buchen", "Sofort buchen und zahlen"]:
print("Buche Rechnung")
if py:
self.use_advance_payment(py)
self.submit()
if bt:
self.payment_from_bank_transaction(bt)
return self
heckert_info = {'parser': 'heckert',
'raw': False, 'multi': False,
'supplier': 'Heckert Solar GmbH'}
PurchaseInvoice.suppliers = {
'Krannich Solar GmbH & Co KG': {
'parser': 'krannich',
'raw': False, 'multi': False
},
'pvXchange Trading GmbH': {
'parser': 'pvxchange',
'raw': True, 'multi': False
},
'Schlußrechnung': heckert_info,
'Vorausrechnung': heckert_info,
'Teilrechnung': heckert_info,
'SOLARWATT': {
'parser': 'generic',
'raw': False, 'multi': False,
'supplier': 'Solarwatt GmbH'
},
'Seite': {
'parser': 'wagner',
'raw': False, 'multi': False,
'supplier': 'Wagner Solar'
},
'Rechnung': {
'parser': 'nkk',
'raw': False, 'multi': False,
'supplier': 'Naturkost Kontor Bremen Gmbh'
},
'Kornkraft Naturkost GmbH': {
'parser': 'kornkraft',
'supplier': 'Kornkraft Naturkost GmbH',
'raw': False, 'multi': True
},
'Verein für Soziales, Ökologie': {
'parser': 'kornkraft',
'supplier': 'Kornkraft Naturkost GmbH',
'raw': False, 'multi': True
}
}