<?php // $Revision: 1.7.2.2 $
/* vim: set expandtab ts=4 sw=4 sts=4: */

/**
 * $Id: noti_logtime,v 1.7.2.2 2007/11/22 07:52:56 luiswang Exp $
 * 
 * Copyright (c) 2007 by the netOffice Dwins developers
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
 
$checkSession = false;
require_once("../includes/library.php");
$eday = date('d');
$month1 = date('n');
$year1 = date('Y');
$timestamp0 = mktime(12, 0, 0, $month1, $eday, $year1);
$timestamp1 = mktime(12, 0, 0, $month1, $eday-28, $year1);
$FourWeekBefore = date('Y-m-d', $timestamp1);
$today = date('Y-m-d', $timestamp0);

// do nothing if today is holiday
$tmpquery = " WHERE wca.date='$today'";
$todayDetail = new request();
$todayDetail->openWorkCalendar($tmpquery);
$comptTodayDetail = count($todayDetail->wca_id);

if (($comptTodayDetail==0)||($todayDetail->wca_hours[0]==0)) {
    exit;
}

// get work hours of the latest 4 weeks
$tmpquery = " WHERE wca.date BETWEEN '$FourWeekBefore' AND '$today' ORDER BY wca.date";
$workCalendarList = new request();
$workCalendarList->openWorkCalendar($tmpquery);
$comptWorkCalendarList = count($workCalendarList->wca_id);
// get the members who want to receive notification
$tmpquery = " WHERE noti.checkLogTime='0' AND mem.id NOT IN ('1','2') AND mem.profil IN ('" . PF_PROJECT_MANAGER_ADMINISTRATOR . "','" . PF_PROJECT_MANAGER . "','" . PF_USER . "')";
$memberList = new request();
$memberList->openNotifications($tmpquery);
$comptMemberList = count($memberList->not_mem_id);

for ($i = 0; $i < $comptMemberList; $i++) {
    $mail = new notification();
    $mail->getUserinfo('1', "from");
    $mail->getUserinfo($memberList->not_mem_id[$i], "to");
    $mail->partSubject = lang_strings("noti_checklogtime1");
    $mail->partMessage = lang_strings("noti_checklogtime2");
    $mail->openBody();
    $mail->openResults();
    $mail->openRow();
    $mail->cellRow(lang_strings('date'));
    $mail->cellRow(lang_strings('hours'));
    $mail->cellRow(lang_strings('standard'));
    $mail->closeRow();
    $comptRow = 0;
    $k = 0;

    for ($j=$timestamp1; $j<$timestamp0;) {
        $curr = date('Y-m-d', $j);

        for (; ($k<$comptWorkCalendarList)&&($workCalendarList->wca_date[$k]!=$curr);) $k++;

        $request1 = new request();
        $timeOfDay = $request1->getMemberTimeOfDay($memberList->not_mem_id[$i], $curr);

        if ($k==$comptWorkCalendarList) {
            if ($timeOfDay != 0) {
                $mail->openRow();
                $mail->cellRow($curr);
                $mail->cellRow($timeOfDay);
                $mail->cellRow("0");
                $mail->closeRow();
                $comptRow++;
            }
        } else {
            if ($timeOfDay != $workCalendarList->wca_hours[$k]) {
                $mail->openRow();
                $mail->cellRow($curr);
                $mail->cellRow($timeOfDay);

                if (trim($workCalendarList->wca_comments[$k])!="") {
                    $mail->cellRow($workCalendarList->wca_hours[$k] . "(" . strip_tags(trim($workCalendarList->wca_comments[$k])) . ")");
                } else {
                    $mail->cellRow($workCalendarList->wca_hours[$k]);
                }

                $mail->closeRow();
                $comptRow++;
            }
        }

        $j = mktime(12,0,0,substr($curr,5,2),substr($curr,8,2)+1,substr($curr,0,4));
    }

    if ($comptRow == 0) continue;

    $mail->closeResults();
    $mail->closeBody();
    $subject = $mail->partSubject;
    $mail->Subject = $subject;
    $mail->Send();
}

?>
