X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=acqui%2Fbasketgroup.pl;h=d2e185624e34411eb8c5b54c40a794ba823db37c;hb=263e5724eb1c9f1e787474ec92403179496c03b4;hp=e8027a811cbcfe490bd2f33e29671eaae195c87b;hpb=aef645e9e5bb85d66ca9aae4d62a3b626ed89602;p=koha_gimpoz diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl index e8027a811c..d2e185624e 100755 --- a/acqui/basketgroup.pl +++ b/acqui/basketgroup.pl @@ -4,6 +4,7 @@ #written by john.soros@biblibre.com 01/10/2008 # Copyright 2008 - 2009 BibLibre SARL +# Parts Copyright Catalyst 2010 # # This file is part of Koha. # @@ -16,9 +17,9 @@ # 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, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. =head1 NAME @@ -44,6 +45,7 @@ The bookseller who we want to display the baskets (and basketgroups) of. use strict; use warnings; +use Carp; use C4::Input; use C4::Auth; @@ -51,8 +53,10 @@ use C4::Output; use CGI; use C4::Bookseller qw/GetBookSellerFromId/; -use C4::Acquisition qw/GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup/; +use C4::Acquisition qw/CloseBasketgroup ReOpenBasketgroup GetOrders GetBasketsByBasketgroup GetBasketsByBookseller ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroups ModBasket GetBasketgroup GetBasket/; use C4::Bookseller qw/GetBookSellerFromId/; +use C4::Branch qw/GetBranches/; +use C4::Members qw/GetMember/; my $input=new CGI; @@ -135,8 +139,8 @@ sub BasketTotal { my @orders = GetOrders($basketno); for my $order (@orders){ $total = $total + ( $order->{ecost} * $order->{quantity} ); - if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} || C4::Context->preference("gist") )) { - my $gst = $bookseller->{gstrate} || C4::Context->preference("gist"); + if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) { + my $gst = $bookseller->{gstrate} // C4::Context->preference("gist"); $total = $total * ( $gst / 100 +1); } } @@ -169,17 +173,115 @@ sub displaybasketgroups { if( ! @$baskets[$i]->{'closedate'} ) { splice(@$baskets, $i, 1); --$i; + }else{ + @$baskets[$i]->{total} = BasketTotal(@$baskets[$i]->{basketno}, $bookseller); } } $template->param(baskets => $baskets); $template->param( booksellername => $bookseller ->{'name'}); } +sub printbasketgrouppdf{ + my ($basketgroupid) = @_; + + my $pdfformat = C4::Context->preference("OrderPdfFormat"); + if ($pdfformat eq 'pdfformat::layout3pages' || $pdfformat eq 'pdfformat::layout2pages'){ + eval { + eval "require $pdfformat"; + import $pdfformat; + }; + if ($@){ + } + } + else { + print $input->header; + print $input->start_html; # FIXME Should do a nicer page + print "

Invalid PDF Format set

