From e150237fd8cb5dff53483414d07e4bce634e925e Mon Sep 17 00:00:00 2001 From: abijah Date: Mon, 6 Jul 2009 02:06:23 +0000 Subject: [PATCH] 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/site@219 97e9348a-65ac-dc4b-aefc-98561f571b83 --- controllers/customers_controller.php | 101 +++++++++++ models/contact.php | 11 +- models/contacts_customer.php | 11 ++ models/customer.php | 1 + views/customers/edit.ctp | 256 +++++++++++++++++++++++++++ 5 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 models/contacts_customer.php create mode 100644 views/customers/edit.ctp diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 24f7e3b..2f2e58d 100644 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -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() { + } + /************************************************************************** ************************************************************************** ************************************************************************** diff --git a/models/contact.php b/models/contact.php index 24b51f8..6e7c69e 100644 --- a/models/contact.php +++ b/models/contact.php @@ -1,7 +1,8 @@ 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'), + )); + } } ?> \ No newline at end of file diff --git a/models/contacts_customer.php b/models/contacts_customer.php new file mode 100644 index 0000000..515744a --- /dev/null +++ b/models/contacts_customer.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/models/customer.php b/models/customer.php index 2454d30..6de7d3c 100644 --- a/models/customer.php +++ b/models/customer.php @@ -20,6 +20,7 @@ class Customer extends AppModel { ), 'Lease', 'LedgerEntry', + 'ContactsCustomer', ); var $hasAndBelongsToMany = array( diff --git a/views/customers/edit.ctp b/views/customers/edit.ctp new file mode 100644 index 0000000..58275ce --- /dev/null +++ b/views/customers/edit.ctp @@ -0,0 +1,256 @@ +varstore = compact('contactTypes', 'contacts'); + +//pr($this->data); +/********************************************************************** + ********************************************************************** + ********************************************************************** + ********************************************************************** + * Javascript + */ + +function customerContactDiv($obj, $values = null) { + + $div = + // BEGIN type-div + '
' . "\n" . + // BEGIN type-fieldset + '
' . "\n" . + 'Contact #%{id} (%{remove})' . "\n" . + + // BEGIN source-div + '
' . "\n" . + // BEGIN source-fieldset + '
' . "\n" . + //'Source' . "\n" . + '' + ; + + if (!isset($values)) { + foreach (array('Existing', 'New') AS $sname) { + $stype = strtolower($sname); + $div .= + '' . "\n" . + ' ' . "\n" . + ' '; + } + } + $div .= "\n"; + + if (isset($values)) { + $div .= customerContactTypeDiv($obj, 'show', $values); + } + else { + $div .= customerContactTypeDiv($obj, 'existing'); + $div .= customerContactTypeDiv($obj, 'new'); + } + + $div .= + // END source-fieldset + '
' . "\n" . + // END source-div + '
' . "\n" . + + // BEGIN contact-div + '
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 + '
' . "\n" . + + // END type-fieldset + '
' . "\n" . + // END type-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 + '' . "\n" . + ''; +} + + +?> + + + +' . "\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"; +?> + +
+
+ Contacts + +
+
+ Add a Contact +
+
+
+ +submit('Update') . "\n"; +echo $form->submit('Cancel', array('name' => 'cancel')) . "\n"; +echo $form->end() . "\n"; +echo '' . "\n";