cc4d2abe161f192bb530c01d7943854c3c827de6
[kivitendo-erp.git] / SL / DB / Helper / Mappings.pm
1 package SL::DB::Helper::Mappings;
2
3 use utf8;
4 use strict;
5
6 use SL::Util qw(camelify);
7
8 require Exporter;
9 our @ISA       = qw(Exporter);
10 our @EXPORT_OK = qw(get_table_for_package get_package_for_table get_package_names);
11
12 # these will not be managed as Rose::DB models, because they are not normalized,
13 # significant changes are needed to get them done, or they were done by CRM.
14 my @lxoffice_blacklist_permanent = qw(
15   leads
16 );
17
18 # these are not managed _yet_, but will hopefully at some point.
19 # if you are confident that one of these works, remove it here.
20 my @lxoffice_blacklist_temp = qw(
21 );
22
23 # tables created by crm module
24 my @crm_blacklist = qw(
25 );
26
27 # tables created by cash register
28 my @cash_register_blacklist = qw(
29 ekartikel ekbon ekkunde ektext erptasten
30 );
31
32 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp, @crm_blacklist, @cash_register_blacklist);
33
34 # map table names to their models.
35 # unlike rails we have no singular<->plural magic.
36 # remeber: tables should be named as the plural of the model name.
37 my %lxoffice_package_names = (
38   acc_trans                      => 'acc_transaction',
39   audittrail                     => 'audit_trail',
40   auth_group                     => 'auth_groups',
41   auth_group_right               => 'auth_group_rights',
42   auth_user                      => 'auth_users',
43   auth_user_config               => 'auth_user_configs',
44   auth_user_group                => 'auth_user_groups',
45   ar                             => 'invoice',
46   ap                             => 'purchase_invoice',
47   background_jobs                => 'background_job',
48   background_job_histories       => 'background_job_history',
49   ap                             => 'purchase_invoice',
50   bank_accounts                  => 'bank_account',
51   buchungsgruppen                => 'buchungsgruppe',
52   contacts                       => 'contact',
53   csv_import_profiles            => 'csv_import_profile',
54   csv_import_profile_settings    => 'csv_import_profile_setting',
55   csv_import_reports             => 'csv_import_report',
56   csv_import_report_rows         => 'csv_import_report_row',
57   csv_import_report_status       => 'csv_import_report_status',
58   custom_variable_configs        => 'custom_variable_config',
59   custom_variables               => 'custom_variable',
60   custom_variables_validity      => 'custom_variable_validity',
61   datev                          => 'datev',
62   defaults                       => 'default',
63   delivery_orders                => 'delivery_order',
64   delivery_order_items           => 'delivery_order_item',
65   department                     => 'department',
66   dpt_trans                      => 'dpt_trans',
67   drafts                         => 'draft',
68   dunning                        => 'dunning',
69   dunning_config                 => 'dunning_config',
70   employee                       => 'employee',
71   exchangerate                   => 'exchangerate',
72   finanzamt                      => 'finanzamt',
73   follow_up_access               => 'follow_up_access',
74   follow_up_links                => 'follow_up_link',
75   follow_ups                     => 'follow_up',
76   generic_translations           => 'generic_translation',
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   payment_terms                  => 'payment_term',
89   periodic_invoices              => 'periodic_invoice',
90   periodic_invoices_configs      => 'periodic_invoices_config',
91   prices                         => 'price',
92   price_factors                  => 'price_factor',
93   pricegroup                     => 'pricegroup',
94   printers                       => 'Printer',
95   record_links                   => 'record_link',
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 );
110
111 my (%lxoffice_tables_to_packages, %lxoffice_tables_to_manager_packages, %lxoffice_packages_to_tables);
112
113 sub get_blacklist {
114   return LXOFFICE => \@lxoffice_blacklist;
115 }
116
117 sub get_package_names {
118   return LXOFFICE => \%lxoffice_package_names;
119 }
120
121 sub get_package_for_table {
122   %lxoffice_tables_to_packages = map { ($_ => "SL::DB::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
123     unless %lxoffice_tables_to_packages;
124
125   return $lxoffice_tables_to_packages{ $_[0] };
126 }
127
128 sub get_manager_package_for_table {
129   %lxoffice_tables_to_manager_packages = map { ($_ => "SL::DB::Manager::" . camelify($lxoffice_package_names{$_})) } keys %lxoffice_package_names
130     unless %lxoffice_tables_to_manager_packages;
131
132   return $lxoffice_tables_to_manager_packages{ $_[0] };
133 }
134
135 sub get_table_for_package {
136   get_package_for_table('dummy') if !%lxoffice_tables_to_packages;
137   %lxoffice_packages_to_tables = reverse %lxoffice_tables_to_packages unless %lxoffice_packages_to_tables;
138
139   my $package = $_[0] =~ m/^SL::DB::/ ? $_[0] : "SL::DB::" . $_[0];
140   return $lxoffice_packages_to_tables{ $package };
141 }
142
143 sub db {
144   my $string = $_[0];
145   my $lookup = $lxoffice_package_names{$_[0]} ||
146       plurify($lxoffice_package_names{singlify($_[0])});
147
148   for my $thing ($string, $lookup) {
149
150     # best guess? its already the name. like part. camelize it first
151     my $class = "SL::DB::" . camelify($thing);
152     return $class if defined *{ $class. '::' };
153
154     # next, someone wants a manager and pluralized.
155     my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
156     return $manager if defined *{ $manager . '::' };
157   }
158
159   die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
160 }
161
162 sub plurify {
163   my ($str) = @_;
164   $str . 's';
165 }
166
167 sub singlify {
168   my ($str) = @_;
169   local $/ = 's';
170   chomp $str;
171   $str;
172 }
173
174 1;
175
176 __END__
177
178 =encoding utf8
179
180 =head1 NAME
181
182 SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
183
184 =head1 SYNOPSIS
185
186   use SL::DB::Helper::Mappings qw(@blacklist %table2model);
187
188 =head1 DESCRIPTION
189
190 This modul stores table <-> model mappings used by the
191 L<scripts/rose_auto_create_model.pl> script.  If you add a new table that has
192 custom mappings, add it here.
193
194 =head1 FUNCTIONS
195
196 =over 4
197
198 =item C<db $name>
199
200 A special function provided here is C<db>. Without it you'd have to write:
201
202   my $part = SL::DB::Part->new(id => 1234);
203   my @all_parts = SL::DB::Manager::Part->get_all;
204
205 with them it becomes:
206
207   my $part = db('part')->new(id => 123);
208   my @all_parts = db('parts')->get_all;
209
210 You don't have to care about add that SL::DB:: incantation anymore. Also, a
211 simple s at the end will get you the associated Manager class.
212
213 db is written to try to make sense of what you give it, but if all fails, it
214 will die with an error.
215
216 =item C<get_package_for_table $table_name>
217
218 Returns the package name for a table name:
219
220   SL::DB::Helper::Mappings::get_package_for_table('oe')
221   # SL::DB::Order
222
223 =item C<get_manager_package_for_table $table_name>
224
225 Returns the manager package name for a table name:
226
227   SL::DB::Helper::Mappings::get_manager_package_for_table('oe')
228   # SL::DB::Manager::Order
229
230 =item C<get_table_for_package $package_name>
231
232 Returns the table name for a package name:
233
234   SL::DB::Helper::Mappings::get_table_for_package('SL::DB::Order')
235   # oe
236   SL::DB::Helper::Mappings::get_table_for_package('Order')
237   # oe
238
239 =back
240
241 =head1 BUGS
242
243 nothing yet
244
245 =head1 SEE ALSO
246
247 L<scripts/rose_auto_create_model.pl>
248
249 =head1 AUTHOR
250
251 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
252 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
253
254 =cut