#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 ($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); my ($aicur_tx_id) = (0); my ($aicur_ledger_id) = (0); my ($aicur_monetary_source_id) = (0); open(SCHEMA, "<$schema_file") || die ("Can't open schema ($!)\n"); my $schema_query = ""; while () { next if /^\s*-- /; $schema_query .= $_; query($db_handle, $schema_query), $schema_query = "" if /;\s*$/; } close(SCHEMA); ++$aicur_c_id; $query = "INSERT INTO pmgr_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 pmgr_contact_addresses (`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 pmgr_contacts_methods (`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 pmgr_contact_addresses (`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 pmgr_contacts_methods (`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 pmgr_contact_addresses (`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 pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'MOBILE', '5098445573')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'MOBILE', '5098445973')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'VIRTUAL', '5095901112')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'LANDLINE', '5098541491')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'VIRTUAL', '8774488664')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_phones (`id`, `type`, `phone`) VALUES ($aicur_cp_id, 'FAX', '8662960131')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_emails (`id`, `email`) VALUES ($aicur_ce_id, 'abijah\@PerkinsHouse.com')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_emails (`id`, `email`) VALUES ($aicur_ce_id, 'abijah\@PerkinsREI.com')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_contact_emails (`id`, `email`) VALUES ($aicur_ce_id, 'abijah\@ValleyStorage.com')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_contacts_methods (`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 pmgr_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 pmgr_group_permissions (`group_id`, `name`, `access`) VALUES ($aicur_gp_id, 'EVERYTHING', 'FORCED')"; query($db_handle, $query) || die("Unable to insert"); ++$aicur_user_id; $query = "INSERT INTO pmgr_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 pmgr_sites (`id`, `code`, `name`) VALUES ($aicur_site_id, 'VSS', 'Valley Storage')"; query($db_handle, $query) || die("Unable to insert"); $query = "INSERT INTO pmgr_site_memberships (`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 pmgr_site_areas (`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 pmgr_lease_types (`id`, `code`, `name`) VALUES ($aicur_lease_type_id, 'SL', 'Storage Lease')"; query($db_handle, $query) || die("Unable to insert"); my (%account_map, %monetary_type_map); $query = "SELECT * FROM pmgr_accounts"; $result = query($db_handle, $query); foreach $row (@$result) { ++$aicur_ledger_id; $query = "INSERT INTO pmgr_ledgers (`id`, `account_id`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($aicur_ledger_id, $row->{'id'}, $row->{'name'} . ' Ledger', undef) )) . ")"; query($db_handle, $query) || die("Unable to insert"); $account_map{$row->{'name'}} = {'id' => $row->{'id'}, 'ledger' => $aicur_ledger_id }; } # $query = # "UPDATE pmgr_accounts A, pmgr_ledgers L SET A.ledger_id = L.id" . # " WHERE A.id = L.account_id"; # query($db_handle, $query); $query = "SELECT * FROM pmgr_monetary_types"; $result = query($db_handle, $query); foreach $row (@$result) { $monetary_type_map{$row->{'name'}} = {'id' => $row->{'id'}, 'name' => $row->{'name'}}; } $account_map{'Late Fee'} = $account_map{'Late Charge'}; $monetary_type_map{1} = $monetary_type_map{'Cash'}; $monetary_type_map{2} = $monetary_type_map{'Check'}; $monetary_type_map{3} = $monetary_type_map{'Money Order'}; $monetary_type_map{4} = $monetary_type_map{'ACH'}; $monetary_type_map{12} = $monetary_type_map{'Other Non-Tillable'}; my $monetary_source_internal = undef; my $monetary_source_cash = ++$aicur_monetary_source_id; $query = "INSERT INTO pmgr_monetary_sources (`id`, `monetary_type_id`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_cash, $monetary_type_map{'Cash'}{'id'}, 'Cash Source', 'Monetary source used for any cash transaction') )) . ")"; query($db_handle, $query) || die("Unable to insert"); my $monetary_source_closing = ++$aicur_monetary_source_id; $query = "INSERT INTO pmgr_monetary_sources (`id`, `monetary_type_id`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_closing, $monetary_type_map{'Other Non-Tillable'}{'id'}, 'Closing Monies Credited', 'Credited at the closing table') )) . ")"; query($db_handle, $query) || die("Unable to insert"); ###################################################################### ###################################################################### ## 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; } ###################################################################### ###################################################################### ## 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 "SQL Query FAILED:\n"; print "$query\n\n"; print Dumper $data if defined $data; die; } 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 pmgr_unit_types (`id`, `code`, `name`) VALUES ($row->{'TypeID'}, 'xxx', '$row->{'UnitType'}')"; query($db_handle, $query, $row); } ###################################################################### ## 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 pmgr_unit_sizes (`id`, `unit_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, $row); } ###################################################################### ## 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 pmgr_units (`id`, `unit_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, $row); } ###################################################################### ## 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 pmgr_maps (id, name, site_area_id, width, depth) VALUES\n" . " (1, 'Main Facility Map', $aicur_area_id," . ($info{'extents'}{'right'} - $info{'extents'}{'left'}) . ", " . ($info{'extents'}{'bottom'} - $info{'extents'}{'top'}) . ")"; query($db_handle, $query, \%info); # 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 pmgr_maps_units (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, $row); } ###################################################################### ###################################################################### ###################################################################### ###################################################################### ###################################################################### ###################################################################### ## ## 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'}} = { 'cid' => ++$aicur_c_id }; my $cid = $tenant_map{$row->{'TenantID'}}{'cid'}; $tenant_map{$row->{'TenantID'}}{'custid'} = $cid; my $custid = $tenant_map{$row->{'TenantID'}}{'custid'}; $tenant_map{$row->{'TenantID'}}{'name'} = "$row->{'LastName'}, $row->{'FirstName'}"; $query = "INSERT INTO pmgr_contacts (`id`, `first_name`, `middle_name`, `last_name`, `display_name`, `id_local`, `id_local_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, $row); $query = "INSERT INTO pmgr_customers (`id`, `name`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($custid, "$row->{'LastName'}, $row->{'FirstName'}") )) . ")"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_contacts_customers (`customer_id`, `contact_id`, `type`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($custid, $cid, 'TENANT') )) . ")"; query($db_handle, $query, $row); if ($row->{'City'}) { $query = "INSERT INTO pmgr_contact_addresses (`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, $row); $query = "INSERT INTO pmgr_contacts_methods (`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES ($cid, 'POST', $aicur_ca_id, 'HOME', 'PRIMARY')"; query($db_handle, $query, $row); } 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 pmgr_contact_phones (`id`, `type`, `phone`, `ext`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} (++$aicur_cp_id, $_->{'type'}, $_->{'phone'}, $_->{'ext'}))) . ")"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_contacts_methods (`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($cid, 'PHONE', $aicur_cp_id, 'MAIN', $_->{'preference'}))) . ")"; query($db_handle, $query, $row); } } if ($row->{'Email'}) { $query = "INSERT INTO pmgr_contact_emails (`id`, `email`) VALUES (".++$aicur_ce_id.", '$row->{'Email'}')"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_contacts_methods (`contact_id`, `method`, `method_id`, `type`, `preference`) VALUE ($cid, 'EMAIL', $aicur_ce_id, 'MAIN', 'PRIMARY')"; query($db_handle, $query, $row); } 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 pmgr_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'} || undef))) )) . ")"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_contacts_customers (`customer_id`, `contact_id`, `type`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($custid, $cid, 'ALTERNATE') )) . ")"; query($db_handle, $query, $row); if ($row->{'AltCity'}) { $query = "INSERT INTO pmgr_contact_addresses (`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, $row); $query = "INSERT INTO pmgr_contacts_methods (`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES ($cid, 'POST', $aicur_ca_id, 'MAIN', 'PRIMARY')"; query($db_handle, $query, $row); } if ($row->{'AltPhone'}) { $query = "INSERT INTO pmgr_contact_phones (`id`, `type`, `phone`, `ext`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} (++$aicur_cp_id, 'LANDLINE', $row->{'AltPhone'}, undef) )) . ")"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_contacts_methods (`contact_id`, `method`, `method_id`, `type`, `preference`) VALUES ($cid, 'PHONE', $aicur_cp_id, 'MAIN', 'PRIMARY')"; query($db_handle, $query, $row); } } ###################################################################### ## 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'}} = { 'lid' => ++$aicur_lease_id }; my $cid = $tenant_map{$row->{'TenantID'}}{'cid'}; my $uid = $unit_map{$row->{'UnitID'}}; my $lid = $ledger_map{$row->{'LedgerID'}}{'lid'}; my $custid = $tenant_map{$row->{'TenantID'}}{'custid'}; $ledger_map{$row->{'LedgerID'}}{'custid'} = $custid; $query = "INSERT INTO pmgr_leases (`id`, `number`, `lease_type_id`, `unit_id`, `customer_id`, `lease_date`, `movein_date`, `moveout_date`, `close_date`, `amount`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($lid, $row->{'LedgerID'}, $aicur_lease_type_id, $uid, $custid, datefmt($row->{'DateIn'}), datefmt($row->{'DateIn'}), datefmt($row->{'DateOut'}), datefmt($row->{'DateClosed'}), $row->{'Rent'}) )) . ")"; query($db_handle, $query, $row); } $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); ###################################################################### ## 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 pmgr_leases SET deposit = $row->{'ChargeAmount'} WHERE `number` = '$row->{'LedgerID'}'"; query($db_handle, $query, $row); } ###################################################################### ###################################################################### ###################################################################### ###################################################################### ###################################################################### ###################################################################### ## ## TRANSACTIONS ## ###################################################################### ## Charges $query = "SELECT * FROM Charges ORDER BY ChargeID"; $result = query($sdbh, $query); $nrows = @$result; print(" $nrows Charges\n"); my %charge_map; foreach $row (@$result) { $charge_map{$row->{'ChargeID'}} = ++$aicur_tx_id; my $lid = $ledger_map{$row->{'LedgerID'}}{'lid'}; my $chid = $charge_map{$row->{'ChargeID'}}; my $custid = $ledger_map{$row->{'LedgerID'}}{'custid'}; $query = "INSERT INTO pmgr_transactions (`id`, `stamp`, `through_date`, `customer_id`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($chid, datefmt($row->{'ChargeDate'}), datefmt($row->{'EndDate'}), $custid) )) . ")"; query($db_handle, $query, $row); $query = "INSERT INTO pmgr_ledger_entries (`monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_internal, $chid, $account_map{'A/R'}{'ledger'}, $account_map{$row->{'ChargeDescription'}}{'ledger'}, $row->{'ChargeAmount'}, "Charge: $row->{'ChargeID'}; Ledger: $row->{'LedgerID'}") )) . ")"; query($db_handle, $query, $row); next unless $row->{'TaxAmount'}; $query = "INSERT INTO pmgr_ledger_entries (`monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_internal, $chid, $account_map{'A/R'}{'ledger'}, $account_map{'Tax'}{'ledger'}, $row->{'TaxAmount'}, undef) )) . ")"; query($db_handle, $query, $row); # $query = "UPDATE pmgr_charges"; # $query .= " SET due_date = DATE_ADD(charge_date, INTERVAL IF(account_id=2,10,0) DAY)"; # query($db_handle, $query); } ###################################################################### ## Receipts #$query = "SELECT * FROM Receipts ORDER BY ReceiptNum"; $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"; $result = query($sdbh, $query); $nrows = @$result; print(" $nrows Receipts\n"); my %receipt_map; foreach $row (@$result) { #print Dumper $row; my $lid = $ledger_map{$row->{'LedgerID'}}{'lid'}; my $custid = $ledger_map{$row->{'LedgerID'}}{'custid'}; if ($receipt_map{$row->{'ReceiptNum'}}) { die unless $receipt_map{$row->{'ReceiptNum'}}{'cust'} == $custid; push(@{$receipt_map{$row->{'ReceiptNum'}}{'lid'}}, $lid); #print Dumper $receipt_map{$row->{'ReceiptNum'}}; next; } $receipt_map{$row->{'ReceiptNum'}} = { 'id' => ++$aicur_tx_id, 'cust' => $custid, 'lid' => [$lid] }; my $rid = $receipt_map{$row->{'ReceiptNum'}}{'id'}; #print Dumper $receipt_map{$row->{'ReceiptNum'}}; $query = "INSERT INTO pmgr_transactions (`id`, `stamp`, `through_date`, `customer_id`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($rid, datefmt($row->{'ReceiptDate'}), undef, $custid) )) . ")"; query($db_handle, $query, $row); } # sub idkeys { [ sort( {$a <=> $b} keys(%{$_[0]})) ] } # $Data::Dumper::Sortkeys = \&idkeys; # print("Tenant\n"); print Dumper \%tenant_map; # print("Ledger\n"); print Dumper \%ledger_map; # print("Charge\n"); print Dumper \%charge_map; # print("Receipt\n"); print Dumper \%receipt_map; ###################################################################### ## 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'}}{'id'}; my $chid = $charge_map{$row->{'ChargeID'}}; my $monetary_source_id; if ($row->{'PaymentDate'} =~ m%3/25/2009%) { $monetary_source_id = $monetary_source_closing; } elsif ($monetary_type_map{$row->{'PaymentType'}}{'name'} eq 'Cash') { $monetary_source_id = $monetary_source_cash; } else { $monetary_source_id = ++$aicur_monetary_source_id; $query = "INSERT INTO pmgr_monetary_sources (`id`, `monetary_type_id`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_id, $monetary_type_map{$row->{'PaymentType'}}{'id'}, $row->{'RecdFrom'}, "Payment: $row->{'PaymentID'}; Type: $row->{'PaymentType'}; Check: $row->{'CheckNum'}; $row->{'Memo'}") )) . ")"; query($db_handle, $query) || die("Unable to insert"); } $query = "INSERT INTO pmgr_ledger_entries (`monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_id, $rid, $account_map{'Cash'}{'ledger'}, $account_map{'A/R'}{'ledger'}, $row->{'PaymentAmount'}, "Receipt: $row->{'ReceiptNum'}; Payment: $row->{'PaymentID'}; Charge: $row->{'ChargeID'}") )) . ")"; query($db_handle, $query, $row); if ($row->{'Memo'}) { $query = "UPDATE pmgr_transactions SET `comment` = '$row->{'Memo'}'" . " WHERE `id` = $rid"; query($db_handle, $query, $row); } } ###################################################################### ## 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 Fee', 'amount' => 25, 'tax' => 0 }, ], }, ], 'receipt' => [ { 'id' => 2000, 'date' => '2009-05-15', 'entry' => [ { 'id' => 2200, 'type' => 1, 'amount' => 15 }, { 'id' => 2201, 'type' => 2, 'amount' => 10 }, { 'id' => 2202, 'type' => 3, 'amount' => 5 }, ], }, { 'id' => 2001, 'date' => '2009-05-18', 'entry' => [ { 'id' => 2210, 'type' => 5, 'amount' => 30 }, { 'id' => 2211, 'type' => 6, 'amount' => 20 }, ], }, { 'id' => 2002, 'date' => '2009-05-22', 'entry' => [ { 'id' => 2220, 'type' => 1, 'amount' => 10 }, { 'id' => 2221, 'type' => 2, 'amount' => 5 }, { 'id' => 2222, 'type' => 7, 'amount' => 20 }, { 'id' => 2223, 'type' => 8, 'amount' => 15 }, ], }, ], ); my $old_cash_ledger = $account_map{'Cash'}{'ledger'}; my $new_cash_ledger = ++$aicur_ledger_id; $account_map{'Cash'}{'ledger'} = $new_cash_ledger; $query = "INSERT INTO pmgr_ledgers (`id`, `account_id`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($new_cash_ledger, $account_map{'Cash'}{'id'}, 'Fake Cash Ledger', 'Fake ledger for testing') )) . ")"; query($db_handle, $query) || die("Unable to insert"); $query = "SELECT SUM(IF(LedgerEntry.debit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS debits, SUM(IF(LedgerEntry.credit_ledger_id = Ledger.id, LedgerEntry.amount, 0)) AS credits, SUM(IF(Account.type IN ('ASSET', 'EXPENSE'), IF(LedgerEntry.debit_ledger_id = Ledger.id, 1, -1), IF(LedgerEntry.credit_ledger_id = Ledger.id, 1, -1) ) * LedgerEntry.amount) AS balance, COUNT(`LedgerEntry`.`id`) AS entries FROM `pmgr_ledgers` AS `Ledger` LEFT JOIN `pmgr_accounts` AS `Account` ON (`Account`.`id` = `Ledger`.`account_id`) LEFT JOIN `pmgr_ledger_entries` AS `LedgerEntry` ON (((`LedgerEntry`.`debit_ledger_id` = Ledger.id) OR (`LedgerEntry`.`credit_ledger_id` = Ledger.id))) WHERE `Ledger`.`id` = $old_cash_ledger GROUP BY `Ledger`.`id` LIMIT 1"; $result = query($db_handle, $query); my $balance = $result->[0]{'balance'}; $query = "INSERT INTO pmgr_transactions (`id`, `customer_id`, `stamp`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} (++$aicur_tx_id, 0, '2009-05-10') )) . ")"; query($db_handle, $query); $query = "INSERT INTO pmgr_ledger_entries (`monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `name`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($monetary_source_internal, $aicur_tx_id, $new_cash_ledger, $old_cash_ledger, $balance, "Close Out ($old_cash_ledger -> $new_cash_ledger)", "Carrying forward old ledger balance onto new ledger") )) . ")"; query($db_handle, $query); $query = "UPDATE pmgr_ledgers SET `closed` = 1" . " WHERE `id` = $old_cash_ledger"; query($db_handle, $query); # my $monetary_source_cash = ++$aicur_monetary_source_id; # $query = "INSERT INTO pmgr_monetary_sources # (`id`, `monetary_type_id`, `name`, `comment`) VALUES (" . # join(", ", map({defined $_ ? "'$_'" : "NULL"} # ($monetary_source_cash, # $monetary_type_map{'Cash'}{'id'}, # 'Fake Cash Source', # 'Fake source used for testing') # )) . ")"; # query($db_handle, $query) || die("Unable to insert"); foreach my $ir ('invoice', 'receipt') { foreach my $tx (@{$fake{$ir}}) { #print Dumper ${%$tx{'date'}}; #exit; $query = "INSERT INTO pmgr_transactions (`id`, `stamp`, `through_date`, `customer_id`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} (@{$tx}{'id', 'date', 'thru'}, $fake{'custid'}, "Fake $ir") )) . ")"; query($db_handle, $query, $tx); foreach my $e (@{$tx->{'entry'}}) { my $dr = ($ir eq 'invoice') ? $account_map{'A/R'} : $account_map{'Cash'}; my $cr = ($ir eq 'invoice') ? $account_map{$e->{'account'}} : $account_map{'A/R'}; $query = "INSERT INTO pmgr_ledger_entries (`id`, `monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($e->{'id'}, $e->{'type'} || $monetary_source_internal, $tx->{'id'}, $dr->{'ledger'}, $cr->{'ledger'}, $e->{'amount'}, "Fake $ir entry") )) . ")"; query($db_handle, $query, $e); next unless $e->{'tax'}; $query = "INSERT INTO pmgr_ledger_entries (`id`, `monetary_source_id`, `transaction_id`, `debit_ledger_id`, `credit_ledger_id`, `amount`, `comment`) VALUES (" . join(", ", map({defined $_ ? "'$_'" : "NULL"} ($e->{'id'}+1, $monetary_source_internal, $tx->{'id'}, $account_map{'A/R'}{'ledger'}, $account_map{'Tax'}{'ledger'}, $e->{'tax'}, "Fake Tax Charge") )) . ")"; query($db_handle, $query, $e); } } }