<?php


//require_once __DIR__."/dbconfig.php";
//require_once "includes/dbconfig.php";
//error_reporting(0);

require_once __DIR__."/../includes/dbconfig.php";
$seconds =0;
set_time_limit($seconds);
function callDbCon()
{

//mysqli_connect(get_dbserver(), get_dbuser(), get_dbpassword()) or die (mysqli_error());
//mysqli_select_db(get_dbname()) or die(mysqli_error());
$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}



}


function GetDataUsingSQL($strSQL,$iduse)
{

    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
  /*  $strSQL = "SELECT * FROM (SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4, t5.name as lev5, t6.name as lev6
              FROM test AS t1
              LEFT JOIN test AS t2 ON t2.parent_id = t1.id
              LEFT JOIN test AS t3 ON t3.parent_id = t2.id
              LEFT JOIN test AS t4 ON t4.parent_id = t3.id
              LEFT JOIN test AS t5 ON t5.parent_id = t4.id
              LEFT JOIN test AS t6 ON t6.parent_id = t5.id
              LEFT JOIN test AS t7 ON t7.parent_id = t6.id
              WHERE t1.ID = '1') as lvlTable WHERE $cond ='".$agent_id."'"; */
    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    $val  = array();
    while($rows = mysqli_fetch_array($sqlQuery,MYSQLI_ASSOC)) {
      //$val = array($rows['id'] => $rows['parent_id']);
      $val[] = $rows;
      //$total = array_sum($rows[$iduse]);
      $list[] = array($rows[$iduse]);
      //return $list;
      //return $total;
      //$val[] = array_column($rows, 'name', 'parent_id');
       //var_dump($val);
    }

    $final = 0;
    if(!empty($list)){

    $ctr = count($list);
    //echo $ctr;
    for ($c=0;$c<$ctr;$c++)
    {
      //echo $list[$c][0];
      $final = $final + $list[$c][0];

    }
    return $final;
    }
    else {
      return 0;
    }
    mysqli_close($con);
    //return $row[$cond];
    //echo $strSQL;
    //return print_r($val);
}


function GetAgentLevelFromParent($agent_id,$cond)
{

    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    $strSQL = "SELECT * FROM (SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4, t5.name as lev5, t6.name as lev6, t7.name as lev7
              FROM test AS t1
              LEFT JOIN test AS t2 ON t2.parent_id = t1.id
              LEFT JOIN test AS t3 ON t3.parent_id = t2.id
              LEFT JOIN test AS t4 ON t4.parent_id = t3.id
              LEFT JOIN test AS t5 ON t5.parent_id = t4.id
              LEFT JOIN test AS t6 ON t6.parent_id = t5.id
              LEFT JOIN test AS t7 ON t7.parent_id = t6.id
              WHERE t1.ID = '1') as lvlTable WHERE $cond ='".$agent_id."'";
    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    $val  = array();
    while($rows = mysqli_fetch_array($sqlQuery,MYSQLI_ASSOC)) {
      //$val = array($rows['id'] => $rows['parent_id']);
      $val[] = $rows;
      return $rows[$cond];
      //$val[] = array_column($rows, 'name', 'parent_id');
       //var_dump($val);
    }
    mysqli_close($con);
    //return $row[$cond];
    //echo $strSQL;
    //return print_r($val);
}


function checkifpaymentexist($extraCondition)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    $strSQL = "SELECT * FROM payments where salesID ='".$extraCondition."'";
    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    mysqli_close($con);
    return $numRows;
}



function checkifpaymentexistpaid($extraCondition)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    $strSQL = "SELECT * FROM payments where paymentstatus='Paid' AND salesID ='".$extraCondition."'";
    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    mysqli_close($con);
    return $numRows;
}


