feat: add automatic item pickup slip printing via HP ePrint
This commit is contained in:
@@ -9,4 +9,14 @@ use Filament\Resources\Pages\CreateRecord;
|
||||
class CreateCheckout extends CreateRecord
|
||||
{
|
||||
protected static string $resource = CheckoutResource::class;
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
try {
|
||||
$printerService = app(\App\Services\PrinterService::class);
|
||||
$printerService->printForCheckout($this->record);
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error('Auto-print failed from Filament: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,6 +197,14 @@ class NorthCheckoutController extends Controller
|
||||
GROUP BY winning_bids.winning_bidder_num
|
||||
");
|
||||
|
||||
// Automatically print item pickup slips
|
||||
try {
|
||||
$printerService = app(\App\Services\PrinterService::class);
|
||||
$printerService->printForCheckout(\App\Models\Checkout::find($checkout_id));
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error('Auto-print failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return view('checkout_complete', [
|
||||
'checkout_result' => $checkout_id,
|
||||
'checkout_list_results' => $checkout_list_results,
|
||||
|
||||
@@ -141,6 +141,15 @@ class PagesController extends Controller
|
||||
'cc_amount' => $cc_amount,
|
||||
]
|
||||
);
|
||||
|
||||
// Automatically print item pickup slips
|
||||
try {
|
||||
$printerService = app(\App\Services\PrinterService::class);
|
||||
$printerService->printForCheckout(\App\Models\Checkout::find($checkout_result));
|
||||
} catch (\Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error('Auto-print failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return view('checkout_complete', [
|
||||
'checkout_result' => $checkout_result,
|
||||
'checkout_list_results' => $checkout_list_results,
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Attachment;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class PickupSlipMail extends Mailable
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
protected $pdfOutput;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*/
|
||||
public function __construct($pdfOutput)
|
||||
{
|
||||
$this->pdfOutput = $pdfOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message envelope.
|
||||
*/
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Item Pickup Slip',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message content definition.
|
||||
*/
|
||||
public function content(): Content
|
||||
{
|
||||
return new Content(
|
||||
view: 'emails.pickup_slip',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachments for the message.
|
||||
*
|
||||
* @return array<int, Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
return [
|
||||
Attachment::fromData(fn () => $this->pdfOutput, 'pickup_slip.pdf')
|
||||
->withMime('application/pdf'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Dompdf\Dompdf;
|
||||
use Dompdf\Options;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PrinterService
|
||||
{
|
||||
/**
|
||||
* Print the pickup slip for a given Checkout model.
|
||||
*
|
||||
* @param \App\Models\Checkout $checkout
|
||||
* @return void
|
||||
*/
|
||||
public function printForCheckout(\App\Models\Checkout $checkout)
|
||||
{
|
||||
$bidder_num = $checkout->bidder_num;
|
||||
|
||||
$checkout_list_results = \Illuminate\Support\Facades\DB::select("SELECT
|
||||
*, items.item_assigned_num, items.item_desc
|
||||
FROM winning_bids
|
||||
INNER JOIN items AS items
|
||||
ON winning_bids.winning_item_num=items.iditems
|
||||
WHERE winning_bidder_num = $bidder_num
|
||||
");
|
||||
|
||||
$checkout_info_results = \Illuminate\Support\Facades\DB::select("SELECT
|
||||
winning_bids.*,
|
||||
bidders.*,
|
||||
sum(winning_cost) AS total_cost
|
||||
FROM winning_bids
|
||||
INNER JOIN bidders AS bidders
|
||||
ON winning_bids.winning_bidder_num=bidders.idbidders
|
||||
WHERE winning_bidder_num = $bidder_num
|
||||
GROUP BY winning_bids.winning_bidder_num
|
||||
");
|
||||
|
||||
$this->printPickupSlip([
|
||||
'checkout_final_results' => $checkout,
|
||||
'checkout_list_results' => $checkout_list_results,
|
||||
'checkout_info_results' => $checkout_info_results
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the pickup slip by sending it to the HP ePrint email address.
|
||||
*
|
||||
* @param array $checkoutData
|
||||
* @return void
|
||||
*/
|
||||
public function printPickupSlip($checkoutData)
|
||||
{
|
||||
if (!config('services.printer.enabled')) {
|
||||
Log::info('Auto-print is disabled.');
|
||||
return;
|
||||
}
|
||||
|
||||
$printerEmail = config('services.printer.email');
|
||||
if (empty($printerEmail)) {
|
||||
Log::error('Printer email is not configured.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$options = new Options();
|
||||
$options->set('isHtml5ParserEnabled', true);
|
||||
$options->set('isRemoteEnabled', true);
|
||||
$dompdf = new Dompdf($options);
|
||||
|
||||
$html = view('receiptpdf', $checkoutData)->render();
|
||||
$dompdf->loadHtml($html);
|
||||
$dompdf->setPaper('letter', 'portrait');
|
||||
$dompdf->render();
|
||||
$pdfOutput = $dompdf->output();
|
||||
|
||||
Mail::to($printerEmail)->send(new \App\Mail\PickupSlipMail($pdfOutput));
|
||||
|
||||
Log::info('Pickup slip sent to printer: ' . $printerEmail);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to print pickup slip: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user