Merge branch 'csv-import-in-perl'
[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 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp);
22
23 # map table names to their models.
24 # unlike rails we have no singular<->plural magic.
25 # remeber: tables should be named as the plural of the model name.
26 my %lxoffice_package_names = (
27   acc_trans                      => 'acc_transaction',
28   audittrail                     => 'audit_trail',
29   auth_group                     => 'auth_groups',
30   auth_group_right               => 'auth_group_rights',
31   auth_user                      => 'auth_users',
32   auth_user_config               => 'auth_user_configs',
33   auth_user_group                => 'auth_user_groups',
34   ar                             => 'invoice',
35   ap                             => 'purchase_invoice',
36   background_jobs                => 'background_job',
37   background_job_histories       => 'background_job_history',
38   ap                             => 'purchase_invoice',
39   bank_accounts                  => 'bank_account',
40   buchungsgruppen                => 'buchungsgruppe',
41   contacts                       => 'contact',
42   csv_import_profiles            => 'csv_import_profile',
43   csv_import_profile_settings    => 'csv_import_profile_setting',
44   custom_variable_configs        => 'custom_variable_config',
45   custom_variables               => 'custom_variable',
46   custom_variables_validity      => 'custom_variable_validity',
47   customertax                    => 'customer_tax',
48   datev                          => 'datev',
49   defaults                       => 'default',
50   delivery_orders                => 'delivery_order',
51   delivery_order_items           => 'delivery_order_item',
52   department                     => 'department',
53   dpt_trans                      => 'dpt_trans',
54   drafts                         => 'draft',
55   dunning                        => 'dunning',
56   dunning_config                 => 'dunning_config',
57   employee                       => 'employee',
58   exchangerate                   => 'exchangerate',
59   finanzamt                      => 'finanzamt',
60   follow_up_access               => 'follow_up_access',
61   follow_up_links                => 'follow_up_link',
62   follow_ups                     => 'follow_up',
63   generic_translations           => 'generic_translation',
64   gifi                           => 'gifi',
65   gl                             => 'GLTransaction',
66   history_erp                    => 'history',
67   inventory                      => 'inventory',
68   invoice                        => 'invoice_item',
69   language                       => 'language',
70   license                        => 'license',
71   licenseinvoice                 => 'license_invoice',
72   makemodel                      => 'make_model',
73   notes                          => 'note',
74   orderitems                     => 'order_item',
75   oe                             => 'order',
76   parts                          => 'part',
77   partsgroup                     => 'parts_group',
78   partstax                       => 'parts_tax',
79   payment_terms                  => 'payment_term',
80   periodic_invoices              => 'periodic_invoice',
81   periodic_invoices_configs      => 'periodic_invoices_config',
82   prices                         => 'price',
83   price_factors                  => 'price_factor',
84   pricegroup                     => 'pricegroup',
85   printers                       => 'Printer',
86   record_links                   => 'record_link',
87   rma                            => 'RMA',
88   rmaitems                       => 'RMA_item',
89   sepa_export                    => 'sepa_export',
90   sepa_export_items              => 'sepa_export_item',
91   schema_info                    => 'schema_info',
92   status                         => 'status',
93   tax                            => 'tax',
94   taxkeys                        => 'tax_key',
95   tax_zones                      => 'tax_zone',
96   todo_user_config               => 'todo_user_config',
97   translation                    => 'translation',
98   translation_payment_terms      => 'translation_payment_term',
99   units                          => 'unit',
100   units_language                 => 'units_language',
101   vendortax                      => 'vendor_tax',
102 );
103
104 my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables);
105
106 sub get_blacklist {
107   return LXOFFICE => \@lxoffice_blacklist;
108 }
109
110 sub get_package_names {
111   return LXOFFICE => \%lxoffice_package_names;
112 }
113
114 sub get_package_for_table {
115   %lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
116     unless %lxoffice_tables_to_packages;
117
118   return $lxoffice_tables_to_packages{ $_[0] };
119 }
120
121 sub get_manager_package_for_table {
122   %lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
123     unless %lxoffice_tables_to_manager_packages;
124
125   return $lxoffice_tables_to_manager_packages{ $_[0] };
126 }
127
128 sub get_table_for_package {
129   get_package_for_table('dummy') if !%lxoffice_tables_to_packages;
130   %lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables;
131
132   my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
133   return $lxoffice_packages_to_tables{ $package };
134 }
135
136 sub db {
137   my $string = $_[0];
138   my $lookup = $lxoffice_package_names{$_[0]} ||
139       plurify($lxoffice_package_names{singlify($_[0])});
140
141   for my $thing ($string, $lookup) {
142
143     # best guess? its already the name. like part. camelize it first
144     my $class = "SL::DB::" . camelify($thing);
145     return $class if defined *{ $class. '::' };
146
147     # next, someone wants a manager and pluralized.
148     my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
149     return $manager if defined *{ $manager . '::' };
150   }
151
152   die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
153 }
154
155 sub camelify {
156   my ($str) = @_;
157   $str =~ s/_+(.)/uc($1)/ge;
158   ucfirst $str;
159 }
160
161 sub snakify {
162   my ($str) = @_;
163   $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
164   lcfirst $str;
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