|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Finance module for HiPanel |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
|
6
|
|
|
* @package hipanel-module-finance |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\behaviors; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\modules\finance\logic\bill\QuantityFormatterFactory; |
|
14
|
|
|
use hipanel\modules\finance\models\Charge; |
|
15
|
|
|
use yii\behaviors\AttributeBehavior; |
|
16
|
|
|
use yii\db\ActiveRecord; |
|
17
|
|
|
|
|
18
|
|
|
class BillQuantity extends AttributeBehavior |
|
19
|
|
|
{ |
|
20
|
|
|
public $quantityAttribute = 'quantity'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var QuantityFormatterFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $qtyFactory; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($config = [], QuantityFormatterFactory $qtyFactory) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($config); |
|
30
|
|
|
$this->qtyFactory = $qtyFactory; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function events() |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => 'transformQuantity', |
|
37
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => 'transformQuantity', |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function transformQuantity() |
|
42
|
|
|
{ |
|
43
|
|
|
// For Bills |
|
44
|
|
|
if ($billQty = $this->transform($this->owner)) { |
|
45
|
|
|
$this->owner->{$this->quantityAttribute} = $billQty->getValue(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// For Charges |
|
49
|
|
|
if (isset($this->owner->charges) && !empty($this->owner->charges)) { |
|
50
|
|
|
foreach ($this->owner->charges as $k => $data) { |
|
51
|
|
|
$charge = new Charge($data); |
|
52
|
|
|
if ($chargeQty = $this->transform($charge)) { |
|
53
|
|
|
$this->owner->charges[$k][$this->quantityAttribute] = $chargeQty->getValue(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function transform($model) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->qtyFactory->create($model); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|