function getTblData($loop,$tbl,$extraCondition,$extraCondition2)
{	$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    $strSQL = "SELECT * FROM ".$tbl." where ".$extraCondition."".$extraCondition2."";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));


    for($ctr=0;$ctr<$loop;$ctr++){
        $row=mysqli_fetch_array($rs,MYSQLI_ASSOC);
	}

    return $row;
    //to use this function to retrieve specific record, you may use the $extraCondition variable and fix the $loop to 1.
    //to use this function to retrieve all existing records record, leave the $extraCondition blank and use function getTblNumRows() to get the total number of loops needed.
    //this function returns an array..dont forget to include an index on usage.
	 mysqli_close($con);
}



function gettaskcount($user,$type)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
	if($type == "Sales Associate")
	{
		 $strSQL = "SELECT * FROM tbl_tasklist WHERE status<>'Completed' AND (user='Unassigned' OR user=".$user.") ";
	}
	else
	{
		 $strSQL = "SELECT * FROM tbl_tasklist WHERE status<>'Completed' AND user=".$user."";
	}

    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    mysqli_close($con);
    return $numRows;
}

function getSalesOrderForJO()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    $strSQL = "SELECT id,POdate,POnumber,custT.customer_company_name,prodT.product_name,status
                FROM tbl_salesorder as mainT inner JOIN tbl_product prodT
                ON mainT.productID = prodT.product_id
                inner join tbl_customer as custT
                ON custT.customer_id = prodT.customer_id
                GROUP BY mainT.productID";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}

function MaterialListDetails($id)
{
    if($id!="")
    {
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());


    $strSQL = "SELECT * FROM products WHERE status='1' AND id=".$id;

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    $row=mysqli_fetch_array($rs,MYSQLI_ASSOC);

    return $row;
    }
}

function getDropdown($tbl,$cond,$param,$select){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $result = "";
                $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ") or die(mysqli_error($con));

                mysqli_close($con);

                while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {

                $result .=  "<option ";
                $result .= " value='".$row[$param]."'>".$row[$select]."</option>";
                }

                return $result;
}


function getTblNumRows($tbl,$extraCondition)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    $strSQL = "SELECT * FROM ".$tbl." where ".$extraCondition;
    $sqlQuery = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    $numRows = mysqli_num_rows($sqlQuery);
    mysqli_close($con);
    return $numRows;
}



function getDropdownExist($tbl,$cond,$param,$select){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $result = "";
                $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ");

                mysqli_close($con);

                while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {

				$numRows = getTblNumRows('tbl_salesorder','quotationID = '.$row['quotation_id'].'');
					if($numRows != 1)
					{
					$result .=  "<option ";
					$result .= " value='".$row[$param]."'>".$row[$select]."</option>";
					}
                }

                return $result;
}



function getDropdownExistJO($tbl,$cond,$param,$select){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $result = "";
                $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ");

                mysqli_close($con);

                while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {
                  //  $numRows = getTblNumRows('tbl_joborder','salesorder =  '.$row['salesorderID'].'');
                  $soID = $row['id'];
				        $numRows = getTblNumRows('tbl_joborder','salesorder = "'.$soID.'"');
					if($numRows != 1)
					{
					$result .=  "<option ";
					$result .= " value='".$row[$param]."'>".$row[$select]."</option>";
					}
                }

                return $result;
}




function getDropdownselect($tbl,$cond,$param,$select){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $result = "";
                $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ");
                mysqli_close($con);

                while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {
                $result .=  "<option ";
                $result .= " value='".$row[$param]."'>".$row[$select]."</option>";
                }

                return $result;
}

function getDropdownuserassign($tbl,$cond,$param,$select,$memberid){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $result = "";
                $query = mysqli_query($con,"select * FROM ".$tbl." WHERE ".$cond. " ");
                mysqli_close($con);

                while($row = mysqli_fetch_array($query ,MYSQLI_ASSOC)) {

					if($row['id'] != $memberid)
					{
						$result .=  "<option ";
						$result .= " value='".$row[$param]."'>".$row[$select]."</option>";
					}
                }

                return $result;
}


