<?php
class LineItemsController extends AppController {

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

    var $components = array('RequestHandler');

    function index() {
        $this->LineItem->recursive = 0;
        $this->set('lineItems', $this->paginate());
    }

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

    function add($quoteid = null) {

        if (!$quoteid && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Quote ID', true));
            $this->redirect(array('action'=>'index'));
        }

        if (!empty($this->data)) {
            $this->LineItem->create();

            if(isset($this->data['LineItem']['product_id'])) {
                $product = $this->LineItem->Product->findById($this->data['LineItem']['product_id']);

                /* Copy all the data from the Product to the new LineItem */

                $this->data['LineItem']['description'] = $product['Product']['description'];
                $this->data['LineItem']['title'] = $product['Product']['title'];
            }



            // $numberOfItems = $this->LineItem->find('count',
            //    array('conditions' => array('LineItem.quote_id' => $this->data['LineItem']['quote_id'])));

            //$numberOfItems++;

            // $this->data['LineItem']['item_number'] = $numberOfItems;



            if ($this->LineItem->save($this->data)) {
                $quoteid = $this->data['LineItem']['quote_id'];

                $this->Session->setFlash(__('Product Added to Quote Successfully', true));
                $this->redirect(array('controller'=>'quotes', 'action'=>'view', $quoteid));
            } else {
                $this->Session->setFlash(__('The LineItem could not be saved. Please, try again.', true));
            }
        }


        $principles = $this->LineItem->Product->Principle->find('list');

        $quote = $this->LineItem->Quote->read(null, $quoteid);
        $this->set(compact('principles','quote', 'products'));

    }


    /* Process the Line Item and add it to a quote */
    function ajaxSave() {


        if($this->RequestHandler->isAjax()) {

            if (!empty($this->data)) {
                $this->LineItem->create();

                if(isset($this->data['LineItem']['product_id'])) {
                    $product = $this->LineItem->Product->findById($this->data['LineItem']['product_id']);
                    
                    /* Copy all the data from the Product to the new LineItem */
                    
                    $this->data['LineItem']['description'] = $product['Product']['description'];



                    $this->data['LineItem']['title'] = $product['Product']['title'];
                }

                


                if($this->LineItem->save($this->data)) {
                    echo 'success';
                }
                else {
                    echo 'failure';
                }
                Configure::write('debug', 0);
                $this->autoRender = false;
                exit();


                /* Need to find a way to append Product Option data to the description / model number title */


            }


        }




    }


    /* Display a list of Products for a given principle. Used for the add() method */
    function principle_products() {
        if (empty($this->data['LineItem']['principle_id'])) {
        }
        else {
            $this->set('products', $this->LineItem->Product->find('list', array('conditions'=>array('Product.principle_id'=>$this->data['LineItem']['principle_id']))));
        }
    }


    /* Display a list of Options (if any) for a given Product. Used for the add() method */
    function product_options() {

        /*$this->set('categories', $this->LineItem->Product->ProductOptionsCategory->find('list'),
			array('conditions' => array('ProductOptionsCategory.product_id'=>$this->data['LineItem']['product_id']),
			'order'=>'ProductOptionsCategory.location ASC') );*/

        $this->set('options', $this->LineItem->Product->ProductOptionsCategory->find('all',
                array('conditions' => array('ProductOptionsCategory.product_id'=>$this->data['LineItem']['product_id']),
                'order'=>'ProductOptionsCategory.location ASC')));


    }



    function edit($id = null) {


        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid LineItem', true));
            $this->redirect(array('action'=>'index'));
        }



        if (!empty($this->data)) {
            if ($this->LineItem->save($this->data)) {
                $this->Session->setFlash(__('The LineItem has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The LineItem could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->LineItem->read(null, $id);
        }

        $quotes = $this->LineItem->Quote->find('list');
        $products = $this->LineItem->Product->find('list');
        $this->set(compact('quotes','products'));
    }

    function delete($id = null) {

        $thisQP = $this->LineItem->read(null, $id);

        if (!$id) {
            $this->Session->setFlash(__('Invalid id for LineItem', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->LineItem->del($id)) {
            $this->Session->setFlash(__('LineItem deleted', true));
            $this->redirect(array('controller'=>'quotes', 'action'=>'view/'.$thisQP['Quote']['id']));
        }
    }



    function viewTable() {


        if($this->RequestHandler->isAjax()) {
            if (!empty($this->data)) {
                if($this->data['LineItem']['quote_id'] != null) {
                    $quoteid = $this->data['LineItem']['quote_id'];

                    $lineItems = $this->LineItem->find('all', array('recursive' => 0, 'conditions' => array('LineItem.quote_id' => $quoteid),
                            'order' => array('LineItem.item_number ASC'))
                    );

                    $quote = $this->LineItem->Quote->read(null, $quoteid);
                    $this->set('quote', $quote);

                    $this->set('lineItems', $lineItems);
                }
            }
            else {
                //Was POSTed with no data.
                $this->set('lineItems', 'Something has broken. Requesting a LineItem table without POSTing a quote or job ID');
            }
        }
    }


}
?>
