<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends oabase {
    public function action_run(){
               $model = parent::model('finance', 'oa');
                $keyword=$_REQUEST['keyword'];
                $searchsql = " AND is_del='0'";  
                if ($keyword!='') {
                    $searchsql .= " AND doc_no LIKE '%".$keyword."%'";
                }
                list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
                foreach($data as $k=>$v){
                    $data[$k]['income']= floatval($v['income']);
                    $data[$k]['payment']= floatval($v['payment']);
                }
                $url = XRequest::getPhpSelf();
                $url .= '?c=Finance';
                $var_array = array(
                    'page'=>$this->page,
                    'nextpage'=>$this->page+1,
                    'prepage'=>$this->page-1,
                    'pagecount'=>ceil($total/$this->pagesize),
                    'pagesize'=>$this->pagesize,
                    'total'=>$total,
                    'showpage'=>XPage::index($total, $this->pagesize, $this->page, $url, 10),
                    'list'=>$data,
                );
               unset($model);
               TPL::assign($var_array);
           TPL::display($this->cptpl.'finance/index.tpl');
    }
        public function action_account_list() {
                $model = parent::model('finance', 'oa');
                $searchsql = " AND is_del='0'";  
                list($total, $data) = $model->get_account_list(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
                $url = XRequest::getPhpSelf();
                $url .= '?c=account_list';
                $var_array = array(
                    'page'=>$this->page,
                    'nextpage'=>$this->page+1,
                    'prepage'=>$this->page-1,
                    'pagecount'=>ceil($total/$this->pagesize),
                    'pagesize'=>$this->pagesize,
                    'total'=>$total,
                    'showpage'=>XPage::index($total, $this->pagesize, $this->page, $url, 10),
                    'list'=>$data,
                );
               unset($model);
               TPL::assign($var_array);
                TPL::display($this->cptpl.'finance/account_list.tpl');
	}
        
        public function action_add_account(){
                $id=$_REQUEST['id'];
               $opmode="add";
               if($id){
                   $opmode="edit";
                   $model = parent::model('finance', 'oa');
                   $vo=$model->get_account_data($id);

               }
                $var_array = array(
                       'opmode'=>$opmode,
                       'vo'=>$vo
                   );            
               TPL::assign($var_array);          
               TPL::display($this->cptpl.'finance/add_account.tpl');           
        }
        
        public function action_save_account(){
                     $arr=$_POST;
                    $id=$arr['id'];
                    $model = parent::model('finance', 'oa');
                    $arr=$this->SafeFilter($arr);
                    if($arr['opmode']=='add'){
                        $result = $model->do_account_Add($arr);
                    }else{
                        $result = $model->do_account_Edit($id, $arr);
                    }
                    if (true === $result) {
                        $data['status']=1;
                        $data['info']="编辑成功";
                    }
                    else {
                        $data['info']="编辑失败";
                    }
                    echo json_encode($data);
        }
                public function action_del_account(){
                $id=$_GET['account_id'];
                 if(empty($id)){
                       XHandle::halt('操作失败', '', 1);
                die;
                }
                $model = parent::model('finance', 'oa');
                $res=$model->del_account($id);
                if($res){
                     XHandle::halt('删除成功', $this->cpfile.'?c=Finance&a=account_list', 0);
                } else {
                     XHandle::halt('删除失败', '', 1);
                }
	}
        
        public function action_add_income(){
            $model = parent::model('finance', 'oa');
            $account_list=$model->getaccount_list();
            $customer_list=$model->getcustomer_list();
            $supplier_list=$model->getsupplier_list();
            $var_array = array(
                'account_list'=>$account_list,
                'customer_list'=>$customer_list,
                'supplier_list'=>$supplier_list,
            );            
            TPL::assign($var_array);  
            TPL::display($this->cptpl.'finance/add_income.tpl'); 
        }
        public function action_add_payment(){
            $model = parent::model('finance', 'oa');
            $account_list=$model->getaccount_list();
            $customer_list=$model->getcustomer_list();
            $supplier_list=$model->getsupplier_list();
            $var_array = array(
                'account_list'=>$account_list,
                'customer_list'=>$customer_list,
                'supplier_list'=>$supplier_list,
            );            
            TPL::assign($var_array);  
            TPL::display($this->cptpl.'finance/add_payment.tpl'); 
        }
        public function action_add_transfer(){
           $model = parent::model('finance', 'oa');
            $account_list=$model->getaccount_list();
            $customer_list=$model->getcustomer_list();
            $supplier_list=$model->getsupplier_list();
            $var_array = array(
                'account_list'=>$account_list,
                'customer_list'=>$customer_list,
                'supplier_list'=>$supplier_list,
            );            
            TPL::assign($var_array);  
            TPL::display($this->cptpl.'finance/add_transfer.tpl'); 
        }
        public function action_read_account() {
                $account_id=$_REQUEST['account_id'];
                $node = parent::model('finance', 'oa');
		$vo = $node -> get_account_data($account_id);
		
			if (!empty($vo)) {// 读取成功
				$return['data'] = $vo;
				$return['status'] = 1;
				$return['info'] = "读取成功";
				echo json_encode($return);
			} else {
				$return['status'] = 0;
				$return['info'] = "读取错误";
				echo json_encode($return);
			}
	}
        
        public function action_save(){
               $arr=$_POST;
               $balance=$arr['balance'];
               $arr['doc_no']=date('Y')."-".str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
               $arr['user_id']=$_SESSION['USER_AUTH_KEY']; 
               $arr['user_name']=$_SESSION['user_name']; 
               $arr['create_time']= time();
               if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
                        $arr['add_file']=substr($str,0,strlen($str)-1); 
                        unset($arr['upload_file']);
              }
              unset($arr['balance']);
              $arr=$this->SafeFilter($arr);
              $model = parent::model('finance', 'oa');
              $result=$model->doAdd($arr);
              if($result){
                      if($arr['doc_type']==1){
                          $data['balance']=$balance+$arr['income'];
                          $model->do_account_Edit($arr['account_id'],$data);
                      }elseif($arr['doc_type']==2) {
                          $data['balance']=$balance-$arr['payment'];
                          $model->do_account_Edit($arr['account_id'],$data);
                      }elseif($arr['doc_type']==3) {

                      }
                        $data['status']=1;
                        $data['info']="编辑成功";
                         echo json_encode($data);die;
              } else {
                   $data['info']="编辑失败";
                   echo json_encode($data);die;
              }
              
        }
        
        public function action_save_zhuan(){
            $arr=$_POST;
            $arr=$this->SafeFilter($arr);
            $payment=array();
            $income=array();
            if(!empty($arr['upload_file'])){
                        $str='';
                        foreach($arr['upload_file'] as $v){
                            $str .=$v.";";
                        }
                        $payment['add_file']=substr($str,0,strlen($str)-1); 
                        $income['add_file']=substr($str,0,strlen($str)-1); 
            }    
            $payment['doc_no']=date('Y')."-".str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
            $payment['remark']="由[".$arr['account_name_payment']."]转出[".$arr['payment']."]";
            $payment['input_date']=$arr['input_date'];
            $payment['account_id']=$arr['account_id_payment'];
            $payment['account_name']=$arr['account_name_payment'];
            $payment['payment']=$arr['payment'];
            $payment['type']=$arr['type'];
            $payment['actor_name']=$arr['actor_name'];
            $payment['user_id']=$_SESSION['USER_AUTH_KEY']; 
            $payment['user_name']=$_SESSION['user_name']; 
            $payment['create_time']= time();
            $payment['doc_type']=$arr['doc_type'];
            $payment['related_account_id']=$arr['account_id_income'];
            $payment['related_account_name']=$arr['account_name_income'];
            $model = parent::model('finance', 'oa');
            $result=$model->doAdd($payment);
            if($result){
                $data['balance']=$arr['account_price_payment']-$arr['payment'];
                $model->do_account_Edit($arr['account_id_payment'],$data);
            }
            $income['doc_no']=date('Y')."-".str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
            $income['remark']="由[".$arr['account_name_payment']."]转入[".$arr['payment']."]";
            $income['input_date']=$arr['input_date'];
            $income['account_id']=$arr['account_id_income'];
            $income['account_name']=$arr['account_name_income'];
            $income['income']=$arr['payment'];
            $income['type']=$arr['type'];
            $income['actor_name']=$arr['actor_name'];
            $income['user_id']=$_SESSION['USER_AUTH_KEY']; 
            $income['user_name']=$_SESSION['user_name']; 
            $income['create_time']= time();
            $income['doc_type']=$arr['doc_type'];
            $income['related_account_id']=$arr['account_id_payment'];
            $income['related_account_name']=$arr['account_name_payment'];
            $result1=$model->doAdd($income);
            if($result1){
                $data1['balance']=$arr['account_price_income']+$arr['payment'];
                $model->do_account_Edit($arr['account_id_income'],$data1);
            }          
            
            if($result && $result1){
                      $data['status']=1;
                      $data['info']="编辑成功";
                      echo json_encode($data);die;
              } else {
                   $data['info']="编辑失败";
                   echo json_encode($data);die;
              }
            
        }
        
           public function action_read() {
                $account_id=$_REQUEST['id'];
                $node = parent::model('finance', 'oa');
		$vo = $node -> get_data($account_id);
		$file = explode(";", $vo['add_file']);
                $files=array();
                foreach($file as $k=>$v){
                   if($v!=''){
                       $files[$k]['name']= trim(strrchr($v, '/'),'/');
                       $files[$k]['path']=$v;
                    }  
                }
              $var_array = array(
                'vo'=>$vo,
                'files'=>$files,
            );            
            TPL::assign($var_array);  
            TPL::display($this->cptpl.'finance/read.tpl');   

	}
}