<?php
class DocumentsController extends AppController {

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

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

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


        $this->set('customer', $this->Document->Enquiry->Customer->findById($document['Enquiry']['customer_id']));

        $this->set('currencies', $this->Document->Quote->Currency->find('list'));

        $this->set('principles', $this->Document->Enquiry->Principle->find('list'));

        $pages = $document['Page'];


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


        $docType = "";

        if(!empty($document['Invoice']['id'])) {
            $docType = "invoice";
        }
        elseif(!empty($document['Quote']['id'])) {
            $docType = "quote";
        }
        elseif(!empty($document['PurchaseOrder']['id'])) {
            $docType = "purchaseOrder";
        }
        $this->set('docType',$docType);
    }

    /**
     * Create a new Quote, along with its associated Document and the first two pages.
     * @param int $id - the Enquiry ID that this quote belongs to.
     */
    function newQuote($id = null) {
        $enquiryid = $id;
        $this->Document->create();

        $enquiry = $this->Document->Enquiry->findById($id);

        $this->data['Document']['enquiry_id'] = $enquiryid;

        $this->data['Quote']['enquiry_id'] = $enquiryid;
        $number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid);
        $this->data['Quote']['revision'] = $number_of_revisions;

        $this->data['Document']['customer_id'] = $enquiry['Enquiry']['customer_id'];
        $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
        $this->data['Document']['contact_id']= $enquiry['Enquiry']['contact_id'];
        $this->data['Page'][0]['page_number'] = 1;
        $this->data['Page'][0]['contentPage'] = 1;
        $this->data['Page'][1]['page_number'] = 2;
        $this->data['Page'][1]['contentPage'] = 0;

        if ($this->Document->saveAll($this->data)) {
            $newid = $this->Document->id;

            $this->Session->setFlash(__('The Document has been saved', true));
            $this->redirect(array('action'=>'view',$newid));
        } else {
            $this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
        }


    }



    /**
     * Create a new Quote, along with its associated Document and the first two pages.
     * @param int $id - the Enquiry ID that this quote belongs to.
     */
    function newInvoice($id = null) {
        $enquiryid = $id;
        $this->Document->create();

        $enquiry = $this->Document->Enquiry->findById($id);

        $this->data['Document']['enquiry_id'] = $enquiryid;

        $this->data['Invoice']['enquiry_id'] = $enquiryid;
        $invoice_number_offset = 4436; //What Invoice number we were up to before the new system.
        $number_of_invoices = $this->Document->Invoice->findCount();

        $newInvoiceNumber = $invoice_number_offset + $number_of_invoices;

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


        $this->data['Document']['customer_id'] = $enquiry['Enquiry']['customer_id'];
        $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
        $this->data['Document']['contact_id']= $enquiry['Enquiry']['contact_id'];
        $this->data['Page'][0]['page_number'] = 1;
        $this->data['Page'][0]['contentPage'] = 0;

        if ($this->Document->saveAll($this->data)) {
            $newid = $this->Document->id;

            $this->Session->setFlash(__('The Document has been saved', true));
            $this->redirect(array('action'=>'view',$newid));
        } else {
            $this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
        }


    }

    /* function add() {
        if (!empty($this->data)) {
            $this->Document->create();
            if ($this->Document->save($this->data)) {
                $this->Session->setFlash(__('The Document has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Document could not be saved. Please, try again.', true));
            }
        }
        $customers = $this->Document->Customer->find('list');
        $enquiries = $this->Document->Enquiry->find('list');
        $jobs = $this->Document->Job->find('list');
        $users = $this->Document->User->find('list');
        $contacts = $this->Document->Contact->find('list');
        $this->set(compact('customers', 'enquiries', 'jobs', 'users', 'contacts'));
    }*/

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

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

}
?>