Beispiel #1
0
 /**
  * Create a new customer 
  * 
  * List of all the array parameters
  * 
  * @param array_item firstname
  * @param array_item lastname
  * @param array_item email
  * @param array_item password
  * @param array_item company
  * @param array_item statusId
  * @param array_item gender
  * @param array_item birthplace
  * @param array_item birthdate
  * @param array_item birthcountry
  * @param array_item birthnationality
  * @param array_item vat
  * @param array_item taxpayernumber
  * @param array_item contact
  * @param array_item contacttype
  * @param array_item address
  * @param array_item city
  * @param array_item code
  * @param array_item country
  * @param array_item area
  * @param array_item legalform
  * @param array_item company_type_id
  * @param array_item welcome_mail
  * @param array_item parent_id
  * @param array_item isreseller
  */
 public static function Create($data)
 {
     $locale = Shineisp_Registry::getInstance()->Zend_Locale;
     $customer = new Customers();
     $isDisabled = false;
     $language_id = Languages::get_language_id_by_code((string) $locale);
     // By default, welcome mail is sent
     $data['welcome_mail'] = isset($data['welcome_mail']) ? intval($data['welcome_mail']) : true;
     // Customer's parameters.
     $customer->company = !empty($data['company']) ? $data['company'] : null;
     $customer->firstname = !empty($data['firstname']) ? $data['firstname'] : null;
     $customer->lastname = !empty($data['lastname']) ? $data['lastname'] : null;
     $customer->gender = !empty($data['gender']) ? $data['gender'] : null;
     $customer->email = $data['email'] ? $data['email'] : null;
     $customer->password = crypt($data['password']);
     $customer->birthplace = !empty($data['birthplace']) ? $data['birthplace'] : null;
     $customer->birthdate = !empty($data['birthdate']) ? Shineisp_Commons_Utilities::formatDateIn($data['birthdate']) : null;
     $customer->birthdistrict = !empty($data['birthdistrict']) ? $data['birthdistrict'] : null;
     $customer->birthcountry = !empty($data['birthcountry']) ? $data['birthcountry'] : null;
     $customer->birthnationality = !empty($data['birthnationality']) ? $data['birthnationality'] : null;
     $customer->note = !empty($data['note']) ? $data['note'] : Null;
     $customer->vat = !empty($data['vat']) ? $data['vat'] : Null;
     $customer->taxpayernumber = !empty($data['taxpayernumber']) ? $data['taxpayernumber'] : Null;
     // Let's try to get status_id from status
     if (isset($data['status']) && !empty($data['status'])) {
         $customer->status_id = Statuses::id($data['status'], 'Customers');
         $isDisabled = isset($data['status']) && strtolower($data['status']) == 'disabled' ? true : false;
     } else {
         $customer->status_id = !empty($data['status_id']) ? $data['status_id'] : Statuses::id('Active', 'Customers');
     }
     $customer->legalform_id = !empty($data['legalform']) ? $data['legalform'] : Null;
     $customer->type_id = !empty($data['company_type_id']) ? $data['company_type_id'] : Null;
     $customer->parent_id = !empty($data['parent_id']) ? $data['parent_id'] : Null;
     $customer->isreseller = !empty($data['isreseller']) ? $data['isreseller'] : Null;
     $customer->language_id = !empty($language_id) ? intval($language_id) : 1;
     $customer->created_at = date('Y-m-d H:i:s');
     $customer->updated_at = date('Y-m-d H:i:s');
     // Try to get the real isp_id, based on logged user or on request uri
     // TODO: this should be done better
     $customer->isp_id = Isp::getCurrentId();
     // Generate an UUID. Used for API when you need to sync customers between services
     // TODO: allow custom uuid coming from api ? If yes, we should check for uniqueness in database
     $customer->uuid = Shineisp_Commons_Uuid::generate();
     // customer disabled? Disable its password
     if ($isDisabled) {
         $customer->password = '******' . $customer->password;
     }
     // Save the data
     $customer->save();
     if (!empty($data['contact'])) {
         $customer->Contacts[0]->contact = !empty($data['contact']) ? $data['contact'] : Null;
         $customer->Contacts[0]->type_id = !empty($data['contacttypes']) ? $data['contacttypes'] : Null;
         $customer->Contacts[0]->base = 1;
         $customer->save();
     }
     if (!empty($data['address'])) {
         $customer->Addresses[0]->address = !empty($data['address']) ? $data['address'] : Null;
         $customer->Addresses[0]->city = !empty($data['city']) ? $data['city'] : Null;
         $customer->Addresses[0]->code = !empty($data['code']) ? $data['code'] : Null;
         $customer->Addresses[0]->country_id = !empty($data['country_id']) ? $data['country_id'] : Null;
         $customer->Addresses[0]->area = !empty($data['area']) ? $data['area'] : Null;
         $customer->save();
     }
     $customerID = $customer->getIncremented();
     // Add the customer email in the newsletter list
     if (!$isDisabled) {
         NewslettersSubscribers::customer_optIn($customer['customer_id']);
     }
     // Send the welcome email
     if ($data['welcome_mail'] == true && !$isDisabled) {
         self::welcome_mail($customerID, $data['password']);
     }
     return $customerID;
 }