Vendor auch in mappings listen.
[kivitendo-erp.git] / SL / DB / Helper / Mappings.pm
1 package SL::DB::Helper::Mappings;
2
3 use utf8;
4 use strict;
5
6 require Exporter;
7 our @ISA       = qw(Exporter);
8 our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
9
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(
13   leads
14 );
15
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(
19 );
20
21 # tables created by crm module
22 my @crm_blacklist = qw(
23 );
24
25 # tables created by cash register
26 my @cash_register_blacklist = qw(
27 ekartikel ekbon ekkunde ektext erptasten
28 );
29
30 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
31
32 # map table names to their models.
33 # unlike rails we have no singular<->plural magic.
34 # remeber: tables should be named as the plural of the model name.
35 my %lxoffice_package_names = (
36   acc_trans                      => 'acc_transaction',
37   audittrail                     => 'audit_trail',
38   auth_group                     => 'auth_groups',
39   auth_group_right               => 'auth_group_rights',
40   auth_user                      => 'auth_users',
41   auth_user_config               => 'auth_user_configs',
42   auth_user_group                => 'auth_user_groups',
43   ar                             => 'invoice',
44   ap                             => 'purchase_invoice',
45   background_jobs                => 'background_job',
46   background_job_histories       => 'background_job_history',
47   ap                             => 'purchase_invoice',
48   bank_accounts                  => 'bank_account',
49   buchungsgruppen                => 'buchungsgruppe',
50   contacts                       => 'contact',
51   csv_import_profiles            => 'csv_import_profile',
52   csv_import_profile_settings    => 'csv_import_profile_setting',
53   custom_variable_configs        => 'custom_variable_config',
54   custom_variables               => 'custom_variable',
55   custom_variables_validity      => 'custom_variable_validity',
56   customertax                    => 'customer_tax',
57   datev                          => 'datev',
58   defaults                       => 'default',
59   delivery_orders                => 'delivery_order',
60   delivery_order_items           => 'delivery_order_item',
61   department                     => 'department',
62   dpt_trans                      => 'dpt_trans',
63   drafts                         => 'draft',
64   dunning                        => 'dunning',
65   dunning_config                 => 'dunning_config',
66   employee                       => 'employee',
67   exchangerate                   => 'exchangerate',
68   finanzamt                      => 'finanzamt',
69   follow_up_access               => 'follow_up_access',
70   follow_up_links                => 'follow_up_link',
71   follow_ups                     => 'follow_up',
72   generic_translations           => 'generic_translation',
73   gifi                           => 'gifi',
74   gl                             => 'GLTransaction',
75   history_erp                    => 'history',
76   inventory                      => 'inventory',
77   invoice                        => 'invoice_item',
78   language                       => 'language',
79   makemodel                      => 'make_model',
80   notes                          => 'note',
81   orderitems                     => 'order_item',
82   oe                             => 'order',
83   parts                          => 'part',
84   partsgroup                     => 'parts_group',
85   partstax                       => 'parts_tax',
86   payment_terms                  => 'payment_term',
87   periodic_invoices              => 'periodic_invoice',
88   periodic_invoices_configs      => 'periodic_invoices_config',
89   prices                         => 'price',
90   price_factors                  => 'price_factor',
91   pricegroup                     => 'pricegroup',
92   printers                       => 'Printer',
93   record_links                   => 'record_link',
94   rma                            => 'RMA',
95   rmaitems                       => 'RMA_item',
96   sepa_export                    => 'sepa_export',
97   sepa_export_items              => 'sepa_export_item',
98   schema_info                    => 'schema_info',
99   status                         => 'status',
100   tax                            => 'tax',
101   taxkeys                        => 'tax_key',
102   tax_zones                      => 'tax_zone',
103   todo_user_config               => 'todo_user_config',
104   translation                    => 'translation',
105   translation_payment_terms      => 'translation_payment_term',
106   units                          => 'unit',
107   units_language                 => 'units_language',
108   vendor                         => 'vendor',
109   vendortax                      => 'vendor_tax',
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 camelify {
164   my ($str) = @_;
165   $str =~ s/_+(.)/uc($1)/ge;
166   ucfirst $str;
167 }
168
169 sub snakify {
170   my ($str) = @_;
171   $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
172   lcfirst $str;
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