SetStartStatus(); //Create other variables $body = null; $delay = 5; //Get today's date $today = Carbon::now(); $today->second = 2; $today->minute = 0; $today->hour = 0; //Get the esi configuration $config = config('esi'); //Get all of the contacts for the flex structures $contacts = FlexStructure::select('requestor_id')->orderBy('requestor_id')->get(); //For each of the contacts, send a reminder mail about the total of the structures they are paying for foreach($contacts as $contact) { //Get all of the structures for requestor $structures = FlexStructure::where([ 'requestor_id' => $contact->requestor_id, ])->get(); //Totalize the total cost of everything $totalCost = $this->TotalizeCost($structures); //Build the body of the mail $body = "Flex Structure Overhead Cost is due for the following structures:
"; foreach($structures as $structure) { $body .= "System: " . $structure->system . " - " . $structure->structure_type . ": " . $structure->structure_cost . " ISK
"; } $body .= "Total Cost: " . number_format($totalCost, 2,".", ","); $body .= "Please remit payment to Spatial Forces by the 3rd of the month.
"; $body .= "Sincerely,
"; $body .= "Warped Intentions Leadership
"; //Dispatch the mail job $subject = "Warped Intentions Flex Structures Payment Due for " . $today->englishMonth; ProcessSendEveMailJob::dispatch($body, (int)$structure->requestor_id, 'character', $subject, $config['primary'])->onQueue('mail')->delay(Carbon::now()->addSeconds($delay)); //Increment the delay for the mail to not hit the rate limits $delay += 60; //After the mail is dispatched, save the sent mail record $this->SaveSentRecord($config['primary'], $subject, $body, (int)$structure->requestor_id, 'character'); } //Mark the job as finished $task->SetStopStatus(); } private function TotalizeCost($structures) { //Declare the total cost $totalCost = 0.00; foreach($structures as $structure) { $totalCost += $structure->structure_cost; } return $totalCost; } private 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(); } }