Skip to content

Instantly share code, notes, and snippets.

@midoalone
Created January 14, 2022 09:05
Show Gist options
  • Save midoalone/a13d1aece2316b7a880442ea58099b39 to your computer and use it in GitHub Desktop.
Save midoalone/a13d1aece2316b7a880442ea58099b39 to your computer and use it in GitHub Desktop.
This file is the recommended way to add custom helper functions on perfex CRM. Here I added some functions to manibulate data on invoice module based on client's requirements.
<?php
/* TAWFIK */
/* Custom helper functions */
// Check if invoice can be edited
function is_invoice_editable( $id ) {
if(get_option('disable_invoice_edit') == 1) {
return check_invoice_is_draft( $id ) == true;
}
return true;
}
// Check if invoice status is draft
function check_invoice_is_draft( $id ): bool {
$CI = &get_instance();
$invoice = $CI->db->where( "id", $id )->get( db_prefix() . 'invoices' )->row_array();
if ( $invoice['status'] == 6 ) {
return true;
} else {
return false;
}
}
// Get invoice item tax rate
function get_item_tax_rate( $id ) {
$CI = &get_instance();
$tax = $CI->db->where( "itemid", $id )->get( db_prefix() . 'item_tax' )->row_array();
return $tax ? (float) $tax['taxrate'] : 0;
}
// Calculate item tax value
function calculate_item_tax( $item ) {
// Tax rate
$tax_rate = get_item_tax_rate( $item['id'] );
// Item amount
$amount = $item['qty'] * $item['rate'];
// Calculate tax
return ( $tax_rate * $amount ) / 100;
}
// Check if invoice number can be edited
function is_invoice_number_disabled() {
if(get_option('disable_invoice_number_edit')) {
return true;
}
return false;
}
// Check if invoice date can be edited
function is_invoice_date_disabled() {
if(get_option('disable_invoice_date_edit')) {
return true;
}
return false;
}
// Custom function for rate field decimals
function get_rate_decimals_place() {
return 5;
}
// Filter function to add tax value beside tax percentage
hooks()->add_filter( 'item_tax_table_row', 'modify_invoice_item_tax', 10, 2 );
function modify_invoice_item_tax( $item_tax, $item ) {
$tax_value = calculate_item_tax( $item );
// Number format
$tax_value = number_format($tax_value, 2);
return "$item_tax ($tax_value)";
}
// Filter function to change invoice description column width
hooks()->add_filter( 'item_description_td_width', 'modify_item_description_td_width', 10, 1 );
function modify_item_description_td_width( $width ) {
return 25;
}
// Filter function to update decimal places to 5 instead of 2
hooks()->add_filter( 'app_decimal_places', 'modify_app_decimal_places', 10, 1 );
function modify_app_decimal_places( $decimals ) {
return 2;
}
// Filter function to format item rate
hooks()->add_filter( 'item_preview_rate', 'modify_item_preview_rate', 10, 1 );
function modify_item_preview_rate( $rate ) {
return floatval($rate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment