From eb1486df737d7942bdab092dc0806cd953f42b7e Mon Sep 17 00:00:00 2001
From: Amit Gupta <amitddng135@gmail.com>
Date: Thu, 17 Oct 2013 21:48:47 +0530
Subject: [PATCH] Bug-4456-Enable addition of PO Number to order

The ability to have a running purchase order sequence

Test Plan:
1) Set systempreference EnablePONumber to Enable for auto generating purchase order number.
2) Select prefix for ex: Direct to requestor (Create PONUMPREFIX in authorized value and insert values).
3) Create an basket.
4) Create an order.
5) Select prefix value for ex: Direct to requestor.
6) Purchase order number will come in purchase order section for ex: DIR0000001.
7) Click on save button.
---
 C4/Acquisition.pm                                  |  2 +-
 acqui/neworderempty.pl                             |  4 ++
 acqui/poseqnextvalue.pl                            | 45 ++++++++++++++++++++++
 .../prog/en/modules/acqui/neworderempty.tt         | 44 +++++++++++++++++++++
 .../en/modules/admin/preferences/acquisitions.pref |  6 +++
 5 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100755 acqui/poseqnextvalue.pl

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 8c40dcd..53a8982 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -1165,7 +1165,7 @@ sub ModOrder {
         #See report 10110 and guided_reports.pl
     my $query = "UPDATE aqorders SET ";
 
-    foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
+    foreach my $orderinfokey (keys %$orderinfo){
         # ... and skip hash entries that are not in the aqorders table
         # FIXME : probably not the best way to do it (would be better to have a correct hash)
         next unless grep(/^$orderinfokey$/, @$colnames);
diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl
index 9c9eed3..157761f 100755
--- a/acqui/neworderempty.pl
+++ b/acqui/neworderempty.pl
@@ -357,11 +357,15 @@ my @gst_values = map {
     option => $_
 }, split( '\|', C4::Context->preference("gist") );
 
+
 $template->param(
     existing         => $biblionumber,
     ordernumber           => $ordernumber,
     # basket informations
     basketno             => $basketno,
+    purchaseordernumber => $data->{purchaseordernumber},
+    prefix           => GetAuthorisedValues('PONUMPREFIX'),
+    enableponumber => C4::Context->preference('EnablePONumber'),
     basketname           => $basket->{'basketname'},
     basketnote           => $basket->{'note'},
     booksellerid         => $basket->{'booksellerid'},
diff --git a/acqui/poseqnextvalue.pl b/acqui/poseqnextvalue.pl
new file mode 100755
index 0000000..81b6183
--- /dev/null
+++ b/acqui/poseqnextvalue.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright (C) Amit Gupta (amitddng135@gmail.com)
+#
+# 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>.
+
+use strict;
+use warnings;
+use CGI;
+use C4::Auth;
+use C4::Output;
+use JSON;
+
+my $input = new CGI;
+
+my $generateno = sprintf( "%0*d", "7", getnext() );
+
+print $input->header('application/json');
+my $json = { generateno => $generateno, };
+my $json_text = to_json( $json, { utf8 => 1 } );
+print $json_text;
+
+sub getnext {
+    my $dbh = C4::Context->dbh;
+    # next two lines should be wrapped in a transaction
+    $dbh->do(
+q{UPDATE po_sequence set seq_value = seq_value + 1 where seqid = 'POSEQ'}
+    );
+    my @row = $dbh->selectrow_array(
+        q{SELECT seq_value from po_sequence where seqid = 'POSEQ'});
+    return $row[0];
+}
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
index c261530..08d548e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt
@@ -144,6 +144,24 @@ $(document).ready(function()
     });
 //]]>
 </script>
+<script language="Javascript" type="text/javascript">
+    $(document).ready(function(){
+    $('#ponumber').change(function() {
+    $.ajax({url:"/cgi-bin/koha/acqui/poseqnextvalue.pl",
+        type: "get",
+        async: false,
+        dataType: 'json',
+        success:function(generateno) {
+            var len =  document.getElementsByName("purchaseordernumber").length - 1;
+            var t1 = document.getElementById('ponumber').value;
+            var t2 = generateno.generateno;
+            if (t1.length > 0 && t2.length > 0) {
+                document.getElementsByName("purchaseordernumber")[len].value = t1+t2;
+            }
+        }});
+    });
+});
+</script>
 </head>
 <body id="acq_neworderempty" class="acq">
 
@@ -538,6 +556,32 @@ $(document).ready(function()
                 <label for="notes">Notes: </label>
                 <textarea id="notes" cols="30" rows="3" name="notes" >[% IF ( notes ) %][% notes %][% END %]</textarea>
             </li>
+            [% IF enableponumber %]
+            [% IF ( ordernumber ) %]
+            <li>
+                <label for="purchaseordernumber">Purchase order: </label>
+                <input type="text" id="purchaseordernumber" size="20" name="purchaseordernumber" value="[% purchaseordernumber %]"/>
+            </li>
+            [% ELSE %]
+            <li><p>Select ordernumber prefix for auto-generated purchase order number.</p></li>
+            <li>
+                <label for="prefix">Prefix:</label>
+                [% IF prefix %]
+                    <select id="ponumber" onchange="Calculatepurorderno(this.value)">
+                    <option value="">Select</option>
+                    [% FOREACH d IN prefix %]
+                        <option value="[% d.authorised_value %]">[% d.lib %]</option>
+                    [% END %]
+                    </select>
+                    [% ELSE %]
+                        <span id="pordernumber">No prefix authorized values found! Please create one or more authorized values with the category PONUMPREFIX.</span>
+                [% END %]
+            </li>
+                <label for="purchaseordernumber">Purchase order: </label>
+                <input type="text" id="purchaseordernumber" size="20" name="purchaseordernumber" value="[% purchaseordernumber %]"/>
+            </li>
+            [% END %]
+            [% END %]
             <li><div class="hint">The 2 following fields are available for your own usage. They can be useful for statistical purposes</div>
                 <label for="sort1">Statistic 1: </label>
                 <span id="sort1_zone">
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref
index f74f991..4a1a6c2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref
@@ -42,6 +42,12 @@ Acquisitions:
                   yes: Warn
                   no: "Do not warn"
             - when the librarian tries to create an invoice with a duplicate number.
+        -
+            - pref: EnablePONumber
+              choices:
+                  yes: Enable
+                  no: Disable
+            - Enable purchase order number.
 
     Printing:
         -
-- 
1.8.4.rc3