/**
  * edit method
  *
  * @param string $id
  * @return void
  */
 public function edit($id = null, $section = null)
 {
     if ($section == 'basic') {
         $section = 'builder-basic';
     }
     $this->Customer->id = $id;
     if (!$this->Customer->exists()) {
         throw new NotFoundException(__('Invalid customer'));
     }
     if ($this->request->is('post') || $this->request->is('put')) {
         $exit_email = $this->Customer->find('first', array('fields' => array('email'), 'conditions' => array('email' => $this->request->data['Customer']['email'])));
         $this->request->data['Customer']['last_name'] = $this->request->data['BuilderAccount']['builder_legal_name'];
         //      debug($this->request->data);exit;
         App::import('Model', 'CustomerManager.BuilderAccount');
         $this->BuilderAccount = new BuilderAccount();
         $builder_account_info = $this->BuilderAccount->find('all', array('conditions' => array('BuilderAccount.customer_id' => $id)));
         $BuilderAccount_old_id = $builder_account_info[0]['BuilderAccount']['id'];
         if ($this->Customer->save($this->request->data)) {
             $msg = "";
             if ($exit_email) {
                 $msg = '"' . $this->request->data['Customer']['email'] . '" This email address already exist.';
             }
             $this->Session->setFlash(__('The customer has been saved. ' . $msg));
             if ($section != 'builder-basic') {
                 $this->redirect(array('action' => 'detail_section', $this->Customer->id, $section));
             } else {
                 $sales_representatives = serialize($this->request->data['Customer']['sales_representative']);
                 $customer_id_sales = $this->Customer->id;
                 App::uses("CustomerSalesRepresentetives", "CustomerManager.Model");
                 $csr = new CustomerSalesRepresentetives();
                 $csr->deleteAll(array('CustomerSalesRepresentetives.customer_id' => $this->Customer->id));
                 foreach ($this->request->data['Customer']['sales_representative'] as $value) {
                     $sales_list = array('user_id' => $value, 'customer_id' => $this->Customer->id);
                     $user = new CustomerSalesRepresentetives();
                     $user->save($sales_list);
                 }
                 App::uses("BuilderSupplyTypesList", "CustomerManager.Model");
                 $bst = new BuilderSupplyTypesList();
                 $bst->deleteAll(array('BuilderSupplyTypesList.builder_account_id' => $BuilderAccount_old_id));
                 $builder_supply_types_list = array();
                 if (is_array($this->request->data['BuilderAccount']['builder_supply_type_id']) && !empty($this->request->data['BuilderAccount']['builder_supply_type_id'])) {
                     foreach ($this->request->data['BuilderAccount']['builder_supply_type_id'] as $value) {
                         $builder_supply_types_list[] = array('inventory_lookup_id' => $value, 'builder_account_id' => $this->Customer->BuilderAccount->id);
                     }
                     $bst = new BuilderSupplyTypesList();
                     $bst->saveAll($builder_supply_types_list);
                 }
                 $this->redirect(array('action' => DETAIL, $this->Customer->id));
             }
         } else {
             $this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
         }
     } else {
         $this->request->data = $this->Customer->find('first', array('conditions' => array('Customer.customer_type_id' => array('2', '3'), 'Customer.id' => $id)));
         $sales_data = $this->request->data['Customer']['sales_representatives'];
         App::uses("BuilderSupplyTypesList", "CustomerManager.Model");
         $builder_supply_types_list = new BuilderSupplyTypesList();
         $builder_supply_types = $builder_supply_types_list->find("all", array("conditions" => array("BuilderSupplyTypesList.builder_account_id" => $this->request->data['BuilderAccount']['id'])));
     }
     $customerTypes = $this->Customer->CustomerType->find('list');
     $this->set(compact('customerTypes', 'id', 'section', 'sales_data', 'builder_supply_types'));
 }
 function getCustomer()
 {
     $this->autoRender = false;
     $sql = "SELECT * FROM customer_old;";
     $datas = $this->Cabinet->query($sql);
     App::import("Model", "CustomerManager.Customer");
     App::import("Model", "CustomerManager.BuilderAccount");
     App::import("Model", "CustomerManager.CustomerAddress");
     foreach ($datas as $data) {
         $Customer_model = new Customer();
         $BuilderAccount_model = new BuilderAccount();
         $CustomerAddress_model = new CustomerAddress();
         $cus_data['Customer']['last_name'] = $data['customer_old']['CustomerName'];
         $cus_data['Customer']['website'] = $data['customer_old']['WebSite'];
         if ($data['customer_old']['Inactive'] == 0) {
             $cus_data['Customer']['status'] = 1;
         } else {
             $cus_data['Customer']['status'] = 0;
         }
         $cus_data['Customer']['address'] = $data['customer_old']['loc_Address1'];
         $cus_data['Customer']['city'] = $data['customer_old']['loc_City'];
         $cus_data['Customer']['province'] = $data['customer_old']['loc_Province'];
         $cus_data['Customer']['postal_code'] = $data['customer_old']['loc_PostalCode'];
         $cus_data['Customer']['country'] = $data['customer_old']['loc_Country'];
         $cus_data['Customer']['phone'] = $data['customer_old']['loc_Phone'];
         $cus_data['Customer']['fax_number'] = $data['customer_old']['loc_FaxNumber'];
         $cus_data['Customer']['customer_type_id'] = 2;
         $cus_data['Customer']['site_address_exists'] = 1;
         $sales_person = array($this->loginUser['id']);
         $cus_data['Customer']['sales_representatives'] = serialize($sales_person);
         $Customer_model->save($cus_data['Customer']);
         App::uses("CustomerSalesRepresentetives", "CustomerManager.Model");
         $s = new CustomerSalesRepresentetives();
         $sales_data['customer_id'] = $Customer_model->id;
         $sales_data['user_id'] = $this->loginUser['id'];
         $s->save($sales_data);
         $cus_data['BuilderAccount']['customer_id'] = $Customer_model->id;
         $cus_data['BuilderAccount']['builder_legal_name'] = $data['customer_old']['CustomerName'];
         $cus_data['BuilderAccount']['credit_terms'] = $data['customer_old']['CreditTerms'];
         $cus_data['BuilderAccount']['credit_limit'] = $data['customer_old']['CreditLimit'];
         $cus_data['BuilderAccount']['invoice_on_day'] = $data['customer_old']['StartDate'];
         $cus_data['BuilderAccount']['quotes_validity'] = 30;
         $BuilderAccount_model->save($cus_data['BuilderAccount']);
         $cus_data['CustomerAddress']['customer_id'] = $Customer_model->id;
         $cus_data['CustomerAddress']['first_name'] = $data['customer_old']['con_ContactName'];
         $cus_data['CustomerAddress']['title'] = $data['customer_old']['con_Title'];
         $cus_data['CustomerAddress']['phone'] = $data['customer_old']['con_WorkPhone'];
         $cus_data['CustomerAddress']['fax_number'] = $data['customer_old']['con_FAX'];
         $cus_data['CustomerAddress']['cell'] = $data['customer_old']['con_MobilePhone'];
         $cus_data['CustomerAddress']['address'] = $data['customer_old']['con_Address1'];
         $cus_data['CustomerAddress']['city'] = $data['customer_old']['con_City'];
         $cus_data['CustomerAddress']['province'] = $data['customer_old']['con_Province'];
         $cus_data['CustomerAddress']['postal_code'] = $data['customer_old']['con_PostalCode'];
         $cus_data['CustomerAddress']['country'] = $data['customer_old']['con_Country'];
         $cus_data['CustomerAddress']['email'] = $data['customer_old']['con_eMail_Address'];
         $CustomerAddress_model->save($cus_data['CustomerAddress']);
     }
 }
 public function print_detail($id = null)
 {
     $this->layoutOpt['layout'] = 'report';
     $this->Customer->id = $id;
     if (!$this->Customer->exists()) {
         throw new NotFoundException(__('Invalid item'));
     }
     $customer = $this->Customer->find("first", array("conditions" => array("Customer.id" => $id)));
     App::import('Model', 'CustomerManager.BuilderAccount');
     $this->BuilderAccount = new BuilderAccount();
     $builder_account_info = $this->BuilderAccount->find('all', array('conditions' => array('BuilderAccount.customer_id' => $id)));
     $reportTitle = "Customer Information Sheet";
     $reportDate = date('l, F d, Y');
     App::uses("CustomerSalesRepresentetives", "CustomerManager.Model");
     $sales = new CustomerSalesRepresentetives();
     $sales_representatives = $sales->find("all", array("conditions" => array("CustomerSalesRepresentetives.customer_id" => $id)));
     $this->set(compact('customer', '$builder_account_info', 'reportTitle', 'reportDate', 'sales_representatives'));
 }