| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Get all tax agencies |
| 9 |
* |
| 10 |
* @return mixed |
| 11 |
*/ |
| 12 |
function erp_acct_get_all_tax_agencies( $args = [] ) { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$defaults = [ |
| 16 |
'number' => 20, |
| 17 |
'offset' => 0, |
| 18 |
'orderby' => 'id', |
| 19 |
'order' => 'ASC', |
| 20 |
'count' => false, |
| 21 |
's' => '', |
| 22 |
]; |
| 23 |
|
| 24 |
$args = wp_parse_args( $args, $defaults ); |
| 25 |
|
| 26 |
$limit = ''; |
| 27 |
|
| 28 |
if ( -1 !== $args['number'] ) { |
| 29 |
$limit = "LIMIT {$args['number']} OFFSET {$args['offset']}"; |
| 30 |
} |
| 31 |
|
| 32 |
$sql = 'SELECT'; |
| 33 |
$sql .= $args['count'] ? ' COUNT( id ) as total_number ' : ' * '; |
| 34 |
$sql .= "FROM {$wpdb->prefix}erp_acct_tax_agencies ORDER BY {$args['orderby']} {$args['order']} {$limit}"; |
| 35 |
|
| 36 |
if ( $args['count'] ) { |
| 37 |
return $wpdb->get_var( $sql ); |
| 38 |
} |
| 39 |
|
| 40 |
return $wpdb->get_results( $sql, ARRAY_A ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get an single tax agency |
| 45 |
* |
| 46 |
* @param $tax_no |
| 47 |
* |
| 48 |
* @return mixed |
| 49 |
*/ |
| 50 |
function erp_acct_get_tax_agency( $tax_no ) { |
| 51 |
global $wpdb; |
| 52 |
|
| 53 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}erp_acct_tax_agencies WHERE id = %d LIMIT 1", $tax_no ), ARRAY_A ); |
| 54 |
|
| 55 |
return $row; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Insert tax agency |
| 60 |
* |
| 61 |
* @param $data |
| 62 |
* |
| 63 |
* @return int |
| 64 |
*/ |
| 65 |
function erp_acct_insert_tax_agency( $data ) { |
| 66 |
global $wpdb; |
| 67 |
|
| 68 |
$created_by = get_current_user_id(); |
| 69 |
$data['created_at'] = date( 'Y-m-d H:i:s' ); |
| 70 |
$data['created_by'] = $created_by; |
| 71 |
|
| 72 |
$tax_data = erp_acct_get_formatted_tax_data( $data ); |
| 73 |
|
| 74 |
$wpdb->insert( |
| 75 |
$wpdb->prefix . 'erp_acct_tax_agencies', |
| 76 |
[ |
| 77 |
'name' => $tax_data['agency_name'], |
| 78 |
'created_at' => $tax_data['created_at'], |
| 79 |
'created_by' => $tax_data['created_by'], |
| 80 |
'updated_at' => $tax_data['updated_at'], |
| 81 |
'updated_by' => $tax_data['updated_by'], |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
$tax_id = $wpdb->insert_id; |
| 86 |
|
| 87 |
return $tax_id; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Update tax agency |
| 92 |
* |
| 93 |
* @param $data |
| 94 |
* |
| 95 |
* @return int |
| 96 |
*/ |
| 97 |
function erp_acct_update_tax_agency( $data, $id ) { |
| 98 |
global $wpdb; |
| 99 |
|
| 100 |
$updated_by = get_current_user_id(); |
| 101 |
$data['updated_at'] = date( 'Y-m-d H:i:s' ); |
| 102 |
$data['updated_by'] = $updated_by; |
| 103 |
|
| 104 |
$tax_data = erp_acct_get_formatted_tax_data( $data ); |
| 105 |
|
| 106 |
$wpdb->update( |
| 107 |
$wpdb->prefix . 'erp_acct_tax_agencies', |
| 108 |
[ |
| 109 |
'name' => $tax_data['agency_name'], |
| 110 |
'created_at' => $tax_data['created_at'], |
| 111 |
'created_by' => $tax_data['created_by'], |
| 112 |
'updated_at' => $tax_data['updated_at'], |
| 113 |
'updated_by' => $tax_data['updated_by'], |
| 114 |
], |
| 115 |
[ |
| 116 |
'id' => $id, |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
return $id; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Delete an tax agency |
| 125 |
* |
| 126 |
* @param $tax_no |
| 127 |
* |
| 128 |
* @return int |
| 129 |
*/ |
| 130 |
function erp_acct_delete_tax_agency( $id ) { |
| 131 |
global $wpdb; |
| 132 |
|
| 133 |
$wpdb->delete( $wpdb->prefix . 'erp_acct_tax_agencies', [ 'id' => $id ] ); |
| 134 |
|
| 135 |
return $id; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get an single tax agency name |
| 140 |
* |
| 141 |
* @param $tax_no |
| 142 |
* |
| 143 |
* @return mixed |
| 144 |
*/ |
| 145 |
function erp_acct_get_tax_agency_name_by_id( $agency_id ) { |
| 146 |
global $wpdb; |
| 147 |
|
| 148 |
$row = $wpdb->get_row( |
| 149 |
$wpdb->prepare( |
| 150 |
"SELECT name FROM {$wpdb->prefix}erp_acct_tax_agencies WHERE id = %d LIMIT 1", |
| 151 |
$agency_id |
| 152 |
), |
| 153 |
ARRAY_A |
| 154 |
); |
| 155 |
|
| 156 |
return $row['name']; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get agency due amount |
| 161 |
* |
| 162 |
* @param int $agency_id |
| 163 |
* |
| 164 |
* @return mixed |
| 165 |
*/ |
| 166 |
function erp_acct_get_agency_due( $agency_id ) { |
| 167 |
global $wpdb; |
| 168 |
|
| 169 |
return $wpdb->get_var( $wpdb->prepare( "SELECT SUM( credit - debit ) as tax_due From {$wpdb->prefix}erp_acct_tax_agency_details WHERE agency_id = %d", $agency_id ) ); |
| 170 |
} |
| 171 |
|