public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     if (!self::isExecutiveOfficer()) {
         $this->render('permission_denied.php');
         return;
     }
     if ($id = phpgw::get_var('id', 'int')) {
         $price_item = rental_price_item::get($id);
     } else {
         $title = phpgw::get_var('price_item_title');
         $responsibility_id = phpgw::get_var('responsibility_id');
         $price_item = new rental_price_item();
         $price_item->set_title($title);
         $price_item->set_responsibility_id($responsibility_id);
         $price_item->set_price_type_id(1);
         // defaults to year
     }
     // Save the price item if it was posted
     if (isset($_POST['save'])) {
         $price_item->set_title(phpgw::get_var('title'));
         $price_item->set_agresso_id(phpgw::get_var('agresso_id'));
         $price_item->set_is_area(phpgw::get_var('is_area') == 'true' ? true : false);
         $price_item->set_is_inactive(phpgw::get_var('is_inactive') == 'on' ? true : false);
         $price_item->set_is_adjustable(phpgw::get_var('is_adjustable') == 'on' ? true : false);
         $price_item->set_standard(phpgw::get_var('standard') == 'on' ? true : false);
         $price_item->set_price(phpgw::get_var('price'));
         $price_item->set_price_type_id(phpgw::get_var('price_type_id', 'int'));
         if ($price_item->get_agresso_id() == null) {
             return $this->viewedit(true, $price_item, '', lang('missing_agresso_id'));
         } else {
             if (rental_soprice_item::get_instance()->store($price_item)) {
                 return $this->viewedit(true, $price_item, lang('messages_saved_form'));
             } else {
                 return $this->viewedit(true, $price_item, '', lang('messages_form_error'));
             }
         }
     }
     return $this->viewedit(true, $price_item);
 }
 protected function populate(int $price_item_id, &$price_item)
 {
     if ($price_item == null) {
         $price_item = new rental_price_item($this->unmarshal($this->db->f('id'), 'int'));
         $price_item->set_title($this->unmarshal($this->db->f('title'), 'string'));
         $price_item->set_agresso_id($this->unmarshal($this->db->f('agresso_id'), 'string'));
         $price_item->set_is_area($this->unmarshal($this->db->f('is_area'), 'bool'));
         $price_item->set_is_inactive($this->unmarshal($this->db->f('is_inactive'), 'bool'));
         $price_item->set_is_adjustable($this->unmarshal($this->db->f('is_adjustable'), 'bool'));
         $price_item->set_standard($this->unmarshal($this->db->f('standard'), 'bool'));
         $price_item->set_price($this->unmarshal($this->db->f('price'), 'float'));
         $price_item->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id', true), 'int'));
         $price_item->set_responsibility_title($this->unmarshal($this->db->f('resp_title', true), 'string'));
         $price_type_id = (int) $this->db->f('type');
         $price_item->set_price_type_id($price_type_id);
         $price_item->set_price_type_title($price_type_id);
     }
     return $price_item;
 }
 public function adjust_contracts($adjustments)
 {
     /*
      * gather all adjustable contracts with 
      * 		interval = adjustment interval and this year = last adjusted + interval
      * 		or
      * 		last adjusted is null / 0 (use contract start year)
      * adjust each contract's price items according to adjustment info (percent, adjustment_percent)
      * Run as transaction
      * update adjustment -> set is_executed to true
      * update price book elements according to type if interval=1
      */
     $current_year = (int) date('Y');
     //var_dump("innicontr");
     foreach ($adjustments as $adjustment) {
         //gather all adjustable contracts
         $adjustable_contracts = "SELECT id, adjustment_share, date_start, adjustment_year FROM rental_contract ";
         $adjustable_contracts .= "WHERE location_id = '{$adjustment->get_responsibility_id()}' AND adjustable ";
         $adjustable_contracts .= "AND adjustment_interval = {$adjustment->get_interval()} ";
         $adjustable_contracts .= "AND (((adjustment_year + {$adjustment->get_interval()}) <= {$adjustment->get_year()})";
         $adjustable_contracts .= " OR ";
         $adjustable_contracts .= "(";
         if ($adjustment->is_extra_adjustment()) {
             $adjustable_contracts .= "adjustment_year = {$adjustment->get_year()}";
             $adjustable_contracts .= " OR ";
         }
         $adjustable_contracts .= "(adjustment_year IS NULL OR adjustment_year = 0)";
         $adjustable_contracts .= "))";
         //var_dump($adjustable_contracts);
         //die();
         $result = $this->db->query($adjustable_contracts);
         while ($this->db->next_record()) {
             $contract_id = $this->unmarshal($this->db->f('id', true), 'int');
             $adjustment_share = $this->unmarshal($this->db->f('adjustment_share', true), 'int');
             $date_start = $this->unmarshal($this->db->f('date_start', true), 'int');
             $adj_year = $this->unmarshal($this->db->f('adjustment_year', true), 'int');
             $start_year = date('Y', $date_start);
             $contract = rental_socontract::get_instance()->get_single($contract_id);
             $firstJanAdjYear = mktime(0, 0, 0, 1, 1, $adjustment->get_year());
             if ($contract->is_active($firstJanAdjYear) && ($adj_year != null && $adj_year > 0 || ($adj_year == null || $adj_year == 0) && $start_year + $adjustment->get_interval() <= $adjustment->get_year())) {
                 //update adjustment_year on contract
                 rental_socontract::get_instance()->update_adjustment_year($contract_id, $adjustment->get_year());
                 //gather price items to be adjusted
                 $contract_price_items = rental_socontract_price_item::get_instance()->get(null, null, null, null, null, null, array('contract_id' => $contract_id));
                 foreach ($contract_price_items as $cpi) {
                     //update price according to adjustment info
                     $cpi_old_price = $cpi->get_price();
                     $cpi_adjustment = $cpi_old_price * ($adjustment->get_percent() / 100) * ($adjustment_share / 100);
                     $cpi_new_price = $cpi_old_price + $cpi_adjustment;
                     $cpi->set_price($cpi_new_price);
                     rental_socontract_price_item::get_instance()->store($cpi);
                 }
             }
         }
         //TODO: update price book
         if ($adjustment->get_interval() == 1) {
             $adjustable_price_items = "SELECT * FROM rental_price_item WHERE responsibility_id = {$adjustment->get_responsibility_id()} AND is_adjustable";
             $result = $this->db->query($adjustable_price_items);
             $price_items = array();
             while ($this->db->next_record()) {
                 $price_item = new rental_price_item($this->unmarshal($this->db->f('id'), 'int'));
                 $price_item->set_title($this->unmarshal($this->db->f('title'), 'string'));
                 $price_item->set_agresso_id($this->unmarshal($this->db->f('agresso_id'), 'string'));
                 $price_item->set_is_area($this->unmarshal($this->db->f('is_area'), 'bool'));
                 $price_item->set_is_inactive($this->unmarshal($this->db->f('is_inactive'), 'bool'));
                 $price_item->set_is_adjustable($this->unmarshal($this->db->f('is_adjustable'), 'bool'));
                 $price_item->set_price($this->unmarshal($this->db->f('price'), 'float'));
                 $price_item->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id', true), 'int'));
                 $price_item->set_responsibility_title($this->unmarshal($this->db->f('resp_title', true), 'string'));
                 $price_items[] = $price_item;
             }
             foreach ($price_items as $pi) {
                 $pi_old_price = $pi->get_price();
                 $pi_adjustment = $pi_old_price * ($adjustment->get_percent() / 100);
                 $pi_new_price = $pi_old_price + $pi_adjustment;
                 $pi->set_price($pi_new_price);
                 rental_soprice_item::get_instance()->store($pi);
             }
         }
         $adjustment->set_is_executed(true);
         $this->update($adjustment);
         //notify all users with write access on the field of responsibility
         $location_id = $adjustment->get_responsibility_id();
         if ($location_id) {
             $location_names = $GLOBALS['phpgw']->locations->get_name($location_id);
             if ($location_names['appname'] == $GLOBALS['phpgw_info']['flags']['currentapp']) {
                 $responsible_accounts = $GLOBALS['phpgw']->acl->get_user_list_right(PHPGW_ACL_EDIT, $location_names['location']);
                 foreach ($responsible_accounts as $ra) {
                     $account_ids[] = $ra['account_id'];
                 }
             }
         }
         $location_label = rental_socontract::get_instance()->get_responsibility_title($location_id);
         $adj_interval = $adjustment->get_interval();
         $day = date("Y-m-d", strtotime('now'));
         $ts_today = strtotime($day);
         //notify each unique account
         foreach ($account_ids as $account_id) {
             if ($account_id && $account_id > 0) {
                 $notification = new rental_notification(0, $account_id, 0, null, $ts_today, $location_label . '_' . $adj_interval, null, null, null, null);
                 rental_soworkbench_notification::get_instance()->store($notification);
             }
         }
     }
     return true;
 }