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