return json with issues for ferweb
[koha_fer] / acqui / invoice.pl
index 28f3841..6608947 100755 (executable)
@@ -35,129 +35,119 @@ use C4::Output;
 use C4::Acquisition;
 use C4::Bookseller qw/GetBookSellerFromId/;
 use C4::Budgets;
+use Koha::Misc::Files;
 
 my $input = new CGI;
-my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
-    template_name   => 'acqui/invoice.tmpl',
-    query           => $input,
-    type            => 'intranet',
-    authnotrequired => 0,
-    flagsrequired   => { 'acquisition' => '*' },
-    debug           => 1,
-} );
+my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
+    {
+        template_name   => 'acqui/invoice.tmpl',
+        query           => $input,
+        type            => 'intranet',
+        authnotrequired => 0,
+        flagsrequired   => { 'acquisition' => '*' },
+        debug           => 1,
+    }
+);
 
 my $invoiceid = $input->param('invoiceid');
-my $op = $input->param('op');
+my $op        = $input->param('op');
+
+my $invoice_files;
+if ( C4::Context->preference('AcqEnableFiles') ) {
+    $invoice_files = Koha::Misc::Files->new(
+        tabletag => 'aqinvoices', recordid => $invoiceid );
+}
 
-if($op && $op eq 'close') {
+if ( $op && $op eq 'close' ) {
     CloseInvoice($invoiceid);
     my $referer = $input->param('referer');
-    if($referer) {
+    if ($referer) {
         print $input->redirect($referer);
         exit 0;
     }
-}elsif($op && $op eq 'reopen') {
+}
+elsif ( $op && $op eq 'reopen' ) {
     ReopenInvoice($invoiceid);
     my $referer = $input->param('referer');
-    if($referer) {
+    if ($referer) {
         print $input->redirect($referer);
         exit 0;
     }
-}elsif($op && $op eq 'mod') {
-    my $shipmentdate = $input->param('shipmentdate');
-    my $billingdate = $input->param('billingdate');
-    my $shipmentcost = $input->param('shipmentcost');
+}
+elsif ( $op && $op eq 'mod' ) {
+    my $shipmentdate       = $input->param('shipmentdate');
+    my $billingdate        = $input->param('billingdate');
+    my $shipmentcost       = $input->param('shipmentcost');
     my $shipment_budget_id = $input->param('shipment_budget_id');
     ModInvoice(
-        invoiceid => $invoiceid,
-        shipmentdate   => C4::Dates->new($shipmentdate)->output("iso"),
-        billingdate   => C4::Dates->new($billingdate)->output("iso"),
-        shipmentcost  => $shipmentcost,
+        invoiceid             => $invoiceid,
+        shipmentdate          => C4::Dates->new($shipmentdate)->output("iso"),
+        billingdate           => C4::Dates->new($billingdate)->output("iso"),
+        shipmentcost          => $shipmentcost,
         shipmentcost_budgetid => $shipment_budget_id
     );
-    $template->param(modified => 1);
+    if ($input->param('reopen')) {
+        ReopenInvoice($invoiceid);
+    } elsif ($input->param('close')) {
+        CloseInvoice($invoiceid);
+    } elsif ($input->param('merge')) {
+        my @sources = $input->param('merge');
+        MergeInvoices($invoiceid, \@sources);
+        defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
+    }
+    $template->param( modified => 1 );
+}
+elsif ( $op && $op eq 'delete' ) {
+    DelInvoice($invoiceid);
+    defined($invoice_files) && $invoice_files->DelAllFiles();
+    my $referer = $input->param('referer') || 'invoices.pl';
+    if ($referer) {
+        print $input->redirect($referer);
+        exit 0;
+    }
 }
 
+
 my $details = GetInvoiceDetails($invoiceid);
 my $bookseller = GetBookSellerFromId($details->{booksellerid});
 my @orders_loop = ();
 my $orders = $details->{'orders'};
 my $qty_total;
-my @books_loop;
-my @book_foot_loop;
+my @foot_loop;
 my %foot;
 my $total_quantity = 0;
-my $total_rrp = 0;
-my $total_est = 0;
+my $total_gste = 0;
+my $total_gsti = 0;
+my $total_gstvalue = 0;
 foreach my $order (@$orders) {
     my $line = get_infos( $order, $bookseller);
 
+    $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
+    $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
+    $total_gstvalue += $$line{gstvalue};
+    $foot{$$line{gstgsti}}{quantity}  += $$line{quantity};
     $total_quantity += $$line{quantity};
-    $total_rrp += $order->{quantity} * $order->{rrp};
-    $total_est += $order->{quantity} * $order->{'ecost'};
+    $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
+    $total_gste += $$line{totalgste};
+    $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
+    $total_gsti += $$line{totalgsti};
 
-    my %row = (%$order, %$line);
-    push @orders_loop, \%row;
+    $line->{orderline} = $line->{parent_ordernumber};
+    push @orders_loop, $line;
 }
 
-my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
-my $discount = $bookseller->{'discount'} ? ($bookseller->{discount} / 100) : 0;
-my $total_est_gste;
-my $total_est_gsti;
-my $total_rrp_gsti; # RRP Total, GST included
-my $total_rrp_gste; # RRP Total, GST excluded
-my $gist_est;
-my $gist_rrp;
-if ($gist){
-    # if we have GST
-    if ( $bookseller->{'listincgst'} ) {
-        # if prices already includes GST
-
-        # we know $total_rrp_gsti
-        $total_rrp_gsti = $total_rrp;
-        # and can reverse compute other values
-        $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
-
-        $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;
-        $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
-        $total_est_gsti = $total_est;
-    } else {
-        # if prices does not include GST
-
-        # then we use the common way to compute other values
-        $total_rrp_gste = $total_rrp;
-        $gist_rrp       = $total_rrp_gste * $gist;
-        $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
-        $total_est_gste = $total_est;
-        $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
-   }
-   $gist_est = $gist_rrp - ( $gist_rrp * $discount );
-} else {
-    $total_rrp_gste = $total_rrp_gsti = $total_rrp;
-    $total_est_gste = $total_est_gsti = $total_est;
-    $gist_rrp = $gist_est = 0;
-}
-my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
+push @foot_loop, map {$_} values %foot;
 
 my $format = "%.2f";
-$template->param(
-    total_rrp_gste => sprintf($format, $total_rrp_gste),
-    total_rrp_gsti => sprintf($format, $total_rrp_gsti),
-    total_est_gste => sprintf($format, $total_est_gste),
-    total_est_gsti => sprintf($format, $total_est_gsti),
-    gist_rrp => sprintf($format, $gist_rrp),
-    gist_est => sprintf($format, $gist_est),
-    total_gsti_shipment => sprintf($format, $total_gsti_shipment),
-    gist => sprintf($format, $gist * 100),
-);
-
 my $budgets = GetBudgets();
 my @budgets_loop;
 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
 foreach my $budget (@$budgets) {
-    next unless CanUserUseBudget($loggedinuser, $budget, $flags);
-    my %line = %{ $budget };
-    if($shipmentcost_budgetid and $budget->{budget_id} == $shipmentcost_budgetid) {
+    next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
+    my %line = %{$budget};
+    if (    $shipmentcost_budgetid
+        and $budget->{budget_id} == $shipmentcost_budgetid )
+    {
         $line{selected} = 1;
     }
     push @budgets_loop, \%line;
@@ -167,20 +157,29 @@ $template->param(
     invoiceid        => $details->{'invoiceid'},
     invoicenumber    => $details->{'invoicenumber'},
     suppliername     => $details->{'suppliername'},
-    supplierid       => $details->{'booksellerid'},
+    booksellerid     => $details->{'booksellerid'},
     datereceived     => $details->{'datereceived'},
-    shipmentdate     => $details->{'shipmentdate'},
     billingdate      => $details->{'billingdate'},
     invoiceclosedate => $details->{'closedate'},
-    shipmentcost     => sprintf($format, $details->{'shipmentcost'} || 0),
+    shipmentcost     => $details->{'shipmentcost'},
     orders_loop      => \@orders_loop,
+    foot_loop        => \@foot_loop,
     total_quantity   => $total_quantity,
+    total_gste       => sprintf( $format, $total_gste ),
+    total_gsti       => sprintf( $format, $total_gsti ),
+    total_gstvalue   => sprintf( $format, $total_gstvalue ),
+    total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
+    total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
     invoiceincgst    => $bookseller->{invoiceincgst},
-    currency         => $bookseller->{listprice},
-    DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
+    currency         => GetCurrency()->{currency},
     budgets_loop     => \@budgets_loop,
 );
 
+defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
+
+# FIXME
+# Fonction dupplicated from basket.pl
+# Code must to be exported. Where ??
 sub get_infos {
     my $order = shift;
     my $bookseller = shift;
@@ -193,9 +192,26 @@ sub get_infos {
     my %line = %{ $order };
     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
     $line{budget_name}    = $budget->{budget_name};
-    $line{total} = $qty * $order->{ecost};
+    if ( $bookseller->{'listincgst'} ) {
+        $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
+        $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
+        $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
+        $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
+        $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
+        $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
+        $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
+    } else {
+        $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
+        $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
+        $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
+        $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
+        $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
+        $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
+        $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
+    }
 
     if ( $line{uncertainprice} ) {
+        $template->param( uncertainprices => 1 );
         $line{rrp} .= ' (Uncertain)';
     }
     if ( $line{'title'} ) {