update moon rental paid state job

This commit is contained in:
2020-05-29 22:06:12 -05:00
parent bff5dbef07
commit 3243bb8702
6 changed files with 170 additions and 455 deletions

View File

@@ -1,231 +0,0 @@
<?php
namespace App\Jobs\Commands\Moons;
//Internal Libraries
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Carbon\Carbon;
//Jobs
use App\Jobs\ProcessSendEveMailJob;
//Library
use App\Library\Moons\MoonCalc;
use App\Library\Esi\Esi;
use Seat\Eseye\Exceptions\RequestFailedException;
use App\Library\Lookups\LookupHelper;
//Models
use App\Models\Moon\RentalMoon;
use App\Models\MoonRent\MoonRental;
use App\Models\Mail\SentMail;
use App\Models\Moon\RentalMoonInvoice;
class MoonRentalInvoiceCreate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Today's date
*
* @var Carbon
*/
private $today;
/**
* Rental Contact
*
* @var int
*/
private $contact;
/**
* Moon Rentals
*
* @var MoonRental
*/
private $rentals;
/**
* ESI mail delay to not hit limit of 4 / min
*
* @var int
*/
private $delay;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($contact, $mailDelay)
{
//Renter contact
$this->contact = $contact;
//Setup today's date
$this->today = Carbon::now();
$this->today->second = 1;
$this->today->minute = 0;
$this->today->hour = 0;
//Setup the delay
$this->delay = $mailDelay;
//Null out unused variables when calling the construct
$this->rentals = null;
//Set the connection
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Create needed variables
$moonCalc = new MoonCalc;
$lookup = new LookupHelper;
$body = null;
$delay = 60;
$config = config('esi');
$moons = null;
//Get the rentals the contact is renting
$this->rentals = MoonRental::where([
'Contact' => $this->contact,
])->get();
//Totalize the cost of the moons
$cost = $this->TotalizeMoonCost();
//Get the list of the moons in a list format
$listItems = $this->GetMoonList();
$invoiceId = $this->CreateInvoiceId();
//Build a mail body to be sent to the renter
$body = "Moon Rent is due for the following moons:<br>";
foreach($listItems as $item) {
$body .= $item . "<br>";
}
$body .= "The price for the next's month rent is " . number_format($cost, 2, ".", ",") . " ISK<br>";
$body .= "Please remit payment to Spatial Forces on the 1st should you continue to wish to rent the moon.<br>";
$body .= "In the description of the payment put the invoice number of: " . $invoiceId . "<br>";
$body .= "Sincerely,<br>";
$body .= "Warped Intentions Leadership<br>";
//Get the information compiled for creating the rental invoice
$charInfo = $lookup->GetCharacterInfo($this->contact);
$corpInfo = $lookup->GetCorporationInfo($charInfo->corporation_id);
foreach($listItems as $item) {
$moons .= $item . ',';
}
$moons = rtrim($moons, ',');
//Create the moon invoice and save it to the database
$invoice = new RentalMoonInvoice;
$invoice->character_id = $this->contact;
$invoice->character_name = $charInfo->name;
$invoice->corporation_id = $charInfo->character_id;
$invoice->corporation_name = $corpInfo->name;
$invoice->rental_moons = $moons;
$invoice->invoice_amount = $cost;
$invoice->due_date = Carbon::now()->addDays(3);
$invoice->paid = 'No';
$invoice->save();
//Dispatch a new mail job
$subject = "Warped Intentions Moon Rental Payment Due for " . $today->englishMonth;
//ProcessSendEveMailJob::dispatch($body, (int)$contact->Contact, 'character', $subject, $config['primary'])->onQueue('mail')->delay(Carbon::now()->addSeconds($this->delay));
ProcessSendEveMailJob::dispatch($body, $config['primary'], 'character', $subject, $config['primary'])->onQueue('mail')->delay(Carbon::now()->addSeconds($this->delay));
MoonRentalUpdate::dispatch($this->rentals)->onQueue('moons');
}
private function TotalizeMoonCost() {
$totalCost = 0.00;
$price = null;
foreach($this->rentals as $rental) {
$moon = RentalMoon::where([
'System' => $rental->System,
'Planet' => $rental->Planet,
'Moon' => $rental->Moon,
])->first();
$end = new Carbon($rental->Paid_Until);
//If today is greater than the rental end, then calculate the moon cost
if($today->greaterThanOrEqualTo($end)) {
//Get the updated price for the moon
$price = $moonCalc->SpatialMoons($moon->FirstOre, $moon->FirstQuantity, $moon->SecondOre, $moon->SecondQuantity,
$moon->ThirdOre, $moon->ThirdQuantity, $moon->FourthOre, $moon->FourthQuantity);
//Check the type and figure out which price to add in
if($rental->Type == 'alliance') {
$totalCost += $price['alliance'];
} else {
$totalCost += $price['outofalliance'];
}
}
}
//Return the total cost to the calling function
return $totalCost;
}
private function GetMoonList() {
//Declare the list variable as an array
$list = array();
//Foreach of the moons, build the system, planet, and moon
foreach($this->rentals as $moon) {
$temp = 'Moon: ' . $moon->System . ' - ' . $moon->Planet . ' - ' . $moon->Moon;
array_push($list, $temp);
}
//Return the list
return $list;
}
/**
*
*/
private function CreateInvoiceId() {
//Set continue to true as a default.
$continue = true;
//Continually get a unique id until one is found where it's not used.
do {
$invoiceId = uniqid('rmi_', true);
$count = MoonRentalInvoice::where(['invoice_id' => $invoiceId])->count();
if($count == 0) {
$continue = false;
}
} while ($continue == true);
return $invoiceId;
}
}

View File

