@@ -, +, @@ * Create a new table 'aqorders_claims' (id, ordernumber, claimed_on) * Remove the two columns from the aqorders table: claims_count and claimed_date * delay * Estimated delivery date from/to * Vendor --- C4/Acquisition.pm | 2 - Koha/Acquisition/Basket.pm | 51 +++++++++++ Koha/Acquisition/Order.pm | 57 +++++++++++++ Koha/Acquisition/Order/Claim.pm | 2 + Koha/Acquisition/Order/Claims.pm | 6 +- Koha/Acquisition/Orders.pm | 99 ++++++++++++++++++++++ acqui/basket.pl | 1 + acqui/lateorders-export.pl | 6 +- acqui/lateorders.pl | 38 ++++----- .../intranet-tmpl/prog/en/modules/acqui/basket.tt | 6 ++ .../prog/en/modules/acqui/csv/lateorders.tt | 4 +- .../prog/en/modules/acqui/lateorders.tt | 70 +++++++-------- .../prog/en/modules/acqui/showorder.tt | 4 +- t/db_dependent/Acquisition.t | 23 +++-- 14 files changed, 293 insertions(+), 76 deletions(-) --- a/C4/Acquisition.pm +++ a/C4/Acquisition.pm @@ -1222,8 +1222,6 @@ sub GetOrder { biblioitems.publishercode, aqorders.rrp AS unitpricesupplier, aqorders.ecost AS unitpricelib, - aqorders.claims_count AS claims_count, - aqorders.claimed_date AS claimed_date, aqbudgets.budget_name AS budget, aqbooksellers.name AS supplier, aqbooksellers.id AS supplierid, --- a/Koha/Acquisition/Basket.pm +++ a/Koha/Acquisition/Basket.pm @@ -20,6 +20,7 @@ package Koha::Acquisition::Basket; use Modern::Perl; use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); use Koha::Acquisition::BasketGroups; use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields ); @@ -71,6 +72,56 @@ sub effective_create_items { return $self->create_items || C4::Context->preference('AcqCreateItem'); } +=head3 estimated_delivery_date + +my $estimated_delivery_date = $basket->estimated_delivery_date; + +Return the estimated delivery date for this basket. + +It is calculated adding the delivery time of the vendor to the close date of this basket. + +Return implicit undef if the basket is not closed, or the vendor does not have a delivery time. + +=cut + +sub estimated_delivery_date { + my ( $self ) = @_; + return unless $self->closedate and $self->bookseller->deliverytime; + return dt_from_string($self->closedate)->add( days => $self->bookseller->deliverytime); +} + +=head3 late_since_days + +my $number_of_days_late = $basket->late_since_days; + +Return the number of days the basket is late. + +Return implicit undef if the basket is not closed. + +=cut + +sub late_since_days { + my ( $self ) = @_; + return unless $self->closedate; + return dt_from_string->delta_days(dt_from_string($self->closedate))->delta_days(); +} + +=head3 authorizer + +my $authorizer = $basket->authorizer; + +Returns the patron who authorized/created this basket. + +=cut + +sub authorizer { + my ($self) = @_; + # FIXME We should use a DBIC rs, but the FK is missing + return unless $self->authorisedby; + return scalar Koha::Patrons->find($self->authorisedby); +} + + =head3 to_api my $json = $basket->to_api; --- a/Koha/Acquisition/Order.pm +++ a/Koha/Acquisition/Order.pm @@ -22,6 +22,7 @@ use Carp qw( croak ); use Koha::Acquisition::Baskets; use Koha::Acquisition::Funds; use Koha::Acquisition::Invoices; +use Koha::Acquisition::Order::Claims; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Biblios; @@ -200,6 +201,62 @@ sub biblio { return Koha::Biblio->_new_from_dbic( $biblio_rs ); } +=head3 claims + + my $claims = $order->claims + +Return the claims history for this order + +=cut + +sub claims { + my ( $self ) = @_; + my $claims_rs = $self->_result->aqorders_claims; + return Koha::Acquisition::Order::Claims->_new_from_dbic( $claims_rs ); +} + +=head3 claim + + my $claim = $order->claim + +Do claim for this order + +=cut + +sub claim { + my ( $self ) = @_; + my $claim_rs = $self->_result->create_related('aqorders_claims', {}); + return Koha::Acquisition::Order::Claim->_new_from_dbic($claim_rs); +} + +=head3 claims_count + +my $nb_of_claims = $order->claims_count; + +This is the equivalent of $order->claims->count. Keeping it for retrocompatibilty. + +=cut + +sub claims_count { + my ( $self ) = @_; + return $self->claims->count; +} + +=head3 claimed_date + +my $last_claim_date = $order->claimed_date; + +This is the equivalent of $order->claims->last->claimed_on. Keeping it for retrocompatibilty. + +=cut + +sub claimed_date { + my ( $self ) = @_; + my $last_claim = $self->claims->last; + return unless $last_claim; + return $last_claim->claimed_on; +} + =head3 duplicate_to my $duplicated_order = $order->duplicate_to($basket, [$default_values]); --- a/Koha/Acquisition/Order/Claim.pm +++ a/Koha/Acquisition/Order/Claim.pm @@ -31,6 +31,8 @@ Koha::Acquisition::Order::Claim - Koha Claim Object class =head2 Class methods +=cut + =head2 Internal methods =head3 _type --- a/Koha/Acquisition/Order/Claims.pm +++ a/Koha/Acquisition/Order/Claims.pm @@ -35,7 +35,7 @@ Koha::Cities - Koha Claim Object set class =cut -=head3 type +=head3 _type =cut @@ -43,6 +43,10 @@ sub _type { return 'AqordersClaim'; } +=head3 object_class + +=cut + sub object_class { return 'Koha::Acquisition::Order::Claim'; } --- a/Koha/Acquisition/Orders.pm +++ a/Koha/Acquisition/Orders.pm @@ -21,6 +21,7 @@ use Carp; use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); use Koha::Acquisition::Order; use base qw(Koha::Objects); @@ -31,6 +32,104 @@ Koha::Acquisition::Orders object set class =head1 API +=head2 Class Methods + +=head3 filter_by_lates + +my $late_orders = $orders->filter_by_lates($params); + +Filter an order set given different parameters. + +This is the équivalent method of the former GetLateOrders C4 subroutine + +$params can be: + +=over + +=item C the number of days the basket has been closed + +=item C the bookseller id + +=item C Beginning of the estimated delivery date + +=item C End of the estimated delivery date + +=back + +=cut + +sub filter_by_lates { + my ( $self, $params ) = @_; + my $delay = $params->{delay}; + my $bookseller_id = $params->{bookseller_id}; + # my $branchcode = $params->{branchcode}; # FIXME do we really need this + my $estimated_from = $params->{estimated_from}; + my $estimated_to = $params->{estimated_to}; + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + my @delivery_time_conditions; + my $date_add = "DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)"; + if ( defined $estimated_from or defined $estimated_to ) { + push @delivery_time_conditions, \[ "$date_add IS NOT NULL" ]; + } + if ( defined $estimated_from ) { + push @delivery_time_conditions, \[ "$date_add >= ?", $dtf->format_date($estimated_from) ]; + } + if ( defined $estimated_to ) { + push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date($estimated_to) ]; + } + if ( defined $estimated_from and not defined $estimated_to ) { + push @delivery_time_conditions, \[ "$date_add <= ?", $dtf->format_date(dt_from_string) ]; + } + + $self->search( + { + -or => [ + { datereceived => undef }, + quantityreceived => { '<' => \'quantity' } + ], + 'basketno.closedate' => [ + -and => + { '!=' => undef }, + { + defined $delay + ? ( + '<=' => $dtf->format_date( + dt_from_string->subtract( days => $delay ) + ) + ) + : () + } + ], + 'datecancellationprinted' => undef, + ( + $bookseller_id + ? ( 'basketno.booksellerid' => $bookseller_id ) + : () + ), + + # ( $branchcode ? ('borrower.branchcode')) # FIXME branch is not a filter we may not need to implement this + + ( @delivery_time_conditions ? ( -and => \@delivery_time_conditions ) : ()), + ( + C4::Context->preference('IndependentBranches') + && !C4::Context->IsSuperLibrarian + ? ( 'borrower.branchcode' => C4::Context->userenv->{branch} ) + : () + ), + + ( orderstatus => { '!=' => 'cancelled' } ), + + }, + { + '+select' => [\"DATE_ADD(basketno.closedate, INTERVAL COALESCE(booksellerid.deliverytime, booksellerid.deliverytime, 0) day)"], + '+as' => ['estimated_delivery_date'], + join => { 'basketno' => 'booksellerid' }, + prefetch => {'basketno' => 'booksellerid'}, + } + ); +} + =head2 Internal methods =head3 _type (internal) --- a/acqui/basket.pl +++ a/acqui/basket.pl @@ -498,6 +498,7 @@ sub get_order_infos { $line{left_holds_on_order} = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds ); $line{holds} = $holds_count; $line{holds_on_order} = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order}; + $line{order_object} = $order; } --- a/acqui/lateorders-export.pl +++ a/acqui/lateorders-export.pl @@ -36,6 +36,8 @@ my @ordernumbers = $input->multi_param('ordernumber'); my @orders; for my $ordernumber ( @ordernumbers ) { my $order = GetOrder $ordernumber; + my $order_object = Koha::Acquisition::Orders->find($ordernumber); + my $claims = $order_object->claims; push @orders, { orderdate => $order->{orderdate}, latesince => $order->{latesince}, @@ -51,8 +53,8 @@ for my $ordernumber ( @ordernumbers ) { budget => $order->{budget}, basketname => $order->{basketname}, basketno => $order->{basketno}, - claims_count => $order->{claims_count}, - claimed_date => $order->{claimed_date}, + claims_count => $claims->count, + claimed_date => $claims->count ? $claims->last->claimed_on : undef, internalnote => $order->{order_internalnote}, vendornote => $order->{order_vendornote}, isbn => $order->{isbn}, --- a/acqui/lateorders.pl +++ a/acqui/lateorders.pl @@ -108,7 +108,7 @@ if ($op and $op eq "send_alert"){ eval { $err = SendAlerts( 'claimacquisition', \@ordernums, $input->param("letter_code") ); if ( not ref $err or not exists $err->{error} ) { - AddClaim ( $_ ) for @ordernums; + Koha::Acquisition::Orders->find($_)->claim() for @ordernums; } }; @@ -145,34 +145,32 @@ $template->param(SUPPLIER_LOOP => \@sloopy); $template->param(Supplier=>$supplierlist{$booksellerid}) if ($booksellerid); $template->param(booksellerid=>$booksellerid) if ($booksellerid); -@parameters = - ( $delay, $booksellerid, $branch ); -if ($estimateddeliverydatefrom_dt) { - push @parameters, $estimateddeliverydatefrom_dt->ymd(); -} -else { - push @parameters, undef; -} -if ($estimateddeliverydateto_dt) { - push @parameters, $estimateddeliverydateto_dt->ymd(); -} -my @lateorders = GetLateOrders( @parameters ); - -my $total; -foreach (@lateorders){ - $total += $_->{subtotal}; -} +my $lateorders = Koha::Acquisition::Orders->filter_by_lates( + { + delay => $delay, + booksellerid => $booksellerid, + ( + $estimateddeliverydatefrom_dt + ? ( estimated_from => $estimateddeliverydatefrom_dt->ymd ) + : () + ), + ( + $estimateddeliverydateto_dt + ? ( estimated_to => $estimateddeliverydateto_dt->ymd ) + : () + ) + } +); my $letters = GetLetters({ module => "claimacquisition" }); $template->param(ERROR_LOOP => \@errors) if (@errors); $template->param( - lateorders => \@lateorders, + lateorders => $lateorders, delay => $delay, letters => $letters, estimateddeliverydatefrom => $estimateddeliverydatefrom, estimateddeliverydateto => $estimateddeliverydateto, - total => $total, intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"), ); output_html_with_http_headers $input, $cookie, $template->output; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt @@ -524,6 +524,12 @@

[% END %] + [% SET claims = books_loo.order_object.claims %] + [% IF claims.count %] +

+ This order has been claimed [% claims.count | html %] times. On [% FOR c IN claims %][% c.claimed_on | $KohaDates %][% UNLESS loop.last %], [% END %][% END %] +

+ [% END %] [% SET zero_regex = "^0{1,}\.?0{1,}[^1-9]" %] [%# 0 or 0.0 or 0.00 or 00 or 00.0 or 00.00 or 0.000 ... %] [%# FIXME: use of a regexp is not ideal; bugs 9410 and 10929 suggest better way of handling this %] --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/lateorders.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/lateorders.tt @@ -6,14 +6,14 @@ [%- INCLUDE empty_line.inc -%] [%- FOREACH o IN orders -%] -"[% o.orderdate | html %] ([% o.latesince | html %] days)"[%- delimiter | html -%] +"[% o.orderdate | $KohaDates %] ([% o.latesince | html %] days)"[%- delimiter | html -%] "[% o.estimateddeliverydate | $KohaDates %]"[%- delimiter | html -%] "[% o.supplier (o.supplierid) | html %]"[%- delimiter | html -%] "[% o.title | html %] [% IF o.author %]Author: [% o.author | html %].[% END %][% IF o.publisher %]Published by: [% o.publisher | html %].[% END %]"[%- delimiter | html -%] "[% o.unitpricesupplier | html %] x [% o.quantity_to_receive | html %] = [% o.subtotal | html %] ([% o.budget | html %])"[%- delimiter | html -%] "[% o.basketname | html %] ([% o.basketno | html %])"[%- delimiter | html -%] "[% o.claims_count | html %]"[%- delimiter | html -%] -"[% o.claimed_date | html %]"[%- delimiter | html -%] +"[% o.claimed_date | $KohaDates %]"[%- delimiter | html -%] "[% o.internalnote | html %]"[%- delimiter | html -%] "[% o.vendornote | html %]"[%- delimiter | html -%] "[% o.isbn | html %]" --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/lateorders.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/lateorders.tt @@ -3,6 +3,7 @@ [% USE KohaDates %] [% USE Branches %] [% USE ColumnsSettings %] +[% USE Price %] [% SET footerjs = 1 %] [% INCLUDE 'doc-head-open.inc' %] Koha › Acquisitions › Late orders @@ -35,7 +36,7 @@ [% IF info_claim %]
Email has been sent.
[% END %] -[% IF ( lateorders ) %] +[% IF lateorders.count %]
@@ -48,6 +49,7 @@

[% END %] + [% SET total = 0 %] @@ -77,71 +79,69 @@ [% FOREACH lateorder IN lateorders %] - - - + - + [% END %] - + --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/showorder.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/showorder.tt @@ -17,11 +17,11 @@
  • Claims count: - [% order.claims_count | html %] + [% order.claims.count | html %]
  • Last claim date: - [% order.claimed_date | html %] + [% order.claims.last.claimed_on | html %]
  • --- a/t/db_dependent/Acquisition.t +++ a/t/db_dependent/Acquisition.t @@ -394,16 +394,16 @@ is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumb ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid, "GetBudgetByOrderNumber returns expected budget" ); -my @lateorders = GetLateOrders(0); -is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ), +my $lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 }); +is( $lateorders->search({ 'me.basketno' => $basketno })->count, 0, "GetLateOrders does not get orders from opened baskets" ); C4::Acquisition::CloseBasket($basketno); -@lateorders = GetLateOrders(0); -isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ), +$lateorders = Koha::Acquisition::Orders->filter_by_lates({ delay => 0 }); +isnt( $lateorders->search({ 'me.basketno' => $basketno })->count, 0, "GetLateOrders gets orders from closed baskets" ); -ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ), +is( $lateorders->search({ ordernumber => $ordernumbers[3] })->count, 0, "GetLateOrders does not get cancelled orders" ); -ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ), +is( $lateorders->search({ ordernumber => $ordernumbers[4] })->count, 0, "GetLateOrders does not get received orders" ); $search_orders = SearchOrders({ @@ -418,13 +418,12 @@ is( scalar (@$search_orders), 4, "SearchOrders with pending and ordered params g # Test AddClaim # -my $order = $lateorders[0]; -AddClaim( $order->{ordernumber} ); -my $neworder = GetOrder( $order->{ordernumber} ); +my $order = $lateorders->next; +$order->claim(); is( - $neworder->{claimed_date}, + output_pref({ str => $order->claimed_date, dateformat => 'iso', dateonly => 1 }), strftime( "%Y-%m-%d", localtime(time) ), - "AddClaim : Check claimed_date" + "Koha::Acquisition::Order->claim: Check claimed_date" ); my $order2 = Koha::Acquisition::Orders->find( $ordernumbers[1] )->unblessed; @@ -452,7 +451,7 @@ is( "ModReceiveOrder only changes the supplied orders internal notes" ); -$neworder = GetOrder($new_ordernumber); +my $neworder = GetOrder($new_ordernumber); is( $neworder->{'quantity'}, 2, '2 items on new order' ); is( $neworder->{'quantityreceived'}, 2, 'Splitting up order received items on new order' ); --
    - + [% lateorder.ordernumber | $raw %] - [% lateorder.orderdate | $KohaDates %] ([% lateorder.latesince | html %] days) + [% lateorder.basket.closedate | $KohaDates %] ([% lateorder.basket.late_since_days | html %] days) - [% IF ( lateorder.estimateddeliverydate ) %] - [% lateorder.estimateddeliverydate | $KohaDates %] - [% ELSE %] - + [% SET estimated_delivery_date = lateorder.get_column('estimated_delivery_date') %] + [% IF estimated_delivery_date %] + [% estimated_delivery_date | $KohaDates %] [% END %] - [% lateorder.supplier | html %] - ([% lateorder.supplierid | html %]) + [% lateorder.basket.bookseller.name | html %] + ([% lateorder.basket.bookseller.id | html %]) - [% lateorder.title | html %] - [% IF ( lateorder.author ) %]
    Author: [% lateorder.author | html %][% END %] - [% IF ( lateorder.publisher ) %] -
    Published by: [% lateorder.publisher | html %] - [% IF ( lateorder.publicationyear ) %] - in [% lateorder.publicationyear | html %] + [% lateorder.biblio.title | html %] + [% IF ( lateorder.biblio.author ) %]
    Author: [% lateorder.biblio.author | html %][% END %] + [% IF ( lateorder.biblio.biblioitem.publishercode ) %] +
    Published by: [% lateorder.biblio.biblioitem.publishercode | html %] + [% IF ( lateorder.biblio.biblioitem.publicationyear ) %] + in [% lateorder.biblio.biblioitem.publicationyear | html %] [% END %] [% END %]
    - [% lateorder.unitpricesupplier | html %]x[% lateorder.quantity | html %] = - [% lateorder.subtotal | html %] + [% SET subtotal = (lateorder.quantity - lateorder.quantityreceived) * lateorder.rrp %] + [% SET total = total + subtotal %] + [% lateorder.rrp | html %]x[% lateorder.quantity - lateorder.quantityreceived | html %] = [% subtotal | $Price %] [% IF ( CAN_user_acquisition_order_manage ) %] - [% lateorder.basketname | html %] ([% lateorder.basketno | html %]) + [% lateorder.basket.basketname | html %] ([% lateorder.basketno | html %]) [% ELSE %] - [% lateorder.basketname | html %] ([% lateorder.basketno | html %]) + [% lateorder.basket.basketname | html %] ([% lateorder.basketno | html %]) [% END %] - [% IF ( lateorder.basketgroupid ) %] + [% IF ( lateorder.basket.basketgroupid ) %] [% IF ( CAN_user_acquisition_group_manage ) %] - [% lateorder.basketgroupname | html %] ([% lateorder.basketgroupid | html %]) + [% lateorder.basket.basket_group.name | html %] ([% lateorder.basket.basketgroupid | html %]) [% ELSE %] - [% lateorder.basketgroupname | html %] ([% lateorder.basketgroupid | html %]) + [% lateorder.basket.basket_group.name | html %] ([% lateorder.basket.basketgroupid | html %]) [% END %] [% END %] [% Branches.GetName( lateorder.branch ) | html %] + [% Branches.GetName( lateorder.basket.authorizer.branchcode ) | html %] [% lateorder.budget | html %] + [% lateorder.fund.budget_name | html %] [% lateorder.claims_count | html %][% lateorder.claims.count | html %] - [% IF ( lateorder.claimed_date ) %] - [% lateorder.claimed_date | $KohaDates %] - [% ELSE %] - + [% FOR claim IN lateorder.claims %] + [% claim.claimed_on | $KohaDates %] [% END %] - [% IF ( lateorder.internalnote ) %] + [% IF lateorder.order_internalnote %]

    - [% lateorder.internalnote | html %] + [% lateorder.order_internalnote | html %] Edit internal note @@ -153,9 +153,9 @@ [% END %]

    - [% IF ( lateorder.vendornote ) %] + [% IF lateorder.order_vendornote %]

    - [% lateorder.vendornote | html %] + [% lateorder.order_vendornote | html %] Edit vendor note @@ -166,14 +166,14 @@ [% END %]

    [% lateorder.isbn | $raw %][% lateorder.biblio.biblioitem.isbn | $raw %]
    Total[% total | html %][% total | $Price %]