|
- <?php
-
- namespace App\Logic\ReceiptIssuingOrder;
-
- use App\Codes\PrefCode;
- use App\Events\ReceiptIssuingOrder\DownloadedEvent;
- use App\Exceptions\AppCommonException;
- use App\Files\PDF\Receipt\A4Receipt;
- use App\Files\TmpFile;
- use App\Models\ReceiptIssuingOrder;
- use App\Models\ReceiptIssuingOrderTax;
- use App\Util\DateUtil;
- use Barryvdh\Snappy\PdfWrapper;
- use Illuminate\Database\Eloquent\Collection;
- use PDF;
-
- class PDFDownLoadManager extends ReceiptIssuingOrderManager
- {
-
- public function __construct(
- protected ReceiptIssuingOrder $order,
- ) {
- parent::__construct($order);
- }
-
- public function initForDownloadLetters()
- {
- $this->initialized = true;
- }
-
- public function downlaodA4()
- {
- $order = $this->order;
-
- $ret = $this->makeA4()
- ->inline()
- ->header("Content-Disposition", sprintf('inline; filename=%s', $this->getFileNameA4()));
- if ($order->status_receipt_download_datetime === null) {
-
- $order->status_receipt_download_datetime = DateUtil::now();
-
- // イベント登録
- DownloadedEvent::dispatch($this->order);
- }
- $this->save();
-
- return $ret;
- }
-
- public function getA4File(): A4Receipt
- {
- $ret = $this->makeA4()
- ->output();
-
- $file = new A4Receipt();
- $file->put($ret);
-
- return $file;
- }
-
- protected function makeA4(): PdfWrapper
- {
- $data = $this->getPDFData();
-
- $pdf = PDF::loadView('pdf/receipt_a4', $data);
- // はがきサイズを指定
- $ret = $pdf->setPaper('A4')
- ->setOption('encoding', 'utf-8');
-
- return $ret;
- }
-
- public function downlaodLetter()
- {
- $data = $this->getPDFData();
- $pdf = PDF::loadView('pdf/receipt_letter', $data);
- // はがきサイズを指定
- $ret = $pdf->setOption('page-height', 148)
- ->setOption('page-width', 100)
- ->setOption('encoding', 'utf-8')
- ->inline()
- ->header("Content-Disposition", sprintf('inline; filename=%s', $this->getFileNameLetter()));
-
-
- return $ret;
- }
-
- protected function getPDFData(?ReceiptIssuingOrder $order = null)
- {
- if ($order === null) {
- $order = $this->order;
- }
- $tax = $order->receiptIssuingOrderTaxes->first() ?? new ReceiptIssuingOrderTax();
- return [
- ...$order->toArray(),
- ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_CONFIRM_DATETIME => $order->status_receipt_confirm_datetime->format('Y年m月d日'),
- ReceiptIssuingOrder::COL_NAME_RECEIPT_USE_DATE => $order->receipt_use_date->format('Y年m月d日'),
- ReceiptIssuingOrder::COL_NAME_RECEIPT_AMOUNT => number_format($order->receipt_amount),
- 'pref_name' => PrefCode::getName($order->mail_pref_code),
- ReceiptIssuingOrderTax::COL_NAME_TAX_RATE => data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_RATE, 0) ?? 0,
- ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT => number_format(data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT, 0)) ?? 0,
- ];
- }
-
- protected function getFileNameA4(): string
- {
- return sprintf("領収証_%s_%s.pdf", $this->order->receipt_no, DateUtil::now()->format('YmdHis'));
- }
- protected function getFileNameLetter(): string
- {
- return sprintf("領収証はがき_%s_%s.pdf", $this->order->receipt_no, DateUtil::now()->format('YmdHis'));
- }
- }
|