| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
require_once ERP_ACCOUNTING_INCLUDES . '/functions/reports/trial-balance.php'; |
| 8 |
|
| 9 |
/** |
| 10 |
* =================================================== |
| 11 |
* Ledger Report |
| 12 |
* =================================================== |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* get ledger report |
| 17 |
* |
| 18 |
* @param int $ledger_id |
| 19 |
* @param string $start_date |
| 20 |
* @param string $end_date |
| 21 |
* |
| 22 |
* @return mixed |
| 23 |
*/ |
| 24 |
function erp_acct_get_ledger_report( $ledger_id, $start_date, $end_date ) { |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
// get closest financial year id and start date |
| 28 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $start_date ); |
| 29 |
|
| 30 |
// get opening balance data within that(^) financial year |
| 31 |
$opening_balance = (float) erp_acct_ledger_report_opening_balance_by_fn_year_id( $closest_fy_date['id'], $ledger_id ); |
| 32 |
|
| 33 |
// should we go further calculation, check the diff |
| 34 |
if ( erp_acct_has_date_diff( $start_date, $closest_fy_date['start_date'] ) ) { |
| 35 |
$prev_date_of_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $start_date ) ) ); |
| 36 |
|
| 37 |
$sql1 = $wpdb->prepare( |
| 38 |
"SELECT SUM(debit - credit) AS balance |
| 39 |
FROM {$wpdb->prefix}erp_acct_ledger_details |
| 40 |
WHERE ledger_id = %d AND trn_date BETWEEN '%s' AND '%s' ORDER BY trn_date ASC", |
| 41 |
$ledger_id, |
| 42 |
$closest_fy_date['start_date'], |
| 43 |
$prev_date_of_start |
| 44 |
); |
| 45 |
|
| 46 |
$prev_ledger_details = $wpdb->get_var( $sql1 ); |
| 47 |
$opening_balance += (float) $prev_ledger_details; |
| 48 |
} |
| 49 |
|
| 50 |
$raw_opening_balance = $opening_balance; |
| 51 |
|
| 52 |
// ledger details |
| 53 |
$sql2 = $wpdb->prepare( |
| 54 |
"SELECT |
| 55 |
trn_no, particulars, debit, credit, trn_date, created_at |
| 56 |
FROM {$wpdb->prefix}erp_acct_ledger_details |
| 57 |
WHERE ledger_id = %d AND trn_date BETWEEN '%s' AND '%s' ORDER BY trn_date ASC", |
| 58 |
$ledger_id, |
| 59 |
$start_date, |
| 60 |
$end_date |
| 61 |
); |
| 62 |
|
| 63 |
$details = $wpdb->get_results( $sql2, ARRAY_A ); |
| 64 |
|
| 65 |
$total_debit = 0; |
| 66 |
$total_credit = 0; |
| 67 |
|
| 68 |
foreach ( $details as $key => $detail ) { |
| 69 |
$total_debit += (float) $detail['debit']; |
| 70 |
$total_credit += (float) $detail['credit']; |
| 71 |
|
| 72 |
if ( '0.00' === $detail['debit'] ) { |
| 73 |
// so we're working with credit |
| 74 |
$opening_balance = $opening_balance + ( - (float) $detail['credit'] ); |
| 75 |
|
| 76 |
// after calculation with credit |
| 77 |
if ( $opening_balance >= 0 ) { |
| 78 |
// opening balance is positive |
| 79 |
$details[ $key ]['balance'] = $opening_balance . ' Dr'; |
| 80 |
} elseif ( $opening_balance < 0 ) { |
| 81 |
// opening balance is negative |
| 82 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( '0.00' === $detail['credit'] ) { |
| 87 |
// so we're working with debit |
| 88 |
$opening_balance = $opening_balance + (float) $detail['debit']; |
| 89 |
|
| 90 |
// after calculation with debit |
| 91 |
if ( $opening_balance >= 0 ) { |
| 92 |
// opening balance is positive |
| 93 |
$details[ $key ]['balance'] = $opening_balance . ' Dr'; |
| 94 |
} elseif ( $opening_balance < 0 ) { |
| 95 |
// opening balance is negative |
| 96 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Assign opening balance as first row |
| 102 |
if ( (float) $raw_opening_balance > 0 ) { |
| 103 |
$balance = $raw_opening_balance . ' Dr'; |
| 104 |
} elseif ( (float) $raw_opening_balance < 0 ) { |
| 105 |
$balance = abs( $raw_opening_balance ) . ' Cr'; |
| 106 |
} else { |
| 107 |
$balance = '0 Dr'; |
| 108 |
} |
| 109 |
|
| 110 |
array_unshift( |
| 111 |
$details, |
| 112 |
[ |
| 113 |
'trn_no' => null, |
| 114 |
'particulars' => 'Opening Balance =', |
| 115 |
'debit' => null, |
| 116 |
'credit' => null, |
| 117 |
'trn_date' => $start_date, |
| 118 |
'balance' => $balance, |
| 119 |
'created_at' => null, |
| 120 |
] |
| 121 |
); |
| 122 |
|
| 123 |
return [ |
| 124 |
'details' => $details, |
| 125 |
'extra' => [ |
| 126 |
'total_debit' => $total_debit, |
| 127 |
'total_credit' => $total_credit, |
| 128 |
], |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Ledger report opening balance helper |
| 134 |
* |
| 135 |
* @param $id |
| 136 |
* @param $ledger_id |
| 137 |
* |
| 138 |
* @return string|null |
| 139 |
*/ |
| 140 |
function erp_acct_ledger_report_opening_balance_by_fn_year_id( $id, $ledger_id ) { |
| 141 |
global $wpdb; |
| 142 |
|
| 143 |
$sql = "SELECT SUM(debit - credit) AS balance FROM {$wpdb->prefix}erp_acct_opening_balances |
| 144 |
WHERE financial_year_id = %d AND ledger_id = %d AND type = 'ledger' GROUP BY ledger_id"; |
| 145 |
|
| 146 |
return $wpdb->get_var( $wpdb->prepare( $sql, $id, $ledger_id ) ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* =================================================== |
| 151 |
* Sales Tax Report |
| 152 |
* =================================================== |
| 153 |
*/ |
| 154 |
|
| 155 |
/** |
| 156 |
* get sales tax report |
| 157 |
* |
| 158 |
* @param int $agency_id |
| 159 |
* @param string $start_date |
| 160 |
* @param string $end_date |
| 161 |
* |
| 162 |
* @return mixed |
| 163 |
*/ |
| 164 |
function erp_acct_get_sales_tax_report( $agency_id, $start_date, $end_date ) { |
| 165 |
global $wpdb; |
| 166 |
|
| 167 |
// opening balance |
| 168 |
$sql1 = $wpdb->prepare( |
| 169 |
"SELECT SUM(debit - credit) AS opening_balance |
| 170 |
FROM {$wpdb->prefix}erp_acct_tax_agency_details |
| 171 |
WHERE agency_id = %d AND trn_date < '%s'", |
| 172 |
$agency_id, |
| 173 |
$start_date |
| 174 |
); |
| 175 |
|
| 176 |
$db_opening_balance = $wpdb->get_var( $sql1 ); |
| 177 |
$opening_balance = (float) $db_opening_balance; |
| 178 |
|
| 179 |
// agency details |
| 180 |
$details = $wpdb->get_results( $wpdb->prepare( "SELECT trn_no, particulars, debit, credit, trn_date, created_at FROM {$wpdb->prefix}erp_acct_tax_agency_details WHERE agency_id = %d AND trn_date BETWEEN '%s' AND '%s'", $agency_id, $start_date, $end_date ), ARRAY_A ); |
| 181 |
|
| 182 |
$total_debit = 0; |
| 183 |
$total_credit = 0; |
| 184 |
|
| 185 |
// Please refactor me |
| 186 |
foreach ( $details as $key => $detail ) { |
| 187 |
$total_debit += (float) $detail['debit']; |
| 188 |
$total_credit += (float) $detail['credit']; |
| 189 |
|
| 190 |
if ( '0.00' === $detail['debit'] ) { |
| 191 |
// so we're working with credit |
| 192 |
if ( $opening_balance < 0 ) { |
| 193 |
// opening balance is negative |
| 194 |
$opening_balance = $opening_balance + ( - (float) $detail['credit'] ); |
| 195 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 196 |
} elseif ( $opening_balance >= 0 ) { |
| 197 |
// opening balance is positive |
| 198 |
$opening_balance = $opening_balance + ( - (float) $detail['credit'] ); |
| 199 |
|
| 200 |
// after calculation with credit |
| 201 |
if ( $opening_balance >= 0 ) { |
| 202 |
$details[ $key ]['balance'] = $opening_balance . ' Dr'; |
| 203 |
} elseif ( $opening_balance < 0 ) { |
| 204 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 205 |
} |
| 206 |
} else { |
| 207 |
// opening balance is 0 |
| 208 |
$details[ $key ]['balance'] = '0 Dr'; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ( '0.00' === $detail['credit'] ) { |
| 213 |
// so we're working with debit |
| 214 |
|
| 215 |
if ( $opening_balance < 0 ) { |
| 216 |
// opening balance is negative |
| 217 |
$opening_balance = $opening_balance + (float) $detail['debit']; |
| 218 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 219 |
} elseif ( $opening_balance >= 0 ) { |
| 220 |
// opening balance is positive |
| 221 |
$opening_balance = $opening_balance + (float) $detail['debit']; |
| 222 |
|
| 223 |
// after calculation with debit |
| 224 |
if ( $opening_balance >= 0 ) { |
| 225 |
$details[ $key ]['balance'] = $opening_balance . ' Dr'; |
| 226 |
} elseif ( $opening_balance < 0 ) { |
| 227 |
$details[ $key ]['balance'] = abs( $opening_balance ) . ' Cr'; |
| 228 |
} |
| 229 |
} else { |
| 230 |
// opening balance is 0 |
| 231 |
$details[ $key ]['balance'] = '0 Dr'; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// Assign opening balance as first row |
| 237 |
if ( (float) $db_opening_balance > 0 ) { |
| 238 |
$balance = $db_opening_balance . ' Dr'; |
| 239 |
} elseif ( (float) $db_opening_balance < 0 ) { |
| 240 |
$balance = abs( $db_opening_balance ) . ' Cr'; |
| 241 |
} else { |
| 242 |
$balance = '0 Dr'; |
| 243 |
} |
| 244 |
|
| 245 |
array_unshift( |
| 246 |
$details, |
| 247 |
[ |
| 248 |
'trn_no' => null, |
| 249 |
'particulars' => 'Opening Balance =', |
| 250 |
'debit' => null, |
| 251 |
'credit' => null, |
| 252 |
'trn_date' => $start_date, |
| 253 |
'balance' => $balance, |
| 254 |
'created_at' => null, |
| 255 |
] |
| 256 |
); |
| 257 |
|
| 258 |
return [ |
| 259 |
'details' => $details, |
| 260 |
'extra' => [ |
| 261 |
'total_debit' => $total_debit, |
| 262 |
'total_credit' => $total_credit, |
| 263 |
], |
| 264 |
]; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* =================================================== |
| 269 |
* Income Statement |
| 270 |
* =================================================== |
| 271 |
*/ |
| 272 |
|
| 273 |
/** |
| 274 |
* Get income statement |
| 275 |
*/ |
| 276 |
function erp_acct_get_income_statement( $args ) { |
| 277 |
global $wpdb; |
| 278 |
|
| 279 |
$results = erp_acct_get_profit_loss( $args ); |
| 280 |
|
| 281 |
if ( $results['income'] >= abs( $results['expense'] ) ) { |
| 282 |
$results['profit'] = $results['income'] - $results['expense']; |
| 283 |
$results['raw_balance'] = $results['profit']; |
| 284 |
} else { |
| 285 |
$results['loss'] = $results['income'] - $results['expense']; |
| 286 |
$results['raw_balance'] = $results['loss']; |
| 287 |
} |
| 288 |
|
| 289 |
$results['balance'] = isset( $results['profit'] ) ? $results['profit'] : $results['loss']; |
| 290 |
|
| 291 |
return $results; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Income statement with opening balance helper |
| 296 |
* |
| 297 |
* @param $bs_start_date |
| 298 |
* @param $data |
| 299 |
* @param $sql |
| 300 |
* @param $chart_id |
| 301 |
* |
| 302 |
* @return array |
| 303 |
*/ |
| 304 |
function erp_acct_income_statement_calculate_with_opening_balance( $is_start_date, $data, $sql, $chart_id ) { |
| 305 |
global $wpdb; |
| 306 |
|
| 307 |
// get closest financial year id and start date |
| 308 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $is_start_date ); |
| 309 |
|
| 310 |
// get opening balance data within that(^) financial year |
| 311 |
$opening_balance = erp_acct_is_opening_balance_by_fn_year_id( $closest_fy_date['id'], $chart_id ); |
| 312 |
|
| 313 |
$ledgers = $wpdb->get_results( $wpdb->prepare( "SELECT ledger.id, ledger.name FROM {$wpdb->prefix}erp_acct_ledgers AS ledger WHERE ledger.chart_id = %d", $chart_id ), ARRAY_A ); |
| 314 |
$temp_data = erp_acct_get_is_balance_with_opening_balance( $ledgers, $data, $opening_balance ); |
| 315 |
$result = []; |
| 316 |
|
| 317 |
if ( ! erp_acct_has_date_diff( $is_start_date, $closest_fy_date['start_date'] ) ) { |
| 318 |
return $temp_data; |
| 319 |
} else { |
| 320 |
$prev_date_of_tb_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $is_start_date ) ) ); |
| 321 |
} |
| 322 |
|
| 323 |
// should we go further calculation, check the diff |
| 324 |
$date1 = date_create( $is_start_date ); |
| 325 |
$date2 = date_create( $closest_fy_date['start_date'] ); |
| 326 |
$interval = date_diff( $date1, $date2 ); |
| 327 |
|
| 328 |
// if difference is `0` OR `1` day |
| 329 |
if ( '2' > $interval->format( '%a' ) ) { |
| 330 |
return $temp_data; |
| 331 |
} else { |
| 332 |
// get previous date from balance sheet start date |
| 333 |
$date_before_balance_sheet_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $is_start_date ) ) ); |
| 334 |
$is_date = $date_before_balance_sheet_start; |
| 335 |
} |
| 336 |
|
| 337 |
// get ledger details data between `financial year start date` and `previous date from balance sheet start date` |
| 338 |
$ledger_details = $wpdb->get_results( |
| 339 |
$wpdb->prepare( $sql, $closest_fy_date['start_date'], $is_date ), |
| 340 |
ARRAY_A |
| 341 |
); |
| 342 |
|
| 343 |
foreach ( $temp_data as $temp ) { |
| 344 |
$balance = $temp['balance']; |
| 345 |
|
| 346 |
foreach ( $ledger_details as $detail ) { |
| 347 |
if ( $temp['id'] === $detail['id'] ) { |
| 348 |
$balance += (float) $detail['balance']; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
$result[] = [ |
| 353 |
'id' => $temp['id'], |
| 354 |
'name' => $temp['name'], |
| 355 |
'balance' => $balance, |
| 356 |
]; |
| 357 |
} |
| 358 |
|
| 359 |
return $result; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Get income statement ledger balance with opening balance |
| 364 |
* |
| 365 |
* @param array $ledgers |
| 366 |
* @param array $data |
| 367 |
* @param array $opening_balance |
| 368 |
* |
| 369 |
* @return array |
| 370 |
*/ |
| 371 |
function erp_acct_get_is_balance_with_opening_balance( $ledgers, $data, $opening_balance ) { |
| 372 |
$temp_data = []; |
| 373 |
|
| 374 |
foreach ( $ledgers as $ledger ) { |
| 375 |
$balance = 0; |
| 376 |
|
| 377 |
foreach ( $data as $row ) { |
| 378 |
if ( $row['balance'] && $row['id'] === $ledger['id'] ) { |
| 379 |
$balance += (float) abs( $row['balance'] ); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
foreach ( $opening_balance as $op_balance ) { |
| 384 |
if ( $op_balance['id'] === $ledger['id'] ) { |
| 385 |
$balance += (float) abs( $op_balance['balance'] ); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
if ( $balance ) { |
| 390 |
$temp_data[] = [ |
| 391 |
'id' => $ledger['id'], |
| 392 |
'name' => $ledger['name'], |
| 393 |
'balance' => $balance, |
| 394 |
]; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return $temp_data; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Get income statement opening balance data by financial year id |
| 403 |
* |
| 404 |
* @param int $id |
| 405 |
* @param int $chart_id ( optional ) |
| 406 |
* |
| 407 |
* @return array |
| 408 |
*/ |
| 409 |
function erp_acct_is_opening_balance_by_fn_year_id( $id, $chart_id ) { |
| 410 |
global $wpdb; |
| 411 |
|
| 412 |
$where = ''; |
| 413 |
|
| 414 |
if ( $chart_id ) { |
| 415 |
$where = $wpdb->prepare( 'AND ledger.chart_id = %d', $chart_id ); |
| 416 |
} |
| 417 |
|
| 418 |
$sql = "SELECT ledger.id, ledger.name, SUM(opb.debit - opb.credit) AS balance |
| 419 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 420 |
LEFT JOIN {$wpdb->prefix}erp_acct_opening_balances AS opb ON ledger.id = opb.ledger_id |
| 421 |
WHERE opb.financial_year_id = %d {$where} AND opb.type = 'ledger' AND ledger.slug <> 'owner_s_equity' |
| 422 |
GROUP BY opb.ledger_id"; |
| 423 |
|
| 424 |
return $wpdb->get_results( $wpdb->prepare( $sql, $id ), ARRAY_A ); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* =================================================== |
| 429 |
* Balance Sheet |
| 430 |
* =================================================== |
| 431 |
*/ |
| 432 |
|
| 433 |
/** |
| 434 |
* Get balance sheet |
| 435 |
* |
| 436 |
* @param $args |
| 437 |
* |
| 438 |
* @return mixed |
| 439 |
*/ |
| 440 |
function erp_acct_get_balance_sheet( $args ) { |
| 441 |
global $wpdb; |
| 442 |
|
| 443 |
if ( empty( $args['start_date'] ) ) { |
| 444 |
$args['start_date'] = date( 'Y-m-d', strtotime( 'first day of this month' ) ); |
| 445 |
} |
| 446 |
|
| 447 |
if ( empty( $args['end_date'] ) ) { |
| 448 |
$args['end_date'] = date( 'Y-m-d', strtotime( 'last day of this month' ) ); |
| 449 |
} |
| 450 |
|
| 451 |
if ( empty( $args['start_date'] ) && empty( $args['end_date'] ) ) { |
| 452 |
$args['start_date'] = date( 'Y-m-d', strtotime( 'first day of this month' ) ); |
| 453 |
$args['end_date'] = date( 'Y-m-d', strtotime( 'last day of this month' ) ); |
| 454 |
} |
| 455 |
|
| 456 |
$sql1 = "SELECT |
| 457 |
ledger.id, |
| 458 |
ledger.name, |
| 459 |
SUM(ledger_detail.debit - ledger_detail.credit) AS balance |
| 460 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 461 |
LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details AS ledger_detail ON ledger.id = ledger_detail.ledger_id WHERE ledger.chart_id=1 AND ledger_detail.trn_date BETWEEN '%s' AND '%s' |
| 462 |
GROUP BY ledger_detail.ledger_id"; |
| 463 |
|
| 464 |
$sql2 = "SELECT |
| 465 |
ledger.id, |
| 466 |
ledger.name, |
| 467 |
SUM(ledger_detail.debit - ledger_detail.credit) AS balance |
| 468 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 469 |
LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details AS ledger_detail ON ledger.id = ledger_detail.ledger_id WHERE ledger.chart_id=2 AND ledger_detail.trn_date BETWEEN '%s' AND '%s' |
| 470 |
GROUP BY ledger_detail.ledger_id"; |
| 471 |
|
| 472 |
$sql3 = "SELECT |
| 473 |
ledger.id, |
| 474 |
ledger.name, |
| 475 |
SUM(ledger_detail.debit - ledger_detail.credit) AS balance |
| 476 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 477 |
LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details AS ledger_detail ON ledger.id = ledger_detail.ledger_id WHERE ledger.chart_id=3 AND ledger.slug <> 'owner_s_equity' AND ledger_detail.trn_date BETWEEN '%s' AND '%s' |
| 478 |
GROUP BY ledger_detail.ledger_id"; |
| 479 |
|
| 480 |
$data1 = $wpdb->get_results( $wpdb->prepare( $sql1, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 481 |
$data2 = $wpdb->get_results( $wpdb->prepare( $sql2, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 482 |
$data3 = $wpdb->get_results( $wpdb->prepare( $sql3, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 483 |
|
| 484 |
$results['rows1'] = erp_acct_balance_sheet_calculate_with_opening_balance( $args['start_date'], $data1, $sql1, 1 ); |
| 485 |
$results['rows2'] = erp_acct_balance_sheet_calculate_with_opening_balance( $args['start_date'], $data2, $sql2, 2 ); |
| 486 |
$results['rows3'] = erp_acct_balance_sheet_calculate_with_opening_balance( $args['start_date'], $data3, $sql3, 3 ); |
| 487 |
|
| 488 |
$final_accounts_helper = new \WeDevs\ERP\Accounting\Includes\Classes\FinalAccountsHelper($args); |
| 489 |
|
| 490 |
$results['rows1'][] = [ |
| 491 |
'name' => 'Accounts Receivable', |
| 492 |
'balance' => erp_acct_get_account_receivable( $args ), |
| 493 |
]; |
| 494 |
$results['rows1'][] = [ |
| 495 |
'name' => 'Cash at Bank', |
| 496 |
'balance' => $final_accounts_helper->totalCashAtBank, //erp_acct_cash_at_bank( $args, 'balance' ), |
| 497 |
'additional' => $final_accounts_helper->cashAtBankBreakdowns , //erp_acct_bank_balance( $args, 'balance' ), |
| 498 |
]; |
| 499 |
|
| 500 |
$results['rows2'][] = [ |
| 501 |
'name' => 'Accounts Payable', |
| 502 |
'balance' => erp_acct_get_account_payable( $args ), |
| 503 |
]; |
| 504 |
|
| 505 |
$results['rows2'][] = [ |
| 506 |
'name' => 'Bank Loan', |
| 507 |
'balance' => $final_accounts_helper->totalLoanAtBank, //erp_acct_cash_at_bank( $args, 'loan' ), |
| 508 |
'additional' => $final_accounts_helper->loanAtBankBreakdowns, //erp_acct_bank_balance( $args, 'loan' ), |
| 509 |
]; |
| 510 |
|
| 511 |
$results['rows2'][] = [ |
| 512 |
'name' => 'Sales Tax Payable', |
| 513 |
'slug' => 'sales_tax', |
| 514 |
'balance' => erp_acct_sales_tax_query( $args, 'payable' ), |
| 515 |
]; |
| 516 |
|
| 517 |
$ledger_map = \WeDevs\ERP\Accounting\Includes\Classes\Ledger_Map::get_instance(); |
| 518 |
$owner_s_equity_id = $ledger_map->get_ledger_id_by_slug( 'owner_s_equity' ); |
| 519 |
|
| 520 |
$capital = erp_acct_get_owners_equity( $args, 'capital' ); |
| 521 |
$drawings = erp_acct_get_owners_equity( $args, 'drawings' ); |
| 522 |
$new_capital = $capital + $drawings; |
| 523 |
|
| 524 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $args['start_date'] ); |
| 525 |
$prev_date_of_tb_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $args['start_date'] ) ) ); |
| 526 |
|
| 527 |
// Owner's Equity calculation with income statement profit/loss |
| 528 |
$inc_statmnt_range = [ |
| 529 |
'start_date' => $closest_fy_date['start_date'], |
| 530 |
'end_date' => $prev_date_of_tb_start, |
| 531 |
]; |
| 532 |
|
| 533 |
$income_statement_balance = erp_acct_get_income_statement( $inc_statmnt_range ); |
| 534 |
|
| 535 |
$new_capital = $new_capital - $income_statement_balance['raw_balance']; |
| 536 |
|
| 537 |
if ( 0 < $new_capital ) { |
| 538 |
$results['rows3'][] = [ |
| 539 |
'id' => $owner_s_equity_id, |
| 540 |
'name' => 'Owner\'s Drawings', |
| 541 |
'balance' => $new_capital, |
| 542 |
]; |
| 543 |
} else { |
| 544 |
$results['rows3'][] = [ |
| 545 |
'id' => $owner_s_equity_id, |
| 546 |
'name' => 'Owner\'s Capital', |
| 547 |
'balance' => $new_capital, |
| 548 |
]; |
| 549 |
} |
| 550 |
|
| 551 |
$profit_loss = erp_acct_get_income_statement( $args ); |
| 552 |
|
| 553 |
if ( ! empty( $profit_loss['profit'] ) ) { |
| 554 |
$results['rows3'][] = [ |
| 555 |
'name' => 'Profit', |
| 556 |
'slug' => 'profit', |
| 557 |
'balance' => -$profit_loss['profit'], |
| 558 |
]; |
| 559 |
} |
| 560 |
|
| 561 |
if ( ! empty( $profit_loss['loss'] ) ) { |
| 562 |
$results['rows3'][] = [ |
| 563 |
'name' => 'Loss', |
| 564 |
'slug' => 'loss', |
| 565 |
'balance' => -$profit_loss['loss'], |
| 566 |
]; |
| 567 |
} |
| 568 |
|
| 569 |
$results['total_asset'] = 0; |
| 570 |
$results['total_equity'] = 0; |
| 571 |
$results['total_liability'] = 0; |
| 572 |
|
| 573 |
foreach ( $results['rows1'] as $result ) { |
| 574 |
if ( ! is_numeric( $result['balance'] ) ) { |
| 575 |
continue; |
| 576 |
} |
| 577 |
|
| 578 |
if ( ! empty( $result['balance'] ) ) { |
| 579 |
$results['total_asset'] += (float) $result['balance']; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
foreach ( $results['rows2'] as $result ) { |
| 584 |
if ( ! is_numeric( $result['balance'] ) ) { |
| 585 |
continue; |
| 586 |
} |
| 587 |
|
| 588 |
if ( ! empty( $result['balance'] ) ) { |
| 589 |
$results['total_liability'] += (float) $result['balance']; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
foreach ( $results['rows3'] as $result ) { |
| 594 |
if ( isset( $results['slug'] ) && 'loss' !== $results['slug'] ) { |
| 595 |
$result['balance'] = abs( $result['balance'] ); |
| 596 |
} |
| 597 |
|
| 598 |
if ( ! empty( $result['balance'] ) ) { |
| 599 |
if ( ! is_numeric( (float) $result['balance'] ) ) { |
| 600 |
continue; |
| 601 |
} |
| 602 |
$results['total_equity'] += (float) $result['balance']; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
$profit = 0; |
| 607 |
$loss = 0; |
| 608 |
|
| 609 |
if ( ! empty( $profit_loss['profit'] ) ) { |
| 610 |
$profit = $profit_loss['profit']; |
| 611 |
} elseif ( ! empty( $profit_loss['loss'] ) ) { |
| 612 |
$loss = $profit_loss['loss']; |
| 613 |
} |
| 614 |
|
| 615 |
$results['owners_equity'] = abs( $capital ) - abs( $drawings ) + abs( $profit ) - abs( $loss ); |
| 616 |
|
| 617 |
return $results; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Balance sheet with opening balance helper |
| 622 |
* |
| 623 |
* @param $bs_start_date |
| 624 |
* @param $data |
| 625 |
* @param $sql |
| 626 |
* @param $chart_id |
| 627 |
* |
| 628 |
* @return array |
| 629 |
*/ |
| 630 |
function erp_acct_balance_sheet_calculate_with_opening_balance( $bs_start_date, $data, $sql, $chart_id ) { |
| 631 |
global $wpdb; |
| 632 |
|
| 633 |
// get closest financial year id and start date |
| 634 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $bs_start_date ); |
| 635 |
|
| 636 |
// get opening balance data within that(^) financial year |
| 637 |
$opening_balance = erp_acct_bs_opening_balance_by_fn_year_id( $closest_fy_date['id'], $chart_id ); |
| 638 |
|
| 639 |
$ledger_sql = "SELECT |
| 640 |
ledger.id, ledger.name |
| 641 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 642 |
WHERE ledger.chart_id={$chart_id} AND ledger.slug <> 'owner_s_equity'"; |
| 643 |
|
| 644 |
$ledgers = $wpdb->get_results( $ledger_sql, ARRAY_A ); |
| 645 |
$temp_data = erp_acct_get_bs_balance_with_opening_balance( $ledgers, $data, $opening_balance ); |
| 646 |
$result = []; |
| 647 |
|
| 648 |
if ( ! erp_acct_has_date_diff( $bs_start_date, $closest_fy_date['start_date'] ) ) { |
| 649 |
return $temp_data; |
| 650 |
} else { |
| 651 |
$prev_date_of_tb_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $bs_start_date ) ) ); |
| 652 |
} |
| 653 |
|
| 654 |
// should we go further calculation, check the diff |
| 655 |
$date1 = date_create( $bs_start_date ); |
| 656 |
$date2 = date_create( $closest_fy_date['start_date'] ); |
| 657 |
$interval = date_diff( $date1, $date2 ); |
| 658 |
|
| 659 |
// if difference is `0` OR `1` day |
| 660 |
if ( '2' > $interval->format( '%a' ) ) { |
| 661 |
return $temp_data; |
| 662 |
} else { |
| 663 |
// get previous date from balance sheet start date |
| 664 |
$date_before_balance_sheet_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $bs_start_date ) ) ); |
| 665 |
$bs_date = $date_before_balance_sheet_start; |
| 666 |
} |
| 667 |
|
| 668 |
// get ledger details data between `financial year start date` and `previous date from balance sheet start date` |
| 669 |
$ledger_details = $wpdb->get_results( |
| 670 |
$wpdb->prepare( $sql, $closest_fy_date['start_date'], $bs_date ), |
| 671 |
ARRAY_A |
| 672 |
); |
| 673 |
|
| 674 |
foreach ( $temp_data as $temp ) { |
| 675 |
$balance = $temp['balance']; |
| 676 |
|
| 677 |
foreach ( $ledger_details as $detail ) { |
| 678 |
if ( $temp['id'] === $detail['id'] ) { |
| 679 |
$balance += (float) $detail['balance']; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
$result[] = [ |
| 684 |
'id' => $temp['id'], |
| 685 |
'name' => $temp['name'], |
| 686 |
'balance' => $balance, |
| 687 |
]; |
| 688 |
} |
| 689 |
|
| 690 |
return $result; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Get ledger balance with opening balance |
| 695 |
* |
| 696 |
* @param array $ledgers |
| 697 |
* @param array $data |
| 698 |
* @param array $opening_balance |
| 699 |
* |
| 700 |
* @return array |
| 701 |
*/ |
| 702 |
function erp_acct_get_bs_balance_with_opening_balance( $ledgers, $data, $opening_balance ) { |
| 703 |
$temp_data = []; |
| 704 |
|
| 705 |
foreach ( $ledgers as $ledger ) { |
| 706 |
$balance = 0; |
| 707 |
|
| 708 |
foreach ( $data as $row ) { |
| 709 |
if ( $row['balance'] && $row['id'] === $ledger['id'] ) { |
| 710 |
$balance += (float) $row['balance']; |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
foreach ( $opening_balance as $op_balance ) { |
| 715 |
if ( $op_balance['id'] === $ledger['id'] ) { |
| 716 |
$balance += (float) $op_balance['balance']; |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if ( $balance ) { |
| 721 |
$temp_data[] = [ |
| 722 |
'id' => $ledger['id'], |
| 723 |
'name' => $ledger['name'], |
| 724 |
'balance' => $balance, |
| 725 |
]; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
return $temp_data; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Get opening balance data by financial year id |
| 734 |
* |
| 735 |
* @param int $id |
| 736 |
* @param int $chart_id ( optional ) |
| 737 |
* |
| 738 |
* @return array |
| 739 |
*/ |
| 740 |
function erp_acct_bs_opening_balance_by_fn_year_id( $id, $chart_id ) { |
| 741 |
global $wpdb; |
| 742 |
|
| 743 |
$where = ''; |
| 744 |
|
| 745 |
if ( $chart_id ) { |
| 746 |
$where = $wpdb->prepare( 'AND ledger.chart_id = %d', $chart_id ); |
| 747 |
} |
| 748 |
|
| 749 |
$sql = "SELECT ledger.id, ledger.name, SUM(opb.debit - opb.credit) AS balance |
| 750 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 751 |
LEFT JOIN {$wpdb->prefix}erp_acct_opening_balances AS opb ON ledger.id = opb.ledger_id |
| 752 |
WHERE opb.financial_year_id = %d {$where} AND opb.type = 'ledger' AND ledger.slug <> 'owner_s_equity' |
| 753 |
GROUP BY opb.ledger_id"; |
| 754 |
|
| 755 |
return $wpdb->get_results( $wpdb->prepare( $sql, $id ), ARRAY_A ); |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* Get profit-loss |
| 760 |
* |
| 761 |
* @param $args |
| 762 |
* |
| 763 |
* @return array |
| 764 |
*/ |
| 765 |
function erp_acct_get_profit_loss( $args ) { |
| 766 |
global $wpdb; |
| 767 |
|
| 768 |
if ( empty( $args['start_date'] ) ) { |
| 769 |
$args['start_date'] = date( 'Y-m-d', strtotime( 'first day of january' ) ); |
| 770 |
} else { |
| 771 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $args['start_date'] ); |
| 772 |
$args['start_date'] = $closest_fy_date['start_date']; |
| 773 |
} |
| 774 |
|
| 775 |
if ( empty( $args['end_date'] ) ) { |
| 776 |
$args['end_date'] = date( 'Y-m-d', strtotime( 'last day of this month' ) ); |
| 777 |
} |
| 778 |
|
| 779 |
if ( empty( $args['start_date'] ) && empty( $args['end_date'] ) ) { |
| 780 |
$args['start_date'] = date( 'Y-m-d', strtotime( 'first day of january' ) ); |
| 781 |
$args['end_date'] = date( 'Y-m-d', strtotime( 'last day of this month' ) ); |
| 782 |
} |
| 783 |
|
| 784 |
$sql1 = "SELECT |
| 785 |
ledger.id, |
| 786 |
ledger.name, |
| 787 |
SUM(ledger_detail.debit - ledger_detail.credit) AS balance |
| 788 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 789 |
LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details AS ledger_detail ON ledger.id = ledger_detail.ledger_id WHERE ledger.chart_id=4 AND ledger_detail.trn_date BETWEEN '%s' AND '%s' |
| 790 |
GROUP BY ledger_detail.ledger_id"; |
| 791 |
|
| 792 |
$sql2 = "SELECT |
| 793 |
ledger.id, |
| 794 |
ledger.name, |
| 795 |
SUM(ledger_detail.debit - ledger_detail.credit) AS balance |
| 796 |
FROM {$wpdb->prefix}erp_acct_ledgers AS ledger |
| 797 |
LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details AS ledger_detail ON ledger.id = ledger_detail.ledger_id WHERE ledger.chart_id=5 AND ledger_detail.trn_date BETWEEN '%s' AND '%s' |
| 798 |
GROUP BY ledger_detail.ledger_id"; |
| 799 |
|
| 800 |
$data1 = $wpdb->get_results( $wpdb->prepare( $sql1, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 801 |
$data2 = $wpdb->get_results( $wpdb->prepare( $sql2, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 802 |
|
| 803 |
$results['rows1'] = erp_acct_income_statement_calculate_with_opening_balance( $args['start_date'], $data1, $sql1, 4 ); |
| 804 |
$results['rows2'] = erp_acct_income_statement_calculate_with_opening_balance( $args['start_date'], $data2, $sql2, 5 ); |
| 805 |
|
| 806 |
$results['income'] = 0; |
| 807 |
$results['expense'] = 0; |
| 808 |
|
| 809 |
foreach ( $results['rows1'] as $result ) { |
| 810 |
$results['income'] += (float) $result['balance']; |
| 811 |
} |
| 812 |
|
| 813 |
foreach ( $results['rows2'] as $result ) { |
| 814 |
$results['expense'] += (float) $result['balance']; |
| 815 |
} |
| 816 |
|
| 817 |
return $results; |
| 818 |
} |
| 819 |
|