Implement sending acquisition orders to booksellers by email.
Introduces a new notice (``letter, form'') orders/ORDEREMAIL.
Cannot use the message queue since it can only mail to patrons (``borrowers'').
See Bug 5260.
---
 acqui/basketgroup.pl                               |  179 ++++++++++++++++++++
 koha-tmpl/intranet-tmpl/prog/en/js/acq.js          |    8 +
 .../prog/en/modules/acqui/basketgroup.tt           |   14 ++
 .../intranet-tmpl/prog/en/modules/tools/letter.tt  |    5 +
 tools/letter.pl                                    |   10 +
 5 files changed, 216 insertions(+), 0 deletions(-)

diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl
index 08356f8..4a4a9f2 100755
--- a/acqui/basketgroup.pl
+++ b/acqui/basketgroup.pl
@@ -248,6 +248,171 @@ sub printbasketgrouppdf{
     print $pdf;
 }
 
+sub order_mail {
+#use Data::Dumper;
+       my ($booksellerid, $mail_orders) = @_;
+#print "order_mail\n";
+#print "booksellerid: $booksellerid\n", "mail_orders: ", Dumper($mail_orders);
+       my $mail_order_total = shift @$mail_orders;
+
+       my $bookseller = GetBookSellerFromId($booksellerid);
+
+       my $letter = C4::Letters::getletter('orders', 'ORDERMAIL') || return;
+#print "letter: ",  Dumper(\$letter);
+
+       eval "use Mail::Sendmail";
+       eval "use C4::Log";
+       eval "use Carp";
+       eval "use Encode";
+
+       # branch info
+       my $userenv = C4::Context->userenv;
+       C4::Letters::parseletter($letter, 'branches', $userenv->{branch});
+
+       # librarian name
+       $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
+       $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
+       $letter->{content} =~ 
s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
+
+       # booksellers
+       C4::Letters::parseletter($letter, 'aqbooksellers', $booksellerid);
+
+       # items and total
+       return unless $letter->{'content'} =~ m/(<item>(.*)<\/item>)/sm;
+       my $item_place = $1; my $item_format = $2;
+       my ($total_place, $total_format);
+       if ($letter->{'content'} =~ m/(<total>(.*)<\/total>)/sm) {
+               $total_place = $1; $total_format = $2;
+       }
+
+       my @items;
+       foreach my $mail_order (@$mail_orders) {
+               my $item = $item_format;
+               while (my ($key, $value) = each %$mail_order) {
+#print "$key: $value\n";
+                       $item =~ s/<<orders.$key>>/$value/g;
+               }
+               push @items, $item;
+       }
+#print "items: ", Dumper(@items);
+       $letter->{'content'} =~ s/\Q$item_place\E/join "\n",@items/e;
+       if ($total_format) {
+#print "total_place: $total_place\n";
+#print "total_format: $total_format\n";
+               my $total = $total_format;
+               while (my ($key, $value) = each %$mail_order_total) {
+                       $total =~ s/<<orders.total.$key>>/$value/g;
+               }
+#print "total: $total\n";
+               $letter->{'content'} =~ s/\Q$total_place\E/$total/;
+       }
+       my %mail = (
+               To => $bookseller->{bookselleremail} ||
+                       $bookseller->{contemail} ||
+                       $userenv->{emailaddress},
+               From => $userenv->{emailaddress},
+               Subject => $letter->{title},
+               Message => Encode::encode("UTF-8", $letter->{content}),
+               'Content-Type' => 'text/plain; charset="utf8"',
+       );
+#print "mail: ", Dumper(\%mail);
+       sendmail(%mail) or carp $Mail::Sendmail::error;
+       logaction(
+               "ORDER",
+               "Send email order",
+               undef,
+               "To=%mail{To}\nSubject=%mail{Subject}\nMessage=%mail{Message}"
+       ) if C4::Context->preference("LetterLog");
+}
+
+sub mailbasketgroup {
+       my ($basketgroupid) = @_;
+    
+       eval "use C4::Branch";
+       eval "use C4::Biblio";
+       eval "use C4::Koha";
+       eval "use Number::Format qw(format_number format_price)";
+
+       my $num = FormatNumber; # C4::Output
+
+       my $itemtypes = GetItemTypes();
+    
+       my $basketgroup = GetBasketgroup($basketgroupid);
+       my $booksellerid = $basketgroup->{booksellerid};
+       my $bookseller = GetBookSellerFromId($booksellerid);
+       my $baskets = GetBasketsByBasketgroup($basketgroupid);
+
+       my $gstrate = $bookseller->{gstrate} || C4::Context->preference("gist") 
|| 0;
+       my $discount = $bookseller->{'discount'} / 100;
+
+       my $total_ecost;        # Total, its value will be assigned to 
$total_ecost_gsti or $total_ecost_gste depending of $bookseller->{'listincgst'}
+       my $total_ecost_gsti;   # Total, GST included
+       my $total_ecost_gste;   # Total, GST excluded
+       my $total_quantity;     # Total quantity
+
+       my @mail_orders;
+       for my $basket (@$baskets) {
+               my $basketno = $basket->{basketno};
+               my @orders = &GetOrders($basketno);
+               for my $order (@orders) {
+                       my %mail_order;
+
+                       my $quantity = $order->{quantity} || 0;
+                       next if $quantity <= 0;
+                       my $ecost = $order->{ecost} || 0;
+
+                       for (qw(quantity quantityreceived author title volume 
seriestitle isbn publishercode)) {
+                               $mail_order{$_} = $order->{$_} if defined 
$order->{$_};
+                       }
+                       for (qw(listprice ecost)) {
+                               $mail_order{$_} = 
$num->format_price($order->{$_}) if defined $order->{$_};
+                       }
+                       my $full_title = $order->{title};
+                       $full_title .= (" " . $order->{seriestitle}) if 
$order->{seriestitle};
+                       $full_title .= (" " . $order->{volume}) if 
$order->{volume};
+                       $mail_order{full_title} = $full_title;
+                       if ($order->{biblionumber}) {
+                               my $bibliodata = 
GetBiblioData($order->{biblionumber});
+                               if ($bibliodata->{itemtype}) {
+                                       $mail_order{itemtype} = 
$itemtypes->{$bibliodata->{itemtype}}->{description};
+                               }
+                       }
+
+                       my $quantity_ecost = $quantity * $ecost;
+                       $mail_order{quantity_ecost} = 
$num->format_price($quantity_ecost);
+                       $mail_order{basketno} = $basketno;
+                       $total_ecost += $quantity_ecost;
+                       $total_quantity += $quantity;
+                       push @mail_orders, \%mail_order;
+               }
+       }
+
+       my %total;
+       $total{quantity} = $total_quantity;
+       $total{gstrate} = $num->format_number($gstrate);
+       $total{currency} = $bookseller->{listprice};
+       $total{discount} = $num->format_number($bookseller->{discount});
+
+       my $total_gist;
+
+       if ($bookseller->{listincgst}) { # prices already includes GST
+               $total_ecost_gsti = $total_ecost;
+               $total_ecost_gste = $total_ecost_gsti / ($gstrate + 1);
+               $total_gist       = $total_ecost_gsti - $total_ecost_gste;
+       } else { # prices does not include GST
+               $total_ecost_gste = $total_ecost;
+               $total_gist = $total_ecost_gste * $gstrate;
+               $total_ecost_gsti = $total_ecost_gste + $total_gist;
+       }
+       $total{ecost_gste} = $num->format_price($total_ecost_gste);
+       $total{ecost_gsti} = $num->format_price($total_ecost_gsti);
+       $total{gist} = $num->format_number($total_gist);
+
+       unshift @mail_orders, \%total;
+       
+       order_mail($booksellerid, \@mail_orders);
+}
+
 my $op = $input->param('op');
 my $booksellerid = $input->param('booksellerid');
 $template->param(booksellerid => $booksellerid);
