<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from erppeek import Client
import configdb
from yamlns import namespace as ns
from consolemsg import step, success, error, warn
import csv
import time
import argparse

O = None
doit = False
EMAIL_TEMPLATE_ID = 666
EMAIL_FROM_ACCOUNT = 666
CONTRACTS_PER_BATCH = 500
TIME_BETWEEN_BATCHS = 100


def connect_erp():
    global O
    if O:
        return O
    step("Connectant a l'erp")
    O = Client(**configdb.erppeek)
    step("connectat...")
    return O


def parse_arguments():
    parser = argparse.ArgumentParser(
        description="Envia correus d'avis de mort al pagador"
    )

    parser.add_argument(
        '--file',
        dest='csv_file',
        required=True,
        help="csv amb les polisses a avisar"
    )

    parser.add_argument(
        '--doit',
        type=bool,
        default=False,
        const=True,
        nargs='?',
        help='realitza les accions'
    )

    args = parser.parse_args()
    if args.doit:
        success("Es faran enviament de correus (--doit)")
    else:
        success("No es faran enviament de correus (sense opció --doit)")
    global doit
    doit = args.doit

    return args


def read_data_from_csv(csv_file):
    with open(csv_file, 'rb') as f:
        reader = csv.reader(f, delimiter=';')
        header = reader.next()

        # check if file is utf8 + BOM
        if '\xef\xbb\xbf' in header[0]:
            raise IOError

        if len(header) == 1:
            reader = csv.reader(f, delimiter=',')
            header = header[0].split(',')

        csv_content = [ns(dict(zip(header, row))) for row in reader if row[0]]

    return csv_content


def get_polissa_ids_from_csv(filename):
    pol_ids = []
    for contract_name in read_data_from_csv(filename):
        pol_obj = O.GiscedataPolissa
        pol_id = pol_obj.search([('name', '=', contract_name.contracte)])
        if len(pol_id) > 1:
            warn("Multiples resultats per polissa {} : {}",
                 contract_name.contracte, pol_id)
        elif len(pol_id) == 0:
            warn("Sense resultats per polissa {}", contract_name.contracte)
        else:
            pol_ids.extend(pol_id)
    return pol_ids


def send_erp_email(obj_id, obj_model, template_id, from_id):
    ctx = {
        'active_ids': [obj_id],
        'active_id': obj_id,
        'template_id': template_id,
        'src_model': obj_model,
        'src_rec_ids': [obj_id],
        'from': from_id,
        }
    params = {'state': 'single', 'priority': 0, 'from': ctx['from']}
    wiz = O.PoweremailSendWizard.create(params, ctx)
    O.PoweremailSendWizard.send_mail([wiz.id], ctx)


def send_contract_erp_email(pol_id):
    step("Enviant correus a la polissa id {}", pol_id)<commit_msg>zfill to copmplete the contract number and not duplicated contracts<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from erppeek import Client
import configdb
from yamlns import namespace as ns
from consolemsg import step, success, warn
import csv
import time
import argparse

O = None
doit = False
EMAIL_TEMPLATE_ID = 666
EMAIL_FROM_ACCOUNT = 666
CONTRACTS_PER_BATCH = 100
TIME_BETWEEN_BATCHS = 1


def connect_erp():
    global O
    if O:
        return O
    step("Connectant a l'erp")
    O = Client(**configdb.erppeek)
    step("connectat...")
    return O


def parse_arguments():
    parser = argparse.ArgumentParser(
        description="Envia correus d'avis de mort al pagador"
    )

    parser.add_argument(
        '--file',
        dest='csv_file',
        required=True,
        help="csv amb les polisses a avisar"
    )

    parser.add_argument(
        '--doit',
        type=bool,
        default=False,
        const=True,
        nargs='?',
        help='realitza les accions'
    )

    args = parser.parse_args()
    if args.doit:
        success("Es faran enviament de correus (--doit)")
    else:
        success("No es faran enviament de correus (sense opció --doit)")
    global doit
    doit = args.doit

    return args


def read_data_from_csv(csv_file):
    with open(csv_file, 'rb') as f:
        reader = csv.reader(f, delimiter=';')
        header = reader.next()

        # check if file is utf8 + BOM
        if '\xef\xbb\xbf' in header[0]:
            raise IOError

        if len(header) == 1:
            reader = csv.reader(f, delimiter=',')
            header = header[0].split(',')

        csv_content = [ns(dict(zip(header, row))) for row in reader if row[0]]

    return csv_content


def get_polissa_ids_from_csv(filename):
    pol_ids = []
    for contract_name in read_data_from_csv(filename):
        pol_obj = O.GiscedataPolissa

        name = contract_name.contracte.zfill(7)
        pol_id = pol_obj.search([('name', '=', name)])
        if len(pol_id) > 1:
            warn("Multiples resultats per polissa {} : {}",
                 name, pol_id)
        elif len(pol_id) == 0:
            warn("Sense resultats per polissa {}", name)
        else:
            if pol_id[0] not in pol_ids:
                pol_ids.extend(pol_id)
    return pol_ids


def send_erp_email(obj_id, obj_model, template_id, from_id):
    ctx = {
        'active_ids': [obj_id],
        'active_id': obj_id,
        'template_id': template_id,
        'src_model': obj_model,
        'src_rec_ids': [obj_id],
        'from': from_id,
        }
    params = {'state': 'single', 'priority': 0, 'from': ctx['from']}
    wiz = O.PoweremailSendWizard.create(params, ctx)
    O.PoweremailSendWizard.send_mail([wiz.id], ctx)


def send_contract_erp_email(pol_id):
    step("Enviant correus a la polissa id {}", pol_id)
{'fraction_non_alphanumeric': 0.11833688699360341, 'fraction_numerical': 0.007818052594171997, 'mean_word_length': 2.472547809993831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6951124', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1889, 'n_words': 470}