/**
  * Get a static reference to the storage object associated with this model object
  * 
  * @return rental_sonotification the storage object
  */
 public static function get_instance()
 {
     if (self::$so == null) {
         self::$so = CreateObject('rental.sonotification');
     }
     return self::$so;
 }
 /**
  * Visible controller function for deleting a contract notification.
  * 
  * @return true on success/false otherwise
  */
 public function delete_notification()
 {
     $notification_id = (int) phpgw::get_var('id');
     $contract_id = (int) phpgw::get_var('contract_id');
     $contract = rental_socontract::get_instance()->get_single($contract_id);
     if ($contract->has_permission(PHPGW_ACL_EDIT)) {
         rental_sonotification::get_instance()->delete_notification($notification_id);
         return true;
     }
     return false;
 }
 protected function import_adjustment_information()
 {
     $start_time = time();
     $sonotification = rental_sonotification::get_instance();
     $socontract = rental_socontract::get_instance();
     $datalines = $this->getcsvdata($this->path . "/u_Hendelse.csv");
     $this->messages[] = "Read 'u_Hendelse.csv' file in " . (time() - $start_time) . " seconds";
     $this->messages[] = "'u_Hendelse.csv' contained " . count($datalines) . " lines";
     foreach ($datalines as $data) {
         $type_id = $data[2];
         $date_array = explode(".", $this->decode($data[7]));
         if (count($date_array) == 3) {
             $y = $date_array[2];
             $m = $date_array[1];
             $d = $date_array[0];
             $date = strtotime($y . "-" . $m . "-" . $d);
         }
         //Which contract the event is linked to
         $contract_id = $this->decode($data[1]);
         if (!isset($contract_id) || $contract_id <= 0) {
             //This event is not bound to a contract that is part of the currently importing respensibiliry area
             continue;
         }
         $location_id = $this->location_id;
         // Add event description to title
         $title = $this->decode($data[3]);
         if (!$this->is_null($data[4])) {
             $title .= " " . $this->decode($data[4]);
         }
         if ($type_id == '1') {
             //price adjustment
             $adjusted = $this->decode($data[8]);
             if ($adjusted == 0 || $adjusted == '0') {
                 $current_year = date('Y');
                 $date_tmp = explode(".", $this->decode($data[7]));
                 if (count($date_tmp) == 3) {
                     $year = $date_tmp[2];
                     $interval = $this->decode($data[6]);
                     $last_adjusted_year = $year - $interval;
                 } else {
                     $last_adjusted_year = 0;
                 }
                 if ($last_adjusted_year <= $current_year) {
                     //update last adjusted on contract.
                     if ($contract_id > 0 && $last_adjusted_year > 0) {
                         //$this->messages[] = "Should set year {$last_adjusted_year} and interval {$interval} on contract {$contract_id}";
                         $result = $socontract->update_adjustment_year_interval($contract_id, $last_adjusted_year, $interval);
                         if ($result) {
                             $this->messages[] = "Successfully imported regulation. Set last regulation year '" . $last_adjusted_year . "' for contract {$contract_id} with interval '{$interval}'";
                         } else {
                             $this->errors[] = "Error importing regulation. Tried to set last regulation year '" . $last_adjusted_year . "' for contract {$contract_id} with interval '{$interval}'";
                         }
                     }
                 } else {
                     $this->warnings[] = "Skipping adjustment on contract ({$contract_id}) because the contract's last adjusted year ({$last_adjusted_year}) is after current year '{$current_year}'.";
                 }
             }
         }
     }
     //loop through events once more to update previous adjustments
     foreach ($datalines as $data) {
         $type_id = $data[2];
         if ($type_id == 1 || $type_id == '1') {
             //price adjustment
             $adjusted = $this->decode($data[8]);
             if ($adjusted == -1 || $adjusted == '-1') {
                 $current_year = date('Y');
                 $date_tmp = explode(".", $this->decode($data[7]));
                 if (count($date_tmp) == 3) {
                     $year = $date_tmp[2];
                 } else {
                     $year = 0;
                 }
                 //update last adjusted and interval on contract.
                 if ($year <= $current_year) {
                     $contract_id = $this->decode($data[1]);
                     if (isset($contract_id) && $contract_id > 0 && $year > 0) {
                         $result = $socontract->update_adjustment_year($contract_id, $year);
                         if ($result) {
                             $this->messages[] = "Successfully updated regulation information. Set last regulation year '" . $year . "' for contract {$contract_id}";
                         } else {
                             $this->errors[] = "Error updating regulation information. Tried to set last regulation year '" . $last_adjusted_year . "' for contract {$contract_id} with interval '{$interval}'";
                         }
                     }
                 } else {
                     $this->warnings[] = "Skipping adjustment-year update on contract {$contract_id} because last adjusted year is after {$current_year}.";
                 }
             }
         }
     }
 }
 /**
  * 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;
     $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);
             if (!$contract->has_permission(PHPGW_ACL_EDIT)) {
                 unset($contract);
                 $this->render('permission_denied.php', array('error' => lang('permission_denied_edit_contract')));
             }
         } else {
             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 = strtotime(phpgw::get_var('date_start_hidden'));
         $date_end = strtotime(phpgw::get_var('date_end_hidden'));
         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(strtotime(phpgw::get_var('billing_start_date_hidden')));
             $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(strtotime(phpgw::get_var('due_date_hidden')));
             $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_hidden');
                 if ($date) {
                     $date = strtotime($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);
 }