use SL::MoreCommon qw(listify);
use SL::DBUtils qw(selectall_hashref_query selectrow_query);
use SL::DB::TransferType;
-use SL::Helper::Number qw(_number _round_number);
+use SL::Helper::Number qw(_format_number _round_number);
+use SL::Helper::Inventory::Allocation;
use SL::X;
our @EXPORT_OK = qw(get_stock get_onhand allocate allocate_for_assembly produce_assembly check_constraints);
sub allocate {
my (%params) = @_;
- die SL::X::Inventory::Allocation->new(
- error => 'allocate needs a part',
- msg => t8("Method allocate needs the parameter 'part'"),
- ) unless $params{part};
- die SL::X::Inventory::Allocation->new(
- error => 'allocate needs a qty',
- msg => t8("Method allocate needs the parameter 'qty'"),
- ) unless $params{qty};
+ croak('allocate needs a part') unless $params{part};
+ croak('allocate needs a qty') unless $params{qty};
my $part = $params{part};
my $qty = $params{qty};
if ($rest_qty > 0) {
die SL::X::Inventory::Allocation->new(
error => 'not enough to allocate',
- msg => t8("can not allocate #1 units of #2, missing #3 units", _number(\%::myconfig, $qty), $part->displayable_name, _number(\%::myconfig, $rest_qty)),
+ msg => t8("can not allocate #1 units of #2, missing #3 units", _format_number($qty), $part->displayable_name, _format_number($rest_qty)),
);
} else {
if ($params{constraints}) {
$err .= ' '.t8('part \'#\'1 in bin \'#2\' only with qty #3 (need additional #4) and chargenumber \'#5\'.',
SL::DB::Part->load_cached($_->parts_id)->description,
SL::DB::Bin->load_cached($_->bin_id)->full_description,
- _number($_->qty), _number($needed), $_->chargenumber ? $_->chargenumber : '--') for @allocs;
+ _format_number($_->qty), _format_number($needed), $_->chargenumber ? $_->chargenumber : '--') for @allocs;
die SL::X::Inventory::Allocation->new(
error => 'allocation constraints failure',
msg => $err,
my @transfers;
for my $allocation (@$allocations) {
my $oe_id = delete $allocation->{for_object_id};
- push @transfers, SL::DB::Inventory->new(
+ push @transfers, $allocation->transfer_object(
trans_id => $trans_id,
- %$allocation,
qty => -$allocation->qty,
trans_type => $trans_type_out,
shippingdate => $shippingdate,
employee => SL::DB::Manager::Employee->current,
- oe_id => $allocation->for_object_id,
);
}
$::instance_conf->get_show_bestbefore
}
-package SL::Helper::Inventory::Allocation {
- my @attributes = qw(parts_id qty bin_id warehouse_id chargenumber bestbefore comment for_object_id);
- my %attributes = map { $_ => 1 } @attributes;
-
- for my $name (@attributes) {
- no strict 'refs';
- *{"SL::Helper::Inventory::Allocation::$name"} = sub { $_[0]{$name} };
- }
-
- sub new {
- my ($class, %params) = @_;
-
- Carp::croak("missing attribute $_") for grep { !exists $params{$_} } @attributes;
- Carp::croak("unknown attribute $_") for grep { !exists $attributes{$_} } keys %params;
- Carp::croak("$_ must be set") for grep { !$params{$_} } qw(parts_id qty bin_id);
- Carp::croak("$_ must be positive") for grep { !($params{$_} > 0) } qw(parts_id qty bin_id);
-
- bless { %params }, $class;
- }
-}
-
1;
=encoding utf-8
# stock, get "what's there" for a part with various conditions:
my $qty = get_stock(part => $part); # how much is on stock?
my $qty = get_stock(part => $part, date => $date); # how much was on stock at a specific time?
- my $qty = get_stock(part => $part, bin => $bin); # how is on stock in a specific bin?
- my $qty = get_stock(part => $part, warehouse => $warehouse); # how is on stock in a specific warehouse?
- my $qty = get_stock(part => $part, chargenumber => $chargenumber); # how is on stock of a specific chargenumber?
+ my $qty = get_stock(part => $part, bin => $bin); # how much is on stock in a specific bin?
+ my $qty = get_stock(part => $part, warehouse => $warehouse); # how much is on stock in a specific warehouse?
+ my $qty = get_stock(part => $part, chargenumber => $chargenumber); # how much is on stock of a specific chargenumber?
# onhand, get "what's available" for a part with various conditions:
my $qty = get_onhand(part => $part); # how much is available?
);
# allocate:
- my @allocations, allocate(
+ my @allocations = allocate(
part => $part, # part_id works too
qty => $qty, # must be positive
chargenumber => $chargenumber, # optional, may be arrayref. if provided these charges will be used first
);
# shortcut to allocate all that is needed for producing an assembly, will use chargenumbers as appropriate
- my @allocations, allocate_for_assembly(
+ my @allocations = allocate_for_assembly(
part => $assembly, # part_id works too
qty => $qty, # must be positive
);
- # create allocation manually, bypassing checks, all of these need to be passed, even undefs
+ # create allocation manually, bypassing checks. all of these need to be passed, even undefs
my $allocation = SL::Helper::Inventory::Allocation->new(
part_id => $part->id,
qty => 15,
chargenumber => $chargenumber, # optional
bestbefore => $datetime, # optional
comment => $comment, # optional
-
- # links, all optional
);
=head1 DESCRIPTION
New functions for the warehouse and inventory api.
-The WH api currently has three large shortcomings. It is very hard to just get
+The WH api currently has three large shortcomings: It is very hard to just get
the current stock for an item, it's extremely complicated to use it to produce
assemblies while ensuring that no stock ends up negative, and it's very hard to
use it to get an overview over the actual contents of the inventory.
there.
=item * Onhand is what is available, which means things that are stocked,
-not expired and not reserved for other uses.
+not expired and not in any other way reserved for other uses.
=back
target of the production, and requires allocations to complete the request. The
allocations can be supplied manually, or can be generated automatically.
C<produce_assembly> will check whether enough allocations are given to create
-the recipe, but will not check whether the allocations are backed. If the
+the assembly, but will not check whether the allocations are backed. If the
allocations are not sufficient or if the auto-allocation fails an exception
is returned. If you need to produce something that is not in the inventory, you
can bypass those checks by creating the allocations yourself (see
=back
-C<chargenumber>, C<bestbefore> and C<for_object_id> may be C<undef> (but must
-still be present at creation time). Instances are considered immutable.
+C<chargenumber>, C<bestbefore> and C<for_object_id> and C<comment> may be
+C<undef> (but must still be present at creation time). Instances are considered
+immutable.
+Allocations also provide the method C<transfer_object> which will create a new
+C<SL::DB::Inventory> bject with all the playload.
=head1 CONSTRAINTS
the internal ordering of the hints is fixed and more complex preferentials
are not supported.
* bestbefore handling is untested
+ * interaction with config option "transfer_default_ignore_onhand" is
+ currently undefined (and implicitly ignores it)
=head1 TODO
* define and describe error classes
* define wrapper classes for stock/onhand batch mode return values
* handle extra arguments in produce: shippingdate, project
- * clean up allocation helper class
* document no_ check
* tests
=head1 AUTHOR
-Sven Schöling E<lt>sven.schoeling@opendynamic.deE<gt>
+Sven Schöling E<lt>sven.schoeling@googlemail.comE<gt>
=cut