function logindetails(){
	if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
	{
	   $itemarray = array();
	   $memberid = "";
	  $type = "";
	  $fname = "";
	}
	else
	{
	  $memberid = $_SESSION['MEMBER_ID'];
	  $type = $_SESSION['TYPE'];
	  $fname = $_SESSION['FIRST_NAME'];
	}
	$result = "";
    $result .=  '<li class="nav-item dropdown no-arrow">';
	$result .=  '<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"';
    $result .=  '       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
    $result .=  '       <span class="mr-2 d-none d-lg-inline text-gray-600 small">'. $fname .'</span>';
    $result .=  '       <img class="img-profile rounded-circle" src="pic/undraw_profile.svg">';
    $result .=  '    </a>';
    $result .=  '     <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="userDropdown">';
    $result .=  '       <a class="dropdown-item" href="#">';
    $result .=  '            <i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i>';
    $result .=  '             Profile';
    $result .=  '        </a>';
    $result .=  '         <a class="dropdown-item" href="#">';
    $result .=  '            <i class="fas fa-cogs fa-sm fa-fw mr-2 text-gray-400"></i>';
    $result .=  '            Settings';
                          $result .=  '        </a>';
                          $result .=  '        <div class="dropdown-divider"></div>';
                          $result .=  '        <a class="dropdown-item" href="#" data-toggle="modal" data-target="#logoutModal">';
                          $result .=  '            <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>';
                          $result .=  '            Logout';
                           $result .=  '       </a>';
                          $result .=  '    </div>';
                       $result .=  '   </li>';




  $result .=  ' <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"  aria-hidden="true">';
  $result .=  '     <div class="modal-dialog" role="document">';
  $result .=  '          <div class="modal-content">';
  $result .=  '             <div class="modal-header">';
  $result .=  '                <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>';
  $result .=  '                <button class="close" type="button" data-dismiss="modal" aria-label="Close">';
  $result .=  '                    <span aria-hidden="true">×</span>';
  $result .=  '                </button>';
  $result .=  '            </div>';
  $result .=  '            <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>';
  $result .=  '            <div class="modal-footer">';
  $result .=  '                <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>';
  $result .=  '               <a class="btn btn-primary" href="../network/destroy.php">Logout</a>';
  $result .=  '            </div>';
  $result .=  '        </div>';
  $result .=  '    </div>';
  $result .=  ' </div>';



                return $result;
}


