Exemplo n.º 1
0
 public function actionDelete(array $ids)
 {
     foreach ($ids as $id) {
         $user = User::model()->findByPk($id);
         $userEmails = array($user->email);
         // Delete contact of this user
         $contact = CustomerContact::model()->find("user_id = {$id}");
         if ($contact instanceof CustomerContact) {
             // Get all emails of his contact
             $sql = 'SELECT DISTINCT email
                     FROM ' . SITE_ID . "_crm_contactemail\n                        WHERE contactid = {$contact->id}";
             $contactEmails = app()->db->createCommand($sql)->queryAll();
             foreach ($contactEmails as $contactEmail) {
                 $userEmails[] = $contactEmail['email'];
             }
             $result = app()->XService->run("CustomerContact.CustomerContact.delete", array('id' => $contact->id));
             if ($result instanceof CException) {
                 return $this->result = $result;
             }
         }
         // Delete customer of this user
         $customer = Customer::model()->find("user_id = {$id}");
         if ($customer instanceof Customer) {
             $result = app()->XService->run("Customer.Customer.delete", array('ids' => array($customer->id), 'isForceDelete' => true));
             if ($result instanceof CException) {
                 return $this->result = $result;
             }
         }
         // Delete all rows in table alliance_qualification has email match with emails of this user
         $userEmails = "'" . implode("','", $userEmails);
         $userEmails = rtrim($userEmails, ",'") . "'";
         OfferQualification::model()->deleteAll("email IN ({$userEmails})");
         // Delete alliance info of this user
         $result = app()->XService->run('Alliance.IComfortApplication.deleteICCardInfoByUser', array('userId' => $id));
         if ($result instanceof CException) {
             return $this->result = $result;
         }
         // Delete vip rewards info of this user
         $result = app()->XService->run('VipRewards.VipRewards.deleteByUser', array('userId' => $id));
         if ($result instanceof CException) {
             return $this->result = $result;
         }
         try {
             $user->delete();
         } catch (CException $ex) {
             errorHandler()->commonException()->modelsWithGiveIdsCantBeDeleted('User', array($id));
         }
     }
     return $this->result = true;
 }
 public function actionUpdateContact($id)
 {
     $contact = Contact::model()->findByPk($id);
     $customerContact = CustomerContact::model()->findByPk($contact->parent_id);
     $customer = Customer::model()->findByPk($customerContact->customer_id);
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation(array($customerContact));
     if (isset($_POST['Contact']) && isset($_POST['CustomerContact'])) {
         $trans = Yii::app()->db->beginTransaction();
         try {
             $contact->attributes = $_POST['Contact'];
             $customerContact->attributes = $_POST['CustomerContact'];
             if ($customerContact->save()) {
                 $contact->parent_model = 'CustomerContact';
                 $contact->parent_id = $customerContact->id;
                 if ($contact->save()) {
                     $trans->commit();
                     Yii::app()->user->setFlash('success', 'Contact Customer sudah dirubah.');
                     $this->redirect(array('customer/update', 'id' => $customerContact->customer_id));
                 } else {
                     throw new CustomerControllerException($contact->getErrors());
                 }
             } else {
                 throw new CustomerControllerException($customerContact->getErrors());
             }
         } catch (CustomerControllerException $e) {
             $trans->rollback();
         } catch (CDbException $e) {
             $trans->rollback();
         }
     }
     $this->render('update_contact', array('contact' => $contact, 'customerContact' => $customerContact, 'id' => $id, 'customer_name' => $customer->name));
 }