public function edit()
 {
     $adjustment_id = (int) phpgw::get_var('id');
     $responsibility_id = (int) phpgw::get_var('responsibility_id');
     $message = null;
     $error = null;
     if (isset($_POST['save'])) {
         if (isset($adjustment_id) && $adjustment_id > 0) {
             $adjustment = rental_soadjustment::get_instance()->get_single($adjustment_id);
             if (!$adjustment->has_permission(PHPGW_ACL_EDIT)) {
                 unset($adjustment);
                 $this->render('permission_denied.php', array('error' => lang('permission_denied_edit_adjustment')));
             }
         } else {
             if (isset($responsibility_id) && ($this->isExecutiveOfficer() || $this->isAdministrator())) {
                 $adjustment = new rental_adjustment();
                 $fields = rental_socontract::get_instance()->get_fields_of_responsibility();
                 $adjustment->set_responsibility_id($responsibility_id);
             }
         }
         $adjustment_date = strtotime(phpgw::get_var('adjustment_date_hidden'));
         if (isset($adjustment)) {
             $adjustment->set_year(phpgw::get_var('adjustment_year'));
             $adjustment->set_adjustment_date($adjustment_date);
             $adjustment->set_price_item_id(0);
             if (isset($responsibility_id) && $responsibility_id > 0) {
                 $adjustment->set_responsibility_id($responsibility_id);
                 // only present when new contract
             }
             $adjustment->set_new_price(0);
             $adjustment->set_percent(phpgw::get_var('percent'));
             $adjustment->set_interval(phpgw::get_var('interval'));
             $adjustment->set_adjustment_type(phpgw::get_var('adjustment_type'));
             $adjustment->set_extra_adjustment(phpgw::get_var('extra_adjustment') == 'on' ? true : false);
             $so_adjustment = rental_soadjustment::get_instance();
             if ($so_adjustment->store($adjustment)) {
                 $message = lang('messages_saved_form');
                 $adjustment_id = $adjustment->get_id();
             } else {
                 $error = lang('messages_form_error');
             }
         }
         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uiadjustment.edit', 'id' => $adjustment->get_id(), 'message' => $message, 'error' => $error));
     }
     return $this->viewedit(true, $adjustment_id, null, $responsibility_id, $message, $error);
 }
 public function run_adjustments()
 {
     /* check if there are incomplete adjustments (for today)
      * 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
      */
     $prev_day = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
     $next_day = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));
     //get incomplete adjustments for today
     $adjustments_query = "SELECT * FROM rental_adjustment WHERE NOT is_executed AND (adjustment_date < {$next_day} AND adjustment_date >= {$prev_day})";
     //var_dump($adjustments_query);
     $result = $this->db->query($adjustments_query);
     //var_dump("etter spr");
     //there are un-executed adjustments
     $adjustments = array();
     while ($this->db->next_record()) {
         $adjustment_id = $this->unmarshal($this->db->f('id', true), 'int');
         $adjustment = new rental_adjustment($adjustment_id);
         $adjustment->set_price_item_id($this->unmarshal($this->db->f('price_item_id', true), 'int'));
         $adjustment->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id', true), 'int'));
         $adjustment->set_new_price($this->unmarshal($this->db->f('new_price', true), 'float'));
         $adjustment->set_percent($this->unmarshal($this->db->f('percent', true), 'float'));
         $adjustment->set_interval($this->unmarshal($this->db->f('adjustment_interval', true), 'int'));
         $adjustment->set_adjustment_date($this->unmarshal($this->db->f('adjustment_date', true), 'int'));
         $adjustment->set_adjustment_type($this->unmarshal($this->db->f('adjustment_type'), 'string'));
         $adjustment->set_is_manual($this->unmarshal($this->db->f('is_manual'), 'bool'));
         $adjustment->set_is_executed($this->unmarshal($this->db->f('is_executed'), 'bool'));
         $adjustment->set_extra_adjustment($this->unmarshal($this->db->f('extra_adjustment'), 'bool'));
         $adjustment->set_year($this->unmarshal($this->db->f('year'), 'int'));
         $adjustments[] = $adjustment;
     }
     if (count($adjustments) > 0) {
         $this->db->transaction_begin();
         $success = $this->adjust_contracts($adjustments);
         if ($success) {
             $this->db->transaction_commit();
         } else {
             $this->db->transaction_abort();
         }
         return $success;
     }
     return false;
 }