<?php
defined('BASEPATH') || exit('No direct script access allowed');

class Crontab extends MY_Controller
{
    public function set_target_poin(){
        $hari = date("N");

        $sql = "INSERT IGNORE INTO sales_target_poin_trx(
                    tgl_trx,
                    id_karyawan,
                    nik,
                    nama_karyawan,
                    poin,
                    id_group_m,
                    nama_group,
                    id_company,
                    user_input,
                    tgl_input,
                    user_update,
                    tgl_update
                )
                SELECT
                    CURDATE() AS tgl_trx,
                    stpp.id_karyawan,
                    dk.nik,
                    dk.nama_lengkap AS nama_karyawan,
                    stpp.poin,
                    sa.id_group_m,
                    CASE
                        WHEN sgm.id_group_m IS NULL THEN ''
                        ELSE CONCAT(sgm.kode, ' - ', sgm.nama)
                    END AS nama_group,
                    stpp.id_company,
                    'Cron Insert' AS user_input,
                    NOW() AS tgl_input,
                    '' AS user_update,
                    '0000-00-00 00:00:00' AS tgl_update
                FROM sales_target_poin_p AS stpp
                JOIN sales_approver AS sa 
                    ON sa.id_karyawan = stpp.id_karyawan
                LEFT JOIN sales_group_m AS sgm
	                ON sgm.id_group_m = sa.id_group_m
                JOIN data_karyawan AS dk
                    ON dk.id_karyawan = stpp.id_karyawan
                JOIN jam_kerja AS jk 
                    ON jk.id_cabang = dk.id_cabang 
                    AND jk.id_company = dk.id_company
                LEFT JOIN holidays
                    ON holidays.id_company = stpp.id_company
                    AND holidays.tanggal = CURDATE()
                WHERE sa.status = '1'
                AND jk.hari = '$hari'
                AND jk.libur = '0'
                AND holidays.tanggal IS NULL ";
    
        return $this->db->query($sql);
    }
}