<?php
class InvoicesController extends AppController {

    var $name = 'Invoices';
    var $helpers = array('Html', 'Form', 'Time');


    var $paginate = array(
        'limit' => 100,
        'contain' => array('Customer')
    );

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

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



    function add() {

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

            $invoice_number_offset = 4436; //What Invoice number we are up to. Starting at 4500 due to the data loss.

            $number_of_invoices = $this->Invoice->findCount();

            $newInvoiceNumber = $invoice_number_offset + $number_of_invoices;

            $this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;

            $enqid = $this->data['Invoice']['enquiry_id'];


            if ($this->Invoice->save($this->data)) {
                $this->Session->setFlash(__('The Invoice has been saved', true));
                $this->redirect(array('controller'=>'enquiries', 'action'=>'view/'.$enqid));
            } else {
                $this->Session->setFlash(__('The Invoice could not be saved. Please, try again.', true));
            }
        }
        else {
            if(isset($this->params['named']['enquiryid'])) {
                $enquiryid = $this->params['named']['enquiryid'];
                $enquiry = $this->Invoice->Enquiry->findById($enquiryid);
                
                

                $user = $this->Auth->user();
                $this->set(compact('enquiry', 'user'));
            }
            else {
                $this->Session->setFlash(__('Invalid Enquiry ID', true));
                $this->redirect(array('action'=>'index'));
            }
        }

    }

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

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

}

?>