Beispiel #1
0
 function close_related_entity()
 {
     global $TPL;
     // It checks for approved transactions and only approves the timesheets
     // or expenseforms that are completely paid for by an invoice item.
     $db = new db_alloc();
     $q = prepare("SELECT amount, currencyTypeID, status \n                    FROM transaction \n                   WHERE invoiceItemID = %d \n                ORDER BY transactionCreatedTime DESC \n                   LIMIT 1\n                 ", $this->get_id());
     $db->query($q);
     $row = $db->row();
     $total = $row["amount"];
     $currency = $row["currencyTypeID"];
     $status = $row["status"];
     $timeSheetID = $this->get_value("timeSheetID");
     $expenseFormID = $this->get_value("expenseFormID");
     if ($timeSheetID) {
         $timeSheet = new timeSheet();
         $timeSheet->set_id($timeSheetID);
         $timeSheet->select();
         $db = new db_alloc();
         if ($timeSheet->get_value("status") == "invoiced") {
             // If the time sheet doesn't have any transactions and it is in
             // status invoiced, then we'll simulate the "Create Default Transactions"
             // button being pressed.
             $q = prepare("SELECT count(*) as num_transactions \n                        FROM transaction \n                       WHERE timeSheetID = %d \n                         AND invoiceItemID IS NULL\n                     ", $timeSheet->get_id());
             $db->query($q);
             $row = $db->row();
             if ($row["num_transactions"] == 0) {
                 $_POST["create_transactions_default"] = true;
                 $timeSheet->createTransactions($status);
                 $TPL["message_good"][] = "Automatically created time sheet transactions.";
             }
             // Get total of all time sheet transactions.
             $q = prepare("SELECT SUM(amount) AS total \n                        FROM transaction \n                       WHERE timeSheetID = %d \n                         AND status != 'rejected' \n                         AND invoiceItemID IS NULL\n                     ", $timeSheet->get_id());
             $db->query($q);
             $row = $db->row();
             $total_timeSheet = $row["total"];
             if ($total >= $total_timeSheet) {
                 $timeSheet->pending_transactions_to_approved();
                 $timeSheet->change_status("forwards");
                 $TPL["message_good"][] = "Closed Time Sheet #" . $timeSheet->get_id() . " and marked its Transactions: " . $status;
             } else {
                 $TPL["message_help"][] = "Unable to close Time Sheet #" . $timeSheet->get_id() . " the sum of the Time Sheet's *Transactions* (" . page::money($timeSheet->get_value("currencyTypeID"), $total_timeSheet, "%s%mo %c") . ") is greater than the Invoice Item Transaction (" . page::money($currency, $total, "%s%mo %c") . ")";
             }
         }
     } else {
         if ($expenseFormID) {
             $expenseForm = new expenseForm();
             $expenseForm->set_id($expenseFormID);
             $expenseForm->select();
             $total_expenseForm = $expenseForm->get_abs_sum_transactions();
             if ($total == $total_expenseForm) {
                 $expenseForm->set_status("approved");
                 $TPL["message_good"][] = "Approved Expense Form #" . $expenseForm->get_id() . ".";
             } else {
                 $TPL["message_help"][] = "Unable to approve Expense Form #" . $expenseForm->get_id() . " the sum of Expense Form Transactions does not equal the Invoice Item Transaction.";
             }
         }
     }
 }