Bug 33161: Clarify method names
[koha-ffzg.git] / admin / aqcontract.pl
index 095d70c..def0339 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Context;
-use C4::Auth;
-use C4::Output;
-use C4::Dates qw/format_date format_date_in_iso/;
-use C4::Contract;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
+use C4::Contract qw(
+    AddContract
+    DelContract
+    GetContract
+    GetContracts
+    ModContract
+);
+use Koha::DateUtils qw( dt_from_string );
 
-use Koha::Acquisition::Bookseller;
+use Koha::Acquisition::Booksellers;
 
-my $input          = new CGI;
+my $input          = CGI->new;
 my $contractnumber = $input->param('contractnumber');
 my $booksellerid   = $input->param('booksellerid');
 my $op             = $input->param('op') || 'list';
 
-my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
 
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     {   template_name   => "admin/aqcontract.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { acquisition => 'contracts_manage' },
-        debug           => 1,
     }
 );
 
 $template->param(
     contractnumber => $contractnumber,
     booksellerid   => $booksellerid,
-    booksellername => $bookseller->{name},
-    basketcount   => $bookseller->{'basketcount'},
-    subscriptioncount   => $bookseller->{'subscriptioncount'},
+    booksellername => $bookseller->name,
+    basketcount   => $bookseller->baskets->count,
+    active         => $bookseller->active,
+    subscriptioncount   => $bookseller->subscriptions->count,
 );
 
 #ADD_FORM: called if $op is 'add_form'. Used to create form to add or  modify a record
@@ -70,8 +74,8 @@ if ( $op eq 'add_form' ) {
             contractnumber      => $contract->{contractnumber},
             contractname        => $contract->{contractname},
             contractdescription => $contract->{contractdescription},
-            contractstartdate => format_date( $contract->{contractstartdate} ),
-            contractenddate   => format_date( $contract->{contractenddate} ),
+            contractstartdate   => $contract->{contractstartdate},
+            contractenddate     => $contract->{contractenddate},
         );
     } else {
         $template->param(
@@ -92,22 +96,30 @@ elsif ( $op eq 'add_validate' ) {
 
     my $is_a_modif = $input->param("is_a_modif");
 
+    my $contractstart_dt = $input->param('contractstartdate');
+    my $contractend_dt = $input->param('contractenddate');
+    unless ( $contractstart_dt and $contractend_dt ) {
+        my $today = dt_from_string;
+        $contractstart_dt ||= $today;
+        $contractend_dt   ||= $today;
+    }
+
     if ( $is_a_modif ) {
         ModContract({
-            contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
-            contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
-            contractname        => $input->param('contractname'),
-            contractdescription => $input->param('contractdescription'),
-            booksellerid        => $input->param('booksellerid'),
-            contractnumber      => $input->param('contractnumber'),
+            contractstartdate   => $contractstart_dt,
+            contractenddate     => $contractend_dt,
+            contractname        => scalar $input->param('contractname'),
+            contractdescription => scalar $input->param('contractdescription'),
+            booksellerid        => scalar $input->param('booksellerid'),
+            contractnumber      => scalar $input->param('contractnumber'),
         });
     } else {
         AddContract({
-            contractname        => $input->param('contractname'),
-            contractdescription => $input->param('contractdescription'),
-            booksellerid        => $input->param('booksellerid'),
-            contractstartdate   => format_date_in_iso( $input->param('contractstartdate') ),
-            contractenddate     => format_date_in_iso( $input->param('contractenddate') ),
+            contractname        => scalar $input->param('contractname'),
+            contractdescription => scalar $input->param('contractdescription'),
+            booksellerid        => scalar $input->param('booksellerid'),
+            contractstartdate   => scalar $input->param('contractstartdate'),
+            contractenddate     => scalar $input->param('contractenddate'),
         });
     }
 
@@ -126,8 +138,8 @@ elsif ( $op eq 'delete_confirm' ) {
         contractnumber      => $$contract{contractnumber},
         contractname        => $$contract{contractname},
         contractdescription => $$contract{contractdescription},
-        contractstartdate   => format_date( $$contract{contractstartdate} ),
-        contractenddate     => format_date( $$contract{contractenddate} ),
+        contractstartdate   => $$contract{contractstartdate},
+        contractenddate     => $$contract{contractenddate},
     );
 
     # END $OP eq DELETE_CONFIRM
@@ -153,12 +165,6 @@ if ( $op eq 'list' ) {
     # get contracts
     my @contracts = @{GetContracts( { booksellerid => $booksellerid } )};
 
-    # format dates
-    for ( @contracts ) {
-        $$_{contractstartdate} = format_date($$_{contractstartdate});
-        $$_{contractenddate}   = format_date($$_{contractenddate});
-    }
-
     $template->param(loop => \@contracts);
 
     #---- END $OP eq DEFAULT