ldc-boutique-cnc/app/Jobs/CsvOrderWrite.php
2022-09-03 16:56:16 +02:00

128 lines
3.1 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Support\Carbon;
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';
// Matricule rh transmis sans les lettres de l'identifiant windows.
$code_client = preg_replace('/[^\d]/', '', $customer->salarie->matricule);
// Convert to date object.
$datLivraison = Carbon::createFromFormat('d/m/Y H:i', $timeslot->day . " " . $timeslot->time);
$oneline = [
$product->foodProduct->no_art,
// 21/01/2022 : suppression du pcb ici
$code_client,
$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é.
"",
$datLivraison->format('d/m/Y'),
$orderline->quantity,
$this->order->reference,
];
$csvlines[] = $oneline;
}//end foreach
$data = $this->array2csv($csvlines, ";");
$outputfilename = "BTQ_" .
$this->order->reference . "_" .
$code_client . "_" .
$customer->salarie->nom . "_" .
$customer->salarie->prenom . "_" .
str_pad($customer->salarie->code_societe, 3, "0", STR_PAD_LEFT) . ".csv";
$disk->put($outputfilename, $data);
// Send this file by mail.
}
}