<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\Controller;use App\Core\Request;use App\Services\DeliveryService;use App\Services\InvoiceService;use App\Services\OrderService;use App\Support\CommercialDocumentPdf;
final class CommercialDocumentPdfController extends Controller{
 public function quote(Request$r):never{$id=(int)$r->param('id');$p=app()->db()->connection();$s=$p->prepare('SELECT * FROM quotes WHERE id=:id AND company_id=:c');$s->execute(['id'=>$id,'c'=>app()->auth()->user()['company_id']]);$h=$s->fetch();if(!$h){http_response_code(404);exit;}$l=$p->prepare('SELECT * FROM quote_lines WHERE quote_id=:id ORDER BY sort_order');$l->execute(['id'=>$id]);$lines=array_map(fn($x)=>['reference'=>$x['reference_snapshot'],'designation'=>$x['designation_snapshot'],'quantity'=>$x['quantity'],'unit'=>$x['unit_snapshot'],'unit_price'=>$x['unit_price'],'tax_rate'=>$x['tax_rate'],'total'=>$x['line_total']],$l->fetchAll());CommercialDocumentPdf::output(['title'=>'Devis','number'=>$h['number'],'company_id'=>$h['company_id'],'customer_id'=>$h['customer_id'],'date'=>$h['quote_date'],'due_date'=>$h['valid_until'],'note'=>$h['customer_notes']?:$h['subject'],'payment_terms'=>$h['payment_terms'],'currency'=>$h['currency_code'],'subtotal'=>$h['subtotal'],'tax_total'=>$h['tax_total'],'total'=>$h['total'],'lines'=>$lines],$h['number'].'.pdf');}
 public function order(Request$r):never{$h=(new OrderService(app()->db()))->full((int)$r->param('id'));$lines=array_map(fn($x)=>['reference'=>$x['reference_snapshot'],'designation'=>$x['designation_snapshot'],'quantity'=>$x['quantity'],'unit'=>$x['unit_snapshot'],'unit_price'=>$x['unit_price'],'tax_rate'=>$x['tax_rate'],'total'=>$x['line_total']],$h['lines']);CommercialDocumentPdf::output(['title'=>'Commande','number'=>$h['number'],'company_id'=>$h['company_id'],'customer_id'=>$h['customer_id'],'date'=>$h['order_date'],'due_date'=>$h['requested_date'],'note'=>$h['customer_notes'],'payment_terms'=>$h['payment_terms'],'currency'=>$h['currency_code'],'subtotal'=>$h['subtotal'],'tax_total'=>$h['tax_total'],'total'=>$h['total'],'lines'=>$lines],$h['number'].'.pdf');}
 public function delivery(Request$r):never{$h=(new DeliveryService(app()->db()))->full((int)$r->param('id'));$lines=array_map(fn($x)=>['reference'=>$x['reference_snapshot'],'designation'=>$x['designation_snapshot'],'ordered'=>$x['ordered_quantity'],'quantity'=>$x['delivery_quantity'],'unit'=>$x['unit_snapshot']],$h['lines']);CommercialDocumentPdf::output(['title'=>'Bon de livraison','number'=>$h['number'],'company_id'=>$h['company_id'],'customer_id'=>$h['customer_id'],'date'=>$h['delivery_date'],'note'=>$h['visible_notes'],'delivery'=>true,'lines'=>$lines],$h['number'].'.pdf');}
 public function invoice(Request$r):never{$h=(new InvoiceService(app()->db()))->full((int)$r->param('id'));$lines=array_map(fn($x)=>['reference'=>$x['reference_snapshot'],'designation'=>$x['designation_snapshot'],'quantity'=>$x['quantity'],'unit'=>$x['unit_snapshot'],'unit_price'=>$x['unit_price'],'tax_rate'=>$x['tax_rate'],'total'=>$x['line_total']],$h['lines']);$title=$h['invoice_type']==='credit_note'?'Avoir':($h['invoice_type']==='proforma'?'Pro forma':'Facture');CommercialDocumentPdf::output(['title'=>$title,'number'=>$h['accounting_number']?:$h['draft_reference'],'company_id'=>$h['company_id'],'customer_id'=>$h['customer_id'],'date'=>$h['invoice_date'],'due_date'=>$h['due_date'],'note'=>$h['visible_notes']?:$h['subject'],'payment_terms'=>$h['payment_terms'],'currency'=>$h['currency_code'],'subtotal'=>$h['subtotal'],'tax_total'=>$h['tax_total'],'total'=>$h['total'],'lines'=>$lines],($h['accounting_number']?:$h['draft_reference']).'.pdf');}
}
