forked from ohbobbyboy/bobby_boy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·90 lines (69 loc) · 2.71 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import argparse
import qr
import config
import report
from ofd import OFDProvider
from drebedengi import Drebedengi
# создаём необходимые директории если отсутствуют
def init():
if not os.path.exists(config.receipt_dir):
os.makedirs(config.receipt_dir)
if not os.path.exists(config.report_dir):
os.makedirs(config.report_dir)
# resend - разрешение повторно передавать данные по чеку, который уже сохранен
def recognize(resend, receipt_text):
ofd_receipt = OFDProvider(resend).detect(receipt_text)
if not type(ofd_receipt) is bool:
items = ofd_receipt.get_items()
if items:
return ofd_receipt
elif ofd_receipt:
kkt = raw_input("Enter `PH KKT`: ")
inn = raw_input("Enter `INN`: ")
ofd_receipt = OFDProvider(resend).detect(receipt_text, kkt, inn)
if not type(ofd_receipt) is bool:
items = ofd_receipt.get_items()
if items:
return ofd_receipt
return False
parser = argparse.ArgumentParser(description='Import receipts data from OFD to Drebedengi')
parser.add_argument('--text', help='take receipt data from string')
parser.add_argument('--noediting', action='store_false', help='disable manual report editing')
args = parser.parse_args()
init()
if args.text is not None:
# распознаём из введённого текста
receipt = recognize(config.already_recognized_send, args.text)
else:
receipt = recognize(config.already_recognized_send,
qr.get_content_with_gui())
if receipt is not None:
report_name = receipt.get_csv_file_name()
dreb_session = Drebedengi(config.username, config.password)
if not dreb_session.logged_in():
print("Auth is not successful!")
sys.exit(-1)
categories = dreb_session.get_categories()
sms_saved_receipt = dreb_session.search(receipt.dreb_time, receipt.raw_sum)
if sms_saved_receipt:
receipt.payment_method = sms_saved_receipt['payment_method']
report.make(receipt.items,
categories,
report_name,
receipt.dreb_time,
receipt.raw_sum,
receipt.total_sum,
receipt.payment_method)
if args.noediting:
report.edit(report_name)
raw_input("Press Enter to export report to Drebedengi...")
report.clear(report_name)
import_result = dreb_session.send_csv(report_name)
if import_result and sms_saved_receipt:
dreb_session.delete_item(sms_saved_receipt['id'])
else:
print("Receipt search failed!")