More changes to how accounts/ledgers/transactions are all handled.

git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@78 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-10 02:27:03 +00:00
parent 374843aeee
commit 190a9259d4
2 changed files with 64 additions and 45 deletions

View File

@@ -632,6 +632,8 @@ CREATE TABLE `pmgr_customers` (
`name` VARCHAR(80) NOT NULL,
`comment` VARCHAR(255) DEFAULT NULL,
`account_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -689,6 +691,7 @@ CREATE TABLE `pmgr_leases` (
`lease_type_id` INT(10) UNSIGNED NOT NULL,
`unit_id` INT(10) UNSIGNED NOT NULL,
`customer_id` INT(10) UNSIGNED NOT NULL,
`account_id` INT(10) UNSIGNED NOT NULL,
`late_schedule_id` INT(10) UNSIGNED DEFAULT NULL,
`lease_date` DATE NOT NULL,
@@ -832,17 +835,17 @@ CREATE TABLE `pmgr_accounts` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
LOCK TABLES `pmgr_accounts` WRITE;
INSERT INTO `pmgr_accounts` (`id`, `type`, `name`)
INSERT INTO `pmgr_accounts` (`type`, `name`)
VALUES
(1, 'ASSET', 'A/R'),
(2, 'LIABILITY', 'A/P'),
(3, 'LIABILITY', 'Tax'),
(4, 'LIABILITY', 'Customer Credit'),
(5, 'ASSET', 'Bank'),
(6, 'ASSET', 'Cash'),
(7, 'LIABILITY', 'Security Deposit'),
(8, 'INCOME', 'Rent'),
(9, 'INCOME', 'Late Charge');
('ASSET', 'A/R'),
('LIABILITY', 'A/P'),
('LIABILITY', 'Tax'),
('LIABILITY', 'Customer Credit'),
('ASSET', 'Bank'),
('ASSET', 'Cash'),
('LIABILITY', 'Security Deposit'),
('INCOME', 'Rent'),
('INCOME', 'Late Charge');
UNLOCK TABLES;
@@ -882,9 +885,18 @@ CREATE TABLE `pmgr_ledgers` (
-- this particular ledger is valid and so on.
`name` VARCHAR(80) DEFAULT NULL,
`sequence` INT(10) UNSIGNED DEFAULT 1,
`account_id` INT(10) UNSIGNED NOT NULL,
`closed` INT UNSIGNED DEFAULT 0,
-- REVISIT <AP>: 20090607
-- Probably, a single close should have the ability to close
-- many different account ledgers. For now, just timestamping.
-- `close_id` INT(10) UNSIGNED NOT NULL,
`open_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`close_stamp` DATETIME DEFAULT NULL,
`comment` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`)
@@ -903,13 +915,6 @@ CREATE TABLE `pmgr_transactions` (
`through_date` DATE DEFAULT NULL,
`due_date` DATE DEFAULT NULL,
-- REVISIT <AP>: 20090604
-- It really seems this is too restrictive. One tenant
-- should be able to, within a single transaction, pay
-- for their own charges and another tenant's as well.
-- customer_id may need to move to ledger_entries
`customer_id` INT(10) UNSIGNED NOT NULL,
-- REVISIT <AP>: 20090604
-- How should we track which charges have been paid?
-- `related_transaction_id` INT(10) UNSIGNED NOT NULL,

View File

@@ -145,17 +145,25 @@ sub executeSchema {
sub buildTables {
foreach my $table (values %{$newdb{'tables'}}) {
printf(STDERR "%-30s : %d rows\n", $table->{'name'}, int(@{$table->{'rows'}}));
foreach (@{$table->{'rows'}}) {
next unless defined $_;
next unless defined $_;
my %row = %$_;
delete $row{'--LINE--'};
my %row = %$_;
delete $row{'--LINE--'};
my $query;
$query = "INSERT INTO " . $table->{'name'};
$query .= " (" . join(", ", map({"`$_`"} keys(%row))) . ")";
$query .= " VALUES (" . join(", ", map({s/'/''/g if defined $_; defined $_ ? "'$_'" : "NULL"} values(%row))) . ")";
query($db_handle, $query, $_);
my $query;
$query = "INSERT INTO " . $table->{'name'};
$query .= " (" . join(", ", map({"`$_`"} keys(%row))) . ")";
$query .= " VALUES (" . join(", ", map({s/'/''/g if defined $_;
defined $_
? (/^\w+\(.*\)$/
? $_
: "'$_'" )
: "NULL" }
values(%row))) . ")";
query($db_handle, $query, $_);
}
}
}
@@ -425,6 +433,7 @@ $result = query($db_handle, $query);
foreach $row (@$result) {
addRow('ledgers',
{ 'account_id' => $row->{'id'},
'open_stamp' => '2009-01-01',
'name' => $row->{'name'} . ' Ledger',
'comment' => undef });
@@ -648,15 +657,19 @@ foreach $row (@{query($sdbh, $query)}) {
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'} =
$newdb{'tables'}{'contacts'}{'autoid'};
# Every customer gets their own ledger
# Every customer gets their own account
addRow('accounts',
{ 'type' => 'ASSET',
'name' => 'Customer: ' . $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'},
'comment' => undef });
'name' => ('Account: Customer #' . ($newdb{'tables'}{'customers'}{'autoid'}+1) .
' ('.$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ')'),
'comment' => undef });
#'comment' => 'For direct customer transaction, NOT lease charges!' });
addRow('ledgers',
{ 'account_id' => $newdb{'tables'}{'accounts'}{'autoid'},
'name' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ' Ledger',
'comment' => 'Ledger for Customer Account #' . $newdb{'tables'}{'accounts'}{'autoid'} });
'open_stamp' => '2009-01-01',
'name' => 'Ledger: Customer #' . ($newdb{'tables'}{'customers'}{'autoid'}+1)
. ' ('.$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ')',
'comment' => undef });
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'} =
$newdb{'tables'}{'accounts'}{'autoid'};
@@ -782,16 +795,19 @@ $newdb{'lookup'}{'ledger'} = {};
$query = "SELECT L.*, A.TenantID FROM TenantLedger L LEFT JOIN `Access` A ON A.LedgerID = L.LedgerID WHERE L.UnitID <> 'POS\$' ORDER BY L.LedgerID";
foreach $row (@{query($sdbh, $query)}) {
# Every lease gets its own ledger
# Every lease gets its own account
addRow('accounts',
{ 'type' => 'ASSET',
'name' => 'Lease: #' . ($newdb{'tables'}{'leases'}{'autoid'}+1),
'comment' => undef });
'name' => ('Account: Lease #' . ($newdb{'tables'}{'leases'}{'autoid'}+1)
. ' ('.$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ')'),
'comment' => undef });
#'comment' => 'For lease related transaction, NOT personal charges!' });
addRow('ledgers',
{ 'account_id' => $newdb{'tables'}{'accounts'}{'autoid'},
'name' => 'Lease #' . ($newdb{'tables'}{'leases'}{'autoid'}+1)
'open_stamp' => datefmt($row->{'DateIn'}),
'name' => 'Ledger: Lease #' . ($newdb{'tables'}{'leases'}{'autoid'}+1)
. ' ('.$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ')',
'comment' => 'Ledger for Customer Account #' . $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'} });
'comment' => undef });
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}
= { 'cust' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
@@ -836,8 +852,7 @@ foreach $row (@{query($sdbh, $query)}) {
addRow('transactions',
{ 'stamp' => datefmt($row->{'ChargeDate'}),
'through_date' => datefmt($row->{'EndDate'}),
'customer_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'} });
'through_date' => datefmt($row->{'EndDate'}) });
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}
= { 'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
@@ -888,8 +903,7 @@ foreach $row (@{query($sdbh, $query)}) {
addRow('transactions',
{ 'stamp' => datefmt($row->{'ReceiptDate'}),
'through_date' => undef,
'customer_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'} });
'through_date' => undef });
$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}
= {'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
@@ -1030,8 +1044,10 @@ $newdb{'ids'}{'ledger'}{'Cash-Old'} =
addRow('ledgers',
{ 'account_id' => $newdb{'lookup'}{'account'}{'Cash'}{'account'},
'name' => 'Fake Cash Ledger',
'comment' => 'Fake ledger for testing' });
'open_stamp' => 'NOW()',
'sequence' => 2,
'name' => 'Fake Cash Ledger',
'comment' => 'Fake ledger for testing' });
$newdb{'lookup'}{'account'}{'Cash'}{'ledger'} =
$newdb{'tables'}{'ledgers'}{'autoid'};
@@ -1046,8 +1062,7 @@ foreach $row (@{$newdb{'tables'}{'ledger_entries'}{'rows'}}) {
}
addRow('transactions',
{ 'customer_id' => 0,
'stamp' => '2009-05-10' });
{ 'stamp' => '2009-05-10' });
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
@@ -1059,7 +1074,7 @@ addRow('ledger_entries',
'comment' => "Carrying forward old ledger balance onto new ledger" });
$newdb{'tables'}{'ledgers'}{'rows'}[$newdb{'ids'}{'ledger'}{'Cash-Old'}]{'closed'} = 1;
$newdb{'tables'}{'ledgers'}{'rows'}[$newdb{'ids'}{'ledger'}{'Cash-Old'}]{'close_stamp'} = 'NOW()';
foreach my $ir ('invoice', 'receipt') {
foreach my $tx (@{$fake{$ir}}) {
@@ -1069,7 +1084,6 @@ foreach my $ir ('invoice', 'receipt') {
{ 'id' => $tx->{'id'},
'stamp' => $tx->{'date'},
'through_date' => $tx->{'thru'},
'customer_id' => $fake{'custid'},
'comment' => "Fake $ir" },
1);