Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
6.1 kB
0
Indexable
Never
from rh.ac.models import *
from rh.models import *
from datetime import datetime, date
from standard.models import (Choice)
from django.contrib.auth.models import User
from contrib.middleware import set_current_user

ids = [786,833,17,304,77,189,93,179,95,102,156,125,212,78,207,68,210,79,110,187,309,191,82,19,20.2,7,4,3,6,324,371,286,363,1,25,80,124,107,64,122,151,83,76,259,283,344,346,346,89,193,299,100,70,132,67,159,103,88,101,90,93,204,208,143,118,155,109,117,126,72,192,127,120,65,126,313,185,131,81,98,80,124,107,64,122,151,83,76,259,283,344,346,346,89,193,299,100,70,132,67,159,103,88,101,90,93,204,208,143,118,155,109,117,126,72,192,127,120,65,126,313,185,131,81,98,140,342,814,305,345,147,141,284]
# days = [date(2023,9,18),date(2023,9,19),date(2023,9,20),date(2023,9,21),date(2023,9,22)]
days = [date(2023,10,13),date(2023,11,23),date(2023,11,24)]

#  13/10, 23 e 24/11/2023
set_current_user(User.objects.get(username='athenas'))

def add_expedientes(lotacoes=[],days=[]):
    for day in days:
        servidores = get_servidores(lotacoes,day)
        for servidor in servidores:
            print servidor, day
            add_expediente(servidor,day)

def get_servidores(lotacoes=None,day=None):  
    sls = ServidorLotacao.objects.filter(lotacao__id__in=lotacoes)
    sls = sls.filter(
            Q(data_vigencia_inicio__lte=day) & (Q(data_vigencia_fim__gte=day) | Q(data_vigencia_fim=None)))
        
    servidores = []
    for sl in sls:
        if sl.servidor not in servidores:
            servidores.append(sl.servidor)
    
    return servidores

def add_expediente(servidor,day):
    if validate_day(servidor,day):
        da = DayActivity.objects.get(employee=servidor,date=day)
        if accumulation_0(servidor,day,da):
            dict_exp = get_expediente(servidor,day)
            create_expediente(servidor,day,dict_exp)
            update_manual_tickets(servidor,day,da)
            recalculate_day(servidor,day,da)
    
def recalculate_day(servidor=None,day=None,da=None):
    da = DayActivity.objects.get(employee=servidor,date=day)
    da.recalculate()
    da.save()


def get_wc(servidor=None,date=None,workhour_contract=None):
    print servidor,date
    dict_wc = get_values_wc(servidor,date)
     
    tipo = dict_wc['tipo']
    horario = dict_wc['horario']
    duracao = dict_wc['duracao']
    code = dict_wc['code'] 
    
    return crete_workhour_contract(code,date,workhour_contract,horario,tipo,duracao)


def get_values_wc(servidor=None,date=None):
    dict_tipos = {'EST':['EST'],
                    'S':['EFE',],
                    'SC':['ECM','EFC','CMS','REQ','RCM','RFC']}
    
    dict_horarios = {'EST':[u'0800',u'1300'],
                    'S':[u'0800',u'1400'],
                    'SC':[u'0800',u'1500']}
    
    dict_duration = {'EST':5,
                    'S':6,
                    'SC':7}
    
    tipo = 'S'
    horario = [u'0800',u'1400']
    duracao = 6
    code = 'D_EXP_S_'+date.strftime("%Y%m%d")

    for key in dict_tipos.keys():
        if servidor.type_by_possession in dict_tipos[key]:
            tipo = key
            horario = dict_horarios[key]
            duracao = dict_duration[key]
            code = 'D_EXP_'+key+'_'+date.strftime("%Y%m%d")
    
    return {'tipo':tipo,'horario':horario,'duracao':duracao,'code':code}
    

def crete_workhour_contract(code='',date=None,workhour_contract=None,horario=[],tipo='',duration=0):
    
    new_workhour_contract = None
    
    if HoursWorkContract.objects.filter(code=code).exists() == False:
        new_workhour_contract = HoursWorkContract()
        new_workhour_contract.title = 'DISPENSA DE EXP - '+tipo
        new_workhour_contract.code = code
        new_workhour_contract.date_start = date
        new_workhour_contract.data_end = date
        new_workhour_contract.time_start = horario[0]
        new_workhour_contract.time_end = horario[1]
        new_workhour_contract.duration = duration
        new_workhour_contract.flexible = workhour_contract.flexible
        new_workhour_contract.tipo = 11
        new_workhour_contract.save()
    else:
        new_workhour_contract = HoursWorkContract.objects.get(code=code)
    
    return new_workhour_contract


def create_expediente(servidor,day,dict_exp):
    
    workhour_contract = dict_exp['workhour_contract']
    hwcw = dict_exp['hwcw']

    print servidor,day,workhour_contract, hwcw
    code_exp = 'D_EXP_'+day.strftime("%Y%m%d")

    new_workhour_contract = get_wc(servidor,day,workhour_contract)

    new_hwcw = HoursWorkContractWorkload()
    new_hwcw.workhour_contract = new_workhour_contract
    new_hwcw.workload = hwcw.workload
    new_hwcw.week_days = hwcw.week_days
    new_hwcw.save()

def get_expediente(servidor,date):

    query_set = HoursWorkContractWorkload.objects.filter(
        workload__in = servidor.workload(date)
    ).filter(
        models.Q(workhour_contract__date_start__lte=date) &
        (models.Q(workhour_contract__data_end=None) | models.Q(workhour_contract__data_end__gte=date))
    )
    hwcw = None
    if query_set.exists():
        hwcw =query_set.order_by('-workhour_contract__date_start').first()
        workhour_contract = hwcw.workhour_contract
        
    return {'hwcw':hwcw,'workhour_contract':workhour_contract}

def update_manual_tickets(servidor=None,day=None,da=None):
    if not da:
        da = DayActivity.objects.filter(employee=servidor,date=day)
    
    bilhetes_manuais = da.ac_tickets.filter(ticket_type=1).values_list('id', flat=True)
    updates = Ticket.objects.filter(id__in=bilhetes_manuais).update(ticket_type=2)

def validate_day(servidor=None,day=None):
    
    da = DayActivity.objects.filter(employee=servidor,date=day)
    
    if da.exists():
        da = da.first()
        if da.activity_type.classification in [1,6]:
            return True
    else:
        return False
    
    return False

def accumulation_0(servidor,day,da):
    if not da:
        da = DayActivity.objects.get(employee=servidor,date=day)

    if da.accumulation == 0:
        return True
    return 0


add_expedientes(ids,days)
Leave a Comment