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