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