<?php
  /**************************************************************************\
  * phpgwtimetrack - phpGroupWare addon application                          *
  * http://phpgwtimetrack.sourceforge.net                                    *
  * Written by Robert Schader <bobs@product-des.com>                         *
  * --------------------------------------------                             *
  *  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: addjobdetail.php,v 1.18.2.3 2003/03/20 21:11:30 gugux Exp $ */
  // This file is being updated to use the new "TimeSelect2()" function instead
  // of TimeSelector(). Also change from the current DateSelector() to the new one.

  // Update complete for phpgroupware 0.9.10 - 4/13/2001 (api calls for accounts and contacts)

 if($submit_detail) {
  $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_detail) {
   $worked_date_sql = $workdate;

   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_billable == "True") { //null value passed
    $billit = "Y";
   } else {
     $billit = "N";
   }
   if($n_jobnum && $n_employee && $n_catagory && $worked_date_sql && $n_whours) {
     $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_jobnum','$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_jobnum");
    $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_jobnum");

    echo '<script LANGUAGE="JavaScript">';
    echo 'window.location="' . $GLOBALS['phpgw']->link("/timetrack/timesheets.php") . '"';
    echo '</script>';
  } else {
    // Error:
    inc_myutil();
    echo "You forgot something that was needed! Use your back button to correct.";
    // added a little something to wake-up the user regarding his mistakes.
    echo "<script LANGUAGE=\"JavaScript1.2\">shake(2);</script>";
  }

  } // end submit
else
  {
   inc_cal(); // Init js calendar datepicker
   inc_myutil(); // validation routines, etc for form inputs
     ?>
    <center><h3><?php echo lang("Job Detail Entry");?></h3></center>
     <?php
      // Get the userid so we can put it in a hidden form element
	$n_employee = $GLOBALS['phpgw_info']["user"]["account_id"];
	echo '<center><h4>' . lang("for") . ' ' . get_fullname($n_employee) . '</h4></center>';
       ?>

       <form method="POST" name="jobform" action="<?php 
		echo $GLOBALS['phpgw']->link("/timetrack/addjobdetail.php");?>">
       <?php
	 $cust = $n_customer;
	 $cnamesize = 'SIZE="' . $GLOBALS['phpgw_info']["user"]["preferences"]["timetrack"]["cnamesize"] . '"';
         if ($error) {
            echo "<center>" . lang("Error") . ":$error</center>";
         }
       ?>
        <center>
	 <input type=hidden name=n_employee value="<?php echo $n_employee; ?>">
         <table border=0 width=65%>
           <tr>
             <td><?php echo lang("Company"); 
		 if ($GLOBALS['phpgw_info']["user"]["preferences"]["timetrack"]["cnamesize"] > 1 && $cust)
		  {
		    echo '<br><i>' . lang("Selected") . ':</i><br>';
		    $GLOBALS['phpgw']->db->query("select company_name from phpgw_ttrack_customers where "
			  . "company_id = $cust");
		    $GLOBALS['phpgw']->db->next_record();
		    echo "<b>" . $GLOBALS['phpgw']->db->f(0) . "</b>";
		  }
		 ?></td>
		 <?php // need to populate a drop down list here
		   // May want to add a where clause later to only present customers whose
		   // who are current to keep list short (use some kind of active flag in table)
		   ?>
		 <td><select name="n_customer" 
		<?php echo $cnamesize; ?> onChange="this.form.submit()">
		 <!-- Let's do our empty option first -->
		 <option value="">
		   <?php echo lang("Select Customer") . '...';?>
		 </option>
		 <?php
		  $GLOBALS['phpgw']->db->query("select company_id,company_name from "
			. "phpgw_ttrack_customers where active='Y' "
			. "order by company_name");
		  while ($GLOBALS['phpgw']->db->next_record()) {
                    $ncust = $GLOBALS['phpgw']->db->f("company_id");
		    echo '<option value="' . $ncust . '"';
                    if ( $cust == $ncust ) {
                           echo " selected";
                    }
		    echo ">" . $GLOBALS['phpgw']->db->f("company_name") . "</option>";
		   } 
 		  ?>	
                  </select></td>
           </tr>
           <tr>
             <td><?php echo lang("Job Number"); ?></td>
             <td><select name="n_jobnum">
                
		<?php 
		// Check value of $cust, if it is empty, just put a null option telling user to
		// pick the customer first.
		if (! $cust) {
                  echo '<option value="">^-'
			. lang("Pick Customer First") . '-^</option>';
		} else {
		  $jobnum_sql = "select job_id,job_number,job_revision,summary "
		    . "from phpgw_ttrack_jobs where company_id = '$cust'"
                    . " order by job_number desc,job_revision desc";
		   $GLOBALS['phpgw']->db->query($jobnum_sql);
		   $test_result = $GLOBALS['phpgw']->db->num_rows();
		   if ($test_result == 0) {
		     echo'<option value="">'
			. lang("No Match") . '</option>';
		   }
		  while ($GLOBALS['phpgw']->db->next_record()) {
		   $njobid = $GLOBALS['phpgw']->db->f("job_id");
		   $job_name = $GLOBALS['phpgw']->db->f("job_number") . $GLOBALS['phpgw']->db->f("job_revision")
			. " - " . $GLOBALS['phpgw']->db->f("summary");
		    echo '<option value="' . $njobid . '">' . $job_name . '</option>';
		  }
		}
                ?>
             </select></td>
           </tr>
           <tr>
             <td><?php echo lang("Category"); ?></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 . '">' . $n_catname . '</option>';
		   }
		?>
	     </select></td>
           </tr>
           <tr>
             <td><?php echo lang("Date Worked"); ?></td>
             <td>
		<?php 
		  //DateSelector("worked",time());
		  //CalDateSelector("jobform","workdate",0,"INLINE");
                  CalDateSelector("jobform","workdate",0,"");
		  //echo '</td><td>';
		  //cal_layer(600,120);
		?>
	     </td>
           </tr>
           <tr>
             <td><?php echo lang("Start Time"); ?></td>
             <td>
		<?php 
		  //TimeSelector("n_start_time");
		  TimeSelect3("jobform","n_start_time",0);
		?>
	     </td>
           </tr>
           <tr>
             <td><?php echo lang("End Time"); ?></td>
             <td>
                <?php
		   $e_hour = sprintf("%02d", date("H") + 1);
                   TimeSelect3("jobform","n_end_time",0,$e_hour);
		   if(!$n_whours) $n_whours = "1.00";
                ?>
             </td>
           </tr>
           <tr>
             <td><?php echo lang("Hours Worked"); ?></td>
             <td><input name="n_whours" size="8" maxlength="6"
	          onBlur="CheckNum(this,0,24,1);Calc_endtime('jobform','n_whours','n_start_time','n_end_time',4);"
		  value="<?php echo $n_whours; ?>">
                 <!-- <input type=button name="calctime" value="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_billable" value="True" CHECKED></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 colspan=2>
              <input type="submit" name="submit_detail" value="<?php echo lang("submit"); ?>"
               onclick="gethours('jobform','n_whours','n_start_time','n_end_time')";>
             </td>
           </tr>
         </table>
        </center>
       </form>
     <?php
     $GLOBALS['phpgw']->common->phpgw_footer();
  }
?>
