diff --git a/scripts/sitelink2pmgr.pl b/scripts/sitelink2pmgr.pl index 3badbf2..9082ce6 100644 --- a/scripts/sitelink2pmgr.pl +++ b/scripts/sitelink2pmgr.pl @@ -33,263 +33,10 @@ my $db_handle = DBI->connect("DBI:mysql:database=$database;host=$hostname", $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"); +my (%newdb) = ( 'schema' => [], + 'tables' => {}, + 'ids' => {}, + 'lookup' => {'state' => { 1=>'AK', 14=>'ID', 48=>'WA' }} ); ###################################################################### @@ -311,6 +58,40 @@ sub Die { 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 @@ -331,9 +112,9 @@ sub query { } if (!$result && !$ignore) { - print "SQL Query FAILED:\n"; - print "$query\n\n"; - print Dumper $data + print STDERR "SQL Query FAILED:\n"; + print STDERR "$sql\n\n"; + print STDERR Dumper $data if defined $data; die; } @@ -341,6 +122,49 @@ sub query { } +###################################################################### +###################################################################### +## 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'}}) { + 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 $_ ? "'$_'" : "NULL"} values(%row))) . ")"; + query($db_handle, $query, $_); + } + } +} + + +###################################################################### +###################################################################### +## helper functions + sub sizeCode { my ($width, $depth) = @_; return "YARD" @@ -358,6 +182,316 @@ sub datefmt { 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 () { + 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'}, + 'name' => $row->{'name'} . ' Ledger', + 'comment' => undef }); + + $newdb{'lookup'}{'account'}{$row->{'name'}} + = {'account' => $row->{'id'}, + 'ledger' => $newdb{'tables'}{'ledgers'}{'autoid'} }; + + if ((!defined $newdb{'tables'}{'accounts'}{'autoid'}) || + $row->{'id'} > $newdb{'tables'}{'accounts'}{'autoid'}) { + $newdb{'tables'}{'accounts'}{'autoid'} = $row->{'id'}; + } +} + +$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', 'id' => $newdb{'lookup'}{'monetary_type'}{'Cash'}{'id'} }; +$newdb{'lookup'}{'payment_type'}{2} += { 'name' => 'Check', 'id' => $newdb{'lookup'}{'monetary_type'}{'Check'}{'id'} }; +$newdb{'lookup'}{'payment_type'}{3} += { 'name' => 'Money Order', 'id' => $newdb{'lookup'}{'monetary_type'}{'Money Order'}{'id'} }; +$newdb{'lookup'}{'payment_type'}{4} += { 'name' => 'ACH', 'id' => $newdb{'lookup'}{'monetary_type'}{'ACH'}{'id'} }; +$newdb{'lookup'}{'payment_type'}{12} += { 'name' => 'Transfer', 'id' => $newdb{'lookup'}{'monetary_type'}{'Other Non-Tillable'}{'id'} }; + + +$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'}; + + + + ###################################################################### ###################################################################### ###################################################################### @@ -372,72 +506,63 @@ sub datefmt { ###################################################################### ## Unit Types +$newdb{'lookup'}{'unit_type'} = {}; + $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); +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"; -$result = query($sdbh, $query); -$nrows = @$result; -print(" $nrows Units\n"); - -my (%unit_size_map); -foreach $row (@$result) { +foreach $row (@{query($sdbh, $query)}) { 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'}; + next if defined $newdb{'lookup'}{'unit_size'}{$sz}; - $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); + 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 -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'}}; +$newdb{'lookup'}{'unit'} = {}; - $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); +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'} }; } @@ -465,12 +590,11 @@ $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); +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,"; @@ -480,22 +604,15 @@ $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); +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'} }); } @@ -513,63 +630,64 @@ foreach $row (@$result) { ###################################################################### ## Tenants +$newdb{'lookup'}{'tenant'} = {}; + $query = "SELECT * FROM TenantInfo WHERE FirstName <> 'POS' ORDER BY TenantID"; -$result = query($sdbh, $query); -$nrows = @$result; -print(" $nrows Tenants\n"); +foreach $row (@{query($sdbh, $query)}) { -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'}; + $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}} + = { 'name' => "$row->{'LastName'}, $row->{'FirstName'}" }; - $tenant_map{$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 }); - $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); + $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'} = + $newdb{'tables'}{'contacts'}{'autoid'}; - $query = "INSERT INTO pmgr_customers - (`id`, `name`) - VALUES (" . - join(", ", map({defined $_ ? "'$_'" : "NULL"} - ($custid, "$row->{'LastName'}, $row->{'FirstName'}") - )) . ")"; - query($db_handle, $query, $row); + # Every customer gets their own ledger + addRow('accounts', + { 'type' => 'ASSET', + 'name' => 'Customer: ' . $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'}, + 'comment' => undef }); + addRow('ledgers', + { 'account_id' => $newdb{'tables'}{'accounts'}{'autoid'}, + 'name' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'name'} . ' Ledger', + 'comment' => 'Ledger for Customer Account #' . $newdb{'tables'}{'accounts'}{'autoid'} }); - $query = "INSERT INTO pmgr_contacts_customers - (`customer_id`, `contact_id`, `type`) - VALUES (" . - join(", ", map({defined $_ ? "'$_'" : "NULL"} - ($custid, $cid, 'TENANT') - )) . ")"; - query($db_handle, $query, $row); + $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger'} = + $newdb{'tables'}{'ledgers'}{'autoid'}; + + addRow('customers', + { 'name' => "$row->{'LastName'}, $row->{'FirstName'}", + 'ledger_id' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'ledger'} }); + + $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'}) { - $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); + 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'}}, @@ -580,90 +698,76 @@ foreach $row (@$result) { {'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); + 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'}) { - $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); + 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'}; - $tenant_map{$row->{'TenantID'}}{'alt'} = ++$aicur_c_id; - $cid = $tenant_map{$row->{'TenantID'}}{'alt'}; + addRow('contacts', + { 'first_name' => $row->{'AltFirstName'} || undef, + 'middle_name' => $row->{'AltMI'} || undef, + 'last_name' => $row->{'AltLastName'} || undef }); - $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); + $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'alt'} = + $newdb{'tables'}{'contacts'}{'autoid'}; - $query = "INSERT INTO pmgr_contacts_customers - (`customer_id`, `contact_id`, `type`) - VALUES (" . - join(", ", map({defined $_ ? "'$_'" : "NULL"} - ($custid, $cid, 'ALTERNATE') - )) . ")"; - query($db_handle, $query, $row); + 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'}) { - $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); + 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'}) { - $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); + 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); } } @@ -671,55 +775,42 @@ foreach $row (@$result) { ###################################################################### ## 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"; -$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; +foreach $row (@{query($sdbh, $query)}) { - $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); + # Every lease gets its own ledger + addRow('accounts', + { 'type' => 'ASSET', + 'name' => 'Lease: #' . $newdb{'tables'}{'leases'}{'autoid'}, + 'comment' => undef }); + addRow('ledgers', + { 'account_id' => $newdb{'tables'}{'accounts'}{'autoid'}, + 'name' => 'Lease #' . ($newdb{'tables'}{'leases'}{'autoid'}+1) . ' Ledger', + 'comment' => 'Ledger for Customer Account #' . $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'id'} }); + + $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'} = + $newdb{'tables'}{'ledgers'}{'autoid'}; + + 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'}, + 'ledger_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'}, + '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'}, + 'cust' => $newdb{'lookup'}{'tenant'}{$row->{'TenantID'}}{'cust'}, + 'ledger' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'} }; } -$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); -} ###################################################################### @@ -735,72 +826,44 @@ foreach $row (@$result) { ###################################################################### ## Charges +$newdb{'lookup'}{'charge'} = {}; + $query = "SELECT * FROM Charges ORDER BY ChargeID"; -$result = query($sdbh, $query); -$nrows = @$result; -print(" $nrows Charges\n"); +foreach $row (@{query($sdbh, $query)}) { -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'}; - + addRow('transactions', + { 'stamp' => datefmt($row->{'ChargeDate'}), + 'through_date' => datefmt($row->{'EndDate'}), + 'customer_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'} }); - $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); + $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}} + = {'tx' => $newdb{'tables'}{'transactions'}{'autoid'} }; - $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); + 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'}}{'ledger'}, + 'credit_ledger_id' => $newdb{'lookup'}{'charge_type'}{$row->{'ChargeDescription'}}{'ledger'}, + 'amount' => $row->{'ChargeAmount'}, + 'comment' => "Charge: $row->{'ChargeID'}; Ledger: $row->{'LedgerID'}" }); 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); + 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'}}{'ledger'}, + 'credit_ledger_id' => $newdb{'lookup'}{'charge_type'}{'Tax'}{'ledger'}, + 'amount' => $row->{'TaxAmount'}, + 'comment' => undef }); } ###################################################################### ## Receipts -#$query = "SELECT * FROM Receipts ORDER BY ReceiptNum"; +$newdb{'lookup'}{'receipt'} = {}; + $query = "SELECT R.ReceiptNum, C.LedgerID, R.ReceiptDate" . " FROM Receipts R, Payments P, Charges C" . @@ -808,38 +871,26 @@ $query = " 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"); +foreach $row (@{query($sdbh, $query)}) { -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); + 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; } - $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'}}; + addRow('transactions', + { 'stamp' => datefmt($row->{'ReceiptDate'}), + 'through_date' => undef, + 'customer_id' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'} }); - $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); + $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}} + = {'tx' => $newdb{'tables'}{'transactions'}{'autoid'}, + 'cust' => $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'cust'}, + 'ledgers' => [ $newdb{'lookup'}{'ledger'}{$row->{'LedgerID'}}{'ledger'} ] }; } @@ -854,60 +905,47 @@ foreach $row (@$result) { ###################################################################### ## Payments +$newdb{'lookup'}{'payment'} = {}; + $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'}}; - +foreach $row (@{query($sdbh, $query)}) +{ 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; + $monetary_source_id = $newdb{'ids'}{'monetary_source'}{'closing'}; } 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"); + $monetary_source_id = $newdb{'ids'}{'monetary_source'}{ + $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'name'} + }; } - $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 (!defined $monetary_source_id) { + addRow('monetary_sources', + { 'monetary_type_id' => $newdb{'lookup'}{'payment_type'}{$row->{'PaymentType'}}{'id'}, + 'name' => $row->{'RecdFrom'}, + 'comment' => "Payment: $row->{'PaymentID'}; Type: $row->{'PaymentType'}; Check: $row->{'CheckNum'}; $row->{'Memo'}" }); + $monetary_source_id = $newdb{'tables'}{'monetary_sources'}{'autoid'}; + } + addRow('ledger_entries', + { 'monetary_source_id' => $monetary_source_id, + 'transaction_id' => $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'}, + 'debit_ledger_id' => $newdb{'lookup'}{'account'}{'Cash'}{'ledger'}, + 'credit_ledger_id' => $newdb{'lookup'}{'account'}{'A/R'}{'ledger'}, + 'amount' => $row->{'PaymentAmount'}, + 'comment' => "Receipt: $row->{'ReceiptNum'}; Payment: $row->{'PaymentID'}; Charge: $row->{'ChargeID'}" }); + + + # Map this to the Charge ?? + # $newdb{'lookup'}{'charge'}{$row->{'ChargeID'}}{'id'} + # $newdb{'lookup'}{'payment'}{$row->{'PaymentID'}}{'id'} + + + # Update the transaction to use the memo from this payment if ($row->{'Memo'}) { - $query = - "UPDATE pmgr_transactions SET `comment` = '$row->{'Memo'}'" . - " WHERE `id` = $rid"; - query($db_handle, $query, $row); + my $txid = $newdb{'lookup'}{'receipt'}{$row->{'ReceiptNum'}}{'tx'}; + $newdb{'tables'}{'transactions'}{'rows'}[$txid]{'comment'} = $row->{'Memo'}; } } @@ -937,7 +975,7 @@ my %fake = { 'id' => 1002, 'date' => '2009-05-11', 'entry' => [ { 'id' => 2120, - 'account' => 'Late Fee', + 'account' => 'Late Charge', 'amount' => 25, 'tax' => 0 }, ], @@ -986,131 +1024,117 @@ my %fake = ); -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'}; +$newdb{'ids'}{'ledger'}{'Cash-Old'} = + $newdb{'lookup'}{'account'}{'Cash'}{'ledger'}; -$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); +addRow('ledgers', + { 'account_id' => $newdb{'lookup'}{'account'}{'Cash'}{'account'}, + 'name' => 'Fake Cash Ledger', + 'comment' => 'Fake ledger for testing' }); -$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); +$newdb{'lookup'}{'account'}{'Cash'}{'ledger'} = + $newdb{'tables'}{'ledgers'}{'autoid'}; -$query = - "UPDATE pmgr_ledgers SET `closed` = 1" . - " WHERE `id` = $old_cash_ledger"; -query($db_handle, $query); +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', + { 'customer_id' => 0, + '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" }); + +$newdb{'tables'}{'ledgers'}{'rows'}[$newdb{'ids'}{'ledger'}{'Cash-Old'}]{'closed'} => 1; -# 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); + addRow('transactions', + { 'id' => $tx->{'id'}, + 'stamp' => $tx->{'date'}, + 'through_date' => $tx->{'thru'}, + 'customer_id' => $fake{'custid'}, + 'comment' => "Fake $ir" }, + 1); 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'}; + my $dr = ($ir eq 'invoice') ? 'A/R' : 'Cash'; + my $cr = ($ir eq 'invoice') ? $e->{'account'} : '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); + addRow('ledger_entries', + { 'id' => $e->{'id'}, + 'monetary_source_id' => $e->{'type'} || $newdb{'ids'}{'monetary_source'}{'internal'}, + '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); 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); + addRow('ledger_entries', + { 'id' => $e->{'id'}+1, + 'monetary_source_id' => $newdb{'ids'}{'monetary_source'}{'internal'}, + '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); +