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