@@ -1,118 +0,0 @@
<?php
namespace App\Jobs\Commands\Moons;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Log;
//App Library
//App Models
use App\Models\Finances\PlayerDonationJournal;
use App\Models\MoonRentals\MoonRentalInvoice;
use App\Models\MoonRentals\MoonRentalPayment;
use App\Models\MoonRentals\MoonRent;
class MoonRentalInvoiceVerify implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//Set the connection
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Create the date to look through
$date = Carbon::now()->subDays(60);
//Get the open moon rental invoices
$unpaid = MoonRentalInvoice::where([
'Paid' => 'No',
])->get();
//If there are no unpaid invoices, then return out of the job as successful
if($unpaid == null || $unpaid == false) {
return;
}
//Foreach unpaid invoice, run through them and check if there is an entry in the player journal table to verify the invoice
foreach($unpaid as $un) {
//Get everything which has a description like the invoice code.
$checkA = PlayerDonationJournal::where('description', 'like', '%' . $un->invoice_id . '%')->get();
//Get the player donations and corp transfers which have the amount we are looking for
$checkB = PlayerDonationJournal::where([
'amount' => $un->invoice_amount,
])->where('date', '>=', $date)->get();
//Check each of the checkA's for the correct description
foreach($checkA as $check) {
//Search for the sub string of the invoice id in the description
$desc = strstr($check->description, $un->invoice_id);
//If the description is found, then update the invoice.
//Create an invoice payment
if($desc == $un->invoice_id) {
//Insert a new moon rental payment once completed
MoonRentalPayment::insert([
'invoice_id' => $un->invoice_id,
'payment_amount' => $un->invoice_amount,
'reference_payment' => $check->id,
]);
//Update the invoice as paid
MoonRentalInvoice::where([
'invoice_id' => $un->invoice_id,
])->update([
'Paid' => 'Yes',
]);
//Increase the moon rental by another month
$moons = $un->rental_moons;
//Turns the moons into an array
$moons = explode(',', $moons);
foreach($moons as $moon) {
//Need to separate each moon into system, planet, and moon to update the moon rental
}
}
}
//Check each of checkB's for the invoice amount
foreach($checkB as $check) {
}
}
}
}

View File

@@ -1,35 +0,0 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class MoonRentalUpdate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//Setup the connection
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
}
}

View File

@@ -1,71 +0,0 @@
<?php
namespace App\Jobs\Commands\Moons;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use DB;
use Carbon\Carbon;
use Log;
//Library
use App\Library\Moons\MoonCalc;
//Models
use App\Models\MoonRentals\AllianceRentalMoon;
class UpdateMoonRentalPrice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3200;
/**
* Retries
*
* @var int
*/
public $retries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//Set the connection for the job
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare the helper for moon calculations
$moonHelper = new MoonCalc;
//Get all the moons from the rental database
$moons = AllianceRentalMoon::all();
//Get the configuration from the database for the pricing calculate of rental moons
$config = DB::table('Config')->get();
/**
* Calculate the worth of each moon along with the rental prices.
*/
foreach($moons as $rental) {
}
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Jobs\Commands\RentalMoons;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Carbon\Carbon;
use Log;
//Models
use App\Models\MoonRentals\AllianceRentalMoon;
class UpdateMoonRentalPaidState implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 1600;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//Set the queue connection up
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Get all of the moons from the rental database
$moons = AllianceRentalMoons::all();
/**
* For each of the moons check the rental until date, the paid until date,
* and compare them to today's current date.
*
* If the paid date is later than the rental until date, then update the rental until
* date to match the paid date. If the paid until date is today or less, then update the
* paid column of the moon to not paid. If the moon hasn't been paid 2 weeks after the first
* of the month, then remove the renter, and send an eve mail to alliance leadership, and the renter
* denoting failure of payment has resulted in the moon rental for the current month being
* revoked.
*/
foreach($moon as $rental) {
}
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Jobs\Commands\RentalMoons;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Log;
//Library
use App\Library\Moons\MoonCalc;
//Models
use App\Models\MoonRentals\AllianceRentalMoon;
/**
* This job performs the action of updating the moon rental price once per day.
*/
class UpdateMoonRentalPrice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3200;
/**
* Retries
*
* @var int
*/
public $retries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//Set the connection for the job
$this->connection = 'redis';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare the helper for moon calculations
$moonHelper = new MoonCalc;
//Get all the moons from the rental database
$moons = AllianceRentalMoon::all();
/**
* Calculate the worth of each moon along with the rental prices.
* For all of the moon calculations, the price of the mineral is averaged,
* thereby, the price of the moon worth is also averaged based on the averaging
* of the mineral price.
*/
foreach($moons as $rental) {
//Calculate the total worth of the moon
$totalWorth = $moonHelper->SpatialMoonsTotalWorth(
$rental->first_ore, $rental->first_quantity,
$rental->second_ore, $rental->second_quantity,
$rental->third_ore, $rental->third_quantity,
$rental->fourth_ore, $rental->fourth_quantity,
);
//Calculate the rental prices of the moon
$rentalPrice = $moonHelper->SpatialMoons(
$rental->first_ore, $rental->first_quantity,
$rental->second_ore, $rental->second_quantity,
$rental->third_ore, $rental->third_quantity,
$rental->fourth_ore, $rental->fourth_quantity,
);
//Update the moon in the database
AllianceRentalMoon::where([
'region' => $rental->region,
'system' => $rental->system,
'planet' => $rental->planet,
'moon' => $rental->moon,
])->update([
'moon_worth' => $totalWorth,
'alliance_rental_price' => $rentalPrice['alliance'],
'out_of_alliance_rental_price' => $rentalPrice['outofalliance'],
]);
}
}
}