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;
 }
 /**
  * Edit a contract
  */
 public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     $contract_id = (int) phpgw::get_var('id');
     $location_id = (int) phpgw::get_var('location_id');
     $update_price_items = false;
     phpgw::import_class('phpgwapi.jquery');
     phpgwapi_jquery::load_widget('core');
     self::add_javascript('rental', 'rental', 'contract.edit.js');
     $message = null;
     $error = null;
     $add_default_price_items = false;
     if (isset($_POST['save_contract'])) {
         if (isset($contract_id) && $contract_id > 0) {
             $contract = rental_socontract::get_instance()->get_single($contract_id);
             // Gets responsibility area from db (ex: eksternleie, internleie)
             $responsibility_area = rental_socontract::get_instance()->get_responsibility_title($contract->get_location_id());
             // Redirect with error message if responsibility area is eksternleie and contract type not set
             if (!is_numeric(phpgw::get_var('contract_type')) && strcmp($responsibility_area, "contract_type_eksternleie") == 0) {
                 $error = lang('billing_removed_external_contract');
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => $message, 'error' => $error));
             }
             if (!$contract->has_permission(PHPGW_ACL_EDIT)) {
                 unset($contract);
                 $this->render('permission_denied.php', array('error' => lang('permission_denied_edit_contract')));
             }
         } else {
             // Gets responsibility area from db (ex: eksternleie, internleie)
             $responsibility_area = rental_socontract::get_instance()->get_responsibility_title($location_id);
             // Redirect with error message if responsibility area is eksternleie and contract type not set
             if (!is_numeric(phpgw::get_var('contract_type')) && strcmp($responsibility_area, "contract_type_eksternleie") == 0) {
                 $error = lang('billing_removed_external_contract');
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'location_id' => $location_id, 'message' => $message, 'error' => $error));
             }
             if (isset($location_id) && ($this->isExecutiveOfficer() || $this->isAdministrator())) {
                 $contract = new rental_contract();
                 $fields = rental_socontract::get_instance()->get_fields_of_responsibility();
                 $contract->set_location_id($location_id);
                 $contract->set_contract_type_title($fields[$location_id]);
                 $add_default_price_items = true;
             }
         }
         $date_start = phpgwapi_datetime::date_to_timestamp(phpgw::get_var('date_start'));
         $date_end = phpgwapi_datetime::date_to_timestamp(phpgw::get_var('date_end'));
         if (isset($contract)) {
             $contract->set_contract_date(new rental_contract_date($date_start, $date_end));
             $contract->set_security_type(phpgw::get_var('security_type'));
             $contract->set_security_amount(phpgw::get_var('security_amount'));
             $contract->set_executive_officer_id(phpgw::get_var('executive_officer'));
             $contract->set_comment(phpgw::get_var('comment'));
             if (isset($location_id) && $location_id > 0) {
                 $contract->set_location_id($location_id);
                 // only present when new contract
             }
             $contract->set_term_id(phpgw::get_var('billing_term'));
             $contract->set_billing_start_date(phpgwapi_datetime::date_to_timestamp(phpgw::get_var('billing_start_date')));
             $contract->set_billing_end_date(phpgwapi_datetime::date_to_timestamp(phpgw::get_var('billing_end_date')));
             $contract->set_service_id(phpgw::get_var('service_id'));
             $contract->set_responsibility_id(phpgw::get_var('responsibility_id'));
             $contract->set_reference(phpgw::get_var('reference'));
             $contract->set_invoice_header(phpgw::get_var('invoice_header'));
             $contract->set_account_in(phpgw::get_var('account_in'));
             /*
             					if($contract->get_contract_type_id() != phpgw::get_var('contract_type'))
             					{
             						// New contract type id set, retrieve correct account out
             						$type_id = phpgw::get_var('contract_type');
             						if(isset($type_id) && $type_is != ''){
             							$account = rental_socontract::get_instance()->get_contract_type_account($type_id);
             							$contract->set_account_out($account);
             						}
             						else
             						{
             							$contract->set_account_out(phpgw::get_var('account_out'));
             						}
             					}
             					else
             					{*/
             $contract->set_account_out(phpgw::get_var('account_out'));
             //}
             $contract->set_project_id(phpgw::get_var('project_id'));
             $contract->set_due_date(phpgwapi_datetime::date_to_timestamp(phpgw::get_var('due_date')));
             $contract->set_contract_type_id(phpgw::get_var('contract_type'));
             $old_rented_area = $contract->get_rented_area();
             $new_rented_area = phpgw::get_var('rented_area');
             $new_rented_area = str_replace(',', '.', $new_rented_area);
             $validated_numeric = false;
             if (!isset($new_rented_area) || $new_rented_area == '') {
                 $new_rented_area = 0;
             }
             if ($old_rented_area != $new_rented_area) {
                 $update_price_items = true;
             }
             $contract->set_rented_area($new_rented_area);
             $contract->set_adjustment_interval(phpgw::get_var('adjustment_interval'));
             $contract->set_adjustment_share(phpgw::get_var('adjustment_share'));
             $contract->set_adjustable(phpgw::get_var('adjustable') == 'on' ? true : false);
             $contract->set_publish_comment(phpgw::get_var('publish_comment') == 'on' ? true : false);
             $validated_numeric = $contract->validate_numeric();
             if ($validated_numeric) {
                 $so_contract = rental_socontract::get_instance();
                 $db_contract = $so_contract->get_db();
                 $db_contract->transaction_begin();
                 if ($so_contract->store($contract)) {
                     if ($update_price_items) {
                         $success = $so_contract->update_price_items($contract->get_id(), $new_rented_area);
                         if ($success) {
                             $db_contract->transaction_commit();
                             $message = lang('messages_saved_form');
                             $contract_id = $contract->get_id();
                         } else {
                             $db_contract->transaction_abort();
                             $error = lang('messages_form_error');
                         }
                     } else {
                         if ($add_default_price_items) {
                             $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());
                             //add price_items to contract
                             foreach ($default_price_items as $price_item_id) {
                                 $so_price_item->add_price_item($contract->get_id(), $price_item_id);
                             }
                             $db_contract->transaction_commit();
                             $message = lang('messages_saved_form');
                             $contract_id = $contract->get_id();
                         } else {
                             $db_contract->transaction_commit();
                             $message = lang('messages_saved_form');
                             $contract_id = $contract->get_id();
                         }
                     }
                 } else {
                     $db_contract->transaction_abort();
                     $error = lang('messages_form_error');
                 }
             } else {
                 $error = $contract->get_validation_errors();
                 return $this->viewedit(true, $contract_id, $contract, $location_id, $notification, $message, $error);
             }
         }
         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'message' => $message, 'error' => $error));
     } else {
         if (isset($_POST['add_notification'])) {
             $contract = rental_socontract::get_instance()->get_single($contract_id);
             if ($contract->has_permission(PHPGW_ACL_EDIT)) {
                 $account_id = phpgw::get_var('notification_target');
                 $location_id = phpgw::get_var('notification_location');
                 $date = phpgw::get_var('date_notification');
                 if ($date) {
                     $date = phpgwapi_datetime::date_to_timestamp($date);
                 }
                 $notification = new rental_notification(-1, $account_id, $location_id, $contract_id, $date, phpgw::get_var('notification_message'), phpgw::get_var('notification_recurrence'));
                 if (rental_sonotification::get_instance()->store($notification)) {
                     $message = lang('messages_saved_form');
                     $notification = null;
                     // We don't want to display the date/message when it was sucessfully stored.
                 } else {
                     $error = lang('messages_form_error');
                 }
             } else {
                 $error = lang('permission_denied_edit_contract');
             }
         }
     }
     return $this->viewedit(true, $contract_id, null, $location_id, $notification, $message, $error);
 }