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