Example #1
0
 public function delete_customer($customer_id, $remove_linked_data = true)
 {
     $customer_id = (int) $customer_id;
     if ($customer_id > 0) {
         if (_DEMO_MODE && $customer_id == 1) {
             set_error('Sorry this is a Demo Customer. It cannot be changed.');
             redirect_browser(self::link_open($customer_id));
         }
         $customer = self::get_customer($customer_id);
         if ($customer && $customer['customer_id'] == $customer_id) {
             // todo: Delete emails (wack these in this customer_deleted hook)
             hook_handle_callback('customer_deleted', $customer_id, $remove_linked_data);
             if (class_exists('module_group', false)) {
                 // remove the customer from his groups
                 module_group::delete_member($customer_id, 'customer');
             }
             if (class_exists('module_extra', false)) {
                 module_extra::delete_extras('customer', 'customer_id', $customer_id);
             }
             // remove the contacts from this customer
             foreach (module_user::get_contacts(array('customer_id' => $customer_id)) as $val) {
                 if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                     module_user::delete_user($val['user_id']);
                 }
             }
             // remove staff
             delete_from_db('customer_user_rel', 'customer_id', $customer_id);
             if (class_exists('module_note', false)) {
                 module_note::note_delete("customer", 'customer_id', $customer_id);
             }
             handle_hook("address_delete", $this, 'all', "customer", 'customer_id', $customer_id);
             // todo, check the 'delete' permission on each one of these 'delete' method calls
             // do that better when we remove each of these and put them into the customer delete hook
             if ($remove_linked_data) {
                 if (class_exists('module_website', false) && module_website::is_plugin_enabled()) {
                     foreach (module_website::get_websites(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             module_website::delete_website($val['website_id']);
                         }
                     }
                 }
                 if (class_exists('module_job', false) && module_job::is_plugin_enabled()) {
                     foreach (module_job::get_jobs(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             module_job::delete_job($val['job_id']);
                         }
                     }
                 }
                 if (class_exists('module_invoice', false) && module_invoice::is_plugin_enabled()) {
                     foreach (module_invoice::get_invoices(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             module_invoice::delete_invoice($val['invoice_id']);
                         }
                     }
                 }
                 if (class_exists('module_quote', false) && module_quote::is_plugin_enabled()) {
                     foreach (module_quote::get_quotes(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             module_quote::delete_quote($val['quote_id']);
                         }
                     }
                 }
                 //handle_hook("file_delete",$this,"customer",'customer_id',$customer_id);
             } else {
                 // instead of deleting these records we just update them to customer_id = 0
                 if (class_exists('module_website', false) && module_website::is_plugin_enabled()) {
                     foreach (module_website::get_websites(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             update_insert('website_id', $val['website_id'], 'website', array('customer_id' => 0));
                         }
                     }
                 }
                 if (class_exists('module_job', false) && module_job::is_plugin_enabled()) {
                     foreach (module_job::get_jobs(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             update_insert('job_id', $val['job_id'], 'job', array('customer_id' => 0));
                         }
                     }
                 }
                 if (class_exists('module_invoice', false) && module_invoice::is_plugin_enabled()) {
                     foreach (module_invoice::get_invoices(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             update_insert('invoice_id', $val['invoice_id'], 'invoice', array('customer_id' => 0));
                         }
                     }
                 }
                 if (class_exists('module_quote', false) && module_quote::is_plugin_enabled()) {
                     foreach (module_quote::get_quotes(array('customer_id' => $customer_id)) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             update_insert('quote_id', $val['quote_id'], 'quote', array('customer_id' => 0));
                         }
                     }
                 }
                 if (class_exists('module_file', false) && module_file::is_plugin_enabled()) {
                     foreach (module_file::get_files(array('owner_id' => $customer_id, 'owner_table' => 'customer')) as $val) {
                         if ($val['customer_id'] && $val['customer_id'] == $customer_id) {
                             update_insert('file_id', $val['file_id'], 'file', array('owner_id' => 0, 'owner_table' => ''));
                         }
                     }
                 }
             }
             // finally delete the main customer record
             // (this is so the above code works with its sql joins)
             $sql = "DELETE FROM " . _DB_PREFIX . "customer WHERE customer_id = '" . $customer_id . "' LIMIT 1";
             query($sql);
         }
     }
 }