Failed Conditions
Pull Request — 4.0 (#3771)
by k-yamamura
17:59 queued 10:53
created

OrderPdfService   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 662
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.69%

Importance

Changes 0
Metric Value
dl 0
loc 662
ccs 226
cts 229
cp 0.9869
rs 8.898
c 0
b 0
f 0
wmc 43
lcom 1
cbo 8

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 32 1
B makePdf() 0 63 5
A outputPdf() 0 4 1
A getPdfFileName() 0 12 3
A Footer() 0 4 1
A addPdfPage() 0 11 1
A renderShopData() 0 27 2
A renderMessageData() 0 6 1
A renderEtcData() 0 20 1
A renderTitle() 0 17 1
A renderOrderData() 0 53 3
B renderOrderDetailData() 0 87 7
A lfText() 0 12 1
B setFancyTable() 0 76 7
A setBasePosition() 0 11 3
A setDefaultData() 0 15 3
A backupFont() 0 7 1
A restoreFont() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like OrderPdfService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OrderPdfService, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service;
15
16
use Eccube\Application;
17
use Eccube\Common\EccubeConfig;
18
use Eccube\Entity\BaseInfo;
19
use Eccube\Entity\OrderItem;
20
use Eccube\Entity\Shipping;
21
use Eccube\Repository\BaseInfoRepository;
22
use Eccube\Repository\OrderPdfRepository;
23
use Eccube\Repository\OrderRepository;
24
use Eccube\Repository\ShippingRepository;
25
use Eccube\Twig\Extension\EccubeExtension;
26
use setasign\Fpdi\TcpdfFpdi;
27
28
/**
29
 * Class OrderPdfService.
30
 * Do export pdf function.
31
 */
32
class OrderPdfService extends TcpdfFpdi
33
{
34
    /** @var OrderRepository */
35
    protected $orderRepository;
36
37
    /** @var ShippingRepository */
38
    protected $shippingRepository;
39
40
    /** @var OrderPdfRepository */
41
    protected $orderPdfRepository;
42
43
    /** @var TaxRuleService */
44
    protected $taxRuleService;
45
46
    /**
47
     * @var Application
48
     */
49
    private $eccubeConfig;
50
51
    /**
52
     * @var EccubeExtension
53
     */
54
    private $eccubeExtension;
55
56
    // ====================================
57
    // 定数宣言
58
    // ====================================
59
60
    /** ダウンロードするPDFファイルのデフォルト名 */
61
    const DEFAULT_PDF_FILE_NAME = 'nouhinsyo.pdf';
62
63
    /** FONT ゴシック */
64
    const FONT_GOTHIC = 'kozgopromedium';
65
    /** FONT 明朝 */
66
    const FONT_SJIS = 'kozminproregular';
67
68
    // ====================================
69
    // 変数宣言
70
    // ====================================
71
72
    /** @var BaseInfo */
73
    public $baseInfoRepository;
74
75
    /** 購入詳細情報 ラベル配列
76
     * @var array
77
     */
78
    private $labelCell = [];
79
80
    /*** 購入詳細情報 幅サイズ配列
81
     * @var array
82
     */
83
    private $widthCell = [];
84
85
    /** 最後に処理した注文番号 @var string */
86
    private $lastOrderId = null;
87
88
    // --------------------------------------
89
    // Font情報のバックアップデータ
90
    /** @var string フォント名 */
91
    private $bakFontFamily;
92
    /** @var string フォントスタイル */
93
    private $bakFontStyle;
94
    /** @var string フォントサイズ */
95
    private $bakFontSize;
96
    // --------------------------------------
97
98
    // lfTextのoffset
99
    private $baseOffsetX = 0;
100
    private $baseOffsetY = -4;
101
102
    /** ダウンロードファイル名 @var string */
103
    private $downloadFileName = null;
104
105
    /** 発行日 @var string */
106
    private $issueDate = '';
107
108
    /**
109
     * OrderPdfService constructor.
110
     *
111
     * @param EccubeConfig $eccubeConfig
112
     * @param OrderRepository $orderRepository
113
     * @param ShippingRepository $shippingRepository
114
     * @param TaxRuleService $taxRuleService
115
     * @param BaseInfoRepository $baseInfoRepository
116
     */
117 28
    public function __construct(EccubeConfig $eccubeConfig, OrderRepository $orderRepository, ShippingRepository $shippingRepository, TaxRuleService $taxRuleService, BaseInfoRepository $baseInfoRepository, EccubeExtension $eccubeExtension)
118
    {
119 28
        $this->eccubeConfig = $eccubeConfig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $eccubeConfig of type object<Eccube\Common\EccubeConfig> is incompatible with the declared type object<Eccube\Application> of property $eccubeConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
120 28
        $this->baseInfoRepository = $baseInfoRepository->get();
121 28
        $this->orderRepository = $orderRepository;
122 28
        $this->shippingRepository = $shippingRepository;
123 28
        $this->taxRuleService = $taxRuleService;
124 28
        $this->eccubeExtension = $eccubeExtension;
125 28
        parent::__construct();
126
127
        // 購入詳細情報の設定を行う
128
        // 動的に入れ替えることはない
129 28
        $this->labelCell[] = '商品名 / 商品コード';
130 28
        $this->labelCell[] = '数量';
131 28
        $this->labelCell[] = '単価';
132 28
        $this->labelCell[] = '金額(税込)';
133 28
        $this->widthCell = [110.3, 12, 21.7, 24.5];
134
135
        // Fontの設定しておかないと文字化けを起こす
136 28
        $this->SetFont(self::FONT_SJIS);
137
138
        // PDFの余白(上左右)を設定
139 28
        $this->SetMargins(15, 20);
140
141
        // ヘッダーの出力を無効化
142 28
        $this->setPrintHeader(false);
143
144
        // フッターの出力を無効化
145 28
        $this->setPrintFooter(true);
146 28
        $this->setFooterMargin();
147 28
        $this->setFooterFont([self::FONT_SJIS, '', 8]);
148
    }
149
150
    /**
151
     * 注文情報からPDFファイルを作成する.
152
     *
153
     * @param array $formData
154
     *                        [KEY]
155
     *                        ids: 注文番号
156
     *                        issue_date: 発行日
157
     *                        title: タイトル
158
     *                        message1: メッセージ1行目
159
     *                        message2: メッセージ2行目
160
     *                        message3: メッセージ3行目
161
     *                        note1: 備考1行目
162
     *                        note2: 備考2行目
163
     *                        note3: 備考3行目
164
     *
165
     * @return bool
166
     */
167 3
    public function makePdf(array $formData)
168
    {
169
        // 発行日の設定
170 3
        $this->issueDate = '作成日: '.$formData['issue_date']->format('Y年m月d日');
171
        // ダウンロードファイル名の初期化
172 3
        $this->downloadFileName = null;
173
174
        // データが空であれば終了
175 3
        if (!$formData['ids']) {
176
            return false;
177
        }
178
179
        // 出荷番号をStringからarrayに変換
180 3
        $ids = explode(',', $formData['ids']);
181
182
        // 空文字列の場合のデフォルトメッセージを設定する
183 3
        $this->setDefaultData($formData);
184
185
        foreach ($ids as $id) {
186 3
            $this->lastOrderId = $id;
187 3
188
            // 出荷番号から出荷情報を取得する
189 3
            /** @var Shipping $Shipping */
190 3
            $Shipping = $this->shippingRepository->find($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $Shipping is correct as $this->shippingRepository->find($id) (which targets Doctrine\ORM\EntityRepository::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
191
            if (!$Shipping) {
192
                // 出荷情報の取得ができなかった場合
193
                continue;
194 3
            }
195 3
196
            // テンプレートファイルを読み込む
197
            $Order = $Shipping->getOrder();
198
            if ($Order->isMultiple()) {
199
                // 複数配送の時は読み込むテンプレートファイルを変更する
200
                $userPath = $this->eccubeConfig->get('eccube_html_admin_dir').'/assets/pdf/nouhinsyo_multiple.pdf';
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Eccube\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201 3
            } else {
202
                $userPath = $this->eccubeConfig->get('eccube_html_admin_dir').'/assets/pdf/nouhinsyo.pdf';
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Eccube\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
            }
204 3
            $this->setSourceFile($userPath);
205
206
            // PDFにページを追加する
207 3
            $this->addPdfPage();
208
209
            // タイトルを描画する
210 3
            $this->renderTitle($formData['title']);
211
212
            // 店舗情報を描画する
213 3
            $this->renderShopData();
214
215
            // 注文情報を描画する
216 3
            $this->renderOrderData($Shipping);
217
218
            // メッセージを描画する
219 3
            $this->renderMessageData($formData);
220
221
            // 出荷詳細情報を描画する
222 3
            $this->renderOrderDetailData($Shipping);
223
224
            // 備考を描画する
225
            $this->renderEtcData($formData);
226
        }
227
228
        return true;
229
    }
230 3
231
    /**
232 3
     * PDFファイルを出力する.
233
     *
234
     * @return string|mixed
235
     */
236
    public function outputPdf()
237
    {
238
        return $this->Output($this->getPdfFileName(), 'S');
239
    }
240
241 3
    /**
242
     * PDFファイル名を取得する
243 3
     * PDFが1枚の時は注文番号をファイル名につける.
244 3
     *
245
     * @return string ファイル名
246 3
     */
247 3
    public function getPdfFileName()
248 3
    {
249
        if (!is_null($this->downloadFileName)) {
250
            return $this->downloadFileName;
251 3
        }
252
        $this->downloadFileName = self::DEFAULT_PDF_FILE_NAME;
253
        if ($this->PageNo() == 1) {
254
            $this->downloadFileName = 'nouhinsyo-No'.$this->lastOrderId.'.pdf';
255
        }
256
257 3
        return $this->downloadFileName;
258
    }
259 3
260
    /**
261
     * フッターに発行日を出力する.
262
     */
263
    public function Footer()
264
    {
265 3
        $this->Cell(0, 0, $this->issueDate, 0, 0, 'R');
266
    }
267
268 3
    /**
269
     * 作成するPDFのテンプレートファイルを指定する.
270
     */
271 3
    protected function addPdfPage()
272
    {
273
        // ページを追加
274 3
        $this->AddPage();
275
276
        // テンプレートに使うテンプレートファイルのページ番号を取得
277
        $tplIdx = $this->importPage(1);
278
279
        // テンプレートに使うテンプレートファイルのページ番号を指定
280
        $this->useTemplate($tplIdx, null, null, null, null, true);
281 3
    }
282
283
    /**
284 3
     * PDFに店舗情報を設定する
285
     * ショップ名、ロゴ画像以外はdtb_helpに登録されたデータを使用する.
286
     */
287 3
    protected function renderShopData()
288
    {
289
        // 基準座標を設定する
290 3
        $this->setBasePosition();
291 3
292 3
        // ショップ名
293
        $this->lfText(125, 60, $this->baseInfoRepository->getShopName(), 8, 'B');
294
295 3
        // 都道府県+所在地
296 3
        $text = $this->baseInfoRepository->getAddr01();
297
        $this->lfText(125, 65, $text, 8);
298
        $this->lfText(125, 69, $this->baseInfoRepository->getAddr02(), 8);
299 3
300 3
        // 電話番号
301 3
        $text = 'TEL: '.$this->baseInfoRepository->getPhoneNumber();
302
        $this->lfText(125, 72, $text, 8); //TEL・FAX
303
304
        // メールアドレス
305 3
        if (strlen($this->baseInfoRepository->getEmail01()) > 0) {
306 3
            $text = 'Email: '.$this->baseInfoRepository->getEmail01();
307
            $this->lfText(125, 75, $text, 8); // Email
308
        }
309
310
        // ロゴ画像(app配下のロゴ画像を優先して読み込む)
311
        $logoFile = $this->eccubeConfig->get('eccube_html_admin_dir').'/assets/pdf/logo.png';
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Eccube\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
312
        $this->Image($logoFile, 124, 46, 40);
313
    }
314 3
315
    /**
316 3
     * メッセージを設定する.
317 3
     *
318 3
     * @param array $formData
319
     */
320
    protected function renderMessageData(array $formData)
321
    {
322
        $this->lfText(27, 70, $formData['message1'], 8); //メッセージ1
323
        $this->lfText(27, 74, $formData['message2'], 8); //メッセージ2
324
        $this->lfText(27, 78, $formData['message3'], 8); //メッセージ3
325
    }
326 3
327
    /**
328
     * PDFに備考を設定数.
329 3
     *
330
     * @param array $formData
331 3
     */
332
    protected function renderEtcData(array $formData)
333 3
    {
334 3
        // フォント情報のバックアップ
335
        $this->backupFont();
336 3
337
        $this->Cell(0, 10, '', 0, 1, 'C', 0, '');
338 3
339
        $this->SetFont(self::FONT_GOTHIC, 'B', 9);
340 3
        $this->MultiCell(0, 6, '< 備考 >', 'T', 2, 'L', 0, '');
341 3
342
        $this->SetFont(self::FONT_SJIS, '', 8);
343
344 3
        $this->Ln();
345
        // rtrimを行う
346
        $text = preg_replace('/\s+$/us', '', $formData['note1']."\n".$formData['note2']."\n".$formData['note3']);
347
        $this->MultiCell(0, 4, $text, '', 2, 'L', 0, '');
348
349
        // フォント情報の復元
350
        $this->restoreFont();
351
    }
352 3
353
    /**
354
     * タイトルをPDFに描画する.
355 3
     *
356
     * @param string $title
357
     */
358 3
    protected function renderTitle($title)
359
    {
360
        // 基準座標を設定する
361 3
        $this->setBasePosition();
362 3
363 3
        // フォント情報のバックアップ
364 3
        $this->backupFont();
365
366
        //文書タイトル(納品書・請求書)
367 3
        $this->SetFont(self::FONT_GOTHIC, '', 15);
368
        $this->Cell(0, 10, $title, 0, 2, 'C', 0, '');
369
        $this->Cell(0, 66, '', 0, 2, 'R', 0, '');
370
        $this->Cell(5, 0, '', 0, 0, 'R', 0, '');
371
372
        // フォント情報の復元
373
        $this->restoreFont();
374
    }
375 3
376
    /**
377
     * 購入者情報を設定する.
378 3
     *
379
     * @param Shipping $Shipping
380
     */
381 3
    protected function renderOrderData(Shipping $Shipping)
382
    {
383
        // 基準座標を設定する
384
        $this->setBasePosition();
385
386
        // フォント情報のバックアップ
387 3
        $this->backupFont();
388
389
        // =========================================
390 3
        // 購入者情報部
391 3
        // =========================================
392 3
393
        $Order = $Shipping->getOrder();
394
395 3
        // 購入者都道府県+住所1
396 3
        // $text = $Order->getPref().$Order->getAddr01();
397
        $text = $Shipping->getPref().$Shipping->getAddr01();
398
        $this->lfText(27, 47, $text, 10);
399
        $this->lfText(27, 51, $Shipping->getAddr02(), 10); //購入者住所2
400
401 3
        // 購入者氏名
402
        $text = $Shipping->getName01().' '.$Shipping->getName02().' 様';
403
        $this->lfText(27, 59, $text, 11);
404 3
405 3
        // =========================================
406
        // お買い上げ明細部
407
        // =========================================
408
        $this->SetFont(self::FONT_SJIS, '', 10);
409 3
410
        //ご注文日
411 3
        $orderDate = $Order->getCreateDate()->format('Y/m/d H:i');
412
        if ($Order->getOrderDate()) {
413
            $orderDate = $Order->getOrderDate()->format('Y/m/d H:i');
414 3
        }
415 3
416
        $this->lfText(25, 125, $orderDate, 10);
417 3
        //注文番号
418 3
        $this->lfText(25, 135, $Order->getId(), 10);
419 3
420 3
        // 総合計金額
421
        if (!$Order->isMultiple()) {
422
            $this->SetFont(self::FONT_SJIS, 'B', 15);
423 3
            $paymentTotalText = $this->eccubeExtension->getPriceFilter($Order->getPaymentTotal());
424
425
            $this->setBasePosition(120, 95.5);
426
            $this->Cell(5, 7, '', 0, 0, '', 0, '');
427
            $this->Cell(67, 8, $paymentTotalText, 0, 2, 'R', 0, '');
428
            $this->Cell(0, 45, '', 0, 2, '', 0, '');
429
        }
430
431 3
        // フォント情報の復元
432
        $this->restoreFont();
433 3
    }
434
435
    /**
436
     * 購入商品詳細情報を設定する.
437
     *
438
     * @param Shipping $Shipping
439 3
     */
440
    protected function renderOrderDetailData(Shipping $Shipping)
441 3
    {
442
        $arrOrder = [];
443 3
        // テーブルの微調整を行うための購入商品詳細情報をarrayに変換する
444
445 3
        // =========================================
446 3
        // 受注詳細情報
447 3
        // =========================================
448 1
        $i = 0;
449
        /* @var OrderItem $OrderItem */
450 2
        foreach ($Shipping->getOrderItems() as $OrderItem) {
451
            // class categoryの生成
452
            $classCategory = '';
453
            /** @var OrderItem $OrderItem */
454
            if ($OrderItem->getClassCategoryName1()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $OrderItem->getClassCategoryName1() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
455 3
                $classCategory .= ' [ '.$OrderItem->getClassCategoryName1();
456
                if ($OrderItem->getClassCategoryName2() == '') {
457 3
                    $classCategory .= ' ]';
458
                } else {
459 3
                    $classCategory .= ' * '.$OrderItem->getClassCategoryName2().' ]';
460
                }
461 3
            }
462
463 3
            // product
464
            $productName = $OrderItem->getProductName();
465
            if ($OrderItem->getProductCode()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $OrderItem->getProductCode() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
466 3
                $productName .= ' / '.$OrderItem->getProductName();
467
            }
468
            if ($classCategory) {
469
                $productName .= ' / '.$classCategory;
470
            }
471 3
            $arrOrder[$i][0] = $productName;
472 3
            // 購入数量
473 3
            $arrOrder[$i][1] = number_format($OrderItem->getQuantity());
474 3
            // 税込金額(単価)
475
            $arrOrder[$i][2] = $this->eccubeExtension->getPriceFilter($OrderItem->getPrice());
476 3
            // 小計(商品毎)
477 3
            $arrOrder[$i][3] = $this->eccubeExtension->getPriceFilter($OrderItem->getTotalPrice());
478 3
479 3
            ++$i;
480 3
        }
481
482 3
        $Order = $Shipping->getOrder();
483 3
484 3
        if (!$Order->isMultiple()) {
485 3
            // =========================================
486 3
            // 小計
487
            // =========================================
488 3
            $arrOrder[$i][0] = '';
489 3
            $arrOrder[$i][1] = '';
490 3
            $arrOrder[$i][2] = '';
491 3
            $arrOrder[$i][3] = '';
492 3
493
            ++$i;
494 3
            $arrOrder[$i][0] = '';
495 3
            $arrOrder[$i][1] = '';
496 3
            $arrOrder[$i][2] = '商品合計';
497 3
            $arrOrder[$i][3] = $this->eccubeExtension->getPriceFilter($Order->getSubtotal());
498 3
499
            ++$i;
500 3
            $arrOrder[$i][0] = '';
501 3
            $arrOrder[$i][1] = '';
502 3
            $arrOrder[$i][2] = '送料';
503 3
            $arrOrder[$i][3] = $this->eccubeExtension->getPriceFilter($Order->getDeliveryFeeTotal());
504 3
505
            ++$i;
506
            $arrOrder[$i][0] = '';
507 3
            $arrOrder[$i][1] = '';
508
            $arrOrder[$i][2] = '手数料';
509
            $arrOrder[$i][3] = $this->eccubeExtension->getPriceFilter($Order->getCharge());
510
511
            ++$i;
512
            $arrOrder[$i][0] = '';
513
            $arrOrder[$i][1] = '';
514
            $arrOrder[$i][2] = '値引き';
515
            $arrOrder[$i][3] = '- '.$this->eccubeExtension->getPriceFilter($Order->getDiscount());
516
517
            ++$i;
518
            $arrOrder[$i][0] = '';
519 3
            $arrOrder[$i][1] = '';
520
            $arrOrder[$i][2] = '請求金額';
521
            $arrOrder[$i][3] = $this->eccubeExtension->getPriceFilter($Order->getPaymentTotal());
522 3
        }
523 3
524
        // PDFに設定する
525 3
        $this->setFancyTable($this->labelCell, $arrOrder, $this->widthCell);
526 3
    }
527
528
    /**
529 3
     * PDFへのテキスト書き込み
530
     *
531
     * @param int    $x     X座標
532
     * @param int    $y     Y座標
533
     * @param string $text  テキスト
534
     * @param int    $size  フォントサイズ
535
     * @param string $style フォントスタイル
536
     */
537
    protected function lfText($x, $y, $text, $size = 0, $style = '')
538
    {
539
        // 退避
540
        $bakFontStyle = $this->FontStyle;
541 3
        $bakFontSize = $this->FontSizePt;
542
543
        $this->SetFont('', $style, $size);
544 3
        $this->Text($x + $this->baseOffsetX, $y + $this->baseOffsetY, $text);
545
546
        // 復元
547 3
        $this->SetFont('', $bakFontStyle, $bakFontSize);
548
    }
549
550 3
    /**
551 3
     * Colored table.
552 3
     *
553 3
     * TODO: 後の列の高さが大きい場合、表示が乱れる。
554 3
     *
555 3
     * @param array $header 出力するラベル名一覧
556
     * @param array $data   出力するデータ
557
     * @param array $w      出力するセル幅一覧
558 3
     */
559 3
    protected function setFancyTable($header, $data, $w)
560 3
    {
561 3
        // フォント情報のバックアップ
562
        $this->backupFont();
563 3
564
        // 開始座標の設定
565
        $this->setBasePosition(0, 149);
566 3
567 3
        // Colors, line width and bold font
568 3
        $this->SetFillColor(216, 216, 216);
569
        $this->SetTextColor(0);
570 3
        $this->SetDrawColor(0, 0, 0);
571 3
        $this->SetLineWidth(.3);
572 3
        $this->SetFont(self::FONT_SJIS, 'B', 8);
573
        $this->SetFont('', 'B');
574 3
575 3
        // Header
576 3
        $this->Cell(5, 7, '', 0, 0, '', 0, '');
577
        $count = count($header);
578
        for ($i = 0; $i < $count; ++$i) {
579 3
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
580 3
        }
581
        $this->Ln();
582
583
        // Color and font restoration
584 3
        $this->SetFillColor(235, 235, 235);
585
        $this->SetTextColor(0);
586
        $this->SetFont('');
587 3
        // Data
588 3
        $fill = 0;
589
        $h = 4;
590
        foreach ($data as $row) {
591
            // 行のの処理
592
            $i = 0;
593 3
            $h = 4;
594
            $this->Cell(5, $h, '', 0, 0, '', 0, '');
595 3
596 3
            // Cellの高さを保持
597 3
            $cellHeight = 0;
598 3
            foreach ($row as $col) {
599 3
                // 列の処理
600 3
                // TODO: 汎用的ではない処理。この指定は呼び出し元で行うようにしたい。
601 3
                // テキストの整列を指定する
602 3
                $align = ($i == 0) ? 'L' : 'R';
603
604 3
                // セル高さが最大値を保持する
605
                if ($h >= $cellHeight) {
606 3
                    $cellHeight = $h;
607
                }
608 3
609
                // 最終列の場合は次の行へ移動
610 3
                // (0: 右へ移動(既定)/1: 次の行へ移動/2: 下へ移動)
611 3
                $ln = ($i == (count($row) - 1)) ? 1 : 0;
612 3
613
                $this->MultiCell(
614
                    $w[$i], // セル幅
615 3
                    $cellHeight, // セルの最小の高さ
616
                    $col, // 文字列
617
                    1, // 境界線の描画方法を指定
618
                    $align, // テキストの整列
619
                    $fill, // 背景の塗つぶし指定
620
                    $ln                 // 出力後のカーソルの移動方法
621
                );
622
                $h = $this->getLastH();
623
624 3
                ++$i;
625
            }
626
            $fill = !$fill;
627 3
        }
628
        $this->Cell(5, $h, '', 0, 0, '', 0, '');
629
        $this->Cell(array_sum($w), 0, '', 'T');
630 3
        $this->SetFillColor(255);
631 3
632 3
        // フォント情報の復元
633 3
        $this->restoreFont();
634
    }
635
636
    /**
637
     * 基準座標を設定する.
638
     *
639
     * @param int $x
640
     * @param int $y
641 3
     */
642
    protected function setBasePosition($x = null, $y = null)
643
    {
644 3
        // 現在のマージンを取得する
645 3
        $result = $this->getMargins();
646 3
647 3
        // 基準座標を指定する
648
        $actualX = is_null($x) ? $result['left'] : $x;
649
        $this->SetX($actualX);
650 3
        $actualY = is_null($y) ? $result['top'] : $y;
651 3
        $this->SetY($actualY);
652 3
    }
653
654
    /**
655
     * データが設定されていない場合にデフォルト値を設定する.
656
     *
657
     * @param array $formData
658
     */
659
    protected function setDefaultData(array &$formData)
660 3
    {
661
        $defaultList = [
662
            'title' => trans('admin.order.delivery_note_title__default'),
663 3
            'message1' => trans('admin.order.delivery_note_message__default1'),
664 3
            'message2' => trans('admin.order.delivery_note_message__default2'),
665 3
            'message3' => trans('admin.order.delivery_note_message__default3'),
666
        ];
667
668
        foreach ($defaultList as $key => $value) {
669
            if (is_null($formData[$key])) {
670
                $formData[$key] = $value;
671 3
            }
672
        }
673 3
    }
674
675
    /**
676
     * Font情報のバックアップ.
677
     */
678
    protected function backupFont()
679
    {
680
        // フォント情報のバックアップ
681
        $this->bakFontFamily = $this->FontFamily;
682
        $this->bakFontStyle = $this->FontStyle;
683
        $this->bakFontSize = $this->FontSizePt;
684
    }
685
686
    /**
687
     * Font情報の復元.
688
     */
689
    protected function restoreFont()
690
    {
691
        $this->SetFont($this->bakFontFamily, $this->bakFontStyle, $this->bakFontSize);
692
    }
693
}
694