Projekt

Allgemein

Profil

Herunterladen (8,47 KB) Statistiken
| Zweig: | Markierung: | Revision:
ea65e003 Moritz Bunkus
package SL::DB::Helper::Mappings;
82515b2d Sven Schöling
05c6840d Moritz Bunkus
use utf8;
82515b2d Sven Schöling
use strict;

a93b3b64 Moritz Bunkus
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);

5991c23e Sven Schöling
# 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.
82515b2d Sven Schöling
my @lxoffice_blacklist_permanent = qw(
5991c23e Sven Schöling
leads
f97b0778 Sven Schöling
);
82515b2d Sven Schöling
# 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(
);

2438dd57 Sven Schöling
# 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 @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
82515b2d Sven Schöling
# 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 = (
f97b0778 Sven Schöling
acc_trans => 'acc_transaction',
audittrail => 'audit_trail',
a5ba22d2 Moritz Bunkus
auth_group => 'auth_groups',
auth_group_right => 'auth_group_rights',
auth_user => 'auth_users',
auth_user_config => 'auth_user_configs',
auth_user_group => 'auth_user_groups',
82515b2d Sven Schöling
ar => 'invoice',
ap => 'purchase_invoice',
295e585a Moritz Bunkus
background_jobs => 'background_job',
background_job_histories => 'background_job_history',
ap => 'purchase_invoice',
4fd22b56 Sven Schöling
bank_accounts => 'bank_account',
buchungsgruppen => 'buchungsgruppe',
contacts => 'contact',
c2cf302a Moritz Bunkus
csv_import_profiles => 'csv_import_profile',
csv_import_profile_settings => 'csv_import_profile_setting',
67643d03 Sven Schöling
csv_import_reports => 'csv_import_report',
csv_import_report_rows => 'csv_import_report_row',
a5a42ed1 Sven Schöling
csv_import_report_status => 'csv_import_report_status',
4fd22b56 Sven Schöling
custom_variable_configs => 'custom_variable_config',
custom_variables => 'custom_variable',
custom_variables_validity => 'custom_variable_validity',
f97b0778 Sven Schöling
datev => 'datev',
defaults => 'default',
82515b2d Sven Schöling
delivery_orders => 'delivery_order',
delivery_order_items => 'delivery_order_item',
f97b0778 Sven Schöling
department => 'department',
dpt_trans => 'dpt_trans',
4fd22b56 Sven Schöling
drafts => 'draft',
dunning => 'dunning',
dunning_config => 'dunning_config',
employee => 'employee',
f97b0778 Sven Schöling
exchangerate => 'exchangerate',
finanzamt => 'finanzamt',
follow_up_access => 'follow_up_access',
4fd22b56 Sven Schöling
follow_up_links => 'follow_up_link',
follow_ups => 'follow_up',
generic_translations => 'generic_translation',
f97b0778 Sven Schöling
gifi => 'gifi',
82515b2d Sven Schöling
gl => 'GLTransaction',
4fd22b56 Sven Schöling
history_erp => 'history',
f97b0778 Sven Schöling
inventory => 'inventory',
82515b2d Sven Schöling
invoice => 'invoice_item',
4fd22b56 Sven Schöling
language => 'language',
f97b0778 Sven Schöling
makemodel => 'make_model',
4fd22b56 Sven Schöling
notes => 'note',
82515b2d Sven Schöling
orderitems => 'order_item',
oe => 'order',
parts => 'part',
f97b0778 Sven Schöling
partsgroup => 'parts_group',
4fd22b56 Sven Schöling
payment_terms => 'payment_term',
250fa402 Moritz Bunkus
periodic_invoices => 'periodic_invoice',
periodic_invoices_configs => 'periodic_invoices_config',
7ade1b95 Moritz Bunkus
prices => 'price',
82515b2d Sven Schöling
price_factors => 'price_factor',
4fd22b56 Sven Schöling
pricegroup => 'pricegroup',
82515b2d Sven Schöling
printers => 'Printer',
f97b0778 Sven Schöling
record_links => 'record_link',
4fd22b56 Sven Schöling
rma => 'RMA',
f97b0778 Sven Schöling
rmaitems => 'RMA_item',
4fd22b56 Sven Schöling
sepa_export => 'sepa_export',
sepa_export_items => 'sepa_export_item',
85da554f Sven Schöling
schema_info => 'schema_info',
f97b0778 Sven Schöling
status => 'status',
4fd22b56 Sven Schöling
tax => 'tax',
f97b0778 Sven Schöling
taxkeys => 'tax_key',
tax_zones => 'tax_zone',
todo_user_config => 'todo_user_config',
translation => 'translation',
translation_payment_terms => 'translation_payment_term',
82515b2d Sven Schöling
units => 'unit',
f97b0778 Sven Schöling
units_language => 'units_language',
5198e9c4 Sven Schöling
vendor => 'vendor',
82515b2d Sven Schöling
);

