From 0b14e96abfbabdb3de62524afcd36fb8b93eecb5 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 11 Feb 2020 17:21:38 +0100
Subject: [PATCH] Bug 24614: Use Koha::Reports from save_report and update_sql

In order to get the default value defined at DBMS level, we use
Koha::Reports (to inherit from Koha::Object->store) from the 2 add/edit
methods of C4::Reports::Guided.
A second step would be to remove completely those CRUD subroutines and
use directly Koha::Reports instead.

Test plan:
1. Add and edit some reports
2. Disable memcached, create a report, edit it
=> Should not crash
3. Make sure the tests make sense and that they pass after the second
patch.

The error was:
DBD::mysql::db do failed: Column 'cache_expiry' cannot be null [for
Statement "UPDATE saved_sql SET savedsql = ?, last_modified = now(),
report_name = ?, report_group = ?, report_subgroup = ?, notes = ?,
cache_expiry = ?, public = ? WHERE id = ? "] at
/kohadevbox/koha/C4/Reports/Guided.pm line 633.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 C4/Reports/Guided.pm | 43 +++++++++++++++++++++++++++++++------------
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm
index 61aa804628..01a613dbf9 100644
--- a/C4/Reports/Guided.pm
+++ b/C4/Reports/Guided.pm
@@ -599,17 +599,29 @@ sub save_report {
     my $area = $fields->{area};
     my $group = $fields->{group};
     my $subgroup = $fields->{subgroup};
-    my $cache_expiry = $fields->{cache_expiry} || 300;
+    my $cache_expiry = $fields->{cache_expiry};
     my $public = $fields->{public};
 
-    my $dbh = C4::Context->dbh();
     $sql =~ s/(\s*\;\s*)$//;    # removes trailing whitespace and /;/
-    my $query = "INSERT INTO saved_sql (borrowernumber,date_created,last_modified,savedsql,report_name,report_area,report_group,report_subgroup,type,notes,cache_expiry,public)  VALUES (?,now(),now(),?,?,?,?,?,?,?,?,?)";
-    $dbh->do($query, undef, $borrowernumber, $sql, $name, $area, $group, $subgroup, $type, $notes, $cache_expiry, $public);
+    my $now = dt_from_string;
+    my $report = Koha::Report->new(
+        {
+            borrowernumber  => $borrowernumber,
+            date_created    => $now, # Must be moved to Koha::Report->store
+            last_modified   => $now, # Must be moved to Koha::Report->store
+            savedsql        => $sql,
+            report_name     => $name,
+            report_area     => $area,
+            report_group    => $group,
+            report_subgroup => $subgroup,
+            type            => $type,
+            notes           => $notes,
+            cache_expiry    => $cache_expiry,
+            public          => $public,
+        }
+    )->store;
 
-    my $id = $dbh->selectrow_array("SELECT max(id) FROM saved_sql WHERE borrowernumber=? AND report_name=?", undef,
-                                   $borrowernumber, $name);
-    return $id;
+    return $report->id;
 }
 
 sub update_sql {
@@ -623,14 +635,21 @@ sub update_sql {
     my $cache_expiry = $fields->{cache_expiry};
     my $public = $fields->{public};
 
+    $sql =~ s/(\s*\;\s*)$//;    # removes trailing whitespace and /;/
+    my $report = Koha::Reports->find($id);
+    $report->last_modified(dt_from_string);
+    $report->savedsql($sql);
+    $report->report_name($name);
+    $report->notes($notes);
+    $report->report_group($group);
+    $report->report_subgroup($subgroup);
+    $report->cache_expiry($cache_expiry) if defined $cache_expiry;
+    $report->public($public);
     if( $cache_expiry >= 2592000 ){
-      die "Please specify a cache expiry less than 30 days\n";
+      die "Please specify a cache expiry less than 30 days\n"; # That's a bit harsh
     }
 
-    my $dbh        = C4::Context->dbh();
-    $sql =~ s/(\s*\;\s*)$//;    # removes trailing whitespace and /;/
-    my $query = "UPDATE saved_sql SET savedsql = ?, last_modified = now(), report_name = ?, report_group = ?, report_subgroup = ?, notes = ?, cache_expiry = ?, public = ? WHERE id = ? ";
-    $dbh->do($query, undef, $sql, $name, $group, $subgroup, $notes, $cache_expiry, $public, $id );
+    return $report;
 }
 
 sub store_results {
-- 
2.20.1