7 use SL::DB::MetaSetup::Part;
 
   8 use SL::DB::Manager::Part;
 
  10 __PACKAGE__->attr_number('lastcost',  places => -2);
 
  11 __PACKAGE__->attr_number('listprice', places => -2);
 
  12 __PACKAGE__->attr_number('sellprice', places => -2);
 
  14 __PACKAGE__->meta->add_relationships(
 
  17     class        => 'SL::DB::Unit',
 
  18     column_map   => { unit => 'name' },
 
  21     type         => 'one to many',
 
  22     class        => 'SL::DB::Assembly',
 
  23     column_map   => { id => 'id' },
 
  27 __PACKAGE__->meta->initialize;
 
  31   my $type  = lc(shift || '');
 
  33   if ($type =~ m/^part/) {
 
  34     return !$self->assembly && $self->inventory_accno_id  ? 1 : 0;
 
  36   } elsif ($type =~ m/^service/) {
 
  37     return !$self->inventory_accno_id && !$self->assembly ? 1 : 0;
 
  39   } elsif ($type =~ m/^assembl/) {
 
  40     return $self->assembly                                ? 1 : 0;
 
  44   confess "Unknown type parameter '$type'";
 
  47 sub get_sellprice_info {
 
  51   confess "Missing part id" unless $self->id;
 
  53   my $object = $self->load;
 
  55   return { sellprice       => $object->sellprice,
 
  56            price_factor_id => $object->price_factor_id };
 
  61   my %result = SL::DB::Manager::Part->get_ordered_qty($self->id);
 
  63   return $result{ $self->id };
 
  66 sub get_uncommissioned_qty {
 
  70   confess "Missing part id" unless $self->id;
 
  74       COALESCE((SELECT SUM(oi.qty)
 
  76                 LEFT JOIN oe ON (oi.trans_id = oe.id)
 
  77                 WHERE (oi.parts_id = ?)
 
  78                   AND (NOT COALESCE(oe.quotation, FALSE))
 
  79                   AND (NOT COALESCE(oe.closed,    FALSE))
 
  80                   AND (NOT COALESCE(oe.delivered, FALSE))
 
  81                   AND (COALESCE(oe.customer_id, 0) <> 0)),
 
  84       COALESCE((SELECT SUM(i.qty) AS qty
 
  86                 LEFT JOIN warehouse wh ON (i.warehouse_id = wh.id)
 
  87                 WHERE (i.parts_id = ?)
 
  88                   AND COALESCE(wh.commission)),
 
  93   my $result = selectfirst_hashref_query($::form, $self->dbh, $query, $self->id, $self->id);
 
  94   return $result ? $result->{qty} : 0;
 
  98   shift->unit_obj->convertible_units;
 
 109 SL::DB::Part: Model for the 'parts' table
 
 113 This is a standard Rose::DB::Object based model and can be used as one.
 
 121 Tests if the current object is a part, a service or an
 
 122 assembly. C<$type> must be one of the words 'part', 'service' or
 
 123 'assembly' (their plurals are ok, too).
 
 125 Returns 1 if the requested type matches, 0 if it doesn't and
 
 126 C<confess>es if an unknown C<$type> parameter is encountered.
 
 128 =item get_sellprice_info %params
 
 130 Retrieves the C<sellprice> and C<price_factor_id> for a part under
 
 131 different conditions and returns a hash reference with those two keys.
 
 133 If C<%params> contains a key C<project_id> then a project price list
 
 134 will be consulted if one exists for that project. In this case the
 
 135 parameter C<country_id> is evaluated as well: if a price list entry
 
 136 has been created for this country then it will be used. Otherwise an
 
 137 entry without a country set will be used.
 
 139 If none of the above conditions is met then the information from
 
 142 =item get_ordered_qty %params
 
 144 Retrieves the quantity that has been ordered from a vendor but that
 
 145 has not been delivered yet. Only open purchase orders are considered.
 
 147 =item get_uncommissioned_qty %params
 
 149 Retrieves the quantity that has been ordered by a customer but that
 
 150 has not been commissioned yet. Only open sales orders are considered.
 
 156 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>