コード例 #1
0
 /**
  * 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.soworkbench_notification');
     }
     return self::$so;
 }
コード例 #2
0
 /**
  * Visible controller function for dismissing all workbench notifications originated 
  * from a given notification. The user must have EDIT privileges on a contract for
  * this action.
  * 
  * @return true on success/false otherwise
  */
 public function dismiss_notification_for_all()
 {
     //the source 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_soworkbench_notification::get_instance()->dismiss_notification_for_all($notification_id);
         return true;
     }
     return false;
 }
コード例 #3
0
 /**
  * CRON
  * 
  * Populate workbench notifications. Traverses all notifications and populates PE users workbenches
  * based on date information and group membership. 
  * 	 
  * @param $today a string date, no parameter means today
  * @return unknown_type
  */
 public function populate_workbench_notifications($day = null)
 {
     // Select all notifications not marked as deleted
     $sql = "SELECT * FROM rental_notification WHERE deleted = false";
     $result = $this->db->query($sql);
     //Iterate through all notifications
     while ($this->db->next_record()) {
         $result_id = $this->unmarshal($this->db->f('id', true), 'int');
         // The id of object
         // Create notification object
         $notification = $this->populate($result_id, $notification);
         // Calculate timestamps the notification date, target date (default: today) and last notified
         $notification_date = date("Y-m-d", $notification->get_date());
         if (!$day) {
             $day = date("Y-m-d", strtotime('now'));
         }
         $ts_notification_date = strtotime($notification_date);
         $ts_today = strtotime($day);
         $ts_last_notified = $notification->get_last_notified();
         // Check whether today is a notification date
         $is_today_notification_date = false;
         if ($ts_today == $ts_notification_date) {
             $is_today_notification_date = true;
             // Delete the notification if it should not recur
             if ($notification->get_recurrence() == rental_notification::RECURRENCE_NEVER) {
                 $this->delete_notification($notification->get_id());
             }
         } else {
             // the original notification date is in the past
             // Find out if today is notification date based on recurrence
             $recurrence_interval = '';
             switch ($notification->get_recurrence()) {
                 case rental_notification::RECURRENCE_ANNUALLY:
                     $recurrence_interval = 'year';
                     break;
                 case rental_notification::RECURRENCE_MONTHLY:
                     $recurrence_interval = 'month';
                     break;
                 case rental_notification::RECURRENCE_WEEKLY:
                     $recurrence_interval = 'week';
                     break;
             }
             $ts_next_recurrence;
             for ($i = 1;; $i++) {
                 // next interval
                 $ts_next_recurrence = strtotime('+' . $i . ' ' . $recurrence_interval, $ts_notification_date);
                 if ($ts_next_recurrence > $ts_last_notified || $ts_last_notified == null) {
                     if ($ts_next_recurrence == $ts_today) {
                         $is_today_notification_date = true;
                         break;
                     }
                     break;
                     // not yet reached date for notification
                 }
             }
         }
         // If users should be notified today
         if ($is_today_notification_date) {
             //notify all users in a group or only the single user if target audience is not a group
             $account_id = $notification->get_account_id();
             $notification_id = $notification->get_id();
             $account_ids = array();
             if ($account_id) {
                 if ($GLOBALS['phpgw']->accounts->get_type($account_id) == phpgwapi_account::TYPE_GROUP) {
                     $accounts = $GLOBALS['phpgw']->accounts->get_members($account_id);
                     $account_ids = array_merge($account_ids, $accounts);
                     //users in a group
                 } else {
                     $account_ids[] = $account_id;
                     // specific user
                 }
             }
             //notify all users with write access on the field of responsibility
             $location_id = $notification->get_location_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'];
                     }
                 }
             }
             //merge the two account arrays and retrieve only unique account ids
             $unique_account_ids = array_unique($account_ids);
             //notify each unique account
             foreach ($unique_account_ids as $unique_account) {
                 if ($unique_account && $unique_account > 0) {
                     $notification = new rental_notification(0, $unique_account, 0, $this->unmarshal($this->db->f('contract_id', true), 'int'), $ts_today, null, null, null, null, $notification_id);
                     rental_soworkbench_notification::get_instance()->store($notification);
                 }
             }
             // set today as last notification date for this notification
             $this->set_notification_date($notification_id, $ts_today);
         }
     }
 }
コード例 #4
0
 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;
 }