/
Purchase.php
144 lines (130 loc) · 3.53 KB
/
Purchase.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace biz\api\models\purchase;
use Yii;
use biz\api\base\ActiveRecord;
use biz\api\models\master\Branch;
use biz\api\models\master\Supplier;
use biz\api\models\inventory\GoodsMovement;
/**
* Description of Purchase
*
* @property integer $id
* @property string $number
* @property integer $supplier_id
* @property integer $branch_id
* @property string $date
* @property double $value
* @property double $discount
* @property integer $status
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
*
* @property PurchaseDtl[] $items
* @property GoodsMovement[] $movements
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
*/
class Purchase extends ActiveRecord
{
const STATUS_DRAFT = 10;
const STATUS_PROCESS = 20;
const STATUS_CLOSE = 90;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%purchase}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['status'], 'default', 'value' => self::STATUS_DRAFT],
[['items'],'resolveValue'],
[['supplier_id', 'branch_id', 'date', 'value', 'items'], 'required'],
[['supplier_id', 'branch_id', 'status'], 'integer'],
[['status'], 'in', 'range' => [self::STATUS_DRAFT, self::STATUS_PROCESS, self::STATUS_CLOSE]],
[['discount', 'value'], 'number'],
[['number'], 'string', 'max' => 16],
];
}
public function resolveValue()
{
$value = 0.0;
foreach ($this->items as $item) {
$value += $item->qty * $item->price;
}
$this->value = $value;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'number' => 'Number',
'supplier_id' => 'Supplier ID',
'branch_id' => 'Branch ID',
'date' => 'Date',
'value' => 'Value',
'discount' => 'Discount',
'status' => 'Status',
'created_at' => 'Created At',
'created_by' => 'Created By',
'updated_at' => 'Updated At',
'updated_by' => 'Updated By',
];
}
public function getItems()
{
return $this->hasMany(PurchaseDtl::className(), ['purchase_id' => 'id']);
}
public function getMovements()
{
return $this->hasMany(GoodsMovement::className(), ['reff_id' => 'id'])
->onCondition(['reff_type' => 100]);
}
public function getSupplier()
{
return $this->hasOne(Supplier::className(), ['id' => 'supplier_id']);
}
public function getBranch()
{
return $this->hasOne(Branch::className(), ['id' => 'branch_id']);
}
public function behaviors()
{
return[
'yii\behaviors\TimestampBehavior',
'yii\behaviors\BlameableBehavior',
[
'class' => 'mdm\autonumber\Behavior',
'digit' => 6,
'attribute' => 'number',
'value' => 'PU' . date('y.?')
],
'biz\api\base\StatusConverter',
'mdm\behaviors\ar\RelationBehavior',
];
}
public function fields()
{
$fields = parent::fields();
$fields['nmStatus'] = 'nmStatus';
return $fields;
}
public function extraFields()
{
return[
'items',
'supplier',
'branch',
'movements'
];
}
}