Bug 20980: DBRev 18.06.00.004
[koha_ffzg] / installer / data / mysql / updatedatabase.pl
index b38db83..cb6c225 100755 (executable)
@@ -46,7 +46,6 @@ use MARC::Record;
 use MARC::File::XML ( BinaryEncoding => 'utf8' );
 
 use File::Path qw[remove_tree]; # perl core module
-use File::Spec;
 use File::Slurp;
 
 # FIXME - The user might be installing a new database, so can't rely
@@ -11772,7 +11771,7 @@ $dbh->do(q{
     WHERE COALESCE(permanent,0)=0 AND dir='koha_upload'
 });
 
-my $tmp= File::Spec->tmpdir.'/koha_upload';
+my $tmp = C4::Context->temporary_directory . '/koha_upload';
 remove_tree( $tmp ) if -d $tmp;
 
     print "Upgrade to $DBversion done (Bug 14893 - Separate temporary storage per instance in Upload.pm)\n";
@@ -13519,53 +13518,55 @@ if ( CheckVersion($DBversion) ) {
         WHERE ordernumber = ?
     |);
 
-    require Koha::Number::Price;
+    require Number::Format;
+    my $format = Number::Format->new;
+    my $precision = 2;
     for my $order ( @$orders ) {
         $sth_get_bookseller->execute( $order->{ordernumber} );
         my ( $bookseller ) = $sth_get_bookseller->fetchrow_hashref;
-        $order->{rrp}   = Koha::Number::Price->new( $order->{rrp} )->round;
-        $order->{ecost} = Koha::Number::Price->new( $order->{ecost} )->round;
+        $order->{rrp}   = $format->round( $order->{rrp}, $precision );
+        $order->{ecost} = $format->round( $order->{ecost}, $precision );
         $order->{tax_rate} ||= 0 ; # tax_rate can be NULL in DB
         # Ordering
         if ( $bookseller->{listincgst} ) {
             $order->{rrp_tax_included} = $order->{rrp};
-            $order->{rrp_tax_excluded} = Koha::Number::Price->new(
-                $order->{rrp_tax_included} / ( 1 + $order->{tax_rate} ) )->round;
+            $order->{rrp_tax_excluded} = $format->round(
+                $order->{rrp_tax_included} / ( 1 + $order->{tax_rate} ), $precision );
             $order->{ecost_tax_included} = $order->{ecost};
-            $order->{ecost_tax_excluded} = Koha::Number::Price->new(
-                $order->{ecost} / ( 1 + $order->{tax_rate} ) )->round;
+            $order->{ecost_tax_excluded} = $format->round(
+                $order->{ecost} / ( 1 + $order->{tax_rate} ), $precision );
         }
         else {
             $order->{rrp_tax_excluded} = $order->{rrp};
-            $order->{rrp_tax_included} = Koha::Number::Price->new(
-                $order->{rrp} * ( 1 + $order->{tax_rate} ) )->round;
+            $order->{rrp_tax_included} = $format->round(
+                $order->{rrp} * ( 1 + $order->{tax_rate} ), $precision );
             $order->{ecost_tax_excluded} = $order->{ecost};
-            $order->{ecost_tax_included} = Koha::Number::Price->new(
-                $order->{ecost} * ( 1 + $order->{tax_rate} ) )->round;
+            $order->{ecost_tax_included} = $format->round(
+                $order->{ecost} * ( 1 + $order->{tax_rate} ), $precision );
         }
 
         #receiving
         if ( $bookseller->{listincgst} ) {
-            $order->{unitprice_tax_included} = Koha::Number::Price->new( $order->{unitprice} )->round;
-            $order->{unitprice_tax_excluded} = Koha::Number::Price->new(
-              $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate} ) )->round;
+            $order->{unitprice_tax_included} = $format->round( $order->{unitprice}, $precision );
+            $order->{unitprice_tax_excluded} = $format->round(
+              $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate} ), $precision );
         }
         else {
-            $order->{unitprice_tax_excluded} = Koha::Number::Price->new( $order->{unitprice} )->round;
-            $order->{unitprice_tax_included} = Koha::Number::Price->new(
-              $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate} ) )->round;
+            $order->{unitprice_tax_excluded} = $format->round( $order->{unitprice}, $precision );
+            $order->{unitprice_tax_included} = $format->round(
+              $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate} ), $precision );
         }
 
         # If the order is received, the tax is calculated from the unit price
         if ( $order->{orderstatus} eq 'complete' ) {
-            $order->{tax_value} = Koha::Number::Price->new(
+            $order->{tax_value} = $format->round(
               ( $order->{unitprice_tax_included} - $order->{unitprice_tax_excluded} )
-              * $order->{quantity} )->round;
+              * $order->{quantity}, $precision );
         } else {
             # otherwise the ecost is used
-            $order->{tax_value} = Koha::Number::Price->new(
+            $order->{tax_value} = $format->round(
                 ( $order->{ecost_tax_included} - $order->{ecost_tax_excluded} ) *
-                  $order->{quantity} )->round;
+                  $order->{quantity}, $precision );
         }
 
         $sth_update_order->execute(
@@ -13608,7 +13609,6 @@ if ( CheckVersion($DBversion) ) {
         WHERE ordernumber = ?
     |);
 
-    require Koha::Number::Price;
     for my $order (@$orders) {
         my $tax_value_on_ordering =
           $order->{quantity} *
@@ -16080,6 +16080,53 @@ if( CheckVersion( $DBversion ) ) {
     print "Upgrade to $DBversion done (Bug 2426 - Remove deprecated management permission)\n";
 }
 
+$DBversion = '18.06.00.003';
+if( CheckVersion( $DBversion ) ) {
+    $dbh->do( "ALTER TABLE search_field CHANGE COLUMN type type ENUM('', 'string', 'date', 'number', 'boolean', 'sum', 'isbn', 'stdno') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine'" );
+    SetVersion( $DBversion );
+    print "Upgrade to $DBversion done (Bug 20073 - Add new types for Elasticsearch fields)\n";
+}
+
+$DBversion = '18.06.00.004';
+if( CheckVersion( $DBversion ) ) {
+
+    # Add 'Manual Credit' offset type
+    $dbh->do(q{
+        INSERT IGNORE INTO `account_offset_types` (`type`) VALUES ('Manual Credit');
+    });
+
+    # Fix wrong account offsets / Manual credits
+    $dbh->do(q{
+        UPDATE account_offsets
+        SET credit_id=debit_id,
+            debit_id=NULL,
+            type='Manual Credit'
+        WHERE amount < 0 AND
+              type='Manual Debit' AND
+              debit_id IN
+                (SELECT accountlines_id AS debit_id
+                 FROM accountlines
+                 WHERE accounttype='C');
+    });
+
+    # Fix wrong account offsets / Manually forgiven amounts
+    $dbh->do(q{
+        UPDATE account_offsets
+        SET credit_id=debit_id,
+            debit_id=NULL,
+            type='Writeoff'
+        WHERE amount < 0 AND
+              type='Manual Debit' AND
+              debit_id IN
+                (SELECT accountlines_id AS debit_id
+                 FROM accountlines
+                 WHERE accounttype='FOR');
+    });
+
+    SetVersion( $DBversion );
+    print "Upgrade to $DBversion done (Bug 20980 - Manual credit offsets are stored as debits)\n";
+}
+
 # SEE bug 13068
 # if there is anything in the atomicupdate, read and execute it.