Nowhere near done yet, but checking in a snapshot of semi-working code. There is some simultaneous support for both with and without use of the Invoice/Receipt account. I want to do away with them completely, but will need to change how sitelink payments are mapped (right now, they split a payment into multiple parts to match the charge).

git-svn-id: file:///svn-source/pmgr/branches/single_AR_20090622@181 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-23 19:04:41 +00:00
parent 7aa1100cac
commit 80f8bd36d5
18 changed files with 502 additions and 409 deletions

View File

@@ -4,6 +4,8 @@ use DBI;
use Data::Dumper;
use File::Copy;
my $use_ir = 0;
# Internally adjust all numbers coming from the database to
# be in inches. Not necessary to go to this detail, but the
# actual units used is irrelevant, provided everything is to
@@ -145,7 +147,13 @@ sub executeSchema {
sub buildTables {
foreach my $table (values %{$newdb{'tables'}}) {
printf(STDERR "%-30s : %d rows\n", $table->{'name'}, int(@{$table->{'rows'}}));
my $count = 0;
foreach (@{$table->{'rows'}}) {
next unless defined $_;
++$count;
}
printf(STDERR "%-30s : %d rows\n", $table->{'name'}, $count);
foreach (@{$table->{'rows'}}) {
next unless defined $_;
@@ -437,9 +445,9 @@ foreach $row (@$result) {
'comment' => undef });
$newdb{'lookup'}{'account'}{$row->{'name'}}
= {'account' => $row->{'id'},
= {'account_id' => $row->{'id'},
'tillable' => $row->{'tillable'},
'ledger' => $newdb{'tables'}{'ledgers'}{'autoid'} };
'ledger_id' => $newdb{'tables'}{'ledgers'}{'autoid'} };
if ((!defined $newdb{'tables'}{'accounts'}{'autoid'}) ||
$row->{'id'} > $newdb{'tables'}{'accounts'}{'autoid'}) {
@@ -449,7 +457,7 @@ foreach $row (@$result) {
# For compatibility, while deciding on account names
$newdb{'lookup'}{'account'}{'Cash'}
= $newdb{'lookup'}{'account'}{'Payment'};
= $newdb{'lookup'}{'account'}{'Till'};
$newdb{'lookup'}{'charge_type'} = {};
$newdb{'lookup'}{'charge_type'}{'Rent'} =
@@ -474,15 +482,15 @@ foreach $row (@$result) {
$newdb{'lookup'}{'payment_type'} = {};
$newdb{'lookup'}{'payment_type'}{1}
= { 'name' => 'Cash', 'account' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Cash'} };
= { 'name' => 'Cash', 'account_name' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Cash'} };
$newdb{'lookup'}{'payment_type'}{2}
= { 'name' => 'Check', 'account' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Check'} };
= { 'name' => 'Check', 'account_name' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Check'} };
$newdb{'lookup'}{'payment_type'}{3}
= { 'name' => 'Money Order', 'account' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Money Order'} };
= { 'name' => 'Money Order', 'account_name' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Money Order'} };
$newdb{'lookup'}{'payment_type'}{4}
= { 'name' => 'ACH', 'account' => 'Bank', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'ACH'} };
= { 'name' => 'ACH', 'account_name' => 'Bank', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'ACH'} };
$newdb{'lookup'}{'payment_type'}{12}
= { 'name' => 'Concession', 'account' => 'Concession', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Other Non-Tillable'} };
= { 'name' => 'Concession', 'account_name' => 'Concession', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Other Non-Tillable'} };
$newdb{'ids'}{'monetary_source'} = {};
@@ -490,14 +498,14 @@ $newdb{'ids'}{'monetary_source'}{'internal'} = undef;
addRow('monetary_sources',
{ 'monetary_type_id' => $newdb{'lookup'}{'monetary_type'}{'Cash'}{'id'},
'name' => 'Cash Source',
'name' => 'Cash',
'comment' => 'Monetary source used for any cash transaction' });
$newdb{'ids'}{'monetary_source'}{'Cash'} =
$newdb{'tables'}{'monetary_sources'}{'autoid'};
addRow('monetary_sources',
{ 'monetary_type_id' => $newdb{'lookup'}{'monetary_type'}{'Other Non-Tillable'}{'id'},
'name' => 'Closing Monies Credited',
'name' => 'Closing',
'comment' => 'Credited at the closing table' });
$newdb{'ids'}{'monetary_source'}{'Closing'} =
$newdb{'tables'}{'monetary_sources'}{'autoid'};
@@ -660,35 +668,21 @@ foreach $row (@{query($sdbh, $query)}) {
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'} =
$newdb{'tables'}{'contacts'}{'autoid'};
# Every customer gets their own account
addRow('accounts',
{ 'type' => 'ASSET',
'trackable' => 1,
'name' => ('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'},
'open_stamp' => '2009-01-01',
'comment' => undef });
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'} =
$newdb{'tables'}{'accounts'}{'autoid'};
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger'} =
$newdb{'tables'}{'ledgers'}{'autoid'};
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account_id'} =
$newdb{'lookup'}{'account'}{'A/R'}{'account_id'};
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger_id'} =
$newdb{'lookup'}{'account'}{'A/R'}{'ledger_id'};
addRow('customers',
{ 'name' => "$row->{'LastName'}, $row->{'FirstName'}",
'primary_contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
'account_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'} });
'account_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account_id'} });
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'} =
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'customer_id'} =
$newdb{'tables'}{'customers'}{'autoid'};
addRow('contacts_customers',
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'customer_id'},
'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
'type' => 'TENANT' },
1);
@@ -754,7 +748,7 @@ foreach $row (@{query($sdbh, $query)}) {
$newdb{'tables'}{'contacts'}{'autoid'};
addRow('contacts_customers',
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'customer_id'},
'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'},
'type' => 'ALTERNATE' },
1);
@@ -800,52 +794,26 @@ $query = "SELECT L.*, A.TenantID FROM TenantLedger L LEFT JOIN `Access` A ON A.L
foreach $row (@{query($sdbh, $query)}) {
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}
= { 'cust' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'} };
= { 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'customer_id'} };
if (1) {
# Every lease gets its own account
addRow('accounts',
{ 'type' => 'ASSET',
'trackable' => 1,
'name' => ('Lease #' . $row->{'LedgerID'}
. ' ('.$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ')'),
'comment' => undef });
#'comment' => 'For lease related transaction, NOT personal charges!' });
addRow('ledgers',
{ 'account_id' => $newdb{'tables'}{'accounts'}{'autoid'},
'open_stamp' => datefmt($row->{'DateIn'}),
'comment' => undef });
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'account'}
= $newdb{'tables'}{'accounts'}{'autoid'};
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'}
= $newdb{'tables'}{'ledgers'}{'autoid'};
}
else {
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'account'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'};
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger'};
}
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust_account'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'};
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust_ledger'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger'};
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'account_id'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account_id'};
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger_id'}
= $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger_id'};
addRow('leases',
{ 'number' => $row->{'LedgerID'},
'lease_type_id' => $newdb{'tables'}{'lease_types'}{'autoid'},
'unit_id' => $newdb{'lookup'}{'unit'}{$row->{'UnitID'}}{'id'},
'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
'account_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'account'},
'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'customer_id'},
'account_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'account_id'},
'lease_date' => datefmt($row->{'DateIn'}),
'movein_date' => datefmt($row->{'DateIn'}),
'moveout_date' => datefmt($row->{'DateOut'}),
'close_date' => datefmt($row->{'DateClosed'}),
'amount' => $row->{'Rent'} });
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'lease'} =
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'lease_id'} =
$newdb{'tables'}{'leases'}{'autoid'};
}
@@ -868,6 +836,8 @@ $newdb{'lookup'}{'charge'} = {};
$query = "SELECT * FROM Charges ORDER BY ChargeID";
foreach $row (@{query($sdbh, $query)}) {
my $credit_ledger_id;
my $ledger_entry_id;
addRow('transactions',
{ 'stamp' => datefmt($row->{'ChargeDate'}),
@@ -875,57 +845,93 @@ foreach $row (@{query($sdbh, $query)}) {
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}
= { 'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
'ledger' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'},
'ledger_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger_id'},
#'ledger_id' => $newdb{'lookup'}{'account'}{'Invoice'}{'ledger_id'},
'amount' => $row->{'ChargeAmount'},
'tax_amount' => $row->{'TaxAmount'},
'customer_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'lease_id'},
};
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'charge_type'}{$row->{'ChargeDescription'}}{'ledger'},
'amount' => $row->{'ChargeAmount'},
'comment' => "Charge: $row->{'ChargeID'}; Ledger: $row->{'LedgerID'}" });
my $debit_entry_id = $newdb{'tables'}{'ledger_entries'}{'autoid'},
$credit_ledger_id = $newdb{'lookup'}{'charge_type'}{$row->{'ChargeDescription'}}{'ledger_id'};
if ($use_ir) {
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{'Invoice'}{'ledger_id'},
'credit_ledger_id' => $credit_ledger_id,
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'ChargeAmount'},
'comment' => "Charge: $row->{'ChargeID'}; Ledger: $row->{'LedgerID'}" });
$ledger_entry_id = $newdb{'tables'}{'ledger_entries'}{'autoid'};
$credit_ledger_id = $newdb{'lookup'}{'account'}{'Invoice'}{'ledger_id'};
}
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust_ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger'},
'debit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_id'},
'credit_ledger_id' => $credit_ledger_id,
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'ChargeAmount'},
'comment' => "Charge: $row->{'ChargeID'}; Ledger: $row->{'LedgerID'}" });
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'terminal'}
= $debit_entry_id;
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'entry'}
= $newdb{'tables'}{'ledger_entries'}{'autoid'},
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger'}
= $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust_ledger'};
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $debit_entry_id,
'credit_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'entry'},
'terminal_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'terminal'},
'amount' => $row->{'ChargeAmount'},
});
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_entry_id'}
= $newdb{'tables'}{'ledger_entries'}{'autoid'};
if ($use_ir) {
# Reconcile the invoice account. Since this is from the perspective
# of the invoice, the entry crediting the charge is the debit, and
# the entry debiting A/R is the credit.
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $ledger_entry_id,
'credit_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_entry_id'},
'amount' => $row->{'ChargeAmount'},
});
}
next unless $row->{'TaxAmount'};
$credit_ledger_id = $newdb{'lookup'}{'charge_type'}{'Tax'}{'ledger_id'};
if ($use_ir) {
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{'Invoice'}{'ledger_id'},
'credit_ledger_id' => $credit_ledger_id,
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'TaxAmount'},
'comment' => undef });
$ledger_entry_id = $newdb{'tables'}{'ledger_entries'}{'autoid'};
$credit_ledger_id = $newdb{'lookup'}{'account'}{'Invoice'}{'ledger_id'};
}
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'charge_type'}{'Tax'}{'ledger'},
'debit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_id'},
'credit_ledger_id' => $credit_ledger_id,
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'TaxAmount'},
'comment' => undef });
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tax_entry'}
= $newdb{'tables'}{'ledger_entries'}{'autoid'},
= $newdb{'tables'}{'ledger_entries'}{'autoid'};
if ($use_ir) {
# Reconcile the invoice account. Since this is from the perspective
# of the invoice, the entry crediting the charge is the debit, and
# the entry debiting A/R is the credit.
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $ledger_entry_id,
'credit_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tax_entry'},
'amount' => $row->{'TaxAmount'},
});
}
}
@@ -944,10 +950,10 @@ $query =
foreach $row (@{query($sdbh, $query)}) {
if ($newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}) {
die unless ($newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'cust'}
== $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'cust'});
die unless ($newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'customer_id'}
== $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'customer_id'});
push(@{$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'ledgers'}},
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'});
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger_id'});
#print Dumper $receipt_map{$row->{'ReceiptNum'}};
next;
}
@@ -958,8 +964,12 @@ foreach $row (@{query($sdbh, $query)}) {
$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}
= {'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
'cust' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'},
'ledgers' => [ $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'} ] };
# NOTE: Use of 'lease_id' would be invalid, since the
# receipt could be for multiple leases. 'customer_id'
# could be OK, but better to avoid this issue all
# together by excluding them both from this lookup.
'ledgers' => [ $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger_id'} ] };
}
@@ -976,47 +986,86 @@ $query = "SELECT * FROM Payments ORDER BY PaymentID";
foreach $row (@{query($sdbh, $query)})
{
my $monetary_source_id;
my $payment_acct;
my $debit_ledger_id;
my $ledger_entry_id;
if ($row->{'PaymentDate'} =~ m%3/25/2009%) {
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{'closing'};
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{'Closing'};
$payment_acct = 'Bank';
}
else {
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{
$newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'name'}
$newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'name'}
};
$payment_acct = $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'account_name'};
}
if (!defined $monetary_source_id) {
addRow('monetary_sources',
{ 'monetary_type_id' => $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'monetary_type'}{'id'},
'name' => $row->{'RecdFrom'},
'comment' => "Payment: $row->{'PaymentID'}; Type: $row->{'PaymentType'}; Check: $row->{'CheckNum'}; $row->{'Memo'}" });
$monetary_source_id = $newdb{'tables'}{'monetary_sources'}{'autoid'};
}
if (!$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{$row->{'PaymentType'}}{'monetary_source_id'}) {
if (!defined $monetary_source_id) {
my $name;
$name = $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'name'};
if ($name eq 'Check') {
$name = 'Check #' . $row->{'CheckNum'};
}
addRow('monetary_sources',
{ 'monetary_type_id' => $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'monetary_type'}{'id'},
'name' => $name,
'comment' => "Payment: $row->{'PaymentID'}; Type: $row->{'PaymentType'}; Check: $row->{'CheckNum'}; $row->{'Memo'}" });
$monetary_source_id = $newdb{'tables'}{'monetary_sources'}{'autoid'};
}
my $payment_acct = $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'account'};
$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{$row->{'PaymentType'}}{'monetary_source_id'}
= $monetary_source_id;
}
addRow('ledger_entries',
{ 'monetary_source_id' => $monetary_source_id,
'transaction_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{$payment_acct}{'ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger'},
'amount' => $row->{'PaymentAmount'},
'comment' => "Receipt: $row->{'ReceiptNum'}; Payment: $row->{'PaymentID'}; Charge: $row->{'ChargeID'}" });
$debit_ledger_id = $newdb{'lookup'}{'account'}{$payment_acct}{'ledger_id'};
if ($use_ir) {
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{$row->{'PaymentType'}}{'monetary_source_id'},
'transaction_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'},
'debit_ledger_id' => $debit_ledger_id,
'credit_ledger_id' => $newdb{'lookup'}{'account'}{'Receipt'}{'ledger_id'},
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'PaymentAmount'},
'comment' => "Receipt: $row->{'ReceiptNum'}; Payment: $row->{'PaymentID'}; Charge: $row->{'ChargeID'}" });
$ledger_entry_id = $newdb{'tables'}{'ledger_entries'}{'autoid'};
$debit_ledger_id = $newdb{'lookup'}{'account'}{'Receipt'}{'ledger_id'};
}
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{$row->{'PaymentType'}}{'monetary_source_id'},
'transaction_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'},
'debit_ledger_id' => $debit_ledger_id,
'credit_ledger_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_id'},
'customer_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'customer_id'},
'lease_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'lease_id'},
'amount' => $row->{'PaymentAmount'},
'comment' => "Receipt: $row->{'ReceiptNum'}; Payment: $row->{'PaymentID'}; Charge: $row->{'ChargeID'}" });
$newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}
= { 'entry' => $newdb{'tables'}{'ledger_entries'}{'autoid'} };
= { 'ledger_entry_id' => $newdb{'tables'}{'ledger_entries'}{'autoid'} };
if ($use_ir) {
# Reconcile the receipt account. Since this is from the perspective
# of the receipt, the entry crediting the A/R is the debit, and
# the entry debiting Cash/Bank/etc is the credit.
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $ledger_entry_id,
'credit_ledger_entry_id' => $newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}{'ledger_entry_id'},
'amount' => $row->{'PaymentAmount'},
});
}
# Reconcile Payment to the Charge. Since tracking is due to
# the A/R account (Lease Account in this case), credit/debit
# is from the perspective of that account. Namely, the charge
# was the debit to the A/R, and the payment was the credit.
my $reconcile_amount = $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'amount'};
$reconcile_amount = $row->{'PaymentAmount'} if $row->{'PaymentAmount'} <= $reconcile_amount;
# Reconcile the A/R account. Since this is from the perspective
# of the A/R, charge is the debit, and payment is the credit
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'entry'},
'credit_ledger_entry_id' => $newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}{'entry'},
'terminal_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'terminal'},
{ 'debit_ledger_entry_id' => $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'ledger_entry_id'},
'credit_ledger_entry_id' => $newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}{'ledger_entry_id'},
'amount' => $reconcile_amount,
});
@@ -1033,11 +1082,12 @@ foreach $row (@{query($sdbh, $query)})
## Fake Data for Testing
my %fake =
('custid' => 2,
('customer_id' => 4,
'ledger_id' => 4,
'invoice' => [ { 'id' => 1000,
'date' => '2009-05-05',
'entry' => [ { 'id' => 2100,
'account' => 'Security Deposit',
'account_name' => 'Security Deposit',
'amount' => 10,
'tax' => 0 },
],
@@ -1046,7 +1096,7 @@ my %fake =
'date' => '2009-05-01',
'thru' => '2009-05-31',
'entry' => [ { 'id' => 2110,
'account' => 'Rent',
'account_name' => 'Rent',
'amount' => 100,
'tax' => 5 },
],
@@ -1054,7 +1104,7 @@ my %fake =
{ 'id' => 1002,
'date' => '2009-05-11',
'entry' => [ { 'id' => 2120,
'account' => 'Late Charge',
'account_name' => 'Late Charge',
'amount' => 25,
'tax' => 0 },
],
@@ -1115,105 +1165,137 @@ my %fake =
);
sub fakeTesting {
$newdb{'ids'}{'ledger'}{'Cash-Old'} =
$newdb{'lookup'}{'account'}{'Cash'}{'ledger_id'};
$newdb{'ids'}{'ledger'}{'Cash-Old'} =
$newdb{'lookup'}{'account'}{'Cash'}{'ledger'};
addRow('ledgers',
{ 'account_id' => $newdb{'lookup'}{'account'}{'Cash'}{'account_id'},
'open_stamp' => 'NOW()',
'sequence' => 2,
'comment' => 'Opened new ledger for testing' });
addRow('ledgers',
{ 'account_id' => $newdb{'lookup'}{'account'}{'Cash'}{'account'},
'open_stamp' => 'NOW()',
'sequence' => 2,
'comment' => 'Opened new ledger for testing' });
$newdb{'lookup'}{'account'}{'Cash'}{'ledger_id'} =
$newdb{'tables'}{'ledgers'}{'autoid'};
$newdb{'lookup'}{'account'}{'Cash'}{'ledger'} =
$newdb{'tables'}{'ledgers'}{'autoid'};
my $balance = 0;
foreach $row (@{$newdb{'tables'}{'ledger_entries'}{'rows'}}) {
next unless defined $row;
$balance += $row->{'amount'}
if $row->{'debit_ledger_id'} == $newdb{'ids'}{'ledger'}{'Cash-Old'};
$balance -= $row->{'amount'}
if $row->{'credit_ledger_id'} == $newdb{'ids'}{'ledger'}{'Cash-Old'};
}
my $balance = 0;
foreach $row (@{$newdb{'tables'}{'ledger_entries'}{'rows'}}) {
next unless defined $row;
$balance += $row->{'amount'}
if $row->{'debit_ledger_id'} == $newdb{'ids'}{'ledger'}{'Cash-Old'};
$balance -= $row->{'amount'}
if $row->{'credit_ledger_id'} == $newdb{'ids'}{'ledger'}{'Cash-Old'};
}
addRow('transactions',
{ 'stamp' => '2009-05-10' });
addRow('transactions',
{ 'stamp' => '2009-05-10' });
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'tables'}{'transactions'}{'autoid'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{'Cash'}{'ledger'},
'credit_ledger_id' => $newdb{'ids'}{'ledger'}{'Cash-Old'},
'amount' => $balance,
'name' => "Close Out ($newdb{'ids'}{'ledger'}{'Cash-Old'} -> $newdb{'tables'}{'transactions'}{'autoid'})",
'comment' => "Carrying forward old ledger balance onto new ledger" });
addRow('ledger_entries',
{ 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'},
'transaction_id' => $newdb{'tables'}{'transactions'}{'autoid'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{'Cash'}{'ledger_id'},
'credit_ledger_id' => $newdb{'ids'}{'ledger'}{'Cash-Old'},
'amount' => $balance,
'name' => "Close Out ($newdb{'ids'}{'ledger'}{'Cash-Old'} -> $newdb{'tables'}{'transactions'}{'autoid'})",
'comment' => "Carrying forward old ledger balance onto new ledger" });
# NOTE: no tracking for the Cash account
$newdb{'tables'}{'ledgers'}{'rows'}[$newdb{'ids'}{'ledger'}{'Cash-Old'}]{'closed'} = 1;
$newdb{'tables'}{'ledgers'}{'rows'}[$newdb{'ids'}{'ledger'}{'Cash-Old'}]{'close_stamp'} = 'NOW()';
$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}}) {
#print Dumper ${%$tx{'date'}};
#exit;
addRow('transactions',
{ 'id' => $tx->{'id'},
'stamp' => $tx->{'date'},
'through_date' => $tx->{'thru'},
'comment' => "Fake $ir" },
1);
foreach my $ir ('invoice', 'receipt') {
foreach my $tx (@{$fake{$ir}}) {
#print Dumper ${%$tx{'date'}};
#exit;
addRow('transactions',
{ 'id' => $tx->{'id'},
'stamp' => $tx->{'date'},
'through_date' => $tx->{'thru'},
'comment' => "Fake $ir" },
1);
foreach my $e (@{$tx->{'entry'}}) {
my $dr = ($ir eq 'invoice') ? 'A/R' : 'Cash';
my $cr = ($ir eq 'invoice') ? $e->{'account'} : 'A/R';
my $monetary_source_id;
foreach my $e (@{$tx->{'entry'}}) {
my $crdr = ($ir eq 'invoice') ? 'Invoice' : 'Receipt';
my $dr = ($ir eq 'invoice') ? 'A/R' : 'Cash';
$crdr = $dr;
my $cr = ($ir eq 'invoice') ? $e->{'account_name'} : 'A/R';
my $monetary_source_id;
if (defined $e->{'type'}) {
addRow('monetary_sources',
{ 'monetary_type_id' => $e->{'type'},
'name' => "Money of type " . $e->{'type'},
'comment' => "Fake Money For " . $e->{'id'} });
$monetary_source_id = $newdb{'tables'}{'monetary_sources'}{'autoid'};
}
else {
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{'internal'};
}
if (defined $e->{'type'}) {
addRow('monetary_sources',
{ 'monetary_type_id' => $e->{'type'},
'name' => "Money of type " . $e->{'type'},
'comment' => "Fake Money For " . $e->{'id'} });
$monetary_source_id = $newdb{'tables'}{'monetary_sources'}{'autoid'};
}
else {
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{'internal'};
}
addRow('ledger_entries',
{ 'id' => $e->{'id'},
'monetary_source_id' => $monetary_source_id,
'transaction_id' => $tx->{'id'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{$dr}{'ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'account'}{$cr}{'ledger'},
'amount' => $e->{'amount'},
'comment' => "Fake $ir entry" },
1);
addRow('ledger_entries',
{ 'id' => $e->{'id'},
'monetary_source_id' => $monetary_source_id,
'transaction_id' => $tx->{'id'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{$crdr}{'ledger_id'},
'credit_ledger_id' => $newdb{'lookup'}{'account'}{$cr}{'ledger_id'},
'customer_id' => $fake{'customer_id'},
'lease_id' => $fake{'lease_id'},
'amount' => $e->{'amount'},
'comment' => "Fake $ir entry" },
1);
foreach my $t (@{$e->{'track'}}) {
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $t->{'debit'},
'credit_ledger_entry_id' => $e->{'id'},
'terminal_ledger_entry_id' => $t->{'debit'},
'amount' => $t->{'amount'}
});
}
# addRow('ledger_entries',
# { 'id' => $e->{'id'},
# 'monetary_source_id' => $monetary_source_id,
# 'transaction_id' => $tx->{'id'},
# 'debit_ledger_id' => $newdb{'lookup'}{'account'}{$dr}{'ledger_id'},
# 'credit_ledger_id' => $newdb{'lookup'}{'account'}{$crdr}{'ledger_id'},
# 'customer_id' => $fake{'customer_id'},
# 'lease_id' => $fake{'lease_id'},
# 'amount' => $e->{'amount'},
# 'comment' => "Fake $ir entry" },
# 1);
next unless $e->{'tax'};
foreach my $t (@{$e->{'track'}}) {
addRow('reconciliations',
{ 'debit_ledger_entry_id' => $t->{'debit'},
'credit_ledger_entry_id' => $e->{'id'},
'amount' => $t->{'amount'}
});
}
addRow('ledger_entries',
{ 'id' => $e->{'id'}+1,
'monetary_source_id' => $monetary_source_id,
'transaction_id' => $tx->{'id'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{$dr}{'ledger'},
'credit_ledger_id' => $newdb{'lookup'}{'account'}{'Tax'}{'ledger'},
'amount' => $e->{'tax'},
'comment' => "Fake Tax" },
1);
}
}
next unless $e->{'tax'};
addRow('ledger_entries',
{ 'id' => $e->{'id'}+1,
'monetary_source_id' => $monetary_source_id,
'transaction_id' => $tx->{'id'},
'debit_ledger_id' => $newdb{'lookup'}{'account'}{$crdr}{'ledger_id'},
'credit_ledger_id' => $newdb{'lookup'}{'account'}{'Tax'}{'ledger_id'},
'customer_id' => $fake{'customer_id'},
'lease_id' => $fake{'lease_id'},
'amount' => $e->{'amount'},
'comment' => "Fake Tax" },
1);
# addRow('ledger_entries',
# { 'id' => $e->{'id'}+1,
# 'monetary_source_id' => $monetary_source_id,
# 'transaction_id' => $tx->{'id'},
# 'debit_ledger_id' => $newdb{'lookup'}{'account'}{$dr}{'ledger_id'},
# 'credit_ledger_id' => $newdb{'lookup'}{'account'}{$crdr}{'ledger_id'},
# 'customer_id' => $fake{'customer_id'},
# 'lease_id' => $fake{'lease_id'},
# 'amount' => $e->{'tax'},
# 'comment' => "Fake Tax" },
# 1);
}
}
}
}
#fakeTesting();
$Data::Dumper::Sortkeys = 1;
print Dumper \%newdb;
# exit;