<?php
  /**************************************************************************\
  * phpgwtimetrack - phpGroupWare addon application                          *
  * http://phpgwtimetrack.sourceforge.net                                    *
  * Written by Robert Schader <bobs@product-des.com>                         *
  * Written by Joseph Engo <jengo@phpgroupware.org>                          *
  * --------------------------------------------                             *
  *  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.                                              *
  \**************************************************************************/

  /* $Id: editdetail.php,v 1.14.2.1 2002/03/14 19:26:33 rschader Exp $ */

  // Update complete for phpgroupware 0.9.10 - 4/14/2001 (api calls for accounts and contacts)
  // Could not find where this file needed any significant changes to work in 0.9.10

  // This file is being updated to use the new "TimeSelect2()" function instead
  // of TimeSelector(). Also change to new DateSelector() function.

 if($submit || $submit_new) {
  $GLOBALS['phpgw_info']["flags"] = array("noheader" => True, "nonavbar" => True);
 }

  $GLOBALS['phpgw_info']["flags"]["enable_nextmatchs_class"] = "True";
  $GLOBALS['phpgw_info']["flags"]["currentapp"] = "timetrack";
  include("../header.inc.php");
?>

<?php
  if ($submit  || $submit_new) {

   //if($workedYear && $workedMonth && $workedDay)
    //$worked_date_sql = $workedYear . "-" . $workedMonth . "-" . $workedDay;
   $worked_date_sql = $workdate;

   //$starttimestamp = date("Ymd", mktime(0,0,0,$workedMonth,$workedDay,$workedYear)) . $n_start_time . "00";

   //echo "n_start_time_ampm = [$n_start_time_ampm]";
   //exit;
   if($n_start_time_ampm == "pm" && $n_start_time_h < 12) {
    $n_start_time_h += 12;
   }
   if($n_start_time_ampm == "am" && $n_start_time_h == 12) {
    $n_start_time_h = 0;
   }
   $starttime = $n_start_time_h . ":" . $n_start_time_m . ":00";
 
   if($n_end_time_ampm == "pm" && $n_end_time_h < 12) {
    $n_end_time_h += 12;
   }
   if($n_end_time_ampm == "am" && $n_end_time_h == 12) {
    $n_end_time_h = 0;
   }
   $endtime = $n_end_time_h . ":" . $n_end_time_m . ":00";

   if ($n_detail_billable == "True") { //null value passed
    $billit = "Y";
   } else {
    $billit = "N";
   }

   // Simple error checking
   if($n_catagory && $worked_date_sql && $starttime && $n_whours) {
    if($submit) {
     $editjdet_sql = "UPDATE phpgw_ttrack_job_details SET work_catagory_id=$n_catagory,"
	. "work_date='$worked_date_sql',start_time='$starttime',"
	. "end_time='$endtime',num_hours='$n_whours',detail_billable='$billit',"
	. "comments='" . addslashes($n_comments) . "'"
	. " WHERE detail_id=$n_detail_id";

     $GLOBALS['phpgw']->db->query($editjdet_sql);
    } else { // Only alternative is $submit_new
     $newjobdetail_sql = "insert into phpgw_ttrack_job_details (job_id, account_id, work_catagory_id, work_date, start_time, "
        . "end_time, num_hours, detail_billable, comments) "
        . "VALUES ('$n_job_id','$n_employee','$n_catagory','$worked_date_sql','$starttime',"
        . "'$endtime','$n_whours','$billit','" . addslashes($n_comments) . "')";
     $GLOBALS['phpgw']->db->query($newjobdetail_sql);
    }

    // Add code here to update the total_hours field in jobs from the SUM'd job_details
    $GLOBALS['phpgw']->db->query("SELECT sum(num_hours) from phpgw_ttrack_job_details "
	. "WHERE job_id=$n_job_id");
    $GLOBALS['phpgw']->db->next_record();
    $total = $GLOBALS['phpgw']->db->f(0);
    $GLOBALS['phpgw']->db->query("UPDATE phpgw_ttrack_jobs set total_hours='$total' WHERE job_id=$n_job_id");

    $yr=strval(substr($workdate,0,4));
    $mo=strval(substr($workdate,5,2));
    $da=strval(substr($workdate,8,2));
    $ytext = "year=$yr&month=$mo&day=$da";

    echo '<script LANGUAGE="JavaScript">';
    echo 'window.location="' . $GLOBALS['phpgw']->link("/timetrack/timesheets.php","$ytext&n_employee=$n_employee") . '"';
    echo '</script>';
  } else { // Error, go back
    echo "You made a mistake, please use your back button to correct it";
  }

  } // end submit
