diff --git a/app/Http/Controllers/OrderTunnelController.php b/app/Http/Controllers/OrderTunnelController.php index a5f79b35..f430a6c1 100644 --- a/app/Http/Controllers/OrderTunnelController.php +++ b/app/Http/Controllers/OrderTunnelController.php @@ -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 diff --git a/app/Jobs/CsvJobs.php b/app/Jobs/CsvJobs.php deleted file mode 100644 index e46921c9..00000000 --- a/app/Jobs/CsvJobs.php +++ /dev/null @@ -1,63 +0,0 @@ -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); - } -} diff --git a/app/Jobs/CsvOrderWrite.php b/app/Jobs/CsvOrderWrite.php deleted file mode 100644 index 3a942f9d..00000000 --- a/app/Jobs/CsvOrderWrite.php +++ /dev/null @@ -1,123 +0,0 @@ - "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. - } -} diff --git a/app/Notifications/Regate/AnOrderWasPlaced.php b/app/Notifications/Regate/AnOrderWasPlaced.php new file mode 100644 index 00000000..2d85a830 --- /dev/null +++ b/app/Notifications/Regate/AnOrderWasPlaced.php @@ -0,0 +1,157 @@ + "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); + } +} diff --git a/config/filesystems.php b/config/filesystems.php index 03ed7d29..11469cd6 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -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'), diff --git a/config/mail.php b/config/mail.php index 68b5f483..8fffd3d4 100644 --- a/config/mail.php +++ b/config/mail.php @@ -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(