<?php
class QuotePagesController extends AppController {

    var $name = 'QuotePages';
    var $helpers = array('Html', 'Form', 'Ajax', 'Number', 'Fck');

    var $components = array('RequestHandler');

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

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

    function add($id = null) {

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



        if (!empty($this->data)) {
            $this->QuotePage->create();
            if ($this->QuotePage->save($this->data)) {
                $this->Session->setFlash(__('The Quote Page has been saved', true));
                $this->redirect(array('controller'=>'quotes', 'action'=>'view/'.$this->data['QuotePage']['quote_id']));
            } else {
                $this->Session->setFlash(__('The Quote Page could not be saved. Please, try again.', true));
            }
        }
        $quotes = $this->QuotePage->Quote->find('list');
        $this->set(compact('quotes'));
        $this->set('quoteid', $id);
        $number_of_pages = $this->QuotePage->find('count', array('conditions' => array('QuotePage.quote_id'=>$id)));
        $number_of_pages++;
        $this->set('pagenumber', $number_of_pages);
    }

    function edit($id = null) {

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

        if (!empty($this->data)) {
            if ($this->QuotePage->save($this->data)) {
                $this->Session->setFlash(__('The QuotePage has been saved', true));
                $id = $this->data['QuotePage']['quote_id'];
                $this->redirect(array('controller' => 'quotes', 'action'=>'view/'.$id));
            } else {
                $this->Session->setFlash(__('The QuotePage could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->QuotePage->read(null, $id);
            $this->set('content', $this->data['QuotePage']['content']);
        }
        $quotes = $this->QuotePage->Quote->find('list');
        $this->set(compact('quotes'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for QuotePage', true));
            $this->redirect(array('action'=>'index'));
        }

        $quotepage = $this->QuotePage->findById($id);
        $quoteid = $quotepage['QuotePage']['quote_id'];

        if ($this->QuotePage->del($id)) {


            $this->Session->setFlash(__('Quote Page deleted', true));
            $this->redirect(array('controller' => 'quotes' , 'action'=>'view', $quoteid));
        }
    }


    function show($id = null) {
        $this->layout = 'ajax';
        $this->set('quotePage', $this->QuotePage->read(null, $id));

    }

    function frame($id = null) {
        $this->layout = 'ajax';
        $this->set('id', $id);
    }


    function regeneratePage($id = null) {
        if(!$id) {
            $this->Session->setFlash(__('Invalid id for QuotePage', true));
            $this->redirect(array('action'=>'index'));
        }
        else {
            $quotePage = $this->QuotePage->read(null, $id);

            //$enquiry = $this->QuotePage->Quote->Enquiry->find('all', array('conditions' => array('Enquiry.id' => $quotePage['Quote']['enquiry_id'])));

            $enquiry = $this->QuotePage->Quote->Enquiry->read(null, $quotePage['Quote']['enquiry_id']);

            //  $quote = $this->QuotePage->Quote->find('all', array('conditions' => array('Quote.id' => $quotePage['Quote']['id'])));

            $quoteProducts = $this->QuotePage->Quote->LineItem->find('all', array('recursive' => 0, 'conditions' =>
                array('LineItem.quote_id' => $quotePage['Quote']['id']),
                'order' => array('LineItem.item_number ASC'))
            );

            $contactFirstName = $enquiry['Contact']['first_name'];

            $salesEngineerName = $enquiry['User']['first_name'].' '.$enquiry['User']['last_name'];

            $productRows = "";


            foreach($quoteProducts as $quoteproduct) {

                $itemNumber = $quoteproduct['LineItem']['item_number'];
                $title = $quoteproduct['LineItem']['title'];

                if($quoteproduct['LineItem']['option'] == 1) {
                    $optionText = "Option ";
                }
                else {
                    $optionText = "";
                }

                $productRows .= <<<ENDROW

<tr>
<td>
                    $optionText
                    $itemNumber
</td>
<td>
                    $title
</td>
</tr>
ENDROW;

            }


            $this->data['QuotePage']['content'] = <<<ENDCONTENT

<p>Dear $contactFirstName, </p>
<p>&nbsp;</p>

<p> Thank for your recent enquiry. Please see the following quotation for: </p>

 <table border="0" cellpadding="1" cellspacing="1" style="width: 100%; ">

<tbody>
<tr>
<th><b>Item</b></th>
<th><b>Product</b></th>
</tr>


                $productRows

</tbody>
</table

<p> &nbsp;</p>

<p>Regards, </p>

<p>$salesEngineerName</p>

ENDCONTENT;



            if (!empty($this->data)) {
                if ($this->QuotePage->save($this->data)) {
                    $this->Session->setFlash(__('The QuotePage has been saved', true));
                    $id = $quotePage['Quote']['id'];
                    $this->redirect(array('controller' => 'quotes', 'action'=>'view/'.$id));
                } else {
                    $this->Session->setFlash(__('The QuotePage could not be saved. Please, try again.', true));
                }
            }



            debug($quotePage);

            echo "<hr>";
            debug ($quoteProducts);

        }



    }




}
?>
