Shipments progress. Not finished yet

This commit is contained in:
Karl Cordes 2011-04-28 17:41:36 +10:00
parent 1bb9d77faf
commit 93c0b1ad78
13 changed files with 485 additions and 280 deletions

View file

@ -1,118 +0,0 @@
<?php
class ContactsController extends AppController {
var $name = 'Contacts';
var $helpers = array('Html', 'Form');
var $components = array('RequestHandler');
var $paginate = array(
'Contact' => array('order' => array('Contact.customer_id' => 'asc')),
'Enquiry' => array('order' => array('Enquiry.id' => 'asc'))
);
function index() {
$this->Contact->recursive = 0;
$this->set('contacts', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Contact.', true));
$this->redirect(array('action'=>'index'));
}
$this->set('contact', $this->Contact->read(null, $id));
//$enquiries = $this->Contact->Enquiry->findAllByContactId($id);
$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.contact_id' => $id)));
//$this->set('enquiries', $this->paginate($enquiries));
$statuses = $this->Contact->Enquiry->Status->find('all');
$status_list = array();
foreach ($statuses as $status) {
$status_list[] = array($status['Status']['id'], $status['Status']['name']);
}
$this->set('status_list', $status_list);
}
function add_to_customer($customerid = null) {
if(isset($this->params['named']['customerid'])) {
$customerid = $this->params['named']['customerid'];
}
if (!$customerid && empty($this->data)) {
$this->Session->setFlash(__('Invalid Customer ID', true));
$this->redirect(array('controller'=>'Enquiries', 'action'=>'index'));
}
if (!empty($this->data)) {
$this->Contact->create();
if ($this->Contact->save($this->data)) {
$this->Session->setFlash(__('The Contact has been saved', true));
$this->redirect(array('controller' => 'Customers', 'action'=>'view/'.$this->data['Contact']['customer_id']));
} else {
$this->Session->setFlash(__('The Contact could not be saved. Please try again.', true));
}
}
$contact_categories = $this->Contact->ContactCategory->find('list');
$customer = $this->Contact->Customer->findById($customerid);
$this->set(compact('customer', 'contact_categories'));
}
function add_another($increment) {
$this->set('contact_categories', $this->Contact->ContactCategory->find('list'));
$this->set('increment', $increment);
}
function remove_another($increment) {
$this->set('increment', $increment);
}
function add_one($customerid) {
$contact_categories = $this->Contact->ContactCategory->find('list');
$this->set('customerid', $customerid);
$this->set('contact_categories', $contact_categories);
}
function remove_one($customerid) {
$contacts = $this->Contact->find('all', array('conditions' => array('Contact.customer_id' => $customerid)));
$contacts = Set::Combine($contacts, '{n}.Contact.id', array('{0} {1}', '{n}.Contact.first_name', '{n}.Contact.last_name'));
$this->set('contacts', $contacts);
$this->set('customerid', $customerid);
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid Contact', true));
$this->redirect(array('action'=>'index'));
}
if (!empty($this->data)) {
$this->data['Contact']['name'] = $this->data['Contact']['first_name'].' '.$this->data['Contact']['last_name'];
if ($this->Contact->save($this->data)) {
$customerid = $this->data['Contact']['customer_id'];
$this->Session->setFlash(__('The Contact has been saved', true));
$this->redirect(array('controller' => 'Customers', 'action'=>'view/'.$customerid));
} else {
$this->Session->setFlash(__('The Contact could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Contact->read(null, $id);
}
$customers = $this->Contact->Customer->find('list');
$contact_categories = $this->Contact->ContactCategory->find('list');
$this->set(compact('customers', 'contact_categories'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for Contact', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Contact->del($id)) {
$this->Session->setFlash(__('Contact deleted', true));
$this->redirect(array('action'=>'index'));
}
}
}
?>

View file

@ -84,7 +84,8 @@ class CustomersController extends AppController {
if ($this->Customer->saveAll($this->data)) {
$this->Session->setFlash(__('The Customer has been saved', true));
$id = $this->Customer->id;
$this->redirect(array('action'=>'view/'.$id));
debug($this->data);
//$this->redirect(array('action'=>'view/'.$id));
} else {
$this->Session->setFlash(__('The Customer could not be saved. Please, try again.', true));

View file

@ -396,6 +396,44 @@ class JobsController extends AppController {
}
function autocomplete() {
$this->layout = 'ajax';
$query = strtolower($_GET["term"]);
//$this->set('customers_json', print_r($this->data));
if($query == null) {
return;
}
App::import('Core', 'Sanitize');
$query = Sanitize::clean($query);
$jobs = $this->Job->find('list', array(
'conditions' => array(
'Job.title LIKE' => '%'.$query.'%'
),'fields' => array('Job.title'),
'order' => array('Job.title ASC')
));
//print_r($POs);
foreach ($jobs as $id => $job) {
$job_list[] = array('id'=> $id, 'label' => $job);
}
$jobs_json = json_encode($job_list);
$this->set('jobs_json', $jobs_json);
}
/**
* Need to fix the customer_id which hasn't been set for these 500odd records.
*

View file

@ -71,6 +71,5 @@ class PrinciplesController extends AppController {
}
}
?>

View file

@ -78,6 +78,47 @@ class PurchaseOrdersController extends AppController {
$this->set(compact('jobs','principles','documents'));
}
function autocomplete() {
$this->layout = 'ajax';
$query = strtolower($_GET["term"]);
//$this->set('customers_json', print_r($this->data));
if($query == null) {
return;
}
App::import('Core', 'Sanitize');
$query = Sanitize::clean($query);
$POs = $this->PurchaseOrder->find('list', array(
'conditions' => array(
'PurchaseOrder.title LIKE' => '%'.$query.'%'
),'fields' => array('PurchaseOrder.title'),
'order' => array('PurchaseOrder.title ASC')
));
//print_r($POs);
foreach ($POs as $id => $po) {
$po_list[] = array('id'=> $id, 'label' => $po);
}
$POs_json = json_encode($po_list);
$this->set('POs_json', $POs_json);
}
/*function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for PurchaseOrder', true));

View file

@ -17,6 +17,13 @@ class ShipmentsController extends AppController {
function index() {
$this->Shipment->recursive = 0;
$this->set('shipments', $this->paginate());
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions(false);
$shipmentTypeShort = $this->typeOptions(true);
$this->set(compact('principles', 'freightForwarders', 'shipmentType', 'shipmentTypeShort'));
}
function view($id = null) {
@ -27,27 +34,46 @@ class ShipmentsController extends AppController {
$this->set('shipment', $this->Shipment->read(null, $id));
}
function ajax_edit() {
$this->layout = 'ajax';
if(!empty($this->data)) {
if ($this->Shipment->saveAll($this->data)) {
echo "SUCCESS";
}
else {
echo "FAILURE";
}
function add($action = 'add') {
$jobs = $this->Shipment->Job->find('list');
$principles = $this->Shipment->Principle->find('list');
$purchaseOrders = $this->Shipment->PurchaseOrder->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions();
$this->set(compact('jobs', 'principles', 'purchaseOrders', 'freightForwarders', 'shipmentType'));
}
else {
return;
}
}
function typeOptions() {
$options = array('','Import to CMC','Direct: Principle to Customer','Export','Local');
/* Moved the Views for Adding/Editing to elements. Called from the Index.
*
* function add($action = 'add') {
$principles = $this->Shipment->Principle->find('list');
$freightForwarders = $this->Shipment->FreightForwarder->find('list');
$shipmentType = $this->typeOptions();
$this->set(compact('principles', 'freightForwarders', 'shipmentType'));
}*/
function typeOptions($short = false) {
if($short == false) {
$options = array('','Import: Principle to CMC','Direct: Principle to Customer','Export','Local');
}
else {
$options = array('','Import','Direct','Export','Local');
}
return $options;
}

View file

@ -21,7 +21,13 @@ class Shipment extends AppModel {
'FreightService' => array(
'className' => 'FreightService',
'foreignKey' => 'freight_service_id'
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => array('User.type' => 'user')
)
);
var $hasMany = array(

View file

@ -78,7 +78,6 @@
<li><?php echo $html->link('Shipments','/shipments/index');?>
<ul>
<li class=""><?php echo $html->link('Shipments List', '/shipments/index');?></li>
<li class="last"><?php echo $html->link('New Shipment', '/shipments/add');?></li>
</ul>
</li>

View file

@ -2,9 +2,6 @@
<?php echo $form->create('Shipment');?>
<fieldset>
<legend><?php __('New Shipment');?></legend>
<?php
echo $form->input('shipmentType', array('options'=>$shipmentType, 'id'=>'shipmentType'));
@ -13,13 +10,19 @@
<?
echo $form->input('date_arrived_display', array('label'=>'Date Arrived', 'id'=>'date_arrived_display'));
echo $form->input('date_arrived', array('type'=>'hidden', 'id'=>'date_arrived'));
?>
</div>
<?
<div id="principle">
<? echo $form->input('Principle', array('type'=>'select', 'multiple'=>true)); ?>
</div>
<?
echo $form->input('PurchaseOrder');
echo $form->input('Job');
echo $form->input('Principle');
echo $form->input('freight_forwarder_id');
@ -30,5 +33,4 @@
echo $form->input('comments');
?>
</fieldset>
<?php echo $form->end();?>

View file

@ -9,6 +9,7 @@
?></p>
<table cellpadding="0" cellspacing="0" class="shipments">
<tr>
<th><?php echo $paginator->sort('user_id');?></th>
<th><?php echo $paginator->sort('date_arrived');?></th>
<th><?php echo $paginator->sort('date_dispatched');?></th>
<th><?php echo $paginator->sort('type');?></th>
@ -28,16 +29,26 @@
foreach ($shipments as $shipment):
?>
<tr>
<td>
<?
$initials = $shipment['User']['first_name'][0].$shipment['User']['last_name'][0];
<td>
<?php echo $shipment['Shipment']['date_arrived']; ?>
</td>
<td>
<?php echo $shipment['Shipment']['date_dispatched']; ?>
echo $html->link($initials, array('controller'=> 'users', 'action'=>'view', $shipment['User']['id'])); ?>
</td>
<td>
<?php echo $shipment['Shipment']['type']; ?>
<?php
echo $this->element('isEmptyDate', array('date'=>$shipment['Shipment']['date_arrived']));
?>
</td>
<td>
<?php
echo $this->element('isEmptyDate', array('date'=>$shipment['Shipment']['date_dispatched']));
?>
</td>
<td>
<?php echo $shipmentTypeShort[$shipment['Shipment']['type']]; ?>
</td>
<td>
@ -83,9 +94,7 @@
<?php echo $shipment['Shipment']['comments']; ?>
</td>
<td class="actions">
<?php echo $html->link(__('View', true), array('action' => 'view', $shipment['Shipment']['id'])); ?>
<?php echo $html->link(__('Edit', true), array('action' => 'edit', $shipment['Shipment']['id'])); ?>
<?php echo $html->link(__('Delete', true), array('action' => 'delete', $shipment['Shipment']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $shipment['Shipment']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
@ -104,7 +113,7 @@
<div id="dialog-form" title="New Shipment">
<?=$this->element('add_shipment');?>
</div>
<?php debug($shipments); ?>

View file

@ -1437,4 +1437,22 @@ tr.cancelled td {
table.shipments tr td {
font-size: 80%;
border: 1px solid;
}
table.shipments {
border: 1px solid;
}
.habtmList {
list-style: none;
margin: 0;
padding: 0;
}
.habtmList li {
padding-top: 0.5em;
padding-left: 0;
margin: 0;
}

View file

@ -0,0 +1,42 @@
$(function() {
$('#date_arrived_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_arrived'
});
$("#importFields").hide();
$("#shipmentType").change(function() {
var shipmentTypeID = getShipmentTypeID();
if(shipmentTypeID == 1) {
$("#importFields").show();
}
});
function getShipmentTypeID() {
var id;
$("#shipmentType :selected").each(function(i, selected) {
id = this.value;
});
return id;
}
});

View file

@ -1,32 +1,174 @@
$(function() {
resetForm();
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 600,
width: 350,
height: 900,
width: 600,
modal: true,
buttons: {
"New Shipment": function() {
var thisShipmentInputs = $('#ShipmentAddForm').find('input,select');
$.post('/shipments/ajax_edit', thisShipmentInputs, function(data) {
$( "#dialog-form" ).dialog('close');
window.location.reload();
});
},
Cancel: function() {
$( this ).dialog( "close" );
$("#ShipmentAddForm")[0].reset();
$("#principlesList").empty();
}
},
close: function() {
$("#importFields").hide();
$("#principle").hide();
}
});
$( "#newShipment" ).button().click(function() {
$.get('/shipments/add', function(data) {
$("#dialog-form").html(data);
$( "#dialog-form" ).dialog( "open" );
});
});
$('#date_arrived_display').datepicker({
showButtonPanel: true,
dateFormat: 'd M yy',
altFormat: 'yy-mm-dd',
altField: '#date_arrived'
});
$("#shipmentType").change(function() {
var shipmentTypeID = getSelectedID('#shipmentType');
if(shipmentTypeID == 1) { //Import
$("#importFields").show();
$("#principle").show();
}
});
//Remove X button clicked.
$('.removeFromList').live('click', function() {
$(this).parent().remove();
});
$('#addPrinciple').button().click(function() {
var principleID = getSelectedID('#PrinciplePrinciple');
if($('#PrincipleID_'+principleID).length > 0) { //This Principle is already in the List. Don't do anything.'
return false;
}
var principleName =getSelectedText('#PrinciplePrinciple');
//addPrincipleToList(principleID);
addToList('Principle', principleID, principleName, $('#principlesList') );
return false;
});
$( "#purchase_order_autocomplete" ).autocomplete({
source: "/purchase_orders/autocomplete",
minLength: 2,
select: function( event, ui ) {
//alert(ui.item.value + ":" + ui.item.id);
//alert($('#PurchaseOrderID_'+ui.item.id).length);
if($('#PurchaseOrderID_'+ui.item.id).length == 0) { //This Principle is already in the List. Don't do anything.'
addToList('PurchaseOrder', ui.item.id, ui.item.value, $('#purchaseOrdersList'));
}
}
});
$( "#job_autocomplete" ).autocomplete({
source: "/jobs/autocomplete",
minLength: 2,
select: function( event, ui ) {
//alert(ui.item.value + ":" + ui.item.id);
if($('#JobID_'+ui.item.id).length == 0) { //This Principle is already in the List. Don't do anything.'
addToList('Job', ui.item.id, ui.item.value, $('#jobsList'));
}
}
});
/**
* Resets the Form to its default site
*/
function resetForm() {
$(".hiddenDefault").hide();
$("#principle").hide();
$("#ShipmentAddForm")[0].reset();
}
/**
* A more generic way of handling the HABTM <ul> <li>[REMOVE BUTTON] NAME [HIDDEN INPUT]</li> </ul>
*/
function addToList(modelName, id, value, ULelement) {
var thisLI = $('<li></li>');
var thisButton = $('<button>X</button>');
thisButton.addClass('removeFromList');
thisButton.button();
var thisHiddenInput = $('<input type="hidden">');
var modelString = '['+modelName+']';
thisHiddenInput.attr('name', 'data'+modelString+modelString+'[]');
thisHiddenInput.attr('value', id);
thisLI.attr('id', modelName+'ID_'+id);
thisLI.html(value);
thisLI.prepend(thisButton);
thisLI.append(thisHiddenInput);
ULelement.append(thisLI);
}
function getSelectedID(elementID) {
var id;
$(elementID+" :selected").each(function(i, selected) {
id = this.value;
});
return id;
}
function getSelectedText(elementID) {
var text;
$(elementID+" :selected").each(function(i, selected) {
text = $(selected).text();
});
return text;
}
});