Documents controller revising quotes OK

This commit is contained in:
Karl Cordes 2011-08-12 15:46:26 +10:00
parent 87b56d5666
commit a92eb939a4
13 changed files with 217 additions and 145 deletions

View file

@ -8,23 +8,23 @@ class AppController extends Controller {
var $helpers = array('Javascript', 'Time', 'Html', 'Form'); var $helpers = array('Javascript', 'Time', 'Html', 'Form');
function beforeFilter() { function beforeFilter() {
$this->set('currentuser', $this->Auth->user()); $this->set('currentuser', $this->Auth->user());
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'enquiries', 'action' => 'index'); $this->Auth->loginRedirect = array('controller' => 'enquiries', 'action' => 'index');
$this->Auth->allow('display'); $this->Auth->allow('display');
$this->Auth->authorize = 'controller'; $this->Auth->authorize = 'controller';
if($this->RequestHandler->isAjax()) { if($this->RequestHandler->isAjax()) {
Configure::write('debug', 0); Configure::write('debug', 0);
} }
} }
function isAuthorized() { function isAuthorized() {
return true; return true;
} }
@ -44,7 +44,7 @@ class AppController extends Controller {
} }
function isManager() { function isManager() {
$currentuser = $this->getCurrentUser(); $currentuser = $this->getCurrentUser();
if($currentuser['access_level'] == 'manager') { if($currentuser['access_level'] == 'manager') {
return true; return true;
@ -78,7 +78,7 @@ class AppController extends Controller {
function calculateTotals($document, $gst) { function calculateTotals($document, $gst) {
$totals = array('subtotal'=>0, 'gst'=>0, 'total'=>0); $totals = array('subtotal'=>0, 'gst'=>0, 'total'=>0);
@ -102,6 +102,22 @@ class AppController extends Controller {
} }
function unset_keys($array, $keys) {
foreach($keys as $key ) {
$array[$key] = null;
}
return $array;
}
function unset_multiple_keys($array, $keys) {
foreach($array as $index => $item) {
$array[$index]['id'] = null;
$array[$index]['document_id'] = null;
$array[$index]['costing_id'] = null;
}
}

View file

@ -38,6 +38,24 @@ class DocumentsController extends AppController {
$this->set('currency', $this->Document->getCurrency($document)); $this->set('currency', $this->Document->getCurrency($document));
$this->set('enquiry', $this->Document->getEnquiry($document)); $this->set('enquiry', $this->Document->getEnquiry($document));
switch($docType) {
case 'quote':
$docTypeElement = 'document_quote_view';
break;
case 'invoice':
$docTypeElement = 'document_invoice_view';
break;
case 'purchaseOrder':
$docTypeElement = 'document_purchase_order_view';
break;
}
$this->set('docTypeElement', $docTypeElement);
// $this->set('principles', $this->Document->LineItem->Product->Principle->find('list')); // $this->set('principles', $this->Document->LineItem->Product->Principle->find('list'));
} }
@ -72,41 +90,6 @@ class DocumentsController extends AppController {
} }
} }
/**
* 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->Quote->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['Quote']['date_issued'] = date('Y-m-d');
$this->data['Quote']['currency_id'] = 2; //All new Quotes default to AUD.
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['enquiry_id'] = $enquiryid;
$this->data['Document']['type'] = 'quote';
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 Document * Create a New Document
@ -115,8 +98,8 @@ class DocumentsController extends AppController {
* @param <type> $type * @param <type> $type
* @param <type> $id * @param <type> $id
*/ */
function newDocument($type = null, $id = null) { function newDocument($type = null, $enquiryid = null, $jobid = 0) {
if(!$type|| !$id) { if(!$type) {
$this->Session->setFlash('Invalid Doc Type or ID'); $this->Session->setFlash('Invalid Doc Type or ID');
} }
@ -125,8 +108,7 @@ class DocumentsController extends AppController {
switch($type) { switch($type) {
case "quote": case "quote":
$enquiryid = $id; $enquiry = $this->Document->Quote->Enquiry->findById($enquiryid);
$enquiry = $this->Document->Quote->Enquiry->findById($id);
if(!$enquiry) { if(!$enquiry) {
$this->Session->setFlash('Invalid Enquiry ID'); $this->Session->setFlash('Invalid Enquiry ID');
@ -134,7 +116,6 @@ class DocumentsController extends AppController {
return; return;
} }
$this->data['Document']['enquiry_id'] = $enquiryid;
$this->data['Quote']['enquiry_id'] = $enquiryid; $this->data['Quote']['enquiry_id'] = $enquiryid;
$number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid); $number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid);
@ -143,14 +124,48 @@ class DocumentsController extends AppController {
$this->data['Quote']['currency_id'] = 2; //All new Quotes default to AUD. $this->data['Quote']['currency_id'] = 2; //All new Quotes default to AUD.
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id']; $this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
$this->data['Document']['enquiry_id'] = $enquiryid;
$this->data['Document']['type'] = 'quote'; $this->data['Document']['type'] = 'quote';
break; break;
case "invoice": case "invoice":
$invoice_number_offset = 4436; //What Invoice number we are up to. Starting at 4500 due to the data loss.
$number_of_invoices = $this->Document->Invoice->findCount();
$enquiry = $this->Document->Invoice->Enquiry->read(null, $enquiryid);
if(!$enquiry) {
$this->Session->setFlash('Invalid Enquiry ID');
$this->redirect(array('action'=>'index'));
return;
}
$newInvoiceNumber = $invoice_number_offset + $number_of_invoices;
$this->data['Invoice']['enquiry_id'] = $enquiryid;
$this->data['Invoice']['title'] = "CMCIN".$newInvoiceNumber;
$this->data['Invoice']['job_id'] = $jobid;
$this->data['Invoice']['customer_id'] = $enquiry['Enquiry']['customer_id'];
$this->data['Document']['user_id']= $enquiry['Enquiry']['user_id'];
//$number_of_revisions = $this->Document->Invoice->findCount('Invoice.enquiry_id ='. $enquiryid);
$this->data['Document']['revision'] = 0; //No revisions for Invoices. I have decreed it!
$this->data['Document']['type'] = 'invoice';
$this->data['Invoice']['issue_date'] = date('Y-m-d'); //Argh. Quote model uses 'date_issed'.
$this->data['Document']['currency_id'] = 2; //All new Invoices default to AUD.
break; break;
case "purchaseOrder": case "purchaseOrder":
break; break;
case "orderAck": case "orderAck":
break; break;
} }
@ -189,7 +204,40 @@ class DocumentsController extends AppController {
echo "WE HAVE AN INVOICE"; echo "WE HAVE AN INVOICE";
} }
else if (!empty($document['Quote']['id'])) { else if (!empty($document['Quote']['id'])) {
echo "WE ARE REVISING A QUOTE";
//First setup the Date and default valid until date. (30 days from today).
$today_date = date("Y-m-d");
$valid_until = date("Y-m-d", strtotime("+30 days"));
$newDoc = array();
$newDoc['Quote'] = $this->unset_keys($document['Quote'], array('id','created'));
$newDoc['Quote']['date_issued'] = $today_date;
$newDoc['Quote']['days_valid'] = 30;
$newDoc['Quote']['valid_until'] = $valid_until;
$enquiryid = $document['Quote']['enquiry_id'];
$number_of_revisions = $this->Document->Quote->findCount('Quote.enquiry_id ='. $enquiryid);
$newDoc['Document']['revision'] = $number_of_revisions;
$newDoc['Document']['type'] = 'quote';
$newDoc['DocPage'] = $document['DocPage'];
foreach($newDoc['DocPage'] as $index => $li) {
$newDoc['DocPage'][$index]['id'] = null;
$newDoc['DocPage'][$index]['document_id'] = null;
}
} }
else if (!empty($document['PurchaseOrder']['id'])) { else if (!empty($document['PurchaseOrder']['id'])) {
@ -201,10 +249,30 @@ class DocumentsController extends AppController {
echo "WE ARE REVISING An ORDER ACK"; echo "WE ARE REVISING An ORDER ACK";
} }
/** Copy the LineItems from this Document. Applies to all of the Document Types */
if(!empty($document['LineItem'])) {
$newDoc['LineItem'] = $document['LineItem'];
foreach($newDoc['LineItem'] as $index => $li) {
$newDoc['LineItem'][$index]['id'] = null;
$newDoc['LineItem'][$index]['document_id'] = null;
$newDoc['LineItem'][$index]['costing_id'] = null;
}
}
$this->set('newDoc', $newDoc);
if ($this->Document->saveAll($newDoc)) {
$newid = $this->Document->id;
$this->Session->setFlash(__("Revision {$number_of_revisions} created", 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. * 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. * @param int $id - the Enquiry ID that this quote belongs to.
@ -369,8 +437,8 @@ class DocumentsController extends AppController {
//If this is a quote, set the filename //If this is a quote, set the filename
//@TODO FIX THIS FOR OTHER DOCTYPES //@TODO FIX THIS FOR OTHER DOCTYPES
if($document['Quote']['revision'] > 0) { if($document['Document']['revision'] > 0) {
$filename = $enquiry['Enquiry']['title'].'rev'.$document['Quote']['revision'].'.pdf'; $filename = $enquiry['Enquiry']['title'].'_'.$document['Document']['revision'].'.pdf';
} }
else { else {
$filename = $enquiry['Enquiry']['title'].'.pdf'; $filename = $enquiry['Enquiry']['title'].'.pdf';

View file

@ -85,7 +85,7 @@ class InvoicesController extends AppController {
} }
function edit($id = null) { /* function edit($id = null) {
if (!$id && empty($this->data)) { if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Invoice', true)); $this->Session->setFlash(__('Invalid Invoice', true));
$this->redirect(array('action'=>'index')); $this->redirect(array('action'=>'index'));
@ -107,7 +107,7 @@ class InvoicesController extends AppController {
$users = $this->Invoice->User->find('list'); $users = $this->Invoice->User->find('list');
$jobs = $this->Invoice->Job->find('list', array('conditions'=>array('Job.enquiry_id'=>$invoice['Invoice']['enquiry_id']))); $jobs = $this->Invoice->Job->find('list', array('conditions'=>array('Job.enquiry_id'=>$invoice['Invoice']['enquiry_id'])));
$this->set(compact('users', 'jobs')); $this->set(compact('users', 'jobs'));
} }*/
/*function delete($id = null) { /*function delete($id = null) {
if (!$id) { if (!$id) {

View file

@ -228,7 +228,7 @@ class JobsController extends AppController {
$this->set('gst_options', $this->gst_options()); $this->set('gst_options', $this->gst_options());
$this->set('totals', $this->getTotals($jobs)); $this->set('totals', $this->getTotals($jobs));
print_r($totals); // print_r($totals);
//print_r($jobs); //print_r($jobs);
} }
@ -558,6 +558,7 @@ class JobsController extends AppController {
$this->data['Job']['customer_id'] = $enquiry['Enquiry']['customer_id']; $this->data['Job']['customer_id'] = $enquiry['Enquiry']['customer_id'];
$this->data['Job']['contact_id'] = $enquiry['Enquiry']['contact_id']; $this->data['Job']['contact_id'] = $enquiry['Enquiry']['contact_id'];
$this->data['Job']['enquiry_id'] = $enquiry['Enquiry']['id']; $this->data['Job']['enquiry_id'] = $enquiry['Enquiry']['id'];
$this->data['Job']['date_order_received'] = date('Y-m-d');
if ($this->Job->save($this->data)) { if ($this->Job->save($this->data)) {
echo "SUCCESS"; echo "SUCCESS";

View file

@ -95,6 +95,11 @@ class UsersController extends AppController {
$this->set('emails', $emails); $this->set('emails', $emails);
/ */ / */
/** $this->render() is a better approach than I used in View Document. Keeps the views out of Elements and in the right directory.
*
Will refactor at some point in the future. Maybe.
*
*/
switch ($user['User']['type']) { switch ($user['User']['type']) {
case 'contact': case 'contact':

View file

@ -19,11 +19,7 @@ class Invoice extends AppModel {
'Enquiry' => array( 'Enquiry' => array(
'className' => 'Enquiry', 'className' => 'Enquiry',
'foreignKey' => 'enquiry_id', 'foreignKey' => 'enquiry_id',
'counterCache'=>true 'counterCache'=> true
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
), ),
'Customer' => array( 'Customer' => array(
'className' => 'Customer', 'className' => 'Customer',

View file

@ -28,8 +28,8 @@ $your_reference = 'Enquiry on '.date('j M Y');
$issue_date = $document['Quote']['date_issued']; $issue_date = $document['Quote']['date_issued'];
$pdfdoc->docRef = $enquiryNumber; $pdfdoc->docRef = $enquiryNumber;
if($document['Quote']['revision'] > 0) { if($document['Document']['revision'] > 0) {
$enquiryNumber = $enquiryNumber.'.'.$document['Quote']['revision']; $enquiryNumber = $enquiryNumber.'.'.$document['Document']['revision'];
} }

View file

@ -6,7 +6,8 @@ echo $javascript->link('document_add_edit');
?> ?>
<h2><?=$docTypeFullName?>: <?=$html->link($enquiry['Enquiry']['title'], array('controller'=>'enquiries','action'=>'view',$enquiry['Enquiry']['id']));?> <? <h2><?=$docTypeFullName?>: <?=$html->link($enquiry['Enquiry']['title'], array('controller'=>'enquiries','action'=>'view',$enquiry['Enquiry']['id']));?>
<?
if($document['Document']['revision'] > 0) { if($document['Document']['revision'] > 0) {
echo "Revision {$document['Document']['revision']} "; echo "Revision {$document['Document']['revision']} ";
} }
@ -18,20 +19,7 @@ echo $javascript->link('document_add_edit');
<?=$this->element('pdf_created_message'); ?> <?=$this->element('pdf_created_message'); ?>
<? <?
echo $this->element($docTypeElement);
switch($docType) {
case 'quote':
echo $this->element('document_quote_view');
break;
case 'invoice':
echo $this->element('document_invoice_view');
break;
case 'purchaseOrder':
echo 'Purchase OOOOORDER!!';
break;
}
?> ?>
<button class="addLineItem">Add Line Item</button> <button class="addLineItem">Add Line Item</button>

View file

@ -1,45 +1,43 @@
<?php if (!empty($quotes)):?> <?php if (!empty($quotes)):?>
<table cellpadding = "0" cellspacing = "0" class="quotetable"> <table cellpadding = "0" cellspacing = "0" class="quotetable">
<tr> <tr>
<th><?php __('Revision'); ?></th> <th><?php __('Revision'); ?></th>
<th><?php __('Sale Currency'); ?></th> <th><?php __('Sale Currency'); ?></th>
<th><?php __('Date / Time Created'); ?></th> <th><?php __('Date / Time Created'); ?></th>
<th class="actions"><?php __('Actions');?></th> <th class="actions"><?php __('Actions');?></th>
</tr> </tr>
<?php <?php
$i = 0; $i = 0;
foreach ($quotes as $quote): foreach ($quotes as $quote):
$class = null; $class = null;
if ($i % 2 == 0) { if ($i % 2 == 0) {
$class = ' class="altrow"'; $class = ' class="altrow"';
} }
if($i == 0) { if($i == 0) {
$class = ' class="mostrecent"'; $class = ' class="mostrecent"';
} }
$i++; $i++;
?> ?>
<tr<?php echo $class;?>> <tr<?php echo $class;?>>
<td><?php echo $quote['Document']['revision'];?></td> <td><?php echo $quote['Document']['revision'];?></td>
<td><?php echo $quote['Currency']['iso4217'];?></td> <td><?php echo $quote['Currency']['iso4217'];?></td>
<td><?php echo $time->nice($quote['Quote']['created']);?></td> <td><?php echo $time->nice($quote['Quote']['created']);?></td>
<td class="actions"> <td class="actions">
<?php echo $html->link(__('View', true), array('controller'=> 'documents', 'action'=>'view', $quote['Quote']['document_id'])); ?> <?php echo $html->link(__('View', true), array('controller'=> 'documents', 'action'=>'view', $quote['Quote']['document_id'])); ?>
<?php echo $html->link(__('Revise', true), array('controller'=>'documents', 'action'=>'revise', $quote['Quote']['document_id'])); <?php echo $html->link(__('Revise', true), array('controller'=>'documents', 'action'=>'revise', $quote['Quote']['document_id']));
?> ?>
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
</table> </table>
<?php endif; ?> <?php endif; ?>
<div class="actions"> <div class="actions">
<ul> <button class="addQuote" id="<?=$enquiry['Enquiry']['id'];?>">New Quote</button>
<li><?php echo $html->link(__('New Quote', true), array('controller'=> 'documents', 'action'=>'newDocument/quote/'.$enquiry['Enquiry']['id']));?> </li> </div>
</ul>
</div>

View file

@ -146,7 +146,7 @@
}?> }?>
</h3> </h3>
<button>New Quote</button>
<? echo $this->element('quote_table', $quotes); <? echo $this->element('quote_table', $quotes);
?> ?>
@ -155,7 +155,7 @@
<div class="related"> <div class="related">
<h3>Jobs for this Enquiry</h3> <h3>Jobs from this Enquiry</h3>
<table cellpadding="0" cellspacing="0"> <table cellpadding="0" cellspacing="0">
<tr> <tr>
@ -168,8 +168,7 @@
<th>Date Sent to Customer</th> <th>Date Sent to Customer</th>
<th>Domestic Freight Paid By</th> <th>Domestic Freight Paid By</th>
<th>Sale Category</th>
<th>Shipment Category</th>
<th>Comments</th> <th>Comments</th>
<th class="actions"><?php __('Actions');?></th> <th class="actions"><?php __('Actions');?></th>
@ -210,18 +209,13 @@
<td> <td>
<?php echo $job['Job']['domestic_freight_paid_by']; ?> <?php echo $job['Job']['domestic_freight_paid_by']; ?>
</td> </td>
<td>
<?php echo $job['Job']['sale_category']; ?>
</td>
<td>
<?php echo $job['Job']['shipment_category']; ?>
</td>
<td> <td>
<?php echo $job['Job']['comments']; ?> <?php echo $job['Job']['comments']; ?>
</td> </td>
<td class="actions"> <td class="actions">
<?php echo $html->link(__('View', true), array('controller'=>'jobs', 'action' => 'view', $job['Job']['id'])); ?> <?php echo $html->link(__('View', true), array('controller'=>'jobs', 'action' => 'view', $job['Job']['id'])); ?>
<?php //echo $html->link(__('New Invoice for this Job', true), array('controller'=>'documents', 'action' => 'newDocument/invoice/'.$job['Job']['enquiry_id'].'/'.$job['Job']['id'])); ?>
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
@ -229,7 +223,7 @@
</div> </div>
<div class="actions"> <div class="actions">
<?=$javascript->link('add_job');?> <?=$javascript->link('view_enquiry');?>
<ul> <ul>
<li><button class="addJob" id="<?=$enquiry['Enquiry']['id'];?>">New Job</button></li> <li><button class="addJob" id="<?=$enquiry['Enquiry']['id'];?>">New Job</button></li>
</ul> </ul>
@ -259,7 +253,7 @@
<th>Customer</th> <th>Customer</th>
<th>Paid / Unpaid</th> <th>Paid / Unpaid</th>
<th>Payment Received Date</th> <th>Payment Received Date</th>
<th>User</th>
<th class="actions"><?php __('Actions');?></th> <th class="actions"><?php __('Actions');?></th>
</tr> </tr>
<?php <?php
@ -307,11 +301,9 @@
?> ?>
<td>
<?php echo $html->link($invoice['User']['username'], array('controller' => 'users', 'action' => 'view', $invoice['User']['id'])); ?>
</td>
<td class="actions"> <td class="actions">
<?php echo $html->link(__('View', true), array('controller'=>'invoices', 'action' => 'view', $invoice['Invoice']['id'])); ?> <?php echo $html->link(__('View', true), array('controller'=>'documents', 'action' => 'view', $invoice['Invoice']['document_id'])); ?>
<?php echo $html->link(__('Edit', true), array('controller'=>'invoices','action' => 'edit', $invoice['Invoice']['id'])); ?> <?php echo $html->link(__('Edit', true), array('controller'=>'invoices','action' => 'edit', $invoice['Invoice']['id'])); ?>
</td> </td>
@ -343,5 +335,5 @@
</div> </div>
<?php // debug($enquiry);?> <?php debug($enquiry);?>
<?php //debug($emails); ?> <?php //debug($emails); ?>

View file

@ -18,7 +18,7 @@
<th><?php echo $paginator->sort('paid');?></th> <th><?php echo $paginator->sort('paid');?></th>
<th><?php echo $paginator->sort('payment_received_date');?></th> <th><?php echo $paginator->sort('payment_received_date');?></th>
<th><?php echo $paginator->sort('user_id');?></th>
<th class="actions"><?php __('Actions');?></th> <th class="actions"><?php __('Actions');?></th>
</tr> </tr>
<?php <?php
@ -73,9 +73,7 @@
</td> </td>
<td>
<?php echo $html->link($invoice['User']['username'], array('controller' => 'users', 'action' => 'view', $invoice['User']['id'])); ?>
</td>
<td class="actions"> <td class="actions">
<?php echo $html->link(__('View', true), array('action' => 'view', $invoice['Invoice']['id'])); ?> <?php echo $html->link(__('View', true), array('action' => 'view', $invoice['Invoice']['id'])); ?>
<?php echo $html->link(__('Edit', true), array('action' => 'edit', $invoice['Invoice']['id'])); ?> <?php echo $html->link(__('Edit', true), array('action' => 'edit', $invoice['Invoice']['id'])); ?>

View file

@ -3,7 +3,7 @@
<div class="users view"> <div class="users view">
<h2><?php __('Contact: '.$user['User']['username'] );?></h2> <h2><?php __('Contact: '.$user['User']['first_name'].' '.$user['User']['last_name'] );?></h2>
<? if(isset($customer)):?> <? if(isset($customer)):?>
<h3>Customer: <?=$html->link($customer['Customer']['name'], '/customers/view/'.$customer['Customer']['id']);?></h3> <h3>Customer: <?=$html->link($customer['Customer']['name'], '/customers/view/'.$customer['Customer']['id']);?></h3>

View file

@ -50,4 +50,14 @@ $(function() {
} }
//Quote code below
$(".addQuote").click(function() {
window.location = '/documents/newDocument/quote/'+$( this ).attr('id');
});
}); });