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