function sidebar(){
  if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["DEPT"]) && empty($_SESSION["FIRST_NAME"]) && empty($_SESSION["ACCESS_TYPE"]))
  {
    $itemarray = array();
    $memberid = "";
    $type = "";
    $firstname = "";
    $dept = "";
    $accesstype = "";
  }
  else
  {
    $memberid = $_SESSION['MEMBER_ID'];
    $type = $_SESSION['TYPE'];
    $firstname = $_SESSION['FIRST_NAME'];
    $dept = $_SESSION['DEPT'];
    $accesstype2 = $_SESSION['ACCESS_TYPE'];

  }

	$result = "";

	$result .=  '            <!-- Divider -->';
    $result .=  '        <hr class="sidebar-divider">';
    $result .=  '        <!-- Heading -->';
	$result .=  '		<div class="sidebar-heading">';
    $result .=  '            Sales';
    $result .=  '        </div>';
	$result .=  '<li class="nav-item">';
    $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo">';
    $result .=  '            <i class="fas fa-fw fa-cog"></i>';
    $result .=  '            <span>Agent</span>';
    $result .=  '            </a>';
    $result .=  '            <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">';
    $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
    $result .=  '            <h6 class="collapse-header">Components:</h6>';
    $result .=  '            <a class="collapse-item" href="custacc.php">Create Agent Account </a>';
    $result .=  '           <a class="collapse-item" href="customerdata.php">Agent Data</a>';
    $result .=  '            </div>';
    $result .=  '            </div>';
	$result .=  '			</li>';
  $result .=  '<li class="nav-item">';
    $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseThree" aria-expanded="true" aria-controls="collapseTwo">';
    $result .=  '            <i class="fas fa-fw fa-cog"></i>';
    $result .=  '            <span>Sales</span>';
    $result .=  '            </a>';
    $result .=  '            <div id="collapseThree" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">';
    $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
    $result .=  '            <h6 class="collapse-header">Components:</h6>';
    $result .=  '            <a class="collapse-item" href="sales.php">Create Sales Order </a>';
    $result .=  '            <a class="collapse-item" href="paymentlist.php">Pending Payment List</a>';
    $result .=  '            </div>';
    $result .=  '            </div>';
	$result .=  '			</li>';
  $result .=  '<li class="nav-item">';
    $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseFour" aria-expanded="true" aria-controls="collapseTwo">';
    $result .=  '            <i class="fas fa-fw fa-cog"></i>';
    $result .=  '            <span>Reports</span>';
    $result .=  '            </a>';
    $result .=  '            <div id="collapseFour" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">';
    $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
    $result .=  '            <h6 class="collapse-header">Components:</h6>';
    $result .=  '            <a class="collapse-item" href="reportlist.php">Payment Transaction History</a>';
    $result .=  '            <a class="collapse-item" href="reportlistsales.php">Sales Transaction History</a>';
    //$result .=  '            <a class="collapse-item" href="paymentlist.php">Pending Payment List</a>';
    $result .=  '            </div>';
    $result .=  '            </div>';
	$result .=  '			</li>';


  if($type =="Admin")
  {
$result .=  '            <!-- Divider -->';
  $result .=  '        <hr class="sidebar-divider">';
  $result .=  '        <!-- Heading -->';
$result .=  '		<div class="sidebar-heading">';
  $result .=  '            System';
  $result .=  '        </div>';


    $result .=  '<li class="nav-item">';
      $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseFive" aria-expanded="true" aria-controls="collapseFour">';
      $result .=  '            <i class="fas fa-fw fa-cog"></i>';
      $result .=  '            <span>Settings</span>';
      $result .=  '            </a>';
      $result .=  '            <div id="collapseFive" class="collapse" aria-labelledby="headingThree" data-parent="#accordionSidebar">';
      $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
      $result .=  '            <h6 class="collapse-header">Components:</h6>';
      //$result .=  '            <a class="collapse-item" href="user.php">Users</a>';
      //$result .=  '           <a class="collapse-item" href="userlist.php">Users List</a>';
      $result .=  '           <a class="collapse-item" href="system.php">System Settings</a>';
      //$result .=  '           <a class="collapse-item" href="voidform.php">Void Form</a>';
      //$result .=  '           <a class="collapse-item" href="voidlist.php">Void Transasction History</a>';
      $result .=  '            </div>';
      $result .=  '            </div>';
    $result .=  '			</li>';

    $result .=  '<li class="nav-item">';
      $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseSix" aria-expanded="true" aria-controls="collapseFour">';
      $result .=  '            <i class="fas fa-fw fa-cog"></i>';
      $result .=  '            <span>Record Maintenance</span>';
      $result .=  '            </a>';
      $result .=  '            <div id="collapseSix" class="collapse" aria-labelledby="headingThree" data-parent="#accordionSidebar">';
      $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
      $result .=  '            <h6 class="collapse-header">Components:</h6>';
      //$result .=  '            <a class="collapse-item" href="user.php">Users</a>';
      $result .=  '           <a class="collapse-item" href="userlist.php">Users List</a>';
      //$result .=  '           <a class="collapse-item" href="system.php">System Settings</a>';
      $result .=  '           <a class="collapse-item" href="voidform.php">Void Form</a>';
      $result .=  '           <a class="collapse-item" href="voidlist.php">Void Transasction History</a>';
      $result .=  '            </div>';
      $result .=  '            </div>';
    $result .=  '			</li>';
  }

