예제 #1
0
 public function index()
 {
     $this->load->helper('operations');
     $this->load->helper('filter');
     $post = $this->input->post();
     if ($post !== FALSE) {
         $post_filter = $this->input->post('filter');
         if ($post_filter !== FALSE) {
             if (@$post_filter['renderas'] == 'graph' && (@$post_filter['orderby'] == 'fullname' || @$post_filter['orderby'] == 'group' || @$post_filter['orderby'] == 'school')) {
                 $post_filter['orderby'] = 'amount_left';
             }
             filter_store_filter(self::FILTER_PERSONS_TABLE, $post_filter);
         }
         redirect('ledcoin');
     }
     $filter = filter_get_filter(self::FILTER_PERSONS_TABLE, array('orderby' => 'amount_left', 'renderas' => 'table', 'graph_type' => 'column'));
     $operations_addition = new Operation();
     $operations_addition->where('type', Operation::TYPE_ADDITION);
     $operations_addition->select_sum('amount', 'amount_sum');
     $operations_addition->where_related_person('id', '${parent}.id');
     $operations_mining = new Operation();
     $operations_mining->where('type', Operation::TYPE_ADDITION);
     $operations_mining->where('addition_type', Operation::ADDITION_TYPE_MINING);
     $operations_mining->select_sum('amount', 'amount_sum');
     $operations_mining->where_related_person('id', '${parent}.id');
     $operations_subtraction_direct = new Operation();
     $operations_subtraction_direct->where('type', Operation::TYPE_SUBTRACTION);
     $operations_subtraction_direct->where('subtraction_type', Operation::SUBTRACTION_TYPE_DIRECT);
     $operations_subtraction_direct->select_sum('amount', 'amount_sum');
     $operations_subtraction_direct->where_related_person('id', '${parent}.id');
     $operations_subtraction_products = new Operation();
     $operations_subtraction_products->where('type', Operation::TYPE_SUBTRACTION);
     $operations_subtraction_products->where('subtraction_type', Operation::SUBTRACTION_TYPE_PRODUCTS);
     $operations_subtraction_products->where_related('product_quantity', 'price >', 0);
     $operations_subtraction_products->group_start(' NOT', 'AND');
     $operations_subtraction_products->where_related('product_quantity', 'product_id', NULL);
     $operations_subtraction_products->group_end();
     unset($operations_subtraction_products->db->ar_select[0]);
     $operations_subtraction_products->select_func('SUM', array('@product_quantities.quantity', '*', '@product_quantities.price', '*', '@product_quantities.multiplier'), 'amount_sum');
     $operations_subtraction_products->where_related_person('id', '${parent}.id');
     $operations_subtraction_services = new Operation();
     $operations_subtraction_services->where('type', Operation::TYPE_SUBTRACTION);
     $operations_subtraction_services->where('subtraction_type', Operation::SUBTRACTION_TYPE_SERVICES);
     $operations_subtraction_services->where_related('service_usage', 'price >', 0);
     $operations_subtraction_services->group_start(' NOT', 'AND');
     $operations_subtraction_services->where_related('service_usage', 'service_id', NULL);
     $operations_subtraction_services->group_end();
     unset($operations_subtraction_services->db->ar_select[0]);
     $operations_subtraction_services->select_func('SUM', array('@service_usages.quantity', '*', '@service_usages.price', '*', '@service_usages.multiplier'), 'amount_sum');
     $operations_subtraction_services->where_related_person('id', '${parent}.id');
     $persons_non_admins = new Person();
     $persons_non_admins->where('admin', 0);
     $persons_non_admins->select('*');
     $persons_non_admins->select_subquery($operations_addition, 'plus_amount');
     $persons_non_admins->select_subquery($operations_mining, 'plus_mined');
     $persons_non_admins->select_subquery($operations_subtraction_direct, 'minus_amount_direct');
     $persons_non_admins->select_subquery($operations_subtraction_products, 'minus_amount_products');
     $persons_non_admins->select_subquery($operations_subtraction_services, 'minus_amount_services');
     $persons_non_admins->include_related('group', 'title');
     if ($filter['orderby'] == 'amount_left') {
         $persons_non_admins->db->ar_orderby[] = '(IFNULL(`plus_amount`, 0) - IFNULL(`minus_amount_direct`, 0) - IFNULL(`minus_amount_products`, 0) - IFNULL(`minus_amount_services`, 0)) DESC';
     } elseif ($filter['orderby'] == 'amount_acquired') {
         $persons_non_admins->db->ar_orderby[] = 'IFNULL(`plus_amount`, 0) DESC';
     } elseif ($filter['orderby'] == 'amount_used') {
         $persons_non_admins->db->ar_orderby[] = '(IFNULL(`minus_amount_direct`, 0) + IFNULL(`minus_amount_products`, 0) + IFNULL(`minus_amount_services`, 0)) DESC';
     } elseif ($filter['orderby'] == 'amount_mined') {
         $persons_non_admins->db->ar_orderby[] = '(IFNULL(`plus_mined`, 0)) DESC';
     } elseif ($filter['orderby'] == 'fullname') {
         $persons_non_admins->order_by('surname', 'asc')->order_by('name', 'asc');
     } elseif ($filter['orderby'] == 'group') {
         $persons_non_admins->order_by_related('group', 'title', 'asc');
     } elseif ($filter['orderby'] == 'school') {
         $persons_non_admins->order_by('organisation', 'asc');
     }
     $persons_non_admins->get_iterated();
     $total_mined = operations_ledcoin_mined();
     $total_ledcoin = operations_ledcoin_maximum();
     $remaining_ledcoin = 0;
     operations_ledcoin_addition_possible(0, $remaining_ledcoin);
     $this->parser->parse('web/controllers/ledcoin/index.tpl', array('persons' => $persons_non_admins, 'title' => 'Účastníci', 'form' => $this->get_persons_filter_form($filter), 'filter' => $filter, 'total_mined' => $total_mined, 'total_ledcoin' => $total_ledcoin, 'remaining_ledcoin' => $remaining_ledcoin));
 }
