feat: Replace Regate EAI order transfert by email

This commit is contained in:
Vincent BRIDIER 2024-11-29 16:49:59 +01:00
parent e7baad6c14
commit 184da0204b
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
6 changed files with 164 additions and 193 deletions

View file

@ -10,14 +10,15 @@ use App\Models\Magasin;
use Illuminate\Support\Facades\Mail;
use App\Mail\OrderConfirmMail;
use App\Mail\OrderConfirmAdminMail;
use App\Jobs\CsvOrderWrite;
use App\Models\Setting;
use App\Notifications\Regate\AnOrderWasPlaced;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterval;
use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Session;
class OrderTunnelController extends Controller
@ -451,7 +452,7 @@ class OrderTunnelController extends Controller
// Export commande.
if (config('bosettings.regate_orders')) {
CsvOrderWrite::dispatch($order);
Notification::route('mail', config('mail.to.regate'))->notify(new AnOrderWasPlaced($order));
}
} //end if

View file

@ -1,63 +0,0 @@
<?php
namespace App\Jobs;
abstract class CsvJobs extends XArcadiaFlux
{
/**
* parse csv file
*
* @param string $csvFile
*
* @return array
*/
protected function parseCsv(string $csvFile)
{
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 0, ";");
if (!end($line_of_text)) {
// Supprime les lignes vides du csv.
array_pop($line_of_text);
}
}
fclose($file_handle);
return $line_of_text;
}
/**
* Is the file to be ignored
*
* @param string $filename
* @return boolean
*/
protected function fileHasToBeIgnored(string $filename): bool
{
return in_array($filename, $this->ignoreFiles) or !$this->checkExtension($filename, 'csv');
}
/**
* converts an array to a csv stream
*
* @param array $data
* @param string $delimiter
* @param string $enclosure
* @param string $escape_char
*
* @return string
*/
protected function array2csv(
array $data,
string $delimiter = ',',
string $enclosure = '"',
string $escape_char = "\\"
) {
$f = fopen('php://memory', 'r+');
foreach ($data as $item) {
fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
}
rewind($f);
return stream_get_contents($f);
}
}

View file

@ -1,123 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\Order;
class CsvOrderWrite extends CsvJobs
{
/**
* The Laravel disk name
*
* @var string
*/
protected $disk = 'orders';
/**
* The Order to be exported
*
* @var Order
* */
private $order;
/**
* 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",
];
/**
* correspondance unité d'appro/mode de commande
*
* @var array
*/
private $mode_commande = [
"COLIS" => "UEA",
"KGM" => "KGM",
"PCE" => "UB"
];
/**
* Create a new job instance.
*
* @param Order $order
*
* @return void
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
/** @var Filesystem */
$disk = $this->getDisk();
/** @var OrderLine */
$orderlines = $this->order->lines;
/** @var Customers */
$customer = $this->order->customer;
/** @var Timeslot */
$timeslot = $this->order->timeslot;
// Insertion de la ligne d'en-têtes.
$csvlines[] = $this->header;
foreach ($orderlines as $orderline) {
$product = $orderline->product;
// COLIS, KGM, PCE (faut il introduire un nouveau champs type unité de commande).
$unité_commande = $product->order_unit ?? 'PCE';
$oneline = [
$product->foodProduct->no_art,
// 21/01/2022 : suppression du pcb ici
$customer->salarie->matricule,
$product->reference,
"commande ferme",
$unité_commande,
// UEA,KGM,UB (respectivement selon le champ précédent).
$this->mode_commande[$unité_commande],
// On vide la colonne pcb comme demandé.
"",
$timeslot->day->format('d/m/Y'),
$orderline->quantity,
$this->order->reference,
];
$csvlines[] = $oneline;
}//end foreach
$data = $this->array2csv($csvlines, ";");
$outputfilename = "BTQ_" .
$this->order->reference . "_" .
$customer->salarie->matricule . "_" .
$customer->salarie->nom . "_" .
$customer->salarie->prenom .
"_001.csv";
$disk->put($outputfilename, $data);
// Send this file by mail.
}
}

View file

@ -0,0 +1,157 @@
<?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);
}
}

View file

@ -75,11 +75,6 @@ return [
'root' => storage_path('salaries'),
],
'orders' => [
'driver' => 'local',
'root' => storage_path('orders/out'),
],
'regate' => [
'driver' => 'local',
'root' => storage_path('orders/in'),

View file

@ -88,6 +88,10 @@ return [
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'to' => [
'regate' => env('MAIL_TO_REGATE', 'btqcnc.ldceai@ldc.fr'),
],
'contactsList' => [
'serviceweb' => explode(",", env('MAIL_LIST_SERVICEWEB', 'informatique.internet@ldc.fr')),
'invalidInco' => explode(