function populate(int $contract_id, &$contract)
 {
     if ($contract == null) {
         $contract_id = (int) $contract_id;
         $contract = new rental_contract($contract_id);
         $contract->set_contract_date(new rental_contract_date($this->unmarshal($this->db->f('date_start'), 'int'), $this->unmarshal($this->db->f('date_end'), 'int')));
         $contract->set_billing_start_date($this->unmarshal($this->db->f('billing_start'), 'int'));
         $contract->set_billing_end_date($this->unmarshal($this->db->f('billing_end'), 'int'));
         $contract->set_old_contract_id($this->unmarshal($this->db->f('old_contract_id'), 'string'));
         $contract->set_contract_type_title($this->unmarshal($this->db->f('title'), 'string'));
         $contract->set_comment($this->unmarshal($this->db->f('comment'), 'string'));
         $contract->set_last_edited_by_current_user($this->unmarshal($this->db->f('edited_on'), 'int'));
         $contract->set_location_id($this->unmarshal($this->db->f('location_id'), 'int'));
         $contract->set_last_updated($this->unmarshal($this->db->f('last_updated'), 'int'));
         $contract->set_service_id($this->unmarshal($this->db->f('service_id'), 'string'));
         $contract->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id'), 'string'));
         $contract->set_reference($this->unmarshal($this->db->f('reference'), 'string'));
         $contract->set_invoice_header($this->unmarshal($this->db->f('invoice_header'), 'string'));
         $contract->set_account_in($this->unmarshal($this->db->f('account_in'), 'string'));
         $contract->set_account_out($this->unmarshal($this->db->f('account_out'), 'string'));
         $contract->set_project_id($this->unmarshal($this->db->f('project_id'), 'string'));
         $contract->set_executive_officer_id($this->unmarshal($this->db->f('executive_officer'), 'int'));
         $contract->set_term_id($this->unmarshal($this->db->f('term_id'), 'int'));
         $contract->set_term_id_title($this->unmarshal($this->db->f('term_title'), 'string'));
         $contract->set_security_type($this->unmarshal($this->db->f('security_type'), 'int'));
         $contract->set_security_amount($this->unmarshal($this->db->f('security_amount'), 'string'));
         $contract->set_due_date($this->unmarshal($this->db->f('due_date'), 'int'));
         $contract->set_contract_type_id($this->unmarshal($this->db->f('contract_type_id'), int));
         $contract->set_rented_area($this->unmarshal($this->db->f('rented_area'), 'float'));
         $contract->set_adjustable($this->unmarshal($this->db->f('adjustable'), 'bool'));
         $contract->set_adjustment_interval($this->unmarshal($this->db->f('adjustment_interval'), 'int'));
         $contract->set_adjustment_share($this->unmarshal($this->db->f('adjustment_share'), 'int'));
         $contract->set_adjustment_year($this->unmarshal($this->db->f('adjustment_year'), 'int'));
         $contract->set_publish_comment($this->unmarshal($this->db->f('publish_comment'), 'bool'));
         $contract->set_notify_before($this->unmarshal($this->db->f('notify_before'), 'int'));
         $contract->set_notify_before_due_date($this->unmarshal($this->db->f('notify_before_due_date'), 'int'));
         $contract->set_notify_after_termination_date($this->unmarshal($this->db->f('notify_after_termination_date'), 'int'));
     }
     $timestamp_end = $this->unmarshal($this->db->f('timestamp_end'), 'int');
     $billing_deleted = $this->unmarshal($this->db->f('deleted'), 'bool');
     if ($timestamp_end && !$billing_deleted) {
         $contract->add_bill_timestamp($timestamp_end);
     }
     $total_price = $this->unmarshal($this->db->f('total_price'), 'int');
     if ($total_price) {
         $contract->set_total_price($total_price);
     }
     $party_id = $this->unmarshal($this->db->f('party_id', true), 'int');
     if ($party_id) {
         $party = new rental_party($party_id);
         $party->set_first_name($this->unmarshal($this->db->f('first_name', true), 'string'));
         $party->set_last_name($this->unmarshal($this->db->f('last_name', true), 'string'));
         $party->set_company_name($this->unmarshal($this->db->f('company_name', true), 'string'));
         $party->set_department($this->unmarshal($this->db->f('department', true), 'string'));
         $party->set_org_enhet_id($this->unmarshal($this->db->f('org_enhet_id'), 'int'));
         $is_payer = $this->unmarshal($this->db->f('is_payer', true), 'bool');
         if ($is_payer) {
             $contract->set_payer_id($party_id);
         }
         $contract->add_party($party);
     }
     $composite_id = $this->unmarshal($this->db->f('composite_id', true), 'int');
     if ($composite_id) {
         $composite = new rental_composite($composite_id);
         $composite->set_name($this->unmarshal($this->db->f('composite_name', true), 'string'));
         $contract->add_composite($composite);
     }
     return $contract;
 }
 /**
  * Public method. Called when user wants to edit a composite.
  * @param HTTP::id	the composite ID
  */
 public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     // Get the composite ID
     $composite_id = (int) phpgw::get_var('id');
     // Retrieve the party object or create a new one if correct permissions
     if ($this->isExecutiveOfficer() || $this->isAdministrator()) {
         if (isset($composite_id) && $composite_id > 0) {
             $composite = rental_socomposite::get_instance()->get_single($composite_id);
         } else {
             $composite = new rental_composite();
         }
     } else {
         $this->render('permission_denied.php', array('error' => lang('permission_denied_edit')));
     }
     if (isset($_POST['save_composite'])) {
         if (isset($composite)) {
             $composite->set_name(phpgw::get_var('name'));
             $composite->set_custom_address_1(phpgw::get_var('address_1'));
             $composite->set_has_custom_address(phpgw::get_var('has_custom_address') == 'on' ? true : false);
             $composite->set_custom_house_number(phpgw::get_var('house_number'));
             $composite->set_custom_address_2(phpgw::get_var('address_2'));
             $composite->set_custom_postcode(phpgw::get_var('postcode'));
             $composite->set_custom_place(phpgw::get_var('place'));
             $composite->set_is_active(phpgw::get_var('is_active') == 'on' ? true : false);
             $composite->set_description(phpgw::get_var('description'));
             $composite->set_furnish_type_id(phpgw::get_var('furnish_type_id'));
             $composite->set_standard_id(phpgw::get_var('composite_standard_id', 'int'));
             if (rental_socomposite::get_instance()->store($composite)) {
                 $message = lang('messages_saved_form');
             } else {
                 $error = lang('messages_form_error');
             }
         }
     }
     return $this->render('composite.php', array('composite' => $composite, 'editable' => true, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error'), 'cancel_link' => self::link(array('menuaction' => 'rental.uicomposite.index', 'populate_form' => 'yes'))));
 }
 protected function import_composites()
 {
     $start_time = time();
     // Storage objects
     $socomposite = rental_socomposite::get_instance();
     $socontract = rental_socontract::get_instance();
     $sounit = rental_sounit::get_instance();
     // Array for mapping the composite ids to the facilit ids
     $composites = array();
     //Read source data
     $datalines = $this->getcsvdata($this->path . "/u_Leieobjekt.csv");
     $this->messages[] = "Read 'u_Leieobjekt.csv' file in " . (time() - $start_time) . " seconds";
     $this->messages[] = "'u_Leieobjekt.csv' contained " . count($datalines) . " lines";
     foreach ($datalines as $data) {
         if (count($data) <= 34) {
             continue;
         }
         //If the composite differs in terms of object number the custom address should be set (default false)
         $set_custom_address = false;
         //Retrieve the title for the responsibility area we are importing (to hande the respoonsibility areas differently)
         $title = $socontract->get_responsibility_title($this->location_id);
         // Variable for the location code (objektnummer)
         $loc1 = null;
         //Three columns for detemining the correct object number
         $object_identifier = trim($this->decode($data[1]));
         //cLeieobjektnr
         $property_identifier = trim($this->decode($data[4]));
         //cInstNr
         $building_identifier = trim($this->decode($data[5]));
         //cByggNr
         if ($title == 'contract_type_internleie') {
             $property_ok = false;
             //Priority 1: The property identifier (most up to date)
             if (isset($property_identifier)) {
                 $correct_length_property = strlen($property_identifier) == 4 ? true : false;
                 $integer_value_property = (int) $property_identifier > 0 ? true : false;
                 if ($correct_length_property && $integer_value_property) {
                     $loc1 = $property_identifier;
                     $property_ok = true;
                 }
             }
             //Priority 2: Use the object identifier
             if (isset($object_identifier)) {
                 $correct_length = strlen($object_identifier) == 6 ? true : false;
                 $integer_value = (int) $object_identifier > 0 ? true : false;
                 if ($correct_length && $integer_value) {
                     if ($property_ok) {
                         // ... add only the building number if the property number is ok
                         $loc1 = $loc1 . "-" . substr($object_identifier, 4, 2);
                     } else {
                         // ... just use the object identifier if not
                         $loc1 = substr_replace($object_identifier, "-", 4, 0);
                     }
                 } else {
                     // Using non-conforming object identifier. Gives a warning.
                     $loc1 = $object_identifier;
                     $set_custom_address = true;
                     $this->warnings[] = "Composite (internal contract) has wrong object-number ({$loc1}). Should consist of 6 numbers. Setting custom address.";
                 }
             } else {
                 if ($property_ok) {
                     //If no object number, only property number
                     $set_custom_address = true;
                     $this->warnings[] = "Composite (internal contract) has no object-number ({$object_identifier}). Using property identifier. Setting custom address.";
                 }
             }
             if (!isset($loc1)) {
                 // No data exist to determine the object number
                 $this->warnings[] = "No data exist to determine the object number. Setting custom address.";
                 $set_custom_address = true;
             }
         } else {
             if ($title == 'contract_type_eksternleie') {
                 // Two forms for object number (xxxx.xxxx) AND (xxxx.xxxxxx.xxxx)
                 $parts = explode('.', $object_identifier);
                 for ($i = 0; $i < count($parts); $i++) {
                     $parts[$i] = trim($parts[$i]);
                 }
                 if (count($parts) == 2) {
                     //Checking parts for correct length
                     $correct_length1 = strlen($parts[0]) == 4 ? true : false;
                     $correct_length2 = strlen($parts[1]) == 4 ? true : false;
                     if ($correct_length1 && $correct_length2) {
                         //If the first part contains any characters from the alphabet
                         if (!is_numeric($parts[0])) {
                             // ... relace the punctuation with an '-'
                             $loc1 = $parts[0] . "-" . $parts[1];
                         }
                     }
                 } else {
                     if (count($parts) == 3) {
                         $correct_length = strlen($parts[1]) == 6 ? true : false;
                         $correct_length_property = strlen($property_identifier) == 4 ? true : false;
                         if ($correct_length && is_numeric($parts[1])) {
                             if (isset($property_identifier) && $correct_length_property) {
                                 // ... add only the building number if the property number is ok
                                 $loc1 = $property_identifier . "-" . substr($parts[1], 4, 2);
                             } else {
                                 // ... insert a '-' at position 4 if not
                                 $loc1 = substr_replace($parts[1], "-", 4, 0);
                             }
                         }
                     }
                 }
                 // If the object identifier is non-conforming
                 // Alernative 1: Try to use the buiding identifier
                 if (!isset($loc1) && isset($building_identifier)) {
                     $correct_length = strlen($building_identifier) == 6 ? true : false;
                     if ($correct_length && is_numeric($building_identifier)) {
                         $loc1 = substr_replace($building_identifier, "-", 4, 0);
                         $set_custom_address = true;
                         $this->warnings[] = "Composite (external) lacks conforming object number ({$object_identifier}). Using building identifier ({$loc1}). Setting custom address.";
                     }
                 }
                 // Alternative 2: Try to use the property identifier
                 if (!isset($loc1) && isset($property_identifier)) {
                     $correct_length = strlen($property_identifier) == 4 ? true : false;
                     if ($correct_length) {
                         //Give a warning
                         $loc1 = $property_identifier;
                         $set_custom_address = true;
                         $this->warnings[] = "Composite (external) lacks conforming object number ({$object_identifier}). Using property identifier ({$loc1}). Setting custom address.";
                     }
                 }
                 // Alternative 3: Use the non-conforming object number
                 if (!isset($loc1)) {
                     $loc1 = $object_identifier;
                     $set_custom_address = true;
                     $this->warnings[] = "Composite (external) lacks data to create an object number. Using non-conforming object number ({$loc1}) Setting custom address.";
                 }
             } else {
                 if ($title == 'contract_type_innleie') {
                     $correct_length = strlen($building_identifier) == 6 ? true : false;
                     $integer_value = (int) $building_identifier > 0 ? true : false;
                     $correct_length_property = strlen($property_identifier) == 4 ? true : false;
                     if ($correct_length && $integer_value) {
                         if (isset($property_identifier) && $correct_length_property) {
                             // ... add only the building number if the property number is ok
                             $loc1 = $property_identifier . "-" . substr($building_identifier, 4, 2);
                         } else {
                             $loc1 = substr_replace($building_identifier, "-", 4, 0);
                         }
                     } else {
                         if (isset($property_identifier) && $correct_length_property) {
                             // ... add only the building number if the property number is ok
                             $loc1 = $property_identifier;
                             $set_custom_address = true;
                             $this->warnings[] = "Composite (innleie) has non-conforming building identifier ({$building_identifier}). Using property identifier instead ({$loc1}). Setting custom address.";
                         }
                     }
                     if (!isset($loc1)) {
                         $loc1 = $object_identifier;
                         $set_custom_address = true;
                         $this->warnings[] = "Composite (innleie) lacks building identifier/property identifier ({$building_identifier}/{$property_identifier}). Using object identifier instead ({$loc1}). Setting custom address.";
                     }
                 } else {
                     $this->errors[] = "The type of import ({$title}) is invalid";
                 }
             }
         }
         $composite = new rental_composite();
         // Use the first address line as name if no name
         $name = $this->decode($data[26]);
         //cLeieobjektnavn
         $address1 = $this->decode($data[6]);
         //cAdresse1
         if (!isset($name)) {
             $name = $address1;
         }
         if ($set_custom_address) {
             // Set address
             $composite->set_custom_address_1($address1);
             $composite->set_custom_address_2($this->decode($data[7]));
             $composite->set_custom_postcode($this->decode($data[8]));
             $composite->set_has_custom_address(true);
         }
         $composite->set_name($name);
         $composite->set_description($this->decode($data[3]));
         //cLeieobjektBeskrivelse
         $composite->set_object_type_id($this->decode($data[25]));
         //nLeieobjektTypeId
         $composite->set_area($this->decode($data[2]));
         //nMengde
         $composite->set_is_active($data[19] == "-1");
         //bTilgjengelig
         // Store composite
         if ($socomposite->store($composite)) {
             // Add composite to collection of composite so we can refer to it later.
             $composites[$data[0]] = $composite->get_id();
             // Add units only if composite stored ok.
             $res = $sounit->store(new rental_unit(null, $composite->get_id(), new rental_property_location($loc1, null)));
             $this->messages[] = "Successfully added composite " . $composite->get_name() . " (" . $composite->get_id() . ")";
             if ($res) {
                 $this->messages[] = "Successfully added unit " . $loc1 . " to composite (" . $composite->get_id() . ")";
             }
         } else {
             $this->errors[] = "Failed to store composite " . $composite->get_name();
         }
     }
     $this->messages[] = "Successfully imported " . count($composites) . " composites (" . (time() - $start_time) . " seconds)";
     return $composites;
 }
 function populate(int $composite_id, &$composite)
 {
     if ($composite == null) {
         $composite = new rental_composite($composite_id);
         $composite->set_description($this->unmarshal($this->db->f('description', true), 'string'));
         $composite->set_is_active($this->db->f('is_active'));
         $composite_name = $this->unmarshal($this->db->f('name', true), 'string');
         if ($composite_name == null || $composite_name == '') {
             $composite_name = lang('no_name_composite', $composite_id);
         }
         $composite->set_name($composite_name);
         $composite->set_has_custom_address($this->unmarshal($this->db->f('has_custom_address', true), 'bool'));
         $composite->set_custom_address_1($this->unmarshal($this->db->f('address_1', true), 'string'));
         $composite->set_custom_address_2($this->unmarshal($this->db->f('address_2', true), 'string'));
         $composite->set_custom_house_number($this->unmarshal($this->db->f('house_number', true), 'string'));
         $composite->set_custom_postcode($this->unmarshal($this->db->f('postcode', true), 'string'));
         $composite->set_custom_place($this->unmarshal($this->db->f('place', true), 'string'));
         $composite->set_area($this->unmarshal($this->db->f('area', true), 'float'));
         $composite->set_furnish_type_id($this->unmarshal($this->db->f('furnish_type_id'), 'int'));
         $composite->set_standard_id($this->unmarshal($this->db->f('standard_id'), 'int'));
     }
     // Location code
     $location_code = $this->unmarshal($this->db->f('location_code', true), 'string');
     //Status
     $database_status = $this->unmarshal($this->db->f('status', true), 'string');
     $composite_status = $composite->get_status();
     if ($composite_status != 'Ikke ledig') {
         $composite->set_status($database_status);
     }
     $contract_id = $this->unmarshal($this->db->f('contract_id', true), 'int');
     // Adds contract to array in composite object if it's not already added
     if ($contract_id != 0 & !$composite->contains_contract($contract_id)) {
         $contract = new rental_contract($contract_id);
         $start_date = $this->unmarshal($this->db->f('date_start', true), 'int');
         $end_date = $this->unmarshal($this->db->f('date_end', true), 'int');
         $old_contract_id = $this->unmarshal($this->db->f('old_contract_id', true), 'string');
         // Adds contract if end date is not specified or greater than todays date
         if ($end_date == 0 || $end_date > time()) {
             $contract_date = new rental_contract_date($start_date, $end_date);
             $contract->set_contract_date($contract_date);
             $contract->set_old_contract_id($old_contract_id);
             $composite->add_contract($contract);
         }
     }
     if (!$composite->contains_unit($location_code)) {
         //composite inneholder ikke unit -> legg den til
         $location = null;
         try {
             // We get the data from the property module
             $data = @execMethod('property.bolocation.read_single', array('location_code' => $location_code, 'extra' => array('view' => true)));
             if ($data != null) {
                 $level = -1;
                 $names = array();
                 $levelFound = false;
                 for ($i = 1; $i < 6; $i++) {
                     $loc_name = 'loc' . $i . '_name';
                     if (array_key_exists($loc_name, $data)) {
                         $level = $i;
                         $names[$level] = $data[$loc_name];
                     }
                 }
                 $gab_id = '';
                 $gabinfos = @execMethod('property.sogab.read', array('location_code' => $location_code, 'allrows' => true));
                 if ($gabinfos != null && is_array($gabinfos) && count($gabinfos) == 1) {
                     $gabinfo = array_shift($gabinfos);
                     $gab_id = $gabinfo['gab_id'];
                 }
                 $location = new rental_property_location($location_code, rental_uicommon::get_nicely_formatted_gab_id($gab_id), $level, $names);
                 if (isset($data['street_name']) && $data['street_name']) {
                     $location->set_address_1($data['street_name'] . ' ' . $data['street_number']);
                 }
                 //$location->set_address_1($data['address']);
                 foreach ($data['attributes'] as $attributes) {
                     switch ($attributes['column_name']) {
                         case 'area_gross':
                             $location->set_area_gros($attributes['value']);
                             break;
                         case 'area_net':
                             $location->set_area_net($attributes['value']);
                             break;
                     }
                 }
             } else {
                 $location = new rental_property_location($location_code, null, 1, array());
             }
         } catch (Exception $e) {
             $location = new rental_property_location($location_code, null, 1, array());
         }
         $composite->add_unit(new rental_unit($this->unmarshal($this->db->f('unit_id', true), 'int'), $composite_id, $location));
     }
     return $composite;
 }