Changed the logic a little bit in the customer model saveCustomer function, such that we can provide a default customer name if otherwise unspecified. Changed the page titles to reflect Customer vs Contact, since they can easily have the same names.
git-svn-id: file:///svn-source/pmgr/branches/invoice_receipt_20090629/site@228 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -87,7 +87,7 @@ class ContactsController extends AppController {
|
|||||||
$id));
|
$id));
|
||||||
|
|
||||||
// Prepare to render.
|
// Prepare to render.
|
||||||
$title = $contact['Contact']['display_name'];
|
$title = 'Contact: ' . $contact['Contact']['display_name'];
|
||||||
$this->set(compact('contact', 'title'));
|
$this->set(compact('contact', 'title'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +192,7 @@ class ContactsController extends AppController {
|
|||||||
'conditions' => array('Contact.id' => $id),
|
'conditions' => array('Contact.id' => $id),
|
||||||
));
|
));
|
||||||
|
|
||||||
$title = $this->data['Contact']['display_name'] . " : Edit";
|
$title = 'Contact: ' . $this->data['Contact']['display_name'] . " : Edit";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$title = "Enter New Contact";
|
$title = "Enter New Contact";
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ class CustomersController extends AppController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepare to render.
|
// Prepare to render.
|
||||||
$title = $customer['Customer']['name'];
|
$title = 'Customer: ' . $customer['Customer']['name'];
|
||||||
$this->set(compact('customer', 'title',
|
$this->set(compact('customer', 'title',
|
||||||
'outstanding_balance',
|
'outstanding_balance',
|
||||||
'outstanding_deposit'));
|
'outstanding_deposit'));
|
||||||
@@ -281,6 +281,20 @@ class CustomersController extends AppController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure we have at least one contact
|
||||||
|
if (!isset($this->data['Contact']) || count($this->data['Contact']) == 0) {
|
||||||
|
$this->Session->setFlash("MUST SPECIFY AT LEAST ONE CONTACT", true);
|
||||||
|
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure there is a primary contact
|
||||||
|
if (!isset($this->data['Customer']['primary_contact_entry'])) {
|
||||||
|
$this->Session->setFlash("MUST SPECIFY A PRIMARY CONTACT", true);
|
||||||
|
$this->redirect(array('action'=>'view', $this->data['Customer']['id']));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Go through each customer and strip the bogus ID if new
|
// Go through each customer and strip the bogus ID if new
|
||||||
foreach ($this->data['Contact'] AS &$contact) {
|
foreach ($this->data['Contact'] AS &$contact) {
|
||||||
if (isset($contact['source']) && $contact['source'] === 'new')
|
if (isset($contact['source']) && $contact['source'] === 'new')
|
||||||
@@ -303,7 +317,7 @@ class CustomersController extends AppController {
|
|||||||
|
|
||||||
if ($id) {
|
if ($id) {
|
||||||
$this->data = $this->Customer->details($id);
|
$this->data = $this->Customer->details($id);
|
||||||
$title = $this->data['Customer']['name'] . " : Edit";
|
$title = 'Customer: ' . $this->data['Customer']['name'] . " : Edit";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$title = "Enter New Customer";
|
$title = "Enter New Customer";
|
||||||
|
|||||||
@@ -194,6 +194,33 @@ class Customer extends AppModel {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function saveCustomer($id, $data, $primary_contact_entry) {
|
function saveCustomer($id, $data, $primary_contact_entry) {
|
||||||
|
|
||||||
|
// Go through each contact, and create new ones as needed
|
||||||
|
foreach ($data['Contact'] AS &$contact) {
|
||||||
|
if (isset($contact['id']))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$I = new Contact();
|
||||||
|
$I->create();
|
||||||
|
if (!$I->save($contact, false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$contact['id'] = $I->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the primary contact ID based on caller selection
|
||||||
|
$data['Customer']['primary_contact_id']
|
||||||
|
= $data['Contact'][$primary_contact_entry]['id'];
|
||||||
|
|
||||||
|
// Provide a default customer name if not specified
|
||||||
|
if (!$data['Customer']['name']) {
|
||||||
|
$this->Contact->recursive = -1;
|
||||||
|
$pcontact = $this->Contact->read(null, $data['Customer']['primary_contact_id']);
|
||||||
|
pr($pcontact);
|
||||||
|
$data['Customer']['name'] = $pcontact['Contact']['display_name'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the customer data
|
||||||
$this->create();
|
$this->create();
|
||||||
if ($id)
|
if ($id)
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
@@ -212,26 +239,15 @@ class Customer extends AppModel {
|
|||||||
/* $this->ContactsCustomer->deleteAll */
|
/* $this->ContactsCustomer->deleteAll */
|
||||||
/* (array('customer_id' => $id), false); */
|
/* (array('customer_id' => $id), false); */
|
||||||
|
|
||||||
// Avoid PHP notifications by making sure we have an array
|
// At this point, since we've saved data to contact,
|
||||||
if (!isset($data['Contact']))
|
// we'll proceed forward as much as possible, even
|
||||||
$data['Contact'] = array();
|
// if we encounter an error. For now, we'll assume
|
||||||
|
// the operation will succeed.
|
||||||
// Assume this operation will succeed
|
|
||||||
$ret = true;
|
$ret = true;
|
||||||
|
|
||||||
// Go through each entry of this customer method
|
// Go through each entry of this customer method
|
||||||
foreach ($data['Contact'] AS &$contact) {
|
foreach ($data['Contact'] AS &$contact) {
|
||||||
|
pr(compact('contact'));
|
||||||
// Check if the user has entered a brand new contact
|
|
||||||
if (!isset($contact['id'])) {
|
|
||||||
$I = new Contact();
|
|
||||||
$I->create();
|
|
||||||
if (!$I->save($contact, false)) {
|
|
||||||
$ret = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$contact['id'] = $I->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the ContactsCustomer to reflect the appropriate IDs
|
// Update the ContactsCustomer to reflect the appropriate IDs
|
||||||
$contact['ContactsCustomer']['customer_id'] = $id;
|
$contact['ContactsCustomer']['customer_id'] = $id;
|
||||||
@@ -241,15 +257,9 @@ class Customer extends AppModel {
|
|||||||
$CM = new ContactsCustomer();
|
$CM = new ContactsCustomer();
|
||||||
if (!$CM->save($contact['ContactsCustomer'], false)) {
|
if (!$CM->save($contact['ContactsCustomer'], false)) {
|
||||||
$ret = false;
|
$ret = false;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the primary contact ID based on caller selection
|
|
||||||
if (!$this->saveField('primary_contact_id',
|
|
||||||
$data['Contact'][$primary_contact_entry]['id']))
|
|
||||||
$ret = false;
|
|
||||||
|
|
||||||
// Return the result
|
// Return the result
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user