<?php
class PurchaseOrdersController extends AppController {

    var $name = 'PurchaseOrders';
    var $helpers = array('Html', 'Form', 'Javascript');



    var $paginate = array(

	    'contain' => false,
	    'limit' => 100,
	    'order'=>array('PurchaseOrder.id' => 'desc')

    );

    function index() {
	$this->PurchaseOrder->recursive = 1;
	$this->set('purchaseOrders', $this->paginate());
    }

    function view($id = null) {
	if (!$id) {
	    $this->Session->setFlash(__('Invalid PurchaseOrder.', true));
	    $this->redirect(array('action'=>'index'));
	}
	$this->set('purchaseOrder', $this->PurchaseOrder->read(null, $id));
    }

    function add() {
	if (!empty($this->data)) {
	    $this->PurchaseOrder->create();

	    $count = $this->PurchaseOrder->findCount();
	    $count++;

	    $offset = 2060;

	    $count += $offset;

	    $newPOnumber = "CMCPO".$count;

	    $this->data['PurchaseOrder']['title'] = $newPOnumber;


	    if ($this->PurchaseOrder->save($this->data)) {
			$this->Session->setFlash(__('The PurchaseOrder has been saved', true));
			$this->redirect(array('action'=>'index'));
	    } else {
			$this->Session->setFlash(__('The PurchaseOrder could not be saved. Please, try again.', true));
	    }
	}
	$jobs = $this->PurchaseOrder->Job->find('list');
	$principles = $this->PurchaseOrder->Principle->find('list');
	$documents = $this->PurchaseOrder->Document->find('list');
	$this->set(compact('jobs', 'principles', 'documents'));
    }

    function edit($id = null) {
	if (!$id && empty($this->data)) {
	    $this->Session->setFlash(__('Invalid PurchaseOrder', true));
	    $this->redirect(array('action'=>'index'));
	}
	if (!empty($this->data)) {
	    if ($this->PurchaseOrder->save($this->data)) {
		$this->Session->setFlash(__('The PurchaseOrder has been saved', true));
		$this->redirect(array('action'=>'index'));
	    } else {
		$this->Session->setFlash(__('The PurchaseOrder could not be saved. Please, try again.', true));
	    }
	}
	if (empty($this->data)) {
	    $this->data = $this->PurchaseOrder->read(null, $id);
	}
	$jobs = $this->PurchaseOrder->Job->find('list');
	$principles = $this->PurchaseOrder->Principle->find('list');
	$documents = $this->PurchaseOrder->Document->find('list');
	$this->set(compact('jobs','principles','documents'));
    }


    function autocomplete() {
	$this->layout = 'ajax';

	$query = strtolower($_GET["term"]);

	//$this->set('customers_json', print_r($this->data));

	if($query == null) {
	    return;
	}

	App::import('Core', 'Sanitize');

	$query = Sanitize::clean($query);

	$POs = $this->PurchaseOrder->find('list', array(
		'conditions' => array(
			'PurchaseOrder.title LIKE' => '%'.$query.'%'
		),'fields' => array('PurchaseOrder.title'),
		'order' => array('PurchaseOrder.title ASC')



	));

	//print_r($POs);

	foreach ($POs as $id => $po) {
	    $po_list[] = array('id'=> $id, 'label' => $po);
	}

	$POs_json = json_encode($po_list);

	$this->set('POs_json', $POs_json);

    }


    function getdata($id) {
	$this->layout = 'ajax';
	$po = $this->PurchaseOrder->read(null, $id);
	$this->set('po_json', json_encode($po));
    }


    
}
?>