else
  {
   inc_cal(); // Init js calendar datepicker
   inc_myutil(); // validation routines, etc for form inputs
 ?>
  <center><h3>
    <?php echo lang("Edit Time Entry");?>
  </h3></center>
  <form method="POST" name="jobform" 
   action="<?php echo $GLOBALS['phpgw']->link("/timetrack/editdetail.php");?>">
   <?php
     if ($error) {
      echo "<center>" . lang("Error") . ":$error</center>";
     }
     echo "<center>" . lang("Detail ID") . ": " . $detailid
        . "</center><br>";
     echo '<input type=hidden name=n_detail_id value="' . $detailid . '">';
     $GLOBALS['phpgw']->db->query("select * from phpgw_ttrack_job_details where detail_id=" . $detailid);
     $GLOBALS['phpgw']->db->next_record();
     $n_job_id = $GLOBALS['phpgw']->db->f("job_id");
     $n_account_id = $GLOBALS['phpgw']->db->f("account_id");
     $n_work_catagory_id = $GLOBALS['phpgw']->db->f("work_catagory_id");
     $n_work_date = $GLOBALS['phpgw']->db->f("work_date");
     $n_start_time = $GLOBALS['phpgw']->db->f("start_time");
     $n_end_time = $GLOBALS['phpgw']->db->f("end_time");
     $n_whours = $GLOBALS['phpgw']->db->f("num_hours");
     $n_detail_billable = $GLOBALS['phpgw']->db->f("detail_billable");
     $n_comments = $GLOBALS['phpgw']->db->f("comments");
     // Need to now get customer_id and job_number,revision from jobs table, then
     // get customers.company_name.
     $GLOBALS['phpgw']->db->query("select company_id,job_number,job_revision,summary "
	. "from phpgw_ttrack_jobs where job_id=" . $n_job_id);
     $GLOBALS['phpgw']->db->next_record();
     $n_company_id = $GLOBALS['phpgw']->db->f("company_id");
     $n_job_number = $GLOBALS['phpgw']->db->f("job_number");
     $n_job_revision = $GLOBALS['phpgw']->db->f("job_revision");
     $n_summary = $GLOBALS['phpgw']->db->f("summary");
     // Info to get from other tables: customers.company_name
     // other table info should be able to get when doing the SELECT dropdowns:
     // contact_id(name), account_id(employee), status_name, approved_by.
     $GLOBALS['phpgw']->db->query("select company_name from phpgw_ttrack_customers "
	. "where company_id=" . $n_company_id);
     $GLOBALS['phpgw']->db->next_record();
     $n_customer = $GLOBALS['phpgw']->db->f("company_name");

     // For passing account_id back to timesheets.php
     echo '<input type=hidden name=n_employee value="' . $n_account_id . '">';
     // For passing other, non editable items back when submitting using the "Add As New" button
     echo '<input type=hidden name=n_job_id value="' . $n_job_id . '">';
     //echo '<input type=hidden name=n_ value="' . $n_ . '">';
     //echo '<input type=hidden name=n_ value="' . $n_ . '">';
     //echo '<input type=hidden name=n_ value="' . $n_ . '">';

?>
        <center>
         <table border=0 width=65%>
           <tr>
             <td><?php echo lang("Company"); ?></td>
	     <td><?php echo $n_customer; ?></td>
           </tr>
           <tr>
             <td><?php echo lang("Job Number"); ?></td>
             <td><?php echo $n_job_number; ?></td>
           </tr>
           <tr>
             <td><?php echo lang("Revision"); ?></td>
             <td><?php echo $n_job_revision; ?></td>
           </tr>
           <tr>
             <td><?php echo lang("Summary"); ?></td>
             <td><?php echo $n_summary; ?></td>
           </tr>
           <tr>
             <td><?php echo lang("Catagory"); ?></td>
             <td><select name="n_catagory">
                <option value="">
		<?php echo lang("Select Work Type") . '...';?>
		</option>
                <?php
                  $GLOBALS['phpgw']->db->query("select * from phpgw_ttrack_wk_cat "
			. "order by work_catagory_id");
                   while ($GLOBALS['phpgw']->db->next_record()) {
                        $n_catagory_id = $GLOBALS['phpgw']->db->f("work_catagory_id");
                        $n_catname = $GLOBALS['phpgw']->db->f("catagory_desc");
                        echo '<option value="' . $n_catagory_id . '"';
                        if($n_catagory_id == $n_work_catagory_id) echo "selected";
                        echo '>' . $n_catname . '</option>';
                   }
                ?>
             </select></td>
           </tr>
           <tr>
             <td><?php echo lang("Date Worked"); ?></td>
		<?php // Need to turn quote date into a unix timestamp here:
                  if(($n_work_date != "") && ($n_work_date !="0000-00-00"))
                  {
		   $work_date = mysql_datetime_to_timestamp($n_work_date);
                  } else {
                   $work_date = 0;
                  }
		?>
	     <td>
		<?php 
		  $yr=strval(substr($n_work_date,0,4));
		  $mo=strval(substr($n_work_date,5,2));
		  $da=strval(substr($n_work_date,8,2));
		  CalDateSelector("jobform","workdate",0,"",$mo,$da,$yr);
		  //DateSelector("worked",$work_date);
		  //echo '</td><td>';
		  //cal_layer(500,150);
		?></td>
           </tr>
           <tr>
             <td><?php echo lang("Start Time"); ?></td>
             <td><?php 
                   //$stime = mysql_timestamp_to_timestamp($n_start_time);
                   TimeSelect3("jobform","n_start_time",0,strval(substr($n_start_time,0,2)),
			strval(substr($n_start_time,3,2)));
                 ?></td>
           </tr>
           <tr>
             <td><?php echo lang("End Time"); ?></td>
             <td><?php
                 if($n_end_time) {
                   // Once the database has been updates, we really won't need to test this
                   TimeSelect3("jobform","n_end_time",0,strval(substr($n_end_time,0,2)),
                        strval(substr($n_end_time,3,2)));
                 } else {
                   TimeSelect3("jobform","n_end_time",0);
                 }
                 ?></td>
           </tr>
           <tr>
             <td><?php echo lang("Hours Worked"); ?></td>
             <td><input name="n_whours" size="8" maxlength="6"
		    onBlur="CheckNum(this,0,24);Calc_endtime('jobform','n_whours','n_start_time','n_end_time',4);"
		    value="<?php echo $n_whours; ?>">
                 <!-- <input type=button name="calctime" 
		    value="<?php echo lang("Update");?>"
                    onclick="gethours('jobform','n_whours','n_start_time','n_end_time')";> -->
             </td>
           </tr>
           <tr>
             <td><?php echo lang("Billable"); ?></td>
              <!-- This could just be a checkbox, default to True for billable -->
	     <td><input type="checkbox" name="n_detail_billable" value="True"
		<?php
		  if($n_detail_billable == "Y") echo " CHECKED";
		  echo "></td>";
		?>
           </tr>
           <tr>
             <td><?php echo lang("Work Comments"); ?></td>
             <td><textarea  name="n_comments" cols="40" rows="4"
		onBlur="this.value = capitalizeFirstWord(this.value);"
                wrap="virtual"><?php echo $n_comments; ?></textarea></td>
           </tr>
           <tr>
             <td width="10%">
              <input type="submit" name="submit" value="<?php echo lang("Update"); ?>">
             </td>
             <td width="10%">
              <input type="submit" name="submit_new" value="<?php echo lang("Add As New"); ?>">
             </td>
             <td width="10%">
               <?php 
                $myyear = date("Y", $work_date);
                $mymonth = date("m", $work_date);
                $myday = date("d", $work_date);
		echo '<A HREF="' . $GLOBALS['phpgw']->link("/timetrack/timesheets.php") 
                 . "?year=$myyear&month=$mymonth&day=$myday&n_employee=$n_account_id\">" . lang("Cancel"); ?></a>
             </td>
             <td align=right>
               <a href="<?php 
		echo $GLOBALS['phpgw']->link("/timetrack/deletedetail.php",
		 "jd_id=$detailid&year=$myyear&month=$mymonth&day=$myday&n_employee=$n_account_id&jobid=$n_job_id") 
		 . "\">" . lang("Delete"); ?></a>
             </td>
           </tr>
         </table>
        </center>
       </form>
     <?php
     $GLOBALS['phpgw']->common->phpgw_footer();
  }
?>
