diff --git a/app/Console/Commands/SupplyChain/EndSupplyChainContractCommand.php b/app/Console/Commands/SupplyChain/EndSupplyChainContractCommand.php index 4c83c5734..4d320f377 100644 --- a/app/Console/Commands/SupplyChain/EndSupplyChainContractCommand.php +++ b/app/Console/Commands/SupplyChain/EndSupplyChainContractCommand.php @@ -2,7 +2,16 @@ namespace App\Console\Commands\SupplyChain; +//Internal Library use Illuminate\Console\Command; +use Log; +use Carbon\Carbon; + +//Models +use App\Models\Contracts\SupplyChainContract; + +//Job +use App\Jobs\Commands\SupplyChain\EndSupplyChainContractJob; class EndSupplyChainContractCommand extends Command { @@ -37,6 +46,16 @@ class EndSupplyChainContractCommand extends Command */ public function handle() { - // + $today = Carbon::now(); + + //Get the supply chain contracts which are open, but need to be closed. + $contracts = SupplyChainContract::where([ + 'state' => 'open', + ])->where('end_date', '>', $today)->get(); + + //Create jobs to complete each contract + foreach($contracts as $contract) { + EndSupplyChainContractJob::dispatch($contract)->onQueue('default'); + } } } diff --git a/app/Jobs/Commands/SupplyChain/EndSupplyChainContractJob.php b/app/Jobs/Commands/SupplyChain/EndSupplyChainContractJob.php index 1451cb8f0..04bf57196 100644 --- a/app/Jobs/Commands/SupplyChain/EndSupplyChainContractJob.php +++ b/app/Jobs/Commands/SupplyChain/EndSupplyChainContractJob.php @@ -38,15 +38,39 @@ class EndSupplyChainContractJob implements ShouldQueue */ public $retries = 3; + /** + * Private Variables + */ + private $contractId; + private $issuerId; + private $issuerName; + private $title; + private $endDate; + private $deliveryBy; + private $body; + private $state; + private $finalCost; + /** * Create a new job instance. * * @return void */ - public function __construct() + public function __construct(SupplyChainContract $contract) { //Set the queue connection up $this->connection = 'redis'; + + //Set the variables + $contractId = $contract->contract_id; + $issuerId = $contract->issuer_id; + $issuerName = $contract->issuer_name; + $title = $contract->title; + $endDate = $contract->end_date; + $deliveryBy = $contract->delivery_by; + $body = $contract->body; + $state = $contract->state; + $finalCost = $contract->final_cost; } /** @@ -57,6 +81,33 @@ class EndSupplyChainContractJob implements ShouldQueue public function handle() { //Declare variables + $bidId = null; + $bidAmount = null; + //Get all of the bids from the contract + $bids = SupplyChainBids::where([ + 'contract_id' => $contractId, + ])->get(); + + //Loop through the bids and find the lowest bid + foreach($bids as $bid) { + if($bidId == null) { + $bidId = $bid->id; + $bidAmount = $bid->bid_amount; + } else { + if($bid->bid_amount < $bidAmount) { + $bidId = $bid->id; + $bidAmount = $bid->bid_amount; + } + } + } + + //Clean up the bids and update the contract with the winning bid + SupplyChainContract::where([ + 'contract_id' => $this->contractId, + ])->update([ + 'final_cost' => $bidAmount, + 'winning_bid_id' => $bidId, + ]); } } diff --git a/app/Models/Contracts/SupplyChainContract.php b/app/Models/Contracts/SupplyChainContract.php index d9461a211..a4ee68c91 100644 --- a/app/Models/Contracts/SupplyChainContract.php +++ b/app/Models/Contracts/SupplyChainContract.php @@ -21,7 +21,6 @@ class SupplyChainContract extends Model 'issuer_id', 'issuer_name', 'title', - 'type', 'end_date', 'delivery_by', 'body', diff --git a/database/migrations/2020_06_29_054832_create_new_contracts_table.php b/database/migrations/2020_06_29_054832_create_new_contracts_table.php index 560a38baf..20ee3ede3 100644 --- a/database/migrations/2020_06_29_054832_create_new_contracts_table.php +++ b/database/migrations/2020_06_29_054832_create_new_contracts_table.php @@ -41,6 +41,7 @@ class CreateNewContractsTable extends Migration ]); $table->unsignedInteger('bids')->default(0); $table->decimal('final_cost', 20, 2)->default(0.00); + $table->unsignedInteger('winning_bid_id')->default(0); $table->timestamps(); }); }