Пример #1
0
 protected function import_parties()
 {
     $start_time = time();
     $soparty = rental_soparty::get_instance();
     $parties = array();
     //Check to see if there is any parties in the database. If so, do not store these
     //... double checking to  ensure that the user has not logged out and in again during import
     $alreay_imported_parties = false;
     $number_of_parties = $soparty->get_number_of_parties();
     if ($number_of_parties > 0) {
         return;
     }
     $datalines = $this->getcsvdata($this->path . "/u_PersonForetak.csv", true);
     $this->messages[] = "Read 'u_PersonForetak.csv' file in " . (time() - $start_time) . " seconds";
     $this->messages[] = "'u_PersonForetak.csv' contained " . count($datalines) . " lines";
     $counter = 1;
     // Loop through each line of the file, parsing CSV data to a php array
     foreach ($datalines as $data) {
         if (count($data) <= 30) {
             continue;
         }
         // Create a new rental party we can fill with info from this line from the file
         $party = new rental_party();
         $identifier = $this->decode($data[24]);
         //cPersonForetaknr
         //Removed whitespace characters
         $identifier = str_replace(' ', '', '' . $identifier);
         // Default information
         $party->set_address_1($this->decode($data[3]));
         //cAdresse1
         $party->set_address_2($this->decode($data[4]));
         //cAdresse2
         $party->set_postal_code($this->decode($data[5]));
         //cPostnr
         $party->set_mobile_phone($this->decode($data[7]));
         //cMobil
         $party->set_phone($this->decode($data[8]));
         //cTelefon
         $party->set_fax($this->decode($data[9]));
         //cTelefaks
         $party->set_title($this->decode($data[12]));
         //cArbeidstittel
         $party->set_email($this->decode($data[25]));
         //cEpost
         $party->set_company_name($this->decode($data[10]));
         //cArbeidsgiver
         $party->set_department($this->decode($data[11]));
         //cAvdeling
         $party->set_account_number($this->decode($data[14]));
         //cBankkontonr
         $party->set_reskontro($this->decode($data[23]));
         //cReskontronr
         $party->set_comment($this->decode($data[26]));
         //cMerknad
         // Insert contract person in comment if present
         $contact_person = $this->decode($data[6]);
         if (isset($contact_person)) {
             $party->set_comment($party->get_comment() . "\n\nKontaktperson: " . $contact_person);
             //cKontaktPerson
         }
         $valid_identifier = false;
         switch (strlen('' . $identifier)) {
             case 4:
                 // Intern organisasjonstilknytning
                 //Should be four number or on the form 'KFxx'
                 if (is_numeric($identifier) || substr($identifier, 0, 2) == 'KF' && is_numeric(substr($identifier, 2, 2))) {
                     $party->set_company_name($this->decode($data[2]));
                     //cForetaksnavn
                     $party->set_first_name(null);
                     $party->set_last_name(null);
                     // Get location ID
                     $locations = $GLOBALS['phpgw']->locations;
                     $subs = $locations->get_subs_from_pattern('rental', '.ORG.BK.__.' . $identifier);
                     //cPersonForetaknr
                     if (count($subs) > 0) {
                         $party->set_location_id($subs[0]['location_id']);
                     } else {
                         $this->warnings[] = "Party with valid identifier ({$identifier}) not found in internal organisation tree. Company name({$party->get_company_name()})";
                     }
                     $valid_identifier = true;
                 }
                 break;
             case 5:
                 //Internal, Should be a result unit on the form 'Rxxxx'
                 if (substr($identifier, 0, 1) == 'R' && is_numeric(substr($identifier, 1, 4))) {
                     $identifier = substr($identifier, 1, 4);
                     $party->set_company_name($this->decode($data[2]));
                     //cForetaksnavn
                     $party->set_first_name(null);
                     $party->set_last_name(null);
                     // Get location ID
                     $locations = $GLOBALS['phpgw']->locations;
                     $subs = $locations->get_subs_from_pattern('rental', '.ORG.BK.__.' . $identifier);
                     //cPersonForetaknr
                     if (count($subs) > 0) {
                         $party->set_location_id($subs[0]['location_id']);
                     } else {
                         $this->warnings[] = "Party with valid identifier ({$identifier}- original R{$identifier}) not found in internal organisation tree. Company name({$party->get_company_name()})";
                     }
                     $valid_identifier = true;
                 }
                 break;
             case 6:
                 // Foretak (agresso-id)
             // Foretak (agresso-id)
             case 9:
                 // Foretak (org.nr)
                 if (is_numeric($identifier)) {
                     $party->set_company_name($this->decode($data[2]));
                     //cForetaksnavn
                     $party->set_first_name(null);
                     $party->set_last_name(null);
                     $valid_identifier = true;
                 }
                 break;
             case 11:
                 // Personnr
                 if (is_numeric($identifier)) {
                     if (!$this->is_null($data[0])) {
                         $party->set_first_name($this->decode($data[0]));
                         //cFornavn
                         $party->set_last_name($this->decode($data[1]));
                         //cEtternavn
                     } else {
                         $company_name = explode(' ', $this->decode($data[2]), 2);
                         //cForetaksnavn
                         $party->set_first_name($company_name[0]);
                         //cFornavn
                         $party->set_last_name($company_name[1]);
                         //cEtternavn
                     }
                     $valid_identifier = true;
                 }
                 break;
         }
         if (!$valid_identifier) {
             $party->set_first_name($this->decode($data[0]));
             //cFornavn
             $party->set_last_name($this->decode($data[1]));
             //cEtternavn
             $party->set_company_name($this->decode($data[2]));
             //cForetaksnavn
             $party->set_is_inactive(true);
             $this->warnings[] = "Party with unknown 'cPersonForetaknr' format ({$identifier}). First name ({$party->get_first_name()}). Last name({$party->get_last_name()}). Company name({$party->get_company_name()}) Setting as inactive.";
         }
         // Fødselsnr/Foretaksnr/AgressoID
         $party->set_identifier($identifier);
         // Store party and log message
         if ($soparty->store($party)) {
             // Add party to collection of parties keyed by its facilit ID so we can refer to it later.
             $facilit_id = $data[17];
             //nPersonForetakId
             $parties[$facilit_id] = $party->get_id();
             $this->messages[] = "Successfully added party " . $party->get_name() . " (" . $party->get_id() . ")";
         } else {
             $this->errors[] = "Failed to store party " . $party->get_name();
         }
     }
     $this->messages[] = "Successfully imported " . count($parties) . " contract parties. (" . (time() - $start_time) . " seconds).";
     //Clean up
     //unset();
     return $parties;
 }