<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
################################################################################
#                                                                              #
# Copyright (C) 2013-Today  <NAME> - CLVsol                  #
#                                                                              #
# This program is free software: you can redistribute it and/or modify         #
# it under the terms of the GNU Affero General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU Affero General Public License for more details.                          #
#                                                                              #
# You should have received a copy of the GNU Affero General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
################################################################################

from __future__ import print_function

from erppeek import *
import csv

from base import *
import argparse
import getpass

from clv_medicament_group import *
from clv_medicament_group_member import *
from clv_tag import *


def get_new_l0_medicament_active_component(client, active_component_id, concentration, pres_form, pres_form_2):

    new_medicament_id = 0

    clv_active_component = client.model('clv_medicament.active_component')
    active_component_browse = clv_active_component.browse([('id', '=', active_component_id),])

    clv_medicament_form = client.model('clv_medicament.form')
    medicament_form_browse = clv_medicament_form.browse([('id', '=', pres_form),])
    medicament_form_2_browse = clv_medicament_form.browse([('id', '=', pres_form_2),])

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse([('medicament_name', '=', active_component_browse.name[0]),
                                               ('active_component', '=', active_component_id),
                                               ('concentration', '=', concentration),
                                               # ('pres_form', '=', pres_form),
                                               ('pres_form_2', '=', pres_form_2),
                                               ('is_product', '=', 0),
                                               ])
    if medicament_browse.id == []:

        name = active_component_browse.name[0] + ' (' + active_component_browse.name[0] + ') ' + \
               concentration + ' ' + medicament_form_2_browse.name[0]

        print('>>>>>', '(new medicament)', name.encode("utf-8"))

        values = {
            'name': name,
            'medicament_name': active_component_browse.name[0],
            'presentation': False,
            'code': False,
            'active_component': active_component_id,
            'concentration': concentration,
            'pres_form': medicament_form_browse.id[0],
            'pres_form_2': medicament_form_2_browse.id[0],
            'is_product': 0,
            'active': 1,
            }
        new_medicament_id = clv_medicament.create(values).id

    return new_medicament_id


def get_new_l0_medicament_name(client, medicament_name, active_component_id, concentration, pres_form, pres_form_2):

    new_medicament_id = 0

    clv_active_component = client.model('clv_medicament.active_component')
    active_component_browse = clv_active_component.browse([('id', '=', active_component_id),])

    clv_medicament_form = client.model('clv_medicament.form')
    medicament_form_browse = clv_medicament_form.browse([('id', '=', pres_form),])
    medicament_form_2_browse = clv_medicament_form.browse([('id', '=', pres_form_2),])

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse([('medicament_name', '=', medicament_name),
                                               ('active_component', '=', active_component_id),
                                               ('concentration', '=', concentration),
                                               # ('pres_form', '=', pres_form),
                                               ('pres_form_2', '=', pres_form_2),
                                               ('is_product', '=', 0),
                                               ])
    if medicament_browse.id == []:

        name = medicament_name + ' (' + active_component_browse.name[0] + ') ' + \
               concentration + ' ' + medicament_form_2_browse.name[0]

        print('>>>>>', '(new medicament)', name.encode("utf-8"))

        values = {
            'name': name,
            'medicament_name': medicament_name,
            'presentation': False,
            'code': False,
            'active_component': active_component_id,
            'concentration': concentration,
            'pres_form': medicament_form_browse.id[0],
            'pres_form_2': medicament_form_2_browse.id[0],
            'is_product': 0,
            'active': 1,
            }
        new_medicament_id = clv_medicament.create(values).id

    return new_medicament_id


