-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSmboTransferTest.php
179 lines (152 loc) · 5.93 KB
/
SmboTransferTest.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Atk4\Data\Tests;
use Atk4\Data\Schema\TestCase;
use Atk4\Data\Tests\Model\Smbo\Account;
use Atk4\Data\Tests\Model\Smbo\Company;
use Atk4\Data\Tests\Model\Smbo\Payment;
use Atk4\Data\Tests\Model\Smbo\Transfer;
class SmboTransferTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createMigrator()->table('account')
->id()
->field('name')
->create();
$this->createMigrator()->table('document')
->id()
->field('reference')
->field('contact_from_id')
->field('contact_to_id')
->field('doc_type')
->field('amount', ['type' => 'float'])
->create();
$this->createMigrator()->table('payment')
->id()
->field('document_id', ['type' => 'integer'])
->field('account_id', ['type' => 'integer'])
->field('cheque_no')
// ->field('misc_payment', ['type' => 'enum(\'N\',\'Y\')'])
->field('misc_payment')
->field('transfer_document_id')
->create();
}
/**
* Testing transfer between two accounts.
*/
public function testTransfer(): void
{
$aib = (new Account($this->db))->createEntity()->save(['name' => 'AIB']);
$boi = (new Account($this->db))->createEntity()->save(['name' => 'BOI']);
$t = $aib->transfer($boi, 100); // create transfer between accounts
$t->save();
$this->assertEquals(-100, $aib->reload()->get('balance'));
$this->assertEquals(100, $boi->reload()->get('balance'));
$t = new Transfer($this->db);
$data = $t->export(['id', 'transfer_document_id']);
usort($data, function ($e1, $e2) {
return $e1['id'] < $e2['id'] ? -1 : 1;
});
$this->assertSame([
['id' => 1, 'transfer_document_id' => 2],
['id' => 2, 'transfer_document_id' => 1],
], $data);
}
public function testRef(): void
{
// create accounts and payments
$a = new Account($this->db);
$aa = $a->createEntity();
$aa->save(['name' => 'AIB']);
$aa->ref('Payment')->createEntity()->save(['amount' => 10]);
$aa->ref('Payment')->createEntity()->save(['amount' => 20]);
$aa->unload();
$aa = $a->createEntity();
$aa->save(['name' => 'BOI']);
$aa->ref('Payment')->createEntity()->save(['amount' => 30]);
$aa->unload();
// create payment without link to account
$p = new Payment($this->db);
$p->createEntity()->saveAndUnload(['amount' => 40]);
// Account is not loaded, will dump all Payments related to ANY Account
$data = $a->ref('Payment')->export(['amount']);
$this->assertSameExportUnordered([
['amount' => 10.0],
['amount' => 20.0],
['amount' => 30.0],
// ['amount' => 40.0], // will not select this because it is not related to any Account
], $data);
// Account is loaded, will dump all Payments related to that particular Account
$a = $a->load(1);
$data = $a->ref('Payment')->export(['amount']);
$this->assertSameExportUnordered([
['amount' => 10.0],
['amount' => 20.0],
], $data);
}
/*
public function testBasicEntities(): void
{
// Create a new company
$company = new Company($this->db);
$company->set([
'name' => 'Test Company 1',
'director_name' => 'Tester Little',
'type' => 'Limited Company',
'vat_registered' => true,
]);
$company->save();
return;
// Create two new clients, one is sole trader, other is limited company
$client = $company->ref('Client');
[$john_id, $agile_id] = $m->insert([
['name' => 'John Smith Consulting', 'vat_registered' => false],
'Agile Software Limited',
]);
// Insert a first, default invoice for our sole-trader
$john = $company->load($john_id);
$john_invoices = $john->ref('Invoice');
$john_invoices->insertInvoice([
'ref_no' => 'INV1',
'due_date' => (new Date())->add(new DateInterval('2w')), // due in 2 weeks
'lines' => [
['descr' => 'Sold some sweets', 'total_gross' => 100.0],
['descr' => 'Delivery', 'total_gross' => 10.0],
],
]);
// Use custom method to create a sub-nominal
$company->ref('Nominal')->insertSubNominal('Sales', 'Discounted');
// Insert our second invoice using set referencing
$company->ref('Client')->load($agile_id)->refSet('Invoice')->insertInvoice([
'lines' => [
[
'item_id' => $john->ref('Product')->insert('Cat Food'),
'nominal' => 'Sales:Discounted',
'total_net' => 50.0,
'vat_rate' => 23,
// calculates total_gross at 61.5
],
[
'item_id' => $john->ref('Service')->insert('Delivery'),
'total_net' => 10.0,
'vat_rate' => '23%',
// calculates total_gross at 12.3
],
],
]);
// Next we create bank account
$hsbc = $john->ref('Account')->set('name', 'HSBC')->save();
// And each of our invoices will have one new payment
foreach ($john_invoices as $invoice) {
$invoice->ref('Payment')->insert(['amount' => 10.2, 'bank_account_id' => $hsbc]);
}
// Now let's execute report
$debt = $john->add(new Model_Report_Debtors());
// This should give us total amount owed by all clients:
// (100.0 + 10.0) + (61.5 + 12.3) - 10.2 * 2
$this->assertSame(163.4, $debt->sum('amount'));
}
*/
}