auditionadmin/app/Http/Controllers/PdfInvoiceController.php

144 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\School;
use App\Services\Invoice\InvoiceDataService;
use Codedge\Fpdf\Fpdf\Fpdf;
use Illuminate\Http\Request;
use function auditionSetting;
class PdfInvoiceController extends Controller
{
protected $pdf;
protected $margin = .3;
protected $entriesPerPage = 45;
protected $invoiceTitle;
protected $paymentAddress;
protected $school;
protected $invoiceDataService;
protected $invoiceData;
/**
* Handle the incoming request.
*/
public function __construct(InvoiceDataService $invoiceDataService)
{
$this->invoiceDataService = $invoiceDataService;
// Set up our PDF
$this->pdf = new Fpdf('P', 'in', 'letter');
$this->pdf->AliasNbPages();
$this->pdf->SetMargins($this->margin, $this->margin);
$this->pdf->SetAutoPageBreak(false);
}
public function __invoke(Request $request, School $school)
{
$this->school = $school;
$this->invoiceData = $this->invoiceDataService->allData($school->id);
$this->invoiceTitle = auditionSetting('auditionAbbreviation').' entry report for '.$this->school->name;
$this->paymentAddress = auditionSetting('auditionName')."\n".
auditionSetting('payment_address')."\n".
auditionSetting('payment_city').', '.auditionSetting('payment_state').' '.auditionSetting('payment_zip');
$this->newInvoicePage();
$this->itemizeEntries();
$this->entryTotals();
if (auditionSetting('school_fee') > 0) {
$this->schoolFee(number_format(auditionSetting('school_fee') / 100, 2));
}
$this->grandTotal();
$this->output();
return redirect()->back();
}
protected function newInvoicePage()
{
$this->pdf->AddPage();
$this->pdf->SetFont('Arial', 'B', 12);
$this->pdf->cell(4, .3, $this->invoiceTitle, 0, 1);
$this->pdf->SetFont('Arial', '', 9);
$this->pdf->cell(4, .1, 'Reflects entries as of '.date('g:i A').' on '.date('F j, Y'), 0, 1);
$this->pdf->cell(4, .15, 'Additional entries may result in additional charges');
$this->pdf->setXY(-3, $this->margin + .45);
$this->pdf->cell(0, 0, 'Page '.$this->pdf->PageNo().' of {nb}', 0, 0, 'R');
$this->pdf->line($this->margin, .6 + $this->margin, 8.5 - $this->margin, .6 + $this->margin);
$this->pdf->setXY($this->margin, $this->margin + .65);
$this->pdf->SetFont('Arial', 'B', 9);
$this->pdf->cell(0, .15, 'SEND PAYMENT TO:', 0, 1);
$this->pdf->SetFont('Arial', '', 9);
$this->pdf->MultiCell(0, .12, $this->paymentAddress);
$this->pdf->setXY($this->margin, $this->margin + 1.4);
$this->pdf->SetFont('Arial', 'B', 9);
$this->pdf->Cell(2.1, .15, 'Student Name', 1);
$this->pdf->Cell(2.1, .15, 'Instrument', 1);
$this->pdf->Cell(1.7, .15, 'Entry Timestamp', 1);
$this->pdf->Cell(1, .15, 'Entry Fee', 1);
$this->pdf->Cell(1, .15, 'Late Fee', 1, 1);
$this->pdf->SetFont('Arial', '', 9);
}
protected function itemizeEntries()
{
$entriesThisPage = 0;
foreach ($this->invoiceData['lines'] as $line) {
if ($entriesThisPage >= $this->entriesPerPage) {
$this->newInvoicePage();
$entriesThisPage = 0;
}
$this->pdf->Cell(2.1, .15, $line['student_name'], 1);
$this->pdf->Cell(2.1, .15, $line['audition'], 1);
$this->pdf->Cell(1.7, .15, $line['entry_timestamp'], 1);
$this->pdf->Cell(1, .15, number_format($line['entry_fee'], 2), 1);
$this->pdf->Cell(1, .15, number_format($line['late_fee'], 2), 1, 1);
$entriesThisPage++;
}
}
protected function entryTotals()
{
$this->pdf->SetFont('Arial', 'B', 9);
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(1.7, .3, 'TOTAL', 1);
$this->pdf->Cell(1, .3, '$'.number_format($this->invoiceData['linesTotal'], 2), 1);
$this->pdf->Cell(1, .3, '$'.number_format($this->invoiceData['lateFeesTotal'], 2), 1, 1);
}
protected function schoolFee($fee)
{
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(1.7, .3, 'SCHOOL FEE', 1);
$this->pdf->Cell(2, .3, '$'.$fee, 1, 1);
}
protected function grandTotal()
{
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(2.1, .3, '', 0);
$this->pdf->Cell(1.7, .3, 'GRAND TOTAL', 1);
$this->pdf->Cell(2, .3, '$'.number_format($this->invoiceData['grandTotal'], 2), 1);
}
public function output($dest = 'D', $name = null)
{
if (! $name) {
$name = auditionSetting('auditionAbbreviation').' Invoice for '.$this->school->name.'.pdf';
$this->pdf->Output($dest, $name);
}
}
}