From c9adf7479023f1fb2499ee37bef5f5c358c2a682 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 5 Feb 2014 17:13:31 +0100 Subject: [PATCH] Bug 7290: More granular permissions for order receiving Add branch to aqorders. Add subpermission acquisition => order_receive_all Users with order_receive_all can receive all orders. Users with only order_receive can receive an order if: - order branch is defined and is the same as the current working branch - order branch is not defined, basket branch is defined and basket branch is the same as the current working branch - order branch and basket branch are not defined Use dataTables filters possibilities instead of custom JS solution in parcel.pl Unit tests are available in t/Acquisition/CanUserReceiveBasket.t --- C4/Acquisition.pm | 71 +++++++++++++ acqui/addorder.pl | 1 + acqui/finishreceive.pl | 10 +- acqui/neworderempty.pl | 5 +- acqui/orderreceive.pl | 12 +++ acqui/parcel.pl | 12 ++- .../data/mysql/de-DE/mandatory/userpermissions.sql | 1 + .../data/mysql/en/mandatory/userpermissions.sql | 1 + .../data/mysql/es-ES/mandatory/userpermissions.sql | 1 + .../mysql/fr-FR/1-Obligatoire/userpermissions.sql | 1 + .../data/mysql/it-IT/necessari/userpermissions.sql | 1 + installer/data/mysql/kohastructure.sql | 3 + .../mysql/nb-NO/1-Obligatorisk/userpermissions.sql | 1 + .../data/mysql/pl-PL/mandatory/userpermissions.sql | 1 + .../ru-RU/mandatory/permissions_and_user_flags.sql | 1 + .../uk-UA/mandatory/permissions_and_user_flags.sql | 1 + installer/data/mysql/updatedatabase.pl | 16 +++ .../prog/en/modules/acqui/neworderempty.tt | 19 ++++ .../prog/en/modules/acqui/orderreceive.tt | 5 + .../intranet-tmpl/prog/en/modules/acqui/parcel.tt | 26 ++++- t/Acquisition/CanUserReceiveBasket.t | 111 ++++++++++++++++++++ 21 files changed, 289 insertions(+), 11 deletions(-) create mode 100644 t/Acquisition/CanUserReceiveBasket.t diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 33aa8e3..3295f96 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -50,6 +50,7 @@ BEGIN { &GetBasketUsers &ModBasketUsers &CanUserManageBasket + &CanUserReceiveOrder &ModBasketHeader @@ -798,6 +799,76 @@ sub CanUserManageBasket { return 1; } +=head3 CanUserReceiveOrder + + my $bool = CanUserReceiveOrder($borrower, $order[, $userflags]); + my $bool = CanUserReceiveOrder($borrowernumber, $orderno[, $userflags]); + +Check if a borrower can receive a order, according to user permissions, current +working branch and order and basket branches. + +First parameter can be either a borrowernumber or a hashref as returned by +C4::Members::GetMember. + +Second parameter can be either a ordernumber or a hashref as returned by +C4::Acquisition::GetOrder. + +The third parameter is optional. If given, it should be a hashref as returned +by C4::Auth::getuserflags. If not, getuserflags is called. + +If user is authorised to receive order, returns 1. +Otherwise returns 0. + +=cut + +sub CanUserReceiveOrder { + my ($borrower, $order, $userflags) = @_; + + if (!ref $borrower) { + $borrower = C4::Members::GetMember(borrowernumber => $borrower); + } + if (!ref $order) { + $order = GetOrder($order); + } + + return 0 unless ($borrower and $order); + + if (!defined $userflags) { + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE borrowernumber = ?"); + $sth->execute($borrower->{borrowernumber}); + my ($flags) = $sth->fetchrow_array; + $sth->finish; + + $userflags = C4::Auth::getuserflags($flags, $borrower->{userid}, $dbh); + } + + my $branch = C4::Context->userenv->{'branch'}; + + unless ($userflags->{superlibrarian} + || (!ref $userflags->{acquisition} && $userflags->{acquisition}) + || (ref $userflags->{acquisition} && $userflags->{acquisition}->{order_receive_all}) ) + { + if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) { + return 0; + } + if (ref $userflags->{acquisition} && !$userflags->{acquisition}->{order_receive}) { + return 0; + } + if (defined $order->{branch} && $order->{branch} ne $branch) { + return 0; + } + if (!defined $order->{branch}) { + my $basket = GetBasket($order->{basketno}); + if (!$basket || (defined $basket->{branch} && $basket->{branch} ne $branch)) { + return 0; + } + } + } + + return 1; +} + #------------------------------------------------------------# =head3 GetBasketsByBasketgroup diff --git a/acqui/addorder.pl b/acqui/addorder.pl index 1b05f07..69d9fdb 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -226,6 +226,7 @@ my $orderinfo = $input->Vars; $orderinfo->{'list_price'} ||= 0; $orderinfo->{'uncertainprice'} ||= 0; $orderinfo->{subscriptionid} ||= undef; +$orderinfo->{branch} = undef if (defined $orderinfo->{branch} and $orderinfo->{branch} eq ''); my $user = $input->remote_user; diff --git a/acqui/finishreceive.pl b/acqui/finishreceive.pl index 71bf90c..d0af779 100755 --- a/acqui/finishreceive.pl +++ b/acqui/finishreceive.pl @@ -36,7 +36,8 @@ use List::MoreUtils qw/any/; my $input=new CGI; my $flagsrequired = {acquisition => 'order_receive'}; -checkauth($input, 0, $flagsrequired, 'intranet'); +my ($userid, $cookie, undef, $userflags) + = checkauth($input, 0, $flagsrequired, 'intranet'); my $user = $input->remote_user; my $biblionumber = $input->param('biblionumber'); @@ -58,6 +59,13 @@ my $bookfund = $input->param("bookfund"); my $order = GetOrder($ordernumber); my $new_ordernumber = $ordernumber; +my $borrower = C4::Members::GetMember(userid => $userid); +unless (CanUserReceiveOrder($borrower, $ordernumber, $userflags)) { + my $error = "cannot_receive_order"; + print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid&error=$error"); + exit; +} + #need old recievedate if we update the order, parcel.pl only shows the right parcel this way FIXME if ($quantityrec > $origquantityrec ) { my @received_items = (); diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl index 9fdf032..6ef0b6d 100755 --- a/acqui/neworderempty.pl +++ b/acqui/neworderempty.pl @@ -66,8 +66,7 @@ the item's id in the breeding reservoir =cut -use warnings; -use strict; +use Modern::Perl; use CGI; use C4::Context; use C4::Input; @@ -241,7 +240,7 @@ foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$ value => $thisbranch, branchname => $branches->{$thisbranch}->{'branchname'}, ); - $row{'selected'} = 1 if( $thisbranch && $data->{branchcode} && $thisbranch eq $data->{branchcode}) ; + $row{'selected'} = 1 if( $thisbranch && $data->{branch} && $thisbranch eq $data->{branch}) ; push @branchloop, \%row; } $template->param( branchloop => \@branchloop ); diff --git a/acqui/orderreceive.pl b/acqui/orderreceive.pl index 2a21c14..dce7af8 100755 --- a/acqui/orderreceive.pl +++ b/acqui/orderreceive.pl @@ -114,6 +114,18 @@ unless ( $results and @$results) { # prepare the form for receiving my $order = $results->[0]; +unless (CanUserReceiveOrder($loggedinuser, $order, $userflags)) { + $template->param( + cannot_receive_order => 1, + ordernumber => $ordernumber, + invoice => $invoice, + booksellerid => $booksellerid, + name => $bookseller->{name} + ); + output_html_with_http_headers $input, $cookie, $template->output; + exit; +} + # Check if ACQ framework exists my $acq_fw = GetMarcStructure(1, 'ACQ'); unless($acq_fw) { diff --git a/acqui/parcel.pl b/acqui/parcel.pl index 557030c..75efeae 100755 --- a/acqui/parcel.pl +++ b/acqui/parcel.pl @@ -54,8 +54,7 @@ To filter the results list on this given date. =cut -use strict; -use warnings; +use Modern::Perl; use C4::Auth; use C4::Acquisition; @@ -110,7 +109,7 @@ sub get_gst { : $value * ( 1 + $gstrate ) - $value; } -my ($template, $loggedinuser, $cookie) +my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({template_name => "acqui/parcel.tmpl", query => $input, type => "intranet", @@ -305,6 +304,8 @@ unless( defined $invoice->{closedate} ) { $line{holds} = $holds; $line{holds_on_order} = $itemholds?$itemholds:$holds if $line{left_holds_on_order}; + $line{can_receive} = CanUserReceiveOrder($loggedinuser, $pendingorders->[$i], $userflags); + my $budget = GetBudget( $line{budget_id} ); $line{budget_name} = $budget->{'budget_name'}; @@ -316,6 +317,11 @@ unless( defined $invoice->{closedate} ) { ); } +my @errors = $input->param('error'); +foreach my $error (@errors) { + $template->param("error_$error" => 1); +} + $template->param( invoiceid => $invoice->{invoiceid}, invoice => $invoice->{invoicenumber}, diff --git a/installer/data/mysql/de-DE/mandatory/userpermissions.sql b/installer/data/mysql/de-DE/mandatory/userpermissions.sql index 9c754ee..c0ad5c5 100644 --- a/installer/data/mysql/de-DE/mandatory/userpermissions.sql +++ b/installer/data/mysql/de-DE/mandatory/userpermissions.sql @@ -21,6 +21,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Bestellungen aller Bibliotheken, unabhängig von Berechtigungen, verwalten'), (11, 'group_manage', 'Bestellgruppen vewalten'), (11, 'order_receive', 'Lieferungen verwalten'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Konten hinzufügen/ändern, aber bestehende nicht ändern'), (11, 'budget_manage_all', 'Alle Konten verwalten'), (13, 'edit_news', 'Nachrichten für OPAC und Dienstoberfläche verfassen'), diff --git a/installer/data/mysql/en/mandatory/userpermissions.sql b/installer/data/mysql/en/mandatory/userpermissions.sql index eb1275c..04a7058 100644 --- a/installer/data/mysql/en/mandatory/userpermissions.sql +++ b/installer/data/mysql/en/mandatory/userpermissions.sql @@ -21,6 +21,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Manage orders & basketgroups'), (11, 'order_receive', 'Manage orders & basket'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Add and delete budgets (but cant modify budgets)'), (11, 'budget_manage_all', 'Manage all budgets'), (13, 'edit_news', 'Write news for the OPAC and staff interfaces'), diff --git a/installer/data/mysql/es-ES/mandatory/userpermissions.sql b/installer/data/mysql/es-ES/mandatory/userpermissions.sql index 2f42224..0805563 100644 --- a/installer/data/mysql/es-ES/mandatory/userpermissions.sql +++ b/installer/data/mysql/es-ES/mandatory/userpermissions.sql @@ -21,6 +21,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Manage orders & basketgroups'), (11, 'order_receive', 'Manage orders & basket'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Add and delete budgets (but cant modify budgets)'), (11, 'budget_manage_all', 'Manage all budgets'), (13, 'edit_news', 'Write news for the OPAC and staff interfaces'), diff --git a/installer/data/mysql/fr-FR/1-Obligatoire/userpermissions.sql b/installer/data/mysql/fr-FR/1-Obligatoire/userpermissions.sql index 702db68..018fa44 100644 --- a/installer/data/mysql/fr-FR/1-Obligatoire/userpermissions.sql +++ b/installer/data/mysql/fr-FR/1-Obligatoire/userpermissions.sql @@ -39,6 +39,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Gérer les commandes et les bons de commande'), (11, 'order_receive', 'Gérer les réceptions'), + (11, 'order_receive_all', 'Gérer toutes les réceptions'), (11, 'budget_add_del', 'Ajouter et supprimer les budgets (mais pas modifier)'), (11, 'budget_manage_all', 'Gérer tous les budgets'), (13, 'manage_csv_profiles', 'Gérer les profils d''export CSV'), diff --git a/installer/data/mysql/it-IT/necessari/userpermissions.sql b/installer/data/mysql/it-IT/necessari/userpermissions.sql index 0d27fc0..6630609 100644 --- a/installer/data/mysql/it-IT/necessari/userpermissions.sql +++ b/installer/data/mysql/it-IT/necessari/userpermissions.sql @@ -23,6 +23,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Gestisci ordini e raccoglitori raggruppati'), (11, 'order_receive', 'Gestisci arrivi'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Aggiungi e cancella budgets (senza modificarli)'), (11, 'budget_manage_all', 'Gestisci tutti i budgets'), (13, 'edit_news', 'Scrivi le news per l\'OPAC e per l\'interfaccia staff'), diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 3cf9c65..aeb349a 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -2996,15 +2996,18 @@ CREATE TABLE `aqorders` ( -- information related to the basket line items `claimed_date` date default NULL, -- last date a claim was generated `subscriptionid` int(11) default NULL, -- links this order line to a subscription (subscription.subscriptionid) parent_ordernumber int(11) default NULL, -- ordernumber of parent order line, or same as ordernumber if no parent + branch varchar(10) default NULL, -- order line branch `orderstatus` varchar(16) default 'new', -- the current status for this line item. Can be 'new', 'ordered', 'partial', 'complete' or 'cancelled' PRIMARY KEY (`ordernumber`), KEY `basketno` (`basketno`), KEY `biblionumber` (`biblionumber`), KEY `budget_id` (`budget_id`), + KEY branch (branch), CONSTRAINT `aqorders_ibfk_1` FOREIGN KEY (`basketno`) REFERENCES `aqbasket` (`basketno`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `aqorders_ibfk_2` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT aqorders_ibfk_3 FOREIGN KEY (invoiceid) REFERENCES aqinvoices (invoiceid) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `aqorders_subscriptionid` FOREIGN KEY (`subscriptionid`) REFERENCES `subscription` (`subscriptionid`) ON DELETE CASCADE ON UPDATE CASCADE + CONSTRAINT aqorders_branch FOREIGN KEY (branch) REFERENCES branches (branchcode) ON UPDATE CASCADE ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/installer/data/mysql/nb-NO/1-Obligatorisk/userpermissions.sql b/installer/data/mysql/nb-NO/1-Obligatorisk/userpermissions.sql index c581bbd..99d4249 100644 --- a/installer/data/mysql/nb-NO/1-Obligatorisk/userpermissions.sql +++ b/installer/data/mysql/nb-NO/1-Obligatorisk/userpermissions.sql @@ -41,6 +41,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Administrere bestillinger og kurv-grupper'), (11, 'order_receive', 'Administrere bestillinger og kurver'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Legge til og slette budsjetter (men ikke endre budsjetter)'), (11, 'budget_manage_all', 'Administrere alle budsjetter'), (13, 'edit_news', 'Legge ut nyhter i OPACen og det interne grensesnittet'), diff --git a/installer/data/mysql/pl-PL/mandatory/userpermissions.sql b/installer/data/mysql/pl-PL/mandatory/userpermissions.sql index dcd8aef..3f1ddd0 100644 --- a/installer/data/mysql/pl-PL/mandatory/userpermissions.sql +++ b/installer/data/mysql/pl-PL/mandatory/userpermissions.sql @@ -21,6 +21,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Manage orders & basketgroups'), (11, 'order_receive', 'Manage orders & basket'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Add and delete budgets (but cant modify budgets)'), (11, 'budget_manage_all', 'Manage all budgets'), (13, 'edit_news', 'RTworzeniei publikowanie wiadomości w interfejsie bibliotekarza i OPAC'), diff --git a/installer/data/mysql/ru-RU/mandatory/permissions_and_user_flags.sql b/installer/data/mysql/ru-RU/mandatory/permissions_and_user_flags.sql index acf904d..66b8240 100644 --- a/installer/data/mysql/ru-RU/mandatory/permissions_and_user_flags.sql +++ b/installer/data/mysql/ru-RU/mandatory/permissions_and_user_flags.sql @@ -47,6 +47,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Manage orders & basketgroups'), (11, 'order_receive', 'Manage orders & basket'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Add and delete budgets (but cant modify budgets)'), (11, 'budget_manage_all', 'Manage all budgets'), (13, 'edit_news', 'Написание новостей для электронного каталога и интерфейса библиотекарей'), diff --git a/installer/data/mysql/uk-UA/mandatory/permissions_and_user_flags.sql b/installer/data/mysql/uk-UA/mandatory/permissions_and_user_flags.sql index adee3ff..a788c42 100644 --- a/installer/data/mysql/uk-UA/mandatory/permissions_and_user_flags.sql +++ b/installer/data/mysql/uk-UA/mandatory/permissions_and_user_flags.sql @@ -47,6 +47,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES (11, 'order_manage_all', 'Manage all orders and baskets, regardless of restrictions on them'), (11, 'group_manage', 'Manage orders & basketgroups'), (11, 'order_receive', 'Manage orders & basket'), + (11, 'order_receive_all', 'Receive all orders'), (11, 'budget_add_del', 'Add and delete budgets (but cant modify budgets)'), (11, 'budget_manage_all', 'Manage all budgets'), (13, 'edit_news', 'Написання новин для електронного каталогу та інтерфейсу бібліотекарів'), diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 062ecc1..09d0cfb 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8083,6 +8083,22 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "XXX"; +if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { + $dbh->do(" + ALTER TABLE aqorders + ADD COLUMN branch VARCHAR(10) DEFAULT NULL, + ADD CONSTRAINT aqorders_ibfk_3 FOREIGN KEY (branch) + REFERENCES branches (branchcode) ON UPDATE CASCADE ON DELETE SET NULL + "); + $dbh->do(" + INSERT INTO permissions (module_bit, code, description) + VALUES (11, 'order_receive_all', 'Receive all orders') + "); + print "Upgrade to $DBversion done (Add aqorders.branch and permission order_receive_all)\n"; + SetVersion($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt index 596aa07..1cbec34 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt @@ -210,6 +210,25 @@ $(document).ready(function()
+ Order details +
    +
  1. + Branch: + +
  2. +
+
+ +
Catalog details [% IF ( biblionumber ) %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt index 47eea31..c9ccfbc 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt @@ -129,6 +129,10 @@

Receive items from : [% name %] [% IF ( invoice ) %][[% invoice %]] [% END %] (order #[% ordernumber %])

+[% IF (cannot_receive_order) %] +

You are not authorised to receive this order.

+[% ELSE %] + [% IF ( count ) %]
@@ -341,6 +345,7 @@ This ordernumber does not exist. [% END %] +[% END %][%# IF (cannot_receive_order) %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt index b537148..673a793 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/parcel.tt @@ -163,11 +163,19 @@
[% IF ( receive_error ) %]
-

Error adding items:

+

Error receiving items:

    - [% FOREACH error_loo IN error_loop %] -
  • [% error_loo.error_param %][% IF ( error_loo.error_duplicate_barcode ) %]Duplicate Barcode[% END %]
  • - [% END %] + [% FOREACH error IN error_loop %] +
  • + [% error.error_param %] + [% IF ( error.error_duplicate_barcode ) %] + Duplicate Barcode + [% ELSIF (error.error_cannot_receive_order) %] + You were not authorised to receive this order + [% END %] + +
  • + [% END %]
[% END %] @@ -214,6 +222,12 @@
[% END %] + [% IF error_cannot_receive_order %] +
+ You cannot receive this order. +
+ [% END %] + [% UNLESS no_orders_to_display %]

Invoice number: [% invoice %] Received by: [% loggedinusername %] On: [% formatteddatereceived %]

@@ -287,7 +301,11 @@ [% loop_order.ordertotal %] [% loop_order.budget_name %] + [% IF (loop_order.can_receive) %] Receive + [% ELSE %] + Can't receive order + [% END %]
Transfer diff --git a/t/Acquisition/CanUserReceiveBasket.t b/t/Acquisition/CanUserReceiveBasket.t new file mode 100644 index 0000000..a0c4633 --- /dev/null +++ b/t/Acquisition/CanUserReceiveBasket.t @@ -0,0 +1,111 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Test::MockModule; +use Test::More tests => 30; + +use C4::Acquisition; + +my $C4_Acquisition_module = new Test::MockModule('C4::Acquisition'); +$C4_Acquisition_module->mock('GetBasket', \&Mock_GetBasket); +my $C4_Context_module = new Test::MockModule('C4::Context'); +$C4_Context_module->mock('userenv', \&Mock_userenv); + +my $baskets = { + 1 => {}, + 2 => { + branch => 'B1' + }, + 3 => { + branch => 'B2' + } +}; + +my $userenv = {}; + +my $borrower = {}; + +my $order1 = { + basketno => 1 +}; +my $order2 = { + basketno => 1, + branch => 'B1' +}; +my $order3 = { + basketno => 1, + branch => 'B2' +}; +my $order4 = { + basketno => 2 +}; +my $order5 = { + basketno => 2, + branch => 'B1' +}; +my $order6 = { + basketno => 2, + branch => 'B2' +}; +my $order7 = { + basketno => 3 +}; +my $order8 = { + basketno => 3, + branch => 'B1' +}; +my $order9 = { + basketno => 3, + branch => 'B2' +}; + +$userenv->{branch} = 'B1'; + +ok( CanUserReceiveOrder($borrower, $order1, {superlibrarian => 1}) ); +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => 1}) ); +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive_all => 1 }}) ); + +ok( not CanUserReceiveOrder($borrower, $order1, {}) ); +ok( not CanUserReceiveOrder($borrower, $order1, {acquisition => 0}) ); +ok( not CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive => 0 }}) ); + +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order2, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order3, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order4, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order5, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order6, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order7, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order8, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order9, {acquisition => { order_receive => 1 }}) ); + + +$userenv->{branch} = 'B2'; + +ok( CanUserReceiveOrder($borrower, $order1, {superlibrarian => 1}) ); +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => 1}) ); +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive_all => 1 }}) ); + +ok( not CanUserReceiveOrder($borrower, $order1, {}) ); +ok( not CanUserReceiveOrder($borrower, $order1, {acquisition => 0}) ); +ok( not CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive => 0 }}) ); + +ok( CanUserReceiveOrder($borrower, $order1, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order2, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order3, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order4, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order5, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order6, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order7, {acquisition => { order_receive => 1 }}) ); +ok( not CanUserReceiveOrder($borrower, $order8, {acquisition => { order_receive => 1 }}) ); +ok( CanUserReceiveOrder($borrower, $order9, {acquisition => { order_receive => 1 }}) ); + +sub Mock_GetBasket { + my ($basketno) = @_; + + return $baskets->{$basketno}; +} + +sub Mock_userenv { + return $userenv; +} -- 1.7.10.4