<?php 
// const BR = '<br/>';

function invoice($type) {

$year = date('Y');
$coid = $GLOBALS['coid'];

// 1. Try to find the existing record
$query = "SELECT LPAD(v_no, 6, '0') AS vvv FROM voucher_no 
          WHERE v_type='$type' AND v_year='$year' AND coid=$coid";

$result = mysqli_query($GLOBALS['conn'], $query) or die(mysqli_error($GLOBALS['conn']));

// 2. If no rows are found, insert the first record for this year
if (mysqli_num_rows($result) == 0) {
    $insert = "INSERT INTO `voucher_no`(`coid`, `bid`, `brid`, `v_type`, `v_year`, `v_no`) 
               VALUES ($coid, {$GLOBALS['bid']}, {$GLOBALS['brid']}, '$type', '$year', 1)";
    
    mysqli_query($GLOBALS['conn'], $insert) or die(mysqli_error($GLOBALS['conn']));
    
    // Set the initial value manually for the first use
    $s_inv = "000001";
} else {
    // 3. If it exists, grab the value
    $row = mysqli_fetch_assoc($result);
    $s_inv = $row['vvv'];
}

  //  $s_inv = substr(str_shuffle('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 5);
   $invoice =$type.'-'.$GLOBALS['coid'].'-'.$GLOBALS['bid'].'-'.$GLOBALS['brid'].'-'.date("dmY").'-'.$s_inv;
   return $invoice;
}

 function money($number, $fractional=false) {
    if ($fractional) { $number = sprintf('%.2f', $number);}
    while (true) {$replacedM = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
      if ($replacedM != $number) {$number = $replacedM;} else {break;}
    } return $number;
  }
  
  function skucode($type) {
   $sku = substr(str_shuffle('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 4);
   $skugenarate =$type.'-'."COLOR".'-'."SIZE".'-'.'-'.$sku;
   return $skugenarate;
}


  function inword($number){
     $no = round($number);
     $point = round($number - $no, 2) * 100;
     $hundred = null;
     $digits_1 = strlen($no);
     $i = 0;
     $str = array();
     $words = array('0' => '', '1' => 'One', '2' => 'Two',
      '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six',
      '7' => 'Seven', '8' => 'Eight', '9' => 'Nine',
      '10' => 'Ten', '11' => 'Eleven', '12' => 'Twelve',
      '13' => 'Thirteen', '14' => 'Fourteen',
      '15' => 'Fifteen', '16' => 'Sixteen', '17' => 'Seventeen',
      '18' => 'Eighteen', '19' =>'Nineteen', '20' => 'Twenty',
      '30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty',
      '60' => 'Sixty', '70' => 'Seventy',
      '80' => 'Eighty', '90' => 'Ninety');
     $digits = array('', 'Hundred', 'Thousand', 'Lac', 'Crore');
     while ($i < $digits_1) {
       $divider = ($i == 2) ? 10 : 100;
       $number = floor($no % $divider);
       $no = floor($no / $divider);
       $i += ($divider == 10) ? 1 : 2;
       if ($number) {
          $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
          $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
          $str [] = ($number < 21) ? $words[$number] .
              " " . $digits[$counter] . $plural . " " . $hundred
              :
              $words[floor($number / 10) * 10]
              . " " . $words[$number % 10] . " "
              . $digits[$counter] . $plural . " " . $hundred;
       } else $str[] = null;
    }
    $str = array_reverse($str);
    $result = implode('', $str);
    $points = ($point) ?
      "." . $words[$point / 10] . " " . 
            $words[$point = $point % 10] : '';
    echo $result  ;
  }
?>