<?php

/*
 *lib_reports相关函数库
 * ============================================================================
 ============================================================================
 * 版权所有 2012-2020 深圳朝九科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: Yukey  QQ:2787720874
*/
if (!defined('YUKEY'))
{
    die('Hacking attempt');
}

/**
 * 获得reports下的项目列表
 *
*/

function loadresult($id)
{
	if($_SESSION['tbRank']!=''){
	   $sql = "SELECT tbPN,tbReportNumber,tbProductName,tbClientID,tbInspectionDate,tbLotQty,tbResult,tbApproval,tbAppDate,tbModify,tbIssue,tbModifyReason FROM" . $GLOBALS['prefix']->table('reports') . " WHERE tbPN='". $id ."' LIMIT 1";
	}
	 if ($sql!=''){
     return $GLOBALS['db']->getRow($sql);
     }else{
     return false;
     }
}

function rep_details($id)
{
	if($_SESSION['tbRank']!=''){
	   $sql = "SELECT a.*,b.tbCompanyName,b.tbNickName,b.tbAddress,b.tbTel,c.tbCompanyName AS client,d.tbBookingDate,d.tbCheckone FROM" . $GLOBALS['prefix']->table('reports') . "AS a LEFT JOIN" . $GLOBALS['si']->table('companys') . "AS b ON a.tbFactoryID = b.tbCompanyID LEFT JOIN " . $GLOBALS['si']->table('companys') . " AS c ON a.tbClientID = c.tbCompanyID LEFT JOIN " . $GLOBALS['prefix']->table('projects') . " AS d ON a.tbPONumber=d.tbPONumber WHERE tbPN='". $id ."'";
	}
	 if ($sql!=''){
     return $GLOBALS['db']->getRow($sql);
     }else{
     return false;
     }
} 

function report_list($k,$v,$star,$num)
{
		 $s=" ORDER BY a.tbInspectionDate desc limit $star,$num";
	if($_SESSION['tbRank']>='50'){
        ($k !='') ? $condition=" WHERE a.".$k."='$v'".$s : $condition=$s;
    }elseif($_SESSION['tbRank']<='40' && $_SESSION['tbRank']>'20'){
	    ($k !='') ? $condition=" WHERE a.tbFactoryID='" . $_SESSION['tbCompanyID'] . "' AND a.".$k."='$v'".$s : $condition=" WHERE tbFactoryID='" . $_SESSION['tbCompanyID']."'".$s;
	}elseif($_SESSION['tbRank']<='20'){
	    ($k !='') ? $condition="WHERE a.tbClientID='" . $_SESSION['tbCompanyID'] . "' AND a.".$k."='$v'".$s : $condition=" WHERE tbClientID='" . $_SESSION['tbCompanyID']."'".$s;
	}  //($k !='' && $v !='')使用该条件时在搜索页面搜不到结果时出现所有结果，所以取消&& $v !=''，但取消后会不会有新的bug出现？
	$sql="SELECT a.tbPN,a.tbReportNumber,a.tbProductName,a.tbClientID,a.tbInspectionDate,a.tbFactoryID,a.tbFactoryBookNO,a.tbLotQty,a.tbWI,a.tbSerialNO,a.tbResult,a.tbReport,a.tbReInspectionNo,b.tbCompanyName,b.tbNickName,c.tbNickName AS client FROM" . $GLOBALS['prefix']->table('reports') . " AS a LEFT JOIN " . $GLOBALS['si']->table('companys') . " AS b ON a.tbFactoryID = b.tbCompanyID LEFT JOIN" . $GLOBALS['si']->table('companys') . " AS c ON a.tbClientID = c.tbCompanyID".$condition;
	 if ($sql!=''){
     return $GLOBALS['db']->getAll($sql);
     }else{
     return false;
     }
} 

function c_serials($k,$v,$table1,$table2)
{
	($k !='' && $v !='') ? $condition = " WHERE $k='$v' " : $condition ='';
	$str="select count(*) from (select tbSN from " . $GLOBALS['prefix']->table($table1) . $condition . " union (select tbSN from " . $GLOBALS['prefix']->table($table2) .  $condition . ")) t";
    if ($GLOBALS['db']->getOne($str)){
     	return $GLOBALS['db']->getOne($str);
    }else{
     	return false;
    }
}

function get_serials($k,$v,$table1,$table2,$order,$row)
{
	 ($k !='' && $v !='') ? $condition = " WHERE $k='$v' " : $condition ="";
	 $sql = "SELECT * FROM" . $GLOBALS['prefix']->table($table1) . $condition . " UNION ALL SELECT * FROM ". $GLOBALS['prefix']->table($table2) . $condition . " ORDER BY $order ASC";
	 if ($sql!=''){
	 		if ($row=="true"){
			return $GLOBALS['db']->getRow($sql);
			}else{
     		return $GLOBALS['db']->getAll($sql);
			}
     }else{
     		return false;
     }
}
?>