Fixes and tests for indirect transfers. Extended balance.

TrsrDB:
    fixed Id -> credId
    autobalance -> make_transfers

TrsrDB::Account:
    added history accessor
TrsrDB::Balance:
    added fieds earned spent and even_until, renamed credit to available
TrsrDB::CurrentArrears:
    fixed account -> debtor in relation declaration
schema.sql:
    fixed triggers
    deactivated trigger enforceFixedCredit
    extended Balance with some more fields
t/schema.{sql,out}
    trivial fixes
t/schema.t added tests:
    Get balances after transfers
    partial use of credit,
    indirect transfers
This commit is contained in:
Florian "flowdy" Heß
2016-07-06 22:28:56 +02:00
parent bc37ee316b
commit 8459e9ae48
8 changed files with 137 additions and 76 deletions

View File

@@ -1,4 +1,4 @@
ID: credit promise debt
ID: availbl promise debt
--------------------------------
Club: 0 +7200 -23450
john: 7200 +0 -7200

View File

@@ -24,18 +24,18 @@ INSERT INTO Debit VALUES ("MB1605-john", "john", 1, "2016-05-01", "Membership fe
("TWX2016/123", "Club", 3, "2016-01-15", "Server Hosting 2016", 23450, 0);
.separator " "
SELECT "ID: credit promise debt";
SELECT "ID: availbl promise debt";
SELECT "--------------------------------";
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("john", "Club");
SELECT ID || ":", available, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("john", "Club");
SELECT "# Reflect john paying its bills all at once ...";
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 ID || ":", available, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("john", "Club");
SELECT "# Charge Club with server hosting provided by alex ...";
INSERT INTO Transfer (billId, credId) VALUES ("TWX2016/123", 1);
SELECT ID || ":", credit, '+' || promised, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
SELECT ID || ":", available, '+' || 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, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
SELECT ID || ":", available, '+' || 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, arrears * -1 FROM Balance WHERE ID in ("Club", "alex");
SELECT ID || ":", available, '+' || 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';

View File

@@ -46,7 +46,7 @@ my %months = (
while ( my ($num, $month) = each %months ) {
my $yy = substr $month, -2;
$db->resultset("Debit")->create({
billId => "MB$yy$num-john",
billId => "MB$yy$num/john",
debtor => "john",
targetCredit => 1,
date => "16-05-01",
@@ -70,17 +70,60 @@ is $db->resultset("Debit")->search({ debtor => 'Club' })->single->billId, "TWX20
is_deeply {
map { $_->ID => {$_->get_columns} }
$db->resultset("Balance")->all
$db->resultset("Balance")->search(
{}, { columns => [qw|ID available arrears promised|] }
)
}, {
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 },
john => { ID=>'john', available=>7200, arrears=>7200, promised=>0 },
Club => { ID=>'Club', available=>0, arrears=>23450, promised=>7200 },
alex => { ID=>'alex', available=>0, arrears=>0, promised=>23450 },
},
"Get balances"
"Get balances before transfers"
;
# 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';
is $db->make_transfers( (q{*} => q{*}) x 2 ), 14400, 'Automatically balanced credits and debits';
is_deeply {
map { $_->ID => {$_->get_columns} }
$db->resultset("Balance")->search(
{}, { columns => [qw|ID available arrears promised|] }
)
}, {
john => { ID=>'john', available=>0, arrears=>0, promised=>0 },
Club => { ID=>'Club', available=>0, arrears=>16250, promised=>0 },
alex => { ID=>'alex', available=>7200, arrears=>0, promised=>16250 },
},
"Get balances after transfers"
;
$db->resultset("Account")->create({ ID => "rose", altId => 45, type => 'member' });
%months = (
'07' => 'July 2016', '08' => 'August 2016',
'09' => 'September 2016', '10' => 'October 2016', '11' => 'November 2016', '12' => 'December 2016',
);
while ( my ($num, $month) = each %months ) {
my $yy = substr $month, -2;
$db->resultset("Debit")->create({
billId => "MB$yy$num/rose",
debtor => "rose",
targetCredit => 1,
date => "16-07-10",
purpose => "Membership fee $month",
value => 600
});
}
my $rose = $db->resultset("Account")->find("rose");
$rose->add_to_credits({
value => 7200, purpose => "Membership fees until 6/17",
date => '16-08-12'
});
$db->make_transfers( q{*} => q{*} );
is $rose->available_credits->get_column("difference")->sum, 3600, "partial use of credit";
is $db->resultset("Balance")->find("alex")->earned, '10800', 'indirect transfers';
done_testing();