Rose-Model für neue Tabelle currencies
[kivitendo-erp.git] / SL / DB / Helper / Mappings.pm
1 package SL::DB::Helper::Mappings;
2
3 use utf8;
4 use strict;
5
6 use SL::Util qw(camelify);
7
8 require Exporter;
9 our @ISA       = qw(Exporter);
10 our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
11
12 # these will not be managed as Rose::DB models, because they are not normalized,
13 # significant changes are needed to get them done, or they were done by CRM.
14 my @lxoffice_blacklist_permanent = qw(
15   leads
16 );
17
18 # these are not managed _yet_, but will hopefully at some point.
19 # if you are confident that one of these works, remove it here.
20 my @lxoffice_blacklist_temp = qw(
21 );
22
23 # tables created by crm module
24 my @crm_blacklist = qw(
25 );
26
27 # tables created by cash register
28 my @cash_register_blacklist = qw(
29 ekartikel ekbon ekkunde ektext erptasten
30 );
31
32 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
33
34 # map table names to their models.
35 # unlike rails we have no singular<->plural magic.
36 # remeber: tables should be named as the plural of the model name.
37 my %lxoffice_package_names = (
38   acc_trans                      => 'acc_transaction',
39   audittrail                     => 'audit_trail',
40   auth_group                     => 'auth_groups',
41   auth_group_right               => 'auth_group_rights',
42   auth_user                      => 'auth_users',
43   auth_user_config               => 'auth_user_configs',
44   auth_user_group                => 'auth_user_groups',
45   ar                             => 'invoice',
46   ap                             => 'purchase_invoice',
47   background_jobs                => 'background_job',
48   background_job_histories       => 'background_job_history',
49   ap                             => 'purchase_invoice',
50   bank_accounts                  => 'bank_account',
51   buchungsgruppen                => 'buchungsgruppe',
52   contacts                       => 'contact',
53   csv_import_profiles            => 'csv_import_profile',
54   csv_import_profile_settings    => 'csv_import_profile_setting',
55   csv_import_reports             => 'csv_import_report',
56   csv_import_report_rows         => 'csv_import_report_row',
57   csv_import_report_status       => 'csv_import_report_status',
58   currencies                     => 'currency',
59   custom_variable_configs        => 'custom_variable_config',
60   custom_variables               => 'custom_variable',
61   custom_variables_validity      => 'custom_variable_validity',
62   datev                          => 'datev',
63   defaults                       => 'default',
64   delivery_orders                => 'delivery_order',
65   delivery_order_items           => 'delivery_order_item',
66   department                     => 'department',
67   dpt_trans                      => 'dpt_trans',
68   drafts                         => 'draft',
69   dunning                        => 'dunning',
70   dunning_config                 => 'dunning_config',
71   employee                       => 'employee',
72   exchangerate                   => 'exchangerate',
73   finanzamt                      => 'finanzamt',
74   follow_up_access               => 'follow_up_access',
75   follow_up_links                => 'follow_up_link',
76   follow_ups                     => 'follow_up',
77   generic_translations           => 'generic_translation',
78   gl                             => 'GLTransaction',
79   history_erp                    => 'history',
80   inventory                      => 'inventory',
81   invoice                        => 'invoice_item',
82   language                       => 'language',
83   makemodel                      => 'make_model',
84   notes                          => 'note',
85   orderitems                     => 'order_item',
86   oe                             => 'order',
87   parts                          => 'part',
88   partsgroup                     => 'parts_group',
89   payment_terms                  => 'payment_term',
90   periodic_invoices              => 'periodic_invoice',
91   periodic_invoices_configs      => 'periodic_invoices_config',
92   prices                         => 'price',
93   price_factors                  => 'price_factor',
94   pricegroup                     => 'pricegroup',
95   printers                       => 'Printer',
96   record_links                   => 'record_link',
97   sepa_export                    => 'sepa_export',
98   sepa_export_items              => 'sepa_export_item',
99   schema_info                    => 'schema_info',
100   status                         => 'status',
101   tax                            => 'tax',
102   taxkeys                        => 'tax_key',
103   tax_zones                      => 'tax_zone',
104   todo_user_config               => 'todo_user_config',
105   translation                    => 'translation',
106   translation_payment_terms      => 'translation_payment_term',
107   units                          => 'unit',
108   units_language                 => 'units_language',
109   vendor                         => 'vendor',
110 );
111
112 my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables);
113
114 sub get_blacklist {
115   return LXOFFICE => \@lxoffice_blacklist;
116 }
117
118 sub get_package_names {
119   return LXOFFICE => \%lxoffice_package_names;
120 }
121
122 sub get_package_for_table {
123   %lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
124     unless %lxoffice_tables_to_packages;
125
126   return $lxoffice_tables_to_packages{ $_[0] };
127 }
128
129 sub get_manager_package_for_table {
130   %lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
131     unless %lxoffice_tables_to_manager_packages;
132
133   return $lxoffice_tables_to_manager_packages{ $_[0] };
134 }
135
136 sub get_table_for_package {
137   get_package_for_table('dummy') if !%lxoffice_tables_to_packages;
138   %lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables;
139
140   my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
141   return $lxoffice_packages_to_tables{ $package };
142 }
143
144 sub db {
145   my $string = $_[0];
146   my $lookup = $lxoffice_package_names{$_[0]} ||
147       plurify($lxoffice_package_names{singlify($_[0])});
148
149   for my $thing ($string, $lookup) {
150
151     # best guess? its already the name. like part. camelize it first
152     my $class = "SL::DB::" . camelify($thing);
153     return $class if defined *{ $class. '::' };
154
155     # next, someone wants a manager and pluralized.
156     my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
157     return $manager if defined *{ $manager . '::' };
158   }
159
160   die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
161 }
162
163 sub plurify {
164   my ($str) = @_;
165   $str . 's';
166 }
167
168 sub singlify {
169   my ($str) = @_;
170   local $/ = 's';
171   chomp $str;
172   $str;
173 }
174
175 1;
176
177 __END__
178
179 =encoding utf8
180
181 =head1 NAME
182
183 SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
184
185 =head1 SYNOPSIS
186
187   use SL::DB::Helper::Mappings qw(@blacklist %table2model);
188
189 =head1 DESCRIPTION
190
191 This modul stores table <-> model mappings used by the
192 L<scripts/rose_auto_create_model.pl> script.  If you add a new table that has
193 custom mappings, add it here.
194
195 =head1 FUNCTIONS
196
197 =over 4
198
199 =item C<db $name>
200
201 A special function provided here is C<db>. Without it you'd have to write:
202
203   my $part = SL::DB::Part->new(id => 1234);
204   my @all_parts = SL::DB::Manager::Part->get_all;
205
206 with them it becomes:
207
208   my $part = db('part')->new(id => 123);
209   my @all_parts = db('parts')->get_all;
210
211 You don't have to care about add that SL::DB:: incantation anymore. Also, a
212 simple s at the end will get you the associated Manager class.
213
214 db is written to try to make sense of what you give it, but if all fails, it
215 will die with an error.
216
217 =item C<get_package_for_table $table_name>
218
219 Returns the package name for a table name:
220
221   SL::DB::Helper::Mappings::get_package_for_table('oe')
222   # SL::DB::Order
223
224 =item C<get_manager_package_for_table $table_name>
225
226 Returns the manager package name for a table name:
227
228   SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
229   # SL::DB::Manager::Order
230
231 =item C<get_table_for_package $package_name>
232
233 Returns the table name for a package name:
234
235   SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
236   # oe
237   SL::DB::Helper::Mappings::get_table_for_package('Order')
238   # oe
239
240 =back
241
242 =head1 BUGS
243
244 nothing yet
245
246 =head1 SEE ALSO
247
248 L<scripts/rose_auto_create_model.pl>
249
250 =head1 AUTHOR
251
252 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
253 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
254
255 =cut