-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinit.php
120 lines (89 loc) · 2.84 KB
/
init.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
<?php
declare(strict_types=1);
namespace Atk4\MasterCrud\Demo;
require '../vendor/autoload.php';
require 'db.php';
use Atk4\Data\Model;
use Atk4\Data\Persistence;
use Atk4\Ui\App;
use Atk4\Ui\Layout;
use Atk4\Ui\Message;
$app = new App('MasterCrud Demo');
$app->initLayout([Layout\Centered::class]);
// change this as needed
try {
$app->db = new Persistence\Sql('pgsql://root:root@localhost/root');
} catch (\Exception $e) {
$app->add([Message::class, 'Database is not available', 'error'])->text
->addParagraph('Import file demos/mastercrud.pgsql and see demos/db.php')
->addParagraph($e->getMessage());
exit;
}
class Client extends Model
{
public $table = 'client';
protected function init(): void
{
parent::init();
$this->addField('name', ['required' => true]);
$this->addField('address', ['type' => 'text']);
$this->hasMany('Invoices', ['model' => new Invoice()]);
$this->hasMany('Payments', ['model' => new Payment()]);
}
}
class Invoice extends Model
{
public $table = 'invoice';
public $title_field = 'ref_no';
protected function init(): void
{
parent::init();
$this->hasOne('client_id', ['model' => new Client()]);
$this->addField('ref_no');
$this->addField('status', ['enum' => ['draft', 'paid', 'partial']]);
$this->hasMany('Lines', ['model' => new Line()])
->addField('total', ['aggregate' => 'sum']);
$this->hasMany('Allocations', ['model' => new Allocation()]);
}
}
class Line extends Model
{
public $table = 'line';
public $title_field = 'item';
protected function init(): void
{
parent::init();
$this->hasOne('invoice_id', ['model' => new Invoice()]);
$this->addField('item');
$this->addField('qty', ['type' => 'integer']);
$this->addField('price', ['type' => 'money']);
$this->addExpression('total', '[qty]*[price]');
}
}
class Payment extends Model
{
public $table = 'payment';
public $title_field = 'ref_no';
protected function init(): void
{
parent::init();
$this->hasOne('client_id', ['model' => new Client()]);
$this->addField('ref_no');
$this->addField('status', ['enum' => ['draft', 'allocated', 'partial']]);
$this->addField('amount', ['type' => 'money']);
$this->hasMany('Allocations', ['model' => new Allocation()]);
}
}
class Allocation extends Model
{
public $table = 'allocation';
public $title_field = 'title';
protected function init(): void
{
parent::init();
$this->addExpression('title', '\'Alloc \' || [id]');
$this->hasOne('payment_id', ['model' => new Payment()]);
$this->hasOne('invoice_id', ['model' => new Invoice()]);
$this->addField('allocated', ['type' => 'money']);
}
}