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