1 package SL::DB::Helper::Mappings;
7 our @ISA = qw(Exporter);
8 our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
10 # these will not be managed as Rose::DB models, because they are not normalized,
11 # significant changes are needed to get them done, or they were done by CRM.
12 my @lxoffice_blacklist_permanent = qw(
16 # these are not managed _yet_, but will hopefully at some point.
17 # if you are confident that one of these works, remove it here.
18 my @lxoffice_blacklist_temp = qw(
21 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp);
23 # map table names to their models.
24 # unlike rails we have no singular<->plural magic.
25 # remeber: tables should be named as the plural of the model name.
26 my %lxoffice_package_names = (
27 acc_trans => 'acc_transaction',
28 audittrail => 'audit_trail',
29 auth_group => 'auth_groups',
30 auth_group_right => 'auth_group_rights',
31 auth_user => 'auth_users',
32 auth_user_config => 'auth_user_configs',
33 auth_user_group => 'auth_user_groups',
35 ap => 'purchase_invoice',
36 background_jobs => 'background_job',
37 background_job_histories => 'background_job_history',
38 ap => 'purchase_invoice',
39 bank_accounts => 'bank_account',
40 buchungsgruppen => 'buchungsgruppe',
41 contacts => 'contact',
42 csv_import_profiles => 'csv_import_profile',
43 csv_import_profile_settings => 'csv_import_profile_setting',
44 custom_variable_configs => 'custom_variable_config',
45 custom_variables => 'custom_variable',
46 custom_variables_validity => 'custom_variable_validity',
47 customertax => 'customer_tax',
49 defaults => 'default',
50 delivery_orders => 'delivery_order',
51 delivery_order_items => 'delivery_order_item',
52 department => 'department',
53 dpt_trans => 'dpt_trans',
56 dunning_config => 'dunning_config',
57 employee => 'employee',
58 exchangerate => 'exchangerate',
59 finanzamt => 'finanzamt',
60 follow_up_access => 'follow_up_access',
61 follow_up_links => 'follow_up_link',
62 follow_ups => 'follow_up',
63 generic_translations => 'generic_translation',
65 gl => 'GLTransaction',
66 history_erp => 'history',
67 inventory => 'inventory',
68 invoice => 'invoice_item',
69 language => 'language',
70 makemodel => 'make_model',
72 orderitems => 'order_item',
75 partsgroup => 'parts_group',
76 partstax => 'parts_tax',
77 payment_terms => 'payment_term',
78 periodic_invoices => 'periodic_invoice',
79 periodic_invoices_configs => 'periodic_invoices_config',
81 price_factors => 'price_factor',
82 pricegroup => 'pricegroup',
83 printers => 'Printer',
84 record_links => 'record_link',
86 rmaitems => 'RMA_item',
87 sepa_export => 'sepa_export',
88 sepa_export_items => 'sepa_export_item',
89 schema_info => 'schema_info',
93 tax_zones => 'tax_zone',
94 todo_user_config => 'todo_user_config',
95 translation => 'translation',
96 translation_payment_terms => 'translation_payment_term',
98 units_language => 'units_language',
99 vendortax => 'vendor_tax',
102 my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables);
105 return LXOFFICE => \@lxoffice_blacklist;
108 sub get_package_names {
109 return LXOFFICE => \%lxoffice_package_names;
112 sub get_package_for_table {
113 %lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
114 unless %lxoffice_tables_to_packages;
116 return $lxoffice_tables_to_packages{ $_[0] };
119 sub get_manager_package_for_table {
120 %lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
121 unless %lxoffice_tables_to_manager_packages;
123 return $lxoffice_tables_to_manager_packages{ $_[0] };
126 sub get_table_for_package {
127 get_package_for_table('dummy') if !%lxoffice_tables_to_packages;
128 %lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables;
130 my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
131 return $lxoffice_packages_to_tables{ $package };
136 my $lookup = $lxoffice_package_names{$_[0]} ||
137 plurify($lxoffice_package_names{singlify($_[0])});
139 for my $thing ($string, $lookup) {
141 # best guess? its already the name. like part. camelize it first
142 my $class = "SL::DB::" . camelify($thing);
143 return $class if defined *{ $class. '::' };
145 # next, someone wants a manager and pluralized.
146 my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
147 return $manager if defined *{ $manager . '::' };
150 die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
155 $str =~ s/_+(.)/uc($1)/ge;
161 $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
185 SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
189 use SL::DB::Helper::Mappings qw(@blacklist %table2model);
193 This modul stores table <-> model mappings used by the
194 L<scripts/rose_auto_create_model.pl> script. If you add a new table that has
195 custom mappings, add it here.
203 A special function provided here is C<db>. Without it you'd have to write:
205 my $part = SL::DB::Part->new(id => 1234);
206 my @all_parts = SL::DB::Manager::Part->get_all;
208 with them it becomes:
210 my $part = db('part')->new(id => 123);
211 my @all_parts = db('parts')->get_all;
213 You don't have to care about add that SL::DB:: incantation anymore. Also, a
214 simple s at the end will get you the associated Manager class.
216 db is written to try to make sense of what you give it, but if all fails, it
217 will die with an error.
219 =item C<get_package_for_table $table_name>
221 Returns the package name for a table name:
223 SL::DB::Helper::Mappings::get_package_for_table('oe')
226 =item C<get_manager_package_for_table $table_name>
228 Returns the manager package name for a table name:
230 SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
231 # SL::DB::Manager::Order
233 =item C<get_table_for_package $package_name>
235 Returns the table name for a package name:
237 SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
239 SL::DB::Helper::Mappings::get_table_for_package('Order')
250 L<scripts/rose_auto_create_model.pl>
254 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
255 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>