Implemented edit functionality for customer. Like with contacts, the interface leaves much to be desired, since it's limited in functionality. Namely, you can add a contact, but you can't add contact methods (phone/mail/email). Also, it doesn't use jqGrid to present the selection choices, and so the user is stuck with the fields we've chosen to display, as well as the sort order we've chosen. Enhancement is possible someday, but for now, it gets the job done.

git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629@219 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-07-06 02:06:23 +00:00
parent ab0e85824b
commit 09cb297dc7
5 changed files with 379 additions and 1 deletions

View File

@@ -268,6 +268,107 @@ class CustomersController extends AppController {
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: edit
* - Edit customer information
*/
function edit($id = null) {
if (isset($this->data)) {
if (isset($this->params['form']['cancel'])) {
if (isset($this->data['Customer']['id']))
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
else
$this->redirect(array('action'=>'index'));
return;
}
$this->Customer->create();
if (!$this->Customer->save($this->data, false)) {
pr("CUSTOMER SAVE FAILED");
$this->Session->setFlash("CUSTOMER SAVE FAILED", true);
return;
}
$this->data['Customer']['id'] = $this->Customer->id;
// Remove all associated Customer Contacts, as it ensures
// any entries deleted by the user actually get deleted
// in the system. We'll recreate the needed ones anyway.
$this->Customer->ContactsCustomer->deleteAll
(array('customer_id' => $this->data['Customer']['id']), false);
if (!isset($this->data['Contact']))
$this->data['Contact'] = array();
// Go through each entry of this customer method
foreach ($this->data['Contact'] AS &$contact) {
// Check if the user has entered a brand new contact
if (isset($contact['source']) && $contact['source'] === 'new') {
unset($contact['id']);
$I = new Contact();
$I->create();
if (!$I->save($contact, false)) {
pr("CONTACT SAVE FAILED");
$this->Session->setFlash("CONTACT SAVE FAILED", true);
continue;
}
$contact['id'] = $I->id;
}
// Update the ContactsCustomer to reflect the appropriate IDs
$contact['ContactsCustomer']['customer_id'] = $this->data['Customer']['id'];
$contact['ContactsCustomer']['contact_id'] = $contact['id'];
// Save the relationship between customer and contact
$CM = new ContactsCustomer();
if (!$CM->save($contact['ContactsCustomer'], false)) {
die("ContactsCustomer Save FAILED!");
}
}
// Now that the work is done, let the user view the updated customer
//$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
$this->autoRender = false;
return;
}
if ($id) {
$this->data = $this->Customer->details($id);
$title = $this->data['Customer']['name'] . " : Edit";
}
else {
$title = "Enter New Customer";
$this->data = array('Contact' => array(), 'PrimaryContact' => null);
}
$contact_types = array_flip($this->Customer->ContactsCustomer->getEnumValues('type'));
unset($contact_types[0]);
$contact_types = array_combine($contact_types, $contact_types);
$this->set(compact('contact_types'));
$contacts = $this->Customer->Contact->contactList();
$this->set(compact('contacts'));
// Prepare to render.
//pr($this->data);
$this->set(compact('title'));
$this->render('edit');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* action: add
* - Add a new customer
*/
function add() {
}
/**************************************************************************
**************************************************************************
**************************************************************************

View File

@@ -1,7 +1,8 @@
<?php
class Contact extends AppModel {
var $name = 'Contact';
var $displayField = 'display_name';
var $validate = array(
'id' => array('numeric'),
'display_name' => array('notempty'),
@@ -11,6 +12,7 @@ class Contact extends AppModel {
var $hasMany = array(
'ContactsMethod',
'ContactsCustomer',
);
var $hasAndBelongsToMany = array(
@@ -35,5 +37,12 @@ class Contact extends AppModel {
),
);
function contactList() {
return $this->find('list',
array('order' =>
//array('last_name', 'first_name', 'middle_name'),
array('display_name'),
));
}
}
?>

View File

@@ -0,0 +1,11 @@
<?php
class ContactsCustomer extends AppModel {
var $primaryKey = false;
var $belongsTo = array(
'Contact',
'Customer',
);
}
?>

View File

@@ -20,6 +20,7 @@ class Customer extends AppModel {
),
'Lease',
'LedgerEntry',
'ContactsCustomer',
);
var $hasAndBelongsToMany = array(

View File

@@ -0,0 +1,256 @@
<?php /* -*- mode:PHP -*- */
/**********************************************************************
* Because we make use of functions here,
* we need to put our top level variables somewhere
* we can access them.
*/
$this->varstore = compact('contactTypes', 'contacts');
//pr($this->data);
/**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
* Javascript
*/
function customerContactDiv($obj, $values = null) {
$div =
// BEGIN type-div
'<DIV>' . "\n" .
// BEGIN type-fieldset
'<FIELDSET CLASS="contact subset">' . "\n" .
'<LEGEND>Contact #%{id} (%{remove})</LEGEND>' . "\n" .
// BEGIN source-div
'<DIV ID="contact-%{id}-source-div">' . "\n" .
// BEGIN source-fieldset
'<FIELDSET>' . "\n" .
//'<LEGEND>Source</LEGEND>' . "\n" .
''
;
if (!isset($values)) {
foreach (array('Existing', 'New') AS $sname) {
$stype = strtolower($sname);
$div .=
'<INPUT TYPE="radio" ' . "\n" .
' NAME="data[Contact][%{id}][source]"' . "\n" .
' ONCLICK="switchContactSource(%{id}, '."'{$stype}'".')"' . "\n" .
' CLASS="contact-%{id}-source" ' . "\n" .
' ID="contact-%{id}-source-'.$stype.'"' . "\n" .
' VALUE="'.$stype.'"' . "\n" .
//' CHECKED' . "\n" .
' />' . "\n" .
' <LABEL FOR="contact-%{id}-source-'.$stype.'">'.$sname.'</LABEL>' . "\n" .
' ';
}
}
$div .= "\n";
if (isset($values)) {
$div .= customerContactTypeDiv($obj, 'show', $values);
}
else {
$div .= customerContactTypeDiv($obj, 'existing');
$div .= customerContactTypeDiv($obj, 'new');
}
$div .=
// END source-fieldset
'</FIELDSET>' . "\n" .
// END source-div
'</DIV>' . "\n" .
// BEGIN contact-div
'<div id="contact-%{id}-contact-div"' . "\n" .
$obj->element
('form_table',
array('class' => "item contact entry",
'field_prefix' => 'Contact.%{id}.ContactsCustomer',
'fields' => array
(
'type' => array
('opts' => array
('options' => $obj->varstore['contactTypes'],
'selected' => (isset($values) ? $values['ContactsCustomer']['type'] : null),
)),
'comment' => array
('opts' => array
('value' => (isset($values) ? $values['ContactsCustomer']['comment'] : null),
)),
))) . "\n" .
// END contact-div
'</div>' . "\n" .
// END type-fieldset
'</FIELDSET>' . "\n" .
// END type-div
'</DIV>'
;
return $div;
}
function customerContactTypeDiv($obj, $stype, $values = null) {
$element = 'form_table';
if ($stype === 'existing') {
$fields = array
('id' => array('name' => 'Contact',
'opts' => array('options' => $obj->varstore['contacts'])),
);
}
elseif ($stype === 'new') {
$fields = array
('first_name' => null,
'last_name' => null,
'middle_name' => null,
'display_name' => null,
'company_name' => array('name' => 'Company'),
'id_federal' => array('name' => 'SSN'),
'id_local' => array('name' => 'ID #'),
'id_local_state' => array('name' => 'ID State'),
/* 'id_local_exp' => array('name' => 'ID Expiration', */
/* 'opts' => array('empty' => true)), */
'comment' => null,
);
}
elseif ($stype === 'show') {
$element = 'table';
$column_class = array('field', 'value');
$rows = array(array('First Name', $values['first_name']),
//array('Middle Name', $values['middle_name']),
array('Last Name', $values['last_name']),
array('Company', $values['company_name']),
//array('SSN', $values['id_federal']),
/* array('ID', ($values['id_local'] */
/* . ($values['id_local'] */
/* ? " - ".$values['id_local_state'] */
/* : ""))), */
array('Comment', $values['comment']));
}
else {
die("\n\nInvalid stype ($stype)\n\n");
}
return
// BEGIN sourcetype-div
'<div ' . "\n" .
' class="contact-%{id}-div"' . "\n" .
' id="contact-%{id}-'.$stype.'-div"' . "\n" .
(isset($values) ? '' : ' STYLE="display:none;"' . "\n") .
'>' . "\n" .
$obj->element
($element,
array('class' => "item contact {$stype}",
'field_prefix' => 'Contact.%{id}')
+ compact('rows', 'fields', 'column_class')) .
($stype === 'show'
? '<input type="hidden" name="data[Contact][%{id}][id]" value="'.$values['id'].'"/>' . "\n"
: '') .
// END sourcetype-div
'</div>' . "\n" .
'';
}
?>
<script type="text/javascript"><!--
function addContact(flash) {
addDiv('contact-entry-id', 'contact', 'contacts', flash, <?php
echo FormatHelper::phpVarToJavascript(customerContactDiv($this),
null,
' ');
?>
);
}
// Reset the form
function resetForm() {
$('#contacts').html('');
$('#contact-entry-id').val(1);
<?php foreach ($this->data['Contact'] AS $contact): ?>
addDiv('contact-entry-id', 'contact', 'contacts', false, <?php
echo FormatHelper::phpVarToJavascript(customerContactDiv($this,
$contact
),
null,
' ');
?>
);
<?php endforeach; ?>
}
function switchContactSource(id, source) {
$(".contact-"+id+"-div")
.slideUp();
$("#contact-"+id+"-"+source+"-div")
.slideDown();
}
$(document).ready(function(){
resetForm();
});
--></script>
<?php
; // alignment
/**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
* Customer Edit
*/
echo '<div class="customer edit">' . "\n";
echo $form->create('Customer', array('action' => 'edit')) . "\n";
echo $form->input('id') . "\n";
echo($this->element
('form_table',
array('class' => 'item customer detail',
'caption' => $this->data ? 'Edit Customer' : 'New Customer',
'fields' => array
('name' => null,
'comment' => null,
))) . "\n");
echo $form->submit('Update') . "\n";
?>
<div CLASS="dynamic-set">
<fieldset CLASS="contact superset">
<legend>Contacts</legend>
<input type="hidden" id="contact-entry-id" value="0">
<div id="contacts"></div>
<fieldset> <legend>
<a href="#" onClick="addContact(true); return false;">Add a Contact</a>
</legend> </fieldset>
</fieldset>
</div>
<?php
; // Alignment
echo $form->submit('Update') . "\n";
echo $form->submit('Cancel', array('name' => 'cancel')) . "\n";
echo $form->end() . "\n";
echo '</div>' . "\n";