????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................????????????????????
??????????????????
ÿØÿà
 JFIF      ÿÛ C      


!"$"$ÿÛ C    
ÿÂ p 
" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................<?php
   include('session.php');
?>

<!DOCTYPE html>
<html>
  
<head>
    <title>Insert Page page</title>
</head>
  
<body>
    <center>
       

<?php
  
        $conn = mysqli_connect("localhost", "root", "", "anilpzp6_account");
  
  // Check connection
  if ($conn === false) {
      die("ERROR: Could not connect. " . mysqli_connect_error());
  }
  
  // Retrieve and sanitize inputs
  $gstno = mysqli_real_escape_string($conn, $_POST['gstno']);
  $userid = mysqli_real_escape_string($conn, $_POST['userid']);
  $password = mysqli_real_escape_string($conn, $_POST['password']);
  $office = mysqli_real_escape_string($conn, $_POST['office']);
  $address = mysqli_real_escape_string($conn, $_POST['address']);
  $moneyserial = mysqli_real_escape_string($conn, $_POST['moneyserial']);
  $consignmentserial = mysqli_real_escape_string($conn, $_POST['consignmentserial']);
  $can_invoice = isset($_POST['can_invoice']) ? 1 : 0;
  $can_moneyreceipt = isset($_POST['can_moneyreceipt']) ? 1 : 0;
  $can_consignment = isset($_POST['can_consignment']) ? 1 : 0;
  
  // Generate invoice serial number
  // Check if the city already exists in the CitySerials table
  $sql_check_city = "SELECT * FROM cityserials WHERE city = '$office'";
  $result = mysqli_query($conn, $sql_check_city);
  
  if (mysqli_num_rows($result) > 0) {
      // City exists, fetch the current serial
      $row = mysqli_fetch_assoc($result);
      $current_serial = $row['invoice_serial'];  // Don't increment, just use current value
  
      // Generate the invoice serial number (e.g., JSR001)
      $office_prefix = strtoupper(substr($office, 0, 3));
      $invoice_serial_number = $office_prefix . str_pad($current_serial, 3, "0", STR_PAD_LEFT);
  } else {
      // City does not exist, initialize serial
      $current_serial = 0;
  
      // Insert the new city into the CitySerials table
      $sql_insert_city = "INSERT INTO cityserials (city, invoice_serial) 
                          VALUES ('$office', $current_serial)";
      mysqli_query($conn, $sql_insert_city);
      
      // Generate initial invoice serial number
      $office_prefix = strtoupper(substr($office, 0, 3));
      $invoice_serial_number = $office_prefix . str_pad($current_serial, 3, "0", STR_PAD_LEFT);
  }
  
  // Insert data into the users table
  $sql = "INSERT INTO users (gstin, userid, pass, office, address, invoiceserial, moneyserial, consignmentserial, can_invoice, can_moneyreceipt, can_consignment)  
          VALUES ('$gstno', '$userid', '$password', '$office', '$address', '$invoice_serial_number','$moneyserial','$consignmentserial', $can_invoice, $can_moneyreceipt, $can_consignment)";
  
  if (mysqli_query($conn, $sql)) {
      echo '<script>alert("Data Uploaded Successfully..."); window.location.href="add-user.php"</script>';
  } else {
      echo "ERROR: Hush! Sorry $sql. " . mysqli_error($conn);
  }
  
  // Close connection
  mysqli_close($conn);
  
  ?>
  
    </center>
</body>
  
</html>