예제 #2
0
 public function do_batch_ledcoin_addition()
 {
     $this->load->helper('operations');
     $this->db->trans_begin();
     build_validator_from_form($this->get_batch_ledcoin_addition_form());
     if ($this->form_validation->run()) {
         $batch_amount_data = $this->input->post('batch_amount');
         $person_amount_data = $this->input->post('person_amount');
         $workplace = new Workplace();
         if ((int) $batch_amount_data['workplace_id'] > 0) {
             $workplace->get_by_id((int) $batch_amount_data['workplace_id']);
             if (!$workplace->exists()) {
                 $this->db->trans_rollback();
                 add_error_flash_message('Zamestnanie sa nenašlo.');
                 redirect(site_url('operations/batch_ledcoin_addition'));
             }
         }
         $persons = new Person();
         $persons->where('admin', 0);
         $persons->get_iterated();
         $total_added = 0;
         foreach ($persons as $person) {
             if (array_key_exists($person->id, $person_amount_data) && (double) $person_amount_data[$person->id] > 0) {
                 $total_added += (double) $person_amount_data[$person->id];
             }
         }
         $remaining = 0;
         if ($total_added > 0 && $batch_amount_data['addition_type'] == Operation::ADDITION_TYPE_TRANSFER && !operations_ledcoin_addition_possible($total_added, $remaining)) {
             $this->db->trans_rollback();
             add_error_flash_message('Nedá sa prideliť <strong>' . $total_added . '</strong> ' . get_inflection_ledcoin($total_added) . ', na účte vedúcich zostáva iba <strong>' . $remaining . '</strong> ' . get_inflection_ledcoin($remaining) . '.');
             redirect('operations');
             return;
         }
         $persons = new Person();
         $persons->where('admin', 0);
         $persons->get_iterated();
         $successful_count = 0;
         $error_count = 0;
         $successful_messages = array();
         $error_messages = array();
         $total_added = 0;
         foreach ($persons as $person) {
             if (array_key_exists($person->id, $person_amount_data) && (double) $person_amount_data[$person->id] > 0) {
                 $operation = new Operation();
                 $operation->admin_id = auth_get_id();
                 $operation->amount = (double) $person_amount_data[$person->id];
                 $operation->type = Operation::TYPE_ADDITION;
                 $operation->subtraction_type = Operation::SUBTRACTION_TYPE_DIRECT;
                 $operation->addition_type = $batch_amount_data['addition_type'];
                 $operation->comment = @$batch_amount_data['comment'];
                 if ($operation->save(array('person' => $person, 'workplace' => $workplace))) {
                     $total_added += (double) $operation->amount;
                     $successful_messages[] = 'Účastník <strong>' . $person->name . ' ' . $person->surname . '</strong> dostal <strong>' . (double) $operation->amount . '</strong> ' . get_inflection_ledcoin((double) $operation->amount) . '.';
                     $successful_count++;
                 } else {
                     $error_count++;
                     $error_messages[] = 'Účastníkovi <strong>' . $person->name . ' ' . $person->surname . '</strong> sa nepodarilo prideliť LEDCOIN.';
                 }
             }
         }
         if ($total_added > 0 && $batch_amount_data['addition_type'] == Operation::ADDITION_TYPE_TRANSFER && !operations_ledcoin_limit_check($total_added)) {
             add_common_flash_message('Pozor, celkovým pridaním ' . $total_added . ' LEDCOIN-ov bol prekročený denný limit. Pred pridaním už bolo pridaných ' . operations_ledcoin_added_in_day() . ' z ' . operations_ledcoin_daily_limit() . ' LEDCOIN-ov!');
         }
         if ($successful_count == 0 && $error_count == 0) {
             $this->db->trans_rollback();
             add_error_flash_message('Nikomu nebol pridelený LEDCOIN, nakoľko bol odoslaný prázdny formulár.');
             redirect(site_url('operations'));
         } elseif ($successful_count == 0 && $error_count > 0) {
             $this->db->trans_rollback();
             add_error_flash_message('Nepodarilo sa nikomu pridať LEDCOIN:<br /><br />' . implode('<br />', $error_messages));
         } else {
             $this->db->trans_commit();
             if ($successful_count > 0) {
                 add_success_flash_message('LEDCOIN bol pridelený <strong>' . $successful_count . '</strong> ' . get_inflection_by_numbers($successful_count, 'účastníkom', 'účastníkovi', 'účastníkom', 'účastníkom', 'účastníkom', 'účastníkom') . ':<br /><br />' . implode('<br />', $successful_messages));
             }
             if ($error_count > 0) {
                 add_error_flash_message('LEDCOIN sa nepodarilo udeliť <strong>' . $error_count . '</strong> ' . get_inflection_by_numbers($error_count, 'účastníkom', 'účastníkovi', 'účastníkom', 'účastníkom', 'účastníkom', 'účastníkom') . ':<br /><br />' . implode('<br />', $error_messages));
             }
             redirect(site_url('operations'));
         }
     } else {
         $this->db->trans_rollback();
         $this->batch_ledcoin_addition();
     }
 }