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);
push @values, $params{date};
}
+ if (!$params{bestbefore} && $onhand_mode && default_show_bestbefore()) {
+ $params{bestbefore} = DateTime->now_local;
+ }
+
if ($params{bestbefore}) {
Carp::croak("not DateTime ".$params{date}) unless ref($params{bestbefore}) eq 'DateTime';
- push @where, sprintf "bestbefore >= ?";
+ push @where, sprintf "(bestbefore IS NULL OR bestbefore >= ?)";
push @values, $params{bestbefore};
}
LEFT JOIN warehouse ON bin.warehouse_id = warehouse.id
$where
$group_by
- HAVING SUM(qty) > 0
+
+ if ($onhand_mode) {
+ $query .= ' HAVING SUM(qty) > 0';
+ }
my $results = selectall_hashref_query($::form, SL::DB->client->dbh, $query, @values);
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};
for my $chunk (@sorted_results) {
my $qty = min($chunk->{qty}, $rest_qty);
+
+ # since allocate operates on stock, this also ensures that no negative stock results are used
if ($qty > 0) {
push @allocations, SL::Helper::Inventory::Allocation->new(
parts_id => $chunk->{parts_id},
}
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)),
+ code => 'not enough to allocate',
+ message => 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}) {
my $part = $params{part} or Carp::croak('allocate needs a part');
my $qty = $params{qty} or Carp::croak('allocate needs a qty');
+ my $wh = $params{warehouse};
+ my $wh_strict = $::instance_conf->get_produce_assembly_same_warehouse;
+ my $consume_service = $::instance_conf->get_produce_assembly_transfer_service;
- Carp::croak('not an assembly') unless $part->is_assembly;
+ Carp::croak('not an assembly') unless $part->is_assembly;
+ Carp::croak('No warehouse selected') if $wh_strict && !$wh;
my %parts_to_allocate;
for my $assembly ($part->assemblies) {
- next if $assembly->part->dispotype eq 'no_stock';
-
- my $tmpqty = $assembly->assembly_part->is_recipe ? $assembly->qty * $qty / $assembly->assembly_part->scalebasis
- : $assembly->part->unit eq 'Stck' ? ceil($assembly->qty * $qty)
- : $assembly->qty * $qty;
+ next if $assembly->part->type eq 'service' && !$consume_service;
$parts_to_allocate{ $assembly->part->id } //= 0;
- $parts_to_allocate{ $assembly->part->id } += $tmpqty;
+ $parts_to_allocate{ $assembly->part->id } += $assembly->qty * $qty;
}
my @allocations;
for my $part_id (keys %parts_to_allocate) {
my $part = SL::DB::Part->load_cached($part_id);
push @allocations, allocate(%params, part => $part, qty => $parts_to_allocate{$part_id});
+ if ($wh_strict) {
+ die SL::X::Inventory::Allocation->new(
+ code => "wrong warehouse for part",
+ message => t8('Part #1 exists in warehouse #2, but not in warehouse #3 ',
+ $part->partnumber . ' ' . $part->description,
+ SL::DB::Manager::Warehouse->find_by(id => $allocations[-1]->{warehouse_id})->description,
+ $wh->description),
+ ) unless $allocations[-1]->{warehouse_id} == $wh->id;
+ }
}
@allocations;
if ('CODE' eq ref $constraints) {
if (!$constraints->(@$allocations)) {
die SL::X::Inventory::Allocation->new(
- error => 'allocation constraints failure',
- msg => t8("Allocations didn't pass constraints"),
+ code => 'allocation constraints failure',
+ message => t8("Allocations didn't pass constraints"),
);
}
} else {
$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,
+ code => 'allocation constraints failure',
+ message => $err,
);
}
}
my $part = $params{part} or Carp::croak('produce_assembly needs a part');
my $qty = $params{qty} or Carp::croak('produce_assembly needs a qty');
+ my $bin = $params{bin} or Carp::croak("need target bin");
my $allocations = $params{allocations};
+ my $strict_wh = $::instance_conf->get_produce_assembly_same_warehouse ? $bin->warehouse : undef;
if ($params{auto_allocate}) {
Carp::croak("produce_assembly: can't have both allocations and auto_allocate") if $params{allocations};
- $allocations = [ allocate_for_assembly(part => $part, qty => $qty) ];
+ $allocations = [ allocate_for_assembly(part => $part, qty => $qty, warehouse => $strict_wh, chargenumber => $params{chargenumber}) ];
} else {
Carp::croak("produce_assembly: need allocations or auto_allocate to produce something") if !$params{allocations};
$allocations = $params{allocations};
}
- my $bin = $params{bin} or Carp::croak("need target bin");
- my $chargenumber = $params{chargenumber};
- my $bestbefore = $params{bestbefore};
+ my $chargenumber = $params{chargenumber};
+ my $bestbefore = $params{bestbefore};
my $for_object_id = $params{for_object_id};
- my $comment = $params{comment} // '';
-
- my $production_order_item = $params{production_order_item};
- my $invoice = $params{invoice};
- my $project = $params{project};
-
- my $shippingdate = $params{shippingsdate} // DateTime->now_local;
+ my $comment = $params{comment} // '';
+ my $invoice = $params{invoice};
+ my $project = $params{project};
+ my $shippingdate = $params{shippingsdate} // DateTime->now_local;
+ my $trans_id = $params{trans_id};
- my $trans_id = $params{trans_id};
($trans_id) = selectrow_query($::form, SL::DB->client->dbh, qq|SELECT nextval('id')| ) unless $trans_id;
my $trans_type_out = SL::DB::Manager::TransferType->find_by(direction => 'out', description => 'used');
- my $trans_type_in = SL::DB::Manager::TransferType->find_by(direction => 'in', description => 'assembled');
+ my $trans_type_in = SL::DB::Manager::TransferType->find_by(direction => 'in', description => 'assembled');
# check whether allocations are sane
if (!$params{no_check_allocations} && !$params{auto_allocate}) {
my %allocations_by_part = map { $_->parts_id => $_->qty } @$allocations;
for my $assembly ($part->assemblies) {
- $allocations_by_part{ $assembly->parts_id } -= $assembly->qty * $qty; # TODO recipe factor
+ $allocations_by_part{ $assembly->parts_id } -= $assembly->qty * $qty;
}
- die "allocations are insufficient for production" if any { $_ < 0 } values %allocations_by_part;
+ die SL::X::Inventory::Allocation->new(
+ code => "allocations are insufficient for production",
+ message => t8('can not allocate enough resources for production'),
+ ) if any { $_ < 0 } values %allocations_by_part;
}
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,
+ comment => t8('Used for assembly #1 #2', $part->partnumber, $part->description),
);
}
project => $project,
invoice => $invoice,
comment => $comment,
- prod => $production_order_item,
employee => SL::DB::Manager::Employee->current,
oe_id => $for_object_id,
);
@transfers;
}
-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;
- }
+sub default_show_bestbefore {
+ $::instance_conf->get_show_bestbefore
}
1;
# 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
- production_order_item => $item,
);
=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.
The first problem has spawned several dozen small functions in the program that
try to implement that, and those usually miss some details. They may ignore
-reservations, or bestbefore times.
+bestbefore times, comments, ignore negative quantities etc.
To get this cleaned up a bit this code introduces two concepts: stock and onhand.
-Stock is defined as the actual contents of the inventory, everything that is
-there. Onhand is what is available, which means things that are stocked
-and not expired.
+=over 4
+
+=item * Stock is defined as the actual contents of the inventory, everything that is
+there.
+
+=item * Onhand is what is available, which means things that are stocked,
+not expired and not in any other way reserved for other uses.
+
+=back
The two new functions C<get_stock> and C<get_onhand> encapsulate these principles and
allow simple access with some optional filters for chargenumbers or warehouses.
Both of them have a batch mode that can be used to get these information to
-supllement smiple reports.
+supplement simple reports.
To address the safe assembly creation a new function has been added.
C<allocate> will try to find the requested quantity of a part in the inventory
and will return allocations of it which can then be used to create the
assembly. Allocation will happen with the C<onhand> semantics defined above,
-meaning that by default no reservations or expired goods will be used. The
-caller can supply hints of what shold be used and in those cases chargenumber
-and reservations will be used up as much as possible first. C<allocate> will
-always try to fulfil the request even beyond those. Should the required amount
-not be stocked, allocate will throw an exception.
+meaning that by default no expired goods will be used. The caller can supply
+hints of what shold be used and in those cases chargenumbers will be used up as
+much as possible first. C<allocate> will always try to fulfil the request even
+beyond those. Should the required amount not be stocked, allocate will throw an
+exception.
C<produce_assembly> has been rewritten to only accept parameters about the
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
=item *
-If you need actual inventory objects because of record links, prod_id links or
-something like that load them directly. And strongly consider redesigning that,
-because it's really fragile.
+If you need actual inventory objects because of record links or something like
+that load them directly. And strongly consider redesigning that, because it's
+really fragile.
=item *
Returns for single parts how much is available in the inventory. That excludes
stock with expired bestbefore.
-It takes all options of L</get_stock> and has some additional ones:
+It takes the same options as L</get_stock>.
=over 4
=item * bestbefore
+If given, will only return stock with a bestbefore at or after the given date.
+Optional. Must be L<DateTime> object.
+
=back
=item * allocate PARAMS
=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
not be completed. The usual reason will be insufficient onhand to allocate, or
insufficient allocations to process the request.
+=head1 KNOWN PROBLEMS
+
+ * It's not currently possible to identify allocations between requests, for
+ example for presenting the user possible allocations and then actually using
+ them on the next request.
+ * It's not currently possible to give C<allocate> prior constraints.
+ Currently all constraints are treated as hints (and will be preferred) but
+ 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, oe
- * clean up allocation helper class
- * with objects for reservations
+ * handle extra arguments in produce: shippingdate, project
* 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