"; + print "Please go to the systempreferences and set a valid pdfformat"; + exit; + } + + my $basketgroup = GetBasketgroup($basketgroupid); + my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'}); + my $baskets = GetBasketsByBasketgroup($basketgroupid); + + my %orders; + for my $basket (@$baskets) { + my @ba_orders; + my @ords = &GetOrders($basket->{basketno}); + for my $ord (@ords) { + # ba_order is filled with : + # 0 1 2 3 4 5 6 7 8 9 + #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate + my @ba_order; + if ( $ord->{biblionumber} && $ord->{quantity}> 0 ) { + eval { + require C4::Biblio; + import C4::Biblio; + }; + if ($@){ + croak $@; + } + eval { + require C4::Koha; + import C4::Koha; + }; + if ($@){ + croak $@; + } + my $bib = GetBiblioData($ord->{biblionumber}); + my $itemtypes = GetItemTypes(); + if($ord->{isbn}){ + push(@ba_order, $ord->{isbn}); + } else { + push(@ba_order, undef); + } + if ($ord->{itemtype} and $bib->{itemtype}){ + push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description}); + } else { + push(@ba_order, undef); + } +# } else { +# push(@ba_order, undef, undef); + for my $key (qw/author title publishercode quantity listprice ecost/) { + push(@ba_order, $ord->{$key}); #Order lines + } + push(@ba_order, $bookseller->{discount}); + push(@ba_order, $bookseller->{gstrate}*100 // C4::Context->preference("gist") // 0); + push(@ba_orders, \@ba_order); + # Editor Number + my $en; + my $marcrecord=eval{MARC::Record::new_from_xml( $ord->{marcxml},'UTF-8' )}; + if ($marcrecord){ + if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) { + $en = $marcrecord->subfield( '345', "b" ); + } elsif ( C4::Context->preference("marcflavour") eq 'MARC21' ) { + $en = $marcrecord->subfield( '037', "a" ); + } + } + if($en){ + push(@ba_order, $en); + } else { + push(@ba_order, undef); + } + } + } + $orders{$basket->{basketno}}=\@ba_orders; + } + print $input->header( + -type => 'application/pdf', + -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf' + ); + my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed"; + print $pdf; + +} my $op = $input->param('op'); my $booksellerid = $input->param('booksellerid'); +$template->param(booksellerid => $booksellerid); -if (! $op ) { +if ( $op eq "add" ) { if(! $booksellerid){ $template->param( ungroupedlist => 1); my @booksellers = GetBookSeller(''); @@ -197,8 +299,63 @@ if (! $op ) { } } } else { + my $basketgroupid = $input->param('basketgroupid'); + my $billingplace; + my $deliveryplace; + my $freedeliveryplace; + if ( $basketgroupid ) { + # Get the selected baskets in the basketgroup to display them + my $selecteds = GetBasketsByBasketgroup($basketgroupid); + foreach (@{$selecteds}){ + $_->{total} = BasketTotal($_->{basketno}, $_); + } + $template->param(basketgroupid => $basketgroupid, + selectedbaskets => $selecteds); + + # Get general informations about the basket group to prefill the form + my $basketgroup = GetBasketgroup($basketgroupid); + $template->param( + name => $basketgroup->{name}, + deliverycomment => $basketgroup->{deliverycomment}, + freedeliveryplace => $basketgroup->{freedeliveryplace}, + ); + $billingplace = $basketgroup->{billingplace}; + $deliveryplace = $basketgroup->{deliveryplace}; + $freedeliveryplace = $basketgroup->{freedeliveryplace}; + } + + # determine default billing and delivery places depending on librarian homebranch and existing basketgroup data + my $borrower = GetMember( ( 'borrowernumber' => $loggedinuser ) ); + $billingplace = $billingplace || $borrower->{'branchcode'}; + $deliveryplace = $deliveryplace || $borrower->{'branchcode'}; + + my $branches = GetBranches; + + # Build the combobox to select the billing place + my @billingplaceloop; + for (sort keys %$branches) { + push @billingplaceloop, { + value => $_, + selected => $_ eq $billingplace, + branchname => $branches->{$_}->{branchname}, + }; + } + $template->param( billingplaceloop => \@billingplaceloop ); + + # Build the combobox to select the delivery place + my @deliveryplaceloop; + for (sort keys %$branches) { + push @deliveryplaceloop, { + value => $_, + selected => $_ eq $deliveryplace, + branchname => $branches->{$_}->{branchname}, + }; + } + $template->param( deliveryplaceloop => \@deliveryplaceloop ); + $template->param( booksellerid => $booksellerid ); } + $template->param(grouping => 1); my $basketgroups = &GetBasketgroups($booksellerid); my $bookseller = &GetBookSellerFromId($booksellerid); my $baskets = &GetBasketsByBookseller($booksellerid); @@ -248,56 +405,84 @@ if (! $op ) { $baskets = &GetBasketsByBookseller($booksellerid); displaybasketgroups($basketgroups, $bookseller, $baskets); -} elsif ( $op eq 'printbgroup') { - my $pdfformat = C4::Context->preference("pdfformat"); - eval "use $pdfformat" ; - eval "use C4::Branch"; - my $basketgroupid = $input->param('bgroupid'); - my $basketgroup = GetBasketgroup($basketgroupid); - my $bookseller = GetBookSellerFromId($basketgroup->{'booksellerid'}); - my $baskets = GetBasketsByBasketgroup($basketgroupid); - my %orders; - for my $basket (@$baskets) { - my @ba_orders; - my @ords = &GetOrders($basket->{basketno}); - for my $ord (@ords) { - # ba_order is filled with : - # 0 1 2 3 4 5 6 7 8 9 - #isbn, itemtype, author, title, publishercode, quantity, listprice ecost discount gstrate - my @ba_order; - if ( $ord->{biblionumber} && $ord->{quantity}> 0 ) { - eval "use C4::Biblio"; - eval "use C4::Koha"; - my $bib = GetBiblioData($ord->{biblionumber}); - my $itemtypes = GetItemTypes(); - if($ord->{isbn}){ - push(@ba_order, $ord->{isbn}); - } else { - push(@ba_order, undef); - } - if ($ord->{itemtype}){ - push(@ba_order, $itemtypes->{$bib->{itemtype}}->{description}) if $bib->{itemtype}; - } else { - push(@ba_order, undef); - } -# } else { -# push(@ba_order, undef, undef); - for my $key (qw/author title publishercode quantity listprice ecost/) { - push(@ba_order, $ord->{$key}); #Order lines - } - push(@ba_order, $bookseller->{discount}); - push(@ba_order, $bookseller->{gstrate}*100 || C4::Context->preference("gist") || 0); - push(@ba_orders, \@ba_order); - } +} elsif ( $op eq 'closeandprint') { + my $basketgroupid = $input->param('basketgroupid'); + + CloseBasketgroup($basketgroupid); + + printbasketgrouppdf($basketgroupid); + exit; +}elsif ($op eq 'print'){ + my $basketgroupid = $input->param('basketgroupid'); + + printbasketgrouppdf($basketgroupid); + exit; +}elsif( $op eq "delete"){ + my $basketgroupid = $input->param('basketgroupid'); + DelBasketgroup($basketgroupid); + print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid); + +}elsif ( $op eq 'reopen'){ + my $basketgroupid = $input->param('basketgroupid'); + my $booksellerid = $input->param('booksellerid'); + + ReOpenBasketgroup($basketgroupid); + + print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid . '#closed'); + +} elsif ( $op eq 'attachbasket') { + + # Getting parameters + my $basketgroup = {}; + my @baskets = $input->param('basket'); + my $basketgroupid = $input->param('basketgroupid'); + my $basketgroupname = $input->param('basketgroupname'); + my $booksellerid = $input->param('booksellerid'); + my $billingplace = $input->param('billingplace'); + my $deliveryplace = $input->param('deliveryplace'); + my $freedeliveryplace = $input->param('freedeliveryplace'); + my $deliverycomment = $input->param('deliverycomment'); + my $close = $input->param('close') ? 1 : 0; + # If we got a basketgroupname, we create a basketgroup + if ($basketgroupid) { + $basketgroup = { + name => $basketgroupname, + id => $basketgroupid, + basketlist => \@baskets, + billingplace => $billingplace, + deliveryplace => $deliveryplace, + freedeliveryplace => $freedeliveryplace, + deliverycomment => $deliverycomment, + closed => $close, + }; + ModBasketgroup($basketgroup); + if($close){ + } - %orders->{$basket->{basketno}}=\@ba_orders; + }else{ + $basketgroup = { + name => $basketgroupname, + booksellerid => $booksellerid, + basketlist => \@baskets, + deliveryplace => $deliveryplace, + freedeliveryplace => $freedeliveryplace, + deliverycomment => $deliverycomment, + closed => $close, + }; + $basketgroupid = NewBasketgroup($basketgroup); } - print $input->header( -type => 'application/pdf', -attachment => 'basketgroup.pdf' ); - my $branch = GetBranchInfo(GetBranch($input, GetBranches())); - $branch = @$branch[0]; - my $pdf = printpdf($basketgroup, $bookseller, $baskets, $branch, \%orders, $bookseller->{gstrate} || C4::Context->preference("gist")) || die "pdf generation failed"; - print $pdf; - exit; + + my $url = '/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' . $booksellerid; + $url .= "&closed=1" if ($input->param("closed")); + print $input->redirect($url); + +}else{ + my $basketgroups = &GetBasketgroups($booksellerid); + my $bookseller = &GetBookSellerFromId($booksellerid); + my $baskets = &GetBasketsByBookseller($booksellerid); + + displaybasketgroups($basketgroups, $bookseller, $baskets); } +$template->param(closed => $input->param("closed")); #prolly won't use all these, maybe just use print, the rest can be done inside validate output_html_with_http_headers $input, $cookie, $template->output;