<?php

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


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

function get_finance_detail($id)
{
	 if($_SESSION['tbRank']>='80' && $id!=''){
     	$sql = "SELECT a.*,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 tbSN='". $id ."' LIMIT 1";
		 if($sql!='')
		 {
		 return $GLOBALS['db']->getRow($sql);
		 }
		 else
		 {
		 return false;
		 }
	 }
}

//$begin,$num（开始和结束笔数）$star,$end（开始和结束日期）
//$period=!empty($star) && !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')";

function get_finance_list($begin,$num,$star,$end)
{
    $sql='';
	$limit=!empty($begin) && !empty($num) ? "limit $star,$num" : '';
	$period=!empty($star) && !empty($end) ? "a.tbInspectionDate >= '" . $star . "' AND a.tbInspectionDate <= '" . $end . "'" : "a.tbInspectionDate >= '2016-11-01'";
	if($_SESSION['tbRank']>='80')
	{
	   $factory=$client='';//预留条件暂时并无作用
	   $sql = "SELECT a.tbSN,a.tbBookingDate,a.tbPONumber,a.tbCheckone,a.tbClientID,a.tbFactoryID,a.tbInspectionDate,a.tbFactoryBookNO,a.tbPM,a.tbStatus,a.tbTeam,a.tbLabor,a.tbDayCount,a.tbDayCharge,a.tbTotalPrice,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 . " ORDER BY a.tbInspectionDate asc " . $limit;
		 if ($sql!='')
		 {
		 return $GLOBALS['db']->getAll($sql);
		 }
		 else
		 {
		 return false;
		 }
	}

}

function get_finance_total($begin,$num,$star,$end)
{
    $sql='';
	$limit=!empty($begin) && !empty($num) ? "limit $star,$num" : '';
	$period=!empty($star) && !empty($end) ? "a.tbInspectionDate >= '" . $star . "' AND a.tbInspectionDate <= '" . $end . "'" : "a.tbInspectionDate >= '2016-11-01'";
	if($_SESSION['tbRank']>='80')
	{
	   $factory=$client='';//预留条件暂时并无作用
	   $sql = "SELECT a.tbCurrent,sum(tbTotalPrice) as money FROM " . $GLOBALS['prefix']->table('projects') . "AS a WHERE " . $period . $factory . $client . " group by a.tbCurrent";
		 if ($sql!='')
		 {
		 return $GLOBALS['db']->getAll($sql);
		 }
		 else
		 {
		 return false;
		 }
	}

}

function chk_finance($labor,$DayCount,$DayCharge,$Current,$total,$id)
{
    $sql='';
	if($_SESSION['tbRank']>='80' && $id!=''){
	$sql = "UPDATE " . $GLOBALS['prefix']->table('projects'). " SET tbLabor='$labor',tbDayCount='$DayCount',tbDayCharge='$DayCharge',tbCurrent='$Current',tbTotalPrice='$total',tbStatus='Finished' WHERE tbSN='" . $id . "' AND tbStatus<>'Cancelled'";
	}
	 if($GLOBALS['db']->query($sql))
     {
	 return true;
     }
     else
     {
     return false;
     }
}

function get_labor($id)
{
    $str='';
	$inspector='';
	$labor='';
	if($_SESSION['tbRank']>='80' && $id!='')
	 {
       $str = "SELECT tbPM,tbTeam FROM " . $GLOBALS['prefix']->table('projects') . " WHERE tbSN='$id'";
		 if ($str!='')
		 {
		 	 $inspector = $GLOBALS['db']->getRow($str);
			 if(!empty($inspector['tbPM']))
			 {
			 	if(!empty($inspector['tbTeam']))
				{
					$labor=substr_count($inspector['tbTeam'],',')+2;
				}
				else
				{
					$labor=1;
				}
			 }
			 else
			 {
			 	echo "No Inspection Leader!";
			 }
		 }
		 else
		 {
		 	 return false;
		 }
	 }
	 else
	 {
	   return false;
	 }
	return $labor;	 
}

?>