/*

  $result .=  '            <!-- Divider -->';
    $result .=  '        <hr class="sidebar-divider">';
    $result .=  '        <!-- Heading -->';
  $result .=  '		<div class="sidebar-heading">';
    $result .=  '            Manufacturing';
    $result .=  '        </div>';


      $result .=  '<li class="nav-item">';
        $result .=  '            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseSix" aria-expanded="true" aria-controls="collapseFour">';
        $result .=  '            <i class="fas fa-fw fa-cog"></i>';
        $result .=  '            <span>Job Order</span>';
        $result .=  '            </a>';
        $result .=  '            <div id="collapseSix" class="collapse" aria-labelledby="headingThree" data-parent="#accordionSidebar">';
        $result .=  '            <div class="bg-white py-2 collapse-inner rounded">';
        $result .=  '            <h6 class="collapse-header">Components:</h6>';
        $result .=  '            <a class="collapse-item" href="joborderdashboard.php">Job Order</a>';
        $result .=  '            </div>';
        $result .=  '            </div>';
      $result .=  '			</li>';

*/

    return $result;
}

function getsalesinquiry()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_sales_inquiry";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}




function getquotation()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_quotation";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}



function getsalesorder()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_salesorder";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getjoborder()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_joborder";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}

function getuserlist()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM users";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getpaymentlist()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM sales where status='1' and paymentstatus ='Pending'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}




function getpaymentlistall($id)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM payments where paymento='".$id."'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);
    //echo $strSQL;
    return $rs;
}

function getsaleslistall($id)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM sales where status=1 and  soldby='".$id."'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);
    //echo $strSQL;
    return $rs;
}

function getpaymentlistreport()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    /* $strSQL = "SELECT s.id,s.productID,s.amount,sum(s.quantity) as quantity,s.soldby,s.boughtby,s.datecreated,sum(s.total_sales) as total_sales,p.paymentstatus FROM sales as s
    INNER JOIN payments as p
    ON s.id = p.salesID
    where s.status='1' AND p.paymentstatus!='Voided'
    group by s.soldby"; */

    $strSQL = "SELECT id,productID,amount,sum(quantity) as quantity,soldby,boughtby,datecreated,sum(total_sales) as total_sales FROM sales
    where status='1' group by soldby";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getvoidlist()
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM void where status='1'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getuserlistdata($id)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM agent WHERE id='".$id."'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getagentlistdata($id)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM users WHERE id='".$id."'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}



function getsalesorderdata($id)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_salesorder WHERE id='".$id."'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}

function gettasklist($user)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    //$strSQL = "SELECT * FROM tbl_tasklist WHERE status<>'Completed' AND (user='Unassigned' OR user=".$user.") ";
	$strSQL = "SELECT * FROM tbl_tasklist WHERE status<>'Completed' AND user_type='".$user."' ";
    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}



function gettasklistsalesassoc($user)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    $strSQL = "SELECT * FROM tbl_tasklist WHERE status<>'Completed'";

    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function gettasklistcompleted($user)
{
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());

    //$strSQL = "SELECT distinct a.itemid AS itemid,b.itemname AS itemname, b.uomid AS uomid, b.catid AS catid ";
    //$strSQL.= "FROM 10_bom a, 8_inventory b ";
    //$strSQL.= "WHERE a.status=1 AND b.status=1 AND a.itemid=b.id ORDER BY itemname" ;
    //$strSQL = "SELECT * FROM tbl_tasklist WHERE status='Completed' AND (user='Unassigned' OR user=".$user.") ";
	$strSQL = "SELECT * FROM tbl_tasklist WHERE status='Completed' AND user_type='".$user."' ";
    $rs = mysqli_query($con,$strSQL) or die(mysqli_error($con));
    mysqli_close($con);

    return $rs;
}


function getUser($id){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $query = mysqli_query($con,"select FIRST_NAME FROM users WHERE id=".$id." ") or die(mysqli_error($con));
                mysqli_close($con);

                $records = mysqli_fetch_array($query,MYSQLI_ASSOC);
                if($records){
                $result =  implode("|" , $records);
                }
                else
                {
                  $result = "";
                }

                return $result;
}


function getRecord($col,$tbl,$cond){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                //echo "select ".$col." FROM ".$tbl." WHERE ".$cond. "";
                //$con =  callDbCon();
                $query = mysqli_query($con,"select ".$col." FROM ".$tbl." WHERE ".$cond. "") or die(mysqli_error($con));
                mysqli_close($con);

                $records = mysqli_fetch_array($query,MYSQLI_ASSOC);
                if($records){
                $result =  implode("|" , $records);
                }
                else
                {
                  $result = "";
                }

                return $result;
}


