X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FPart.pm;h=df0169d9c701e479628261bc5ba530dc98b905ec;hb=177cb111a3514218b8bd7c9221f7b90ce2bdac7e;hp=a2c3a73394e0e18ee484cf0e7e434c87cd517d50;hpb=67ad549c69391a69b48b13e220f34c8e29162b0d;p=kivitendo-erp.git diff --git a/SL/DB/Part.pm b/SL/DB/Part.pm index a2c3a7339..df0169d9c 100644 --- a/SL/DB/Part.pm +++ b/SL/DB/Part.pm @@ -176,7 +176,6 @@ sub orphaned { SL::DB::OrderItem SL::DB::DeliveryOrderItem SL::DB::Inventory - SL::DB::Assembly SL::DB::AssortmentItem ); @@ -263,6 +262,36 @@ sub get_chart { return $charts->{$taxzone}->{$type}; } +sub get_stock { + my ($self, %params) = @_; + + return undef unless $self->id; + + my $query = 'SELECT SUM(qty) FROM inventory WHERE parts_id = ?'; + my @values = ($self->id); + + if ( $params{bin_id} ) { + $query .= ' AND bin_id = ?'; + push(@values, $params{bin_id}); + } + + if ( $params{warehouse_id} ) { + $query .= ' AND warehouse_id = ?'; + push(@values, $params{warehouse_id}); + } + + if ( $params{shippingdate} ) { + die unless ref($params{shippingdate}) eq 'DateTime'; + $query .= ' AND shippingdate <= ?'; + push(@values, $params{shippingdate}); + } + + my ($stock) = selectrow_query($::form, $self->db->dbh, $query, @values); + + return $stock || 0; # never return undef +}; + + # this is designed to ignore chargenumbers, expiration dates and just give a list of how much <-> where sub get_simple_stock { my ($self, %params) = @_; @@ -520,6 +549,20 @@ Non-recursive lastcost sum of all the assembly item lastcosts. Non-recursive lastcost sum of all the assortment item lastcosts. +=item C + +Fetches stock qty in the default unit for a part. + +bin_id and warehouse_id may be passed as params. If only a bin_id is passed, +the stock qty for that bin is returned. If only a warehouse_id is passed, the +stock qty for all bins in that warehouse is returned. If a shippingdate is +passed the stock qty for that date is returned. + +Examples: + my $qty = $part->get_stock(bin_id => 52); + + $part->get_stock(shippingdate => DateTime->today->add(days => -5)); + =back =head1 AUTHORS