git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@179 97e9348a-65ac-dc4b-aefc-98561f571b83
1253 lines
42 KiB
Perl
1253 lines
42 KiB
Perl
#perl -w
|
|
use strict;
|
|
use DBI;
|
|
use Data::Dumper;
|
|
use File::Copy;
|
|
|
|
# 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
|
|
# scale, and this factor ensures that any fractional units
|
|
# become whole (e.g. 7.5 "feet" becomes 90 "units")
|
|
my $internal_adjustment_factor = 12;
|
|
|
|
my $schema_file = shift || die("Must specify schema file\n");
|
|
my $slink_file = shift || die("Must specify sitelink file\n");
|
|
|
|
my $slink_file = ";Data Source=$slink_file";
|
|
my $slink_pass = ";Jet OLEDB:Database Password=2web";
|
|
my $sdsn="Provider=Microsoft.Jet.OLEDB.4.0$slink_pass$slink_file";
|
|
my $sdbh = DBI->connect("dbi:ADO:$sdsn", undef, undef, {PrintError => 1,
|
|
RaiseError => 1});
|
|
|
|
# Connect to the database.
|
|
my($hostname, $database, $user, $password) = ('localhost',
|
|
'property_manager',
|
|
'pmgr', 'pmgruser');
|
|
my $db_handle = DBI->connect("DBI:mysql:database=$database;host=$hostname",
|
|
$user, $password,
|
|
{'PrintError' => 1,
|
|
'RaiseError' => 0});
|
|
|
|
|
|
$SIG{__DIE__} = \&Die;
|
|
|
|
my ($query, $result, $nrows, $row);
|
|
my (%newdb) = ( 'schema' => [],
|
|
'tables' => {},
|
|
'ids' => {},
|
|
'lookup' => {'state' => { 1=>'AK', 14=>'ID', 48=>'WA' }} );
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## Die
|
|
|
|
sub Die {
|
|
my @text = @_;
|
|
warn "----------------------------------------------------------\n";
|
|
warn "FATAL ERROR: @text\n";
|
|
my $count = 0;
|
|
{
|
|
my ($package, $filename, $line, $sub) = caller($count);
|
|
last unless defined $line;
|
|
warn sprintf("%02i %5i %-35s %-20s\n", $count++, $line, $sub,
|
|
$filename);
|
|
redo;
|
|
}
|
|
exit 1;
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## addRow
|
|
|
|
sub addRow {
|
|
my ($table, $cols, $noid) = @_;
|
|
die unless $table;
|
|
die unless ref($cols) eq 'HASH';
|
|
|
|
die "Table `$table` is unknown!"
|
|
unless defined $newdb{'tables'}{$table};
|
|
|
|
my $id;
|
|
if ($noid) {
|
|
$id = $cols->{'id'};
|
|
}
|
|
|
|
if (!defined $id) {
|
|
$id = ++$newdb{'tables'}{$table}{'autoid'};
|
|
}
|
|
|
|
$cols->{'id'} = $id
|
|
unless ($noid);
|
|
|
|
# For debug purposes
|
|
my $line = (caller())[2];
|
|
$cols->{'--LINE--'} = "addRow called from line: $line";
|
|
|
|
$newdb{'tables'}{$table}{'rows'}[$id] = $cols;
|
|
return $id;
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## query
|
|
|
|
sub query {
|
|
my ($dbh, $sql, $data, $ignore) = @_;
|
|
#print("$sql\n\n"); #return [ { 'id' => 7 } ];
|
|
#print("$sql\n\n") if $sql =~ /^\s*UPDATE/i;
|
|
#return 1 unless $sql =~ /^\s*SELECT/i;
|
|
|
|
my ($sth, $result);
|
|
if ($sql =~ /^\s*SELECT/i) {
|
|
$sth = $dbh->prepare($sql);
|
|
$sth->execute();
|
|
$result = $sth->fetchall_arrayref({});
|
|
} else {
|
|
$result = $dbh->do($sql);
|
|
}
|
|
|
|
if (!$result && !$ignore) {
|
|
print STDERR "SQL Query FAILED:\n";
|
|
print STDERR "$sql\n\n";
|
|
print STDERR Dumper $data
|
|
if defined $data;
|
|
die;
|
|
}
|
|
return ($sth, $result);
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## executeSchema
|
|
|
|
sub executeSchema {
|
|
foreach (@{$newdb{'schema'}}) {
|
|
query($db_handle, $_);
|
|
}
|
|
|
|
foreach my $table (values %{$newdb{'tables'}}) {
|
|
foreach (@{$table->{'schema'}}) {
|
|
query($db_handle, $_);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## buildTables
|
|
|
|
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 $_;
|
|
|
|
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 $_
|
|
? (/^\w+\(.*\)$/
|
|
? $_
|
|
: "'$_'" )
|
|
: "NULL" }
|
|
values(%row))) . ")";
|
|
query($db_handle, $query, $_);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
## helper functions
|
|
|
|
sub sizeCode {
|
|
my ($width, $depth) = @_;
|
|
return "YARD"
|
|
if ($width == 12 && $depth == 40);
|
|
return "APARTMENT"
|
|
if ($width == 20 && $depth == 30);
|
|
return sprintf("%02dx%02d", $width, $depth);
|
|
}
|
|
|
|
sub datefmt {
|
|
my ($dt) = @_;
|
|
return undef unless $dt;
|
|
my @dt = split(/\/|\s/, $dt);
|
|
#print("$dt : " . sprintf("%04d-%02d-%02d", $dt[2], $dt[0], $dt[1]) . "\n");
|
|
return sprintf("%04d-%02d-%02d%s", $dt[2], $dt[0], $dt[1], $dt[3] ? ' '.$dt[3] : "");
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
## BUILD THE DATABASE
|
|
|
|
open(SCHEMA, "<$schema_file") || die ("Can't open schema ($!)\n");
|
|
my $schema_query = "";
|
|
my $table;
|
|
while (<SCHEMA>) {
|
|
next if /^\s*--/;
|
|
if (/DROP\s+TABLE\s.*`?(pmgr_(\w+))/i) {
|
|
$table = $2;
|
|
$newdb{'tables'}{$table} = { 'name' => $1,
|
|
'schema' => [],
|
|
'autoid' => 0,
|
|
'rows' => [] };
|
|
}
|
|
|
|
$schema_query .= $_;
|
|
if (/;\s*$/) {
|
|
$schema_query =~ s/^\s+//;
|
|
$schema_query =~ s/\s*;\s*$//;
|
|
if (!$table) {
|
|
push(@{$newdb{'schema'}}, $schema_query);
|
|
} else {
|
|
push(@{$newdb{'tables'}{$table}{'schema'}}, $schema_query);
|
|
}
|
|
$schema_query = "";
|
|
}
|
|
}
|
|
close(SCHEMA);
|
|
|
|
executeSchema();
|
|
|
|
|
|
#################################################################
|
|
## Test Contact
|
|
|
|
addRow('contacts',
|
|
{ 'first_name' => 'Abijah',
|
|
'middle_name' => 'M',
|
|
'last_name' => 'Perkins' });
|
|
|
|
addRow('contact_addresses',
|
|
{ 'address' => '1324 N Liberty Lake Rd\nPMB 263',
|
|
'city' => 'Liberty Lake',
|
|
'state' => 'WA',
|
|
'postcode' => '99019',
|
|
'country' => 'USA' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_addresses'}{'autoid'},
|
|
'method' => 'POST',
|
|
'type' => 'MAIN',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
|
|
addRow('contact_addresses',
|
|
{ 'address' => '5221 W Myrtlewood Ct',
|
|
'city' => 'Spokane',
|
|
'state' => 'WA',
|
|
'postcode' => '99208',
|
|
'country' => 'USA' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_addresses'}{'autoid'},
|
|
'method' => 'POST',
|
|
'type' => 'HOME',
|
|
'preference' => 'ALTERNATE' },
|
|
1);
|
|
|
|
addRow('contact_addresses',
|
|
{ 'address' => 'PO Box 69',
|
|
'city' => 'Granger',
|
|
'state' => 'WA',
|
|
'postcode' => '98932',
|
|
'country' => 'USA' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_addresses'}{'autoid'},
|
|
'method' => 'POST',
|
|
'type' => 'HOME',
|
|
'preference' => 'ALTERNATE' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'MOBILE',
|
|
'phone' => '5098445573' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'MAIN',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'MOBILE',
|
|
'phone' => '5098445973' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'MAIN',
|
|
'preference' => 'ALTERNATE' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'VIRTUAL',
|
|
'phone' => '5095901112' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'BUSINESS',
|
|
'preference' => 'WORK' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'LANDLINE',
|
|
'phone' => '5098541491' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'HOME',
|
|
'preference' => 'ALTERNATE' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'VIRTUAL',
|
|
'phone' => '8774488664' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'BUSINESS',
|
|
'preference' => 'WORK' },
|
|
1);
|
|
|
|
addRow('contact_phones',
|
|
{ 'type' => 'FAX',
|
|
'phone' => '8662960131' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'BUSINESS',
|
|
'preference' => 'WORK' },
|
|
1);
|
|
|
|
addRow('contact_emails',
|
|
{ 'email' => 'abijah\@PerkinsHouse.com' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_emails'}{'autoid'},
|
|
'method' => 'EMAIL',
|
|
'type' => 'HOME',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
|
|
addRow('contact_emails',
|
|
{ 'email' => 'abijah\@PerkinsREI.com' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_emails'}{'autoid'},
|
|
'method' => 'EMAIL',
|
|
'type' => 'HOME',
|
|
'preference' => 'WORK' },
|
|
1);
|
|
|
|
addRow('contact_emails',
|
|
{ 'email' => 'abijah\@ValleyStorage.com' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'},
|
|
'method_id' => $newdb{'tables'}{'contact_emails'}{'autoid'},
|
|
'method' => 'EMAIL',
|
|
'type' => 'BUSINESS',
|
|
'preference' => 'WORK' },
|
|
1);
|
|
|
|
|
|
#################################################################
|
|
## GROUPS
|
|
|
|
addRow('groups',
|
|
{ 'code' => 'Owner',
|
|
'name' => 'Owner Group' });
|
|
addRow('group_permissions',
|
|
{ 'group_id' => $newdb{'tables'}{'groups'}{'autoid'},
|
|
'name' => 'EVERYTHING',
|
|
'access' => 'FORCED' },
|
|
1);
|
|
|
|
|
|
#################################################################
|
|
## USERS
|
|
|
|
addRow('users',
|
|
{ 'code' => 'AP',
|
|
'login' => 'abijah',
|
|
'contact_id' => $newdb{'tables'}{'contacts'}{'autoid'} });
|
|
|
|
|
|
#################################################################
|
|
## SITES
|
|
|
|
addRow('sites',
|
|
{ 'code' => 'VSS',
|
|
'name' => 'Valley Storage' });
|
|
|
|
addRow('site_memberships',
|
|
{ 'site_id' => $newdb{'tables'}{'sites'}{'autoid'},
|
|
'user_id' => $newdb{'tables'}{'users'}{'autoid'},
|
|
'group_id' => $newdb{'tables'}{'groups'}{'autoid'} },
|
|
1);
|
|
|
|
addRow('site_areas',
|
|
{ 'site_id' => $newdb{'tables'}{'sites'}{'autoid'},
|
|
'code' => 'Main',
|
|
'name' => 'Main Facility Area' });
|
|
|
|
|
|
#################################################################
|
|
## LEASES
|
|
|
|
addRow('lease_types',
|
|
{ 'code' => 'SL',
|
|
'name' => 'Storage Lease' });
|
|
|
|
|
|
#################################################################
|
|
## LEDGERS
|
|
|
|
$newdb{'lookup'}{'account'} = {};
|
|
|
|
$query = "SELECT * FROM pmgr_accounts";
|
|
$result = query($db_handle, $query);
|
|
foreach $row (@$result) {
|
|
addRow('ledgers',
|
|
{ 'account_id' => $row->{'id'},
|
|
'open_stamp' => '2009-01-01',
|
|
'comment' => undef });
|
|
|
|
$newdb{'lookup'}{'account'}{$row->{'name'}}
|
|
= {'account' => $row->{'id'},
|
|
'tillable' => $row->{'tillable'},
|
|
'ledger' => $newdb{'tables'}{'ledgers'}{'autoid'} };
|
|
|
|
if ((!defined $newdb{'tables'}{'accounts'}{'autoid'}) ||
|
|
$row->{'id'} > $newdb{'tables'}{'accounts'}{'autoid'}) {
|
|
$newdb{'tables'}{'accounts'}{'autoid'} = $row->{'id'};
|
|
}
|
|
}
|
|
|
|
# For compatibility, while deciding on account names
|
|
$newdb{'lookup'}{'account'}{'Cash'}
|
|
= $newdb{'lookup'}{'account'}{'Payment'};
|
|
|
|
$newdb{'lookup'}{'charge_type'} = {};
|
|
$newdb{'lookup'}{'charge_type'}{'Rent'} =
|
|
$newdb{'lookup'}{'account'}{'Rent'};
|
|
$newdb{'lookup'}{'charge_type'}{'Late Fee'} =
|
|
$newdb{'lookup'}{'account'}{'Late Charge'};
|
|
$newdb{'lookup'}{'charge_type'}{'Security Deposit'} =
|
|
$newdb{'lookup'}{'account'}{'Security Deposit'};
|
|
|
|
|
|
#################################################################
|
|
## MONETARY
|
|
|
|
$newdb{'lookup'}{'monetary_type'} = {};
|
|
|
|
$query = "SELECT * FROM pmgr_monetary_types";
|
|
$result = query($db_handle, $query);
|
|
foreach $row (@$result) {
|
|
$newdb{'lookup'}{'monetary_type'}{$row->{'name'}}
|
|
= {'id' => $row->{'id'} };
|
|
}
|
|
|
|
$newdb{'lookup'}{'payment_type'} = {};
|
|
$newdb{'lookup'}{'payment_type'}{1}
|
|
= { 'name' => 'Cash', 'account' => 'Cash', 'monetary_type' => $newdb{'lookup'}{'monetary_type'}{'Cash'} };
|
|
$newdb{'lookup'}{'payment_type'}{2}
|
|
= { 'name' => 'Check', 'account' => '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'} };
|
|
$newdb{'lookup'}{'payment_type'}{4}
|
|
= { 'name' => 'ACH', 'account' => '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'} };
|
|
|
|
|
|
$newdb{'ids'}{'monetary_source'} = {};
|
|
$newdb{'ids'}{'monetary_source'}{'internal'} = undef;
|
|
|
|
addRow('monetary_sources',
|
|
{ 'monetary_type_id' => $newdb{'lookup'}{'monetary_type'}{'Cash'}{'id'},
|
|
'name' => 'Cash Source',
|
|
'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',
|
|
'comment' => 'Credited at the closing table' });
|
|
$newdb{'ids'}{'monetary_source'}{'Closing'} =
|
|
$newdb{'tables'}{'monetary_sources'}{'autoid'};
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
##
|
|
## UNITS
|
|
##
|
|
|
|
|
|
######################################################################
|
|
## Unit Types
|
|
|
|
$newdb{'lookup'}{'unit_type'} = {};
|
|
|
|
$query = "SELECT * FROM UnitType ORDER BY TypeID";
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
addRow('unit_types',
|
|
{ 'code' => 'xxx',
|
|
'name' => $row->{'UnitType'} });
|
|
$newdb{'lookup'}{'unit_type'}{$row->{'TypeID'}} =
|
|
$newdb{'tables'}{'unit_types'}{'autoid'};
|
|
}
|
|
|
|
######################################################################
|
|
## Unit Sizes
|
|
|
|
$newdb{'lookup'}{'unit_size'} = {};
|
|
|
|
$query = "SELECT * FROM UnitInfo WHERE UnitID <> 'POS\$' ORDER BY UnitID";
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
my $sz = sizeCode($row->{'Width'}, $row->{'Depth'});
|
|
next if defined $newdb{'lookup'}{'unit_size'}{$sz};
|
|
|
|
addRow('unit_sizes',
|
|
{ 'unit_type_id' => $row->{'Type'},
|
|
'code' => $sz,
|
|
'name' => $sz,
|
|
'width' => $internal_adjustment_factor * $row->{'Width'},
|
|
'depth' => $internal_adjustment_factor * $row->{'Depth'},
|
|
'deposit' => $row->{'StdSecDep'},
|
|
'amount' => $row->{'StdRent'} });
|
|
|
|
$newdb{'lookup'}{'unit_size'}{$sz}
|
|
= { 'id' => $newdb{'tables'}{'unit_sizes'}{'autoid'},
|
|
'rent' => $row->{'StdRent'},
|
|
'dep' => $row->{'StdSecDep'} };
|
|
}
|
|
|
|
######################################################################
|
|
## Units
|
|
|
|
$newdb{'lookup'}{'unit'} = {};
|
|
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
my $sz = sizeCode($row->{'Width'}, $row->{'Depth'});
|
|
my $szid = $newdb{'lookup'}{'unit_size'}{$sz}{'id'};
|
|
|
|
addRow('units',
|
|
{ 'unit_size_id' => $szid,
|
|
'code' => $row->{'UnitID'},
|
|
'name' => $row->{'UnitID'},
|
|
'status' => $row->{'Rented'} ?'OCCUPIED' :($row->{'Rentable'} ?'VACANT' :'UNAVAILABLE'),
|
|
'sort_order' => $newdb{'tables'}{'units'}{'autoid'},
|
|
'walk_order' => $newdb{'tables'}{'units'}{'autoid'},
|
|
'deposit' => $row->{'StdSecDep'},
|
|
'amount' => $row->{'StdRent'} });
|
|
|
|
$newdb{'lookup'}{'unit'}{$row->{'UnitID'}}
|
|
= { 'id' => $newdb{'tables'}{'units'}{'autoid'} };
|
|
}
|
|
|
|
|
|
######################################################################
|
|
## Map
|
|
|
|
my %info = ('extents' => {}, 'units' => {});
|
|
|
|
# Get the overall site limits
|
|
$query = "SELECT MIN(M.Top) AS mintop, MIN(M.Left) AS minlft,";
|
|
$query .= " MAX(M.Top + IIF(M.reverseWL, U.Width, U.Depth)) AS bot,";
|
|
$query .= " MAX(M.Left + IIF(M.reverseWL, U.Depth, U.Width)) AS rgt";
|
|
$query .= ' FROM UnitInfo U INNER JOIN mapUnitsV2 M ON M.unitID = U.UnitID';
|
|
$result = query($sdbh, $query);
|
|
|
|
# Fetch and verify the result
|
|
my $row = shift(@$result);
|
|
die("MIN query failed!") unless $row;
|
|
|
|
# Compute the actual boundaries, adjusting for a (0,0) origin
|
|
my $top_adjustment = 0 - $row->{'mintop'};
|
|
my $left_adjustment = 0 - $row->{'minlft'};
|
|
$info{'extents'}{'top'} = 0;
|
|
$info{'extents'}{'left'} = 0;
|
|
$info{'extents'}{'bottom'} = $internal_adjustment_factor * ($top_adjustment + $row->{'bot'} + 0);
|
|
$info{'extents'}{'right'} = $internal_adjustment_factor * ($left_adjustment + $row->{'rgt'} + 0);
|
|
|
|
addRow('maps',
|
|
{ 'name' => 'Main Facility Map',
|
|
'site_area_id' => $newdb{'tables'}{'site_areas'}{'autoid'},
|
|
'width' => $info{'extents'}{'right'} - $info{'extents'}{'left'},
|
|
'depth' => $info{'extents'}{'bottom'} - $info{'extents'}{'top'} });
|
|
|
|
# Get list of units and positions
|
|
$query = "SELECT U.UnitID, U.UnitID as name,";
|
|
$query .= " ($top_adjustment + M.Top) AS pt_t,";
|
|
$query .= " ($left_adjustment + M.Left) AS pt_l,";
|
|
$query .= " IIF(M.reverseWL, U.Depth, U.Width) AS Width,";
|
|
$query .= " IIF(M.reverseWL, U.Width, U.Depth) AS Depth,";
|
|
$query .= " M.reverseWL, U.Rented, U.Rentable";
|
|
$query .= " FROM UnitInfo U INNER JOIN mapUnitsV2 M ON M.unitID = U.UnitID";
|
|
|
|
# Go through each one, calculating the map location
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
addRow('maps_units',
|
|
{ 'map_id' => $newdb{'tables'}{'maps'}{'autoid'},
|
|
'unit_id' => $newdb{'lookup'}{'unit'}{$row->{'UnitID'}}{'id'},
|
|
'pt_top' => $internal_adjustment_factor * ($row->{'pt_t'}),
|
|
'pt_left' => $internal_adjustment_factor * ($row->{'pt_l'}),
|
|
'transpose' => $row->{'reverseWL'} });
|
|
}
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
##
|
|
## TENANTS
|
|
##
|
|
|
|
|
|
######################################################################
|
|
## Tenants
|
|
|
|
$newdb{'lookup'}{'tenant'} = {};
|
|
|
|
$query = "SELECT * FROM TenantInfo WHERE FirstName <> 'POS' ORDER BY TenantID";
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
|
|
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}
|
|
= { 'name' => "$row->{'LastName'}, $row->{'FirstName'}" };
|
|
|
|
addRow('contacts',
|
|
{ 'first_name' => $row->{'FirstName'},
|
|
'middle_name' => $row->{'MiddleName'},
|
|
'last_name' => $row->{'LastName'},
|
|
'id_local' => $row->{'IDNum'} || undef,
|
|
'id_local_state' => $row->{'IDNum'} ? $newdb{'lookup'}{'state'}{$row->{'DLStateID'}} : undef });
|
|
|
|
$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'};
|
|
|
|
addRow('customers',
|
|
{ 'name' => "$row->{'LastName'}, $row->{'FirstName'}",
|
|
'primary_contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
|
|
'account_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'account'} });
|
|
|
|
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'} =
|
|
$newdb{'tables'}{'customers'}{'autoid'};
|
|
|
|
addRow('contacts_customers',
|
|
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
|
|
'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
|
|
'type' => 'TENANT' },
|
|
1);
|
|
|
|
if ($row->{'City'}) {
|
|
addRow('contact_addresses',
|
|
{ 'address' => $row->{'HomeAddress'} . ($row->{'HomeAddr2'} ? "\n".$row->{'HomeAddr2'} : "") || undef,
|
|
'city' => $row->{'City'},
|
|
'state' => $newdb{'lookup'}{'state'}{$row->{'StateID'}},
|
|
'postcode' => $row->{'Zip'} || undef,
|
|
'country' => 'USA' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
|
|
'method_id' => $newdb{'tables'}{'contact_addresses'}{'autoid'},
|
|
'method' => 'POST',
|
|
'type' => 'HOME',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
}
|
|
|
|
foreach ({'type' => 'LANDLINE', 'preference' => 'PRIMARY', 'phone' => $row->{'Phone'}},
|
|
{'type' => 'LANDLINE', 'preference' => 'WORK',
|
|
'phone' => $row->{'BusinessPhone'}, 'ext' => $row->{'BusinessExt'}},
|
|
{'type' => 'FAX', 'preference' => 'PRIMARY', 'phone' => $row->{'FAX'}},
|
|
{'type' => 'PAGER', 'preference' => 'PRIMARY', 'phone' => $row->{'Pager'}},
|
|
{'type' => 'MOBILE', 'preference' => 'ALTERNATE', 'phone' => $row->{'CellPhone'}})
|
|
{
|
|
if ($_->{'phone'}) {
|
|
addRow('contact_phones',
|
|
{ 'type' => $_->{'type'},
|
|
'phone' => $_->{'phone'},
|
|
'ext' => $_->{'ext'} });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'MAIN',
|
|
'preference' => $_->{'preference'} },
|
|
1);
|
|
}
|
|
}
|
|
|
|
if ($row->{'Email'}) {
|
|
addRow('contact_emails',
|
|
{ 'email' => $row->{'Email'} });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'},
|
|
'method_id' => $newdb{'tables'}{'contact_emails'}{'autoid'},
|
|
'method' => 'EMAIL',
|
|
'type' => 'MAIN',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
}
|
|
|
|
next unless $row->{'AltFirstName'} || $row->{'AltLastName'} || $row->{'AltAddress'} || $row->{'AltPhone'};
|
|
|
|
addRow('contacts',
|
|
{ 'first_name' => $row->{'AltFirstName'} || undef,
|
|
'middle_name' => $row->{'AltMI'} || undef,
|
|
'last_name' => $row->{'AltLastName'} || undef });
|
|
|
|
$newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'} =
|
|
$newdb{'tables'}{'contacts'}{'autoid'};
|
|
|
|
addRow('contacts_customers',
|
|
{ 'customer_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'},
|
|
'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'},
|
|
'type' => 'ALTERNATE' },
|
|
1);
|
|
|
|
if ($row->{'AltCity'}) {
|
|
addRow('contact_addresses',
|
|
{ 'address' => $row->{'AltAddress'} . ($row->{'AltAddr2'} ? "\n".$row->{'AltAddr2'} : ""),
|
|
'city' => $row->{'AltCity'},
|
|
'state' => $newdb{'lookup'}{'state'}{$row->{'AltStateID'}},
|
|
'postcode' => $row->{'AltZip'},
|
|
'country' => 'USA' });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'},
|
|
'method_id' => $newdb{'tables'}{'contact_addresses'}{'autoid'},
|
|
'method' => 'POST',
|
|
'type' => 'MAIN',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
}
|
|
|
|
if ($row->{'AltPhone'}) {
|
|
addRow('contact_phones',
|
|
{ 'type' => 'LANDLINE',
|
|
'phone' => $row->{'AltPhone'},
|
|
'ext' => undef });
|
|
addRow('contacts_methods',
|
|
{ 'contact_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'},
|
|
'method_id' => $newdb{'tables'}{'contact_phones'}{'autoid'},
|
|
'method' => 'PHONE',
|
|
'type' => 'MAIN',
|
|
'preference' => 'PRIMARY' },
|
|
1);
|
|
}
|
|
}
|
|
|
|
|
|
######################################################################
|
|
## Tenant Leases
|
|
|
|
$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)}) {
|
|
|
|
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}
|
|
= { 'cust' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'} };
|
|
|
|
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'};
|
|
|
|
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'},
|
|
'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{'tables'}{'leases'}{'autoid'};
|
|
}
|
|
|
|
|
|
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
######################################################################
|
|
##
|
|
## TRANSACTIONS
|
|
##
|
|
|
|
######################################################################
|
|
## Charges
|
|
|
|
$newdb{'lookup'}{'charge'} = {};
|
|
|
|
$query = "SELECT * FROM Charges ORDER BY ChargeID";
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
|
|
addRow('transactions',
|
|
{ 'stamp' => datefmt($row->{'ChargeDate'}),
|
|
'through_date' => datefmt($row->{'EndDate'}) });
|
|
|
|
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}
|
|
= { 'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
|
|
'ledger' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'},
|
|
'amount' => $row->{'ChargeAmount'},
|
|
'tax_amount' => $row->{'TaxAmount'},
|
|
};
|
|
|
|
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'},
|
|
|
|
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'},
|
|
'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'},
|
|
});
|
|
|
|
|
|
next unless $row->{'TaxAmount'};
|
|
|
|
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'},
|
|
'amount' => $row->{'TaxAmount'},
|
|
'comment' => undef });
|
|
|
|
$newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'tax_entry'}
|
|
= $newdb{'tables'}{'ledger_entries'}{'autoid'},
|
|
|
|
}
|
|
|
|
|
|
######################################################################
|
|
## Receipts
|
|
|
|
$newdb{'lookup'}{'receipt'} = {};
|
|
|
|
$query =
|
|
"SELECT R.ReceiptNum, C.LedgerID, R.ReceiptDate" .
|
|
" FROM Receipts R, Payments P, Charges C" .
|
|
" WHERE P.ReceiptNum = R.ReceiptNum" .
|
|
" AND C.ChargeID = P.ChargeID" .
|
|
" GROUP BY R.ReceiptNum, C.LedgerID, R.ReceiptDate" .
|
|
" ORDER BY R.ReceiptNum";
|
|
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'});
|
|
push(@{$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'ledgers'}},
|
|
$newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'});
|
|
#print Dumper $receipt_map{$row->{'ReceiptNum'}};
|
|
next;
|
|
}
|
|
|
|
addRow('transactions',
|
|
{ 'stamp' => datefmt($row->{'ReceiptDate'}),
|
|
'through_date' => undef });
|
|
|
|
$newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}
|
|
= {'tx' => $newdb{'tables'}{'transactions'}{'autoid'},
|
|
'cust' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'},
|
|
'ledgers' => [ $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'} ] };
|
|
}
|
|
|
|
|
|
# sub idkeys { [ sort( {$a <=> $b} keys(%{$_[0]})) ] }
|
|
# $Data::Dumper::Sortkeys = \&idkeys;
|
|
|
|
|
|
######################################################################
|
|
## Payments
|
|
|
|
$newdb{'lookup'}{'payment'} = {};
|
|
|
|
$query = "SELECT * FROM Payments ORDER BY PaymentID";
|
|
foreach $row (@{query($sdbh, $query)})
|
|
{
|
|
my $monetary_source_id;
|
|
if ($row->{'PaymentDate'} =~ m%3/25/2009%) {
|
|
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{'closing'};
|
|
}
|
|
else {
|
|
$monetary_source_id = $newdb{'ids'}{'monetary_source'}{
|
|
$newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'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'};
|
|
}
|
|
|
|
my $payment_acct = $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'account'};
|
|
|
|
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'}" });
|
|
|
|
$newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}
|
|
= { 'entry' => $newdb{'tables'}{'ledger_entries'}{'autoid'} };
|
|
|
|
# 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;
|
|
|
|
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'},
|
|
'amount' => $reconcile_amount,
|
|
});
|
|
|
|
# Update the transaction to use the memo from this payment
|
|
if ($row->{'Memo'}) {
|
|
my $txid = $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'};
|
|
$newdb{'tables'}{'transactions'}{'rows'}[$txid]{'comment'} = $row->{'Memo'};
|
|
}
|
|
|
|
}
|
|
|
|
|
|
######################################################################
|
|
## Fake Data for Testing
|
|
|
|
my %fake =
|
|
('custid' => 2,
|
|
'invoice' => [ { 'id' => 1000,
|
|
'date' => '2009-05-05',
|
|
'entry' => [ { 'id' => 2100,
|
|
'account' => 'Security Deposit',
|
|
'amount' => 10,
|
|
'tax' => 0 },
|
|
],
|
|
},
|
|
{ 'id' => 1001,
|
|
'date' => '2009-05-01',
|
|
'thru' => '2009-05-31',
|
|
'entry' => [ { 'id' => 2110,
|
|
'account' => 'Rent',
|
|
'amount' => 100,
|
|
'tax' => 5 },
|
|
],
|
|
},
|
|
{ 'id' => 1002,
|
|
'date' => '2009-05-11',
|
|
'entry' => [ { 'id' => 2120,
|
|
'account' => 'Late Charge',
|
|
'amount' => 25,
|
|
'tax' => 0 },
|
|
],
|
|
},
|
|
],
|
|
'receipt' => [ { 'id' => 2000,
|
|
'date' => '2009-05-15',
|
|
'entry' => [ { 'id' => 2200,
|
|
'track' => [ { 'debit'=>2100, 'amount'=>10 },
|
|
{ 'debit'=>2110, 'amount'=>5 } ],
|
|
'type' => 1,
|
|
'amount' => 15 },
|
|
{ 'id' => 2201,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>10 } ],
|
|
'type' => 2,
|
|
'amount' => 10 },
|
|
{ 'id' => 2202,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>5 } ],
|
|
'type' => 3,
|
|
'amount' => 5 },
|
|
],
|
|
},
|
|
{ 'id' => 2001,
|
|
'date' => '2009-05-18',
|
|
'entry' => [ { 'id' => 2210,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>30 } ],
|
|
'type' => 5,
|
|
'amount' => 30 },
|
|
{ 'id' => 2211,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>20 } ],
|
|
'type' => 6,
|
|
'amount' => 20 },
|
|
],
|
|
},
|
|
{ 'id' => 2002,
|
|
'date' => '2009-05-22',
|
|
'entry' => [ { 'id' => 2220,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>10 } ],
|
|
'type' => 1,
|
|
'amount' => 10 },
|
|
{ 'id' => 2221,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>5 } ],
|
|
'type' => 2,
|
|
'amount' => 5 },
|
|
{ 'id' => 2222,
|
|
'track' => [ { 'debit'=>2110, 'amount'=>15 },
|
|
{ 'debit'=>2111, 'amount'=>5 },
|
|
{ 'debit'=>2120, 'amount'=>5 } ],
|
|
'type' => 7,
|
|
'amount' => 25 },
|
|
{ 'id' => 2223,
|
|
'track' => [ { 'debit'=>2120, 'amount'=>20 } ],
|
|
'type' => 8,
|
|
'amount' => 30 },
|
|
],
|
|
},
|
|
],
|
|
|
|
);
|
|
|
|
|
|
$newdb{'ids'}{'ledger'}{'Cash-Old'} =
|
|
$newdb{'lookup'}{'account'}{'Cash'}{'ledger'};
|
|
|
|
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'} =
|
|
$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'};
|
|
}
|
|
|
|
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" });
|
|
# 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()';
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
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'}
|
|
});
|
|
}
|
|
|
|
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'}{$dr}{'ledger'},
|
|
'credit_ledger_id' => $newdb{'lookup'}{'account'}{'Tax'}{'ledger'},
|
|
'amount' => $e->{'tax'},
|
|
'comment' => "Fake Tax" },
|
|
1);
|
|
}
|
|
}
|
|
}
|
|
|
|
$Data::Dumper::Sortkeys = 1;
|
|
print Dumper \%newdb;
|
|
# exit;
|
|
|
|
buildTables();
|
|
|
|
|
|
######################################################################
|
|
## Security Deposits
|
|
|
|
$query = "SELECT L.LedgerID, L.UnitID, C.ChargeAmount FROM TenantLedger L INNER JOIN Charges C ON C.LedgerID = L.LedgerID WHERE L.UnitID <> 'POS\$' AND C.ChargeDescription = 'Security Deposit' ORDER BY UnitID";
|
|
|
|
foreach $row (@{query($sdbh, $query)}) {
|
|
#my $uid = $newdb{'lookup'}{'unit'}{$row->{'UnitID'}};
|
|
$query = "UPDATE pmgr_leases
|
|
SET deposit = $row->{'ChargeAmount'}
|
|
WHERE `number` = '$row->{'LedgerID'}'";
|
|
query($db_handle, $query, $row);
|
|
}
|
|
|
|
######################################################################
|
|
## Ledger Assignments
|
|
|
|
# $query =
|
|
# "UPDATE pmgr_accounts A, pmgr_ledgers L SET A.ledger_id = L.id" .
|
|
# " WHERE A.id = L.account_id";
|
|
# query($db_handle, $query);
|
|
|
|
######################################################################
|
|
## Unit Lease Assignments
|
|
|
|
$query = "UPDATE pmgr_units U, pmgr_leases L
|
|
SET U.`current_lease_id` = L.id
|
|
WHERE L.unit_id = U.id AND L.close_date IS NULL";
|
|
query($db_handle, $query);
|
|
|