<?php

$exporttitle = str_replace('-', '<br>', $title);

$thtitle ="";

$html ='<div>';
	// $html .= '<h2 align="center"> '.$langs->trans('pointagerh').' '.$langs->trans('Month'.sprintf('%02d', $month)).' '.$year.' </h2>';
    $html .= '<h2 align="center"> '.$exporttitle.' </h2>';
$html.= '</div>';

$html.= '<br>';

$html.= '<meta charset="utf-8" />';
$html.= '<table class="liste_" style="width:100%;" cellpadding="10px" cellspacing="" border="1">';
	$html.= '<thead>';
		$html.= '<tr class="liste_titre">';

			$html .= '<th>'.$langs->trans('Employes').'</th>';

            if($mode == 'qz') {
                // TJM
                $html .= '<th class="width50 center" rowspan="2">'.$langs->trans('P/J').'</th>';
                // CHANTIER
                if($parcmaterielenabled) {
                    $html .= '<th class="width100 left" rowspan="2">'.$langs->trans('chantierObj').'</th>';
                }
            }

            for ($j=$firstdaymonth; $j <= $lastdaymonth ; $j++) {
                $date = dol_mktime(0, 0, 0, $month, $j, $year);
                $week = (date('N', $date) == 7) ? 'td-week' : '';

                $html .= '<th align="center" class="'.$week.'" colspan="2">'.substr($langs->trans(date('l', $date)), 0, 1).' '.$j.'</th>';   
                $thtitle .= '<th align="center" class="'.$week.'">&nbsp; J &nbsp;</th>';
                $thtitle .= '<th align="center" class="'.$week.'">&nbsp; H &nbsp;</th>';
            }

            $html .= '<th rowspan="2" class="mode_vertical" align="center">'.$langs->trans('nb_day').'</th>';
            $html .= '<th rowspan="2" class="mode_vertical" align="center">'.$langs->trans('nbh_sup').'</th>';

            if($mode == 'qz') {
                $html .= '<th rowspan="2" class="mode_vertical classfortooltip center" title="'.$langs->trans('TotalMO').'">'.$langs->trans('TotalMO').'</th>';
            } else {
                $html .= '<th rowspan="2" class="mode_vertical" align="center">'.$langs->trans('nbh_week').'</th>';
                $html .= '<th rowspan="2" class="mode_vertical" align="center">'.$langs->trans('nbday_fix').'</th>';
                $html .= '<th rowspan="2" class="mode_vertical" align="center">'.$langs->trans('nbh_abs').'</th>';
            }

		$html.= '</tr>';
		$html .= '<tr class="liste_titre">';
            $html .= '<th>'.$langs->trans('Hour/Day').'</th>';
            $html .= $thtitle;
        $html .= '</tr>';
	$html.= '</thead>';

	$html.= '<tbody>';

		$cl = "pair";
		$i=0;
		if ($cl == "pair") { $cl = "impair"; }else{ $cl = "pair"; }
		
		$i=0;
        $var = true;
        if($num>0){
            while ($obj = $db->fetch_object($resql)) {

                $var = !$var;
                $nb_week = 0;
                $total_nbj = 0;
                $total_nb_sup = 0;
                $total_nbh_abs = 0;

                $fkuserid = $obj->fk_user;

                $userstatic->id = $fkuserid;
                $userstatic->admin = isset($obj->admin) ? $obj->admin : '';
                $userstatic->ref = $fkuserid;
                $userstatic->login = $obj->login;
                $userstatic->statut = isset($obj->status) ? $obj->status : '';
                $userstatic->status = isset($obj->status) ? $obj->status : '';
                $userstatic->office_phone = $obj->office_phone;
                $userstatic->user_mobile = $obj->user_mobile;
                $userstatic->email = isset($obj->email) ? $obj->email : '';
                $userstatic->gender = $obj->gender;
                $userstatic->socid = isset($obj->fk_soc) ? $obj->fk_soc : '';
                $userstatic->firstname = $obj->firstname;
                $userstatic->lastname = $obj->lastname;
                $userstatic->employee = isset($obj->employee) ? $obj->employee : '';
                $userstatic->photo = $obj->photo;

                $tjm = $obj->tjm;

                $nbrjoursmois = 0;
                
                $html .= '<tr class="'.$cl.'" id="trpointage_'.$fkuserid.'">';
                    $html .= '<td class="nowrap">'.$userstatic->firstname.' '.$userstatic->lastname.'</td>';


                    // ------------------------------------------------------------------------------------------------------------------------ Data Month
                        if($mode == 'qz') {
                            // TJM
                            $html .= '<td class="nowrap center">';
                                $html .= $tjm ? ($tjm+0) : '';
                            $html .= '</td>';
                        }
                    // ------------------------------------------------------------------------------------------------------------------------


                    for ($j=$firstdaymonth; $j <= $lastdaymonth ; $j++) {

                        $datep = dol_mktime(0, 0, 0, $month, $j, $year);
                        $week = (date('N', $datep) == 7) ? 'td-week' : '';
                        $checked = !empty($checked) ? $checked : '';
                        $checkedweek = !empty($checkedweek) ? $checkedweek : '';

                        $check = isset($datapointage[$fkuserid][$j]['id']) ? ($datapointage[$fkuserid][$j]['jour'] ? ($week ? $checkedweek : $checked) : 'x') : '-';


                        // -------------------------------------------------------------------------------------------------------------------
                        
                        $absent = (($check == 'x' && !$week && $mode == 'month') || ($check == 'x' && $mode == 'qz')) ? 'error' : '';

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

                        $pointid = (isset($datapointage[$fkuserid][$j]) && isset($datapointage[$fkuserid][$j]['id'])) ? $datapointage[$fkuserid][$j]['id'] : 0;
                        $jour       = ($pointid) ? ($datapointage[$fkuserid][$j]['jour']+0) : '';
                        $nbhr       = ($pointid) ? ($datapointage[$fkuserid][$j]['nbhr']+0) : '';
                        $chantier   = ($parcmaterielenabled && $pointid) ? ($datapointage[$fkuserid][$j]['fk_chantier']) : '';
                        $labelchan  = ($pointid) ? ($datapointage[$fkuserid][$j]['label_chantier']) : '';
                        
                        // $jour = $jour ? $jour : ($absent ? 'x' : '');

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

                        $val_jour = $jour ? $jour : ($absent ? 'x' : '');
                        $val_nbhr = ($nbhr > 0) ? $nbhr : '';

                        if($chantier && $srch_chantier > 0 && $chantier != $srch_chantier) {
                            $val_jour = $val_nbhr = '';
                        }

                        $html .= '<td align="center" class="'.$week.' '.$absent.'">'.$val_jour.'</td>';
                        $html .= '<td align="center" class="'.$week.'">'.$val_nbhr.'</td>';
                        

                        if($jour && $week) $nb_week++;

                        if($jour && ((!$week && $mode == 'month') || $mode == 'qz')) {
                            $total_nbj++;
                        }

                        if($nbhr) $total_nb_sup += $nbhr; 
                        if(!$week) $nbrjoursmois++;

                    }
                    $total_nbh_abs = $total_nbj ? ($nbrjoursmois - $total_nbj)*$nbh_day_fixe : 0;
                    $total_nbh_week = $nb_week*9;

                    $html .= '<td align="center">'.$total_nbj.'</td>';
                    $html .= '<td align="center">'.$total_nb_sup.'</td>';

                    if($mode == 'qz') {
                        $total_mo = ($total_nbj*$tjm) + (($total_nb_sup*$tjm)/(int)$nbh_day_fixe);
                        $html .=  '<td align="center">'.number_format($total_mo, 2, '.', ' ').'</td>';
                    } else {
                        $html .= '<td align="center">'.$total_nbh_week.'</td>';
                        $html .= '<td align="center">'.$nbrjoursmois.'</td>';
                        $html .= '<td align="center">'.($total_nbh_abs>0 ? $total_nbh_abs : 0).'</td>';
                    }

                $html .= '</tr>';

                $i++;
            }

        }
		else{
			$html .='<tr><td align="center" colspan="15">'.$langs->trans("NoRecordFound").'</td></tr>';
		}
	$html.= '</tbody>';



