Added predefined expenses to export-import.
authorNik Okuntseff <support@anuko.com>
Sat, 17 Nov 2018 15:40:55 +0000 (15:40 +0000)
committerNik Okuntseff <support@anuko.com>
Sat, 17 Nov 2018 15:40:55 +0000 (15:40 +0000)
WEB-INF/lib/ttGroupExportHelper.class.php
WEB-INF/lib/ttOrgImportHelper.class.php
WEB-INF/templates/footer.tpl

index 4e6daad..0e4777a 100644 (file)
@@ -174,6 +174,24 @@ class ttGroupExportHelper {
     return false;
   }
 
+  // getPredefinedExpenses - obtains all predefined expenses for group.
+  function getPredefinedExpenses() {
+    global $user;
+    $mdb2 = getConnection();
+
+    $result = array();
+    $sql = "select * from tt_predefined_expenses where group_id = $this->group_id"; // TODO: add " and org_id = $user->org_id" when possible.
+    $res = $mdb2->query($sql);
+    $result = array();
+    if (!is_a($res, 'PEAR_Error')) {
+      while ($val = $res->fetchRow()) {
+        $result[] = $val;
+      }
+      return $result;
+    }
+    return false;
+  }
+
   // writeData writes group data into file.
   function writeData() {
 
@@ -467,6 +485,19 @@ class ttGroupExportHelper {
     unset($expense_items);
     unset($expense_item_part);
 
+    // Write predefined expenses.
+    $predefined_expenses = $this->getPredefinedExpenses();
+    fwrite($this->file, $this->indentation."  <predefined_expenses>\n");
+    foreach ($predefined_expenses as $predefined_expense) {
+      $predefined_expense_part = $this->indentation.'    '."<predefined_expense name=\"".htmlspecialchars($predefined_expense['name'])."\"";
+      $predefined_expense_part .= " cost=\"".$predefined_expense['cost']."\"";
+      $predefined_expense_part .= "></predefined_expense>\n";
+      fwrite($this->file, $predefined_expense_part);
+    }
+    fwrite($this->file, $this->indentation."  </predefined_expenses>\n");
+    unset($predefined_expenses);
+    unset($predefined_expense_part);
+
     // Write monthly quotas.
     $quotas = ttTeamHelper::getMonthlyQuotas($this->group_id);
     fwrite($this->file, $this->indentation."  <monthly_quotas>\n");
index 3afe269..9f5dd78 100644 (file)
@@ -431,12 +431,24 @@ class ttOrgImportHelper {
         return;
       }
 
+      if ($name == 'PREDEFINED_EXPENSE') {
+        if (!$this->insertPredefinedExpense(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'cost' => $attrs['COST']))) {
+          $this->errors->add($i18n->get('error.db'));
+        }
+        return;
+      }
+
       if ($name == 'MONTHLY_QUOTA') {
-        if (!$this->insertMonthlyQuota($this->current_group_id,
-          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
-          $attrs['YEAR'],
-          $attrs['MONTH'],
-          $attrs['MINUTES'])) {
+        if (!$this->insertMonthlyQuota(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'year' => $attrs['YEAR'],
+          'month' => $attrs['MONTH'],
+          'minutes' => $attrs['MINUTES']))) {
           $this->errors->add($i18n->get('error.db'));
         }
         return;
@@ -640,9 +652,30 @@ class ttOrgImportHelper {
   }
 
   // insertMonthlyQuota - a helper function to insert a monthly quota.
-  private function insertMonthlyQuota($group_id, $year, $month, $minutes) {
+  private function insertMonthlyQuota($fields) {
+    $mdb2 = getConnection();
+    $group_id = (int) $fields['group_id'];
+    $org_id = (int) $fields['org_id'];
+    $year = (int) $fields['year'];
+    $month = (int) $fields['month'];
+    $minutes = (int) $fields['minutes'];
+
+    $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)".
+      " values ($group_id, $org_id, $year, $month, $minutes)";
+    $affected = $mdb2->exec($sql);
+    return (!is_a($affected, 'PEAR_Error'));
+  }
+
+  // insertPredefinedExpense - a helper function to insert a predefined expense.
+  private function insertPredefinedExpense($fields) {
     $mdb2 = getConnection();
-    $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)";
+    $group_id = (int) $fields['group_id'];
+    $org_id = (int) $fields['org_id'];
+    $name = $mdb2->quote($fields['name']);
+    $cost = $mdb2->quote($fields['cost']);
+
+    $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)".
+      " values ($group_id, $org_id, $name, $cost)";
     $affected = $mdb2->exec($sql);
     return (!is_a($affected, 'PEAR_Error'));
   }
index a7e71cf..60ca1c1 100644 (file)
@@ -12,7 +12,7 @@
       <br>
       <table cellspacing="0" cellpadding="4" width="100%" border="0">
         <tr>
-          <td align="center">&nbsp;Anuko Time Tracker 1.18.16.4439 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
+          <td align="center">&nbsp;Anuko Time Tracker 1.18.16.4440 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
             <a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
             <a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
             <a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>