a93b3b64 Moritz Bunkus
my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables);

82515b2d Sven Schöling
sub get_blacklist {
return LXOFFICE => \@lxoffice_blacklist;
}

sub get_package_names {
return LXOFFICE => \%lxoffice_package_names;
}

a93b3b64 Moritz Bunkus
sub get_package_for_table {
%lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
unless %lxoffice_tables_to_packages;

return $lxoffice_tables_to_packages{ $_[0] };
}

sub get_manager_package_for_table {
%lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
unless %lxoffice_tables_to_manager_packages;

return $lxoffice_tables_to_manager_packages{ $_[0] };
}

sub get_table_for_package {
get_package_for_table('dummy') if !%lxoffice_tables_to_packages;
%lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables;

my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
return $lxoffice_packages_to_tables{ $package };
}

3cc8ad87 Sven Schöling
sub db {
my $string = $_[0];
my $lookup = $lxoffice_package_names{$_[0]} ||
plurify($lxoffice_package_names{singlify($_[0])});

for my $thing ($string, $lookup) {

# best guess? its already the name. like part. camelize it first
my $class = "SL::DB::" . camelify($thing);
return $class if defined *{ $class. '::' };

# next, someone wants a manager and pluralized.
my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
return $manager if defined *{ $manager . '::' };
}

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';
}

sub singlify {
my ($str) = @_;
local $/ = 's';
chomp $str;
$str;
}

82515b2d Sven Schöling
1;

__END__

a93b3b64 Moritz Bunkus
=encoding utf8

82515b2d Sven Schöling
=head1 NAME

ea65e003 Moritz Bunkus
SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
82515b2d Sven Schöling
=head1 SYNOPSIS

ea65e003 Moritz Bunkus
use SL::DB::Helper::Mappings qw(@blacklist %table2model);
82515b2d Sven Schöling
=head1 DESCRIPTION

This modul stores table <-> model mappings used by the
L<scripts/rose_auto_create_model.pl> script. If you add a new table that has
custom mappings, add it here.

a93b3b64 Moritz Bunkus
=head1 FUNCTIONS
3cc8ad87 Sven Schöling
a93b3b64 Moritz Bunkus
=over 4

=item C<db $name>

A special function provided here is C<db>. Without it you'd have to write:
3cc8ad87 Sven Schöling
my $part = SL::DB::Part->new(id => 1234);
my @all_parts = SL::DB::Manager::Part->get_all;

with them it becomes:

my $part = db('part')->new(id => 123);
my @all_parts = db('parts')->get_all;

You don't have to care about add that SL::DB:: incantation anymore. Also, a
simple s at the end will get you the associated Manager class.

db is written to try to make sense of what you give it, but if all fails, it
will die with an error.

a93b3b64 Moritz Bunkus
=item C<get_package_for_table $table_name>

Returns the package name for a table name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_package_for_table('oe')
a93b3b64 Moritz Bunkus
# SL::DB::Order

=item C<get_manager_package_for_table $table_name>

Returns the manager package name for a table name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
a93b3b64 Moritz Bunkus
# SL::DB::Manager::Order

=item C<get_table_for_package $package_name>

Returns the table name for a package name:

0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
a93b3b64 Moritz Bunkus
# oe
0bb70855 Moritz Bunkus
SL::DB::Helper::Mappings::get_table_for_package('Order')
a93b3b64 Moritz Bunkus
# oe

=back

82515b2d Sven Schöling
=head1 BUGS

nothing yet

=head1 SEE ALSO

L<scripts/rose_auto_create_model.pl>

=head1 AUTHOR

a93b3b64 Moritz Bunkus
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
82515b2d Sven Schöling
=cut