Added a scripts directory and the latest copy of our sitelink conversion script. For the complete revision history of sitelink2pmgr.pl, see branches/rent_manager_20090510 path of the 'domain' repository

git-svn-id: file:///svn-source/pmgr/branches/initial_20090526@8 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-05-27 19:26:04 +00:00
parent c5d5e2c651
commit 2665d012c2

796
scripts/sitelink2rmgr.pl Normal file
View File

@@ -0,0 +1,796 @@
#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', 'rentmgr', 'root', 'mysql97312519');
my $db_handle = DBI->connect("DBI:mysql:database=$database;host=$hostname",
$user, $password,
{'RaiseError' => 1});
my ($query, $result, $nrows, $row);
my ($aicur_c_id, $aicur_ca_id, $aicur_cp_id, $aicur_ce_id) = (0, 0, 0, 0);
my ($aicur_g_id, $aicur_gp_id, $aicur_user_id) = (0, 0, 0);
my ($aicur_type_id, $aicur_size_id, $aicur_unit_id) = (0, 0, 0);
my ($aicur_site_id, $aicur_area_id) = (0, 0);
my ($aicur_lease_type_id, $aicur_lease_id) = (0, 0);
my ($aicur_charge_id, $aicur_receipt_id, $aicur_payment_id) = (0, 0, 0);
open(SCHEMA, "<$schema_file") || die ("Can't open schema ($!)\n");
my $schema_query = "";
while (<SCHEMA>) {
next if /^\s*-- /;
$schema_query .= $_;
query($db_handle, $schema_query), $schema_query = ""
if /;\s*$/;
}
close(SCHEMA);
++$aicur_c_id;
$query = "INSERT INTO rmgr_contacts
(`id`, `first_name`, `middle_name`, `last_name`, `display_name`) VALUES
($aicur_c_id, 'Abijah', 'M', 'Perkins', 'Perkins, Abijah')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ca_id;
$query = "INSERT INTO rmgr_contactAddresses
(`id`, `address`, `city`, `state`, `postcode`, `country`) VALUES
($aicur_ca_id, '1324 N Liberty Lake Rd\nPMB 263', 'Liberty Lake', 'WA', '99019', 'USA')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'POST', $aicur_ca_id, 'MAIN', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ca_id;
$query = "INSERT INTO rmgr_contactAddresses
(`id`, `address`, `city`, `state`, `postcode`, `country`) VALUES
($aicur_ca_id, '5221 W Myrtlewood Ct', 'Spokane', 'WA', '99208', 'USA')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'POST', $aicur_ca_id, 'HOME', 'ALTERNATE')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ca_id;
$query = "INSERT INTO rmgr_contactAddresses
(`id`, `address`, `city`, `state`, `postcode`, `country`) VALUES
($aicur_ca_id, 'PO Box 69', 'Granger', 'WA', '98932', 'USA')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'POST', $aicur_ca_id, 'HOME', 'ALTERNATE')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'MOBILE', '5098445573')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'MAIN', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'MOBILE', '5098445973')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'MAIN', 'ALTERNATE')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'VIRTUAL', '5095901112')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'BUSINESS', 'WORK')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'LANDLINE', '5098541491')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'HOME', 'ALTERNATE')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'VIRTUAL', '8774488664')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'BUSINESS', 'WORK')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_cp_id;
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`) VALUES
($aicur_cp_id, 'FAX', '8662960131')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'PHONE', $aicur_cp_id, 'BUSINESS', 'WORK')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ce_id;
$query = "INSERT INTO rmgr_contactEmails
(`id`, `email`) VALUES
($aicur_ce_id, 'abijah\@PerkinsHouse.com')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'EMAIL', $aicur_ce_id, 'HOME', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ce_id;
$query = "INSERT INTO rmgr_contactEmails
(`id`, `email`) VALUES
($aicur_ce_id, 'abijah\@PerkinsREI.com')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'EMAIL', $aicur_ce_id, 'HOME', 'WORK')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_ce_id;
$query = "INSERT INTO rmgr_contactEmails
(`id`, `email`) VALUES
($aicur_ce_id, 'abijah\@ValleyStorage.com')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($aicur_c_id, 'EMAIL', $aicur_ce_id, 'BUSINESS', 'WORK')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_g_id;
$query = "INSERT INTO rmgr_groups
(`id`, `code`, `name`)
VALUES
($aicur_g_id, 'Owner', 'Owner Group')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_gp_id;
$query = "INSERT INTO rmgr_groupPermissions
(`group_id`, `name`, `access`)
VALUES
($aicur_gp_id, 'EVERYTHING', 'FORCED')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_user_id;
$query = "INSERT INTO rmgr_users
(`id`, `code`, `login`, `contact_id`)
VALUES
($aicur_user_id, 'AP', 'abijah', $aicur_c_id)";
query($db_handle, $query) || die("Unable to insert");
++$aicur_site_id;
$query = "INSERT INTO rmgr_sites
(`id`, `code`, `name`)
VALUES
($aicur_site_id, 'VSS', 'Valley Storage')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_siteMemberships
(`site_id`, `user_id`, `group_id`)
VALUES
($aicur_site_id, $aicur_user_id, $aicur_g_id)";
query($db_handle, $query) || die("Unable to insert");
++$aicur_area_id;
$query = "INSERT INTO rmgr_siteAreas
(`id`, `site_id`, `code`, `name`)
VALUES
($aicur_area_id, $aicur_site_id, 'Main', 'Main Facility Area')";
query($db_handle, $query) || die("Unable to insert");
++$aicur_lease_type_id;
$query = "INSERT INTO rmgr_leaseTypes
(`id`, `code`, `name`)
VALUES
($aicur_lease_type_id, 'SL', 'Storage Lease')";
query($db_handle, $query) || die("Unable to insert");
######################################################################
######################################################################
## query
sub query {
my ($dbh, $sql) = @_;
#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);
}
#print("sth = " . Dumper($sth) . "\n");
#print("result = " . Dumper($result) . "\n");
return ($sth, $result);
}
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] : "");
}
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
##
## UNITS
##
######################################################################
## Unit Types
$query = "SELECT * FROM UnitType ORDER BY TypeID";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Unit Types\n");
foreach $row (@$result) {
$query = "INSERT INTO rmgr_unitTypes
(`id`, `code`, `name`)
VALUES
($row->{'TypeID'}, 'xxx', '$row->{'UnitType'}')";
query($db_handle, $query) || die("Unable to insert");
}
######################################################################
## Unit Sizes
$query = "SELECT * FROM UnitInfo WHERE UnitID <> 'POS\$' ORDER BY UnitID";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Units\n");
my (%unit_size_map);
foreach $row (@$result) {
my $sz = sizeCode($row->{'Width'}, $row->{'Depth'});
next if defined $unit_size_map{$sz};
$unit_size_map{$sz} = { 'id' => ++$aicur_size_id };
$unit_size_map{$sz}{'rent'} = $row->{'StdRent'};
$unit_size_map{$sz}{'dep'} = $row->{'StdSecDep'};
my $szid = $unit_size_map{$sz}{'id'};
$query = "INSERT INTO rmgr_unitSizes
(`id`, `type_id`, `code`, `name`, `width`, `depth`,
`deposit`, `amount`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($szid, $row->{'Type'}, $sz, $sz,
$internal_adjustment_factor * $row->{'Width'},
$internal_adjustment_factor * $row->{'Depth'},
$row->{'StdSecDep'}, $row->{'StdRent'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
######################################################################
## Units
my (%unit_map);
foreach $row (@$result) {
my $sz = sizeCode($row->{'Width'}, $row->{'Depth'});
my $szid = $unit_size_map{$sz}{'id'};
$unit_map{$row->{'UnitID'}} = ++$aicur_unit_id;
my $uid = $unit_map{$row->{'UnitID'}};
$query = "INSERT INTO rmgr_units
(`id`, `size_id`, `code`, `name`,
`status`,
`sort_order`, `walk_order`, `deposit`, `amount`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($uid, $szid, $row->{'UnitID'}, $row->{'UnitID'},
$row->{'Rented'} ?'OCCUPIED' :($row->{'Rentable'} ?'VACANT' :'UNAVAILABLE'),
$uid, $uid, $row->{'StdSecDep'}, $row->{'StdRent'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
######################################################################
## 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);
$query =
"INSERT INTO rmgr_maps (id, site_id, area_id, width, depth) VALUES\n" .
" (1, $aicur_site_id, $aicur_area_id," .
($info{'extents'}{'right'} - $info{'extents'}{'left'}) . ", " .
($info{'extents'}{'bottom'} - $info{'extents'}{'top'}) . ")";
query($db_handle, $query) || die("Unable to insert");
# 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";
$result = query($sdbh, $query);
# Go through each one, calculating the map location
foreach $row (@$result) {
my $uid = $unit_map{$row->{'UnitID'}};
$query = # pt_bottom, pt_right,
"INSERT INTO rmgr_mapUnits (map_id, unit_id,
pt_top, pt_left,
transpose) VALUES
(" . join(", ",
1, $uid,
$internal_adjustment_factor * ($row->{'pt_t'}),
$internal_adjustment_factor * ($row->{'pt_l'}),
$row->{'reverseWL'}) . ")";
query($db_handle, $query) || die("Unable to insert");
}
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
##
## TENANTS
##
######################################################################
## Tenants
$query = "SELECT * FROM TenantInfo WHERE FirstName <> 'POS' ORDER BY TenantID";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Tenants\n");
my %stmap = ( 1=>'AK', 14=>'ID', 48=>'WA' );
my (%tenant_map);
foreach $row (@$result) {
$tenant_map{$row->{'TenantID'}} = { 'id' => ++$aicur_c_id };
my $cid = $tenant_map{$row->{'TenantID'}}{'id'};
$query = "INSERT INTO rmgr_contacts
(`id`, `first_name`, `middle_name`, `last_name`, `display_name`, `id_num`, `id_state`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($cid, $row->{'FirstName'}, $row->{'MiddleName'}, $row->{'LastName'},
"$row->{'LastName'}, $row->{'FirstName'}",
$row->{'IDNum'} || undef, $row->{'IDNum'} ? $stmap{$row->{'DLStateID'}} : undef)
)) . ")";
query($db_handle, $query) || die("Unable to insert");
if ($row->{'City'}) {
$query = "INSERT INTO rmgr_contactAddresses
(`id`, `address`, `city`, `state`, `postcode`, `country`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
(++$aicur_ca_id,
#$row->{'HomeAddress'}
$row->{'HomeAddress'} . ($row->{'HomeAddr2'} ? "\n".$row->{'HomeAddr2'} : "") || undef,
$row->{'City'}, $stmap{$row->{'StateID'}}, $row->{'Zip'} || undef, 'USA')
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($cid, 'POST', $aicur_ca_id, 'HOME', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
}
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'}) {
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`, `ext`) VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
(++$aicur_cp_id, $_->{'type'}, $_->{'phone'}, $_->{'ext'})))
. ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($cid, 'PHONE', $aicur_cp_id, 'MAIN', $_->{'preference'})))
. ")";
query($db_handle, $query) || die("Unable to insert");
}
}
if ($row->{'Email'}) {
$query = "INSERT INTO rmgr_contactEmails
(`id`, `email`) VALUES
(".++$aicur_ce_id.", '$row->{'Email'}')";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUE
($cid, 'EMAIL', $aicur_ce_id, 'MAIN', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
}
next unless $row->{'AltFirstName'} || $row->{'AltLastName'} || $row->{'AltAddress'} || $row->{'AltPhone'};
$tenant_map{$row->{'TenantID'}}{'alt'} = ++$aicur_c_id;
$cid = $tenant_map{$row->{'TenantID'}}{'alt'};
$query = "INSERT INTO rmgr_contacts
(`id`, `first_name`, `middle_name`, `last_name`, `display_name`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($cid,
$row->{'AltFirstName'} || undef,
$row->{'AltMI'} || undef,
$row->{'AltLastName'} || undef,
($row->{'AltLastName'} && $row->{'AltFirstName'}
? "$row->{'AltLastName'}, $row->{'AltFirstName'}"
: ($row->{'AltLastName'} || $row->{'AltFirstName'} || '<UNKNOWN>')))
)) . ")";
query($db_handle, $query) || die("Unable to insert");
if ($row->{'AltCity'}) {
$query = "INSERT INTO rmgr_contactAddresses
(`id`, `address`, `city`, `state`, `postcode`, `country`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
(++$aicur_ca_id,
$row->{'AltAddress'} . ($row->{'AltAddr2'} ? "\n".$row->{'AltAddr2'} : ""),
$row->{'AltCity'}, $stmap{$row->{'AltStateID'}}, $row->{'AltZip'}, 'USA')
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($cid, 'POST', $aicur_ca_id, 'MAIN', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
}
if ($row->{'AltPhone'}) {
$query = "INSERT INTO rmgr_contactPhones
(`id`, `type`, `phone`, `ext`) VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
(++$aicur_cp_id, 'LANDLINE', $row->{'AltPhone'}, undef)
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_contactMethods
(`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES
($cid, 'PHONE', $aicur_cp_id, 'MAIN', 'PRIMARY')";
query($db_handle, $query) || die("Unable to insert");
}
}
######################################################################
## Tenant Leases
$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";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Tenant Ledgers\n");
my (%ledger_map);
foreach $row (@$result) {
$ledger_map{$row->{'LedgerID'}} = ++$aicur_lease_id;
my $cid = $tenant_map{$row->{'TenantID'}}{'id'};
my $uid = $unit_map{$row->{'UnitID'}};
my $lid = $ledger_map{$row->{'LedgerID'}};
$query = "INSERT INTO rmgr_leases
(`id`, `number`, `lease_type_id`, `unit_id`,
`lease_date`, `movein_date`,
`moveout_date`, `close_date`,
`amount`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($lid, $row->{'LedgerID'}, $aicur_lease_type_id, $uid,
datefmt($row->{'DateIn'}), datefmt($row->{'DateIn'}),
datefmt($row->{'DateOut'}), datefmt($row->{'DateClosed'}),
$row->{'Rent'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_leaseContacts
(`lease_id`, `contact_id`, `type`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($lid, $cid, 'TENANT')
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$cid = $tenant_map{$row->{'TenantID'}}{'alt'};
next unless $cid;
$query = "INSERT INTO rmgr_leaseContacts
(`lease_id`, `contact_id`, `type`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($lid, $cid, 'ALTERNATE')
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
######################################################################
## 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";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Security Deposits\n");
foreach $row (@$result) {
my $uid = $unit_map{$row->{'UnitID'}};
$query = "UPDATE rmgr_leases
SET deposit = $row->{'ChargeAmount'}
WHERE `number` = '$row->{'LedgerID'}'";
query($db_handle, $query) || die("Unable to update");
}
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
######################################################################
##
## TRANSACTIONS
##
######################################################################
## Charges
$query = "SELECT * FROM Charges ORDER BY ChargeID";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Charges\n");
my %charge_type_map = ('Rent' => 2, 'Security Deposit' => 1, 'Late Fee' => 3);
my %charge_map;
foreach $row (@$result) {
$charge_map{$row->{'ChargeID'}} = ++$aicur_charge_id;
my $lid = $ledger_map{$row->{'LedgerID'}};
my $chid = $charge_map{$row->{'ChargeID'}};
$query = "INSERT INTO rmgr_transactionCharges
(`id`, `type_id`, `lease_id`,
`charge_date`, `charge_to_date`,
`amount`, `tax`, `total`, `comment`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($chid, $charge_type_map{$row->{'ChargeDescription'}}, $lid,
datefmt($row->{'ChargeDate'}), datefmt($row->{'EndDate'}),
$row->{'ChargeAmount'}, $row->{'TaxAmount'},
$row->{'ChargeAmount'} + $row->{'TaxAmount'},
"ChargeID #".$row->{'ChargeID'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "UPDATE rmgr_transactionCharges";
$query .= " SET due_date = DATE_ADD(charge_date, INTERVAL IF(type_id=2,10,0) DAY)";
query($db_handle, $query) || die("Unable to update");
}
######################################################################
## Receipts
$query = "SELECT * FROM Receipts ORDER BY ReceiptNum";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Receipts\n");
my %receipt_map;
foreach $row (@$result) {
$receipt_map{$row->{'ReceiptNum'}} = ++$aicur_receipt_id;
my $lid = $ledger_map{$row->{'LedgerID'}};
my $rid = $receipt_map{$row->{'ReceiptNum'}};
$query = "INSERT INTO rmgr_transactionReceipts
(`id`, `stamp`, `comment`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($rid, datefmt($row->{'ReceiptDate'}),
"ReceiptNum #".$row->{'ReceiptNum'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
# $query = "SELECT * FROM Receipts R INNER JOIN Payments P ON P.ReceiptNum = R.ReceiptNum" .
# " WHERE P.PaymentDate <> R.ReceiptDate";
# $result = query($sdbh, $query);
# $nrows = @$result;
# print(" $nrows Mismatches\n");
# foreach $row (@$result) {
# my @keys = qw(PaymentID P.ReceiptNum PaymentDate ReceiptDate);
# print("Payment Date Mismatch:\n");
# print(" ",
# join("\n ", map({sprintf("%-12s => %s", $_, $row->{$_})} @keys)),
# "\n");
# }
######################################################################
## Payments
$query = "SELECT * FROM Payments ORDER BY PaymentID";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Payments\n");
my %payment_map;
foreach $row (@$result) {
$payment_map{$row->{'PaymentID'}} = ++$aicur_payment_id;
my $pid = $payment_map{$row->{'PaymentID'}};
my $rid = $receipt_map{$row->{'ReceiptNum'}};
my $chid = $charge_map{$row->{'ChargeID'}};
$query = "INSERT INTO rmgr_transactionPayments
(`id`, `receipt_id`, `payment_type_id`, `amount`, `comment`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($pid, $rid,
$row->{'PaymentDate'} =~ m%3/25/2009% ? 8 : $row->{'PaymentType'},
$row->{'PaymentAmount'},
"PaymentID #".$row->{'PaymentID'} . ($row->{'Memo'} ? "; ".$row->{'Memo'} : ""))
)) . ")";
query($db_handle, $query) || die("Unable to insert");
$query = "INSERT INTO rmgr_transactionReconciliations
(`charge_id`, `receipt_id`, `amount`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($chid, $rid, $row->{'PaymentAmount'})
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
__END__
######################################################################
## Reconciliations
# $query = "SELECT ReceiptNum, SUM(PaymentAmount) AS 'rTot'";
# $query .= " FROM Payments GROUP BY ReceiptNum, ORDER BY ReceiptNum";
$query = "SELECT ChargeID, ReceiptNum";
$query .= " FROM Payments GROUP BY ChargeID, ReceiptNum ORDER BY ChargeID, ReceiptNum";
$result = query($sdbh, $query);
$nrows = @$result;
print(" $nrows Reconciliations\n");
foreach $row (@$result) {
my $chid = $charge_map{$row->{'ChargeID'}};
my $rid = $receipt_map{$row->{'ReceiptNum'}};
$query = "INSERT INTO rmgr_transactionReconciliations
(`charge_id`, `receipt_id`, `amount`)
VALUES (" .
join(", ", map({defined $_ ? "'$_'" : "NULL"}
($chid, $rid, 0)
)) . ")";
query($db_handle, $query) || die("Unable to insert");
}
$query ="UPDATE rmgr_transactionReconciliations R
JOIN (SELECT P.receipt_id AS rid,
IF(COALESCE(Ch.total, 0)<SUM(P.amount),
COALESCE(Ch.total, 0),
SUM(P.amount)) AS rtot
FROM rmgr_transactionpayments P
LEFT JOIN rmgr_transactionReconciliations Rl ON Rl.receipt_id = P.receipt_id
LEFT JOIN rmgr_transactionCharges Ch ON Ch.id = Rl.charge_id
GROUP BY P.receipt_id) Rx ON Rx.rid = R.receipt_id
SET R.amount = Rx.rtot";
query($db_handle, $query) || die("Unable to update");
$query ="UPDATE rmgr_transactionReconciliations R
JOIN (SELECT P.receipt_id AS rid, P.total AS total
FROM rmgr_transactionpayments P
LEFT JOIN rmgr_transactionReconciliations Rl ON Rl.receipt_id = P.receipt_id
GROUP BY P.receipt_id) Rx ON Rx.rid = R.receipt_id
SET R.amount = Rx.rtot";
query($db_handle, $query) || die("Unable to update");
# SELECT Rs.receipt_id, Rs.rsum, Ps.psum, rids, pids
# FROM
# (SELECT receipt_id, SUM(R.amount) AS rsum, CAST(GROUP_CONCAT(id) AS CHAR) AS rids
# FROM rmgr_transactionreconciliations R
# GROUP BY receipt_id) Rs
# LEFT JOIN
# (SELECT receipt_id, SUM(P.amount) AS psum, CAST(GROUP_CONCAT(id) AS CHAR) AS pids
# FROM rmgr_transactionpayments P
# GROUP BY receipt_id) Ps ON Ps.receipt_id = Rs.receipt_id
# Disconnect from the database.
$db_handle->disconnect();