Bug 22600: Add 'interface' to accountlines
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 28 Mar 2019 10:00:22 +0000 (10:00 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 10 Apr 2019 19:43:11 +0000 (19:43 +0000)
This patch adds the 'interface' field to the accountlines table and
updates all Koha::Object routines and calls to use it.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

19 files changed:
C4/Accounts.pm
C4/Circulation.pm
C4/Log.pm
C4/Overdues.pm
C4/Reserves.pm
C4/SIP/ILS/Transaction/FeePayment.pm
C4/SIP/SIPServer.pm
Koha/Account.pm
Koha/Account/Line.pm
Koha/Hold.pm
Koha/Patron.pm
Koha/REST/V1/Patrons/Account.pm
installer/data/mysql/atomicupdate/bug_22600.perl [new file with mode: 0644]
installer/data/mysql/kohastructure.sql
members/mancredit.pl
members/pay.pl
members/paycollect.pl
offline_circ/process_koc.pl
opac/opac-account-pay-paypal-return.pl

index 39b2bda..174c825 100644 (file)
@@ -107,6 +107,7 @@ sub chargelostitem{
                     description => $description,
                     note        => $processingfeenote,
                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                    interface   => C4::Context->interface,
                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
                     type        => 'processing',
                     item_id     => $itemnumber,
@@ -122,6 +123,7 @@ sub chargelostitem{
                     description => $description,
                     note        => undef,
                     user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                    interface   => C4::Context->interface,
                     library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
                     type        => 'lost_item',
                     item_id     => $itemnumber,
index 4ac38fe..cf4077d 100644 (file)
@@ -2416,6 +2416,7 @@ sub _FixAccountForLostAndReturned {
             {   amount      => $credit_total,
                 description => 'Item Returned ' . $item_id,
                 type        => 'lost_item_return',
+                interface   => C4::Context->interface,
                 library_id  => $branchcode
             }
         );
@@ -3220,6 +3221,7 @@ sub AddIssuingCharge {
             note        => undef,
             user_id     => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
             library_id  => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
+            interface   => C4::Context->interface,
             type        => 'rent',
             item_id     => $checkout->itemnumber,
             issue_id    => $checkout->issue_id,
@@ -3810,7 +3812,13 @@ sub ProcessOfflinePayment {
 
     my $patron = Koha::Patrons->find({ cardnumber => $operation->{cardnumber} });
 
-    $patron->account->pay({ amount => $operation->{amount}, library_id => $operation->{branchcode} });
+    $patron->account->pay(
+        {
+            amount     => $operation->{amount},
+            library_id => $operation->{branchcode},
+            interface  => 'koc'
+        }
+    );
 
     return "Success.";
 }
index 3a1e93e..34e2b60 100644 (file)
--- a/C4/Log.pm
+++ b/C4/Log.pm
@@ -56,7 +56,7 @@ The functions in this module perform various functions in order to log all the o
 
 =item logaction
 
-  &logaction($modulename, $actionname, $objectnumber, $infos);
+  &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
 
 Adds a record into action_logs table to report the different changes upon the database.
 Each log entry includes the number of the user currently logged in.  For batch
index ae6b8ce..b44aa58 100644 (file)
@@ -573,8 +573,15 @@ sub UpdateFine {
 
     if ( $data ) {
         if ( $data->{'amount'} != $amount ) {
-            my $accountline = Koha::Account::Lines->find( $data->{accountlines_id} );
-            $accountline->adjust({ amount => $amount, type => 'fine_update' });
+            my $accountline =
+              Koha::Account::Lines->find( $data->{accountlines_id} );
+            $accountline->adjust(
+                {
+                    amount    => $amount,
+                    type      => 'fine_update',
+                    interface => C4::Context->interface
+                }
+            );
         }
     } else {
         if ( $amount ) { # Don't add new fines with an amount of 0
@@ -592,7 +599,8 @@ sub UpdateFine {
                     description => $desc,
                     note        => undef,
                     user_id     => undef,
-                    library_id  => undef,
+                    interface   => C4::Context->interface,
+                    library_id  => undef, #FIXME: Should we grab the checkout or circ-control branch here perhaps?
                     type        => 'fine',
                     item_id     => $itemnum,
                     issue_id    => $issue_id,
index 09d3628..bbe0309 100644 (file)
@@ -575,6 +575,7 @@ sub ChargeReserveFee {
             note         => undef,
             user_id      => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
             library_id   => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
+            interface    => C4::Context->interface,
             sip          => undef,
             invoice_type => undef,
             type         => 'reserve',
index 41ed5c7..de0c22b 100644 (file)
@@ -65,10 +65,11 @@ sub pay {
         if ( $fee ) {
             $account->pay(
                 {
-                    amount => $amt,
-                    sip    => $sip_type,
-                    type   => $type,
-                    lines  => [$fee],
+                    amount    => $amt,
+                    sip       => $sip_type,
+                    type      => $type,
+                    lines     => [$fee],
+                    interface => C4::Context->interface
                 }
             );
             return 1;
@@ -80,9 +81,10 @@ sub pay {
     else {
         $account->pay(
             {
-                amount => $amt,
-                sip    => $sip_type,
-                type   => $type,
+                amount    => $amt,
+                sip       => $sip_type,
+                type      => $type,
+                interface => C4::Context->interface
             }
         );
         return 1;
index 5e1e3a6..63880c9 100755 (executable)
@@ -11,6 +11,7 @@ use IO::Socket::INET;
 use Socket qw(:DEFAULT :crlf);
 require UNIVERSAL::require;
 
+use C4::Context;
 use C4::SIP::Sip::Constants qw(:all);
 use C4::SIP::Sip::Configuration;
 use C4::SIP::Sip::Checksum qw(checksum verify_cksum);
@@ -27,6 +28,9 @@ use constant LOG_SIP => "local6"; # Local alias for the logging facility
 # A script with no MAIN namespace?
 # A module that takes command line args?
 
+# Set interface to 'sip'
+C4::Context->interface('sip');
+
 my %transports = (
     RAW    => \&raw_transport,
     telnet => \&telnet_transport,
index 120ab60..61fdef2 100644 (file)
@@ -86,6 +86,7 @@ sub pay {
     my $patron = Koha::Patrons->find( $self->{patron_id} );
 
     my $manager_id = $userenv ? $userenv->{number} : 0;
+    my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
 
     my @fines_paid; # List of account lines paid on with this payment
 
@@ -135,7 +136,8 @@ sub pay {
                         manager_id            => $manager_id,
                         note                  => $note,
                     }
-                )
+                ),
+                $interface
             );
             push( @fines_paid, $fine->id );
         }
@@ -185,7 +187,8 @@ sub pay {
                         manager_id            => $manager_id,
                         note                  => $note,
                     }
-                )
+                ),
+                $interface
             );
             push( @fines_paid, $fine->id );
         }
@@ -211,6 +214,7 @@ sub pay {
             payment_type      => $payment_type,
             amountoutstanding => 0 - $balance_remaining,
             manager_id        => $manager_id,
+            interface         => $interface,
             branchcode        => $library_id,
             note              => $note,
         }
@@ -244,7 +248,8 @@ sub pay {
                     accountlines_paid => \@fines_paid,
                     manager_id        => $manager_id,
                 }
-            )
+            ),
+            $interface
         );
     }
 
@@ -289,6 +294,7 @@ my $credit_line = Koha::Account->new({ patron_id => $patron_id })->add_credit(
         description  => $description,
         note         => $note,
         user_id      => $user_id,
+        interface    => $interface,
         library_id   => $library_id,
         sip          => $sip,
         payment_type => $payment_type,
@@ -315,6 +321,7 @@ sub add_credit {
     my $description  = $params->{description} // q{};
     my $note         = $params->{note} // q{};
     my $user_id      = $params->{user_id};
+    my $interface    = $params->{interface};
     my $library_id   = $params->{library_id};
     my $sip          = $params->{sip};
     my $payment_type = $params->{payment_type};
@@ -344,6 +351,7 @@ sub add_credit {
                     payment_type      => $payment_type,
                     note              => $note,
                     manager_id        => $user_id,
+                    interface         => $interface,
                     branchcode        => $library_id,
                     itemnumber        => $item_id,
                 }
@@ -381,7 +389,8 @@ sub add_credit {
                             manager_id        => $user_id,
                             branchcode        => $library_id,
                         }
-                    )
+                    ),
+                    $interface
                 );
             }
         }
@@ -400,6 +409,7 @@ my $debit_line = Koha::Account->new({ patron_id => $patron_id })->add_debit(
         description  => $description,
         note         => $note,
         user_id      => $user_id,
+        interface    => $interface,
         library_id   => $library_id,
         type         => $debit_type,
         item_id      => $item_id,
@@ -436,6 +446,7 @@ sub add_debit {
     my $description  = $params->{description} // q{};
     my $note         = $params->{note} // q{};
     my $user_id      = $params->{user_id};
+    my $interface    = $params->{interface};
     my $library_id   = $params->{library_id};
     my $type         = $params->{type};
     my $item_id      = $params->{item_id};
@@ -467,6 +478,7 @@ sub add_debit {
                     payment_type      => undef,
                     note              => $note,
                     manager_id        => $user_id,
+                    interface         => $interface,
                     itemnumber        => $item_id,
                     issue_id          => $issue_id,
                     branchcode        => $library_id,
@@ -496,7 +508,8 @@ sub add_debit {
                             itemnumber        => $item_id,
                             manager_id        => $user_id,
                         }
-                    )
+                    ),
+                    $interface
                 );
             }
         }
index ef9f4f9..6443b4d 100644 (file)
@@ -229,8 +229,9 @@ This method allows updating a debit or credit on a patron's account
 
     $account_line->adjust(
         {
-            amount => $amount,
-            type   => $update_type,
+            amount    => $amount,
+            type      => $update_type,
+            interface => $interface
         }
     );
 
@@ -250,6 +251,7 @@ sub adjust {
 
     my $amount       = $params->{amount};
     my $update_type  = $params->{type};
+    my $interface    = $params->{interface};
 
     unless ( exists($Koha::Account::Line::allowed_update->{$update_type}) ) {
         Koha::Exceptions::Account::UnrecognisedType->throw(
@@ -286,6 +288,7 @@ sub adjust {
                         amount      => $new_outstanding * -1,
                         description => 'Overpayment refund',
                         type        => 'credit',
+                        interface   => $interface,
                         ( $update_type eq 'fine_update' ? ( item_id => $self->itemnumber ) : ()),
                     }
                 );
index 37c7fa1..bfd3434 100644 (file)
@@ -369,11 +369,12 @@ sub cancel {
                   Koha::Account->new( { patron_id => $self->borrowernumber } );
                 $account->add_debit(
                     {
-                        amount  => $charge,
-                        user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                        amount     => $charge,
+                        user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                        interface  => C4::Context->interface,
                         library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
-                        type    => 'hold_expired',
-                        item_id => $self->itemnumber
+                        type       => 'hold_expired',
+                        item_id    => $self->itemnumber
                     }
                 );
             }
index cc1892b..d454444 100644 (file)
@@ -849,10 +849,11 @@ sub add_enrolment_fee_if_needed {
     if ( $enrolment_fee && $enrolment_fee > 0 ) {
         $self->account->add_debit(
             {
-                amount  => $enrolment_fee,
-                user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                amount     => $enrolment_fee,
+                user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
+                interface  => C4::Context->interface,
                 library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef,
-                type => 'account'
+                type       => 'account'
             }
         );
     }
index b0d623b..d35e896 100644 (file)
@@ -114,6 +114,7 @@ sub add_credit {
                 description  => $description,
                 note         => $note,
                 user_id      => $user->id,
+                interface    => 'api',
                 library_id   => $library_id
             }
         );
diff --git a/installer/data/mysql/atomicupdate/bug_22600.perl b/installer/data/mysql/atomicupdate/bug_22600.perl
new file mode 100644 (file)
index 0000000..d33e0a7
--- /dev/null
@@ -0,0 +1,52 @@
+$DBversion = 'XXX';    # will be replaced by the RM
+if ( CheckVersion($DBversion) ) {
+
+    if ( !column_exists( 'accountlines', 'interface' ) ) {
+        $dbh->do(
+            qq{
+            ALTER TABLE `accountlines`
+            ADD
+              `interface` varchar(16)
+            AFTER
+              `manager_id`;
+          }
+        );
+    }
+
+    $dbh->do(qq{
+        UPDATE
+          `accountlines`
+        SET
+          interface = 'opac'
+        WHERE
+          borrowernumber = manager_id;
+    });
+
+    $dbh->do(qq{
+        UPDATE
+          `accountlines`
+        SET
+          interface = 'cron'
+        WHERE
+          manager_id IS NULL
+        AND
+          branchcode IS NULL;
+    });
+
+    $dbh->do(qq{
+        UPDATE
+          `accountlines`
+        SET
+          interface = 'intranet'
+        WHERE
+          interface IS NULL;
+    });
+
+    $dbh->do(qq{
+        ALTER TABLE `accountlines`
+        MODIFY COLUMN `interface` varchar(16) NOT NULL;
+    });
+
+    SetVersion($DBversion);
+    print "Upgrade to $DBversion done (Bug 22600 - Add interface to accountlines)\n";
+}
index 510e39e..b1fe423 100644 (file)
@@ -2683,6 +2683,7 @@ CREATE TABLE `accountlines` (
   `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
   `note` MEDIUMTEXT NULL default NULL,
   `manager_id` int(11) NULL DEFAULT NULL,
+  `interface` VARCHAR(16) NOT NULL,
   `branchcode` VARCHAR( 10 ) NULL DEFAULT NULL, -- the branchcode of the library where a payment was made, a manual invoice created, etc.
   PRIMARY KEY (`accountlines_id`),
   KEY `acctsborridx` (`borrowernumber`),
index 8b349bd..c37e39e 100755 (executable)
@@ -89,7 +89,8 @@ if ($add){
         library_id  => $library_id,
         note        => $note,
         type        => $type,
-        user_id     => $logged_in_user->id
+        user_id     => $logged_in_user->id,
+        interface   => C4::Context->interface
     });
 
     if ( C4::Context->preference('AccountAutoReconcile') ) {
index b128053..dd7a1ef 100755 (executable)
@@ -113,6 +113,7 @@ elsif ( $input->param('confirm_writeoff') ) {
                 lines      => [ scalar Koha::Account::Lines->find($accountlines_id) ],
                 type       => 'writeoff',
                 note       => $payment_note,
+                interface  => C4::Context->interface,
                 library_id => $branch,
             }
         );
@@ -216,6 +217,7 @@ sub writeoff_all {
                     lines  => [ scalar Koha::Account::Lines->find($accountlines_id) ],
                     type   => 'writeoff',
                     note   => $payment_note,
+                    interface  => C4::Context->interface,
                     library_id => $branch,
                 }
             );
index 5bd9c0e..cecf2e3 100755 (executable)
@@ -125,6 +125,7 @@ if ( $total_paid and $total_paid ne '0.00' ) {
                     amount       => $total_paid,
                     library_id   => $branch,
                     note         => $payment_note,
+                    interface    => C4::Context->interface,
                     payment_type => $payment_type,
                 }
             );
@@ -157,6 +158,7 @@ if ( $total_paid and $total_paid ne '0.00' ) {
                         amount       => $total_paid,
                         lines        => \@lines,
                         note         => $note,
+                        interface    => C4::Context->interface,
                         payment_type => $payment_type,
                     }
                   );
@@ -168,6 +170,7 @@ if ( $total_paid and $total_paid ne '0.00' ) {
                         amount       => $total_paid,
                         note         => $note,
                         payment_type => $payment_type,
+                        interface    => C4::Context->interface
                     }
                 );
             }
index 174f00d..aacfeda 100755 (executable)
@@ -371,7 +371,7 @@ sub kocMakePayment {
     my $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
 
     Koha::Account->new( { patron_id => $patron->id } )
-      ->pay( { amount => $amount } );
+      ->pay( { amount => $amount, interface => C4::Context->interface } );
 
     push @output,
       {
index 76f6d21..07ed4bc 100755 (executable)
@@ -103,7 +103,8 @@ if ( $response->is_success ) {
             {
                 amount => $amount,
                 lines  => \@lines,
-                note   => 'PayPal'
+                note   => 'PayPal',
+                interface => C4::Context->interface
             }
         );
     }