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