<?php

/* * ********************************************************************
  Kenn
 * ********************************************************************* */

$path_to_root = "..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/sales/includes/db/customers_db.inc");
include_once($path_to_root . '/modules/kanban/includes/kanban_db.inc');
include_once($path_to_root . '/modules/kanban/includes/kanban_ui.inc');

//----------------------------------------------------------------------------------------------------

print_member_transactions();

function get_transactions($emp_id, $from, $to, $dept = 0, $gender = '') {

    $where = '';
    if ($emp_id)
        $where .= " AND a.emp_id=" . db_escape($emp_id);
    if (!empty($dept))
        $where .= " AND e.department_id=" . db_escape($dept);
    if (!empty($gender) && $gender != ALL_NUMERIC)
        $where .= " AND e.gender=" . db_escape($gender);
    if (is_date($from) && is_date($to))
        $where .= " AND a.att_date between '" . date2sql($from) . "' and '" . date2sql($to) . "'";

    $day_work_hours = " IF(a.overtime_id=0,hours_no,0) as day_work_hours ";
    $night_work_hours = " IF(a.overtime_id=1,hours_no,0) as night_work_hours ";
    $sql = "SELECT concat(e.emp_first_name,' ',e.emp_last_name) as employee_name,a.att_date,ci.time,co.time as clockout_time,"
            . " ".$day_work_hours.", ".$night_work_hours
            . " FROM " . TB_PREF . "attendance a "
            . " left join " . TB_PREF . "employee e on a.emp_id=e.emp_id "
            . " left join " . TB_PREF . "clockin_clockout ci on ci.date=a.att_date and ci.user_bio_id=e.bio_data_id and ci.type='33' "
            . " left join " . TB_PREF . "clockin_clockout co on co.date=a.att_date and co.user_bio_id=e.bio_data_id and co.type='44' "
            . " and co.id=(SELECT id from " . TB_PREF . "clockin_clockout ct where time>ci.time and ct.type='44' order by ct.time asc limit 1)"
            . " WHERE  a.overtime_id in (0,1)   " . $where;
    return db_query($sql, "No transactions were returned");
}

//----------------------------------------------------------------------------------------------------

function print_member_transactions() {
    global $path_to_root, $systypes_array;

    $from = $_POST['PARAM_0'];
    $to = $_POST['PARAM_1'];
    $employee_id = $_POST['PARAM_2'];
    $dept = $_POST['PARAM_3'];
    $gender = $_POST['PARAM_4'];
    $orientation = $_POST['PARAM_5'];
    $destination = $_POST['PARAM_6'];

    if ($destination)
        include_once($path_to_root . "/reporting/includes/excel_report.inc");
    else
        include_once($path_to_root . "/reporting/includes/pdf_report.inc");

    $orientation = ($orientation ? 'L' : 'P');
    if ($employee_id == 0)
        $emp = _('All');
    else
        $emp = get_employees($employee_id)['name'];
    $dec = user_price_dec();


    $cols = array(0, 95, 170, 270, 370, 450, 530);

    $headers = array(_('Employee'), _('Date'), _('Clockin'), _('Clockout'), _('Day Work Hrs'), _('Night Work Hrs'));
    $aligns = array('left', 'left', 'left', 'left', 'right', 'right');

    $department_name = '';
    if ($dept) {
        $department_name = get_departments($dept)['dept_name'];
    }

    $params = array(0 => '',
        1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
        2 => array('text' => _('Member'), 'from' => $emp, 'to' => ''),
        3 => array('text' => _('Project'), 'from' => $department_name, 'to' => ''));

    $rep = new FrontReport(_('Employee Logs'), "EmployeeLogs", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
        recalculate_cols($cols);
    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();

    $res = get_transactions($employee_id, $from, $to, $dept, $gender);

    $rep->NewLine(1, 2);
    while ($trans = db_fetch($res)) {

        $rep->NewLine(1, 2);
        $rep->TextCol(0, 1, $trans['employee_name']);
        $rep->TextCol(1, 2, $trans['att_date']);
        $rep->TextCol(2, 3, $trans['time'], true);
        $rep->TextCol(3, 4, $trans['clockout_time'], true);
        $rep->TextCol(4, 5, $trans['day_work_hours'], true);
        $rep->TextCol(5, 6, $trans['night_work_hours'], true);
    }

    
    $rep->Line($rep->row - 4);
    $rep->NewLine();
    $rep->End();
}
