-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathLine.php
48 lines (38 loc) · 1.55 KB
/
Line.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace Atk4\Data\Tests\ContainsMany;
use Atk4\Data\Model;
/**
* Invoice lines model.
*
* @property VatRate $vat_rate_id @Atk4\RefOne()
* @property float $price @Atk4\Field()
* @property float $qty @Atk4\Field()
* @property \DateTime $add_date @Atk4\Field()
* @property string $total_gross @Atk4\Field()
* @property Discount $discounts @Atk4\RefMany()
* @property float $discounts_percent @Atk4\Field()
*/
class Line extends Model
{
protected function init(): void
{
parent::init();
$this->hasOne($this->fieldName()->vat_rate_id, ['model' => [VatRate::class]]);
$this->addField($this->fieldName()->price, ['type' => 'money', 'required' => true]);
$this->addField($this->fieldName()->qty, ['type' => 'float', 'required' => true]);
$this->addField($this->fieldName()->add_date, ['type' => 'datetime']);
$this->addExpression($this->fieldName()->total_gross, function (self $m) {
return $m->price * $m->qty * (1 + $m->vat_rate_id->rate / 100);
});
// each line can have multiple discounts and calculate total of these discounts
$this->containsMany($this->fieldName()->discounts, ['model' => [Discount::class]]);
$this->addCalculatedField($this->fieldName()->discounts_percent, function ($m) {
$total = 0;
foreach ($m->discounts as $d) {
$total += $d->percent;
}
return $total;
});
}
}