# -*- coding:utf-8 -*-
from rh.ac.models import *
from datetime import datetime
from contrib.utils import DateRange, MultipleDateRange
import sys
import os
import time
from standard.models import ClassCode
MONTHS = [6,7,8,9,10,11,12]
YEAR = 2022
#ABRIR/FECHAR MESES
def open_close_months(close=False):
for month in MONTHS:
print'ABRINDO/FECHANDO MES : {}'.format(month)
MonthActivity.closing_months(month,YEAR,close)
#ATUALIZAR OS CALCS
def update_calcs():
classcode = ClassCode.objects.get(slug='ac-mppi-calculation')
cfgs = Configuration.objects.filter()
for cfg in cfgs:
print 'SALVANDO CFG:',cfg ,classcode
cfg.calculation = classcode
cfg.save()
def update_mas():
mas = MonthActivity.objects.filter(month=6,year=YEAR)
startTime = datetime.now()
total = len(mas)
atual = 0
print '<<< ATUALIZANDO SALDO MONTHACTIVITYS >>>'
for ma in mas:
atual+=1
print_pct(atual,total,datetime.now() - startTime,ma)
# print 'atualizando saldo', ma
ma.save()
def print_pct(atual,total,segundos_gastos,texto=None):
pct = int((float(atual)/float(total)*100))
pct_float = float(atual)/float(total)*100
qnt = pct/2
barra = ''
segundos_gastos = segundos_gastos.seconds
total_segundos = int((float(segundos_gastos) / pct_float)*100)
for q in range(1, qnt, 1):
barra +='|'
for q in range(1, 51 - qnt, 1):
barra += ' '
barra = '['+barra+'] '
sys.stdout.write("\033[F") # Cursor up one line
if texto:
print texto
print barra + str(pct) +' /100%' + ' tempo gasto: '+ time.strftime('%H:%M:%S', time.gmtime(segundos_gastos)) + ' | previsao total : ' + time.strftime('%H:%M:%S', time.gmtime(total_segundos))
def get_balance_zero():
mas = MonthActivity.objects.filter(month=3,year=2023,balance__lt=0)
sem_expedientes = []
for ma in mas:
das = DayActivity.objects.filter(employee=ma.employee,month_activity__month__in=MONTHS,month_activity__year=YEAR,activity_type__classification=7)
for da in das:
if da.month_activity not in sem_expedientes:
sem_expedientes.append(da.month_activity)
for ma in sem_expedientes:
print ma
def get_balance_old():
MATRICULAS =[2609,15326,173,16120,15052,15444,15279,15148,15534,15524,137,15234,15306,2456,15817,269,15596,16500,15528,225,365,15451,375,15211,16801,376]
mas = MonthActivity.objects.filter(year=2023,month=3,employee__matricula__in=MATRICULAS)
dict_ma = {}
for ma in mas:
dict_ma[ma.employee.matricula] = ma.balance
print dict_ma
def get_dict_balance():
return {15234: 312, 137: 19, 269: 35, 2456: 194, 16801: 73, 15524: 0, 15528: 1070, 15148: 0, 173: 56, 15534: 176, 15279: 0, 2609: 286, 15817: 0, 15306: 0, 15052: 117, 16120: 10, 15444: 0, 15451: 58, 15326: 288, 225: 0, 15211: 1098, 15596: 745, 365: 495, 16500: 0, 375: 147, 376: 131}
def update_balances():
MATRICULAS =[2609,15326,173,16120,15052,15444,15279,15148,15534,15524,137,15234,15306,2456,15817,269,15596,16500,15528,225,365,15451,375,15211,16801,376]
saldos_anteriores = get_dict_balance()
for matricula in MATRICULAS:
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=matricula).update(is_changeable=False)
mas = MonthActivity.objects.filter(month__in=[6],year=YEAR,employee__matricula=matricula)
for ma in mas:
ma.recalculate()
ma.save()
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=matricula).update(is_changeable=True)
for matricula in MATRICULAS:
saldo_atual = MonthActivity.objects.filter(employee__matricula=matricula,month=3,year=2023).first().balance
saldo_anterior = saldos_anteriores[matricula]
acumulos_semana = 0
das = DayActivity.objects.filter(date__gte='2023-03-03',date__lte='2023-03-10',employee__matricula=matricula)
for da in das:
acumulos_semana += da.authorized_accumulation
if saldo_atual >= 0:
saldo_correcao = (saldo_anterior + acumulos_semana) - saldo_atual
else:
saldo_correcao = (saldo_anterior + acumulos_semana) - saldo_atual
ma = MonthActivity.objects.get(month=6,year=2022,employee__matricula=matricula)
balance = ma.balance + saldo_correcao
print 'ATUALIZANDO SALDO:' ,ma
MonthActivity.objects.filter(id=ma.pk).update(balance=balance)
#UPDATE SALDOS
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=matricula).update(is_changeable=False)
mas = MonthActivity.objects.filter(month__in=[7],year=YEAR,employee__matricula=matricula)
for ma in mas:
ma.recalculate()
ma.save()
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=matricula).update(is_changeable=True)
def recalculate_saldo_zero():
mas = MonthActivity.objects.filter(month=3,year=2023,balance__lt=0)
servidores_com_atrasos = []
for ma in mas:
if ma.employee not in servidores_com_atrasos:
servidores_com_atrasos.append(ma.employee)
#ABRINDO MESES
for s in servidores_com_atrasos:
print 'recalculando servidor:' , s
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=s.matricula).update(is_changeable=False)
mas = MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=s.matricula)
for ma in mas:
ma.save()
MonthActivity.objects.filter(month__in=MONTHS,year=YEAR,employee__matricula=s.matricula).update(is_changeable=True)
def print_saldo_zero():
mas = MonthActivity.objects.filter(month=3,year=2023,balance__lt=0)
for ma in mas:
print ma