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