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