function action_update_total() { log_debug("invoice_items", "Executing action_update_total()"); // default values $amount = "0"; $amount_tax = "0"; $amount_total = "0"; /* Total up all the items, and all the tax */ $amount = 0; $amount_tax = 0; $amount_paid = 0; // fetch item amounts from DB $sql_obj = new sql_query(); $sql_obj->string = "SELECT amount, type FROM `account_items` WHERE invoicetype='" . $this->type_invoice . "' AND invoiceid='" . $this->id_invoice . "'"; $sql_obj->execute(); if ($sql_obj->num_rows()) { $sql_obj->fetch_array(); foreach ($sql_obj->data as $data_sql) { // total up the different item types if ($data_sql["type"] != "tax" && $data_sql["type"] != "payment") { $amount += $data_sql["amount"]; } if ($data_sql["type"] == "tax") { $amount_tax += $data_sql["amount"]; } if ($data_sql["type"] == "payment") { $amount_paid += $data_sql["amount"]; } } } // final totals $amount_total = $amount + $amount_tax; $amount = sprintf("%0.2f", $amount); $amount_tax = sprintf("%0.2f", $amount_tax); $amount_total = sprintf("%0.2f", $amount_total); $amount_paid = sprintf("%0.2f", $amount_paid); /* Update the invoice */ $sql_obj = new sql_query(); if ($this->type_invoice == "quotes" || $this->type_invoice == "ar_credit" || $this->type_invoice == "ar_credit") { $sql_obj->string = "UPDATE `account_" . $this->type_invoice . "` SET " . "amount='" . $amount . "', " . "amount_tax='" . $amount_tax . "', " . "amount_total='" . $amount_total . "' " . "WHERE id='" . $this->id_invoice . "' LIMIT 1"; } else { $sql_obj->string = "UPDATE `account_" . $this->type_invoice . "` SET " . "amount='" . $amount . "', " . "amount_tax='" . $amount_tax . "', " . "amount_total='" . $amount_total . "', " . "amount_paid='" . $amount_paid . "' " . "WHERE id='" . $this->id_invoice . "' LIMIT 1"; } if (!$sql_obj->execute()) { log_debug("invoice_items", "A fatal SQL error occured whilst attempting to update invoice totals"); return 0; } /* Update the credit (if any) */ if ($this->type_invoice == "ar_credit" || $this->type_invoice == "ap_credit") { $credit = new credit(); $credit->id = $this->id_invoice; $credit->type = $this->type_invoice; $credit->load_data(); $credit->action_update_balance(); } return 1; }