示例#1
0
 function save_invoice_expenseForm($invoiceID, $expenseFormID)
 {
     $expenseForm = new expenseForm();
     $expenseForm->set_id($expenseFormID);
     $expenseForm->select();
     $db = new db_alloc();
     $db->query("SELECT max(transactionDate) as maxDate\n                  FROM transaction\n                 WHERE expenseFormID = %d", $expenseFormID);
     $row = $db->row();
     $amount = $expenseForm->get_abs_sum_transactions();
     $q = prepare("SELECT * FROM invoiceItem WHERE expenseFormID = %d AND transactionID IS NULL", $expenseFormID);
     $db = new db_alloc();
     $q2 = $db->query($q);
     $r2 = $db->row($q2);
     $ii = new invoiceItem();
     if ($r2) {
         $ii->set_id($r2["invoiceItemID"]);
     }
     $ii->set_value("invoiceID", $invoiceID);
     $ii->set_value("expenseFormID", $expenseForm->get_id());
     $ii->set_value("iiMemo", "Expense Form #" . $expenseForm->get_id() . " for " . person::get_fullname($expenseForm->get_value("expenseFormCreatedUser")));
     $ii->set_value("iiQuantity", 1);
     $ii->set_value("iiUnitPrice", $amount);
     $ii->set_value("iiAmount", $amount);
     $ii->set_value("iiDate", $row["maxDate"]);
     $ii->set_value("iiTax", config::get_config_item("taxPercent"));
     $ii->save();
 }
示例#2
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.";
             }
         }
     }
 }
