Doku-Update
[kivitendo-erp.git] / SL / DB / Part.pm
1 package SL::DB::Part;
2
3 use strict;
4
5 use Carp;
6 use SL::DBUtils;
7 use SL::DB::MetaSetup::Part;
8 use SL::DB::Manager::Part;
9
10 __PACKAGE__->meta->add_relationships(
11   unit_obj                     => {
12     type         => 'one to one',
13     class        => 'SL::DB::Unit',
14     column_map   => { unit => 'name' },
15   },
16   assemblies                     => {
17     type         => 'one to many',
18     class        => 'SL::DB::Assembly',
19     column_map   => { id => 'id' },
20   },
21   partsgroup                     => {
22     type         => 'one to one',
23     class        => 'SL::DB::PartsGroup',
24     column_map   => { partsgroup_id => 'id' },
25   },
26   price_factor   => {
27     type         => 'one to one',
28     class        => 'SL::DB::PriceFactor',
29     column_map   => { price_factor_id => 'id' },
30   },
31 );
32
33 __PACKAGE__->meta->initialize;
34
35 sub is_type {
36   my $self = shift;
37   my $type  = lc(shift || '');
38   die 'invalid type' unless $type =~ /^(?:part|service|assembly)$/;
39
40   return $self->type eq $type ? 1 : 0;
41 }
42
43 sub is_part     { $_[0]->is_type('part') }
44 sub is_assembly { $_[0]->is_type('assembly') }
45 sub is_service  { $_[0]->is_type('service') }
46
47 sub type {
48   my ($self, $type) = @_;
49   if (@_ > 1) {
50     die 'invalid type' unless $type =~ /^(?:part|service|assembly)$/;
51     $self->assembly(          $type eq 'assembly' ? 1 : 0);
52     $self->inventory_accno_id($type ne 'service'  ? 1 : undef);
53   }
54
55   return 'assembly' if $self->assembly;
56   return 'part'     if $self->inventory_accno_id;
57   return 'service';
58 }
59
60 sub new_part {
61   my ($class, %params) = @_;
62   $class->new(%params, type => 'part');
63 }
64
65 sub new_assembly {
66   my ($class, %params) = @_;
67   $class->new(%params, type => 'assembly');
68 }
69
70 sub new_service {
71   my ($class, %params) = @_;
72   $class->new(%params, type => 'service');
73 }
74
75 sub orphaned {
76   my ($self) = @_;
77   die 'not an accessor' if @_ > 1;
78
79   my @relations = qw(
80     SL::DB::InvoiceItem
81     SL::DB::OrderItem
82     SL::DB::Inventory
83     SL::DB::RMAItem
84   );
85
86   for my $class (@relations) {
87     eval "require $class";
88     return 0 if $class->_get_manager_class->get_all_count(query => [ parts_id => $self->id ]);
89   }
90   return 1;
91 }
92
93 sub get_sellprice_info {
94   my $self   = shift;
95   my %params = @_;
96
97   confess "Missing part id" unless $self->id;
98
99   my $object = $self->load;
100
101   return { sellprice       => $object->sellprice,
102            price_factor_id => $object->price_factor_id };
103 }
104
105 sub get_ordered_qty {
106   my $self   = shift;
107   my %result = SL::DB::Manager::Part->get_ordered_qty($self->id);
108
109   return $result{ $self->id };
110 }
111
112 sub available_units {
113   shift->unit_obj->convertible_units;
114 }
115
116 # autogenerated accessor is slightly off...
117 sub buchungsgruppe {
118   shift->buchungsgruppen(@_);
119 }
120
121 1;
122
123 __END__
124
125 =pod
126
127 =encoding utf-8
128
129 =head1 NAME
130
131 SL::DB::Part: Model for the 'parts' table
132
133 =head1 SYNOPSIS
134
135 This is a standard Rose::DB::Object based model and can be used as one.
136
137 =head1 TYPES
138
139 Although the base class is called C<Part> we usually talk about C<Articles> if
140 we mean instances of this class. This is because articles come in three
141 flavours called:
142
143 =over 4
144
145 =item Part     - a single part
146
147 =item Service  - a part without onhand, and without inventory accounting
148
149 =item Assembly - a collection of both parts and services
150
151 =back
152
153 These types are sadly represented by data inside the class and cannot be
154 migrated into a flag. To work around this, each C<Part> object knows what type
155 it currently is. Since the type ist data driven, there ist no explicit setting
156 method for it, but you can construct them explicitly with C<new_part>,
157 C<new_service>, and C<new_assembly>. A Buchungsgruppe should be supplied in this
158 case, but it will use the default Buchungsgruppe if you don't.
159
160 Matching these there are assorted helper methods dealing with types,
161 e.g.  L</new_part>, L</new_service>, L</new_assembly>, L</type>,
162 L</is_type> and others.
163
164 =head1 FUNCTIONS
165
166 =over 4
167
168 =item C<new_part %PARAMS>
169
170 =item C<new_service %PARAMS>
171
172 =item C<new_assembly %PARAMS>
173
174 Will set the appropriate data fields so that the resulting instance will be of
175 tthe requested type. Since part of the distinction are accounting targets,
176 providing a C<Buchungsgruppe> is recommended. If none is given the constructor
177 will load a default one and set the accounting targets from it.
178
179 =item C<type>
180
181 Returns the type as a string. Can be one of C<part>, C<service>, C<assembly>.
182
183 =item C<is_type $TYPE>
184
185 Tests if the current object is a part, a service or an
186 assembly. C<$type> must be one of the words 'part', 'service' or
187 'assembly' (their plurals are ok, too).
188
189 Returns 1 if the requested type matches, 0 if it doesn't and
190 C<confess>es if an unknown C<$type> parameter is encountered.
191
192 =item C<is_part>
193
194 =item C<is_service>
195
196 =item C<is_assembly>
197
198 Shorthand for C<is_type('part')> etc.
199
200 =item C<get_sellprice_info %params>
201
202 Retrieves the C<sellprice> and C<price_factor_id> for a part under
203 different conditions and returns a hash reference with those two keys.
204
205 If C<%params> contains a key C<project_id> then a project price list
206 will be consulted if one exists for that project. In this case the
207 parameter C<country_id> is evaluated as well: if a price list entry
208 has been created for this country then it will be used. Otherwise an
209 entry without a country set will be used.
210
211 If none of the above conditions is met then the information from
212 C<$self> is used.
213
214 =item C<get_ordered_qty %params>
215
216 Retrieves the quantity that has been ordered from a vendor but that
217 has not been delivered yet. Only open purchase orders are considered.
218
219 =item C<orphaned>
220
221 Checks if this articke is used in orders, invoices, delivery orders or
222 assemblies.
223
224 =item C<buchungsgruppe BUCHUNGSGRUPPE>
225
226 Used to set the accounting informations from a L<SL:DB::Buchungsgruppe> object.
227 Please note, that this is a write only accessor, the original Buchungsgruppe can
228 not be retrieved from an article once set.
229
230 =back
231
232 =head1 AUTHORS
233
234 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
235 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
236
237 =cut