function delete_invoice_item($itemid)
 {
     log_debug("accounts_invoices_manage", "Executing delete_invoice_itemid({$itemid})");
     // sanatise item ID
     $itemid = @security_script_input_predefined("any", $itemid);
     // fetch the invoice ID and type
     $sql_item_obj = new sql_query();
     $sql_item_obj->string = "SELECT invoiceid, invoicetype FROM account_items WHERE id='" . $itemid . "' LIMIT 1";
     $sql_item_obj->execute();
     if (!$sql_item_obj->num_rows()) {
         throw new SoapFault("Sender", "INVALID_ITEMID");
     }
     $sql_item_obj->fetch_array();
     if (user_permissions_get("accounts_" . $sql_item_obj->data[0]["invoicetype"] . "_write")) {
         $obj_invoice_item = new invoice_items();
         $obj_invoice_item->type_invoice = $sql_item_obj->data[0]["invoicetype"];
         $obj_invoice_item->id_invoice = $sql_item_obj->data[0]["invoiceid"];
         $obj_invoice_item->id_item = $itemid;
         // make sure invoice is not locked
         if ($obj_invoice_item->check_lock()) {
             throw new SoapFault("Sender", "LOCKED");
         }
         /*
         	Perform Changes
         */
         // start SQL transaction
         $sql_obj = new sql_query();
         $sql_obj->trans_begin();
         if (!$obj_invoice_item->action_delete()) {
             $sql_obj->trans_rollback();
             throw new SoapFault("Sender", "UNEXPECTED_ACTION_ERROR");
         }
         // re-calculate taxes, totals and ledgers as required
         $obj_invoice_item->action_update_tax();
         $obj_invoice_item->action_update_total();
         $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 1;
         }
     } else {
         throw new SoapFault("Sender", "ACCESS DENIED");
     }
 }
Example #2
0
 function action_delete()
 {
     log_debug("invoice", "Executing action_delete()");
     // we must have an ID provided
     if (!$this->id) {
         log_debug("invoice", "No invoice ID supplied to action_delete function");
         return 0;
     }
     /*
     	Start SQL Transaction
     */
     $sql_obj = new sql_query();
     $sql_obj->trans_begin();
     /*
     	Delete Invoice
     */
     $sql_obj->string = "DELETE FROM account_" . $this->type . " WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     /*
     	Delete Invoice Items
     
     	We do this by using the invoice_items::action_delete() function, since there are number of complex
     	steps when deleting certain invoice items (such as time items)
     */
     $sql_items_obj = new sql_query();
     $sql_items_obj->string = "SELECT id FROM account_items WHERE invoicetype='" . $this->type . "' AND invoiceid='" . $this->id . "'";
     $sql_items_obj->execute();
     if ($sql_items_obj->num_rows()) {
         $sql_items_obj->fetch_array();
         foreach ($sql_items_obj->data as $data_sql) {
             // delete each invoice one-at-a-time.
             $obj_invoice_item = new invoice_items();
             $obj_invoice_item->type_invoice = $this->type;
             $obj_invoice_item->id_invoice = $this->id;
             $obj_invoice_item->id_item = $data_sql["id"];
             $obj_invoice_item->action_delete();
             unset($obj_invoice_item);
         }
     }
     /*
     	Delete Journal
     */
     journal_delete_entire("account_" . $this->type . "", $this->id);
     /*
     	Delete transactions from ledger
     	
     	(Most transactions are deleted by the item deletion code, but tax, pay and AR/AP
     	 ledger transactions need to be removed manually)
     */
     $sql_obj->string = "DELETE FROM account_trans WHERE (type='" . $this->type . "' || type='" . $this->type . "_tax' || type='" . $this->type . "_pay') AND customid='" . $this->id . "'";
     $sql_obj->execute();
     /*
     	Commit
     */
     if (error_check()) {
         $sql_obj->trans_rollback();
         log_write("error", "invoice", "An error occured whilst deleting the invoice. No changes have been made.");
         return 0;
     } else {
         $sql_obj->trans_commit();
         return 1;
     }
 }
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);
    }
}