Bug 7703: (QA follow-up)
[srvgit] / pos / register.pl
index 343526f..490fe11 100755 (executable)
 
 use Modern::Perl;
 use CGI;
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 use C4::Context;
 
+use Koha::Account::Lines;
 use Koha::Cash::Registers;
 use Koha::Database;
+use Koha::DateUtils qw( dt_from_string output_pref );
 
 my $input = CGI->new();
 
@@ -70,47 +72,85 @@ else {
         accountlines => $accountlines
     );
 
-    my $op = $input->param('op') // '';
-    if ( $op eq 'cashup' ) {
-        $cash_register->add_cashup(
+    my $transactions_range_from = $input->param('trange_f');
+    my $last_cashup             = $cash_register->last_cashup;
+    my $transactions_range_to =
+        $input->param('trange_t') ? $input->param('trange_t')
+      : $last_cashup              ? $last_cashup->timestamp
+      :                             '';
+    my $end               = dt_from_string($transactions_range_to);
+
+    if ($transactions_range_from) {
+
+        my $dtf               = $schema->storage->datetime_parser;
+        my $start             = dt_from_string($transactions_range_from);
+        my $past_accountlines = Koha::Account::Lines->search(
             {
-                manager_id => $logged_in_user->id,
-                amount     => $cash_register->outstanding_accountlines->total
+                register_id => $registerid,
+                timestamp   => {
+                    -between => [
+                        $dtf->format_datetime($start),
+                        $dtf->format_datetime($end)
+                    ]
+                }
             }
         );
+        $template->param( past_accountlines => $past_accountlines );
+        $template->param( trange_f => output_pref({dt => $start, dateonly => 1}));
+    }
+    $template->param( trange_t => output_pref({dt => $end, dateonly => 1}));
+
+    my $op = $input->param('op') // '';
+    if ( $op eq 'cashup' ) {
+        if ( $logged_in_user->has_permission( { cash_management => 'cashup' } ) ) {
+            $cash_register->add_cashup(
+                {
+                    manager_id => $logged_in_user->id,
+                    amount     => $cash_register->outstanding_accountlines->total
+                }
+            );
+        }
+        else {
+            $template->param( error_cashup_permission => 1 );
+        }
     }
     elsif ( $op eq 'refund' ) {
-        my $amount           = $input->param('amount');
-        my $quantity         = $input->param('quantity');
-        my $accountline_id   = $input->param('accountline');
-        my $transaction_type = $input->param('transaction_type');
-
-        my $accountline = Koha::Account::Lines->find($accountline_id);
-        $schema->txn_do(
-            sub {
-
-                my $refund = $accountline->reduce(
-                    {
-                        reduction_type => 'Refund',
-                        branch         => $library_id,
-                        staff_id       => $logged_in_user->id,
-                        interface      => 'intranet',
-                        amount         => $amount
-                    }
-                );
-                my $payout = $refund->payout(
-                    {
-                        payout_type   => $transaction_type,
-                        branch        => $library_id,
-                        staff_id      => $logged_in_user->id,
-                        cash_register => $cash_register->id,
-                        interface     => 'intranet',
-                        amount        => $amount
-                    }
-                );
+        if ( $logged_in_user->has_permission( { cash_management => 'anonymous_refund' } ) ) {
+            my $amount           = $input->param('amount');
+            my $quantity         = $input->param('quantity');
+            my $accountline_id   = $input->param('accountline');
+            my $refund_type      = $input->param('refund_type');
 
-            }
-        );
+            my $accountline = Koha::Account::Lines->find($accountline_id);
+            $schema->txn_do(
+                sub {
+
+                    my $refund = $accountline->reduce(
+                        {
+                            reduction_type => 'REFUND',
+                            branch         => $library_id,
+                            staff_id       => $logged_in_user->id,
+                            interface      => 'intranet',
+                            amount         => $amount
+                        }
+                    );
+                    my $payout = $refund->payout(
+                        {
+                            payout_type   => $refund_type,
+                            branch        => $library_id,
+                            staff_id      => $logged_in_user->id,
+                            cash_register => $cash_register->id,
+                            interface     => 'intranet',
+                            amount        => $amount
+                        }
+                    );
+
+                }
+            );
+        }
+        else {
+            $template->param( error_refund_permission => 1 );
+        }
     }
 }