Index: trunk/cgi-bin/IPDB.pm
===================================================================
--- trunk/cgi-bin/IPDB.pm	(revision 945)
+++ trunk/cgi-bin/IPDB.pm	(revision 947)
@@ -1,11 +1,21 @@
 # ipdb/cgi-bin/IPDB.pm
 # Contains functions for IPDB - database access, subnet mangling, block allocation, etc
-###
-# SVN revision info
-# $Date$
-# SVN revision $Rev$
-# Last update by $Author$
-###
-# Copyright (C) 2004-2016 - Kris Deugau
+##
+# $Id$
+# Copyright (C) 2004-2018,2022-2024 - Kris Deugau <kdeugau@deepnet.cx>
+#
+#    This program 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.
+#
+#    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+##
 
 package IPDB;
@@ -353,9 +363,9 @@
       INSERT INTO allocations (
               cidr,type,city, description, notes, circuitid, createstamp, modifystamp,
-              privdata, custid, vrf, vlan, rdns, parent_id, master_id
+              privdata, custid, vrf, vlan, rdns, parent_id, master_id, userid
           )
       SELECT
               ip, ? AS type, city, description, notes, circuitid, createstamp, modifystamp,
-              privdata, custid, vrf, vlan, rdns, ? AS parent_id, master_id
+              privdata, custid, vrf, vlan, rdns, ? AS parent_id, master_id, userid
           FROM poolips
           WHERE parent_id = ? AND available = 'n'
@@ -1596,4 +1606,6 @@
   $args{vlan} = '' if !$args{vlan};
   $args{rdns} = '' if !$args{rdns};
+# customer accounts may have multiple allocations;  tag the specific billing user for the allocation
+  $args{billinguserid} = 0 if !$args{billinguserid};
 
   # munge the reverse name to include the default domain if there are no dots.  Don't append if
@@ -1687,8 +1699,8 @@
       # finally assign the IP
       $dbh->do("UPDATE poolips SET custid = ?, city = ?, available='n', description = ?, notes = ?, vlan = ?, ".
-	"circuitid = ?, privdata = ?, rdns = ?, backup_id = ? ".
+	"circuitid = ?, privdata = ?, rdns = ?, backup_id = ?, userid = ? ".
 	"WHERE ip = ? AND parent_id = ?", undef,
 		($args{custid}, $args{city}, $args{desc}, $args{notes}, $args{vlan},
-		$args{circid}, $args{privdata}, $args{rdns}, $backupid,
+		$args{circid}, $args{privdata}, $args{rdns}, $backupid, $args{billinguserid},
 		$args{cidr}, $args{parent}) );
 
@@ -1748,8 +1760,8 @@
 	# Insert the allocations entry
 	$dbh->do("INSERT INTO allocations ".
-		"(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,backup_id)".
-		" VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?,?)", undef,
+		"(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,backup_id,userid)".
+		" VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?,?,?)", undef,
 		($args{cidr}, $fbparent, $fbmaster, $fbmaster, $args{vlan}, $args{custid}, $args{type}, $args{city},
-		$args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $backupid) );
+		$args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $backupid, $args{billinguserid}) );
 	my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 
@@ -1824,8 +1836,8 @@
 	# Insert the allocations entry
 	$dbh->do("INSERT INTO allocations ".
-		"(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns)".
-		" VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?)", undef,
+		"(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,userid)".
+		" VALUES (?,?,?,(SELECT vrf FROM allocations WHERE id=?),?,?,?,?,?,?,?,?,?,?)", undef,
 		($args{cidr}, $fbparent, $fbmaster, $fbmaster, $args{vlan}, $args{custid}, $args{type}, $args{city},
-		$args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
+		$args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $args{billinguserid}) );
 	my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 
@@ -2276,5 +2288,5 @@
     # direct in SQL, something like
     # "INSERT INTO foo (f1,f2,f3) VALUES (newf1,(SELECT oldf2,oldf3 FROM foo WHERE baz))"
-    my @fieldlist = qw(type city description notes circuitid privdata custid swip vrf vlan rdns parent_id master_id);
+    my @fieldlist = qw(type city description notes circuitid privdata custid swip vrf vlan rdns parent_id master_id userid);
     my $fields_sql = join(',', @fieldlist);
     my @vals;
@@ -3372,5 +3384,5 @@
   # Note city, vrf, parent_id and master_id removed due to JOIN uncertainty for block allocations
   my $commonfields = q(a.custid, a.type, a.circuitid, a.description, a.notes, a.modifystamp AS lastmod,
-	a.privdata, a.vlan, a.rdns);
+	a.privdata, a.vlan, a.rdns, a.userid);
   my $bkfields = q(b.backup_id AS hasbk, b.bkbrand, b.bkmodel, b.bktype, b.bkport, b.bksrc,
 	b.bkuser, b.bkvpass, b.bkepass, b.bkip);
