LibreCodeCoop / producao-cooperativista

Automações financeiras de cálculo de produção cooperativista
GNU Affero General Public License v3.0
2 stars 0 forks source link

Relatório de pesos por tempo trabalhado #127

Open vitormattos opened 2 months ago

vitormattos commented 2 months ago

Retorno necessário por cooperado:

A query deve considerar:

vitormattos commented 1 month ago
SELECT JSON_ARRAYAGG(
           JSON_OBJECT(
               'weight', weight,
               'tax_number', tax_number
           )
       ) AS result
FROM (
    SELECT 
        u.alias,
        uptn.value AS tax_number,
        SUM(TIMESTAMPDIFF(SECOND, t.start_time, t.end_time) / 3600) / 8 AS total_work_days,
        CASE 
            WHEN SUM(TIMESTAMPDIFF(SECOND, t.start_time, t.end_time) / 3600) / 8 >= 260*2 THEN 5 -- 2 anos
            WHEN SUM(TIMESTAMPDIFF(SECOND, t.start_time, t.end_time) / 3600) / 8 >= 260 THEN 4 -- 1 ano
            WHEN SUM(TIMESTAMPDIFF(SECOND, t.start_time, t.end_time) / 3600) / 8 >= 100 THEN 3
            WHEN SUM(TIMESTAMPDIFF(SECOND, t.start_time, t.end_time) / 3600) / 8 >= 50 THEN 2
            ELSE 1
        END AS weight
    FROM kimai2_timesheet t
    JOIN kimai2_projects p ON p.id = t.project_id
    JOIN kimai2_customers c ON c.id = p.customer_id
    JOIN kimai2_users u ON u.id = t.user
    JOIN kimai2_user_preferences uptn ON uptn.user_id = u.id AND uptn.name = 'tax_number'
    WHERE c.id NOT IN (18, 22)
    AND t.user IN (SELECT t.user FROM kimai2_timesheet t WHERE t.end_time >= '2024-08-01 23:59:59' AND t.end_time <= NOW())
    GROUP BY u.alias, uptn.value
    ORDER BY weight, uptn.value
) AS aggregated_data;