alloc_error("Please select a Project and then click the Create Time Sheet button."); } // If it's a Pre-paid project, join this time sheet onto an invoice if (is_object($project) && $project->get_id() && $project->get_value("projectType") == "Prepaid") { $invoiceID = $project->get_prepaid_invoice(); if (!$invoiceID) { $save_error = true; alloc_error("Unable to find a Pre-paid Invoice for this Project or Client."); } else { if (!$timeSheet->get_id()) { $add_timeSheet_to_invoiceID = $invoiceID; } } } if ($_POST["save_and_MoveForward"]) { $msg .= $timeSheet->change_status("forwards"); } else { if ($_POST["save_and_MoveBack"]) { $msg .= $timeSheet->change_status("backwards"); } } $timeSheet->set_value("billingNote", rtrim($timeSheet->get_value("billingNote"))); if ($TPL['message'] || $save_error) { // don't save or sql will complain $url = $TPL["url_alloc_timeSheet"]; } else { if (!$timeSheet->get_value("personID") && $timeSheetID) { //if TS ID is set but person ID is not, it's an existing timesheet this // user doesn't have access to (and will overwrite). Don't proceed. $url = $TPL["url_alloc_timeSheet"]; } else {
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."; } } } }
/** * Move a time sheet to a different status * @param integer $timeSheetID the time sheet to change * @param string $direction the direction to move the timesheet eg "forwards" or "backwards" * @return string a success message */ public function change_timeSheet_status($timeSheetID, $direction) { $timeSheet = new timeSheet(); $timeSheet->set_id($timeSheetID); $timeSheet->select(); $rtn = $timeSheet->change_status($direction); $timeSheet->save(); return $rtn; }