<?php

class CompanyController extends Zend_Controller_Action {

	public function init() {
		$information = new Zend_Session_Namespace('information');	
		if( Lib_Permission::CheckPermission('company') != true ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}
		
		$admin = new Zend_Session_Namespace('admin');
		if( $admin->admin_auth != true ){
			$this->_helper->redirector('index','login');
		}			
	}	
	
	
	public function indexAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		
		$v = $this->_request->getParams();
		
		
		if( $information->addCompanySuccess ){
			$this->view->addCompanySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		if( $information->removeSuccess ){
			$this->view->removeSuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		$limit = 10;
		$limitpage = 10;

		$page = $this->_request->getParams();
		if( isset($page['page']) ) $page = $page['page'];
		else $page = 1;
			
		$Company = new Company();	
		$dataCount = count($Company->fetchAll(Lib_Permission::setPermissionCompanySQL() , 'company_ins DESC')); 
			$p = ($page-1)*$limit;		
		$data = $Company->fetchAll(Lib_Permission::setPermissionCompanySQL() , 'company_ins DESC', $limit,$p); 		
		$this->view->dataTable = $data;		
	
		$Lib_Page = new Lib_Page();
		$Lib_Page->setLimit( $limit );
		$Lib_Page->setLimitPage( $limitpage );
		$Lib_Page->setMax( $dataCount );
		$Lib_Page->setActualId( $page );
		$Lib_Page->setController('company');
		$Lib_Page->setAction('index');
		$Lib_Page->setPrefix('page');
		$this->view->str = $Lib_Page->result();
		$this->view->page = ($page-1)*$limit;		
		
	}
	
	
	public function addAction() {	
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');
		
		$Company = new Company();		
		
		$v = $this->_request->getPost();
		
		if( isset($v['admin_addcompany']) ){
		
			$data = array(
				'company_name' => $v['company_name'],
				'company_ins' => Lib_Date::datetime(),
				'company_edit' => Lib_Date::datetime()
			);
			$idIns = $Company->insert($data);
			
			//var_dump($data);die();
			
			$information->addCompanySuccess = true; 
			$this->_helper->redirector('index','company');		
			
		}		

	}
	
	public function editAction() {	
		$v = $this->_request->getParams();
		$information = new Zend_Session_Namespace('information');
		
		if( Lib_Permission::getPermissionCompany( $v['id'] ) != 1 ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}

		$Company = new Company();	
		$Currency = new Currency();		
		
		if( $information->editCompanySuccess ){
			$this->view->editCompanySuccess = true;
			Zend_Session::namespaceUnset('information');			
		}
		
		
		if( isset($v['admin_editcompany']) ){		
			$data = array(
				'company_name' => $v['company_name'],
				'company_status' => $v['company_status'],
				'company_currency' => isset($v['company_currency'])?$v['company_currency']:0,
				'currency_id' => $v['currency_id'],
				'company_tex' => str_replace(",",".",$v['company_tex']),
				'company_def_mathod_pay' => $v['company_def_mathod_pay'],
				'company_edit' => Lib_Date::datetime()
			);
			$Company->update( $data , 'company_id='.$v['id'] );
						
			$information->editCompanySuccess = true; 
			$this->_helper->redirector('edit','company',null,array('id' => $v['id']));			
		}		
		
		
		
		$select = $Company->select();
		$select->where('company_id = ?', $v['id']);	
		$row = $Company->fetchRow($select);
		$this->view->companyData = $row;

		$dataCurrency = $Currency->fetchAll(); 		
		$this->view->dataTableCurrency = $dataCurrency;	
	}
	
	public function removeAction() {	
		$v = $this->_request->getParams();		
		$this->_helper->viewRenderer->setNoRender();
		$admin = new Zend_Session_Namespace('admin');
		$information = new Zend_Session_Namespace('information');	
		
		if( Lib_Permission::getPermissionCompany( $v['id'] ) != 1 ){
			$information->permissionError = true;
			$this->_helper->redirector('index','index');		
		}
		
		$Company = new Company();
		$where = array(
			'company_id = ?' => $v['id']
		);
		$Company->delete($where);	
		$information->removeSuccess = true; 
		$this->_helper->redirector('index','company');		
	}

}
