Skip to content

Instantly share code, notes, and snippets.

@fatihgune
Created December 6, 2020 16:07
Show Gist options
  • Save fatihgune/981500b35ac83233b2c16705cd6c67ab to your computer and use it in GitHub Desktop.
Save fatihgune/981500b35ac83233b2c16705cd6c67ab to your computer and use it in GitHub Desktop.
Asset Match (PHP - Laravel)
<?
// Get all book asset records for given country and type (unlisted and bankrupted companies only)
$allUnlistedAssets = BookAsset::join('companies AS c', 'company_id', '=', 'c.id')
->where('c.type', '=', 'unlisted')
->where('c.country', '=', 'COUNTRY_NAME_HERE')
->where('c.bankrupted', '=', 'yes')
->get();
// Sort them by their value's descending order
$unlistedAssets = $allUnlistedAssets->sortByDesc('value', SORT_NUMERIC);
// Get all book asset records for given country and type (listed companies only)
$allListedAssetsGroupOne = BookAsset::join('companies AS c', 'company_id', '=', 'c.id')
->where('c.type', '=', 'listed')
->where('c.country', '=', 'COUNTRY_NAME_HERE')
->get();
// for each record (company)
foreach ($unlistedAssets as $unlistedAsset) {
// the year the company bankrupted
$year = $unlistedAsset->company->when;
// Create a list of book asset values
// where the year matches the year company bankrupted
// and assets belong to listed companies.
// Also sort them by descending order
$allListedAssetValues = $allListedAssetsGroupOne->where('year', $year)->filter(function ($asset) use ($year) {
return $asset->company->bookAssets->where('year', $year)->count() === 1;
})->sortByDesc('value', SORT_NUMERIC)->pluck('value')->toArray();
// get the current company's book asset record and
// match with the closest asset from that list we generated above.
$closestNumber = $this->getClosestNumber($unlistedAsset['value'], $allListedAssetValues);
// find the company that has the matched value as their book asset (closest book asset record).
$matchedListedCompany = BookAsset::where('value', $closestNumber)->where('year', $year)->first();
// self explanatory
return [
'listed_company_name' => $matchedListedCompany->company->name,
'unlisted_company_name' => $unlistedAsset->company->name,
'year' => $year,
'country' => $unlistedAsset->company->country,
'listed_company_market_cap' => (float) $matchedListedCompany->company->marketCapitalizations->where('year', $year)->sortByDesc('value', SORT_NUMERIC)->first()['value'],
'listed_company_standard_dev' => (float) $matchedListedCompany->company->standartDeviationSampleResults->where('year', $year)->first()['value'],
'unlisted_company_total_liability' => (float) $unlistedAsset->company->totalLiabilities->where('year', $year)->first()['value'],
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment