ldc-cocotte/app/Notifications/Regate/AnOrderWasPlaced.php
2024-11-29 16:49:59 +01:00

157 lines
3.7 KiB
PHP

<?php
namespace App\Notifications\Regate;
use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AnOrderWasPlaced extends Notification implements ShouldQueue
{
use Queueable;
/**
* CSV File headers
*
* @var array
*/
private $header = [
"CodeArt",
"Client livré code",
"ean13",
"Type de commande",
"Unité Elem. Appro",
"mode de commande",
"Pcb Art",
"Date livraison",
"Qté commandée",
"Num Cde Client",
];
/**
* Mapping between order unit and order mode
*
* @var array
*/
private $mode_commande = [
"COLIS" => "UEA",
"KGM" => "KGM",
"PCE" => "UB"
];
/**
* The Order to be exported
*
* @var \App\Models\Order
* */
private $order;
/**
* Create a new notification instance.
*
* @param \App\Models\Order $order
* @return void
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
// phpcs:ignore
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
// phpcs:ignore
public function toMail($notifiable)
{
$fileName = sprintf(
'BTQ-%s-%s%s.csv',
$this->order->reference,
date('Ymd_His'),
\microtime(true)
);
return (new MailMessage())->subject(
sprintf(
"%s - %s",
config('app.name'),
__('An order was placed')
)
)
->line(
sprintf(
"%s. %s : %s",
__("An order was placed"),
__("Attachement filename"),
$fileName
)
)
->attachData(
$this->makeCsv(),
$fileName,
['mime' => 'text/csv']
);
}
/**
* Render Regate CSV for order.
*
* @return string
*/
private function makeCsv(): string
{
/**
* Formatted array to make CSV
*
* @var array $csvlines
*/
$csvlines = [];
foreach ($this->order->lines as $orderline) {
array_push(
$csvlines,
[
$orderline->product->foodProduct->no_art,
// 21/01/2022 : suppression du pcb ici
$this->order->customer->salarie->matricule,
$orderline->product->reference,
"commande ferme",
$orderline->product->order_unit ?? 'PCE',
// UEA,KGM,UB (respectivement selon le champ précédent).
$this->mode_commande[$orderline->product->order_unit ?? 'PCE'],
// On vide la colonne pcb comme demandé.
"",
$this->order->timeslot->day->format('d/m/Y'),
$orderline->quantity,
$this->order->reference,
]
);
}
// Add header line.
array_unshift($csvlines, $this->header);
$f = fopen('php://temp', 'w');
foreach ($csvlines as $item) {
fputcsv($f, $item, ";", '"', "\\");
}
rewind($f);
return stream_get_contents($f);
}
}