updated according to comments from anuko
authorAnže Videnič <anze.videnic@gmail.com>
Mon, 4 Jul 2016 09:28:13 +0000 (11:28 +0200)
committerAnže Videnič <anze.videnic@gmail.com>
Mon, 4 Jul 2016 09:28:13 +0000 (11:28 +0200)
22 files changed:
WEB-INF/config.php.dist
WEB-INF/lib/ttTeamHelper.class.php
WEB-INF/resources/de.lang.php
WEB-INF/resources/en.lang.php
WEB-INF/resources/es.lang.php
WEB-INF/resources/fa.lang.php
WEB-INF/resources/fi.lang.php
WEB-INF/resources/fr.lang.php
WEB-INF/resources/he.lang.php
WEB-INF/resources/nl.lang.php
WEB-INF/resources/pl.lang.php
WEB-INF/resources/pt-br.lang.php
WEB-INF/resources/ru.lang.php
WEB-INF/resources/sk.lang.php
WEB-INF/resources/sr.lang.php
WEB-INF/templates/cf_monthly_quota.tpl
WEB-INF/templates/reports.tpl
cf_monthly_quota.php
dbinstall.php [changed mode: 0644->0755]
default.css
mysql.sql
plugins/MonthlyQuota.class.php

index b52e0d8..5afca00 100644 (file)
@@ -214,3 +214,7 @@ define('AUTH_MODULE', 'db');
 //  'member_of' => array());            // List of groups, membership in which is required for user to be authenticated.
 
 // define('AUTH_DEBUG', false); // Note: enabling AUTH_DEBUG breaks redirects as debug output is printed before setting redirect header. Do not enable on production systems.
