Untitled

 avatar
unknown
php
a year ago
4.3 kB
5
Indexable
// Check if $screenId is not null and matches the ledger screen ID
				$isScreenLedger = $screenId != null && config('screens.ledger.ledger') == $screenId;
				
				// Rewrite the retained earnings brought forward data if the conditions are met
				if ($journal->account_title_id == config('const.account_earnedsurplus') && $isScreenLedger) {
					Log::debug('Condition met for rewriting retained earnings.', ['journal' => $journal]);
					// Retrieve company term and first term
					$companyTerm = session('selectedterm');
					$firstTerm = CompanyAccountTerm::retrieveTerms(bn_selected_company_id())->first();

					// Initialize variables
					$openingBalance = $journal->sum_pre_amount;
					$rangeAdjustments = [0];
					$retainedEarnings_c = $journal->c_amount;
					$retainedEarnings_d = $journal->d_amount;
					$retainedEarnings_pre = $beCarryAmountSurplus = 0;

					// Iterate through each company term
					CompanyAccountTerm::retrieveTerms(bn_selected_company_id())->each(function (CompanyAccountTerm $term) use (&$retainedEarnings_pre, $rangeAdjustments) {
						// Calculate begin and end dates
						$begin = Carbon::createFromFormat('Y-m-d', $term->term_beginmonth)->format('Ym');
						$end = Carbon::createFromFormat('Y-m-d', $term->term_endmonth)->format('Ym');

						// Skip the current term
						if ($term->id === session('selectedterm')) {
							return false;
						}

						// Get previous earned surplus for the term
						$previousEarnedsurplus = CompanyJournalDetail::getLedger([
							'company_id' => $term->company_id,
							'accountterm_id' => $term->id,
							'account_title_id' => config('const.account_earnedsurplus')
						]);

						// Create a financial report instance
						$financialReport = new FinancialReportService($term->company_id, $term->id, $begin, 2, bn_language_id());

						// Get P/L data last year
						$plBalances = $financialReport->createPlData($begin, $end, 0, 1, [], -1, $rangeAdjustments);
						$plBalance = end($plBalances);

						// Accumulate the retained earnings and profit in this year
						$retainedEarnings_pre = BridgenoteTraitCollection::bn_add(
							$plBalance['amount'] + $previousEarnedsurplus->sum_amount,
							$retainedEarnings_pre
						);
					});

					// Adjust opening balance based on conditions
					if ($companyTerm == $firstTerm->id && !empty($accountEarnedsurplusNA)) {
						$openingBalance = 0;
					}

					// Calculate opening balance
					$openingBalance = BridgenoteTraitCollection::bn_add($openingBalance, $beCarryAmountSurplus, session('roundup_digit'));
					$calculateDl = BridgenoteTraitCollection::bn_sub($journal->d_amount, $journal->c_amount, session('roundup_digit'));
					$calculateCr = BridgenoteTraitCollection::bn_sub($journal->c_amount, $journal->d_amount, session('roundup_digit'));

					// Update journal entries based on conditions
					if ($journal->detailed_id == -1 || ($journal->detailed_id == 0 && $companyTerm == $firstTerm->id)) {
						$journal->pre_amount = BridgenoteTraitCollection::bn_add($retainedEarnings_pre, $openingBalance, session('roundup_digit'));
						$journal->d_amount = $retainedEarnings_d;
						$journal->c_amount = $retainedEarnings_c;
						$journal->amount = $retainedEarnings_pre + $openingBalance + ($journal->dc_type == 'DL' ? $calculateDl : $calculateCr);
					} elseif ($companyTerm != $firstTerm->id) {
						if ($journal->detailed_id == 0) {
							$journal->pre_amount = BridgenoteTraitCollection::bn_add($retainedEarnings_pre, $openingBalance, session('roundup_digit'));
							$journal->amount = $retainedEarnings_pre + $openingBalance + ($journal->dc_type == 'DL' ? $calculateDl : $calculateCr);
						} else {
							$journal->pre_amount = 0;
							$journal->amount = ($journal->dc_type == 'DL' ? $calculateDl : $calculateCr);
						}
					} elseif ($companyTerm == $firstTerm->id && $journal->detailed_id > 0) {
						$journal->pre_amount = 0;
						$journal->amount = ($journal->dc_type == 'DL' ? $calculateDl : $calculateCr);
					}

					// Round and update sum amounts
					$journal->sum_pre_amount = round($journal->pre_amount, session('roundup_digit'));
					$journal->sum_amount = round($journal->amount, session('roundup_digit'));
				}
Editor is loading...
Leave a Comment