<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_hrank();
require(ROOT_PATH . 'includes/lib_finance.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 0;
$start_year=!empty($_POST['start_year']) ? intval($_POST['start_year']) : date('Y');
$start_month=!empty($_POST['start_month']) ? intval($_POST['start_month']) : date('m');
$start_day=!empty($_POST['start_day']) ? intval($_POST['start_day']) : '1';
	if($start=!empty($_REQUEST['start'])){
	   $start=$_REQUEST['start'];
	}else{
	   $start=$start_year.'-'.$start_month.'-'.$start_day;
	}
$end_year=!empty($_POST['end_year']) ? intval($_POST['end_year']) : date('Y');
$end_month=!empty($_POST['end_month']) ? intval($_POST['end_month']) : date('m');
$end_day=!empty($_POST['end_day']) ? intval($_POST['end_day']) : '31';
    if($end=!empty($_REQUEST['end'])){
	   $end=$_REQUEST['end'];
	}else{
	   $end=$end_year.'-'.$end_month.'-'.$end_day;
	}

if($act=='ready')
{
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('factory_list',get_rows('si','tbCompanyID,tbType,tbNickName','tbType=2','companys','tbNickName'));
	$smarty->assign('finance_list',get_finance_list('','',$start,$end));
	$smarty->assign('finance_total',get_finance_total('','',$start,$end));
	$smarty->display('finance.dwt');
}
elseif($act=='check')
{
	$tbSN=!empty($_POST['tbSN']) ? explode(',',$_POST['tbSN']) : '';
	$tbDayCount=!empty($_POST['tbDayCount']) ? intval($_POST['tbDayCount']) : 1;
	$tbDayCharge=!empty($_POST['tbDayCharge']) ? intval($_POST['tbDayCharge']) : 200;
	$tbCurrent=!empty($_POST['tbCurrent']) ? $_POST['tbCurrent'] : 'USD';
	
	if($tbSN!=''){
		foreach($tbSN as $v){
			$t_labor=get_labor(trim($v));
			$amount=$t_labor*$tbDayCount*$tbDayCharge;
			if(chk_finance($t_labor,$tbDayCount,$tbDayCharge,$tbCurrent,$amount,trim($v))){}
			else{
				$GLOBALS['err'] ->add($GLOBALS['_LANG']['finance_update_fail']);
				$GLOBALS['err'] ->show();
			}
		}
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('factory_list',get_rows('si','tbCompanyID,tbType,tbNickName','tbType=2','companys','tbNickName'));
	$smarty->assign('finance_list',get_finance_list('','',$start,$end));
	$smarty->display('finance.dwt');
	}

}
elseif($act=='detail' || $act=='edit')
{
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('act',$act);
	$detail=get_finance_detail($id);
	$smarty->assign('detail',$detail);
	$smarty->assign('rep_summary',get_rows('',"tbPN,tbPONumber,tbProductName,tbModelNO,tbShippingQty","`tbPONumber`='".$detail['tbPONumber']."' ","reports","tbPN")); 
	$smarty->display('finance_page.dwt');
}
elseif($act=='update')
{
	$tbLabor=!empty($_POST['tbLabor']) ? $_POST['tbLabor'] : '';
	$tbDayCount=!empty($_POST['tbDayCount']) ? $_POST['tbDayCount'] : '';
	$tbDayCharge=!empty($_POST['tbDayCharge']) ? $_POST['tbDayCharge'] : '';
	$oFinanceFrom=!empty($_POST['oFinanceFrom']) ? $_POST['oFinanceFrom'] : '';
	$tboLabor=!empty($_POST['tboLabor']) ? $_POST['tboLabor'] : '';
	$tbOverTime=!empty($_POST['tbOverTime']) ? $_POST['tbOverTime'] : '';
	$tbOverTimePrice=!empty($_POST['tbOPrice']) ? $_POST['tbOPrice'] : '';
	$tboAmount=!empty($_POST['tboAmount']) ? $_POST['tboAmount'] : '';
	$tbMemo=!empty($_POST['tbMemo']) ? $_POST['tbMemo'] : '';
	if($oFinanceFrom=='' && $tbMemo=='')
	{
	$sql_p="UPDATE " .$GLOBALS['prefix']->table('projects'). " SET". " tbLabor='$tbLabor',tbDayCount='$tbDayCount',tbDayCharge='$tbDayCharge' WHERE tbSN='$id'";
	}else{
	$sql_p="UPDATE " .$GLOBALS['prefix']->table('projects'). " SET". " tbLabor='$tbLabor',tbDayCount='$tbDayCount',tbDayCharge='$tbDayCharge',tbOverTimeLabor='$tboLabor',tbOverTime='$tbOverTime',tbOverTimePrice='$tbOverTimePrice',tbOverTotal='$tboAmount',tbOverFinanceFrom='$oFinanceFrom',tbMemo='$tbMemo' WHERE tbSN='$id'";
	}
	if($GLOBALS['db']->query($sql_p))
	{
		$smarty->assign('active',$active);
		$smarty->assign('pre',$pre);
		$smarty->assign('act','detail');
		$detail=get_finance_detail($id);
		$smarty->assign('detail',$detail);
		$smarty->assign('rep_summary',get_rows('',"tbPN,tbPONumber,tbProductName,tbModelNO,tbShippingQty","`tbPONumber`='".$detail['tbPONumber']."' ","reports","tbPN")); 
		$smarty->display('finance_page.dwt');
	}
	else
	{
		$GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
		$GLOBALS['err'] ->show();
}
}
?>