From f5aebaed66f2f367b10fd8845dbc232cd3e03fe1 Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Fri, 22 Sep 2017 10:22:42 -0400 Subject: [PATCH] Bug 16820 - Edit accountlines from borrower's profile Adds the option to edit an accountline from a borrower's profile page in the intranet. Test plan : 0) Try to edit an accountline, you can't 1) Apply patch 2) Try to edit an accountline, you can Also solves Bug 2193 --- C4/Accounts.pm | 183 +++++++++++++++++++++ .../prog/en/modules/members/boraccount.tt | 61 +++++++ members/boraccount.pl | 100 +++++++++++ 3 files changed, 344 insertions(+) diff --git a/C4/Accounts.pm b/C4/Accounts.pm index 0a1e779..a197911 100644 --- a/C4/Accounts.pm +++ b/C4/Accounts.pm @@ -45,6 +45,7 @@ BEGIN { &chargelostitem &ReversePayment &purge_zero_balance_fees + &updatepayment ); } @@ -379,9 +380,191 @@ sub purge_zero_balance_fees { $sth->execute($days) or die $dbh->errstr; } +sub updatepayment { + my ( $borrowernumber, $account_id, $date, $itemnum, $desc, $type, $amount, $oldamount, $note ) = @_; + my $manager_id = 0; + $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; + my $dbh = C4::Context->dbh; + my $notifyid = 0; + my $amountleft = $amount; + + if ( ( $type eq 'L' ) + or ( $type eq 'F' ) + or ( $type eq 'A' ) + or ( $type eq 'N' ) + or ( $type eq 'M' ) ) + { + $notifyid = 1; + } + + if ( $type eq 'Pay' ) { + $amountleft = 0.0; + my $amontdif = $amount - $oldamount; + + if ( $amontdif <= 0 ) { + my $debtamount = abs($amontdif); + $amountleft = addfinetoaccountlines( $borrowernumber, $debtamount ); + + } + else { + $amountleft = reducefinefromaccountlines( $borrowernumber, $amontdif ); + } + $amount = -$amount; + } + + my $sth = $dbh->prepare( + 'UPDATE accountlines + SET date=?, amount=?, description=?, accounttype=?, amountoutstanding=?, + itemnumber=?,notify_id=?, note=?, manager_id=? + WHERE accountlines_id=?' + ); + $sth->execute( $date, $amount, $desc, $type, $amountleft, $itemnum, $notifyid, $note, $manager_id, $account_id ) || return $sth->errstr; + + if ( C4::Context->preference("FinesLog") ) { + logaction( + "FINES", 'MODIFY', + $borrowernumber, + Dumper( + { accountno => $account_id, + amount => $amount, + description => $desc, + accounttype => $type, + amountoutstanding => $amountleft, + notify_id => $notifyid, + note => $note, + itemnumber => $itemnum, + manager_id => $manager_id, + } + ) + ); + } + return 0; +} + +sub addfinetoaccountlines { + + #here we update the account lines + my ( $borrowernumber, $debtamount ) = @_; + my $newamtos = 0; + my $dbh = C4::Context->dbh; + my $accdata = ""; + + # get lines with outstanding amounts to offset + my $sth = $dbh->prepare( + "SELECT * FROM accountlines + WHERE (borrowernumber = ?) AND (amountoutstanding<>amount) and (amountoutstanding>=0) and (accounttype <> 'Pay') + ORDER BY date" + ); + $sth->execute($borrowernumber); + my @ids; + + # offset transactions + while ( ( $accdata = $sth->fetchrow_hashref ) and ( $debtamount > 0 ) ) { + if ( $accdata->{'amount'} - $accdata->{'amountoutstanding'} < $debtamount ) { + $debtamount = $debtamount - ( $accdata->{'amount'} - $accdata->{'amountoutstanding'} ); + $newamtos = $accdata->{'amount'}; + } + else { + $newamtos = $accdata->{'amountoutstanding'} + $debtamount; + $debtamount = 0; + } + my $thisacct = $accdata->{accountlines_id}; + my $usth = $dbh->prepare( + "UPDATE accountlines SET amountoutstanding= ? + WHERE (accountlines_id = ?)" + ); + $usth->execute( $newamtos, $thisacct ); + + if ( C4::Context->preference("FinesLog") ) { + my $manager_id = 0; + $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; + $accdata->{'amountoutstanding_new'} = $newamtos; + logaction( + "FINES", 'MODIFY', + $borrowernumber, + Dumper( + { action => 'fee_payment', + borrowernumber => $accdata->{'borrowernumber'}, + old_amountoutstanding => $accdata->{'amountoutstanding'}, + new_amountoutstanding => $newamtos, + amount_paid => $accdata->{'amountoutstanding'} - $newamtos, + accountlines_id => $accdata->{'accountlines_id'}, + accountno => $accdata->{'accountno'}, + manager_id => $manager_id, + } + ) + ); + push( @ids, $accdata->{'accountlines_id'} ); + } + } + return $debtamount; +} + +sub reducefinefromaccountlines { + + #here we update the account lines + my ( $borrowernumber, $amountpayed ) = @_; + my $amountleft = $amountpayed; + my $newamtos = 0; + my $dbh = C4::Context->dbh; + my $accdata = ""; + + # get lines with outstanding amounts to offset + my $sth = $dbh->prepare( + "SELECT * FROM accountlines + WHERE (borrowernumber = ?) AND (amountoutstanding<>0) + ORDER BY date" + ); + $sth->execute($borrowernumber); + my @ids; + + # offset transactions + while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) { + if ( $accdata->{'amountoutstanding'} < $amountleft ) { + $newamtos = 0; + $amountleft -= $accdata->{'amountoutstanding'}; + } + else { + $newamtos = $accdata->{'amountoutstanding'} - $amountleft; + $amountleft = 0; + } + my $thisacct = $accdata->{accountlines_id}; + my $usth = $dbh->prepare( + "UPDATE accountlines SET amountoutstanding= ? + WHERE (accountlines_id = ?)" + ); + $usth->execute( $newamtos, $thisacct ); + + if ( C4::Context->preference("FinesLog") ) { + my $manager_id = 0; + $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; + $accdata->{'amountoutstanding_new'} = $newamtos; + logaction( + "FINES", 'MODIFY', + $borrowernumber, + Dumper( + { action => 'fee_payment', + borrowernumber => $accdata->{'borrowernumber'}, + old_amountoutstanding => $accdata->{'amountoutstanding'}, + new_amountoutstanding => $newamtos, + amount_paid => $accdata->{'amountoutstanding'} - $newamtos, + accountlines_id => $accdata->{'accountlines_id'}, + accountno => $accdata->{'accountno'}, + manager_id => $manager_id, + } + ) + ); + push( @ids, $accdata->{'accountlines_id'} ); + } + } + return $amountleft; +} + + END { } # module clean-up code here (global destructor) 1; + __END__ =head1 SEE ALSO diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt index ec79237..dba1905 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/boraccount.tt @@ -61,6 +61,65 @@ $(document).ready(function() {
  • Create manual credit
  • + [% IF (edit) %] + [% IF ( ERROR ) %] + ERROR an invalid itemnumber was entered, please hit back and try again + [% ELSE %] + [% IF ( error_over ) %] +
    + The amount value must be less than or equal to [% total_due | format('%.2f') %]. +
    + [% END %] +
    +
    + Edit +
      +
    1. +
    2. + + + +
    3. +
    4. +
    5. +
    6. +
    7. Example: 5.00
    8. + + +
    +
    Cancel
    +
    + [% END %] + [% ELSE %] @@ -112,6 +171,7 @@ $(document).ready(function() { [% IF ( account.amountcredit ) %] [% IF ( account.amountoutstandingcredit ) %]
    [% ELSE %][% END %][% account.amount | $Price %][% ELSE %][% END %][% account.amountoutstanding | $Price %] + Edit [% IF ( account.payment ) %] Print [% ELSE %] @@ -140,6 +200,7 @@ $(document).ready(function() {
    +[% END %]
    diff --git a/members/boraccount.pl b/members/boraccount.pl index e7d769e..be8e7c6 100755 --- a/members/boraccount.pl +++ b/members/boraccount.pl @@ -31,11 +31,14 @@ use CGI qw ( -utf8 ); use C4::Members; use C4::Accounts; use C4::Members::Attributes qw(GetBorrowerAttributes); +use C4::Items; use Koha::Patrons; use Koha::Patron::Categories; my $input=new CGI; +my $borrowernumber=$input->param('borrowernumber'); +my ($total,$accts,$numaccts)=GetMemberAccountRecords($borrowernumber); my ($template, $loggedinuser, $cookie) = get_template_and_user( { @@ -62,6 +65,103 @@ unless ( $patron ) { if ( $action eq 'reverse' ) { ReversePayment( scalar $input->param('accountlines_id') ); } +elsif ( $input->param('add') or $action eq 'edit' ) { + my $borrowernumber = $input->param('borrowernumber'); + + #get account details + my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber); + if ( $input->param('add') ) { + my $total_due = $total; + my $barcode = $input->param('barcode'); + my $itemnum; + if ($barcode) { + $itemnum = GetItemnumberFromBarcode($barcode); + } + my $desc = $input->param('desc'); + my $amount = $input->param('amount'); + my $oldamount = $input->param('accountoldamount'); + my $type = $input->param('type'); + my $note = $input->param('note'); + + if ( $type eq "Pay" ) { + if ( $amount <= $total_due + $oldamount or $amount < 0 ) { + my $error = updatepayment( $borrowernumber, $input->param('accountlines_id'), $input->param('date'), $itemnum, $desc, $type, $amount, $oldamount, $note ); + if ($error) { + $template->param( 'ERROR' => $error ); + output_html_with_http_headers $input, $cookie, $template->output; + exit; + } + else { + print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"); + exit; + } + } + else { + $template->param( + error_over => 1, + total_due => $total_due + $oldamount + ); + } + } + else { + if ( $type eq "C" or $type eq "FOR" ) { + $amount = -abs($amount); + } + my $error = updatepayment( $borrowernumber, $input->param('accountlines_id'), $input->param('date'), $itemnum, $desc, $type, $amount, $oldamount, $note ); + if ($error) { + $template->param( 'ERROR' => $error ); + output_html_with_http_headers $input, $cookie, $template->output; + exit; + } + else { + print $input->redirect("/cgi-bin/koha/members/boraccount.pl?borrowernumber=$borrowernumber"); + exit; + } + } + } + my $data = $patron; + my $accountlines_id = $input->param('accountlines_id'); + + #get authorised values with type of MANUAL_INV + my @invoice_types; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('SELECT * FROM authorised_values WHERE category = "MANUAL_INV"'); + $sth->execute(); + while ( my $row = $sth->fetchrow_hashref() ) { + push @invoice_types, $row; + } + $template->param( invoice_types_loop => \@invoice_types ); + + if ( $data->{'category_type'} eq 'C' ) { + my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' ); + my $cnt = scalar(@$catcodes); + $template->param( 'CATCODE_MULTI' => 1 ) if $cnt > 1; + $template->param( 'catcode' => $catcodes->[0] ) if $cnt == 1; + } + + for ( my $i = 0; $i < $numaccts; $i++ ) { + next if ( $accts->[$i]{'accountlines_id'} ne $accountlines_id ); + my $barcode; + if ( $accts->[$i]{'itemnumber'} ) { + $barcode = GetBarcodeFromItemnumber( $accts->[$i]{'itemnumber'} ); + } + my $amount = sprintf '%.2f', $accts->[$i]{'amount'}; + if ( $accts->[$i]{'accounttype'} eq "Pay" or $accts->[$i]{'accounttype'} eq "C" or $accts->[$i]{'accounttype'} eq "FOR" ) { + $amount = abs($amount); + } + $template->param( + accbarcode => $barcode, + accdate => $accts->[$i]{'date'}, + accdescription => $accts->[$i]{'description'}, + accnote => $accts->[$i]{'note'}, + acctype => $accts->[$i]{'accounttype'}, + accamount => $amount, + accid => $accountlines_id, + ); + } + + $template->param( edit => 1, ); +} if ( $patron->category->category_type eq 'C') { my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']}); -- 2.7.4