/**
  * Get a static reference to the storage object associated with this model object
  * 
  * @return the storage object
  */
 public static function get_instance()
 {
     if (self::$so == null) {
         self::$so = CreateObject('rental.soadjustment');
     }
     return self::$so;
 }
 public function run_adjustments()
 {
     rental_soadjustment::get_instance()->run_adjustments();
 }
 protected function import_adjustments($contracts, $regulation_id_location_id)
 {
     $start_time = time();
     $soadjustment = rental_soadjustment::get_instance();
     $datalines = $this->getcsvdata($this->path . "/u_Regulering.csv");
     $this->messages[] = "Read 'u_Regulering.csv' file in " . (time() - $start_time) . " seconds";
     $this->messages[] = "'u_Regulering.csv' contained " . count($datalines) . " lines";
     foreach ($datalines as $data) {
         if (count($data) <= 8) {
             continue;
         }
         $adjustment = new rental_adjustment();
         $regulation_id = $this->decode($data[0]);
         //nReguleringId
         $loc_id = $regulation_id_location_id[$regulation_id];
         if (isset($loc_id) && $loc_id != '') {
             $adjustment->set_responsibility_id($loc_id);
         } else {
             $this->messages[] = "This adjustment '" . $regulation_id . "' could not be linked to this responsibility area.  Date ({$this->decode($data[1])}), Text ({$this->decode($data[4])}), Interval ({$this->decode($data[2])})";
             continue;
         }
         $date_array = explode(".", $this->decode($data[1]));
         //dAktuellDato
         if (count($date_array) == 3) {
             $y = $date_array[2];
             $m = $date_array[1];
             $d = $date_array[0];
             $date = strtotime($y . "-" . $m . "-" . $d);
         }
         $adjustment->set_adjustment_date($date);
         $description_array = explode(" ", $this->decode($data[4]));
         //cBeskrivelse
         $number = end($description_array);
         $percent = substr($number, 0, strlen($number) - 2);
         $percent = str_replace(',', '.', $percent);
         $adjustment->set_percent($percent);
         $adjustment->set_interval($this->decode($data[2]));
         $adjustment->set_new_price(0);
         $adjustment->set_price_item_id(0);
         if (!$soadjustment->adjustment_exist($adjustment)) {
             // All is good, store notification
             if ($soadjustment->store($adjustment)) {
                 $this->messages[] = "Successfully imported adjustment: Date ({$this->decode($data[1])}), Percent ({$adjustment->get_percent()}), Interval ({$adjustment->get_interval()})";
             } else {
                 $this->errors[] = "Error importing adjustment: Date ({$this->decode($data[1])}), Percent ({$adjustment->get_percent()}), Interval ({$adjustment->get_interval()})";
             }
         } else {
             $this->messages[] = "Adjustment already exist: Date ({$this->decode($data[1])}), Percent ({$adjustment->get_percent()}), Interval ({$adjustment->get_interval()})";
         }
     }
     $this->messages[] = "Imported adjustments. (" . (time() - $start_time) . " seconds)";
     return true;
 }
 public function adjust_price()
 {
     if (!self::isExecutiveOfficer()) {
         $this->render('permission_denied.php');
         return;
     }
     $id = (int) phpgw::get_var('price_item_id');
     $new_price = phpgw::get_var('new_price');
     $new_price = str_replace(',', '.', $new_price);
     if ($new_price != null && is_numeric($new_price)) {
         $price_item = rental_price_item::get($id);
         $price_item->set_price($new_price);
         if (rental_soprice_item::get_instance()->store($price_item)) {
             $adjustment = new rental_adjustment();
             $adjustment->set_price_item_id($price_item->get_id());
             $adjustment->set_new_price($new_price);
             $adjustment->set_year(date('Y'));
             $adjustment->set_percent(0);
             $adjustment->set_interval(0);
             $adjustment->set_responsibility_id($price_item->get_responsibility_id());
             $adjustment->set_is_manual(true);
             $adjustment->set_adjustment_date(time());
             rental_soadjustment::get_instance()->store($adjustment);
             $message[] = "Priselement med Agresso id {$price_item->get_agresso_id()} er oppdatert med ny pris {$new_price}";
             //update affected contract_price_items
             $no_of_contracts_updated = rental_soprice_item::get_instance()->adjust_contract_price_items($id, $new_price);
             if ($no_of_contracts_updated > 0) {
                 $message[] = $no_of_contracts_updated . ' priselementer p&aring; kontrakter er oppdatert';
             } else {
                 $message[] = "Ingen kontrakter er oppdatert";
             }
             $data = array('price_item_id' => $id, 'message' => $message);
             self::set_active_menu('rental::contracts::price_item_list::manual_adjustment');
             $this->render('admin_price_item_manual_adjustment.php', $data);
         } else {
             $data = array('price_item_id' => $id, 'error' => $error);
             self::set_active_menu('rental::contracts::price_item_list::manual_adjustment');
             $this->render('admin_price_item_manual_adjustment.php', $data);
         }
     } else {
         $data = array('price_item_id' => $id, 'error' => lang('price_not_numeric'));
         self::set_active_menu('rental::contracts::price_item_list::manual_adjustment');
         $this->render('admin_price_item_manual_adjustment.php', $data);
     }
 }
 public function query()
 {
     if ($GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'] > 0) {
         $user_rows_per_page = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
     } else {
         $user_rows_per_page = 10;
     }
     // YUI variables for paging and sorting
     $start_index = phpgw::get_var('startIndex', 'int');
     $num_of_objects = phpgw::get_var('results', 'int', 'GET', $user_rows_per_page);
     $sort_field = phpgw::get_var('sort');
     $sort_ascending = phpgw::get_var('dir') == 'desc' ? false : true;
     // Form variables
     $search_for = phpgw::get_var('query');
     $search_type = phpgw::get_var('search_option');
     // Create an empty result set
     $result_objects = array();
     $result_count = 0;
     $exp_param = phpgw::get_var('export');
     $export = false;
     if (isset($exp_param)) {
         $export = true;
         $num_of_objects = null;
     }
     $type = phpgw::get_var('type');
     switch ($type) {
         case 'contracts_for_adjustment':
             $adjustment_id = (int) phpgw::get_var('id');
             $adjustment = rental_soadjustment::get_instance()->get_single($adjustment_id);
             $filters = array('contract_type' => $adjustment->get_responsibility_id(), 'adjustment_interval' => $adjustment->get_interval(), 'adjustment_year' => $adjustment->get_year(), 'adjustment_is_executed' => $adjustment->is_executed());
             break;
         case 'contracts_part':
             // Contracts for this party
             $filters = array('party_id' => phpgw::get_var('party_id'), 'contract_status' => phpgw::get_var('contract_status'), 'contract_type' => phpgw::get_var('contract_type'), 'status_date_hidden' => phpgw::get_var('status_date_hidden'));
             break;
         case 'contracts_for_executive_officer':
             // Contracts for this executive officer
             $filters = array('executive_officer' => $GLOBALS['phpgw_info']['user']['account_id']);
             break;
         case 'ending_contracts':
         case 'ended_contracts':
         case 'last_edited':
         case 'closing_due_date':
         case 'terminated_contracts':
             // Queries that depend on areas of responsibility
             $types = rental_socontract::get_instance()->get_fields_of_responsibility();
             $ids = array();
             $read_access = array();
             foreach ($types as $id => $label) {
                 $names = $this->locations->get_name($id);
                 if ($names['appname'] == $GLOBALS['phpgw_info']['flags']['currentapp']) {
                     if ($this->hasPermissionOn($names['location'], PHPGW_ACL_ADD)) {
                         $ids[] = $id;
                     } else {
                         $read_access[] = $id;
                     }
                 }
             }
             if (count($ids) > 0) {
                 $comma_seperated_ids = implode(',', $ids);
             } else {
                 $comma_seperated_ids = implode(',', $read_access);
             }
             switch ($type) {
                 case 'ending_contracts':
                     // Contracts that are about to end in areas of responsibility
                     $filters = array('contract_status' => 'under_dismissal', 'contract_type' => $comma_seperated_ids);
                     break;
                 case 'ended_contracts':
                     // Contracts that are ended in areas of responsibility
                     $filters = array('contract_status' => 'ended', 'contract_type' => $comma_seperated_ids);
                     break;
                 case 'last_edited':
                     // Contracts that are last edited in areas of resposibility
                     $filters = array('contract_type' => $comma_seperated_ids);
                     $sort_field = 'contract.last_updated';
                     $sort_ascending = false;
                     break;
                 case 'closing_due_date':
                     //Contracts closing due date in areas of responsibility
                     $filters = array('contract_status' => 'closing_due_date', 'contract_type' => $comma_seperated_ids);
                     break;
                 case 'terminated_contracts':
                     $filters = array('contract_status' => 'terminated_contracts', 'contract_type' => $comma_seperated_ids);
                     break;
             }
             break;
         case 'contracts_for_composite':
             // ... all contracts this composite is involved in, filters (status and date)
             $filters = array('composite_id' => phpgw::get_var('composite_id'), 'contract_status' => phpgw::get_var('contract_status'), 'contract_type' => phpgw::get_var('contract_type'), 'status_date_hidden' => phpgw::get_var('date_status_hidden'));
             break;
         case 'get_contract_warnings':
             //get the contract warnings
             $contract = rental_socontract::get_instance()->get_single(phpgw::get_var('contract_id'));
             $contract->check_consistency();
             $rows = $contract->get_consistency_warnings();
             $result_count = count($rows);
             $export = true;
             break;
         case 'all_contracts':
         default:
             phpgwapi_cache::session_set('rental', 'contract_query', $search_for);
             phpgwapi_cache::session_set('rental', 'contract_search_type', $search_type);
             phpgwapi_cache::session_set('rental', 'contract_status', phpgw::get_var('contract_status'));
             phpgwapi_cache::session_set('rental', 'contract_status_date', phpgw::get_var('date_status'));
             phpgwapi_cache::session_set('rental', 'contract_type', phpgw::get_var('contract_type'));
             $filters = array('contract_status' => phpgw::get_var('contract_status'), 'contract_type' => phpgw::get_var('contract_type'), 'status_date_hidden' => phpgw::get_var('date_status_hidden'));
     }
     if ($type != 'get_contract_warnings') {
         $result_objects = rental_socontract::get_instance()->get($start_index, $num_of_objects, $sort_field, $sort_ascending, $search_for, $search_type, $filters);
         $result_count = rental_socontract::get_instance()->get_count($search_for, $search_type, $filters);
         //Serialize the contracts found
         $rows = array();
         foreach ($result_objects as $result) {
             if (isset($result)) {
                 $rows[] = $result->serialize();
             }
         }
         //var_dump("Usage " .memory_get_usage() . " bytes after serializing");
     }
     if (!$export) {
         //Add context menu columns (actions and labels)
         array_walk($rows, array($this, 'add_actions'), array($type, $ids, $adjustment_id));
     }
     //var_dump("Usage " .memory_get_usage() . " bytes after menu");
     //Build a YUI result from the data
     $result_data = array('results' => $rows, 'total_records' => $result_count);
     return $this->yui_results($result_data, 'total_records', 'results');
 }