commands and job
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ class SupplyChainContract extends Model
|
||||
'issuer_id',
|
||||
'issuer_name',
|
||||
'title',
|
||||
'type',
|
||||
'end_date',
|
||||
'delivery_by',
|
||||
'body',
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user