def include_medicament_into_group(client, medicament):

    medicament_group_id = get_medicament_group_id(client, medicament.active_component.id, 
                                                          medicament.concentration, 
                                                          medicament.pres_form_2.id)

    print('>>>>>', medicament.code, medicament.name.encode("utf-8"), medicament_group_id)

    medicament_group_member_l9 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                        medicament.id, 
                                                                        9)

    print('>>>>>', medicament_group_member_l9)

    new_medicament_id = get_new_l0_medicament_active_component(client, medicament.active_component.id, 
                                                                       medicament.concentration, 
                                                                       medicament.pres_form.id, 
                                                                       medicament.pres_form_2.id)
    medicament_group_member_l0 = False
    if new_medicament_id != 0:
        medicament_group_member_l0 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                            new_medicament_id, 
                                                                            0)

    print('>>>>>', medicament_group_member_l0, medicament_group_member_l0)

    new_medicament_id = get_new_l0_medicament_name(client, medicament.medicament_name,
                                                           medicament.active_component.id, 
                                                           medicament.concentration, 
                                                           medicament.pres_form.id, 
                                                           medicament.pres_form_2.id)
    medicament_group_member_l0 = False
    if new_medicament_id != 0:
        medicament_group_member_l0 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                            new_medicament_id, 
                                                                            0)

    print('>>>>>', medicament_group_member_l0, medicament_group_member_l0)


