Added TrsrDB::autobalance(). Renamings and fixes.
Renamed "debts" to "arrears", because of psychologically slightly more positive connotations. Renamed field "Id" of table Credit to "credId" in analogy to field "billId" of table Debit.
This commit is contained in:
14
t/schema.sql
14
t/schema.sql
@@ -27,15 +27,15 @@ INSERT INTO Debit VALUES ("MB1605-john", "john", 1, "2016-05-01", "Membership fe
|
||||
SELECT "ID: credit promise debt";
|
||||
SELECT "--------------------------------";
|
||||
|
||||
SELECT ID || ":", credit, '+' || promised, debt * -1 FROM Balance WHERE ID in ("john", "Club");
|
||||
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("john", "Club");
|
||||
|
||||
SELECT "# Reflect john paying its bills all at once ...";
|
||||
INSERT INTO Transfer (billId, fromCredit) VALUES ("MB1605-john", 2), ("MB1606-john", 2), ("MB1607-john", 2), ("MB1608-john", 2), ("MB1609-john", 2), ("MB1610-john", 2), ("MB1611-john", 2), ("MB1612-john", 2), ("MB1701-john", 2), ("MB1702-john", 2), ("MB1703-john", 2), ("MB1704-john", 2);
|
||||
SELECT ID || ":", credit, '+' || promised, debt * -1 FROM Balance WHERE ID in ("john", "Club");
|
||||
INSERT INTO Transfer (billId, credId) VALUES ("MB1605-john", 2), ("MB1606-john", 2), ("MB1607-john", 2), ("MB1608-john", 2), ("MB1609-john", 2), ("MB1610-john", 2), ("MB1611-john", 2), ("MB1612-john", 2), ("MB1701-john", 2), ("MB1702-john", 2), ("MB1703-john", 2), ("MB1704-john", 2);
|
||||
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("john", "Club");
|
||||
|
||||
SELECT "# Charge Club with server hosting provided by alex ...";
|
||||
INSERT INTO Transfer (billId, fromCredit) VALUES ("TWX2016/123", 1);
|
||||
SELECT ID || ":", credit, '+' || promised, debt * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
INSERT INTO Transfer (billId, credId) VALUES ("TWX2016/123", 1);
|
||||
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
|
||||
SELECT "# Some updates and deletes that could, unless denied, destroy consistency ...";
|
||||
UPDATE Debit SET paid = 20000 WHERE billId="TWX2016/123";
|
||||
@@ -47,11 +47,11 @@ BEGIN TRANSACTION;
|
||||
DELETE FROM Transfer WHERE billId="TWX2016/123";
|
||||
UPDATE Debit SET value = 20000 WHERE billId="TWX2016/123";
|
||||
DELETE FROM Debit WHERE billId="TWX2016/123"; -- *SHOULD* work
|
||||
SELECT ID || ":", credit, '+' || promised, debt * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
ROLLBACK TRANSACTION;
|
||||
|
||||
SELECT '# But let''s rollback that what-if excurse. This is how it currently is ...';
|
||||
SELECT ID || ":", credit, '+' || promised, debt * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
|
||||
|
||||
SELECT '###################################################################';
|
||||
SELECT '# Now it is your turn: Study the sql code yielding the output above';
|
||||
|
||||
23
t/schema.t
23
t/schema.t
@@ -55,10 +55,10 @@ while ( my ($num, $month) = each %months ) {
|
||||
});
|
||||
}
|
||||
|
||||
is $db->resultset("Account")->find("john")->current_debts->count(), 12,
|
||||
is $db->resultset("Account")->find("john")->current_arrears->count(), 12,
|
||||
"Entering outstanding member fees for john";
|
||||
|
||||
$db->resultset("Account")->find("Club")->add_to_debts({
|
||||
$db->resultset("Account")->find("Club")->add_to_debits({
|
||||
billId => "TWX2016/123",
|
||||
targetCredit => 3,
|
||||
date => "2016-01-15",
|
||||
@@ -68,12 +68,19 @@ $db->resultset("Account")->find("Club")->add_to_debts({
|
||||
|
||||
is $db->resultset("Debit")->search({ debtor => 'Club' })->single->billId, "TWX2016/123", "Invoicing server hosting for club";
|
||||
|
||||
is_deeply { map { $_->ID => {$_->get_columns} } $db->resultset("Balance")->all },
|
||||
{ john => { ID => 'john', credit => 7200, debt => 7200, promised => 0 },
|
||||
Club => { ID => 'Club', credit => 0, debt => 23450, promised => 7200 },
|
||||
alex => { ID => 'alex', credit => 0, debt => 0, promised => 23450 },
|
||||
},
|
||||
"Get balances"
|
||||
is_deeply {
|
||||
map { $_->ID => {$_->get_columns} }
|
||||
$db->resultset("Balance")->all
|
||||
}, {
|
||||
john => { ID => 'john', credit => 7200, arrears => 7200, promised => 0 },
|
||||
Club => { ID => 'Club', credit => 0, arrears => 23450, promised => 7200 },
|
||||
alex => { ID => 'alex', credit => 0, arrears => 0, promised => 23450 },
|
||||
},
|
||||
"Get balances"
|
||||
;
|
||||
|
||||
# Transfer 72 Euro (6 Euro per month) from john's to Club account.
|
||||
# Transfer same 72 Euro from Club account to alex hosting the web site.
|
||||
is $db->autobalance( (q{*} => q{*}) x 2 ), 14400, 'Automatically balanced credits and debits';
|
||||
|
||||
done_testing();
|
||||
|
||||
Reference in New Issue
Block a user