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; } /** * 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:
"; foreach($listItems as $item) { $body .= $item . "
"; } $body .= "The price for the next's month rent is " . number_format($cost, 2, ".", ",") . " ISK
"; $body .= "Please remit payment to Spatial Forces on the 1st should you continue to wish to rent the moon.
"; $body .= "In the description of the payment put the invoice number of: " . $invoiceId . "
"; $body .= "Sincerely,
"; $body .= "Warped Intentions Leadership
"; //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; } }