<?php

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

if (!defined('YUKEY'))
{
    die('Hacking attempt');
}

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

//$start,$num（开始和结束笔数） $begin,$end（开始和结束日期）
function get_projects($key,$words,$start,$num,$begin,$end)
{
    $sql='';
	$limit=isset($start) && isset($num) ? "limit $start,$num" : '';
	$period=!isset($begin) && !isset($end) ? "a.tbInspectionDate >= '" . $begin . "' AND a.tbInspectionDate <= '" . $end . "'" : "a.tbInspectionDate >= '2016-11-01'";
	$seek=!empty($key) && !empty($words) ? " AND a." . $key . "='". $words."'" : '';

	//用以下语句时造成第一页把所有条目都列出，故障在empty把0当作空处理
	//$period=!empty($start) && !empty($end) ? "unix_timestamp(a.`tbInspectionDate` ) >= unix_timestamp(" . $begin . ") AND unix_timestamp(a.`tbInspectionDate` ) <= unix_timestamp(" . $end . ")" : "unix_timestamp(a.`tbInspectionDate` ) >= unix_timestamp('2016-10-01 00:00:00')";
	if($_SESSION['tbRank']>='50')
	{
	   $factory=$client='';
	}
	elseif($_SESSION['tbRank']<='40' && $_SESSION['tbRank']>'20')
	{
	   $factory="AND a.tbFactoryID='" . $_SESSION['tbCompanyID'] . "'";
	   $client='';
	}
	elseif($_SESSION['tbRank']<='20'){
	   $factory='';
	   $client="AND a.tbClientID='" . $_SESSION['tbCompanyID'] . "'";
	}
	   $sql = "SELECT a.tbSN,a.tbBookingDate,a.tbPONumber,a.tbCheckone,a.tbClientID,a.tbFactoryID,a.tbInspectionDate,a.tbFactoryBookNO,a.tbPM,a.tbStatus,b.tbCompanyName,b.tbNickName,c.tbNickName AS client FROM" . $GLOBALS['prefix']->table('projects'). "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 WHERE ". $period . $factory . $client . $seek. " ORDER BY a.tbInspectionDate desc " . $limit;
	 if ($sql!=''){
     return $GLOBALS['db']->getAll($sql);
     }else{
     return false;
     }
}

function get_summary($id)
{
    $sql='';
	if($_SESSION['tbRank']!=''){
	   $sql = "SELECT a.tbSN,a.tbBookingDate,a.tbPONumber,a.tbCheckone,a.tbClientID,a.tbFactoryID,a.tbInspectionDate,a.tbFinishDate,a.tbShippingDate,a.tbInspectionTime,a.tbFactoryBookNO,a.tbPM,a.tbStatus,a.tbTeam,a.tbFrom,a.tbATeam,a.tbOverWork,a.tbTaxi,b.tbCompanyName,b.tbNickName,b.tbContactPerson,b.tbTel,b.tbFax,b.tbAddress,c.tbCompanyName AS client FROM" . $GLOBALS['prefix']->table('projects'). "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 WHERE a.tbSN='" . $id . "'";
	}
	 
	 if ($sql!=''){
     return $GLOBALS['db']->getRow($sql);
     }else{
     return false;
     }
}

?>