Datenbankupgradescript, Rose-Models für Wiederkehrende Rechnungen
[kivitendo-erp.git] / SL / DB / Helper / Mappings.pm
1 package SL::DB::Helper::Mappings;
2
3 use utf8;
4 use strict;
5
6 # these will not be managed as Rose::DB models, because they are not normalized,
7 # significant changes are needed to get them done, or they were done by CRM.
8 my @lxoffice_blacklist_permanent = qw(
9   leads
10 );
11
12 # these are not managed _yet_, but will hopefully at some point.
13 # if you are confident that one of these works, remove it here.
14 my @lxoffice_blacklist_temp = qw(
15 );
16
17 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp);
18
19 # map table names to their models.
20 # unlike rails we have no singular<->plural magic.
21 # remeber: tables should be named as the plural of the model name.
22 my %lxoffice_package_names = (
23   acc_trans                      => 'acc_transaction',
24   audittrail                     => 'audit_trail',
25   ar                             => 'invoice',
26   ap                             => 'purchase_invoice',
27   background_jobs                => 'background_job',
28   background_job_histories       => 'background_job_history',
29   ap                             => 'purchase_invoice',
30   bank_accounts                  => 'bank_account',
31   buchungsgruppen                => 'buchungsgruppe',
32   contacts                       => 'contact',
33   custom_variable_configs        => 'custom_variable_config',
34   custom_variables               => 'custom_variable',
35   custom_variables_validity      => 'custom_variable_validity',
36   customertax                    => 'customer_tax',
37   datev                          => 'datev',
38   defaults                       => 'default',
39   delivery_orders                => 'delivery_order',
40   delivery_order_items           => 'delivery_order_item',
41   department                     => 'department',
42   dpt_trans                      => 'dpt_trans',
43   drafts                         => 'draft',
44   dunning                        => 'dunning',
45   dunning_config                 => 'dunning_config',
46   employee                       => 'employee',
47   exchangerate                   => 'exchangerate',
48   finanzamt                      => 'finanzamt',
49   follow_up_access               => 'follow_up_access',
50   follow_up_links                => 'follow_up_link',
51   follow_ups                     => 'follow_up',
52   generic_translations           => 'generic_translation',
53   gifi                           => 'gifi',
54   gl                             => 'GLTransaction',
55   history_erp                    => 'history',
56   inventory                      => 'inventory',
57   invoice                        => 'invoice_item',
58   language                       => 'language',
59   license                        => 'license',
60   licenseinvoice                 => 'license_invoice',
61   makemodel                      => 'make_model',
62   notes                          => 'note',
63   orderitems                     => 'order_item',
64   oe                             => 'order',
65   parts                          => 'part',
66   partsgroup                     => 'parts_group',
67   partstax                       => 'parts_tax',
68   payment_terms                  => 'payment_term',
69   periodic_invoices              => 'periodic_invoice',
70   periodic_invoices_configs      => 'periodic_invoices_config',
71   prices                         => 'prices',
72   price_factors                  => 'price_factor',
73   pricegroup                     => 'pricegroup',
74   printers                       => 'Printer',
75   record_links                   => 'record_link',
76   rma                            => 'RMA',
77   rmaitems                       => 'RMA_item',
78   sepa_export                    => 'sepa_export',
79   sepa_export_items              => 'sepa_export_item',
80   schema_info                    => 'schema_info',
81   status                         => 'status',
82   tax                            => 'tax',
83   taxkeys                        => 'tax_key',
84   tax_zones                      => 'tax_zone',
85   todo_user_config               => 'todo_user_config',
86   translation                    => 'translation',
87   translation_payment_terms      => 'translation_payment_term',
88   units                          => 'unit',
89   units_language                 => 'units_language',
90   vendortax                      => 'vendor_tax',
91 );
92
93 sub get_blacklist {
94   return LXOFFICE => \@lxoffice_blacklist;
95 }
96
97 sub get_package_names {
98   return LXOFFICE => \%lxoffice_package_names;
99 }
100
101 sub db {
102   my $string = $_[0];
103   my $lookup = $lxoffice_package_names{$_[0]} ||
104       plurify($lxoffice_package_names{singlify($_[0])});
105
106   for my $thing ($string, $lookup) {
107
108     # best guess? its already the name. like part. camelize it first
109     my $class = "SL::DB::" . camelify($thing);
110     return $class if defined *{ $class. '::' };
111
112     # next, someone wants a manager and pluralized.
113     my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
114     return $manager if defined *{ $manager . '::' };
115   }
116
117   die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
118 }
119
120 sub camelify {
121   my ($str) = @_;
122   $str =~ s/_+(.)/uc($1)/ge;
123   ucfirst $str;
124 }
125
126 sub snakify {
127   my ($str) = @_;
128   $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
129   lcfirst $str;
130 }
131
132 sub plurify {
133   my ($str) = @_;
134   $str . 's';
135 }
136
137 sub singlify {
138   my ($str) = @_;
139   local $/ = 's';
140   chomp $str;
141   $str;
142 }
143
144 1;
145
146 __END__
147
148 =head1 NAME
149
150 SL::DB::Helper::Mappings - Rose Table <-> Model mapping information
151
152 =head1 SYNOPSIS
153
154   use SL::DB::Helper::Mappings qw(@blacklist %table2model);
155
156 =head1 DESCRIPTION
157
158 This modul stores table <-> model mappings used by the
159 L<scripts/rose_auto_create_model.pl> script.  If you add a new table that has
160 custom mappings, add it here.
161
162 =head2 db
163
164 A special function provided here is E<db>. Without it you'd have to write:
165
166   my $part = SL::DB::Part->new(id => 1234);
167   my @all_parts = SL::DB::Manager::Part->get_all;
168
169 with them it becomes:
170
171   my $part = db('part')->new(id => 123);
172   my @all_parts = db('parts')->get_all;
173
174 You don't have to care about add that SL::DB:: incantation anymore. Also, a
175 simple s at the end will get you the associated Manager class.
176
177 db is written to try to make sense of what you give it, but if all fails, it
178 will die with an error.
179
180 =head1 BUGS
181
182 nothing yet
183
184 =head1 SEE ALSO
185
186 L<scripts/rose_auto_create_model.pl>
187
188 =head1 AUTHOR
189
190 Sven Schöling <s.schoeling@linet-services.de>
191
192 =cut