]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttInvoiceHelper.class.php
More refactoring of invoices for subgroups.
[timetracker.git] / WEB-INF / lib / ttInvoiceHelper.class.php
index 13e784d076031b4fc39b47c5cdee16f03972535a..445c02c21fdd458a6d5e3580e8fa16b6679091cf 100644 (file)
@@ -37,10 +37,13 @@ class ttInvoiceHelper {
     global $user;
     $mdb2 = getConnection();
 
-    if ($user->isClient()) $client_part = " and client_id = $user->client_id";
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
 
-    $sql = "select * from tt_invoices where id = $invoice_id and group_id = ".
-            $user->getGroup()."$client_part and status = 1";
+    if ($user->isClient()) $client_part = "and client_id = $user->client_id";
+
+    $sql = "select * from tt_invoices".
+      " where id = $invoice_id and group_id = $group_id and org_id = $org_id $client_part and status = 1";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       if ($val = $res->fetchRow())
@@ -51,12 +54,14 @@ class ttInvoiceHelper {
 
   // The getInvoiceByName looks up an invoice by name.
   static function getInvoiceByName($invoice_name) {
-
-    $mdb2 = getConnection();
     global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
 
-    $sql = "select id from tt_invoices where group_id = ".
-            $user->getGroup()." and name = ".$mdb2->quote($invoice_name)." and status = 1";
+    $sql = "select id from tt_invoices where group_id = $group_id and org_id = $org_id".
+      " and name = ".$mdb2->quote($invoice_name)." and status = 1";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       $val = $res->fetchRow();
@@ -72,18 +77,22 @@ class ttInvoiceHelper {
   // Therefore, the paid status of the invoice is a calculated value.
   // This is because we maintain the paid status on individual item level.
   static function isPaid($invoice_id) {
-
-    $mdb2 = getConnection();
     global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
 
-    $sql = "select count(*) as count from tt_log where invoice_id = $invoice_id and status = 1 and paid < 1";
+    $sql = "select count(*) as count from tt_log".
+      " where invoice_id = $invoice_id and group_id = $group_id and org_id = $org_id and status = 1 and paid < 1";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       $val = $res->fetchRow();
       if ($val['count'] > 0)
         return false; // A non-paid time item exists.
     }
-    $sql = "select count(*) as count from tt_expense_items where invoice_id = $invoice_id and status = 1 and paid < 1";
+    $sql = "select count(*) as count from tt_expense_items".
+      " where invoice_id = $invoice_id and group_id = $group_id and org_id = $org_id and status = 1 and paid < 1";
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       $val = $res->fetchRow();
@@ -257,18 +266,20 @@ class ttInvoiceHelper {
       }
     }
 
-    // sql part for project id.
-    if ($project_id) $project_part = " and ei.project_id = $project_id";
-
-    $sql = "select count(*) as num from tt_expense_items ei
-      where ei.client_id = $client_id $project_part and ei.invoice_id is NULL
-      and ei.date >= ".$mdb2->quote($start)." and ei.date <= ".$mdb2->quote($end)."
-      and ei.cost <> 0 and ei.status = 1";
-    $res = $mdb2->query($sql);
-    if (!is_a($res, 'PEAR_Error')) {
-      $val = $res->fetchRow();
-      if ($val['num']) {
-        return true;
+    if ($user->isPluginEnabled('ex')) {
+      // sql part for project id.
+      if ($project_id) $project_part = " and ei.project_id = $project_id";
+
+      $sql = "select count(*) as num from tt_expense_items ei
+        where ei.client_id = $client_id $project_part and ei.invoice_id is NULL
+        and ei.date >= ".$mdb2->quote($start)." and ei.date <= ".$mdb2->quote($end)."
+        and ei.cost <> 0 and ei.status = 1";
+      $res = $mdb2->query($sql);
+      if (!is_a($res, 'PEAR_Error')) {
+        $val = $res->fetchRow();
+        if ($val['num']) {
+          return true;
+        }
       }
     }