public function _new()
 {
     parent::_new();
     $ledgercategory = $this->_uses[$this->modeltype];
     if (!$ledgercategory->isLoaded()) {
         $current = $ledgercategory->getAll();
         $categories = new ContactCategory();
         $cc = new ConstraintChain();
         if (count($current) > 0) {
             $cc->add(new Constraint('id', 'not in', '(' . implode(',', $current) . ')'));
         }
         $this->view->set('categories', $categories->getAll($cc));
     }
 }
Beispiel #2
0
 function loadAccounts($data, &$errors = array(), &$warnings = array())
 {
     $system = system::Instance();
     $db = DB::Instance();
     $flash = Flash::Instance();
     if ($this->abort_action == 'A') {
         // Set encompassing transaction if need to abort all on error
         $db->StartTrans();
     }
     $data_in = $this->convertData($data);
     if (is_array($data_in) && count($data_in) > 0) {
         foreach ($data_in as $line) {
             $model_data = array();
             foreach ($line as $model => $fields) {
                 switch ($model) {
                     case 'Company':
                         $model_data['Party']['type'] = $model;
                         if (empty($fields['accountnumber'])) {
                             $model = 'Lead';
                             unset($fields['accountnumber']);
                         }
                         $company_model = $model;
                         $model_data[$model] = $fields;
                         $model_data[$model]['is_lead'] = empty($fields['accountnumber']) ? true : false;
                         if (!empty($model_data[$model]['name'])) {
                             $account = new $company_model();
                             $account->loadBy('name', $model_data[$model]['name']);
                             if ($account->isLoaded()) {
                                 $model_data[$model][$account->idField] = $account->{$account->idField};
                                 $model_data['Party']['id'] = $account->party_id;
                             }
                         }
                         break;
                     case 'CompanyAddress':
                         $model_data['Address'] = $fields;
                         $model_data['PartyAddress']['main'] = true;
                         // Check for existing entry
                         $address = new CompanyAddress();
                         $address->loadBy(array('street1', 'street2', 'street3', 'town', 'county', 'postcode'), array($fields['street1'], $fields['street2'], $fields['street3'], $fields['town'], $fields['county'], $fields['postcode']));
                         if ($address->isLoaded()) {
                             $model_data['PartyAddress']['address_id'] = $model_data['Address'][$address->idField] = $address->{$address->idField};
                         }
                         break;
                     case 'ContactMethod':
                         foreach ($fields as $type => $value) {
                             if (!empty($value)) {
                                 $model_data[$type]['ContactMethod']['contact'] = $value;
                                 $model_data[$type]['PartyContactMethod']['main'] = true;
                                 switch ($type) {
                                     case 'phone':
                                         $model_data[$type]['PartyContactMethod']['type'] = 'T';
                                         break;
                                     case 'email':
                                         $model_data[$type]['PartyContactMethod']['type'] = 'E';
                                         break;
                                     case 'fax':
                                         $model_data[$type]['PartyContactMethod']['type'] = 'F';
                                         break;
                                     case 'mobile':
                                         $model_data[$type]['PartyContactMethod']['type'] = 'M';
                                         break;
                                     default:
                                         $model_data[$type]['PartyContactMethod']['type'] = '';
                                 }
                                 // Check for existing entry
                                 $contactmethod = new ContactMethod();
                                 $contactmethod->loadBy('contact', $value);
                                 if ($contactmethod->isLoaded()) {
                                     $model_data[$type]['PartyContactMethod']['contactmethod_id'] = $model_data[$type]['ContactMethod'][$contactmethod->idField] = $contactmethod->{$contactmethod->idField};
                                 }
                             }
                         }
                         break;
                 }
             }
             if (!$system->controller->save_model($company_model, $model_data, $errors, $warnings, $this->duplicates_action)) {
                 if ($this->abort_action != 'S') {
                     // Abort on Error
                     if ($this->abort_action == 'A') {
                         // Rollback all imported records
                         $db->FailTrans();
                         $db->CompleteTrans();
                     }
                     // If not abort_all_on_error,
                     // then imported records up to this point will be saved
                     return false;
                 }
                 // Skip error record and continue, so reset errors array
                 $warnings[] = 'Ignoring Company ' . $model_data[$model]['name'] . ' ' . $db->ErrorMsg();
                 $warnings = array_merge($warnings, $errors);
                 $errors = array();
                 $flash->clear();
             }
             $company = $system->controller->getSavedModel($company_model);
             if (isset($accounts_data['CompanyInCategories'])) {
                 foreach ($accounts_data['CompanyInCategories'] as $type => $value) {
                     $companycategories['company_id'] = $company->id;
                     if (!empty($value)) {
                         // look up ContactCategory for this type
                         $category = new ContactCategory();
                         $categories = $category->getCategoriesByName(ucfirst($type));
                         if (count($categories) == 1) {
                             $companycategories['category_id'] = key($categories);
                             $account_errors = array();
                             $companyincategory = DataObject::Factory($companycategories, $account_errors, 'CompanyInCategories');
                             if (count($account_errors) > 0 || !$companyincategory || !$companyincategory->save()) {
                                 if ($this->abort_action != 'S') {
                                     // Abort on Error
                                     if ($this->abort_action == 'A') {
                                         // Rollback all imported records
                                         $errors = array_merge_recursive($errors, $account_errors);
                                         $errors[] = 'Failed to save Company Category ' . $db->ErrorMsg();
                                         $db->FailTrans();
                                         $db->CompleteTrans();
                                     }
                                     // If not abort_all_on_error,
                                     // then imported records up to this point will be saved
                                     return false;
                                 }
                                 // Skip error record and continue, so reset errors array
                                 $warnings[] = 'Ignoring Company Category for ' . $company->name . ' ' . $db->ErrorMsg();
                                 $warnings = array_merge($warnings, $account_errors);
                                 $flash->clear();
                             }
                         }
                     }
                 }
             }
             $system->controller->clearSavedModels();
         }
     }
     if ($this->abort_action == 'A') {
         // Everything is OK here, just need to complete transaction
         // if mode is set to abort all on error
         $db->CompleteTrans();
     }
     return array('internal_id' => null, 'internal_identifier_field' => '', 'internal_identifier_value' => '');
 }