示例#3
0
function show_new_invoiceItem($template)
{
    global $TPL;
    global $invoice;
    global $invoiceID;
    $current_user =& singleton("current_user");
    // Don't show entry form if no ID
    if (!$invoiceID) {
        return;
    }
    $TPL["div1"] = "";
    $TPL["div2"] = " class=\"hidden\"";
    $TPL["div3"] = " class=\"hidden\"";
    $TPL["div4"] = " class=\"hidden\"";
    if (is_object($invoice) && $invoice->get_value("invoiceStatus") == 'edit' && $current_user->have_role('admin')) {
        // If we are editing an existing invoiceItem
        if (is_array($_POST["invoiceItem_edit"])) {
            $invoiceItemID = key($_POST["invoiceItem_edit"]);
            $invoiceItem = new invoiceItem();
            $invoiceItem->currency = $invoice->get_value("currencyTypeID");
            $invoiceItem->set_id($invoiceItemID);
            $invoiceItem->select();
            $invoiceItem->set_tpl_values("invoiceItem_");
            $TPL["invoiceItem_buttons"] = '
        <button type="submit" name="invoiceItem_delete[' . $invoiceItemID . ']" value="1" class="delete_button">Delete<i class="icon-trash"></i></button>
        <button type="submit" name="invoiceItem_save[' . $invoiceItemID . ']" value="1" class="save_button">Save Item<i class="icon-edit"></i></button>
      ';
            if ($invoiceItem->get_value("timeSheetID")) {
                unset($TPL["div2"]);
                $TPL["div1"] = " class=\"hidden\"";
                $TPL["sbs_link"] = "timeSheet_ii";
            } else {
                if ($invoiceItem->get_value("expenseFormID")) {
                    unset($TPL["div3"]);
                    $TPL["div1"] = " class=\"hidden\"";
                    $TPL["sbs_link"] = "expenseForm_ii";
                } else {
                    if ($invoiceItem->get_value("productSaleID")) {
                        unset($TPL["div4"]);
                        $TPL["div1"] = " class=\"hidden\"";
                        $TPL["sbs_link"] = "productSale_ii";
                    }
                }
            }
            // Else default values for creating a new invoiceItem
        } else {
            $invoiceItem = new invoiceItem();
            $invoiceItem->set_values("invoiceItem_");
            $TPL["invoiceItem_buttons"] = '
         <button type="submit" name="invoiceItem_save" value="1" class="save_button">Add Item<i class="icon-plus-sign"></i></button>
      ';
        }
        // Build dropdown lists for timeSheet and expenseForm options.
        if ($invoice->get_value("clientID")) {
            // Time Sheet dropdown
            $db = new db_alloc();
            $q = prepare("SELECT projectID FROM project WHERE clientID = %d", $invoice->get_value("clientID"));
            $db->query($q);
            $projectIDs = array();
            while ($row = $db->row()) {
                $projectIDs[] = $row["projectID"];
            }
            if ($projectIDs) {
                $q = prepare("SELECT timeSheet.*, project.projectName \n                        FROM timeSheet\n                   LEFT JOIN project ON project.projectID = timeSheet.projectID \n                       WHERE timeSheet.projectID IN (%s) \n                         AND timeSheet.status != 'finished'\n                    GROUP BY timeSheet.timeSheetID\n                    ORDER BY timeSheetID\n                     ", $projectIDs);
                $db->query($q);
                $timeSheetStatii = timeSheet::get_timeSheet_statii();
                while ($row = $db->row()) {
                    $t = new timeSheet();
                    $t->read_db_record($db);
                    $t->load_pay_info();
                    $dollars = $t->pay_info["total_customerBilledDollars"] or $dollars = $t->pay_info["total_dollars"];
                    $timeSheetOptions[$row["timeSheetID"]] = "Time Sheet #" . $t->get_id() . " " . $row["dateFrom"] . " " . $dollars . " for " . person::get_fullname($row["personID"]) . ", Project: " . $row["projectName"] . " [" . $timeSheetStatii[$t->get_value("status")] . "]";
                }
                $TPL["timeSheetOptions"] = page::select_options($timeSheetOptions, $invoiceItem->get_value("timeSheetID"), 150);
            }
            // Expense Form dropdown
            $db = new db_alloc();
            $q = prepare("SELECT expenseFormID, expenseFormCreatedUser\n                      FROM expenseForm \n                     WHERE expenseFormFinalised = 1 \n                       AND seekClientReimbursement = 1\n                       AND clientID = %d\n                  ORDER BY expenseForm.expenseFormCreatedTime", $invoice->get_value("clientID"));
            $db->query($q);
            while ($row = $db->row()) {
                $expenseFormOptions[$row["expenseFormID"]] = "Expense Form #" . $row["expenseFormID"] . " " . page::money(config::get_config_item("currency"), expenseForm::get_abs_sum_transactions($row["expenseFormID"]), "%s%m %c") . " " . person::get_fullname($row["expenseFormCreatedUser"]);
            }
            if ($invoiceItem->get_value("expenseFormID")) {
                $id = $invoiceItem->get_value("expenseFormID");
            }
            $TPL["expenseFormOptions"] = page::select_options($expenseFormOptions, $id, 90);
            $q = prepare("SELECT *\n                      FROM productSale\n                     WHERE clientID = %d\n                       AND status = 'admin'\n                   ", $invoice->get_value("clientID"));
            $invoice->get_value("projectID") and $q .= prepare(" AND projectID = %d", $invoice->get_value("projectID"));
            $db->query($q);
            while ($row = $db->row()) {
                $productSale = new productSale();
                $productSale->set_id($row["productSaleID"]);
                $productSale->select();
                $ps_row = $productSale->get_amounts();
                $productSaleOptions[$row["productSaleID"]] = "Sale #" . $row["productSaleID"] . " " . $ps_row["total_sellPrice"] . " " . person::get_fullname($row["personID"]);
            }
            if ($invoiceItem->get_value("productSaleID")) {
                $id = $invoiceItem->get_value("productSaleID");
            }
            $TPL["productSaleOptions"] = page::select_options($productSaleOptions, $id, 90);
        }
        $TPL["invoiceItem_iiQuantity"] or $TPL["invoiceItem_iiQuantity"] = 1;
        $TPL["invoiceItem_invoiceID"] = $invoice->get_id();
        include_template($template);
    }
}