-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccounting.py
executable file
·5915 lines (5148 loc) · 361 KB
/
accounting.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 python3
# -*- coding: utf-8 -*-
# Add parent dir to path to fix imports from inside submodules
import os
import sys
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.insert(0, currentdir)
# Import common code
from sysadmws_common import *
from gsuite_scripts import *
import gitlab
import glob
import psycopg2
import textwrap
from datetime import datetime
from datetime import timedelta
from datetime import time
from dateutil.relativedelta import relativedelta
from num2words import num2words
import re
from zipfile import ZipFile
import subprocess
import woocommerce
import paramiko
# Constants and envs
LOGO="Accounting"
WORK_DIR = os.environ.get("ACC_WORKDIR", "/opt/sysadmws/accounting")
LOG_DIR = os.environ.get("ACC_LOGDIR", "/opt/sysadmws/accounting/log")
LOG_FILE = "accounting.log"
TARIFFS_SUBDIR = "tariffs"
CLIENTS_SUBDIR = "clients"
YAML_GLOB = "*.yaml"
YAML_EXT = "yaml"
DB_STRUCTURE_FILE = "accounting_db_structure.sql"
ACC_YAML = "accounting.yaml"
INVOICE_TYPES = ["Hourly", "Monthly", "Storage"]
# Functions
def calculate_range_size(range_id):
num = 0
for c in range_id.split(":")[0]:
if c in string.ascii_letters:
num = num * 26 + (ord(c.upper()) - ord('A')) + 1
left_column = num
num = 0
for c in range_id.split(":")[1]:
if c in string.ascii_letters:
num = num * 26 + (ord(c.upper()) - ord('A')) + 1
right_column = num
range_size = right_column - left_column + 1
return range_size
def download_pdf_package(acc_yaml_dict, client_dict, client_folder_files, invoice_type, invoice_number, invoice_needed, act_needed, pack_to_archive, double_act):
try:
if not invoice_needed and not act_needed:
raise LoadError("Invoice not needed and act not needed, do not know what to do")
found_invoice = False
found_details = False
found_act = False
pdf_list = []
# Construct name prefix we are searching
search_prefix_invoice = (
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["filename"][0] +
" " +
invoice_number +
" " +
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["filename"][1]
)
search_prefix_details = (
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["details"]["filename"][0] +
" " +
invoice_number +
" " +
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["details"]["filename"][1]
)
if act_needed:
search_prefix_act = (
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["act"]["filename"][0] +
" " +
invoice_number +
" " +
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["act"]["filename"][1]
)
for item in client_folder_files:
# Invoice
if invoice_needed and item["name"].startswith(search_prefix_invoice) and item["mimeType"] == "application/pdf":
# Remove local pdf
if os.path.exists(item["name"]):
os.remove(item["name"])
# Download pdf
try:
response = drive_download(SA_SECRETS_FILE, item["id"], item["name"], acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Doc drive_download response: {0}".format(response))
except:
raise
# Add pdf to archive list
pdf_list.append(item["name"])
# Set found
logger.info("Invoice PDF for invoice_number = {0} found: {1}".format(invoice_number, item["name"]))
found_invoice = True
# Details
if invoice_needed and item["name"].startswith(search_prefix_details) and item["mimeType"] == "application/pdf":
# Remove local pdf
if os.path.exists(item["name"]):
os.remove(item["name"])
# Download pdf
try:
response = drive_download(SA_SECRETS_FILE, item["id"], item["name"], acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Doc drive_download response: {0}".format(response))
except:
raise
# Add pdf to archive list
pdf_list.append(item["name"])
# Set found
logger.info("Details PDF for invoice_number = {0} found: {1}".format(invoice_number, item["name"]))
found_details = True
# Act
if act_needed and item["name"].startswith(search_prefix_act) and item["mimeType"] == "application/pdf":
# Remove local pdf
if os.path.exists(item["name"]):
os.remove(item["name"])
# Download pdf
try:
response = drive_download(SA_SECRETS_FILE, item["id"], item["name"], acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Doc drive_download response: {0}".format(response))
except:
raise
# Add pdf to archive list
pdf_list.append(item["name"])
# Add second act to list if needed
if double_act:
pdf_list.append(item["name"])
# Set found
logger.info("Act PDF for invoice_number = {0} found: {1}".format(invoice_number, item["name"]))
found_act = True
# Check if needed pdfs found
if invoice_needed and not found_invoice:
raise LoadError("Invoice PDF for invoice_number = {0} not found".format(invoice_number))
if invoice_needed and not found_details:
raise LoadError("Details PDF for invoice_number = {0} not found".format(invoice_number))
if act_needed and not found_act:
raise LoadError("Act PDF for invoice_number = {0} not found".format(invoice_number))
# Create zip archive if needed and return archive filename
if pack_to_archive:
if os.path.exists(invoice_number + '.zip'):
os.remove(invoice_number + '.zip')
with ZipFile(invoice_number + '.zip','w') as zip:
for file in pdf_list:
zip.write(file)
# Remove pdfs
for pdf_file in pdf_list:
if os.path.exists(pdf_file):
os.remove(pdf_file)
return [invoice_number + '.zip']
# Else return pdf filenames
else:
return pdf_list
except:
# Remove pdfs
for pdf_file in pdf_list:
if os.path.exists(pdf_file):
os.remove(pdf_file)
raise
# Main
if __name__ == "__main__":
# Set parser and parse args
parser = argparse.ArgumentParser(description='{LOGO} functions.'.format(LOGO=LOGO))
parser.add_argument("--debug", dest="debug", help="enable debug", action="store_true")
parser.add_argument("--no-exceptions-on-label-errors", dest="no_exceptions_on_label_errors", help="use with dry-runs to bulk check label errors", action="store_true")
parser.add_argument("--dry-run-db", dest="dry_run_db", help="do not commit to database", action="store_true")
parser.add_argument("--dry-run-gitlab", dest="dry_run_gitlab", help="no new objects created in gitlab", action="store_true")
parser.add_argument("--dry-run-gsuite", dest="dry_run_gsuite", help="no new objects created in gsuite", action="store_true")
parser.add_argument("--dry-run-print", dest="dry_run_print", help="no print commands executed", action="store_true")
parser.add_argument("--dry-run-woocommerce", dest="dry_run_woocommerce", help="no woocommerce api commands executed", action="store_true")
parser.add_argument("--timelogs-spent-before-date", dest="timelogs_spent_before_date", help="select unchecked timelogs for hourly invoices spent before date DATE", nargs=1, metavar=("DATE"))
parser.add_argument("--at-date", dest="at_date", help="use DATETIME instead of now for tariff", nargs=1, metavar=("DATETIME"))
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument("--exclude-clients", dest="exclude_clients", help="exclude clients defined by JSON_LIST from all-clients operations", nargs=1, metavar=("JSON_LIST"))
group.add_argument("--include-clients", dest="include_clients", help="include only clients defined by JSON_LIST for all-clients operations", nargs=1, metavar=("JSON_LIST"))
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--db-structure", dest="db_structure", help="create database structure", action="store_true")
group.add_argument("--yaml-check", dest="yaml_check", help="check yaml structure", action="store_true")
group.add_argument("--asset-labels", dest="asset_labels", help="sync asset labels", action="store_true")
group.add_argument("--issues-check", dest="issues_check", help="report issue activities as new issue in accounting project", action="store_true")
group.add_argument("--merge-requests-check", dest="merge_requests_check", help="report MR activities as new issue in accounting project", action="store_true")
group.add_argument("--storage-usage", dest="storage_usage", help="save all clients billable storage usage to database, excluding --exclude-clients or only for --include-clients", action="store_true")
group.add_argument("--report-hourly-employee-timelogs", dest="report_hourly_employee_timelogs", help="check new timelogs for EMPLOYEE_EMAIL and report them as new issue", nargs=1, metavar=("EMPLOYEE_EMAIL"))
group.add_argument("--update-envelopes-for-client", dest="update_envelopes_for_client", help="update envelope pdfs in envelopes folder for client CLIENT", nargs=1, metavar=("CLIENT"))
group.add_argument("--update-envelopes-for-all-clients", dest="update_envelopes_for_all_clients", help="update envelope pdfs in envelopes folder for all clients excluding --exclude-clients or only for --include-clients", action="store_true")
group.add_argument("--make-pdfs-for-client", dest="make_pdfs_for_client", help="make pdfs for client CLIENT folder for docs that do not have pdf copy yet", nargs=1, metavar=("CLIENT"))
group.add_argument("--make-pdfs-for-all-clients", dest="make_pdfs_for_all_clients", help="make pdfs for every client folder for docs that do not have pdf copy yet", action="store_true")
group.add_argument("--make-gmail-drafts-for-client", dest="make_gmail_drafts_for_client", help="make GMail drafts with PDFs of invoices with status Prepared/Sent for CLIENT", nargs=1, metavar=("CLIENT"))
group.add_argument("--make-gmail-drafts-for-all-clients", dest="make_gmail_drafts_for_all_clients", help="make GMail drafts with PDFs of invoices with status Prepared/Sent for all clients", action="store_true")
group.add_argument("--print-papers-for-client", dest="print_papers_for_client", help="print invoice papers with status not Printed for CLIENT", nargs=1, metavar=("CLIENT"))
group.add_argument("--print-papers-for-all-clients", dest="print_papers_for_all_clients", help="print invoice papers with status not Printed for all clients", action="store_true")
group.add_argument("--make-hourly-invoice-for-client", dest="make_hourly_invoice_for_client", help="check new timelogs for hourly issues or MRs in projects of CLIENT and make invoice", nargs=1, metavar=("CLIENT"))
group.add_argument("--make-hourly-invoice-for-all-clients", dest="make_hourly_invoice_for_all_clients", help="check new timelogs for hourly issues or MRs in projects of all clients and make invoice for each", action="store_true")
group.add_argument("--make-monthly-invoice-for-client", dest="make_monthly_invoice_for_client", help="make monthly invoice for month +MONTH by current month for client CLIENT", nargs=2, metavar=("CLIENT", "MONTH"))
group.add_argument("--make-monthly-invoice-for-all-clients", dest="make_monthly_invoice_for_all_clients", help="make monthly invoice for month +MONTH by current month for all clients excluding --exclude-clients or only for --include-clients", nargs=1, metavar=("MONTH"))
group.add_argument("--make-storage-invoice-for-client", dest="make_storage_invoice_for_client", help="compute monthly storage usage for month -MONTH of CLIENT and make invoice", nargs=2, metavar=("CLIENT", "MONTH"))
group.add_argument("--make-storage-invoice-for-all-clients", dest="make_storage_invoice_for_all_clients", help="compute monthly storage usage for month -MONTH of all clients and make invoice for each", nargs=1, metavar=("MONTH"))
group.add_argument("--list-assets-for-client", dest="list_assets_for_client", help="list assets for CLIENT", nargs=1, metavar=("CLIENT"))
group.add_argument("--list-assets-for-all-clients", dest="list_assets_for_all_clients", help="list assets for all clients", action="store_true")
group.add_argument("--count-assets", dest="count_assets", help="add a new record to the asset_count table", action="store_true")
group.add_argument("--count-timelog-stats", dest="count_timelog_stats", help="add a new record to the timelogs_stats table", action="store_true")
if len(sys.argv) > 1:
args = parser.parse_args()
else:
parser.print_help()
sys.exit(1)
# Set logger and console debug
if args.debug:
logger = set_logger(logging.DEBUG, LOG_DIR, LOG_FILE)
else:
logger = set_logger(logging.ERROR, LOG_DIR, LOG_FILE)
# Skip vars check where not needed
if not (args.yaml_check or args.list_assets_for_client is not None or args.list_assets_for_all_clients):
PG_DB_HOST = os.environ.get("PG_DB_HOST")
if PG_DB_HOST is None:
raise Exception("Env var PG_DB_HOST missing")
PG_DB_NAME = os.environ.get("PG_DB_NAME")
if PG_DB_NAME is None:
raise Exception("Env var PG_DB_NAME missing")
PG_DB_USER = os.environ.get("PG_DB_USER")
if PG_DB_USER is None:
raise Exception("Env var PG_DB_USER missing")
PG_DB_PASS = os.environ.get("PG_DB_PASS")
if PG_DB_PASS is None:
raise Exception("Env var PG_DB_PASS missing")
if not (args.yaml_check or args.list_assets_for_client is not None or args.list_assets_for_all_clients or args.db_structure):
GL_ADMIN_PRIVATE_TOKEN = os.environ.get("GL_ADMIN_PRIVATE_TOKEN")
if GL_ADMIN_PRIVATE_TOKEN is None:
raise Exception("Env var GL_ADMIN_PRIVATE_TOKEN missing")
GL_BOT_PRIVATE_TOKEN = os.environ.get("GL_BOT_PRIVATE_TOKEN")
if GL_BOT_PRIVATE_TOKEN is None:
raise Exception("Env var GL_BOT_PRIVATE_TOKEN missing")
GL_PG_DB_HOST = os.environ.get("GL_PG_DB_HOST")
if GL_PG_DB_HOST is None:
raise Exception("Env var GL_PG_DB_HOST missing")
GL_PG_DB_NAME = os.environ.get("GL_PG_DB_NAME")
if GL_PG_DB_NAME is None:
raise Exception("Env var GL_PG_DB_NAME missing")
GL_PG_DB_USER = os.environ.get("GL_PG_DB_USER")
if GL_PG_DB_USER is None:
raise Exception("Env var GL_PG_DB_USER missing")
GL_PG_DB_PASS = os.environ.get("GL_PG_DB_PASS")
if GL_PG_DB_PASS is None:
raise Exception("Env var GL_PG_DB_PASS missing")
SA_SECRETS_FILE = os.environ.get("SA_SECRETS_FILE")
if SA_SECRETS_FILE is None:
raise Exception("Env var SA_SECRETS_FILE missing")
SSH_DU_S_M_KEYFILE = os.environ.get("SSH_DU_S_M_KEYFILE")
if SSH_DU_S_M_KEYFILE is None:
raise Exception("Env var SSH_DU_S_M_KEYFILE missing")
SSH_DU_S_M_USER = os.environ.get("SSH_DU_S_M_USER")
if SSH_DU_S_M_USER is None:
raise Exception("Env var SSH_DU_S_M_USER missing")
# Catch exception to logger
try:
logger.info("Starting {LOGO}".format(LOGO=LOGO))
# Chdir to work dir
os.chdir(WORK_DIR)
# Skip pgconnect where not needed
if not (args.yaml_check or args.list_assets_for_client is not None or args.list_assets_for_all_clients):
# Connect to PG
dsn = "host={} dbname={} user={} password={}".format(PG_DB_HOST, PG_DB_NAME, PG_DB_USER, PG_DB_PASS)
conn = psycopg2.connect(dsn)
# Read ACC_YAML
acc_yaml_dict = load_yaml("{0}/{1}".format(WORK_DIR, ACC_YAML), logger)
if acc_yaml_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, ACC_YAML))
# Do tasks
# If --at-date is set use it instead of today and now
if args.at_date is not None:
used_today = datetime.strptime(args.at_date[0], "%Y-%m-%d")
used_now = datetime.strptime(args.at_date[0], "%Y-%m-%d")
else:
used_today = datetime.today()
used_now = datetime.now()
if args.exclude_clients is not None:
json_str, = args.exclude_clients
exclude_clients_list = json.loads(json_str)
else:
exclude_clients_list = []
if args.include_clients is not None:
json_str, = args.include_clients
include_clients_list = json.loads(json_str)
else:
include_clients_list = []
if args.db_structure:
# New cursor
cur = conn.cursor()
# Queries
sql = load_file_string("{0}/{1}".format(WORK_DIR, DB_STRUCTURE_FILE), logger)
try:
cur.execute(sql)
logger.info("Query execution status:")
logger.info(cur.statusmessage)
conn.commit()
except Exception as e:
raise Exception("Caught exception on query execution")
# Close cursor
cur.close()
if args.storage_usage:
errors = False
# For *.yaml in client dir
for client_file in sorted(glob.glob("{0}/{1}".format(CLIENTS_SUBDIR, YAML_GLOB))):
logger.info("Found client file: {0}".format(client_file))
# Load client YAML
client_dict = load_client_yaml(WORK_DIR, client_file, CLIENTS_SUBDIR, YAML_GLOB, logger)
if client_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, client_file))
# Check if client is active
if client_dict["active"] and (
(
args.exclude_clients is not None
and
client_dict["name"].lower() not in exclude_clients_list
)
or
(
args.include_clients is not None
and
client_dict["name"].lower() in include_clients_list
)
or
(
args.exclude_clients is None
and
args.include_clients is None
)
):
asset_list = get_asset_list(client_dict, WORK_DIR, TARIFFS_SUBDIR, logger, used_now)
# If there are assets
if len(asset_list) > 0:
# Iterate over assets in client
for asset in asset_list:
if asset["active"]:
logger.info("Active asset: {0}".format(asset["fqdn"]))
if "storage" in asset:
for storage_item in asset["storage"]:
for storage_asset, storage_paths in storage_item.items():
for storage_path in storage_paths:
# Compute path usage
# As we have ssh cmd restrictions we need only to supply path as command
cmd = "{folder}".format(folder=storage_path)
logger.info("SSH cmd: {storage_asset}:{cmd}".format(storage_asset=storage_asset, cmd=cmd))
# Clear value
mb_used = None
try:
private_key = paramiko.Ed25519Key.from_private_key_file(SSH_DU_S_M_KEYFILE)
ssh_client = paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=storage_asset, username=SSH_DU_S_M_USER, pkey=private_key)
stdin, stdout, stderr = ssh_client.exec_command(cmd)
if stdout.channel.recv_exit_status() != 0:
logger.error("SSH exit code is not 0")
logger.error("SSH stderr:")
for line in iter(stderr.readline, ""):
logger.error(line)
ssh_client.close()
errors = True
else:
logger.info("SSH value received via stdout:")
mb_used = int("".join(stdout.readlines()))
logger.info(mb_used)
# Save usage to db
# New cursor
cur = conn.cursor()
# Queries
sql = """
INSERT INTO
storage_usage
(
checked_at
, client_asset_fqdn
, storage_asset_fqdn
, storage_asset_path
, mb_used
)
VALUES
(
NOW() AT TIME ZONE 'UTC'
, '{client_asset_fqdn}'
, '{storage_asset_fqdn}'
, '{storage_asset_path}'
, {mb_used}
)
;
""".format(client_asset_fqdn=asset["fqdn"], storage_asset_fqdn=storage_asset, storage_asset_path=storage_path, mb_used=mb_used)
logger.info("Query:")
logger.info(sql)
try:
cur.execute(sql)
logger.info("Query execution status:")
logger.info(cur.statusmessage)
conn.commit()
except Exception as e:
raise Exception("Caught exception on query execution")
# Close cursor
cur.close()
ssh_client.close()
except Exception as e:
logger.error("Caught exception on SSH execution")
logger.exception(e)
errors = True
# Exit with error if there were errors
if errors:
raise Exception("There were errors within SSH execution")
if args.update_envelopes_for_all_clients or args.update_envelopes_for_client is not None:
# List all files in envelopes folder
try:
envelopes_folder_files = drive_ls(SA_SECRETS_FILE, acc_yaml_dict["envelopes"], acc_yaml_dict["gsuite"]["drive_user"])
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# For *.yaml in client dir
for client_file in sorted(glob.glob("{0}/{1}".format(CLIENTS_SUBDIR, YAML_GLOB))):
logger.info("Found client file: {0}".format(client_file))
# Load client YAML
client_dict = load_client_yaml(WORK_DIR, client_file, CLIENTS_SUBDIR, YAML_GLOB, logger)
if client_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, client_file))
# Check specific client
if args.update_envelopes_for_client is not None:
client, = args.update_envelopes_for_client
if client_dict["name"].lower() != client:
continue
# If client yaml has envelope address and not excluded
if (
"papers" in client_dict
and
(
(
args.exclude_clients is not None
and
client_dict["name"].lower() not in exclude_clients_list
)
or
(
args.include_clients is not None
and
client_dict["name"].lower() in include_clients_list
)
or
(
args.exclude_clients is None
and
args.include_clients is None
)
)
and
(
(
"envelope_address" in client_dict["billing"]["papers"] and "envelope" in acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]
)
or
(
"envelope_address_no_recipient" in client_dict["billing"]["papers"] and "envelope_no_recipient" in acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]
)
)
):
# Envelope file name client - merchant - template
envelope_file_name = client_dict["name"] + " - " + client_dict["billing"]["merchant"] + " - " + client_dict["billing"]["template"]
# Remove file in envelopes folder
if not args.dry_run_gsuite:
for item in envelopes_folder_files:
if item["name"] == envelope_file_name:
try:
response = drive_rm(SA_SECRETS_FILE, item["id"], acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Envelope drive_rm response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Copy envelope from template
if not args.dry_run_gsuite:
try:
client_doc_envelope = drive_cp(SA_SECRETS_FILE,
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]["envelope"] if "envelope_address" in client_dict["billing"]["papers"] else acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]["envelope_no_recipient"],
acc_yaml_dict["envelopes"],
envelope_file_name, acc_yaml_dict["gsuite"]["drive_user"])
logger.info("New client envelope id: {0}".format(client_doc_envelope))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Templates
envelope_data = {
"__CONTRACT_RECIPIENT__": client_dict["billing"]["contract"]["recipient"],
"__ENVELOPE_ADDRESS__": client_dict["billing"]["papers"]["envelope_address"] if "envelope_address" in client_dict["billing"]["papers"] else client_dict["billing"]["papers"]["envelope_address_no_recipient"]
}
if not args.dry_run_gsuite:
try:
response = docs_replace_all_text(SA_SECRETS_FILE, client_doc_envelope, json.dumps(envelope_data))
logger.info("Envelope docs_replace_all_text response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# PDF
# Remove tmp file
if os.path.exists(envelope_file_name + ".pdf"):
os.remove(envelope_file_name + ".pdf")
# Download as pdf to tmp file
try:
response = drive_pdf(SA_SECRETS_FILE, client_doc_envelope, envelope_file_name + ".pdf", acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Envelope drive_pdf response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Upload pdf back to drive
if not args.dry_run_gsuite:
try:
response = drive_upload(SA_SECRETS_FILE, envelope_file_name + ".pdf", acc_yaml_dict["envelopes"], envelope_file_name + ".pdf", acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Envelope drive_upload response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Remove original
if not args.dry_run_gsuite:
try:
response = drive_rm(SA_SECRETS_FILE, client_doc_envelope, acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Envelope drive_rm response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Remove tmp file
if os.path.exists(envelope_file_name + ".pdf"):
os.remove(envelope_file_name + ".pdf")
if args.make_pdfs_for_all_clients or args.make_pdfs_for_client is not None:
# Init empty list of new pdfs
uploaded_pdfs = []
# For *.yaml in client dir
for client_file in sorted(glob.glob("{0}/{1}".format(CLIENTS_SUBDIR, YAML_GLOB))):
logger.info("Found client file: {0}".format(client_file))
# Load client YAML
client_dict = load_client_yaml(WORK_DIR, client_file, CLIENTS_SUBDIR, YAML_GLOB, logger)
if client_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, client_file))
# Check specific client
if args.make_pdfs_for_client is not None:
client, = args.make_pdfs_for_client
if client_dict["name"].lower() != client:
continue
# If client yaml has gsuite folder
if "gsuite" in client_dict and "folder" in client_dict["gsuite"]:
# List all files in client folder
try:
client_folder_files = drive_ls(SA_SECRETS_FILE, client_dict["gsuite"]["folder"], acc_yaml_dict["gsuite"]["drive_user"])
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# For every file that is not pdf and do not have pdf copy and has mimeType = application/vnd.google-apps.document
for item in client_folder_files:
if not re.match("^.*\.pdf$", item["name"]) and not any(subitem["name"] == item["name"] + ".pdf" for subitem in client_folder_files) and item["mimeType"] == "application/vnd.google-apps.document":
# Remove tmp file
if os.path.exists(item["name"] + ".pdf"):
os.remove(item["name"] + ".pdf")
# Download as pdf to tmp file
try:
response = drive_pdf(SA_SECRETS_FILE, item["id"], item["name"] + ".pdf", acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Doc drive_pdf response: {0}".format(response))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Upload pdf back to drive
if not args.dry_run_gsuite:
try:
response = drive_upload(SA_SECRETS_FILE, item["name"] + ".pdf", client_dict["gsuite"]["folder"], item["name"] + ".pdf", acc_yaml_dict["gsuite"]["drive_user"])
logger.info("Pdf drive_upload response: {0}".format(response))
if response is not None:
uploaded_pdfs.append(response)
else:
logger.error("Cannot upload document {0} to folder {1} - may be duplicate".format(item["name"] + ".pdf", client_dict["gsuite"]["folder"]))
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Remove tmp file
if os.path.exists(item["name"] + ".pdf"):
os.remove(item["name"] + ".pdf")
print("New PDFs to check:")
for item in uploaded_pdfs:
print("https://drive.google.com/file/d/" + item + "/view")
# Create issue to check new PDFs
# Prepare issue header
issue_text = textwrap.dedent("""
Please check new PDFs.
If markup errors found:
- Delete PDF
- Fix original Google Doc
- Run Make PDFs job again
- Check new issue with new PDFs again
- Continue with Invoices procedure
If data errors found:
- Delete PDF
- Delete original Google Doc
- Delete Invoices sheet line for the Client
- Delete DB records
- Run Invoice generation again manually for that Client
- Run Make PDFs job again
- Check new issue with new PDFs again
- Continue with Invoices procedure
New PDFs to check:\
""")
# Add report rows
for item in uploaded_pdfs:
issue_text = "{}\n- https://drive.google.com/file/d/{}/view".format(issue_text, item)
# Connect to GitLab as Bot
gl = gitlab.Gitlab(acc_yaml_dict["gitlab"]["url"], private_token=GL_BOT_PRIVATE_TOKEN)
gl.auth()
# Post report as an issue in GitLab
logger.info("Going to create new issue:")
logger.info("Title: New PDFs Check Report")
logger.info("Body:")
logger.info(issue_text)
project = gl.projects.get(acc_yaml_dict["accounting"]["project"])
if not args.dry_run_gitlab:
issue = project.issues.create({"title": "New PDFs Check Report", "description": issue_text})
# Add assignee
issue.assignee_ids = [acc_yaml_dict["accounting"]["manager_id"]]
issue.save()
if args.make_gmail_drafts_for_client is not None or args.make_gmail_drafts_for_all_clients or args.print_papers_for_client is not None or args.print_papers_for_all_clients:
# Get Invoices raw data
try:
invoices_raw_dict = sheets_get_as_json(SA_SECRETS_FILE, acc_yaml_dict["invoices"]["spreadsheet"], acc_yaml_dict["invoices"]["invoices"]["sheet"], acc_yaml_dict["invoices"]["invoices"]["range"], 'ROWS', 'FORMATTED_VALUE', 'FORMATTED_STRING')
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Prepare structured Invoices dict per client
invoices_dict = {}
for invoices_line in invoices_raw_dict:
invoices_order_dict = acc_yaml_dict["invoices"]["invoices"]["columns"]["order"]
invoices_line_client = invoices_line[invoices_order_dict['client'] - 1]
if not invoices_line_client in invoices_dict:
invoices_dict[invoices_line_client] = []
invoices_dict[invoices_line_client].append(
{
'date_created': invoices_line[invoices_order_dict['date_created'] - 1],
'type': invoices_line[invoices_order_dict['type'] - 1],
'period': invoices_line[invoices_order_dict['period'] - 1],
'merchant': invoices_line[invoices_order_dict['merchant'] - 1],
'ext_order_number': invoices_line[invoices_order_dict['ext_order_number'] - 1],
'invoice_number': invoices_line[invoices_order_dict['invoice_number'] - 1],
'invoice_currency': invoices_line[invoices_order_dict['invoice_currency'] - 1],
'invoice_sum': invoices_line[invoices_order_dict['invoice_sum'] - 1],
'status': invoices_line[invoices_order_dict['status'] - 1],
'sum_processed': invoices_line[invoices_order_dict['sum_processed'] - 1],
'sum_received': invoices_line[invoices_order_dict['sum_received'] - 1],
'papers': invoices_line[invoices_order_dict['papers'] - 1]
}
)
# Read all or specific clients
clients_dict = {}
if args.make_gmail_drafts_for_all_clients or args.print_papers_for_all_clients:
# For *.yaml in client dir
for client_file in sorted(glob.glob("{0}/{1}".format(CLIENTS_SUBDIR, YAML_GLOB))):
logger.info("Found client file: {0}".format(client_file))
# Load client YAML
client_dict = load_client_yaml(WORK_DIR, client_file, CLIENTS_SUBDIR, YAML_GLOB, logger)
if client_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, client_file))
# Check client active and exclude/include
if client_dict["active"] and (
(
args.exclude_clients is not None
and
client_dict["name"].lower() not in exclude_clients_list
)
or
(
args.include_clients is not None
and
client_dict["name"].lower() in include_clients_list
)
or
(
args.exclude_clients is None
and
args.include_clients is None
)
):
clients_dict[client_dict["name"]] = client_dict
else:
# Read specific client yaml
if args.make_gmail_drafts_for_client is not None:
client_in_arg, = args.make_gmail_drafts_for_client
elif args.print_papers_for_client is not None:
client_in_arg, = args.print_papers_for_client
else:
raise Exception("Impossible became possible")
client_dict = load_client_yaml(WORK_DIR, "{0}/{1}.{2}".format(CLIENTS_SUBDIR, client_in_arg.lower(), YAML_EXT), CLIENTS_SUBDIR, YAML_GLOB, logger)
if client_dict is None:
raise Exception("Config file error or missing: {0}/{1}".format(WORK_DIR, client_file))
clients_dict[client_dict["name"]] = client_dict
# List all files in envelopes folder for further usage
try:
envelopes_folder_files = drive_ls(SA_SECRETS_FILE, acc_yaml_dict["envelopes"], acc_yaml_dict["gsuite"]["drive_user"])
except Exception as e:
raise Exception("Caught exception on gsuite execution")
# Iterate over clients and check Invoices data per client
for client in clients_dict:
client_dict = clients_dict[client]
# If client has Invoices and client active
if client in invoices_dict and client_dict["active"]:
# Make Drafts
if args.make_gmail_drafts_for_client is not None or args.make_gmail_drafts_for_all_clients:
# Iterate over each type of Invoice (needed for template file names)
for invoice_type in INVOICE_TYPES:
# Find all unique merchants for client
client_merchants = set([invoice["merchant"] for invoice in invoices_dict[client] if invoice["type"] == invoice_type])
# For each merchant within client
for client_merchant in client_merchants:
# Throw error if more than 1 Prepared Invoice of each type for the same merchant
# We shouldn't accumulate Prepared Invoices - we need to send them after each preparation as email templates has specific Invoice number
if sum(invoice["merchant"] == client_merchant and invoice["status"] == "Prepared" and invoice["type"] == invoice_type for invoice in invoices_dict[client]) == 1:
# Find Prepared Invoice (first item of one item list)
client_prepared_invoice = [invoice for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Prepared" and invoice["type"] == invoice_type][0]
# Prepare draft debt text template (any invoices with Status == Sent)
if any(invoice["merchant"] == client_merchant and invoice["status"] == "Sent" and invoice["type"] == invoice_type for invoice in invoices_dict[client]):
if "pack_to_archive" in client_dict["billing"]["papers"]["email"] and client_dict["billing"]["papers"]["email"]["pack_to_archive"] == False:
client_gmail_draft_text_debt = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["debt"].format(
debt_list="\n".join([
"- "
+ acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]["number_symbol"]
+ " "
+ invoice["invoice_number"]
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Sent" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_debt = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["debt"].format(
debt_list="\n".join([
"- "
+ invoice["invoice_number"]
+ ".zip"
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Sent" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_debt = ""
# Prepare draft partially received text template (any invoices with Status == Partially Received)
if any(invoice["merchant"] == client_merchant and invoice["status"] == "Partially Received" and invoice["type"] == invoice_type for invoice in invoices_dict[client]):
if "pack_to_archive" in client_dict["billing"]["papers"]["email"] and client_dict["billing"]["papers"]["email"]["pack_to_archive"] == False:
client_gmail_draft_text_part = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["part"].format(
part_list="\n".join([
"- "
+ acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]["number_symbol"]
+ " "
+ invoice["invoice_number"]
+ " "
+ "("
+ (invoice["sum_processed"] if (invoice["sum_processed"] is not None and invoice["sum_processed"] != "") else invoice["sum_received"])
+ "/"
+ invoice["invoice_sum"]
+ " "
+ invoice["invoice_currency"]
+ ")"
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Partially Received" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_part = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["part"].format(
part_list="\n".join([
"- "
+ invoice["invoice_number"]
+ ".zip"
+ " "
+ "("
+ (invoice["sum_processed"] if (invoice["sum_processed"] is not None and invoice["sum_processed"] != "") else invoice["sum_received"])
+ "/"
+ invoice["invoice_sum"]
+ " "
+ invoice["invoice_currency"]
+ ")"
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Partially Received" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_part = ""
# Prepare draft over received text template (any invoices with Status == Over Received)
if any(invoice["merchant"] == client_merchant and invoice["status"] == "Over Received" and invoice["type"] == invoice_type for invoice in invoices_dict[client]):
if "pack_to_archive" in client_dict["billing"]["papers"]["email"] and client_dict["billing"]["papers"]["email"]["pack_to_archive"] == False:
client_gmail_draft_text_over = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["over"].format(
over_list="\n".join([
"- "
+ acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]]["number_symbol"]
+ " "
+ invoice["invoice_number"]
+ " "
+ "("
+ (invoice["sum_processed"] if (invoice["sum_processed"] is not None and invoice["sum_processed"] != "") else invoice["sum_received"])
+ "/"
+ invoice["invoice_sum"]
+ " "
+ invoice["invoice_currency"]
+ ")"
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Over Received" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_over = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["over"].format(
over_list="\n".join([
"- "
+ invoice["invoice_number"]
+ ".zip"
+ " "
+ "("
+ (invoice["sum_processed"] if (invoice["sum_processed"] is not None and invoice["sum_processed"] != "") else invoice["sum_received"])
+ "/"
+ invoice["invoice_sum"]
+ " "
+ invoice["invoice_currency"]
+ ")"
for invoice in invoices_dict[client] if invoice["merchant"] == client_merchant and invoice["status"] == "Over Received" and invoice["type"] == invoice_type
])
)
else:
client_gmail_draft_text_over = ""
# Prepare draft act text template if act needed
if client_dict["billing"]["papers"]["act"]["email"]:
client_gmail_draft_text_act = \
acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["templates"][client_dict["billing"]["template"]][invoice_type.lower()]["invoice"]["email"]["text"]["act"]
else:
client_gmail_draft_text_act = ""
# Prepare draft final text and remove double newlines several times
if "woocommerce" in acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]:
order_link_text = "{woocommerce_url}/my-account/view-order/{order_id}/".format(
woocommerce_url=acc_yaml_dict["merchants"][client_dict["billing"]["merchant"]]["woocommerce"]["url"],
order_id=client_prepared_invoice["ext_order_number"]
)
else:
order_link_text = ""
# Choose email main text depending on pack_to_archive
if "pack_to_archive" in client_dict["billing"]["papers"]["email"] and client_dict["billing"]["papers"]["email"]["pack_to_archive"] == False:
main_text_key = "main_no_pack_to_archive"
else:
main_text_key = "main"