function getRecordNoWhere($col,$tbl,$cond){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                //echo "select ".$col." FROM ".$tbl." WHERE ".$cond. "";
                //$con =  callDbCon();
                $query = mysqli_query($con,"select ".$col." FROM ".$tbl."  ".$cond. "") or die(mysqli_error($con));
                mysqli_close($con);

                $records = mysqli_fetch_array($query,MYSQLI_ASSOC);
                if($records){
                $result =  implode("|" , $records);
                }
                else
                {
                  $result = "";
                }

                return $result;
}


function getRecordbyID($col,$tbl,$cond){

                $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
                $query = mysqli_query($con,"select ".$col." FROM ".$tbl." WHERE ".$cond) or die(mysqli_error($con));
                mysqli_close($con);
                $test = "select ".$col." FROM ".$tbl." WHERE ".$cond;
                $records = mysqli_fetch_array($query,MYSQLI_ASSOC);
                if($records){
                $result =  implode("|" , $records);
                }
                else
                {
                  $result = "";
                }

                return $result;
}



function generateCode(){

    $unique =   FALSE;
    $length =   5;
   // $chrDb  =   array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
    $chrDb  =   array('0','1','2','3','4','5','6','7','8','9');
    $con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
    while (!$unique){


        /*  for ($count = 0; $count < $length; $count++){

              $chr = $chrDb[rand(0,count($chrDb)-1)];

              if (rand(0,1) == 0){
                 $chr = strtolower($chr);
              }
           //   if (3 == $count){
           //      $str .= '-';
           //   }
              $str .= $chr; */
              $getval = getRecordNoWhere('paymentref','payments',' ORDER BY ID DESC LIMIT 1');
              $str = (int) $getval;
              $str++;


          //$str .= "E".$chr;
          /* check if unique */

          //$query = mysqli_query($con,SELECT * FROM  tbl_joborder WHERE jobordernum = '".$str."' and userstatus = '1') or die(mysqli_error($con));
        //  $query = mysqli_query($con,"SELECT * FROM  tbl_joborder WHERE jobordernum = '".$str."') or die(mysqli_error($con));
          $query = mysqli_query($con,"select * FROM payments WHERE paymentref=".$str." ") or die(mysqli_error($con));

          $existingCode = mysqli_num_rows($query);

          if (!$existingCode){
             $unique = TRUE;
          }
    }
    mysqli_close($con);
    return $str;
}


function generateCode2(){

    $unique =   FALSE;
    $length =   8;
    $chrDb  =   array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
    //$chrDb  =   array('0','1','2','3','4','5','6','7','8','9');


          $str = '';
          for ($count = 0; $count < $length; $count++){

              $chr = $chrDb[rand(0,count($chrDb)-1)];

              if (rand(0,1) == 0){
                 $chr = strtolower($chr);
              }
           //   if (3 == $count){
           //      $str .= '-';
           //   }
              $str .= $chr;
          }
          //$str .= "E".$chr;
          /* check if unique */

    return $str;
}

function generatePassword(){

    $unique =   FALSE;
    $length =   8;
    $chrDb  =   array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
    //$chrDb  =   array('0','1','2','3','4','5','6','7','8','9');
    while (!$unique){

          $str = '';
          for ($count = 0; $count < $length; $count++){

              $chr = $chrDb[rand(0,count($chrDb)-1)];

              if (rand(0,1) == 0){
                 $chr = strtolower($chr);
              }
           //   if (3 == $count){
           //      $str .= '-';
           //   }
              $str .= $chr;
          }
          //$str .= "E".$chr;
          /* check if unique */
          callDbCon();
          $strSQL = "SELECT * FROM 00_users WHERE password = '".$str."' and userstatus = '1'";
          $sqlQuery = mysql_query($strSQL);
          $existingCode = mysql_num_rows($sqlQuery);
          mysql_close();
          if (!$existingCode){
             $unique = TRUE;
          }
    }
    return $str;
}



?>
