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."; } } } }
$timeSheet->read_globals(); $timeSheet->read_globals("timeSheet_"); $timeSheet->set_value("status", "edit"); $TPL["message_help"] = "Begin a Time Sheet by selecting a Project and clicking the Create Time Sheet button. A manager must add you to the project before you can create time sheets for it."; } } } // THAT'S THE END OF THE BIG SAVE. $person = $timeSheet->get_foreign_object("person"); $TPL["timeSheet_personName"] = $person->get_name(); $timeSheet->set_values("timeSheet_"); if (!$timeSheetID) { $timeSheet->set_value("personID", $current_user->get_id()); } if ($_POST["create_transactions_default"] && $timeSheet->have_perm(PERM_TIME_INVOICE_TIMESHEETS)) { $msg .= $timeSheet->createTransactions(); } else { if ($_POST["delete_all_transactions"] && $timeSheet->have_perm(PERM_TIME_INVOICE_TIMESHEETS)) { $msg .= $timeSheet->destroyTransactions(); } } // Take care of saving transactions if (($_POST["p_button"] || $_POST["a_button"] || $_POST["r_button"]) && $timeSheet->have_perm(PERM_TIME_INVOICE_TIMESHEETS)) { if ($_POST["p_button"]) { $status = "pending"; } else { if ($_POST["a_button"]) { $status = "approved"; } else { if ($_POST["r_button"]) { $status = "rejected";