| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get all accounting reports |
| 4 |
* |
| 5 |
* @return array |
| 6 |
*/ |
| 7 |
function erp_ac_get_reports() { |
| 8 |
|
| 9 |
$reports = [ |
| 10 |
'trial-balance' => [ |
| 11 |
'title' => __( 'Trial Balance', 'erp' ), |
| 12 |
'description' => __( 'Trial balance is the bookkeeping or accounting report that lists the balances in each of general ledger accounts', 'erp' ) |
| 13 |
], |
| 14 |
'sales-tax' => [ |
| 15 |
'title' => __( 'Sales Tax', 'erp' ), |
| 16 |
'description' => __( 'It generates report based on the sales tax charged or paid for the current financial cycle/year.', 'erp' ) |
| 17 |
], |
| 18 |
'income-statement' => [ |
| 19 |
'title' => __( 'Income Statement', 'erp' ), |
| 20 |
'description' => __( 'A summary of a management\'s performance as reflecte the profitability of an organization during the time interval.', 'erp' ) |
| 21 |
], |
| 22 |
'balance-sheet' => [ |
| 23 |
'title' => __( 'Balance Sheet', 'erp' ), |
| 24 |
'description' => __( 'This is a report gives you an immediate status of your accounts at a specified date. You can call it a "Snapshot" view of the current position (day) of the financial year.', 'erp' ) |
| 25 |
], |
| 26 |
// 'profit-loss' => [ |
| 27 |
// 'title' => __( 'Profit and Loss', 'erp' ), |
| 28 |
// 'description' => __( '', 'erp' ) |
| 29 |
// ], |
| 30 |
// 'ar-aging-summary' => [ |
| 31 |
// 'title' => __( 'A/R Aging Summary', 'erp' ), |
| 32 |
// 'description' => __( '', 'erp' ) |
| 33 |
// ], |
| 34 |
// 'company-snapshot' => [ |
| 35 |
// 'title' => __( 'Company Snapshot', 'erp' ), |
| 36 |
// 'description' => __( '', 'erp' ) |
| 37 |
// ], |
| 38 |
|
| 39 |
// 'ap-aging-summary' => [ |
| 40 |
// 'title' => __( 'A/P Aging Summary', 'erp' ), |
| 41 |
// 'description' => __( '', 'erp' ) |
| 42 |
// ], |
| 43 |
// 'cash-flow' => [ |
| 44 |
// 'title' => __( 'Statement of Cash Flows', 'erp' ), |
| 45 |
// 'description' => __( '', 'erp' ) |
| 46 |
// ], |
| 47 |
// 'vendor-balance-summary' => [ |
| 48 |
// 'title' => __( 'Vendor Balance Summary', 'erp' ), |
| 49 |
// 'description' => __( '', 'erp' ) |
| 50 |
// ], |
| 51 |
]; |
| 52 |
|
| 53 |
return apply_filters( 'erp_ac_reports', $reports ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get closing Balnace |
| 58 |
* |
| 59 |
* @since 1.1.9 |
| 60 |
* |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
function erp_ac_get_asset_liability_equity_balance( $financial_end = false ) { |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
$tbl_ledger = $wpdb->prefix . 'erp_ac_ledger'; |
| 67 |
$tbl_type = $wpdb->prefix . 'erp_ac_chart_types'; |
| 68 |
$tbl_class = $wpdb->prefix . 'erp_ac_chart_classes'; |
| 69 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 70 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 71 |
|
| 72 |
if ( $financial_end ) { |
| 73 |
$financial_end = date( 'Y-m-d', strtotime( $financial_end ) ); |
| 74 |
|
| 75 |
} else { |
| 76 |
$financial_end = date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 77 |
} |
| 78 |
|
| 79 |
$sql = $wpdb->prepare( |
| 80 |
"SELECT ledger.id, ledger.code, ledger.name, ledger.type_id, type.name as type_name, type.class_id, class.name as class_name, sum(jour.debit) as debit, sum(jour.credit) as credit |
| 81 |
FROM $tbl_class as class |
| 82 |
LEFT JOIN $tbl_type as type ON type.class_id = class.id |
| 83 |
LEFT JOIN $tbl_ledger as ledger ON ledger.type_id = type.id |
| 84 |
LEFT JOIN $tbl_journals as jour ON jour.ledger_id = ledger.id |
| 85 |
LEFT JOIN $tbl_transaction as trans ON trans.id = jour.transaction_id |
| 86 |
WHERE class.id IN ( 1, 2, 5 ) |
| 87 |
AND ( trans.status IS NULL OR trans.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) |
| 88 |
AND ( trans.issue_date <= '%s' ) |
| 89 |
GROUP BY ledger.id", $financial_end |
| 90 |
); |
| 91 |
|
| 92 |
return $wpdb->get_results( $sql ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get closing debit and credit |
| 97 |
* |
| 98 |
* @since 1.1.9 |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
function erp_ac_get_opening_income_expense( $financial_end = false ) { |
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
$tbl_ledger = $wpdb->prefix . 'erp_ac_ledger'; |
| 106 |
$tbl_type = $wpdb->prefix . 'erp_ac_chart_types'; |
| 107 |
$tbl_class = $wpdb->prefix . 'erp_ac_chart_classes'; |
| 108 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 109 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 110 |
|
| 111 |
if ( $financial_end ) { |
| 112 |
$financial_end = date( 'Y-m-d', strtotime( $financial_end ) ); |
| 113 |
|
| 114 |
} else { |
| 115 |
$financial_end = date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 116 |
} |
| 117 |
|
| 118 |
$financial_start = date( 'Y-m-d', strtotime( erp_financial_start_date() ) ); |
| 119 |
|
| 120 |
$sql = $wpdb->prepare( |
| 121 |
"SELECT ledger.id, ledger.code, ledger.name, ledger.type_id, type.name as type_name, type.class_id, class.name as class_name, sum(jour.debit) as debit, sum(jour.credit) as credit |
| 122 |
FROM $tbl_class as class |
| 123 |
LEFT JOIN $tbl_type as type ON type.class_id = class.id |
| 124 |
LEFT JOIN $tbl_ledger as ledger ON ledger.type_id = type.id |
| 125 |
LEFT JOIN $tbl_journals as jour ON jour.ledger_id = ledger.id |
| 126 |
LEFT JOIN $tbl_transaction as trans ON trans.id = jour.transaction_id |
| 127 |
WHERE class.id IN ( 3, 4 ) |
| 128 |
AND ( trans.status IS NULL OR trans.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) |
| 129 |
AND ( trans.issue_date <= '%s' ) |
| 130 |
GROUP BY ledger.id", $financial_end |
| 131 |
); |
| 132 |
|
| 133 |
return $wpdb->get_results( $sql ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get closing debit and credit |
| 138 |
* |
| 139 |
* @since 1.1.9 |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
function erp_ac_get_closing_income_expense( $financial_end = false ) { |
| 144 |
global $wpdb; |
| 145 |
|
| 146 |
$tbl_ledger = $wpdb->prefix . 'erp_ac_ledger'; |
| 147 |
$tbl_type = $wpdb->prefix . 'erp_ac_chart_types'; |
| 148 |
$tbl_class = $wpdb->prefix . 'erp_ac_chart_classes'; |
| 149 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 150 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 151 |
|
| 152 |
$financial_start = date( 'Y-m-d', strtotime( erp_financial_start_date() ) ); |
| 153 |
if ( $financial_end ) { |
| 154 |
$financial_end = date( 'Y-m-d', strtotime( $financial_end ) ); |
| 155 |
|
| 156 |
if ( $financial_end >= $financial_start ) { |
| 157 |
$financial_end = $financial_start; |
| 158 |
} |
| 159 |
} else { |
| 160 |
$financial_end = $financial_start; |
| 161 |
} |
| 162 |
|
| 163 |
$sql = $wpdb->prepare( |
| 164 |
"SELECT sum(jour.debit) as debit, sum(jour.credit) as credit |
| 165 |
FROM $tbl_class as class |
| 166 |
LEFT JOIN $tbl_type as type ON type.class_id = class.id |
| 167 |
LEFT JOIN $tbl_ledger as ledger ON ledger.type_id = type.id |
| 168 |
LEFT JOIN $tbl_journals as jour ON jour.ledger_id = ledger.id |
| 169 |
LEFT JOIN $tbl_transaction as trans ON trans.id = jour.transaction_id |
| 170 |
WHERE class.id IN ( 3, 4 ) |
| 171 |
AND ( trans.status IS NULL OR trans.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) |
| 172 |
AND ( trans.issue_date < '%s' )", $financial_end |
| 173 |
); |
| 174 |
|
| 175 |
$balance = $wpdb->get_results( $sql ); |
| 176 |
$balance = reset( $balance ); |
| 177 |
|
| 178 |
if ( $balance->credit > $balance->debit ) { |
| 179 |
$balance->credit = abs( $balance->credit - $balance->debit ); |
| 180 |
$balance->debit = abs( 0 ); |
| 181 |
|
| 182 |
} else if ( $balance->credit < $balance->debit ) { |
| 183 |
$balance->debit = abs( $balance->debit - $balance->credit ); |
| 184 |
$balance->credit = abs( 0 ); |
| 185 |
|
| 186 |
} else { |
| 187 |
$balance->debit = abs( 0 ); |
| 188 |
$balance->credit = abs( 0 ); |
| 189 |
} |
| 190 |
|
| 191 |
return $balance; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Transaction report query |
| 196 |
* |
| 197 |
* @param string $financial_end |
| 198 |
* |
| 199 |
* @since 1.0 |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
function erp_ac_reporting_query( $financial_end = false ) { |
| 204 |
$financial_start = date( 'Y-m-d', strtotime( erp_financial_start_date() ) ); |
| 205 |
|
| 206 |
if ( $financial_end ) { |
| 207 |
$financial_end = date( 'Y-m-d', strtotime( $financial_end ) ); |
| 208 |
} else { |
| 209 |
$financial_end = date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 210 |
} |
| 211 |
|
| 212 |
$unit_balance = erp_ac_get_asset_liability_equity_balance( $financial_end ); |
| 213 |
$ope_in_ex_balance = erp_ac_get_opening_income_expense( $financial_end ); |
| 214 |
$report = array_merge( $unit_balance, $ope_in_ex_balance ); |
| 215 |
return $report; |
| 216 |
|
| 217 |
|
| 218 |
if ( $financial_start > $financial_end ) { |
| 219 |
return []; |
| 220 |
} |
| 221 |
|
| 222 |
global $wpdb; |
| 223 |
$tbl_ledger = $wpdb->prefix . 'erp_ac_ledger'; |
| 224 |
$tbl_type = $wpdb->prefix . 'erp_ac_chart_types'; |
| 225 |
$tbl_class = $wpdb->prefix . 'erp_ac_chart_classes'; |
| 226 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 227 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 228 |
$query = []; |
| 229 |
|
| 230 |
$query[] = "tran.issue_date >= '$financial_start'"; |
| 231 |
$query[] = "tran.issue_date <= '$financial_end'"; |
| 232 |
|
| 233 |
$query = $query ? ' AND ' . implode( ' AND ', $query ) : ''; |
| 234 |
$where = "( tran.status IS NULL OR tran.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) AND ( 1=1 $query )"; |
| 235 |
$join = ''; |
| 236 |
$where = apply_filters( 'erp_ac_trial_balance_where', $where ); |
| 237 |
$join = apply_filters( 'erp_ac_trial_balance_join', $join ); |
| 238 |
|
| 239 |
$sql = "SELECT led.id, led.code, led.name, led.type_id, types.name as type_name, types.class_id, class.name as class_name, sum(jour.debit) as debit, sum(jour.credit) as credit |
| 240 |
FROM $tbl_ledger as led |
| 241 |
LEFT JOIN $tbl_type as types ON types.id = led.type_id |
| 242 |
LEFT JOIN $tbl_class as class ON class.id = types.class_id |
| 243 |
LEFT JOIN $tbl_journals as jour ON jour.ledger_id = led.id |
| 244 |
LEFT JOIN $tbl_transaction as tran ON tran.id = jour.transaction_id |
| 245 |
$join |
| 246 |
WHERE |
| 247 |
$where |
| 248 |
GROUP BY led.id"; |
| 249 |
|
| 250 |
return $wpdb->get_results( $sql ); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Get transaction by class id |
| 256 |
* |
| 257 |
* @param array $class_id |
| 258 |
* @param date $financial_start |
| 259 |
* @param date $financial_end |
| 260 |
* |
| 261 |
* @since 1.1.9 |
| 262 |
* |
| 263 |
* @return array |
| 264 |
*/ |
| 265 |
function erp_ac_get_transaction_by_calss_id( $class_id = [], $financial_start = false, $financial_end = false ) { |
| 266 |
global $wpdb; |
| 267 |
|
| 268 |
$cache_key = 'erp-ac-transaction-by-calss-id-' . md5( serialize( $class_id ) ) . $financial_start . $financial_end; |
| 269 |
$items = wp_cache_get( $cache_key, 'erp' ); |
| 270 |
|
| 271 |
if ( false === $items ) { |
| 272 |
$tbl_ledger = $wpdb->prefix . 'erp_ac_ledger'; |
| 273 |
$tbl_type = $wpdb->prefix . 'erp_ac_chart_types'; |
| 274 |
$tbl_class = $wpdb->prefix . 'erp_ac_chart_classes'; |
| 275 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 276 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 277 |
|
| 278 |
$financial_start = $financial_start ? date( 'Y-m-d', strtotime( $financial_start ) ) : date( 'Y-m-d', strtotime( erp_financial_start_date() ) ); |
| 279 |
$financial_end = $financial_end ? date( 'Y-m-d', strtotime( $financial_end ) ) : date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 280 |
|
| 281 |
if ( count( $class_id ) ) { |
| 282 |
$class_id = implode( "','", $class_id ); |
| 283 |
$where = " AND class.id IN ( '$class_id' ) "; |
| 284 |
} else { |
| 285 |
$where = ''; |
| 286 |
} |
| 287 |
|
| 288 |
$sql = $wpdb->prepare( |
| 289 |
"SELECT trans.id as transaction_id, trans.issue_date, trans.status as trans_status, trans.type as trans_type, jour.debit, jour.credit, ledger.id as ledger_id, |
| 290 |
ledger.code, ledger.name as ledger_name, ledger.type_id, type.name as type_name, type.class_id, |
| 291 |
class.name as class_name |
| 292 |
FROM $tbl_class as class |
| 293 |
LEFT JOIN $tbl_type as type ON type.class_id = class.id |
| 294 |
LEFT JOIN $tbl_ledger as ledger ON ledger.type_id = type.id |
| 295 |
LEFT JOIN $tbl_journals as jour ON jour.ledger_id = ledger.id |
| 296 |
LEFT JOIN $tbl_transaction as trans ON trans.id = jour.transaction_id |
| 297 |
WHERE ( trans.status IS NULL OR trans.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) |
| 298 |
AND ( trans.issue_date >= '%s' AND trans.issue_date <= '%s' ) |
| 299 |
$where", $financial_start, $financial_end |
| 300 |
); |
| 301 |
|
| 302 |
$items = $wpdb->get_results( $sql ); |
| 303 |
wp_cache_set( $cache_key, $items, 'erp' ); |
| 304 |
} |
| 305 |
|
| 306 |
return $items; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Get transaction group by class id |
| 311 |
* |
| 312 |
* @param array $class_id |
| 313 |
* @param date $financial_start |
| 314 |
* @param date $financial_end |
| 315 |
* |
| 316 |
* @since 1.1.9 |
| 317 |
* |
| 318 |
* @return array |
| 319 |
*/ |
| 320 |
function erp_ac_get_transaction_group_by_calss_id( $class_id = [], $financial_start = false, $financial_end = false ) { |
| 321 |
$trans = erp_ac_get_transaction_by_calss_id( $class_id, $financial_start = false, $financial_end = false ); |
| 322 |
$group = []; |
| 323 |
|
| 324 |
foreach ( $trans as $tran ) { |
| 325 |
$group[$tran->class_id][] = $tran; |
| 326 |
} |
| 327 |
|
| 328 |
return $group; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Get transaction group by month from class id |
| 333 |
* |
| 334 |
* @param array $class_id |
| 335 |
* @param date $financial_start |
| 336 |
* @param date $financial_end |
| 337 |
* |
| 338 |
* @since 1.1.9 |
| 339 |
* |
| 340 |
* @return array |
| 341 |
*/ |
| 342 |
function erp_ac_get_transaction_group_by_month_from_calss_id( $class_id = [], $financial_start = false, $financial_end = false ) { |
| 343 |
$trans = erp_ac_get_transaction_by_calss_id( $class_id, $financial_start = false, $financial_end = false ); |
| 344 |
$group = []; |
| 345 |
|
| 346 |
foreach ( $trans as $tran ) { |
| 347 |
$date = date( 'm', strtotime( $tran->issue_date ) ); |
| 348 |
$group[$tran->class_id][$date][] = $tran; |
| 349 |
} |
| 350 |
|
| 351 |
return $group; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Tax report query |
| 356 |
* |
| 357 |
* @since 1.1 |
| 358 |
* |
| 359 |
* @param array $args |
| 360 |
* |
| 361 |
* @return array |
| 362 |
*/ |
| 363 |
function erp_ac_get_sales_tax_report( $args ) { |
| 364 |
$all_tax_id = array_keys( erp_ac_get_tax_dropdown() ); |
| 365 |
|
| 366 |
if ( isset( $args['tax_id'] ) && is_array( $args['tax_id'] ) ) { |
| 367 |
$all_tax_id = $args['tax_id']; |
| 368 |
} |
| 369 |
|
| 370 |
$defaults = array( |
| 371 |
'number' => 20, |
| 372 |
'offset' => 0, |
| 373 |
'start' => date( 'Y-m-d', strtotime( erp_financial_start_date() ) ), |
| 374 |
'end' => date( 'Y-m-d', strtotime( erp_financial_end_date() ) ), |
| 375 |
'tax_id' => $all_tax_id |
| 376 |
); |
| 377 |
|
| 378 |
$args = wp_parse_args( $args, $defaults ); |
| 379 |
//$args['start'] = ( $args['start'] && ! empty( $args['start'] ) ) ? $args['start'] : date( 'Y-m-d', strtotime( erp_financial_start_date() ) ); |
| 380 |
$args['end'] = ( $args['end'] && ! empty( $args['end'] ) ) ? $args['end'] : date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 381 |
$cache_key = 'erp-ac-tax-report' . md5( serialize( $args ) ) . md5( serialize( get_current_user_id() ) ); |
| 382 |
$tax_report = wp_cache_get( $cache_key, 'erp' ); |
| 383 |
|
| 384 |
if ( false === $tax_report ) { |
| 385 |
$tax_report = WeDevs\ERP\Accounting\Model\Transaction::with([ 'journals' => function( $q ) use( $args ) { |
| 386 |
return $q->with([ 'ledger' => function( $l ) use( $args ) { |
| 387 |
return $l->whereIn( 'tax', $args['tax_id'] ); |
| 388 |
}]); |
| 389 |
}])//->where( 'issue_date', '>=', $args['start'] ) |
| 390 |
->where( 'issue_date', '<=', $args['end'] ) |
| 391 |
->where( function($q) { |
| 392 |
$q->whereNull( 'status' )->orWhereNotIn( 'status', ['draft', 'void', 'awaiting_approval'] ); |
| 393 |
} ) |
| 394 |
->skip($args['offset']) |
| 395 |
->take($args['number']) |
| 396 |
->get() |
| 397 |
->toArray(); |
| 398 |
|
| 399 |
wp_cache_set( $cache_key, $tax_report, 'erp' ); |
| 400 |
} |
| 401 |
|
| 402 |
return $tax_report; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Tax report count query |
| 407 |
* |
| 408 |
* @param array $args |
| 409 |
* |
| 410 |
* @since 1.1 |
| 411 |
* |
| 412 |
* @return int |
| 413 |
*/ |
| 414 |
function erp_ac_get_sales_tax_report_count( $args = [] ) { |
| 415 |
$all_tax_id = array_keys( erp_ac_get_tax_dropdown() ); |
| 416 |
|
| 417 |
if ( isset( $args['tax_id'] ) && is_array( $args['tax_id'] ) ) { |
| 418 |
$all_tax_id = $args['tax_id']; |
| 419 |
} |
| 420 |
|
| 421 |
$defaults = array( |
| 422 |
'start' => date( 'Y-m-d', strtotime( erp_financial_start_date() ) ), |
| 423 |
'end' => date( 'Y-m-d', strtotime( erp_financial_end_date() ) ), |
| 424 |
'tax_id' => $all_tax_id |
| 425 |
); |
| 426 |
|
| 427 |
$args = wp_parse_args( $args, $defaults ); |
| 428 |
$cache_key = 'erp-ac-tax-report_count' . md5( serialize( $args ) ) . md5( serialize( get_current_user_id() ) ); |
| 429 |
$tax_report = wp_cache_get( $cache_key, 'erp' ); |
| 430 |
|
| 431 |
if ( false === $tax_report ) { |
| 432 |
$tax_report = WeDevs\ERP\Accounting\Model\Transaction::with([ 'journals' => function( $q ) use( $args ) { |
| 433 |
return $q->with([ 'ledger' => function( $l ) use( $args ) { |
| 434 |
return $l->whereIn( 'tax', $args['tax_id'] ); |
| 435 |
}]); |
| 436 |
}])//->where( 'issue_date', '>=', $args['start'] ) |
| 437 |
->where( 'issue_date', '<=', $args['end'] ) |
| 438 |
->where( function($q) { |
| 439 |
$q->whereNull( 'status' )->orWhereNotIn( 'status', ['draft', 'void', 'awaiting_approval'] ); |
| 440 |
}) |
| 441 |
->count(); |
| 442 |
|
| 443 |
wp_cache_set( $cache_key, $tax_report, 'erp' ); |
| 444 |
} |
| 445 |
|
| 446 |
return $tax_report; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Formating tax report query result for individual tax |
| 451 |
* |
| 452 |
* @param array $args [description] |
| 453 |
* |
| 454 |
* @since 1.1.9 |
| 455 |
* |
| 456 |
* @return array |
| 457 |
*/ |
| 458 |
function erp_ac_normarlize_individual_tax( $args = [] ) { |
| 459 |
$tax_id = is_array( $args['tax_id'] ) && count( $args['tax_id'] ) ? reset( $args['tax_id'] ) : false; |
| 460 |
$transactions = erp_ac_get_sales_tax_report( $args ); |
| 461 |
$tax_receivable = erp_ac_get_tax_account_from_tax_id( $tax_id, 'expense' ); |
| 462 |
$tax_payable = erp_ac_get_tax_account_from_tax_id( $tax_id, 'sales' ); |
| 463 |
$tax_unit_info = []; |
| 464 |
|
| 465 |
foreach ( $transactions as $trans ) { |
| 466 |
foreach ( $trans['journals'] as $jour ) { |
| 467 |
$tax_ledger_id = isset( $jour['ledger']['id'] ) ? $jour['ledger']['id'] : false; |
| 468 |
$transaction_id = $trans['id']; |
| 469 |
|
| 470 |
if ( $tax_ledger_id == $tax_receivable ) { |
| 471 |
if ( isset( $tax_unit_info[$transaction_id]['receivable'] ) ) { |
| 472 |
$tax_unit_info[$transaction_id]['receivable'] = $tax_unit_info[$transaction_id]['receivable'] + ( $jour['debit'] - $jour['credit'] ); |
| 473 |
} else { |
| 474 |
$tax_unit_info[$transaction_id]['receivable'] = $jour['debit'] - $jour['credit']; |
| 475 |
} |
| 476 |
|
| 477 |
$tax_unit_info[$transaction_id]['receivable'] = $tax_unit_info[$transaction_id]['receivable']; |
| 478 |
$tax_unit_info[$transaction_id]['issue_date'] = $trans['issue_date']; |
| 479 |
$tax_unit_info[$transaction_id]['transaction_id'] = $transaction_id; |
| 480 |
$tax_unit_info[$transaction_id]['type'] = 'expense'; |
| 481 |
} |
| 482 |
|
| 483 |
if ( $tax_ledger_id == $tax_payable ) { |
| 484 |
if ( isset( $tax_unit_info[$transaction_id]['payable'] ) ) { |
| 485 |
$tax_unit_info[$transaction_id]['payable'] = $tax_unit_info[$transaction_id]['payable'] + ( $jour['credit'] - $jour['debit'] ); |
| 486 |
} else { |
| 487 |
$tax_unit_info[$transaction_id]['payable'] = $jour['credit'] - $jour['debit']; |
| 488 |
} |
| 489 |
|
| 490 |
$tax_unit_info[$transaction_id]['payable'] = $tax_unit_info[$transaction_id]['payable']; |
| 491 |
$tax_unit_info[$transaction_id]['issue_date'] = $trans['issue_date']; |
| 492 |
$tax_unit_info[$transaction_id]['transaction_id'] = $transaction_id; |
| 493 |
$tax_unit_info[$transaction_id]['type'] = 'sales'; |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
return $tax_unit_info; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Formating tax report query result for tax summery |
| 503 |
* |
| 504 |
* @param array $args |
| 505 |
* |
| 506 |
* @since 1.1.9 |
| 507 |
* |
| 508 |
* @return array |
| 509 |
*/ |
| 510 |
function erp_ac_normarlize_tax_from_transaction( $args = [] ) { |
| 511 |
$transactions = erp_ac_get_sales_tax_report( $args ); |
| 512 |
$tax_receivable = wp_list_pluck( erp_ac_get_tax_receivable_ledger(), 'id' ); |
| 513 |
$tax_payable = wp_list_pluck( erp_ac_get_tax_payable_ledger(), 'id' ); |
| 514 |
$tax_info = erp_ac_get_tax_info(); |
| 515 |
$tax_unit_info = []; |
| 516 |
|
| 517 |
|
| 518 |
foreach ( $transactions as $trans ) { |
| 519 |
foreach ( $trans['journals'] as $jour ) { |
| 520 |
$tax_ledger_id = isset( $jour['ledger']['id'] ) ? $jour['ledger']['id'] : false; |
| 521 |
$tax_id = isset( $jour['ledger']['tax'] ) ? $jour['ledger']['tax'] : false; |
| 522 |
|
| 523 |
if ( in_array( $tax_ledger_id, $tax_receivable ) ) { |
| 524 |
if ( isset( $tax_unit_info[$tax_id]['expense']['amount'] ) ) { |
| 525 |
$tax_unit_info[$tax_id]['expense']['amount'] = $tax_unit_info[$tax_id]['expense']['amount'] + ( $jour['debit'] - $jour['credit'] ); |
| 526 |
} else { |
| 527 |
$tax_unit_info[$tax_id]['expense']['amount'] = ( $jour['debit'] - $jour['credit'] ); |
| 528 |
} |
| 529 |
|
| 530 |
$tax_unit_info[$tax_id]['expense']['tax_id'] = $tax_info[$tax_id]['id']; |
| 531 |
$tax_unit_info[$tax_id]['expense']['tax_name'] = $tax_info[$tax_id]['name']; |
| 532 |
$tax_unit_info[$tax_id]['expense']['tax_number'] = $tax_info[$tax_id]['number']; |
| 533 |
$tax_unit_info[$tax_id]['expense']['rate'] = $tax_info[$tax_id]['rate']; |
| 534 |
} |
| 535 |
|
| 536 |
if ( in_array( $tax_ledger_id, $tax_payable ) ) { |
| 537 |
if ( isset( $tax_unit_info[$tax_id]['sales']['amount'] ) ) { |
| 538 |
$tax_unit_info[$tax_id]['sales']['amount'] = $tax_unit_info[$tax_id]['sales']['amount'] + ( $jour['credit'] - $jour['debit'] ); |
| 539 |
} else { |
| 540 |
$tax_unit_info[$tax_id]['sales']['amount'] = ( $jour['credit'] - $jour['debit'] ); |
| 541 |
} |
| 542 |
|
| 543 |
$tax_unit_info[$tax_id]['sales']['tax_id'] = $tax_info[$tax_id]['id']; |
| 544 |
$tax_unit_info[$tax_id]['sales']['tax_name'] = $tax_info[$tax_id]['name']; |
| 545 |
$tax_unit_info[$tax_id]['sales']['tax_number'] = $tax_info[$tax_id]['number']; |
| 546 |
$tax_unit_info[$tax_id]['sales']['rate'] = $tax_info[$tax_id]['rate']; |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
return $tax_unit_info; |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Get total sales amount without tax |
| 556 |
* |
| 557 |
* @param array $charts |
| 558 |
* |
| 559 |
* @since 1.1 |
| 560 |
* |
| 561 |
* @return int |
| 562 |
*/ |
| 563 |
function erp_ac_get_sales_total_without_tax( $charts ) { |
| 564 |
|
| 565 |
$sales_journals = isset( $charts[4] ) ? $charts[4] : []; |
| 566 |
$sales_total = 0; |
| 567 |
|
| 568 |
foreach ( $sales_journals as $key => $ledger_jours ) { |
| 569 |
$sales_total = $sales_total + array_sum( wp_list_pluck( $ledger_jours, 'credit' ) ) - array_sum( wp_list_pluck( $ledger_jours, 'debit' ) ); |
| 570 |
} |
| 571 |
|
| 572 |
return $sales_total; |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Get total sales total amount |
| 577 |
* |
| 578 |
* @param array $charts |
| 579 |
* |
| 580 |
* @since 1.1 |
| 581 |
* |
| 582 |
* @return int |
| 583 |
*/ |
| 584 |
function erp_ac_get_sales_tax_total( $charts ) { |
| 585 |
$payable_tax = erp_ac_get_tax_payable_ledger(); |
| 586 |
$payable_tax = wp_list_pluck( $payable_tax, 'id' ); |
| 587 |
$payable_tax_journals = []; |
| 588 |
$tax_total = 0; |
| 589 |
$libility_payable_tax_journals = isset( $charts[2] ) ? $charts[2] : []; |
| 590 |
|
| 591 |
foreach ( $libility_payable_tax_journals as $key => $libility_journal ) { |
| 592 |
if ( in_array( $key , $payable_tax ) ) { |
| 593 |
$payable_tax_journals[$key] = $libility_journal; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
foreach ( $payable_tax_journals as $key => $ledger_jours ) { |
| 598 |
$tax_total = $tax_total + array_sum( wp_list_pluck( $ledger_jours, 'credit' ) ) - array_sum( wp_list_pluck( $ledger_jours, 'debit' ) ); |
| 599 |
} |
| 600 |
|
| 601 |
return $tax_total; |
| 602 |
} |
| 603 |
|
| 604 |
/** |
| 605 |
* Get cost of good sold amount |
| 606 |
* |
| 607 |
* @param string $charts |
| 608 |
* |
| 609 |
* @since 1.1 |
| 610 |
* |
| 611 |
* @return int |
| 612 |
*/ |
| 613 |
function erp_ac_get_good_sold_total_amount( $financial_end = false ) { |
| 614 |
|
| 615 |
if ( $financial_end ) { |
| 616 |
$financial_end = date( 'Y-m-d', strtotime( $financial_end ) ); |
| 617 |
} else { |
| 618 |
$financial_end = date( 'Y-m-d', strtotime( erp_financial_end_date() ) ); |
| 619 |
} |
| 620 |
|
| 621 |
global $wpdb; |
| 622 |
|
| 623 |
$tbl_journals = $wpdb->prefix . 'erp_ac_journals'; |
| 624 |
$tbl_transaction = $wpdb->prefix . 'erp_ac_transactions'; |
| 625 |
|
| 626 |
$sql = $wpdb->prepare( |
| 627 |
"SELECT trans.id as transaction_id |
| 628 |
FROM $tbl_transaction as trans |
| 629 |
LEFT JOIN $tbl_journals as jour ON jour.transaction_id = trans.id |
| 630 |
WHERE jour.ledger_id = '%d' |
| 631 |
AND ( trans.status IS NULL OR trans.status NOT IN ( 'draft', 'void', 'awaiting_approval' ) ) |
| 632 |
AND ( trans.issue_date < '%s' )", 24, $financial_end |
| 633 |
); |
| 634 |
|
| 635 |
$results = $wpdb->get_results($sql); |
| 636 |
$trans_ids = implode( "','", wp_list_pluck( $results, 'transaction_id' ) ); |
| 637 |
|
| 638 |
$sql = "SELECT sum( jour.debit ) as debit FROM $tbl_journals as jour WHERE jour.transaction_id IN ( '$trans_ids' )"; |
| 639 |
$results = $wpdb->get_var($sql); |
| 640 |
|
| 641 |
return $results; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Get total expense amount without tax |
| 646 |
* |
| 647 |
* @param array $charts |
| 648 |
* |
| 649 |
* @since 1.1 |
| 650 |
* |
| 651 |
* @return int |
| 652 |
*/ |
| 653 |
function erp_ac_get_expense_total_with_tax( $charts ) { |
| 654 |
$expense_journals = isset( $charts[3] ) ? $charts[3] : []; |
| 655 |
$expense_total = 0; |
| 656 |
|
| 657 |
foreach ( $expense_journals as $key => $ledger_jours ) { |
| 658 |
$expense_total = $expense_total + array_sum( wp_list_pluck( $ledger_jours, 'debit' ) ) - array_sum( wp_list_pluck( $ledger_jours, 'credit' ) ); |
| 659 |
} |
| 660 |
return $expense_total; |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Get total expense tax total |
| 666 |
* |
| 667 |
* @param array $charts |
| 668 |
* |
| 669 |
* @since 1.1 |
| 670 |
* |
| 671 |
* @return int |
| 672 |
*/ |
| 673 |
function erp_ac_get_expense_tax_total( $charts ) { |
| 674 |
$expense_journals = isset( $charts[3] ) ? $charts[3] : []; |
| 675 |
$receivable_tax = erp_ac_get_tax_receivable_ledger(); |
| 676 |
$receivable_tax = wp_list_pluck( $receivable_tax, 'id' ); |
| 677 |
$payable_tax_journals = []; |
| 678 |
$expense_tax_total = 0; |
| 679 |
|
| 680 |
foreach ( $expense_journals as $key => $ledger_jours ) { |
| 681 |
if ( in_array( $key, $receivable_tax ) ) { |
| 682 |
$expense_tax_total = $expense_tax_total + array_sum( wp_list_pluck( $ledger_jours, 'debit' ) ) - array_sum( wp_list_pluck( $ledger_jours, 'credit' ) ); |
| 683 |
} |
| 684 |
|
| 685 |
} |
| 686 |
|
| 687 |
return $expense_tax_total; |
| 688 |
} |
| 689 |
|