@@ -383,11 +548,25 @@ if ( $op eq "add" ) {
     
     printbasketgrouppdf($basketgroupid);
     exit;
+} elsif ( $op eq 'closeandmail') {
+    my $basketgroupid = $input->param('basketgroupid');
+    
+    CloseBasketgroup($basketgroupid);
+    
+    mailbasketgroup($basketgroupid);
+
+    print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' 
. $booksellerid);
 }elsif ($op eq 'print'){
     my $basketgroupid = $input->param('basketgroupid');
     
     printbasketgrouppdf($basketgroupid);
     exit;
+}elsif ($op eq 'mail'){
+    my $basketgroupid = $input->param('basketgroupid');
+    
+    mailbasketgroup($basketgroupid);
+
+    print $input->redirect('/cgi-bin/koha/acqui/basketgroup.pl?booksellerid=' 
. $booksellerid);
 }elsif( $op eq "delete"){
     my $basketgroupid = $input->param('basketgroupid');
     DelBasketgroup($basketgroupid);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js 
b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
index bcfb7b8..63d5d3a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
@@ -388,6 +388,14 @@ function closeandprint(bg){
        }
 }
 
+function closeandmail(bs, bg){
+       if(document.location = 
'/cgi-bin/koha/acqui/basketgroup.pl?op=closeandmail&amp;booksellerid=' + bs + 
'&amp;basketgroupid=' + bg ){
+               setTimeout("window.location.reload();",3000);
+       }else{
+               alert('Error downloading the file');
+       }
+}
+
 //function that lets the user unclose a basketgroup as long as he hasn't 
submitted the changes to the page.
 function unclosegroup(bgid){
     var div = 
document.getElementById('basketgroup-'+bgid+'-closed').parentNode;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt 
b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
index ba12d7b..31fa5cf 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basketgroup.tt
@@ -297,6 +297,13 @@ function submitForm(form) {
                                                        <li>
                                                                <span 
class="yui-button yui-link-button">
                                                                        <span 
class="first-child">
+                                                                               
<a href="/cgi-bin/koha/acqui/basketgroup.pl?op=closeandmail&amp;booksellerid=[% 
basketgroup.booksellerid %]&amp;basketgroupid=[% basketgroup.id %]" 
class="yui-button yui-link-button">Close & Send Email</a>
+                                                                       </span>
+                                                               </span>
+                                                               </li>
+                                                       <li>
+                                                               <span 
class="yui-button yui-link-button">
+                                                                       <span 
class="first-child">
                                                                                
<a href="?op=add&amp;booksellerid=[% basketgroup.booksellerid 
%]&amp;basketgroupid=[% basketgroup.id %]" class="yui-button yui-link-button" 
>Edit</a>
                                                                        </span>
                                                                </span>
@@ -333,6 +340,13 @@ function submitForm(form) {
                                                                                
</span>
                                                                        </span>
                                                                </li>
+                                                               <li>
+                                                                       <span 
class="yui-button yui-link-button">
+                                                                               
<span class="first-child">
+                                                                               
        <a href="/cgi-bin/koha/acqui/basketgroup.pl?op=mail&amp;booksellerid=[% 
basketgroup.booksellerid %]&amp;basketgroupid=[% basketgroup.id %]" 
class="yui-button yui-link-button">Send Email</a>
+                                                                               
</span>
+                                                                       </span>
+                                                               </li>
                                                                <li>
                                                                                
<span class="yui-button yui-link-button">
                                                                                
<span class="first-child">
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt 
b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
index 1ec73c5..a423982 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt
@@ -210,6 +210,11 @@ $(document).ready(function() {
                                     [% ELSE %]
                                     <option value="reserves">Holds</option>
                                     [% END %]
+                                    [% IF ( orders ) %]
+                                    <option value="orders" 
selected="selected">Orders</option>
+                                    [% ELSE %]
+                                    <option value="orders">Orders</option>
+                                    [% END %]
                                     [% IF ( members ) %]
                                     <option value="members" 
selected="selected">Members</option>
                                     [% ELSE %]
diff --git a/tools/letter.pl b/tools/letter.pl
index ab6039c..ff9ab60 100755
--- a/tools/letter.pl
+++ b/tools/letter.pl
@@ -154,6 +154,16 @@ sub add_form {
     elsif ($module eq 'claimacquisition') {
         push @{$field_selection}, add_fields('aqbooksellers', 'aqorders');
     }
+    elsif ($module eq 'orders') {
+        push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 
'biblio', 'items');
+        push @{$field_selection}, { value => q{}, text => '---ORDER ITEMS---' 
};
+        push @{$field_selection}, { value => "orders.$_", text => "orders.$_" }
+               foreach (qw/listprice quantity ecost quantityreceived author 
title volume seriestitle isbn publishercode/);
+        push @{$field_selection}, { value => "orders.$_", text => "orders.$_" }
+               foreach (qw/full_title itemtype quantity_ecost basketno/);
+        push @{$field_selection}, { value => "orders.total.$_", text => 
"orders.total.$_" }
+               foreach (qw/quantity gstrate currency discount ecost_gste 
ecost_gsti gstrate/);
+    }
     elsif ($module eq 'claimissues') {
         push @{$field_selection}, add_fields('aqbooksellers', 'serial', 
'subscription');
         push @{$field_selection},
-- 
1.7.2.5

_______________________________________________
Koha-patches mailing list
[email protected]
http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-patches
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to