Koha/acqui/pdfformat/layout3pagesfr.pm
Jonathan Druart 19a977dc7b Bug 15758: Koha::Libraries - Remove GetBranchName
This is the fourth and last patch set to remove C4::Branch.
The real purpose of this patch is to standardise and refactor some code
which is related to the libraries selection/display.
Its unconfessed purpose is to remove the C4::Branch package.

Before this patch set, only 6 subroutines still existed in the C4::Branch
package:
- GetBranchName
- GetBranchesLoop
- mybranch
- onlymine
- GetBranches
- GetBranch

GetBranchName basically returns the branchname for a given branchcode.
The branchname is only used for a display purpose and we don't need to
retrieve it in package or pl scripts (unless for a few exceptions).
We have a `Branches` template plugin with a `GetName` method which does
exactly this job.
To achieve this removal, we will use this template plugin and delete the
GetBranchName from pl and pm files.
The `Branches.all()` will now select the library of the logged in user
if no `selected` parameter has been passed.
This new behavior could cause regressions, for instance there are some
places where we do not want an option preselected (batch item
modification for instance), keep that in mind when testing.

GetBranchesLoop took 3 parameters: $branch and $onlymine.
The first one was used to set a "selected" flag, for a display purpose:
select an option in the libraries dropdown lists.
The second one was useless: If not passed or set to 0, the
`C4::Branch::onlymine` subroutine was called.
This onlymine flag was use to know if the logged in user was able to see
other libraries infos.
A patron can see the infos from other libraries if IndependentBranches
is not set OR if he has the superlibrarian permission.
Prior to this patch set, the "onlymine test" was done on different
places (neworderempty.pl, additem.pl, holidays.pl, etc.), including the
Branches TT plugin. In this patch set, this test is only done on one
place (C4::Context::only_my_library, code moved from
C4::Branch::onlymine).
To accomplish the same job as this subroutine, we just need to call the
`Branches.all()` method from the `Branches` TT plugin. It already
accepts a `selected` parameter to set a flag on the option to select.
To avoid the repetitive
  [% IF selected %]<option selected="selected">[% ELSE %]<option>[% END %]
pattern, a new `html_helpers` TT include file has been created, it
defines an `options_for_libraries` block, which takes a `selected`
parameter. We could imagine to use this include file for other
selects.

