7 use SL::DB::MetaSetup::Part;
 
   8 use SL::DB::Manager::Part;
 
  10 __PACKAGE__->meta->add_relationships(
 
  13     class        => 'SL::DB::Unit',
 
  14     column_map   => { unit => 'name' },
 
  17     type         => 'one to many',
 
  18     class        => 'SL::DB::Assembly',
 
  19     column_map   => { id => 'id' },
 
  23     class        => 'SL::DB::PartsGroup',
 
  24     column_map   => { partsgroup_id => 'id' },
 
  28 __PACKAGE__->meta->initialize;
 
  32   my $type  = lc(shift || '');
 
  33   die 'invalid type' unless $type =~ /^(?:part|service|assembly)$/;
 
  35   return $self->type eq $type ? 1 : 0;
 
  38 sub is_part     { $_[0]->is_type('part') }
 
  39 sub is_assembly { $_[0]->is_type('assembly') }
 
  40 sub is_service  { $_[0]->is_type('service') }
 
  43   my ($self, $type) = @_;
 
  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);
 
  50   return 'assembly' if $self->assembly;
 
  51   return 'part'     if $self->inventory_accno_id;
 
  56   my ($class, %params) = @_;
 
  57   $class->new(%params, type => 'part');
 
  61   my ($class, %params) = @_;
 
  62   $class->new(%params, type => 'assembly');
 
  66   my ($class, %params) = @_;
 
  67   $class->new(%params, type => 'service');
 
  72   die 'not an accessor' if @_ > 1;
 
  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 ]);
 
  88 sub get_sellprice_info {
 
  92   confess "Missing part id" unless $self->id;
 
  94   my $object = $self->load;
 
  96   return { sellprice       => $object->sellprice,
 
  97            price_factor_id => $object->price_factor_id };
 
 100 sub get_ordered_qty {
 
 102   my %result = SL::DB::Manager::Part->get_ordered_qty($self->id);
 
 104   return $result{ $self->id };
 
 107 sub available_units {
 
 108   shift->unit_obj->convertible_units;
 
 111 # autogenerated accessor is slightly off...
 
 113   shift->buchungsgruppen(@_);
 
 124 SL::DB::Part: Model for the 'parts' table
 
 128 This is a standard Rose::DB::Object based model and can be used as one.
 
 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
 
 138 =item Part     - a single part
 
 140 =item Service  - a part without onhand, and without inventory accounting
 
 142 =item Assembly - a collection of both parts and services
 
 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.
 
 153 Matching these there are assorted helper methods dealing with type:
 
 155 =head2 new_part PARAMS
 
 157 =head2 new_service PARAMS
 
 159 =head2 new_assembly PARAMS
 
 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.
 
 168 Returns the type as a string. Can be one of C<part>, C<service>, C<assembly>.
 
 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).
 
 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.
 
 185 Shorthand for is_type('part') etc.
 
 189 =head2 get_sellprice_info %params
 
 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.
 
 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.
 
 200 If none of the above conditions is met then the information from
 
 203 =head2 get_ordered_qty %params
 
 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.
 
 210 Checks if this articke is used in orders, invoices, delivery orders or
 
 213 =head2 buchungsgruppe BUCHUNGSGRUPPE
 
 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.
 
 221 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>