$html.= '</table>';
$html.= '<style>
th{font-family: Arial, Helvetica, sans-serif;font-weight:bold;}
td{font-family: Arial, Helvetica, sans-serif;}
th.mode_vertical{text-orientation: sideways;writing-mode: vertical-rl;}
.td-week{background: grey}
.td-week{background: #ddd}
.totp_table td{text-align:left;}
.title1{text-align:center;font-size:13;font-weight:bold;}
.title2{text-align:center;font-size:10;}

.liste_ td{border:solid 1px #000;}
.liste_ th{color: #fff;border-bottom: solid 1px #000;border: solid 1px #000;}
.liste_{border: solid 1px #000;}

table tr.pair td{background-color: #F3F4F6;}
table tr.impair td{background-color: #fff;}
table tr.total td{background-color: #e6e6e6; font-weight: bold}

.tfoot td{background-color: #eee;border-top:1px solid #000;}

.liste_ th{text-align:center;background-color: #e6e6e6;}

.badge-status2{background-color: #9c9c26;color: #ffffff; width: 100%;}
.badge-status1{background-color: #bc9526;color: #ffffff; width: 100%;}
.badge-status3{background-color: #bca52b;color: #212529; width: 100%;}

.error {
    color: #660000;
    font-weight: bold;
}

</style>';
// header("Content-Type: application/xls");
// header("Content-Disposition: attachment; filename=".$filename."");
// echo $html;
// die($html);

?>