+
+// team manager can set monthly quota for years between these values:
+define('MONTHLY_QUOTA_YEARS_START', 2010); // if nothing is specified, it falls back to 2010
+define('MONTHLY_QUOTA_YEARS_END', 2031); // if nothing is specified it falls back to 2030
index ed4c519..186f7a1 100644 (file)
@@ -703,7 +703,7 @@ class ttTeamHelper {
   static function update($team_id, $fields)
   {
     // We'll require team name to be always set.
-    if (!isset($fields['name'])) return false;
+    if (!isset($fields['name']) || $fields['name'] == "") return false;
 
     $mdb2 = getConnection();
     $name_part = 'name = '.$mdb2->quote($fields['name']);
@@ -718,6 +718,7 @@ class ttTeamHelper {
     $record_type_part = '';
     $plugins_part = '';
     $lock_spec_part = '';
+    $working_hours_part = '';
 
     if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
     if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
@@ -730,10 +731,11 @@ class ttTeamHelper {
     if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
     if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
     if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
+    if (isset($fields['working_hours'])) $working_hours_part = ', daily_working_hours = '.$mdb2->quote($fields['working_hours']);
 
     $sql = "update tt_teams set $name_part $addr_part $currency_part $lang_part $decimal_mark_part
       $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
-      $plugins_part $lock_spec_part where id = $team_id";
+      $plugins_part $lock_spec_part $working_hours_part where id = $team_id";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error')) return false;
 
index 24f18b6..ae05e9f 100644 (file)
@@ -203,6 +203,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Anmelden',
index f540a93..f82e543 100644 (file)
@@ -199,6 +199,8 @@ $i18n_key_words = array(
 'label.year' => 'Year',
 'label.month' => 'Month',
 'label.quota' => 'Quota',
+'label.dailyWorkingHours' => 'Daily working hours',
+'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Login',
index dfdaba3..3f1696e 100644 (file)
@@ -234,6 +234,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Sesión iniciada',
index d63ec4f..acdb8f0 100644 (file)
@@ -211,6 +211,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'ورود',
index d94c3c6..ee94cad 100644 (file)
@@ -197,6 +197,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Kirjautuminen',
index e9e71fd..ee899fa 100644 (file)
@@ -216,6 +216,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Connexion',
index e3ef87a..ce02dba 100644 (file)
@@ -216,6 +216,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'כניסה',
index 167bb29..b2ad9bf 100644 (file)
@@ -198,6 +198,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Aanmelden',
index 59257b1..35a5355 100644 (file)
@@ -203,6 +203,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Logowanie',
index f52a9df..021c138 100644 (file)
@@ -197,6 +197,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Login',
index 932f0a9..0c99c07 100644 (file)
@@ -199,6 +199,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Вход в систему',
index 6ee28ba..c8479ad 100644 (file)
@@ -213,6 +213,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Prihlásenie',
index b24a57b..546515b 100644 (file)
@@ -199,6 +199,8 @@ $i18n_key_words = array(
 // 'label.year' => 'Year',
 // 'label.month' => 'Month',
 // 'label.quota' => 'Quota',
+// 'label.dailyWorkingHours' => 'Daily working hours',
+// 'label.empty_values_explanation' => 'If values are empty, quotas are calculated automatically based on holidays in config',
 
 // Form titles.
 'title.login' => 'Prijava',
index 8243eb1..6e3d9fe 100644 (file)
@@ -1,7 +1,22 @@
 {$forms.monthlyQuotaForm.open}
+<div style="padding: 0 0 10 0">
+    <table border="0" class="divider">
+        <tr>
+            <td align="center">
+                <table>
+                    <tr>
+                        <td>{$i18n.label.dailyWorkingHours}</td>
+                        <td>{$forms.monthlyQuotaForm.dailyWorkingHours.control}</td>
+                        <td><input type="submit" name="dailyHours" value="{$i18n.button.save}"></td>
+                    </tr>
+                </table>
+            </td>
+        </tr>        
+    </table>
+</div>
 <table>
     <tr>
-        <td>{$i18n.label.year}</td>
+        <td>{$i18n.label.year}:</td>
         <td>{$forms.monthlyQuotaForm.years.control}</td>
     </tr>
     <tr>
                     <td>{$month}</td>
                     <td>{$forms.monthlyQuotaForm.$month.control}</td>
                 </tr>
- {/foreach}         
+ {/foreach}     
                 <tr>
                     <td colspan="2" style="text-align:center;">
-                        <input type="submit" value="{$i18n.button.save}">
+                        <input type="submit" name="quotas" value="{$i18n.button.save}*">
                     </td>
                 </tr>
             </table>
         </td>
     </tr>
 </table>
+<div>* - {$i18n.label.empty_values_explanation}</div>
 {$forms.monthlyQuotaForm.close}
 <script>
 function yearChange(value){
index bd5ab3f..ce804dc 100644 (file)
@@ -149,7 +149,7 @@ function handleCheckboxes() {
 
 {$forms.reportForm.open}
 <div style="padding: 0 0 10 0;">
-  <table border="0" bgcolor="#efefef" width="720">
+  <table border="0" class="divider">
     <tr>
       <td>
         <table cellspacing="1" cellpadding="3" border="0">
@@ -278,7 +278,7 @@ function handleCheckboxes() {
       </table>
 
 <div style="padding: 10 0 10 0;">
-  <table border="0" bgcolor="#efefef" width="720">
+  <table border="0" class="divider">
     <tr>
       <td align="center">
         <table cellspacing="1" cellpadding="3" border="0">
index 14d0f3f..19b43bf 100644 (file)
@@ -3,6 +3,7 @@
 require_once('initialize.php');
 require_once('plugins/MonthlyQuota.class.php');
 import('form.Form');
+import('WEB-INF/lib/ttTeamHelper');
 
 // Access check.
 if (!ttAccessCheck(right_manage_team)) {
@@ -10,41 +11,67 @@ if (!ttAccessCheck(right_manage_team)) {
   exit();
 }
 
-$form = new Form('monthlyQuotaForm');
-// months are zero indexed
-$months = $i18n->monthNames;
+// fallback values
+$yearStart = 2010;
+$yearEnd = 2030;
+
+if (defined('MONTHLY_QUOTA_YEARS_START')){
+  $yearStart = (int)MONTHLY_QUOTA_YEARS_START;
+}
+if (defined('MONTHLY_QUOTA_YEARS_END')){
+  $yearEnd = (int)MONTHLY_QUOTA_YEARS_END;
+}
+
+// create values for dropdown
 $years = array();
-for ($i=1990; $i < 2040; $i++) { 
+for ($i=$yearStart; $i < $yearEnd; $i++) { 
   array_push($years, array('id'=>$i, 'name'=>$i));
 }
 
-$year = $request->getParameter("year");
-if (!$year or !ttValidInteger($year)){
-  $year = date("Y");
-}else {
-  $year = intval($year);
+// get selected year from url parameters
+$selectedYear = $request->getParameter("year");
+if (!$selectedYear or !ttValidInteger($selectedYear)){
+  $selectedYear = date("Y");
+} else {
+  $selectedYear = intval($selectedYear);
 }
 
+// months are zero indexed
+$months = $i18n->monthNames;
+
 $quota = new MonthlyQuota();
 
 if ($request->isPost()){
-  $postedYear = $request->getParameter("years");
-  $year = intval($postedYear);
   $res = false;
-  for ($i=0; $i < count($months); $i++){
-    $res = $quota->update($postedYear, $i+1, $request->getParameter($months[$i]));
+  // if user pressed save fpr monthly quotas
+  if ($_POST["quotas"]){
+    $postedYear = $request->getParameter("years");
+    $selectedYear = intval($postedYear);
+    for ($i=0; $i < count($months); $i++){
+      $res = $quota->update($postedYear, $i+1, $request->getParameter($months[$i]));
+    }
+  }
+  // if user saved required working hours for a day
+  if ($_POST["dailyHours"]){
+    $hours = $request->getParameter("dailyWorkingHours");
+    $teamDetails = ttTeamHelper::getTeamDetails($quota->usersTeamId);
+    $res = ttTeamHelper::update($quota->usersTeamId, array('name'=>$teamDetails['team_name'], 
+                                                           'working_hours'=>$hours));
   }
-  if ($res){
+  if ($res) {
     header('Location: profile_edit.php');
     exit();
-  } else
-      $err->add($i18n->getKey('error.db'));
+  } else {
+    $err->add($i18n->getKey('error.db'));
+  }
 }
 
 // returns months where January is month 1, not 0
-$monthsData = $quota->get($year);
+$monthsData = $quota->get($selectedYear);
+
+$form = new Form('monthlyQuotaForm');
 
-$form->addInput(array('type'=>'combobox', 'name'=>'years', 'data'=>$years, 'datakeys'=>array('id', 'name'), 'value'=>$year, 'onchange'=>'yearChange(this.value);'));
+$form->addInput(array('type'=>'combobox', 'name'=>'years', 'data'=>$years, 'datakeys'=>array('id', 'name'), 'value'=>$selectedYear, 'onchange'=>'yearChange(this.value);'));
 for ($i=0; $i < count($months); $i++) { 
   $value = "";
   if (array_key_exists($i+1, $monthsData)){
@@ -53,7 +80,9 @@ for ($i=0; $i < count($months); $i++) {
   $name = $months[$i];
   $form->addInput(array('type'=>'text', 'name'=>$name, 'maxlength'=>3, 'value'=> $value, 'style'=>'width:50px'));
 }
+$form->addInput(array('type'=>'text', 'name'=>'dailyWorkingHours', 'value'=>$quota->getDailyWorkingHours(), 'style'=>'width:50px'));
 $smarty->assign('months', $months);
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
+$smarty->assign('title', $i18n->getKey('title.monthly_quota'));
 $smarty->assign('content_page_name', 'cf_monthly_quota.tpl');
 $smarty->display('index.tpl');
old mode 100644 (file)
new mode 100755 (executable)
index 8964d93..1009f51
@@ -521,9 +521,12 @@ if ($_POST) {
   }
   
   if ($_POST["convert1900to1930"]){
-    setChange("CREATE TABLE `timetracker`.`tt_monthly_quota` ( `year` SMALLINT UNSIGNED NOT NULL , `month` TINYINT UNSIGNED NOT NULL , `quota` SMALLINT UNSIGNED NOT NULL , PRIMARY KEY (`year`, `month`))");
+    setChange("CREATE TABLE `tt_monthly_quota` (`team_id` int(11) NOT NULL, `year` smallint(5) UNSIGNED NOT NULL, `month` tinyint(3) UNSIGNED NOT NULL, `quota` smallint(5) UNSIGNED NOT NULL, PRIMARY KEY (`year`,`month`,`team_id`))");
+    setChange("ALTER TABLE `tt_monthly_quota` ADD CONSTRAINT `FK_TT_TEAM_CONSTRAING` FOREIGN KEY (`team_id`) REFERENCES `tt_teams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE");
+    setChange("ALTER TABLE `tt_teams` ADD `daily_working_hours` SMALLINT NULL DEFAULT '8' AFTER `lock_spec`");
+    setChange("UPDATE `tt_teams` SET `daily_working_hours` = 8");
   }
-
+  
   // The update_clients function updates projects field in tt_clients table.
   if ($_POST["update_clients"]) {
     $mdb2 = getConnection();
@@ -607,6 +610,7 @@ if ($_POST) {
     setChange("OPTIMIZE TABLE tt_fav_reports");
     setChange("OPTIMIZE TABLE tt_invoices");
     setChange("OPTIMIZE TABLE tt_log");
+    setChange("OPTIMIZE TABLE tt_monthly_quota");
     setChange("OPTIMIZE TABLE tt_project_task_binds");
     setChange("OPTIMIZE TABLE tt_projects");
     setChange("OPTIMIZE TABLE tt_tasks");
index a24eda7..62032fc 100644 (file)
@@ -117,6 +117,10 @@ select{ font-size: 10pt; font-family: verdana; }
   border-bottom: 1px solid silver;
 }
 
+.borderTop td {
+  border-top: 1px solid silver;
+}
+
 .sectionHeaderNoBorder {
   font-weight: bold;
 }
@@ -131,4 +135,11 @@ select{ font-size: 10pt; font-family: verdana; }
   color: #0000c0;
 }
 
+.divider {
+  background-color: #efefef;
+}
+table.divider {
+  width: 720px;
+}
+
 div#LoginAboutText { width:400px; }
index 1734852..bfc198a 100644 (file)
--- a/mysql.sql
+++ b/mysql.sql
@@ -28,6 +28,7 @@ CREATE TABLE `tt_teams` (
   `plugins` varchar(255) default NULL,                   # a list of enabled plugins for team
   `lock_spec` varchar(255) default NULL,                 # Cron specification for record locking,
                                                          # for example: "0 10 * * 1" for "weekly on Mon at 10:00".
+  `daily_working_hours` smallint(6) DEFAULT '8',         # number of working hours per days, a worker is suppose to work
   `custom_logo` tinyint(4) default '0',                  # whether to use a custom logo or not
   `status` tinyint(4) default '1',                       # team status
   PRIMARY KEY (`id`)
@@ -341,12 +342,17 @@ create index invoice_idx on tt_expense_items(invoice_id);
 
 #
 # Structure for table tt_monthly_quota.
-# This table lists expense items.
+# This table lists monthly quota per team.
 #
 
-CREATE TABLE `tt_monthly_quota` ( 
-  `year` SMALLINT UNSIGNED NOT NULL ,   # year we'setting monthly quota for
-  `month` TINYINT UNSIGNED NOT NULL ,   # month we're settng monthly quota for
-  `quota` SMALLINT UNSIGNED NOT NULL ,  # the monthly quota
-  PRIMARY KEY (`year`, `month`)
+CREATE TABLE `tt_monthly_quota` (
+  `team_id` int(11) NOT NULL,             # team's id
+  `year` smallint(5) UNSIGNED NOT NULL,   # year we'setting monthly quota for
+  `month` tinyint(3) UNSIGNED NOT NULL,   # month we're settng monthly quota for
+  `quota` smallint(5) UNSIGNED NOT NULL,  # the monthly quota
+  PRIMARY KEY (`year`,`month`,`team_id`)
 );
+
+ALTER TABLE `tt_monthly_quota`
+  ADD CONSTRAINT `FK_TT_TEAM_CONSTRAING` FOREIGN KEY (`team_id`) REFERENCES `tt_teams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
+
index ed1c9f8..b50469b 100644 (file)
@@ -4,33 +4,51 @@ class MonthlyQuota {
     
     var $db;
     var $holidays;
+    var $usersTeamId;
     // Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.
     function __construct() {
         $this->db = getConnection();
         $i18n = $GLOBALS['I18N'];
         $this->holidays = $i18n->holidays;
+        global $user;
+        $this->usersTeamId = $user->team_id;
     }
     
     public function update($year, $month, $quota) {
-        $deleteSql = "DELETE FROM tt_monthly_quota WHERE year = $year AND month = $month";
+        $teamId = $this->usersTeamId;
+        $deleteSql = "DELETE FROM tt_monthly_quota WHERE year = $year AND month = $month AND team_id = $teamId";
         $this->db->exec($deleteSql);
-        $insertSql = "INSERT INTO tt_monthly_quota (year, month, quota) values ($year, $month, $quota)";
-        $affected = $this->db->exec($insertSql);
-        return (!is_a($affected, 'PEAR_Error'));
+        if ($quota){
+            $insertSql = "INSERT INTO tt_monthly_quota (team_id, year, month, quota) values ($teamId, $year, $month, $quota)";
+            $affected = $this->db->exec($insertSql);
+            return (!is_a($affected, 'PEAR_Error'));
+        }
+        return true;
     }
         
     public function get($year, $month) {
-        
         if (is_null($month)){
             return $this->getMany($year);
         }
         
         return $this->getSingle($year, $month);
     }
+
+    public function getDailyWorkingHours(){
+        $teamId = $this->usersTeamId;
+        $sql = "SELECT daily_working_hours FROM tt_teams where id = $teamId";
+        $reader = $this->db->query($sql);
+        if (is_a($reader, 'PEAR_Error')) {
+            return false;
+        }
+
+        $row = $reader->fetchRow();
+        return $row["daily_working_hours"];
+    }
     
     private function getSingle($year, $month) {
-        
-        $sql = "SELECT quota FROM tt_monthly_quota WHERE year = $year AND month = $month";
+        $teamId = $this->usersTeamId;
+        $sql = "SELECT quota FROM tt_monthly_quota WHERE year = $year AND month = $month AND team_id = $teamId";
         $reader = $this->db->query($sql);
         if (is_a($reader, 'PEAR_Error')) {
             return false;
@@ -49,14 +67,15 @@ class MonthlyQuota {
             }
             
             $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
-            return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * 8;
+            return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * $this->getDailyWorkingHours();
         }
         
         return $row["quota"];  
     }
     
     private function getMany($year){
-        $sql = "SELECT year, month, quota FROM tt_monthly_quota WHERE year = $year";
+        $teamId = $this->usersTeamId;
+        $sql = "SELECT month, quota FROM tt_monthly_quota WHERE year = $year AND team_id = $teamId";
         $result = array();
         $res = $this->db->query($sql);
         if (is_a($res, 'PEAR_Error')) {