コード例 #1
0
 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;
 }