Bug 23321: Add cash register support to paycollect
[koha.git] / members / paycollect.pl
1 #!/usr/bin/perl
2 # Copyright 2009,2010 PTFS Inc.
3 # Copyright 2011 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use URI::Escape;
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Accounts;
29 use C4::Koha;
30
31 use Koha::Cash::Registers;
32 use Koha::Patrons;
33 use Koha::Patron::Categories;
34 use Koha::AuthorisedValues;
35 use Koha::Account;
36 use Koha::Token;
37
38 my $input = CGI->new();
39
40 my $payment_id          = $input->param('payment_id');
41 my $writeoff_individual = $input->param('writeoff_individual');
42 my $type                = scalar $input->param('type') || 'payment';
43
44 my $updatecharges_permissions = ($writeoff_individual || $type eq 'writeoff') ? 'writeoff' : 'remaining_permissions';
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {   template_name   => 'members/paycollect.tt',
47         query           => $input,
48         type            => 'intranet',
49         authnotrequired => 0,
50         flagsrequired   => { borrowers => 'edit_borrowers', updatecharges => $updatecharges_permissions },
51         debug           => 1,
52     }
53 );
54
55 # get borrower details
56 my $borrowernumber = $input->param('borrowernumber');
57 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
58 my $patron         = Koha::Patrons->find( $borrowernumber );
59 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
60
61 my $borrower       = $patron->unblessed;
62 my $account        = $patron->account;
63 my $category       = $patron->category;
64 my $user           = $input->remote_user;
65
66 my $library_id = C4::Context->userenv->{'branch'};
67 my $total_due  = $account->outstanding_debits->total_outstanding;
68
69 my $total_paid = $input->param('paid');
70
71 my $select_lines = $input->param('selected');
72 my $pay_individual   = $input->param('pay_individual');
73 my $select       = $input->param('selected_accts');
74 my $payment_note = uri_unescape scalar $input->param('payment_note');
75 my $payment_type = scalar $input->param('payment_type');
76 my $accountlines_id;
77
78 my $registerid = $input->param('registerid');
79 if ( !$registerid ) {
80     $registerid = Koha::Cash::Registers->find(
81         { branch => $library_id, branch_default => 1 },
82     )->id;
83 }
84 my $registers = Koha::Cash::Registers->search(
85     { branch   => $library_id, archived => 0 },
86     { order_by => { '-asc' => 'name' } }
87 );
88 $template->param(
89     registerid => $registerid,
90     registers  => $registers,
91 );
92
93 if ( $pay_individual || $writeoff_individual ) {
94     if ($pay_individual) {
95         $template->param( pay_individual => 1 );
96     } elsif ($writeoff_individual) {
97         $template->param( writeoff_individual => 1 );
98     }
99     my $accounttype       = $input->param('accounttype');
100     $accountlines_id       = $input->param('accountlines_id');
101     my $amount            = $input->param('amount');
102     my $amountoutstanding = $input->param('amountoutstanding');
103     my $itemnumber  = $input->param('itemnumber');
104     my $description  = $input->param('description');
105     my $title        = $input->param('title');
106     $total_due = $amountoutstanding;
107     $template->param(
108         accounttype       => $accounttype,
109         accountlines_id    => $accountlines_id,
110         amount            => $amount,
111         amountoutstanding => $amountoutstanding,
112         title             => $title,
113         itemnumber        => $itemnumber,
114         individual_description => $description,
115         payment_note    => $payment_note,
116     );
117 } elsif ($select_lines) {
118     $total_due = $input->param('amt');
119     $template->param(
120         selected_accts => $select_lines,
121         amt            => $total_due,
122         selected_accts_notes => scalar $input->param('notes'),
123     );
124 }
125
126 if ( $total_paid and $total_paid ne '0.00' ) {
127     if ( $total_paid < 0 or $total_paid > $total_due ) {
128         $template->param(
129             error_over => 1,
130             total_due => $total_due
131         );
132     } else {
133         output_and_exit( $input, $cookie, $template,  'wrong_csrf_token' )
134             unless Koha::Token->new->check_csrf( {
135                 session_id => $input->cookie('CGISESSID'),
136                 token  => scalar $input->param('csrf_token'),
137             });
138
139         if ($pay_individual) {
140             my $line = Koha::Account::Lines->find($accountlines_id);
141             $payment_id = $account->pay(
142                 {
143                     lines        => [$line],
144                     amount       => $total_paid,
145                     library_id   => $library_id,
146                     note         => $payment_note,
147                     interface    => C4::Context->interface,
148                     payment_type => $payment_type,
149                     cash_register => $registerid
150                 }
151             );
152             print $input->redirect(
153                 "/cgi-bin/koha/members/pay.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
154         } else {
155             if ($select) {
156                 if ( $select =~ /^([\d,]*).*/ ) {
157                     $select = $1;    # ensure passing no junk
158                 }
159                 my @acc = split /,/, $select;
160                 my $note = $input->param('selected_accts_notes');
161
162                 my @lines = Koha::Account::Lines->search(
163                     {
164                         borrowernumber    => $borrowernumber,
165                         amountoutstanding => { '<>' => 0 },
166                         accountlines_id   => { 'IN' => \@acc },
167                     },
168                     { order_by => 'date' }
169                 );
170
171                 $payment_id = $account->pay(
172                     {
173                         type         => $type,
174                         amount       => $total_paid,
175                         library_id   => $library_id,
176                         lines        => \@lines,
177                         note         => $note,
178                         interface    => C4::Context->interface,
179                         payment_type => $payment_type,
180                         cash_register => $registerid
181                     }
182                   );
183             }
184             else {
185                 my $note = $input->param('selected_accts_notes');
186                 $payment_id = $account->pay(
187                     {
188                         amount       => $total_paid,
189                         library_id   => $library_id,
190                         note         => $note,
191                         payment_type => $payment_type,
192                         interface    => C4::Context->interface,
193                         payment_type => $payment_type,
194                         cash_register => $registerid
195                     }
196                 );
197             }
198
199             print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber&payment_id=$payment_id");
200         }
201     }
202 } else {
203     $total_paid = '0.00';    #TODO not right with pay_individual
204 }
205
206 $template->param(%$borrower);
207
208 if ( $input->param('error_over') ) {
209     $template->param( error_over => 1, total_due => scalar $input->param('amountoutstanding') );
210 }
211
212 $template->param(
213     payment_id => $payment_id,
214
215     type           => $type,
216     borrowernumber => $borrowernumber,    # some templates require global
217     patron         => $patron,
218     total          => $total_due,
219
220     csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID') } ),
221 );
222
223 output_html_with_http_headers $input, $cookie, $template->output;