def include_medicaments_into_groups(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:

        medicament_count += 1
        print(medicament_count, medicament.name.encode("utf-8"))

        include_medicament_into_group(client, medicament)

    print('medicament_count: ', medicament_count)


def clv_medicament_updt_code(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:
        medicament_count += 1

        print(medicament_count, medicament.name.encode("utf-8"))

        values = {
            'code': '/',
            }
        clv_medicament.write(medicament.id, values)

    print('medicament_count: ', medicament_count)


def clv_medicament_updt_state_active(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:
        medicament_count += 1

        print(medicament_count, medicament.state, medicament.name.encode("utf-8"))

        if medicament.state == 'new':
            client.exec_workflow('clv_medicament', 'button_revised', medicament.id)
            client.exec_workflow('clv_medicament', 'button_waiting', medicament.id)
            client.exec_workflow('clv_medicament', 'button_activate', medicament.id)

        if medicament.state == 'waiting':
            client.exec_workflow('clv_medicament', 'button_activate', medicament.id)

    print('medicament_count: ', medicament_count)


def clv_medicament_mark_verify_from_orizon_lpm(client):

    args = [('excluded', '=', False),
            ('medicament_ids', '!=', False), ]
    clv_orizon_lpm = client.model('clv_orizon_lpm')
    orizon_lpm_browse = clv_orizon_lpm.browse(args)

    orizon_lpm_count = 0
    medicament_ok = 0
    medicament_to_verify = 0
    for orizon_lpm in orizon_lpm_browse:

        orizon_lpm_count += 1
        print(orizon_lpm_count, orizon_lpm.name.encode("utf-8"))

        clv_medicament = client.model('clv_medicament')
        medicament_browse = clv_medicament.browse([('id', '=', orizon_lpm.medicament_ids[0].id),
                                                   ('state', '!=', 'active'),
                                                   ('state', '!=', 'waiting'),
                                                   ])
        print('>>>>>', medicament_browse.id)

        if medicament_browse.id != []:
            medicament_to_verify += 1
        else:
            medicament_ok += 1

    print('orizon_lpm_count: ', orizon_lpm_count)
    print('medicament_to_verify: ', medicament_to_verify)
    print('medicament_ok: ', medicament_ok)


def clv_medicament_mark_verify_from_medicament_list(client, list_name, list_version_name, args):

    tag_id_Verificar = get_tag_id(\
        client,
        'Verificar', 
        'Registros que necessitam de algum tipo de verificação.')

    clv_medicament_list = client.model('clv_medicament_list')
    medicament_list_browse = clv_medicament_list.browse([('name', '=', list_name),])
    print('>>>>>', medicament_list_browse)

    clv_medicament_list_version = client.model('clv_medicament_list.version')
    medicament_list_version_browse = clv_medicament_list_version.browse(
        [('list_id', '=', medicament_list_browse[0].id),
         ('name', '=', list_version_name),
         ])
    print('>>>>>', medicament_list_version_browse)<commit_msg>Updated "clv_medicament.py".<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
################################################################################
#                                                                              #
# Copyright (C) 2013-Today  <NAME> - CLVsol                  #
#                                                                              #
# This program is free software: you can redistribute it and/or modify         #
# it under the terms of the GNU Affero General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU Affero General Public License for more details.                          #
#                                                                              #
# You should have received a copy of the GNU Affero General Public License     #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
################################################################################

from __future__ import print_function

from erppeek import *
import csv

from base import *
import argparse
import getpass

from clv_medicament_group import *
from clv_medicament_group_member import *
from clv_tag import *


def get_new_l0_medicament_active_component(client, active_component_id, concentration, pres_form, pres_form_2):

    new_medicament_id = 0

    clv_active_component = client.model('clv_medicament.active_component')
    active_component_browse = clv_active_component.browse([('id', '=', active_component_id),])

    clv_medicament_form = client.model('clv_medicament.form')
    medicament_form_browse = clv_medicament_form.browse([('id', '=', pres_form),])
    medicament_form_2_browse = clv_medicament_form.browse([('id', '=', pres_form_2),])

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse([('medicament_name', '=', active_component_browse.name[0]),
                                               ('active_component', '=', active_component_id),
                                               ('concentration', '=', concentration),
                                               # ('pres_form', '=', pres_form),
                                               ('pres_form_2', '=', pres_form_2),
                                               ('is_product', '=', 0),
                                               ])
    if medicament_browse.id == []:

        name = active_component_browse.name[0] + ' (' + active_component_browse.name[0] + ') ' + \
               concentration + ' ' + medicament_form_2_browse.name[0]

        print('>>>>>', '(new medicament)', name.encode("utf-8"))

        values = {
            'name': name,
            'medicament_name': active_component_browse.name[0],
            'presentation': False,
            'code': False,
            'active_component': active_component_id,
            'concentration': concentration,
            'pres_form': medicament_form_browse.id[0],
            'pres_form_2': medicament_form_2_browse.id[0],
            'is_product': 0,
            'active': 1,
            }
        new_medicament_id = clv_medicament.create(values).id

    return new_medicament_id


def get_new_l0_medicament_name(client, medicament_name, active_component_id, concentration, pres_form, pres_form_2):

    new_medicament_id = 0

    clv_active_component = client.model('clv_medicament.active_component')
    active_component_browse = clv_active_component.browse([('id', '=', active_component_id),])

    clv_medicament_form = client.model('clv_medicament.form')
    medicament_form_browse = clv_medicament_form.browse([('id', '=', pres_form),])
    medicament_form_2_browse = clv_medicament_form.browse([('id', '=', pres_form_2),])

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse([('medicament_name', '=', medicament_name),
                                               ('active_component', '=', active_component_id),
                                               ('concentration', '=', concentration),
                                               # ('pres_form', '=', pres_form),
                                               ('pres_form_2', '=', pres_form_2),
                                               ('is_product', '=', 0),
                                               ])
    if medicament_browse.id == []:

        name = medicament_name + ' (' + active_component_browse.name[0] + ') ' + \
               concentration + ' ' + medicament_form_2_browse.name[0]

        print('>>>>>', '(new medicament)', name.encode("utf-8"))

        values = {
            'name': name,
            'medicament_name': medicament_name,
            'presentation': False,
            'code': False,
            'active_component': active_component_id,
            'concentration': concentration,
            'pres_form': medicament_form_browse.id[0],
            'pres_form_2': medicament_form_2_browse.id[0],
            'is_product': 0,
            'active': 1,
            }
        new_medicament_id = clv_medicament.create(values).id

    return new_medicament_id


def include_medicament_into_group(client, medicament):

    medicament_group_id = get_medicament_group_id(client, medicament.active_component.id, 
                                                          medicament.concentration, 
                                                          medicament.pres_form_2.id)

    print('>>>>>', medicament.code, medicament.name.encode("utf-8"), medicament_group_id)

    medicament_group_member_l9 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                        medicament.id, 
                                                                        9)

    print('>>>>>', medicament_group_member_l9)

    new_medicament_id = get_new_l0_medicament_active_component(client, medicament.active_component.id, 
                                                                       medicament.concentration, 
                                                                       medicament.pres_form.id, 
                                                                       medicament.pres_form_2.id)
    medicament_group_member_l0 = False
    if new_medicament_id != 0:
        medicament_group_member_l0 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                            new_medicament_id, 
                                                                            0)

    print('>>>>>', medicament_group_member_l0, medicament_group_member_l0)

    new_medicament_id = get_new_l0_medicament_name(client, medicament.medicament_name,
                                                           medicament.active_component.id, 
                                                           medicament.concentration, 
                                                           medicament.pres_form.id, 
                                                           medicament.pres_form_2.id)
    medicament_group_member_l0 = False
    if new_medicament_id != 0:
        medicament_group_member_l0 = get_medicament_group_member_id(client, medicament_group_id, 
                                                                            new_medicament_id, 
                                                                            0)

    print('>>>>>', medicament_group_member_l0, medicament_group_member_l0)


def include_medicaments_into_groups(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:

        medicament_count += 1
        print(medicament_count, medicament.name.encode("utf-8"))

        include_medicament_into_group(client, medicament)

    print('medicament_count: ', medicament_count)


def clv_medicament_updt_code(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:
        medicament_count += 1

        print(medicament_count, medicament.name.encode("utf-8"))

        values = {
            'code': '/',
            }
        clv_medicament.write(medicament.id, values)

    print('medicament_count: ', medicament_count)


def clv_medicament_updt_state_active(client, args):

    clv_medicament = client.model('clv_medicament')
    medicament_browse = clv_medicament.browse(args)

    medicament_count = 0
    for medicament in medicament_browse:
        medicament_count += 1

        print(medicament_count, medicament.state, medicament.name.encode("utf-8"))

        if medicament.state == 'new':
            client.exec_workflow('clv_medicament', 'button_revised', medicament.id)
            client.exec_workflow('clv_medicament', 'button_waiting', medicament.id)
            client.exec_workflow('clv_medicament', 'button_activate', medicament.id)

        if medicament.state == 'waiting':
            client.exec_workflow('clv_medicament', 'button_activate', medicament.id)

    print('medicament_count: ', medicament_count)


def clv_medicament_mark_verify_from_orizon_lpm(client):

    tag_id_Verificar = get_tag_id(
        client,
        'Verificar',
        'Registro que necessita de algum tipo de verificação.')

    args = [('excluded', '=', False),
            ('medicament_ids', '!=', False), ]
    clv_orizon_lpm = client.model('clv_orizon_lpm')
    orizon_lpm_browse = clv_orizon_lpm.browse(args)

    orizon_lpm_count = 0
    medicament_ok = 0
    medicament_to_verify = 0
    for orizon_lpm in orizon_lpm_browse:

        orizon_lpm_count += 1
        print(orizon_lpm_count, orizon_lpm.name.encode("utf-8"))

        clv_medicament = client.model('clv_medicament')
        medicament_browse = clv_medicament.browse([('id', '=', orizon_lpm.medicament_ids[0].id),
                                                   ('state', '!=', 'active'),
                                                   ('state', '!=', 'waiting'),
                                                   ])
        print('>>>>>', medicament_browse.id)

        if medicament_browse.id != []:
            medicament_to_verify += 1
            values = {
                'tag_ids': [(4, tag_id_Verificar)],
                }
            clv_medicament.write(medicament_browse[0].id, values)
        else:
            medicament_ok += 1

    print('orizon_lpm_count: ', orizon_lpm_count)
    print('medicament_to_verify: ', medicament_to_verify)
    print('medicament_ok: ', medicament_ok)


def clv_medicament_mark_verify_from_medicament_list(client, list_name, list_version_name, args):

    tag_id_Verificar = get_tag_id(\
        client,
        'Verificar', 
        'Registros que necessitam de algum tipo de verificação.')

    clv_medicament_list = client.model('clv_medicament_list')
    medicament_list_browse = clv_medicament_list.browse([('name', '=', list_name),])
    print('>>>>>', medicament_list_browse)

    clv_medicament_list_version = client.model('clv_medicament_list.version')
    medicament_list_version_browse = clv_medicament_list_version.browse(
        [('list_id', '=', medicament_list_browse[0].id),
         ('name', '=', list_version_name),
         ])
    print('>>>>>', medicament_list_version_browse)
{'fraction_non_alphanumeric': 0.1105902407841466, 'fraction_numerical': 0.007244832729597273, 'mean_word_length': 1.8680029332681496, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 164, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6671999', 'n_tokens_mistral': 6742, 'n_tokens_neox': 6433, 'n_words': 1026}