-package SL::DB::Helpers::Mappings;
+package SL::DB::Helper::Mappings;
use utf8;
use strict;
+use SL::Util qw(camelify);
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
+
# these will not be managed as Rose::DB models, because they are not normalized,
# significant changes are needed to get them done, or they were done by CRM.
-my @lxoffice_blacklist_permanent = qw(
+my @kivitendo_blacklist_permanent = qw(
leads
);
# these are not managed _yet_, but will hopefully at some point.
# if you are confident that one of these works, remove it here.
-my @lxoffice_blacklist_temp = qw(
+my @kivitendo_blacklist_temp = qw(
);
-my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp);
+# tables created by crm module
+my @crm_blacklist = qw(
+);
+
+# tables created by cash register
+my @cash_register_blacklist = qw(
+ekartikel ekbon ekkunde ektext erptasten
+);
+
+my @kivitendo_blacklist = (@kivitendo_blacklist_permanent, @kivitendo_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
# map table names to their models.
# unlike rails we have no singular<->plural magic.
# remeber: tables should be named as the plural of the model name.
-my %lxoffice_package_names = (
+my %kivitendo_package_names = (
+ # TABLE # MODEL (given in C style)
acc_trans => 'acc_transaction',
audittrail => 'audit_trail',
+ 'auth.clients' => 'auth_client',
+ 'auth.clients_users' => 'auth_client_user',
+ 'auth.clients_groups' => 'auth_client_group',
+ 'auth.group' => 'auth_group',
+ 'auth.group_rights' => 'auth_group_right',
+ 'auth.user' => 'auth_user',
+ 'auth.user_config' => 'auth_user_config',
+ 'auth.user_group' => 'auth_user_group',
ar => 'invoice',
ap => 'purchase_invoice',
background_jobs => 'background_job',
bank_accounts => 'bank_account',
buchungsgruppen => 'buchungsgruppe',
contacts => 'contact',
+ csv_import_profiles => 'csv_import_profile',
+ csv_import_profile_settings => 'csv_import_profile_setting',
+ csv_import_reports => 'csv_import_report',
+ csv_import_report_rows => 'csv_import_report_row',
+ csv_import_report_status => 'csv_import_report_status',
+ currencies => 'currency',
custom_variable_configs => 'custom_variable_config',
custom_variables => 'custom_variable',
custom_variables_validity => 'custom_variable_validity',
- customertax => 'customer_tax',
datev => 'datev',
defaults => 'default',
delivery_orders => 'delivery_order',
follow_up_links => 'follow_up_link',
follow_ups => 'follow_up',
generic_translations => 'generic_translation',
- gifi => 'gifi',
gl => 'GLTransaction',
history_erp => 'history',
inventory => 'inventory',
invoice => 'invoice_item',
language => 'language',
- license => 'license',
- licenseinvoice => 'license_invoice',
makemodel => 'make_model',
notes => 'note',
orderitems => 'order_item',
oe => 'order',
parts => 'part',
partsgroup => 'parts_group',
- partstax => 'parts_tax',
payment_terms => 'payment_term',
- prices => 'prices',
+ periodic_invoices => 'periodic_invoice',
+ periodic_invoices_configs => 'periodic_invoices_config',
+ prices => 'price',
price_factors => 'price_factor',
pricegroup => 'pricegroup',
printers => 'Printer',
record_links => 'record_link',
- rma => 'RMA',
- rmaitems => 'RMA_item',
sepa_export => 'sepa_export',
sepa_export_items => 'sepa_export_item',
schema_info => 'schema_info',
translation_payment_terms => 'translation_payment_term',
units => 'unit',
units_language => 'units_language',
- vendortax => 'vendor_tax',
+ vendor => 'vendor',
);
+my (%kivitendo_tables_to_packages, %kivitendo_tables_to_manager_packages, %kivitendo_packages_to_tables);
+
sub get_blacklist {
- return LXOFFICE => \@lxoffice_blacklist;
+ return KIVITENDO => \@kivitendo_blacklist;
}
sub get_package_names {
- return LXOFFICE => \%lxoffice_package_names;
+ return KIVITENDO => \%kivitendo_package_names;
+}
+
+sub get_package_for_table {
+ %kivitendo_tables_to_packages = map { ($_ => "SL::DB::" . camelify($kivitendo_package_names{$_})) } keys %kivitendo_package_names
+ unless %kivitendo_tables_to_packages;
+
+ return $kivitendo_tables_to_packages{ $_[0] };
+}
+
+sub get_manager_package_for_table {
+ %kivitendo_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($kivitendo_package_names{$_})) } keys %kivitendo_package_names
+ unless %kivitendo_tables_to_manager_packages;
+
+ return $kivitendo_tables_to_manager_packages{ $_[0] };
+}
+
+sub get_table_for_package {
+ get_package_for_table('dummy') if !%kivitendo_tables_to_packages;
+ %kivitendo_packages_to_tables = reverse %kivitendo_tables_to_packages unless %kivitendo_packages_to_tables;
+
+ my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
+ return $kivitendo_packages_to_tables{ $package };
}
sub db {
my $string = $_[0];
- my $lookup = $lxoffice_package_names{$_[0]} ||
- plurify($lxoffice_package_names{singlify($_[0])});
+ my $lookup = $kivitendo_package_names{$_[0]} ||
+ plurify($kivitendo_package_names{singlify($_[0])});
for my $thing ($string, $lookup) {
die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
}
-sub camelify {
- my ($str) = @_;
- $str =~ s/_+(.)/uc($1)/ge;
- ucfirst $str;
-}
-
-sub snakify {
- my ($str) = @_;
- $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
- lcfirst $str;
-}
-
sub plurify {
my ($str) = @_;
$str . 's';
__END__
+=encoding utf8
+
=head1 NAME
-SL::DB::Helpers::Mappings - Rose Table <-> Model mapping information
+SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
=head1 SYNOPSIS
- use SL::DB::Helpers::Mappings qw(@blacklist %table2model);
+ use SL::DB::Helper::Mappings qw(@blacklist %table2model);
=head1 DESCRIPTION
L<scripts/rose_auto_create_model.pl> script. If you add a new table that has
custom mappings, add it here.
-=head2 db
+=head1 FUNCTIONS
-A special function provided here is E<db>. Without it you'd have to write:
+=over 4
+
+=item C<db $name>
+
+A special function provided here is C<db>. Without it you'd have to write:
my $part = SL::DB::Part->new(id => 1234);
my @all_parts = SL::DB::Manager::Part->get_all;
db is written to try to make sense of what you give it, but if all fails, it
will die with an error.
+=item C<get_package_for_table $table_name>
+
+Returns the package name for a table name:
+
+ SL::DB::Helper::Mappings::get_package_for_table('oe')
+ # SL::DB::Order
+
+=item C<get_manager_package_for_table $table_name>
+
+Returns the manager package name for a table name:
+
+ SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
+ # SL::DB::Manager::Order
+
+=item C<get_table_for_package $package_name>
+
+Returns the table name for a package name:
+
+ SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
+ # oe
+ SL::DB::Helper::Mappings::get_table_for_package('Order')
+ # oe
+
+=back
+
=head1 BUGS
nothing yet
=head1 AUTHOR
-Sven Schöling <s.schoeling@linet-services.de>
+Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
=cut