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