+ // The isPaid determines if an invoice is paid by looking at the paid status of its items.
+ // If any non-paid item is found, the entire invoice is considered not paid.
+ // 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;
+
+ $sql = "select count(*) as count from tt_log where invoice_id = $invoice_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";
+ $res = $mdb2->query($sql);
+ if (!is_a($res, 'PEAR_Error')) {
+ $val = $res->fetchRow();
+ if ($val['count'] > 0)
+ return false; // A non-paid expense item exists.
+ else
+ return true; // All time and expense items in invoice are paid.
+ }
+ return false;
+ }
+
+ // markPaid marks invoice items as paid.
+ static function markPaid($invoice_id, $mark_paid = true) {
+
+ global $user;
+ $mdb2 = getConnection();
+
+ $paid_status = $mark_paid ? 1 : 0;
+ $sql = "update tt_log set paid = $paid_status where invoice_id = $invoice_id and status = 1";
+ $affected = $mdb2->exec($sql);
+ if (is_a($affected, 'PEAR_Error')) return false;
+
+ $sql = "update tt_expense_items set paid = $paid_status where invoice_id = $invoice_id and status = 1";
+ $affected = $mdb2->exec($sql);
+ if (is_a($affected, 'PEAR_Error')) return false;
+
+ return true;
+ }
+