The 'mybranch` and `onlymine` subroutines of the C4::Branch package have
been moved to C4::Context. onlymine has been renamed with
only_my_library. There are only 4 occurrences of it, against 11 before
this patch set.
There 2 subroutines are Context-centric and it makes sense to put them
in `C4::Context` (at least it's the least worst place!)

GetBranches is the tricky part of this patch set: It retrieves all the
libraries, independently of the value of IndependentBranches.
To keep the same way as the existing calls of `Branches.all()`, I have
added a `unfiltered` parameter. If set, the `Branches.all()` will call
a usual Koha::Libraries->search method, otherwise
Koha::Libraries->search_filtered will be called. This new method will
check if the logged in user is allowed to see other libraries or only
its library.
Note that this `GetBranches` subroutine also created a `category` key:
it allowed to get the list of groups (of libraries) where this library
existed. Thanks to a previous patch set (bug 15295), this value was
not used anymore (I may have missed something!).

Note that the only use of `GetBranch` was buggy (see bug 15746).

Test plan (for the whole patch set):
The best way to test this whole patch set is to test with 2 instances: 1
with the patch set applied, 1 using master, to be sure there is no
regression.
It would be good to test the same with `IndependentBranches` and the
without `IndependentBranches`.
No difference should be found.
The tester must focus on the library dropdowns on as many forms as
possible.
You will notice changes in the order of the options: the libraries will
now be ordered by branchname (instead of branchcode in some places).
A special attention will be given to the following page:
- acqui/neworderempty.pl
- catalogue/search.pl
- members/members-home.pl (header?)
- opac/opac-topissues.pl
- tools/holidays.pl
- admin/branch_transfer_limits.pl
- admin/item_circulation_alerts.pl
- rotating_collections/transferCollection.pl
- suggestion/suggestion.pl
- tools/export.pl

Notes for QA:
- There are 2 FIXMEs in the patch set, I have kept the existing behavior,
but I am not sure it's the good one. Feel free to open a bug report and
I will fill a patch if you think it's not correct. Otherwise, remove the
FIXME lines in a follow-up patch.
- The whole patch set is huge and makes a lot of changes.
But it finally will tremendously reduce the number of lines:
716 insertions for 1910 deletions

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
2016-09-08 14:36:01 +00:00

434 lines
19 KiB
Perl

#!/usr/bin/perl
#example script to print a basketgroup
#written 07/11/08 by john.soros@biblibre.com and paul.poulain@biblibre.com
# Copyright 2008-2009, 2013 BibLibre SARL
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, see <http://www.gnu.org/licenses>.
#you can use any PDF::API2 module, all you need to do is return the stringifyed pdf object from the printpdf sub.
package pdfformat::layout3pagesfr;
use vars qw(@ISA @EXPORT);
use MIME::Base64;
use List::MoreUtils qw/uniq/;
use strict;
use warnings;
use utf8;
use Koha::Number::Price;
use Koha::DateUtils;
use Koha::Libraries;
BEGIN {
use Exporter ();
our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@ISA = qw(Exporter);
@EXPORT = qw(printpdf);
}
#be careful, all the sizes (height, width, etc...) are in mm, not PostScript points (the default measurment of PDF::API2).
#The constants exported transform that into PostScript points (/mm for milimeter, /in for inch, pt is postscript point, and as so is there only to show what is happening.
use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;
use PDF::API2;
#A4 paper specs
my ($height, $width) = (297, 210);
use PDF::Table;
sub printorders {
my ($pdf, $basketgroup, $baskets, $orders) = @_;
my $cur_format = C4::Context->preference("CurrencyFormat");
$pdf->mediabox($height/mm, $width/mm);
my $number = 3;
for my $basket (@$baskets){
my $page = $pdf->page();
my $billing_library = Koha::Libraries->find( $basket->{billingplace} );
my $delivery_library = Koha::Libraries->find( $basket->{deliveryplace} );
# print basket header (box)
my $box = $page->gfx;
$box->rectxy(($width - 10)/mm, ($height - 5)/mm, 10/mm, ($height - 25)/mm);
$box->stroke;
# $box->restore();
# create a text
my $text = $page->text;
# add basketgroup number
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
$text->translate(20/mm, ($height-15)/mm);
$text->text("Commande N°".$basketgroup->{'id'}.". Panier N° ".$basket->{basketno}.". ".$basket->{booksellernote});
$text->translate(20/mm, ($height-20)/mm);
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
$text->text( ( $billing_library ? "Facturation à " . $billing_library->branchname : "" )
. ( $billing_library and $delivery_library ? " et " : "" )
. ( $delivery_library ? "livraison à " . $delivery_library->branchname : "" )
);
my $pdftable = new PDF::Table();
my $abaskets;
my $arrbasket;
my @keys = ('Document', 'Qté', 'Prix', 'Prix net', '% Remise', 'Remise', 'Taux TVA', 'Total HT', 'Total TTC');
for my $bkey (@keys) {
push(@$arrbasket, $bkey);
}
push(@$abaskets, $arrbasket);
my $titleinfo;
foreach my $line (@{$orders->{$basket->{basketno}}}) {
$arrbasket = undef;
$titleinfo = "";
if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
$titleinfo = $line->{title} . " / " . $line->{author} .
( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
( $line->{en} ? " EN : " . $line->{en} : '' ) .
( $line->{itemtype} ? ", " . $line->{itemtype} : '' ) .
( $line->{edition} ? ", " . $line->{edition} : '' ) .
( $line->{publishercode} ? ' publié par '. $line->{publishercode} : '') .
( $line->{publicationyear} ? ', '. $line->{publicationyear} : '');
}
else { # MARC21, NORMARC
$titleinfo = $line->{title} . " " . $line->{author} .
( $line->{isbn} ? " ISBN : " . $line->{isbn} : '' ) .
( $line->{en} ? " EN : " . $line->{en} : '' ) .
( $line->{itemtype} ? " " . $line->{itemtype} : '' ) .
( $line->{edition} ? ", " . $line->{edition} : '' ) .
( $line->{publishercode} ? ' publié par '. $line->{publishercode} : '') .
( $line->{copyrightdate} ? ' '. $line->{copyrightdate} : '');
}
push( @$arrbasket,
$titleinfo. ($line->{order_vendornote} ? "\n----------------\nNote pour le fournisseur : ". $line->{order_vendornote} : '' ),
$line->{quantity},
Koha::Number::Price->new( $line->{rrpgste})->format,
Koha::Number::Price->new( $line->{rrpgsti})->format,
Koha::Number::Price->new( $line->{discount})->format.'%',
Koha::Number::Price->new( $line->{rrpgste} - $line->{ecostgste})->format,
Koha::Number::Price->new( $line->{gstrate} * 100)->format.'%',
Koha::Number::Price->new( $line->{totalgste})->format,
Koha::Number::Price->new( $line->{totalgsti})->format,
);
push(@$abaskets, $arrbasket);
}
$pdftable->table($pdf, $page, $abaskets,
x => 10/mm,
w => ($width - 20)/mm,
start_y => 270/mm,
next_y => 285/mm,
start_h => 250/mm,
next_h => 250/mm,
padding => 5,
padding_right => 5,
background_color_odd => "lightgray",
font => $pdf->corefont("Times", -encoding => "utf8"),
font_size => 3/mm,
header_props => {
font => $pdf->corefont("Times", -encoding => "utf8"),
font_size => 9,
bg_color => 'gray',
repeat => 1,
},
column_props => [
{
min_w => 85/mm, # Minimum column width.
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
{
justify => 'right', # One of left|right ,
},
],
);
}
$pdf->mediabox($width/mm, $height/mm);
}
sub printbaskets {
my ($pdf, $basketgroup, $hbaskets, $bookseller, $GSTrate, $orders) = @_;
# get library name
my $libraryname = C4::Context->preference("LibraryName");
my $cur_format = C4::Context->preference("CurrencyFormat");
$pdf->mediabox($width/mm, $height/mm);
my $page = $pdf->openpage(2);
# create a text
my $text = $page->text;
# add basketgroup number
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
$text->translate(($width-40)/mm, ($height-53)/mm);
$text->text("".$basketgroup->{'id'});
# print the libraryname in the header
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
$text->translate(30/mm, ($height-28.5)/mm);
$text->text($libraryname);
my $pdftable = new PDF::Table();
my $abaskets;
my $arrbasket;
# header of the table
my @keys = ('Lot', 'Panier', 'Prix', 'Prix net', 'Taux TVA', 'TVA', 'Remise', 'Total HT', 'Total TTC');
for my $bkey (@keys) {
push(@$arrbasket, $bkey);
}
my ($grandtotalrrpgsti, $grandtotalrrpgste, $grandtotalgsti, $grandtotalgste, $grandtotalgstvalue, $grandtotaldiscount);
# calculate each basket total
push(@$abaskets, $arrbasket);
for my $basket (@$hbaskets) {
my @gst;
$arrbasket = undef;
my ($totalrrpgste, $totalrrpgsti, $totalgste, $totalgsti, $totalgstvalue, $totaldiscount);
my $ords = $orders->{$basket->{basketno}};
my $ordlength = @$ords;
foreach my $ord (@$ords) {
$totalgste += $ord->{totalgste};
$totalgsti += $ord->{totalgsti};
$totalgstvalue += $ord->{gstvalue};
$totaldiscount += ($ord->{rrpgste} - $ord->{ecostgste} ) * $ord->{quantity};
$totalrrpgste += $ord->{rrpgste} * $ord->{quantity};
$totalrrpgsti += $ord->{rrpgsti} * $ord->{quantity};
push @gst, $ord->{gstrate};
}
@gst = uniq map { $_ * 100 } @gst;
$grandtotalrrpgste += $totalrrpgste;
$grandtotalrrpgsti += $totalrrpgsti;
$grandtotalgsti += $totalgsti;
$grandtotalgste += $totalgste;
$grandtotalgstvalue += $totalgstvalue;
$grandtotaldiscount += $totaldiscount;
my @gst_string =
map { Koha::Number::Price->new($_)->format . '%' } @gst;
push(@$arrbasket,
$basket->{contractname},
$basket->{basketname} . ' (No. ' . $basket->{basketno} . ')',
Koha::Number::Price->new( $totalrrpgste )->format,
Koha::Number::Price->new( $totalrrpgsti )->format,
"@gst_string",
Koha::Number::Price->new( $totalgstvalue )->format,
Koha::Number::Price->new( $totaldiscount )->format,
Koha::Number::Price->new( $totalgste )->format,
Koha::Number::Price->new( $totalgsti )->format,
);
push(@$abaskets, $arrbasket);
}
# now, push total
undef $arrbasket;
push @$arrbasket,
'',
'Total',
Koha::Number::Price->new( $grandtotalrrpgste )->format,
Koha::Number::Price->new( $grandtotalrrpgsti )->format,
'',
Koha::Number::Price->new( $grandtotalgstvalue )->format,
Koha::Number::Price->new( $grandtotaldiscount )->format,
Koha::Number::Price->new( $grandtotalgste )->format,
Koha::Number::Price->new( $grandtotalgsti )->format;
push @$abaskets,$arrbasket;
# height is width and width is height in this function, as the pdf is in landscape mode for the Tables.
$pdftable->table($pdf, $page, $abaskets,
x => 5/mm,
w => ($width - 10)/mm,
start_y => 230/mm,
next_y => 230/mm,
start_h => 230/mm,
next_h => 230/mm,
font => $pdf->corefont("Times", -encoding => "utf8"),
font_size => 3/mm,
padding => 5,
padding_right => 10,
background_color_odd => "lightgray",
header_props => {
bg_color => 'gray',
repeat => 1,
},
column_props => [
{
},
{
},
{
justify => 'right',
},
{
justify => 'right',
},
{
justify => 'right',
},
{
justify => 'right',
},
{
justify => 'right',
},
],
);
$pdf->mediabox($height/mm, $width/mm);
}
sub printhead {
my ($pdf, $basketgroup, $bookseller) = @_;
# get library name
my $libraryname = C4::Context->preference("LibraryName");
my $billing_library = Koha::Libraries->find( $basketgroup->{billingplace} );
my $delivery_library = Koha::Libraries->find( $basketgroup->{deliveryplace} );
my $freedeliveryplace = $basketgroup->{freedeliveryplace};
# get the subject
my $subject;
# open 1st page (with the header)
my $page = $pdf->openpage(1);
# create a text
my $text = $page->text;
# print the libraryname in the header
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 6/mm );
$text->translate(30/mm, ($height-28.5)/mm);
$text->text($libraryname);
# print order info, on the default PDF
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 8/mm );
$text->translate(100/mm, ($height-5-48)/mm);
$text->text($basketgroup->{'id'});
# print the date
my $today = output_pref({ dt => dt_from_string, dateonly => 1 });
$text->translate(130/mm, ($height-5-48)/mm);
$text->text($today);
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 4/mm );
# print billing infos
$text->translate(100/mm, ($height-86)/mm);
$text->text($libraryname);
$text->translate(100/mm, ($height-97)/mm);
$text->text($billing_library->branchname);
$text->translate(100/mm, ($height-108.5)/mm);
$text->text($billing_library->branchphone);
$text->translate(100/mm, ($height-115.5)/mm);
$text->text($billing_library->branchfax);
$text->translate(100/mm, ($height-122.5)/mm);
$text->text($billing_library->branchaddress1);
$text->translate(100/mm, ($height-127.5)/mm);
$text->text($billing_library->branchaddress2);
$text->translate(100/mm, ($height-132.5)/mm);
$text->text($billing_library->branchaddress3);
$text->translate(100/mm, ($height-137.5)/mm);
$text->text(join(' ', $billing_library->branchzip, $billing_library->branchcity, $billing_library->branchcountry));
$text->translate(100/mm, ($height-147.5)/mm);
$text->text($billing_library->branchemail);
# print subject
$text->translate(100/mm, ($height-145.5)/mm);
$text->text($subject);
# print bookseller infos
$text->translate(100/mm, ($height-180)/mm);
$text->text($bookseller->{name});
$text->translate(100/mm, ($height-185)/mm);
$text->text($bookseller->{postal});
$text->translate(100/mm, ($height-190)/mm);
$text->text($bookseller->{address1});
$text->translate(100/mm, ($height-195)/mm);
$text->text($bookseller->{address2});
$text->translate(100/mm, ($height-200)/mm);
$text->text($bookseller->{address3});
$text->translate(100/mm, ($height-205)/mm);
$text->text($bookseller->{accountnumber});
# print delivery infos
$text->font( $pdf->corefont("Times-Bold", -encoding => "utf8"), 4/mm );
$text->translate(50/mm, ($height-237)/mm);
if ($freedeliveryplace) {
my $start = 242;
my @fdp = split('\n', $freedeliveryplace);
foreach (@fdp) {
$text->text($_);
$text->translate( 50 / mm, ( $height - $start ) / mm );
$start += 5;
}
} else {
$text->text($delivery_library->branchaddress1);
$text->translate(50/mm, ($height-242)/mm);
$text->text($delivery_library->branchaddress2);
$text->translate(50/mm, ($height-247)/mm);
$text->text($delivery_library->branchaddress3);
$text->translate(50/mm, ($height-252)/mm);
$text->text(join(' ', $delivery_library->branchzip, $delivery_library->branchcity, $delivery_library->branchcountry));
}
$text->translate(50/mm, ($height-262)/mm);
$text->text($basketgroup->{deliverycomment});
}
sub printfooters {
my ($pdf) = @_;
for (my $i=1;$i <= $pdf->pages;$i++) {
my $page = $pdf->openpage($i);
my $text = $page->text;
$text->font( $pdf->corefont("Times", -encoding => "utf8"), 3/mm );
$text->translate(10/mm, 10/mm);
$text->text("Page $i / ".$pdf->pages);
}
}
sub printpdf {
my ($basketgroup, $bookseller, $baskets, $orders, $GST) = @_;
# open the default PDF that will be used for base (1st page already filled)
my $pdf_template = C4::Context->config('intrahtdocs') . '/' . C4::Context->preference('template') . '/pdf/layout3pagesfr.pdf';
my $pdf = PDF::API2->open($pdf_template);
$pdf->pageLabel( 0, {
-style => 'roman',
} ); # start with roman numbering
# fill the 1st page (basketgroup information)
printhead($pdf, $basketgroup, $bookseller);
# fill the 2nd page (orders summary)
printbaskets($pdf, $basketgroup, $baskets, $bookseller, $GST, $orders);
# fill other pages (orders)
printorders($pdf, $basketgroup, $baskets, $orders);
# print something on each page (usually the footer, but you could also put a header
printfooters($pdf);
return $pdf->stringify;
}
1;