* @package WB * @subpackage ITS */ WBClass::load('WBEvent_Handler' , 'WBEvent_Handler_ITS' , 'patI18n'); /** * WBEvent_Handler_ITS_AutoAddCustomer * * Add customer id if not set already * * @version 1.0.1 * @package Wombat * @subpackage ITS */ class WBEvent_Handler_ITS_AutoAddCustomer extends WBEvent_Handler_ITS { /** * Handler config * * * @see WBEvent_Handler_Mail * @var array */ protected $config = array( 'loadticket' => '' ); /** * Customer * @var ValueAce_Customer */ private $customer; /** * Process event * * - check if customer id is already set, if yes, return * - generat customer data from ticket * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $this->loadTicket($e); if (empty($this->ticket)) { return true; } $id = $this->ticket->getId(); if (empty($id)) { return true; } $this->customer = WBClass::create('ValueAce_Customer'); $pCus = $this->customer->getIdentifier(); $ticket = $this->ticket->getData(); if (0 < $ticket[$pCus]) { return true; } $cus = $this->customer->getByEmail($ticket['emailurlid']); if (0 < count($cus)) { $cus = $cus[0]; $save = array( $pCus => $cus[$pCus] ); $this->ticket->save($save); return true; } // add customer $map = array( 'company' => 'customer', 'emailurlid' => 'contactemailurlid', 'forename' => 'contactforename', 'surname' => 'contactname', 'phone' => 'contactphone', 'addresslabel' => 'billaddresslabel', 'country' => 'country' ); $cus = array( 'customerdescription' => sprintf('
Ticket %s: Automatically created from Ticket
', $this->ticket->getObscureId()), 'contactname' => '', 'creditor' => 0, 'debitor' => 1 ); foreach ($map as $t => $c) { if (empty($ticket[$t])) { continue; } $cus[$c] = $ticket[$t]; } // add forename and surname if (!empty($ticket['forename'])) { $cus['contactname'] = trim($ticket['forename'] . ' ' . $cus['contactname']); } if (!empty($ticket['surname'])) { $cus['contactsurname'] = $ticket['surname']; } if (empty($cus['billaddresslabel'])) { $al = array(); if (!empty($cus['customer'])) { $al[] = $cus['customer']; } if (!empty($cus['contactname'])) { $al[] = $cus['contactname']; } if (!empty($ticket['street'])) { $al[] = $ticket['street']; } if (!empty($ticket['city'])) { if (!empty($ticket['zip'])) { $al[] = sprintf('%s %s', $ticket['zip'], $ticket['city']); } else { $al[] = $ticket['city']; } } $cus['billaddresslabel'] = implode("\n", $al); } if (empty($cus['customer'])) { $cus['customer'] = trim($cus['contactname']); } if (empty($cus['customer'])) { $cus['customer'] = trim($cus['contactforename'] . ' ' . $cus['contactsurname']); } if (empty($cus['customer'])) { $cus['customer'] = 'Ticket ' . $ticket->getObscureId(); } $idCus = $this->customer->add($cus); $save = array( $pCus => $idCus ); $this->ticket->save($save); return true; } }