Last active
August 12, 2022 06:25
-
-
Save tinogis/1107a180c89a5cfef417593bab4f2494 to your computer and use it in GitHub Desktop.
TM/TG Billing utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*# | |
from erppeek import Client | |
import click | |
from urlparse import urlparse | |
import sys | |
import json | |
from datetime import datetime | |
from dateutil.relativedelta import relativedelta | |
try: | |
# Due: https://www.python.org/dev/peps/pep-0476 | |
import ssl | |
try: | |
_create_unverified_https_context = ssl._create_unverified_context | |
except AttributeError: | |
# Legacy Python that doesn't verify HTTPS certificates by default | |
pass | |
else: | |
# Handle target environment that doesn't support HTTPS verification | |
ssl._create_default_https_context = _create_unverified_https_context | |
except ImportError: | |
pass | |
def get_connection(url): | |
url = urlparse(url) | |
conn = [ | |
'{}://{}'.format(url.scheme, url.netloc), | |
url.path.replace('/', ''), | |
url.username | |
] | |
if url.password: | |
conn.append(url.password) | |
c = Client(*conn) | |
return c | |
def clean_bill(c, reg_name, bill_date): | |
sys.stdout.write(" * Cleaning billings of registrator {}\n".format(reg_name)) | |
reg_ids = c.GiscedataRegistrador.search([('name', '=', reg_name)]) | |
if not reg_ids: | |
sys.stdout.write(" [E] Registrator {} not found\n".format(reg_name)) | |
return False | |
bill_datetime = datetime.strptime(bill_date, '%Y-%m-%d').strftime('%Y-%m-%d 00:00:00') | |
c.TgLecturesValidate.clean_duplicated_with_zero(reg_name, bill_datetime) | |
def clean_bills(c, cnc_name, bill_date): | |
# search for meters in cnc | |
# CNC | |
cnc_ids = c.TgConcentrator.search([('name', '=', cnc_name)]) | |
if not cnc_ids: | |
sys.stdout.write("[E] The {} cnc is not found\n".format(cnc_name)) | |
exit(1) | |
cnc_id = cnc_ids[0] | |
meter_ids = c.GiscedataLecturesComptador.search([('tg_cnc_id', '=', cnc_id)]) | |
sys.stdout.write("# Found {} meters on CNC {}\n".format(len(meter_ids), cnc_name)) | |
for meter_data in c.GiscedataLecturesComptador.read(meter_ids, ['name', 'registrador_id']): | |
meter_name = meter_data['name'] | |
reg_id, reg_name = meter_data['registrador_id'] | |
clean_bill(c, reg_name, bill_date) | |
exit(0) | |
@click.command() | |
@click.option('-u', '--url', | |
help='URL to connect to via ERP Peek ( http://user:pass@server:port/DATABASE )', | |
type=str, default='http://admin:admin@localhost/gisce', show_default=True, required=True) | |
@click.option('-c', '--cnc', help='Concentrator to modify', type=str) | |
@click.option('-d', '--bill_date', | |
help='Date of the billings to modify', | |
type=str, default=datetime.today().strftime('%-Y-%m-%d')) | |
def connect_host(url, bill_date, cnc): | |
server_url = urlparse(url).netloc | |
server_url = server_url[server_url.index('@')+1: server_url.rindex(':')] | |
sys.stdout.write("Connecting to server {} ...\n".format(server_url)) | |
c = get_connection(url) | |
clean_bills(c, cnc, bill_date) | |
if __name__ == '__main__': | |
connect_host() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment