Bug 33056: Terminology: change 'fine' to 'charge'when making a payment or writeoff
[srvgit] / C4 / Acquisition.pm
index bbd0168..6873a67 100644 (file)
@@ -30,6 +30,7 @@ use C4::Templates qw(gettemplate);
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Acquisition::Baskets;
 use Koha::Acquisition::Booksellers;
+use Koha::Acquisition::Invoices;
 use Koha::Acquisition::Orders;
 use Koha::Biblios;
 use Koha::Exceptions;
@@ -321,20 +322,23 @@ sub GetBasketAsCSV {
     else {
         foreach my $order (@orders) {
             my $biblio = Koha::Biblios->find( $order->{biblionumber} );
-            my $biblioitem = $biblio->biblioitem;
+            my $biblioitem;
+            if ($biblio) {
+                $biblioitem = $biblio->biblioitem;
+            }
             my $row = {
-                contractname => $contract->{'contractname'},
-                ordernumber => $order->{'ordernumber'},
-                entrydate => $order->{'entrydate'},
-                isbn => $order->{'isbn'},
-                author => $biblio->author,
-                title => $biblio->title,
-                publicationyear => $biblioitem->publicationyear,
-                publishercode => $biblioitem->publishercode,
-                collectiontitle => $biblioitem->collectiontitle,
-                notes => $order->{'order_vendornote'},
-                quantity => $order->{'quantity'},
-                rrp => $order->{'rrp'},
+                contractname    => $contract->{'contractname'},
+                ordernumber     => $order->{'ordernumber'},
+                entrydate       => $order->{'entrydate'},
+                isbn            => $order->{'isbn'},
+                author          => $biblio     ? $biblio->author              : q{},
+                title           => $biblio     ? $biblio->title               : q{},
+                publicationyear => $biblioitem ? $biblioitem->publicationyear : q{},
+                publishercode   => $biblioitem ? $biblioitem->publishercode   : q{},
+                collectiontitle => $biblioitem ? $biblioitem->collectiontitle : q{},
+                notes           => $order->{'order_vendornote'},
+                quantity        => $order->{'quantity'},
+                rrp             => $order->{'rrp'},
             };
             for my $place ( qw( deliveryplace billingplace ) ) {
                 if ( my $library = Koha::Libraries->find( $row->{deliveryplace} ) ) {
@@ -391,30 +395,33 @@ sub GetBasketGroupAsCSV {
 
         foreach my $order (@orders) {
             my $biblio = Koha::Biblios->find( $order->{biblionumber} );
-            my $biblioitem = $biblio->biblioitem;
+            my $biblioitem;
+            if ($biblio) {
+                $biblioitem = $biblio->biblioitem;
+            }
             my $row = {
-                clientnumber => $bookseller->accountnumber,
-                basketname => $basket->{basketname},
-                ordernumber => $order->{ordernumber},
-                author => $biblio->author,
-                title => $biblio->title,
-                publishercode => $biblioitem->publishercode,
-                publicationyear => $biblioitem->publicationyear,
-                collectiontitle => $biblioitem->collectiontitle,
-                isbn => $order->{isbn},
-                quantity => $order->{quantity},
-                rrp_tax_included => $order->{rrp_tax_included},
-                rrp_tax_excluded => $order->{rrp_tax_excluded},
-                discount => $bookseller->discount,
+                clientnumber       => $bookseller->accountnumber,
+                basketname         => $basket->{basketname},
+                ordernumber        => $order->{ordernumber},
+                author             => $biblio     ? $biblio->author              : q{},
+                title              => $biblio     ? $biblio->title               : q{},
+                publishercode      => $biblioitem ? $biblioitem->publishercode   : q{},
+                publicationyear    => $biblioitem ? $biblioitem->publicationyear : q{},
+                collectiontitle    => $biblioitem ? $biblioitem->collectiontitle : q{},
+                isbn               => $order->{isbn},
+                quantity           => $order->{quantity},
+                rrp_tax_included   => $order->{rrp_tax_included},
+                rrp_tax_excluded   => $order->{rrp_tax_excluded},
+                discount           => $bookseller->discount,
                 ecost_tax_included => $order->{ecost_tax_included},
                 ecost_tax_excluded => $order->{ecost_tax_excluded},
-                notes => $order->{order_vendornote},
-                entrydate => $order->{entrydate},
-                booksellername => $bookseller->name,
-                bookselleraddress => $bookseller->address1,
-                booksellerpostal => $bookseller->postal,
-                contractnumber => $contract->{contractnumber},
-                contractname => $contract->{contractname},
+                notes              => $order->{order_vendornote},
+                entrydate          => $order->{entrydate},
+                booksellername     => $bookseller->name,
+                bookselleraddress  => $bookseller->address1,
+                booksellerpostal   => $bookseller->postal,
+                contractnumber     => $contract->{contractnumber},
+                contractname       => $contract->{contractname},
             };
             my $temp = {
                 basketgroupdeliveryplace => $basketgroup->{deliveryplace},
@@ -1376,6 +1383,9 @@ sub ModReceiveOrder {
     $datereceived = $datereceived ? dt_from_string( $datereceived ) : dt_from_string;
     $datereceived = $datereceived->ymd;
 
+    $order->{invoice_unitprice} ||= $order->{unitprice};
+    $order->{invoice_currency}  ||= Koha::Acquisition::Currencies->get_active->currency;
+
     my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
     if ($suggestionid) {
         ModSuggestion( {suggestionid=>$suggestionid,
@@ -1408,23 +1418,29 @@ sub ModReceiveOrder {
             $order->{ordernumber}
         );
 
-        if ( not $order->{subscriptionid} && defined $order->{order_internalnote} ) {
-            $dbh->do(
-                q|UPDATE aqorders
-                SET order_internalnote = ?
-                WHERE ordernumber = ?|, {},
-                $order->{order_internalnote}, $order->{ordernumber}
-            );
-        }
-
         # Recalculate tax_value
-        $dbh->do(q|
+        $query = q|
             UPDATE aqorders
             SET
                 tax_value_on_ordering = quantity * | . get_rounding_sql(q|ecost_tax_excluded|) . q| * tax_rate_on_ordering,
                 tax_value_on_receiving = quantity * | . get_rounding_sql(q|unitprice_tax_excluded|) . q| * tax_rate_on_receiving
+        |;
+
+        my @params;
+        if ( not $order->{subscriptionid} && defined $order->{order_internalnote} )
+        {
+            $query .= q|, order_internalnote = ?|;
+            push @params, $order->{order_internalnote};
+        }
+
+        $query .= q|, invoice_unitprice = ?, invoice_currency = ?|;
+        push @params, $order->{invoice_unitprice}, $order->{invoice_currency};
+
+        $query .= q|
             WHERE ordernumber = ?
-        |, undef, $order->{ordernumber});
+        |;
+
+        $dbh->do($query, undef, @params, $order->{ordernumber});
 
         delete $order->{ordernumber};
         $order->{budget_id} = ( $budget_id || $order->{budget_id} );
@@ -1476,6 +1492,10 @@ sub ModReceiveOrder {
             , order_internalnote = ?
         | if defined $order->{order_internalnote};
 
+        $query .= q|
+            , invoice_unitprice = ?, invoice_currency = ?
+        |;
+
         $query .= q| where biblionumber=? and ordernumber=?|;
 
         my $sth = $dbh->prepare( $query );
@@ -1501,6 +1521,8 @@ sub ModReceiveOrder {
             push @params, $order->{order_internalnote};
         }
 
+        push @params, $order->{invoice_unitprice}, $order->{invoice_currency};
+
         push @params, ( $biblionumber, $order->{ordernumber} );
 
         $sth->execute( @params );
@@ -2406,6 +2428,18 @@ asc is the default if omitted
 sub GetInvoices {
     my %args = @_;
 
+    my $additional_fields = $args{additional_fields} // [];
+    my $matching_invoice_ids_for_additional_fields = [];
+    if ( @$additional_fields ) {
+        my @invoices = Koha::Acquisition::Invoices->filter_by_additional_fields($additional_fields)->as_list;
+
+        return () unless @invoices;
+
+        $matching_invoice_ids_for_additional_fields = [ map {
+            $_->id
+        } @invoices ];
+    }
+
     my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
         closedate shipmentcost shipmentcost_budgetid);
 
@@ -2498,6 +2532,18 @@ sub GetInvoices {
     }
 
     $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
+
+    # Handle additional fields filtering
+    if ( @$additional_fields ) {
+        my $operator = ' WHERE';
+        if ( @bind_strs ) { # there's a WHERE already
+            $operator = ' AND';
+        }
+        $query .= "$operator aqinvoices.invoiceid IN ("
+            . join( ', ', @$matching_invoice_ids_for_additional_fields )
+        . ')';
+    }
+
     $query .= " GROUP BY aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id, aqbooksellers.name";
 
     if($args{order_by}) {