領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.4KB

  1. <?php
  2. namespace App\Logic\ReceiptIssuingOrder;
  3. use App\Codes\PrefCode;
  4. use App\Events\ReceiptIssuingOrder\DownloadedEvent;
  5. use App\Exceptions\AppCommonException;
  6. use App\Files\PDF\Receipt\A4Receipt;
  7. use App\Files\TmpFile;
  8. use App\Models\ReceiptIssuingOrder;
  9. use App\Models\ReceiptIssuingOrderTax;
  10. use App\Util\DateUtil;
  11. use Barryvdh\Snappy\PdfWrapper;
  12. use Illuminate\Database\Eloquent\Collection;
  13. use PDF;
  14. class PDFDownLoadManager extends ReceiptIssuingOrderManager
  15. {
  16. public function __construct(
  17. protected ReceiptIssuingOrder $order,
  18. ) {
  19. parent::__construct($order);
  20. }
  21. public function initForDownloadLetters()
  22. {
  23. $this->initialized = true;
  24. }
  25. public function downlaodA4()
  26. {
  27. $order = $this->order;
  28. $ret = $this->makeA4()
  29. ->inline()
  30. ->header("Content-Disposition", sprintf('inline; filename=%s', $this->getFileNameA4()));
  31. if ($order->status_receipt_download_datetime === null) {
  32. $order->status_receipt_download_datetime = DateUtil::now();
  33. // イベント登録
  34. DownloadedEvent::dispatch($this->order);
  35. }
  36. $this->save();
  37. return $ret;
  38. }
  39. public function getA4File(): A4Receipt
  40. {
  41. $ret = $this->makeA4()
  42. ->output();
  43. $file = new A4Receipt();
  44. $file->put($ret);
  45. return $file;
  46. }
  47. protected function makeA4(): PdfWrapper
  48. {
  49. $data = $this->getPDFData();
  50. $pdf = PDF::loadView('pdf/receipt_a4', $data);
  51. // はがきサイズを指定
  52. $ret = $pdf->setPaper('A4')
  53. ->setOption('encoding', 'utf-8');
  54. return $ret;
  55. }
  56. public function downlaodLetter()
  57. {
  58. $data = $this->getPDFData();
  59. $pdf = PDF::loadView('pdf/receipt_letter', $data);
  60. // はがきサイズを指定
  61. $ret = $pdf->setOption('page-height', 148)
  62. ->setOption('page-width', 100)
  63. ->setOption('encoding', 'utf-8')
  64. ->inline()
  65. ->header("Content-Disposition", sprintf('inline; filename=%s', $this->getFileNameLetter()));
  66. return $ret;
  67. }
  68. protected function getPDFData(?ReceiptIssuingOrder $order = null)
  69. {
  70. if ($order === null) {
  71. $order = $this->order;
  72. }
  73. $tax = $order->receiptIssuingOrderTaxes->first() ?? new ReceiptIssuingOrderTax();
  74. return [
  75. ...$order->toArray(),
  76. ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_CONFIRM_DATETIME => $order->status_receipt_confirm_datetime->format('Y年m月d日'),
  77. ReceiptIssuingOrder::COL_NAME_RECEIPT_USE_DATE => $order->receipt_use_date->format('Y年m月d日'),
  78. ReceiptIssuingOrder::COL_NAME_RECEIPT_AMOUNT => number_format($order->receipt_amount),
  79. 'pref_name' => PrefCode::getName($order->mail_pref_code),
  80. ReceiptIssuingOrderTax::COL_NAME_TAX_RATE => data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_RATE, 0) ?? 0,
  81. ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT => number_format(data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT, 0)) ?? 0,
  82. ];
  83. }
  84. protected function getFileNameA4(): string
  85. {
  86. return sprintf("領収証_%s_%s.pdf", $this->order->receipt_no, DateUtil::now()->format('YmdHis'));
  87. }
  88. protected function getFileNameLetter(): string
  89. {
  90. return sprintf("領収証はがき_%s_%s.pdf", $this->order->receipt_no, DateUtil::now()->format('YmdHis'));
  91. }
  92. }