| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Get closest next financial year |
| 9 |
* |
| 10 |
* @param string $date |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
function erp_acct_clsbl_get_closest_next_fn_year( $date ) { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
return $wpdb->get_row( $wpdb->prepare( "SELECT id, start_date, end_date FROM {$wpdb->prefix}erp_acct_financial_years WHERE start_date > '%s' ORDER BY start_date ASC LIMIT 1", $date ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Close balance sheet now |
| 22 |
* |
| 23 |
* @param array $args |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
function erp_acct_clsbl_close_balance_sheet_now( $args ) { |
| 28 |
$balance_sheet = erp_acct_get_balance_sheet( $args ); |
| 29 |
$assets = $balance_sheet['rows1']; |
| 30 |
$liability = $balance_sheet['rows2']; |
| 31 |
$equity = $balance_sheet['rows3']; |
| 32 |
$next_f_year_id = $args['f_year_id']; |
| 33 |
|
| 34 |
global $wpdb; |
| 35 |
|
| 36 |
// remove next financial year data if exists |
| 37 |
$wpdb->query( |
| 38 |
$wpdb->prepare( |
| 39 |
"DELETE FROM {$wpdb->prefix}erp_acct_opening_balances |
| 40 |
WHERE financial_year_id = %d", |
| 41 |
$next_f_year_id |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
$ledger_map = \WeDevs\ERP\Accounting\Includes\Classes\Ledger_Map::get_instance(); |
| 46 |
|
| 47 |
// ledgers |
| 48 |
$sql = "SELECT id, chart_id, name, slug FROM {$wpdb->prefix}erp_acct_ledgers"; |
| 49 |
$ledgers = $wpdb->get_results( $sql, ARRAY_A ); |
| 50 |
|
| 51 |
foreach ( $ledgers as $ledger ) { |
| 52 |
// assets |
| 53 |
foreach ( $assets as $asset ) { |
| 54 |
if ( ! empty( $asset['id'] ) ) { |
| 55 |
if ( $asset['id'] === $ledger['id'] ) { |
| 56 |
if ( 0 <= $asset['balance'] ) { |
| 57 |
$debit = abs( $asset['balance'] ); |
| 58 |
$credit = 0.00; |
| 59 |
} else { |
| 60 |
$debit = 0.00; |
| 61 |
$credit = abs( $asset['balance'] ); |
| 62 |
} |
| 63 |
|
| 64 |
erp_acct_clsbl_insert_into_opening_balance( |
| 65 |
$next_f_year_id, |
| 66 |
$ledger['chart_id'], |
| 67 |
$ledger['id'], |
| 68 |
'ledger', |
| 69 |
$debit, |
| 70 |
$credit |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
} // assets loop |
| 75 |
|
| 76 |
// liability |
| 77 |
foreach ( $liability as $liab ) { |
| 78 |
if ( ! empty( $liab['id'] ) ) { |
| 79 |
if ( $liab['id'] === $ledger['id'] ) { |
| 80 |
if ( 0 <= $liab['balance'] ) { |
| 81 |
$debit = abs( $liab['balance'] ); |
| 82 |
$credit = 0.00; |
| 83 |
} else { |
| 84 |
$debit = 0.00; |
| 85 |
$credit = abs( $liab['balance'] ); |
| 86 |
} |
| 87 |
|
| 88 |
erp_acct_clsbl_insert_into_opening_balance( |
| 89 |
$next_f_year_id, |
| 90 |
$ledger['chart_id'], |
| 91 |
$ledger['id'], |
| 92 |
'ledger', |
| 93 |
$debit, |
| 94 |
$credit |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
} // liability loop |
| 99 |
|
| 100 |
// equity |
| 101 |
$owners_equity_id = $ledger_map->get_ledger_id_by_slug( 'owner_s_equity' ); |
| 102 |
|
| 103 |
foreach ( $equity as $eqt ) { |
| 104 |
if ( ! empty( $eqt['id'] ) && $owners_equity_id !== $eqt['id'] ) { |
| 105 |
if ( $eqt['id'] === $ledger['id'] ) { |
| 106 |
if ( 0 <= $eqt['balance'] ) { |
| 107 |
$debit = abs( $eqt['balance'] ); |
| 108 |
$credit = 0.00; |
| 109 |
} else { |
| 110 |
$debit = 0.00; |
| 111 |
$credit = abs( $eqt['balance'] ); |
| 112 |
} |
| 113 |
|
| 114 |
erp_acct_clsbl_insert_into_opening_balance( |
| 115 |
$next_f_year_id, |
| 116 |
$ledger['chart_id'], |
| 117 |
$ledger['id'], |
| 118 |
'ledger', |
| 119 |
$debit, |
| 120 |
$credit |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
} // liability loop |
| 125 |
} // ledger loop |
| 126 |
|
| 127 |
$chart_id_bank = 7; |
| 128 |
$final_accounts_helper = new \WeDevs\ERP\Accounting\Includes\Classes\FinalAccountsHelper($args); |
| 129 |
|
| 130 |
// get bank balance |
| 131 |
//$bank_balance = erp_acct_bank_balance( $args, 'balance' ); |
| 132 |
|
| 133 |
if ( is_array( $final_accounts_helper->cashAtBankBreakdowns ) ) { |
| 134 |
foreach ( $final_accounts_helper->cashAtBankBreakdowns as $b_balance ) { |
| 135 |
erp_acct_clsbl_insert_into_opening_balance( |
| 136 |
$next_f_year_id, |
| 137 |
$chart_id_bank, |
| 138 |
$b_balance['ledger_id'], |
| 139 |
'ledger', |
| 140 |
$b_balance['balance'], |
| 141 |
0.00 |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// get bank loan |
| 147 |
//$bank_loan = erp_acct_bank_balance( $args, 'loan' ); |
| 148 |
|
| 149 |
if ( is_array( $final_accounts_helper->loanAtBankBreakdowns ) ) { |
| 150 |
foreach ( $final_accounts_helper->loanAtBankBreakdowns as $b_loan ) { |
| 151 |
erp_acct_clsbl_insert_into_opening_balance( |
| 152 |
$next_f_year_id, |
| 153 |
$chart_id_bank, |
| 154 |
$b_loan['ledger_id'], |
| 155 |
'ledger', |
| 156 |
0.00, |
| 157 |
abs( $b_loan['balance'] ) |
| 158 |
); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// get accounts receivable |
| 163 |
$accounts_receivable = erp_acct_clsbl_people_ar_calc_with_opening_balance( $args['start_date'] ); //erp_acct_clsbl_get_accounts_receivable_balance_with_people( $args ); |
| 164 |
|
| 165 |
foreach ( $accounts_receivable as $acc_receivable ) { |
| 166 |
erp_acct_clsbl_insert_into_opening_balance( |
| 167 |
$next_f_year_id, |
| 168 |
null, |
| 169 |
$acc_receivable['id'], |
| 170 |
'people', |
| 171 |
$acc_receivable['balance'], |
| 172 |
0.00 |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
// get accounts payable |
| 177 |
$accounts_payable = erp_acct_clsbl_vendor_ap_calc_with_opening_balance( $args['start_date'] ); //erp_acct_clsbl_get_accounts_payable_balance_with_people( $args ); |
| 178 |
|
| 179 |
foreach ( $accounts_payable as $acc_payable ) { |
| 180 |
erp_acct_clsbl_insert_into_opening_balance( |
| 181 |
$next_f_year_id, |
| 182 |
null, |
| 183 |
$acc_payable['id'], |
| 184 |
'people', |
| 185 |
0.00, |
| 186 |
abs( $acc_payable['balance'] ) |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
// sales tax receivable |
| 191 |
$tax_receivable = erp_acct_clsbl_sales_tax_agency( $args, 'receivable' ); |
| 192 |
|
| 193 |
foreach ( $tax_receivable as $receivable_agency ) { |
| 194 |
erp_acct_clsbl_insert_into_opening_balance( |
| 195 |
$next_f_year_id, |
| 196 |
null, |
| 197 |
$receivable_agency['id'], |
| 198 |
'tax_agency', |
| 199 |
$receivable_agency['balance'], |
| 200 |
0.00 |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
// sales tax payable |
| 205 |
$tax_payable = erp_acct_clsbl_sales_tax_agency( $args, 'payable' ); |
| 206 |
|
| 207 |
foreach ( $tax_payable as $payable_agency ) { |
| 208 |
erp_acct_clsbl_insert_into_opening_balance( |
| 209 |
$next_f_year_id, |
| 210 |
null, |
| 211 |
$payable_agency['id'], |
| 212 |
'tax_agency', |
| 213 |
0.00, |
| 214 |
abs( $payable_agency['balance'] ) |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
$owners_equity_ledger = $ledger_map->get_ledger_id_by_slug( 'owner_s_equity' ); |
| 219 |
$chart_equity_id = 3; |
| 220 |
|
| 221 |
if ( 0 === $balance_sheet['owners_equity'] ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
if ( $balance_sheet['owners_equity'] > 0 ) { |
| 226 |
erp_acct_clsbl_insert_into_opening_balance( |
| 227 |
$next_f_year_id, |
| 228 |
$chart_equity_id, |
| 229 |
$owners_equity_ledger, |
| 230 |
'ledger', |
| 231 |
0.00, |
| 232 |
abs( $balance_sheet['owners_equity'] ) |
| 233 |
); |
| 234 |
} else { |
| 235 |
erp_acct_clsbl_insert_into_opening_balance( |
| 236 |
$next_f_year_id, |
| 237 |
$chart_equity_id, |
| 238 |
$owners_equity_ledger, |
| 239 |
'ledger', |
| 240 |
abs( $balance_sheet['owners_equity'] ), |
| 241 |
0.00 |
| 242 |
); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Insert closing balance data into opening balance |
| 248 |
* |
| 249 |
* @param int $f_year_id |
| 250 |
* @param int $chart_id |
| 251 |
* @param int $ledger_id |
| 252 |
* @param string $type |
| 253 |
* @param int $debit |
| 254 |
* @param int $credit |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
function erp_acct_clsbl_insert_into_opening_balance( $f_year_id, $chart_id, $ledger_id, $type, $debit, $credit ) { |
| 259 |
global $wpdb; |
| 260 |
|
| 261 |
$wpdb->insert( |
| 262 |
"{$wpdb->prefix}erp_acct_opening_balances", |
| 263 |
[ |
| 264 |
'financial_year_id' => $f_year_id, |
| 265 |
'chart_id' => $chart_id, |
| 266 |
'ledger_id' => $ledger_id, |
| 267 |
'type' => $type, |
| 268 |
'debit' => $debit, |
| 269 |
'credit' => $credit, |
| 270 |
'created_at' => date( 'Y-m-d H:i:s' ), |
| 271 |
'created_by' => get_current_user_id(), |
| 272 |
] |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get accounts receivable balance with people |
| 278 |
* |
| 279 |
* @param array $args |
| 280 |
* |
| 281 |
* @return array |
| 282 |
*/ |
| 283 |
function erp_acct_clsbl_get_accounts_receivable_balance_with_people( $args ) { |
| 284 |
global $wpdb; |
| 285 |
|
| 286 |
// mainly ( debit - credit ) |
| 287 |
$sql = "SELECT invoice.customer_id AS id, SUM( debit - credit ) AS balance |
| 288 |
FROM {$wpdb->prefix}erp_acct_invoice_account_details AS invoice_acd |
| 289 |
LEFT JOIN {$wpdb->prefix}erp_acct_invoices AS invoice ON invoice_acd.invoice_no = invoice.voucher_no |
| 290 |
WHERE invoice_acd.trn_date BETWEEN '%s' AND '%s' GROUP BY invoice_acd.invoice_no HAVING balance > 0"; |
| 291 |
|
| 292 |
$data = $wpdb->get_results( $wpdb->prepare( $sql, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 293 |
|
| 294 |
return erp_acct_clsbl_people_ar_calc_with_opening_balance( $args['start_date'], $data, $sql ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Get accounts payable balance with people |
| 299 |
* |
| 300 |
* @param array $args |
| 301 |
* |
| 302 |
* @return array |
| 303 |
*/ |
| 304 |
function erp_acct_clsbl_get_accounts_payable_balance_with_people( $args ) { |
| 305 |
global $wpdb; |
| 306 |
|
| 307 |
$bill_sql = "SELECT bill.vendor_id AS id, SUM( debit - credit ) AS balance |
| 308 |
FROM {$wpdb->prefix}erp_acct_bill_account_details AS bill_acd |
| 309 |
LEFT JOIN {$wpdb->prefix}erp_acct_bills AS bill ON bill_acd.bill_no = bill.voucher_no |
| 310 |
WHERE bill_acd.trn_date BETWEEN '%s' AND '%s' GROUP BY bill_acd.bill_no HAVING balance < 0"; |
| 311 |
|
| 312 |
$purchase_sql = "SELECT purchase.vendor_id AS id, SUM( debit - credit ) AS balance |
| 313 |
FROM {$wpdb->prefix}erp_acct_purchase_account_details AS purchase_acd |
| 314 |
LEFT JOIN {$wpdb->prefix}erp_acct_purchase AS purchase ON purchase_acd.purchase_no = purchase.voucher_no |
| 315 |
WHERE purchase_acd.trn_date BETWEEN '%s' AND '%s' GROUP BY purchase_acd.purchase_no HAVING balance < 0"; |
| 316 |
|
| 317 |
$bill_data = $wpdb->get_results( $wpdb->prepare( $bill_sql, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 318 |
$purchase_data = $wpdb->get_results( $wpdb->prepare( $purchase_sql, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 319 |
|
| 320 |
return erp_acct_clsbl_vendor_ap_calc_with_opening_balance( |
| 321 |
$args['start_date'], |
| 322 |
$bill_data, |
| 323 |
$purchase_data, |
| 324 |
$bill_sql, |
| 325 |
$purchase_sql |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get people account receivable calculate with opening balance within financial year date range |
| 331 |
* |
| 332 |
* @param string $bs_start_date |
| 333 |
* @param float $data => account details data on balance sheet date range |
| 334 |
* @param string $sql |
| 335 |
* @param string $type |
| 336 |
* |
| 337 |
* @return array |
| 338 |
*/ |
| 339 |
function erp_acct_clsbl_people_ar_calc_with_opening_balance( $bs_start_date ) { |
| 340 |
global $wpdb; |
| 341 |
|
| 342 |
// get closest financial year id and start date |
| 343 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $bs_start_date ); |
| 344 |
|
| 345 |
// get opening balance data within that(^) financial year |
| 346 |
$opening_balance = erp_acct_clsbl_customer_ar_opening_balance_by_fn_year_id( $closest_fy_date['id'] ); |
| 347 |
|
| 348 |
// $merged = array_merge( $data, $opening_balance ); |
| 349 |
return erp_acct_clsbl_get_formatted_people_balance( $opening_balance ); |
| 350 |
|
| 351 |
// should we go further calculation, check the diff |
| 352 |
// if ( ! erp_acct_has_date_diff( $bs_start_date, $closest_fy_date['start_date'] ) ) { |
| 353 |
// return $result; |
| 354 |
// } else { |
| 355 |
// $prev_date_of_bs_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $bs_start_date ) ) ); |
| 356 |
// } |
| 357 |
|
| 358 |
// $query = $wpdb->get_results( $wpdb->prepare( $sql, $closest_fy_date['start_date'], $prev_date_of_bs_start ), ARRAY_A ); |
| 359 |
// $merged = array_merge( $result, $query ); |
| 360 |
|
| 361 |
// return erp_acct_clsbl_get_formatted_people_balance( $merged ); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Get people account payable calculate with opening balance within financial year date range |
| 366 |
* |
| 367 |
* @param string $bs_start_date |
| 368 |
* @param float $data => account details data on balance sheet date range |
| 369 |
* @param string $sql |
| 370 |
* @param string $type |
| 371 |
* |
| 372 |
* @return array |
| 373 |
*/ |
| 374 |
function erp_acct_clsbl_vendor_ap_calc_with_opening_balance( $bs_start_date ) { |
| 375 |
global $wpdb; |
| 376 |
|
| 377 |
// get closest financial year id and start date |
| 378 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $bs_start_date ); |
| 379 |
|
| 380 |
// get opening balance data within that(^) financial year |
| 381 |
$opening_balance = erp_acct_clsbl_vendor_ap_opening_balance_by_fn_year_id( $closest_fy_date['id'] ); |
| 382 |
|
| 383 |
// $merged = array_merge( $bill_data, $purchase_data, $opening_balance ); |
| 384 |
return erp_acct_clsbl_get_formatted_people_balance( $opening_balance ); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* People accounts receivable from opening balance |
| 389 |
* |
| 390 |
* @param int $id |
| 391 |
* |
| 392 |
* @return void |
| 393 |
*/ |
| 394 |
function erp_acct_clsbl_customer_ar_opening_balance_by_fn_year_id( $id ) { |
| 395 |
global $wpdb; |
| 396 |
|
| 397 |
$sql = "SELECT ledger_id AS id, SUM( debit - credit ) AS balance |
| 398 |
FROM {$wpdb->prefix}erp_acct_opening_balances |
| 399 |
WHERE financial_year_id = %d AND type = 'people' GROUP BY ledger_id HAVING balance > 0"; |
| 400 |
|
| 401 |
return $wpdb->get_results( $wpdb->prepare( $sql, $id ), ARRAY_A ); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* People accounts payable from opening balance |
| 406 |
* |
| 407 |
* @param int $id |
| 408 |
* |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
function erp_acct_clsbl_vendor_ap_opening_balance_by_fn_year_id( $id ) { |
| 412 |
global $wpdb; |
| 413 |
|
| 414 |
$sql = "SELECT ledger_id AS id, SUM( debit - credit ) AS balance |
| 415 |
FROM {$wpdb->prefix}erp_acct_opening_balances |
| 416 |
WHERE financial_year_id = %d AND type = 'people' GROUP BY ledger_id HAVING balance < 0"; |
| 417 |
|
| 418 |
return $wpdb->get_results( $wpdb->prepare( $sql, $id ), ARRAY_A ); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Accounts receivable array merge |
| 423 |
* |
| 424 |
* @param array $arr1 |
| 425 |
* @param array $arr2 |
| 426 |
* |
| 427 |
* @return array |
| 428 |
*/ |
| 429 |
function erp_acct_clsbl_get_formatted_people_balance( $arr ) { |
| 430 |
$temp = []; |
| 431 |
|
| 432 |
foreach ( $arr as $entry ) { |
| 433 |
// get index by id from a multidimensional array |
| 434 |
$index = array_search( $entry['id'], array_column( $arr, 'id' ), true ); |
| 435 |
|
| 436 |
if ( ! empty( $temp[ $index ] ) ) { |
| 437 |
$temp[ $index ]['balance'] += $entry['balance']; |
| 438 |
} else { |
| 439 |
$temp[] = [ |
| 440 |
'id' => $entry['id'], |
| 441 |
'balance' => $entry['balance'], |
| 442 |
]; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
return $temp; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Sales tax agency with closing balance |
| 451 |
* |
| 452 |
* @param array $args |
| 453 |
* @param array $type |
| 454 |
* |
| 455 |
* @return float |
| 456 |
*/ |
| 457 |
function erp_acct_clsbl_sales_tax_agency( $args, $type ) { |
| 458 |
global $wpdb; |
| 459 |
|
| 460 |
if ( 'payable' === $type ) { |
| 461 |
$having = 'HAVING balance < 0'; |
| 462 |
} elseif ( 'receivable' === $type ) { |
| 463 |
$having = 'HAVING balance > 0'; |
| 464 |
} |
| 465 |
|
| 466 |
$sql = "SELECT agency_id AS id, SUM( debit - credit ) AS balance FROM {$wpdb->prefix}erp_acct_tax_agency_details |
| 467 |
WHERE trn_date BETWEEN '%s' AND '%s' |
| 468 |
GROUP BY agency_id {$having}"; |
| 469 |
|
| 470 |
$data = $wpdb->get_results( $wpdb->prepare( $sql, $args['start_date'], $args['end_date'] ), ARRAY_A ); |
| 471 |
|
| 472 |
return erp_acct_clsbl_sales_tax_agency_with_opening_balance( $args['start_date'], $data, $sql, $type ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Get sales tax payable calculate with opening balance within financial year date range |
| 477 |
* |
| 478 |
* @param string $bs_start_date |
| 479 |
* @param float $data => agency details data on trial balance date range |
| 480 |
* @param string $sql |
| 481 |
* @param string $type |
| 482 |
* |
| 483 |
* @return float |
| 484 |
*/ |
| 485 |
function erp_acct_clsbl_sales_tax_agency_with_opening_balance( $bs_start_date, $data, $sql, $type ) { |
| 486 |
global $wpdb; |
| 487 |
|
| 488 |
// get closest financial year id and start date |
| 489 |
$closest_fy_date = erp_acct_get_closest_fn_year_date( $bs_start_date ); |
| 490 |
|
| 491 |
// get opening balance data within that(^) financial year |
| 492 |
$opening_balance = erp_acct_clsbl_sales_tax_agency_opening_balance_by_fn_year_id( $closest_fy_date['id'], $type ); |
| 493 |
|
| 494 |
$merged = array_merge( $data, $opening_balance ); |
| 495 |
$result = erp_acct_clsbl_get_formatted_people_balance( $merged ); |
| 496 |
|
| 497 |
// should we go further calculation, check the diff |
| 498 |
if ( ! erp_acct_has_date_diff( $bs_start_date, $closest_fy_date['start_date'] ) ) { |
| 499 |
return $result; |
| 500 |
} else { |
| 501 |
$prev_date_of_tb_start = date( 'Y-m-d', strtotime( '-1 day', strtotime( $bs_start_date ) ) ); |
| 502 |
} |
| 503 |
|
| 504 |
// get agency details data between |
| 505 |
// `financial year start date` |
| 506 |
// and |
| 507 |
// `previous date from trial balance start date` |
| 508 |
$agency_details_balance = $wpdb->get_results( $wpdb->prepare( $sql, $closest_fy_date['start_date'], $prev_date_of_tb_start ), ARRAY_A ); |
| 509 |
|
| 510 |
$merged = array_merge( $result, $agency_details_balance ); |
| 511 |
|
| 512 |
return erp_acct_clsbl_get_formatted_people_balance( $merged ); |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* @param int $id |
| 517 |
* @param string $type |
| 518 |
* |
| 519 |
* @return void |
| 520 |
*/ |
| 521 |
function erp_acct_clsbl_sales_tax_agency_opening_balance_by_fn_year_id( $id, $type ) { |
| 522 |
global $wpdb; |
| 523 |
|
| 524 |
if ( 'payable' === $type ) { |
| 525 |
$having = 'HAVING balance < 0'; |
| 526 |
} elseif ( 'receivable' === $type ) { |
| 527 |
$having = 'HAVING balance > 0'; |
| 528 |
} |
| 529 |
|
| 530 |
$sql = "SELECT ledger_id AS id, SUM( debit - credit ) AS balance |
| 531 |
FROM {$wpdb->prefix}erp_acct_opening_balances |
| 532 |
WHERE type = 'tax_agency' GROUP BY ledger_id {$having}"; |
| 533 |
|
| 534 |
return $wpdb->get_results( $sql, ARRAY_A ); |
| 535 |
} |
| 536 |
|