Attribute Helper umgeschrieben.
[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 );
22
23 __PACKAGE__->meta->initialize;
24
25 sub is_type {
26   my $self = shift;
27   my $type  = lc(shift || '');
28
29   if ($type =~ m/^part/) {
30     return !$self->assembly && $self->inventory_accno_id  ? 1 : 0;
31
32   } elsif ($type =~ m/^service/) {
33     return !$self->inventory_accno_id && !$self->assembly ? 1 : 0;
34
35   } elsif ($type =~ m/^assembl/) {
36     return $self->assembly                                ? 1 : 0;
37
38   }
39
40   confess "Unknown type parameter '$type'";
41 }
42
43 sub get_sellprice_info {
44   my $self   = shift;
45   my %params = @_;
46
47   confess "Missing part id" unless $self->id;
48
49   my $object = $self->load;
50
51   return { sellprice       => $object->sellprice,
52            price_factor_id => $object->price_factor_id };
53 }
54
55 sub get_ordered_qty {
56   my $self   = shift;
57   my %result = SL::DB::Manager::Part->get_ordered_qty($self->id);
58
59   return $result{ $self->id };
60 }
61
62 sub available_units {
63   shift->unit_obj->convertible_units;
64 }
65
66 1;
67
68 __END__
69
70 =pod
71
72 =head1 NAME
73
74 SL::DB::Part: Model for the 'parts' table
75
76 =head1 SYNOPSIS
77
78 This is a standard Rose::DB::Object based model and can be used as one.
79
80 =head1 FUNCTIONS
81
82 =over 4
83
84 =item is_type $type
85
86 Tests if the current object is a part, a service or an
87 assembly. C<$type> must be one of the words 'part', 'service' or
88 'assembly' (their plurals are ok, too).
89
90 Returns 1 if the requested type matches, 0 if it doesn't and
91 C<confess>es if an unknown C<$type> parameter is encountered.
92
93 =item get_sellprice_info %params
94
95 Retrieves the C<sellprice> and C<price_factor_id> for a part under
96 different conditions and returns a hash reference with those two keys.
97
98 If C<%params> contains a key C<project_id> then a project price list
99 will be consulted if one exists for that project. In this case the
100 parameter C<country_id> is evaluated as well: if a price list entry
101 has been created for this country then it will be used. Otherwise an
102 entry without a country set will be used.
103
104 If none of the above conditions is met then the information from
105 C<$self> is used.
106
107 =item get_ordered_qty %params
108
109 Retrieves the quantity that has been ordered from a vendor but that
110 has not been delivered yet. Only open purchase orders are considered.
111
112 =item get_uncommissioned_qty %params
113
114 Retrieves the quantity that has been ordered by a customer but that
115 has not been commissioned yet. Only open sales orders are considered.
116
117 =back
118
119 =head1 AUTHOR
120
121 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
122
123 =cut