領収証発行サービス
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.

102 lines
2.9KB

  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. if ($order->status_receipt_download_datetime === null) {
  31. $order->status_receipt_download_datetime = DateUtil::now();
  32. // イベント登録
  33. DownloadedEvent::dispatch($this->order);
  34. }
  35. $this->save();
  36. return $ret;
  37. }
  38. public function getA4File(): A4Receipt
  39. {
  40. $ret = $this->makeA4()
  41. ->output();
  42. $file = new A4Receipt();
  43. $file->put($ret);
  44. return $file;
  45. }
  46. protected function makeA4(): PdfWrapper
  47. {
  48. $data = $this->getPDFData();
  49. $pdf = PDF::loadView('pdf/receipt_a4', $data);
  50. // はがきサイズを指定
  51. $ret = $pdf->setPaper('A4')
  52. ->setOption('encoding', 'utf-8');
  53. return $ret;
  54. }
  55. public function downlaodLetter()
  56. {
  57. $data = $this->getPDFData();
  58. $pdf = PDF::loadView('pdf/receipt_letter', $data);
  59. // はがきサイズを指定
  60. $ret = $pdf->setOption('page-height', 148)
  61. ->setOption('page-width', 100)
  62. ->setOption('encoding', 'utf-8')
  63. ->inline();
  64. return $ret;
  65. }
  66. protected function getPDFData(?ReceiptIssuingOrder $order = null)
  67. {
  68. if ($order === null) {
  69. $order = $this->order;
  70. }
  71. $tax = $order->receiptIssuingOrderTaxes->first() ?? new ReceiptIssuingOrderTax();
  72. return [
  73. ...$order->toArray(),
  74. ReceiptIssuingOrder::COL_NAME_STATUS_RECEIPT_CONFIRM_DATETIME => $order->status_receipt_confirm_datetime->format('Y/m/d'),
  75. ReceiptIssuingOrder::COL_NAME_RECEIPT_USE_DATE => $order->receipt_use_date->format('Y/m/d'),
  76. ReceiptIssuingOrder::COL_NAME_RECEIPT_AMOUNT => number_format($order->receipt_amount),
  77. 'pref_name' => PrefCode::getName($order->mail_pref_code),
  78. ReceiptIssuingOrderTax::COL_NAME_TAX_RATE => data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_RATE, 0) ?? 0,
  79. ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT => number_format(data_get($tax, ReceiptIssuingOrderTax::COL_NAME_TAX_AMOUNT, 0)) ?? 0,
  80. ];
  81. }
  82. }