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