diff --git a/app/Console/Commands/moonmailer.php b/app/Console/Commands/moonmailer.php index aa0442c9d..b2db38aec 100644 --- a/app/Console/Commands/moonmailer.php +++ b/app/Console/Commands/moonmailer.php @@ -17,7 +17,6 @@ use App\Library\Moons\MoonCalc; //Models use App\Models\Moon\Moon; use App\Models\Moon\MoonRent; -use App\Models\Mail\SentMail; use App\Models\Jobs\JobSendEveMail; class MoonMailerCommand extends Command @@ -59,11 +58,9 @@ class MoonMailerCommand extends Command $task->SetStartStatus(); //Declare the moon calc class variable to utilize the library to update the price - $moonCalc = new MoonCalc; $mailer = new MoonMailer; //Create other variables - $price = null; $body = null; //Get today's date. @@ -104,33 +101,18 @@ class MoonMailerCommand extends Command $mail->body = $body; $mail->recipient = (int)$contact; $mail->recipient_type = 'character'; - - //Dispatch the job and cycle to the next moon rental SendEveMailJob::dispatch($mail); - //After the mail is dispatched, saved the sent mail record, - $sentmail = new SentMail; - $sentmail->sender = $mail->sender; - $sentmail->subject = $mail->subject; - $sentmail->body = $mail->body; - $sentmail->recipient = $mail->recipient; - $sentmail->recipient_type = $mail->recipient_type; - $sentmail->save(); + //After the mail is dispatched, saved the sent mail record + $moonMailer->SaveSentRecord($mail->sender, $mail->subject, $mail->body, $mail->recipient, $mail->recipient_type); //Delete the record from the database foreach($rentals as $rental) { - if($today->greaterThanOrEqualTo($rental->RentalEnd)) { - MoonRent::where(['id' => $rental->id])->delete(); - } + //Delete the moon rental + $moonMailer->DeleteMoonRental($rental, $today); //Mark the moon as not paid for the next month - Moon::where([ - 'System' => $rental->System, - 'Planet' => $rental->Planet, - 'Moon' => $rental->Moon, - ])->update([ - 'Paid' => 'No', - ]); + $moonMailer->UpdateNotPaid($rental); } } diff --git a/app/Library/Moons/MoonMailer.php b/app/Library/Moons/MoonMailer.php index 0b56fbba7..93f60ebec 100644 --- a/app/Library/Moons/MoonMailer.php +++ b/app/Library/Moons/MoonMailer.php @@ -2,6 +2,9 @@ namespace App\Library\Moons; +//Internal Library +use Carbon\Carbon; + //Jobs use App\Jobs\SendEveMailJob; @@ -15,6 +18,35 @@ use App\Models\Moon\Moon; use App\Models\Moon\MoonRent; class MoonMailer { + public function DeleteMoonRent(MoonRent $rental, Carbon $today) { + if($today->greaterThanOrEqualTo($rental->RentalEnd)) { + MoonRent::where(['id' => $rental->id])->delete(); + } + } + + public function PaidUntil(MoonRent $rental) { + return $rental->paid_until; + } + + public function UpdateNotPaid(MoonRent $rental) { + Moon::where([ + 'System' => $rental->System, + 'Planet'=> $rental->Planet, + 'Moon'=> $rental->Moon, + ])->update([ + 'Paid' => 'No', + ]); + } + + public function SaveSentRecord($sender, $subject, $body, $recipient, $recipientType) { + $sentmail = new SentMail; + $sentmail->sender = $sender; + $sentmail->subject = $subject; + $sentmail->body = $body; + $sentmail->recipient = $recipient; + $sentmail->recipient_type = $recipientType; + $sentmail->save(); + } public function GetMoonList(MoonRent $moons) { //Declare the variable to be used as a global part of the function diff --git a/app/Models/Moon/Moon.php b/app/Models/Moon/Moon.php index 6433559af..d35d1ebd7 100644 --- a/app/Models/Moon/Moon.php +++ b/app/Models/Moon/Moon.php @@ -31,5 +31,7 @@ class Moon extends Model 'FourthQuantity', 'RentalCorp', 'RentalEnd', + 'Paid', + 'Paid_Until', ]; } diff --git a/app/Models/Moon/MoonRent.php b/app/Models/Moon/MoonRent.php index ccd7ee57e..cc96a864c 100644 --- a/app/Models/Moon/MoonRent.php +++ b/app/Models/Moon/MoonRent.php @@ -24,5 +24,6 @@ class MoonRent extends Model 'RentalEnd', 'Contact', 'Price', + 'Paid_Until', ]; } diff --git a/database/migrations/2018_10_06_220248_create_moons_table.php b/database/migrations/2018_10_06_220248_create_moons_table.php index 56baf621f..451b970d6 100644 --- a/database/migrations/2018_10_06_220248_create_moons_table.php +++ b/database/migrations/2018_10_06_220248_create_moons_table.php @@ -47,6 +47,7 @@ class CreateMoonsTable extends Migration $table->string('Price'); $table->string('Type'); $table->string('Paid'); + $table->string('Paid_Until')->nullable(); }); } }