function set_invoice_override_tax($id, $invoicetype, $itemid, $amount)
 {
     log_debug("accounts_invoices_manage", "Executing set_invoice_override_tax({$id}, {$invoicetype}, values...)");
     // check the invoicetype
     if ($invoicetype != "ar" && $invoicetype != "ap") {
         throw new SoapFault("Sender", "INVALID_INVOICE_TYPE");
     }
     if (user_permissions_get("accounts_" . $invoicetype . "_write")) {
         $obj_invoice_item = new invoice_items();
         $obj_invoice_item->type_invoice = $invoicetype;
         $obj_invoice_item->id_invoice = @security_script_input_predefined("int", $id);
         $obj_invoice_item->id_item = @security_script_input_predefined("any", $itemid);
         $obj_invoice_item->type_item = "tax";
         /*
         	Error Handling
         */
         // verify invoice existance
         if (!$obj_invoice_item->verify_invoice()) {
             throw new SoapFault("Sender", "INVALID_INVOICE");
         }
         // make sure invoice is not locked
         if ($obj_invoice_item->check_lock()) {
             throw new SoapFault("Sender", "LOCKED");
         }
         // verify item existance (if editing one)
         if ($obj_invoice_item->id_item) {
             if (!$obj_invoice_item->verify_item()) {
                 throw new SoapFault("Sender", "INVALID_ITEMID");
             }
         }
         // make sure invoice is not locked
         if ($obj_invoice_item->check_lock()) {
             throw new SoapFault("Sender", "LOCKED");
         }
         /*
         	Load tax item data
         
         	We need to load the existing item data, update the amount field and then
         	save it again.
         */
         $obj_invoice_item->load_data();
         /*
         	Load SOAP data
         */
         $obj_invoice_item->data["amount"] = @security_script_input_predefined("money", $amount);
         if ($obj_invoice_item->data["amount"] == "error") {
             throw new SoapFault("Sender", "INVALID_INPUT");
         }
         /*
         	Apply Data
         */
         // start SQL transaction
         $sql_obj = new sql_query();
         $sql_obj->trans_begin();
         // update tax amounts
         if (!$obj_invoice_item->action_update()) {
             throw new SoapFault("Sender", "UNEXPECTED_ACTION_ERROR");
         }
         // Update invoice totals
         $obj_invoice_item->action_update_total();
         // Update ledger
         $obj_invoice_item->action_update_ledger();
         // commit
         if (error_check()) {
             $sql_obj->trans_rollback();
             throw new SoapFault("Sender", "UNEXPECTED_ACTION_ERROR");
         } else {
             $sql_obj->trans_commit();
             return $obj_invoice_item->id_item;
         }
     } else {
         throw new SoapFault("Sender", "ACCESS DENIED");
     }
 }
function invoice_form_tax_override_process($returnpage)
{
    log_debug("inc_invoices_items", "Executing invoice_form_tax_override_process({$returnpage})");
    /*
    	Start invoice_items object
    */
    $item = new invoice_items();
    $item->id_invoice = @security_form_input_predefined("int", "invoiceid", 1, "");
    $item->id_item = @security_form_input_predefined("int", "itemid", 1, "");
    $item->type_invoice = "ap";
    // only AP invoices can have taxes overridden
    /*
    	Fetch all form data
    */
    $data["amount"] = @security_form_input_predefined("money", "amount", 0, "");
    //// ERROR CHECKING ///////////////////////
    /*
    	Verify invoice/form data
    */
    if ($item->verify_invoice()) {
        if (!$item->verify_item()) {
            $_SESSION["error"]["message"][] = "The provided tax does not exist.";
        }
    } else {
        $_SESSION["error"]["message"][] = "The provided invoice does not exist.";
    }
    /// if there was an error, go back to the entry page
    if ($_SESSION["error"]["message"]) {
        $_SESSION["error"]["form"]["ap_invoice_" . $mode . "_override"] = "failed";
        header("Location: ../../index.php?page={$returnpage}&id=" . $item->id_invoice);
        exit(0);
    } else {
        /*
        	Start SQL Transaction
        */
        $sql_obj = new sql_query();
        $sql_obj->trans_begin();
        /*
        	Depending on the amount, we either delete the tax item (if the amount is 0) or we
        	adjust the tax item.
        */
        if ($data["amount"] == 0) {
            // delete item
            $item->action_delete();
            // done
            $_SESSION["notification"]["message"] = array("Deleted unwanted tax.");
        } else {
            // load & update the tax item
            $item->load_data();
            $item->data["amount"] = $data["amount"];
            $item->action_update();
            // done
            $_SESSION["notification"]["message"] = array("Updated tax value with custom input.");
        }
        // update invoice summary
        $item->action_update_total();
        // update ledger
        $item->action_update_ledger();
        /*
        	Commit
        */
        if (error_check()) {
            $sql_obj->trans_rollback();
            log_write("error", "inc_invoice_items", "An error occured whilst overriding tax. No changes have been made");
        } else {
            $sql_obj->trans_commit();
        }
        // done
        header("Location: ../../index.php?page={$returnpage}&id=" . $item->id_invoice);
        exit(0);
    }
}