/** * Step 4: import the contracts from the file 'u_Kontrakt.csv' * @param $composites array mapping facilit ids and protico ids for composites * @param $rentalobject_to_contract array mapping composites and contracts * @param $parties array mapping party ids * @param $default_values the default accounts and project numbers * @return array of contracts */ protected function import_contracts($composites, $rentalobject_to_contract, $parties, $default_values) { $start_time = time(); $socontract = rental_socontract::get_instance(); $contracts = array(); $datalines = $this->getcsvdata($this->path . "/u_Kontrakt.csv"); $this->messages[] = "Read 'u_Kontrakt.csv' file in " . (time() - $start_time) . " seconds"; $this->messages[] = "'u_Kontrakt.csv' contained " . count($datalines) . " lines"; // Old->new ID mapping $contract_types = array(2 => 2, 3 => 1, 4 => 8, 5 => 4, 12 => 6, 13 => 7, 14 => 8, 15 => 3, 17 => NULL, 18 => 8, 19 => 8); foreach ($datalines as $data) { // Skip this contract if its data is incomplete if (count($data) <= 27) { continue; } // Create a new contract object $contract = new rental_contract(); //Set the contract dates $date_start = is_numeric(strtotime($this->decode($data[3]))) ? strtotime($this->decode($data[3])) : null; $date_end = is_numeric(strtotime($this->decode($data[4]))) ? strtotime($this->decode($data[4])) : null; $contract->set_contract_date(new rental_contract_date($date_start, $date_end)); // Set the old contract identifier $contract->set_old_contract_id($this->decode($data[5])); //cKontraktnr // Set the contract biling term $term = $data[10]; //nTermin switch ($term) { case 1: // Monthly $contract->set_term_id(1); break; case 2: // Quarterly $contract->set_term_id(4); break; case 4: // Half-year $contract->set_term_id(3); break; case 5: // Yearly $contract->set_term_id(2); break; } // Report non-conforming price periods $price_period = $data[14]; //nPrisPeriode (4=month, 8=year) if ($price_period == 4) { // The price period is month. We ignore this but print a warning. $this->warnings[] = "Price period of contract " . $contract->get_old_contract_id() . " is month. Ignored."; //echo "<br/>Price period of contract " . $contract->get_old_contract_id() . " is month. Ignored."; } elseif ($price_period == 5) { // The price period is 5, which is unknown. We ignore this but print a warning. $this->warnings[] = "Price period of contract " . $contract->get_old_contract_id() . " is unknown (value: 5). Ignored."; //echo "<br/>Price period of contract " . $contract->get_old_contract_id() . " is unknown (value: 5). Ignored."; } $contract_status = $data[6]; if ($contract_status == 3) { // Report contracts under dismissal. Send warning if contract status is '3' (Under avslutning) $this->warnings[] = "Status of contract " . $contract->get_old_contract_id() . " is '" . lang('contract_under_dismissal') . "'"; } else { if ($contract_status == 1) { // Report contracts under plannning. Send warning if contract status is '1' (Under planlegging) $this->warnings[] = "Status of contract " . $contract->get_old_contract_id() . " is 'Under planlegging'"; } else { if ($contract_status == 2) { //Test: if the contract is running; is import date within the contract period if ($date_start != null && time() < $date_start) { $this->warnings[] = "Status of contract " . $contract->get_old_contract_id() . " is 'Løpende' but the start date is in the future."; } else { if ($date_end != null && time() > $date_end) { $this->warnings[] = "Status of contract " . $contract->get_old_contract_id() . " is 'Løpende' but the end date is in the past."; } } } else { if ($contract_status == 4) { if ($date_end == null || time() < $date_end) { $this->warnings[] = "Status of contract " . $contract->get_old_contract_id() . " is 'Avsluttet' but the end date not set or in the future."; } } } } } // Set the billing start date for the contract $billing_start_date = is_numeric(strtotime($this->decode($data[16]))) ? strtotime($this->decode($data[16])) : null; $contract->set_billing_start_date($billing_start_date); // Deres ref. $contract->set_invoice_header($this->decode($data[17])); //cFakturaRef $contract->set_comment($this->decode($data[18])); //cMerknad $contract->set_contract_type_id($contract_types[$this->decode($data[1])]); // // Set the location identifier (responsibiity area) $contract->set_location_id($this->location_id); // Get the composite identifier for the composite included in this contract $composite_id = $composites[$rentalobject_to_contract[$data[0]]]; // Retrieve the title for the responsibility area we are importing (to hande the respoonsibility areas differently) $title = $socontract->get_responsibility_title($this->location_id); // For external contract types the rented area resides on the composite ... if ($title == 'contract_type_eksternleie') { if ($composite_id) { $socomposite = rental_socomposite::get_instance(); $contract->set_rented_area($socomposite->get_area($composite_id)); } } else { if ($title == 'contract_type_innleie') { $rented_area_on_contract = $this->decode($data[21]); if (isset($rented_area_on_contract) && $rented_area_on_contract > 0) { $contract->set_rented_area($rented_area_on_contract); } else { if ($composite_id) { $socomposite = rental_socomposite::get_instance(); $contract->set_rented_area($socomposite->get_area($composite_id)); } } } else { // ... and for others contract types the rented area resides on the contract $contract->set_rented_area($this->decode($data[21])); } } // Retrieve default values for accounts and project numbers if ($title == 'contract_type_eksternleie') { $type_id = $contract->get_contract_type_id(); if (!in_array($type_id, array(6, 7, 8))) { $contract->set_contract_type_id(8); } } else { if ($title == 'contract_type_internleie') { //Set default account in/out and project numbers for internal contracts $contract->set_account_in($default_values['account_in']); $contract->set_account_out($default_values['account_out']); $contract->set_project_id($default_values['project_number']); // Ansvar/Tjenestested: F.eks: 080400.13000 $ansvar_tjeneste = $this->decode($data[26]); //cSikkerhetsTekst $ansvar_tjeneste_components = explode(".", $ansvar_tjeneste); if (count($ansvar_tjeneste_components) == 2) { $contract->set_responsibility_id($ansvar_tjeneste_components[0]); $contract->set_service_id($ansvar_tjeneste_components[1]); } else { $this->warnings[] = "The contract (internal) " . $contract->get_old_contract_id() . " lacks service and responsibility ids"; } } } // Store contract if ($socontract->store($contract)) { // Map contract ids in Facilit and PE contract id (should be the same) $contracts[$data[0]] = $contract->get_id(); // Check if this contract has a composite and if so add rental composite to contract if (!$this->is_null($rentalobject_to_contract[$data[0]]) && !$this->is_null($composite_id)) { $socontract->add_composite($contract->get_id(), $composite_id); } // Check if this contract has a contract part and if so add party to contract if (!$this->is_null($data[2])) { //nPersonForetakId $party_id = $parties[$this->decode($data[2])]; $socontract->add_party($contract->get_id(), $party_id); // Set this party to be the contract invoice recipient $socontract->set_payer($contract->get_id(), $party_id); } $this->messages[] = "Successfully added contract (" . $contract->get_id() . "/" . $contract->get_old_contract_id() . ")"; } else { $this->errors[] = "Failed to store contract " . $this->decode($data[5]); } } $this->messages[] = "Successfully imported " . count($contracts) . " contracts. (" . (time() - $start_time) . " seconds)"; return $contracts; }