Created
January 4, 2021 18:04
-
-
Save jongravois/edf69c970517d5eb49eef5e9735a0339 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Pipes\Invoices; | |
use App\Models\ConsignmentBand; | |
use App\Models\PostedInvoice; | |
use App\Models\Project; | |
use Closure; | |
class BandedRatesProfit | |
{ | |
public function handle(PostedInvoice $invoice, Closure $next) | |
{ | |
$project = Project::whereLotCode($invoice->consignment_code)->first(); | |
if(isset($project)) { | |
$currentBand = ConsignmentBand::query() | |
->whereConsignmentCode($invoice->consignment_code) | |
->where('low', '<=', $project->current_net) | |
->where('high', '>=', $project->current_net) | |
->first(); | |
//No Band Found - Abort | |
if (!isset($currentBand)) { | |
$invoice->gp_percent_used = 0; | |
$invoice->profit = 0; | |
} else { | |
$netWithInvoice = $project->current_net + $invoice->net; | |
//TEST IF NetWithInvoice STAYS in BAND | |
if ($netWithInvoice <= $currentBand->high) { | |
$invoice->update([ | |
'gp_percent_used' => $currentBand->percent, | |
'profit' => $invoice->net * $currentBand->percent, | |
'split_bands' => false, | |
'processed' => true | |
]); | |
} else { | |
$rollover = $netWithInvoice - $currentBand->high; | |
$sameBandPortion = $invoice->net - $rollover; | |
$nextBand = ConsignmentBand::query() | |
->whereConsignmentCode($invoice->consignment_code) | |
->where('low', (double)$currentBand->high + 1) | |
->first(); | |
$cBandProfit = $currentBand->percent * $sameBandPortion; | |
$nBandProfit = $nextBand->percent * $rollover; | |
$invoice->update([ | |
'gp_percent_used' => $nextBand->percent, | |
'profit' => $cBandProfit + $nBandProfit, | |
'split_bands' => true, | |
'processed' => true | |
]); | |
} // end if | |
} // end if | |
$project->update([ | |
'current_gross' => $project->current_gross + $invoice->gross, | |
'current_net' => $project->current_net + $invoice->net, | |
]); | |
} else { | |
//TODO NOTIFY | |
} // end if | |
return $next($invoice); | |
} // end function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment