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;
 }
 /**
  * Create a new contract tied to the composite provided in the composite_id parameter
  */
 public function add_from_composite()
 {
     $contract = new rental_contract();
     $contract->set_location_id(phpgw::get_var('responsibility_id'));
     if ($contract->has_permission(PHPGW_ACL_EDIT)) {
         $so_contract = rental_socontract::get_instance();
         $db_contract = $so_contract->get_db();
         $db_contract->transaction_begin();
         if ($so_contract->store($contract)) {
             // Add that composite to the new contract
             $success = $so_contract->add_composite($contract->get_id(), phpgw::get_var('id'));
             if ($success) {
                 $db_contract->transaction_commit();
                 $comp_name = rental_socomposite::get_instance()->get_single(phpgw::get_var('id'))->get_name();
                 $message = lang('messages_new_contract_from_composite') . ' ' . $comp_name;
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => $message));
             } else {
                 $db_contract->transaction_abort();
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => lang('messages_form_error')));
             }
         } else {
             $db_contract->transaction_abort();
             $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => lang('messages_form_error')));
         }
     }
     // If no executive officer
     $this->render('permission_denied.php', array('error' => lang('permission_denied_new_contract')));
 }
 /**
  * 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;
 }
 /**
  * Create a new contract tied to the composite provided in the composite_id parameter
  */
 public function add_from_composite()
 {
     $contract = new rental_contract();
     $contract->set_location_id(phpgw::get_var('responsibility_id'));
     $contract->set_account_in(rental_socontract::get_instance()->get_default_account($contract->get_location_id(), true));
     $contract->set_account_out(rental_socontract::get_instance()->get_default_account($contract->get_location_id(), false));
     $contract->set_executive_officer_id($GLOBALS['phpgw_info']['user']['account_id']);
     $config = CreateObject('phpgwapi.config', 'rental');
     $config->read();
     $default_billing_term = $config->config_data['default_billing_term'];
     $contract->set_term_id($default_billing_term);
     $units = rental_socomposite::get_instance()->get_single(phpgw::get_var('id'))->get_units();
     $location_code = $units[0]->get_location()->get_location_code();
     $args = array('acl_location' => '.contract', 'location_code' => $location_code, 'contract' => &$contract);
     $hook_helper = CreateObject('rental.hook_helper');
     $hook_helper->add_contract_from_composite($args);
     //		_debug_array($contract); die();
     if ($contract->has_permission(PHPGW_ACL_EDIT)) {
         $so_contract = rental_socontract::get_instance();
         $db_contract = $so_contract->get_db();
         $db_contract->transaction_begin();
         if ($so_contract->store($contract)) {
             // Add standard price items to contract
             if ($contract->get_location_id() && ($this->isExecutiveOfficer() || $this->isAdministrator())) {
                 $so_price_item = rental_soprice_item::get_instance();
                 //get default price items for location_id
                 $default_price_items = $so_contract->get_default_price_items($contract->get_location_id());
                 foreach ($default_price_items as $price_item_id) {
                     $so_price_item->add_price_item($contract->get_id(), $price_item_id);
                 }
             }
             // Add that composite to the new contract
             $success = $so_contract->add_composite($contract->get_id(), phpgw::get_var('id'));
             if ($success) {
                 $db_contract->transaction_commit();
                 $comp_name = rental_socomposite::get_instance()->get_single(phpgw::get_var('id'))->get_name();
                 $message = lang('messages_new_contract_from_composite') . ' ' . $comp_name;
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => $message));
             } else {
                 $db_contract->transaction_abort();
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => lang('messages_form_error')));
             }
         } else {
             $db_contract->transaction_abort();
             $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => lang('messages_form_error')));
         }
     }
     // If no executive officer
     $this->render('permission_denied.php', array('error' => lang('permission_denied_new_contract')));
 }