Esempio n. 1
0
 public function create_operation()
 {
     $this->load->helper('operations');
     $operation_data_temp = $this->input->post('operation');
     $this->db->trans_begin();
     $form = $this->get_form(@$operation_data_temp['type'], @$operation_data_temp['subtraction_type']);
     build_validator_from_form($form);
     if ($this->form_validation->run()) {
         $operation_data = $this->input->post('operation');
         $operation_service_data = $this->input->post('operation_service');
         $operation_product_data = $this->input->post('operation_product');
         $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_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');
         $person = new Person();
         $person->where('admin', 0);
         $person->select('*');
         $person->select_subquery($operations_addition, 'plus_amount');
         $person->select_subquery($operations_subtraction_direct, 'minus_amount_direct');
         $person->select_subquery($operations_subtraction_products, 'minus_amount_products');
         $person->select_subquery($operations_subtraction_services, 'minus_amount_services');
         $person->get_by_id((int) $operation_data['person_id']);
         if (!$person->exists()) {
             $this->db->trans_rollback();
             add_error_flash_message('Účastník sa nenašiel.');
             redirect(site_url('operations/new_operation'));
         }
         $admin = new Person();
         $admin->where('admin', 1);
         $admin->get_by_id((int) auth_get_id());
         if (!$admin->exists()) {
             $this->db->trans_rollback();
             add_error_flash_message('Administrátor sa nenašiel.');
             redirect(site_url('operations/new_operation'));
         }
         $workplace = new Workplace();
         if ((int) $operation_data['workplace_id'] > 0) {
             $workplace->get_by_id((int) $operation_data['workplace_id']);
             if (!$workplace->exists()) {
                 $this->db->trans_rollback();
                 add_error_flash_message('Zamestnanie sa nenašlo.');
                 redirect(site_url('operations/new_operation'));
             }
         }
         if ($operation_data['type'] == Operation::TYPE_ADDITION) {
             $amount_to_add = (double) $operation_data['amount'];
             $remaining = 0;
             if ($operation_data['addition_type'] == Operation::ADDITION_TYPE_TRANSFER && !operations_ledcoin_addition_possible($amount_to_add, $remaining)) {
                 $this->db->trans_rollback();
                 add_error_flash_message('Nedá sa prideliť <strong>' . $amount_to_add . '</strong> ' . get_inflection_ledcoin($amount_to_add) . ', na účte vedúcich zostáva iba <strong>' . $remaining . '</strong> ' . get_inflection_ledcoin($remaining) . '.');
                 redirect('operations');
                 return;
             }
             if ($operation_data['addition_type'] == Operation::ADDITION_TYPE_TRANSFER && !operations_ledcoin_limit_check($amount_to_add)) {
                 add_common_flash_message('Pozor, pridanie ' . $amount_to_add . ' LEDCOIN-ov presahuje denný limit. Pred pridaním bolo už použitých ' . operations_ledcoin_added_in_day() . ' z ' . operations_ledcoin_daily_limit() . ' LEDCOIN-ov!');
             }
             $operation = new Operation();
             $operation->from_array($operation_data, array('comment', 'amount', 'type', 'addition_type'));
             $operation->subtraction_type = Operation::SUBTRACTION_TYPE_DIRECT;
             if ($operation->save(array('person' => $person, 'admin' => $admin, 'workplace' => $workplace)) && $this->db->trans_status()) {
                 $this->db->trans_commit();
                 add_success_flash_message('Účastník <strong>' . $person->name . ' ' . $person->surname . '</strong> dostal <strong>' . $operation->amount . '</strong> ' . get_inflection_ledcoin((double) $operation->amount) . ' úspešne.');
                 redirect(site_url('operations'));
             } else {
                 $this->db->trans_rollback();
                 add_error_flash_message('Účastníkovi <strong>' . $person->name . ' ' . $person->surname . '</strong> sa nepodarilo prideliť <strong>' . $operation->amount . '</strong> ' . get_inflection_ledcoin((double) $operation->amount) . '.');
                 redirect(site_url('operations/new_operation'));
             }
         } else {
             $amount_at_disposal = doubleval($person->plus_amount) - doubleval($person->minus_amount_direct) - doubleval($person->minus_amount_products) - doubleval($person->minus_amount_services);
             $total_amount = 0;
             if ($operation_data['subtraction_type'] == Operation::SUBTRACTION_TYPE_DIRECT) {
                 $total_amount += (double) $operation_data['amount'];
             }
             $service_data = array();
             if ($operation_data['subtraction_type'] == Operation::SUBTRACTION_TYPE_SERVICES) {
                 $services = new Service();
                 $services->order_by('title', 'asc');
                 $services->get_iterated();
                 foreach ($services as $service) {
                     if (isset($operation_service_data[$service->id])) {
                         if (isset($operation_service_data[$service->id]['quantity']) && (int) $operation_service_data[$service->id]['quantity'] > 0 && isset($operation_service_data[$service->id]['price']) && (double) $operation_service_data[$service->id]['price'] > 0) {
                             $service_data[$service->id] = $operation_service_data[$service->id];
                             $total_amount += (int) $operation_service_data[$service->id]['quantity'] * (double) $operation_service_data[$service->id]['price'] * (double) $operation_data['multiplier'];
                         }
                     }
                 }
             }
             $product_data = array();
             if ($operation_data['subtraction_type'] == Operation::SUBTRACTION_TYPE_PRODUCTS) {
                 $quantity_addition = new Product_quantity();
                 $quantity_addition->select_sum('quantity', 'quantity_sum');
                 $quantity_addition->where('type', Product_quantity::TYPE_ADDITION);
                 $quantity_addition->where_related('product', 'id', '${parent}.id');
                 $quantity_subtraction = new Product_quantity();
                 $quantity_subtraction->select_sum('quantity', 'quantity_sum');
                 $quantity_subtraction->where('type', Product_quantity::TYPE_SUBTRACTION);
                 $quantity_subtraction->where_related('product', 'id', '${parent}.id');
                 $products = new Product();
                 $products->order_by('title', 'asc');
                 $products->select('*');
                 $products->select_subquery($quantity_addition, 'plus_quantity');
                 $products->select_subquery($quantity_subtraction, 'minus_quantity');
                 $products->get_iterated();
                 foreach ($products as $product) {
                     if (isset($operation_product_data[$product->id])) {
                         if (isset($operation_product_data[$product->id]['quantity']) && (int) $operation_product_data[$product->id]['quantity'] > 0 && isset($operation_product_data[$product->id]['price']) && (double) $operation_product_data[$product->id]['price'] > 0) {
                             $product_data[$product->id] = $operation_product_data[$product->id];
                             $total_amount += (int) $operation_product_data[$product->id]['quantity'] * (double) $operation_product_data[$product->id]['price'] * (double) $operation_data['multiplier'];
                         }
                     }
                 }
             }
             if ($total_amount > $amount_at_disposal) {
                 $this->db->trans_rollback();
                 add_error_flash_message('Účastník <strong>' . $person->name . ' ' . $person->surname . '</strong> nemá dostatok LEDCOIN-u. Potrebuje <strong>' . $total_amount . '</strong> ' . get_inflection_ledcoin((double) $total_amount) . ' ale má iba <strong>' . $amount_at_disposal . '</strong> ' . get_inflection_ledcoin((double) $amount_at_disposal) . '.');
                 redirect(site_url('operations/new_operation'));
             }
             if ($total_amount == 0) {
                 $this->db->trans_rollback();
                 add_error_flash_message('Celková suma LEDCOIN-u na odobratie je nulová, preto nie je možné pokračovať.');
                 redirect(site_url('operations/new_operation'));
             }
             $operation = new Operation();
             $operation->from_array($operation_data, array('comment', 'type', 'subtraction_type'));
             if ($operation_data['subtraction_type'] == Operation::SUBTRACTION_TYPE_DIRECT) {
                 $operation->amount = (double) $operation_data['amount'];
             } else {
                 $operation->amount = 0.0;
             }
             if ($operation->save(array('person' => $person, 'admin' => $admin, 'workplace' => $workplace)) && $this->db->trans_status()) {
                 if (count($service_data) > 0) {
                     foreach ($service_data as $service_id => $service_post) {
                         $service_usage = new Service_usage();
                         $service_usage->from_array($service_post, array('quantity', 'price'));
                         $service_usage->multiplier = (double) $operation_data['multiplier'];
                         $service_usage->service_id = (int) $service_id;
                         if (!$service_usage->save(array('operation' => $operation))) {
                             $service = new Service();
                             $service->get_by_id((int) $service_id);
                             $this->db->trans_rollback();
                             add_error_flash_message('Nepodarilo sa uložiť záznam o odobratí LEDCOIN-u za službu <strong>' . $service->title . '</strong>.');
                             redirect(site_url('operations/new_operation'));
                             die;
                         }
                     }
                 }
                 if (count($product_data) > 0) {
                     foreach ($product_data as $product_id => $product_post) {
                         $product_quantity = new Product_quantity();
                         $product_quantity->type = Product_quantity::TYPE_SUBTRACTION;
                         $product_quantity->from_array($product_post, array('quantity', 'price'));
                         $product_quantity->multiplier = (double) $operation_data['multiplier'];
                         $product_quantity->product_id = (int) $product_id;
                         if (!$product_quantity->save(array('operation' => $operation))) {
                             $product = new Product();
                             $product->get_by_id((int) $product_id);
                             $this->db->trans_rollback();
                             add_error_flash_message('Nepodarilo sa uložiť záznam o odobratí LEDCOIN-u za produkt <strong>' . $product->title . '</strong>.');
                             redirect(site_url('operations/new_operation'));
                             die;
                         }
                     }
                 }
                 $this->db->trans_commit();
                 add_success_flash_message('Účastníkovi <strong>' . $person->name . ' ' . $person->surname . '</strong> sa úspešne podarilo odobrať <strong>' . $total_amount . '</strong> ' . get_inflection_ledcoin((double) $total_amount) . '.');
                 redirect(site_url('operations'));
             } else {
                 $this->db->trans_rollback();
                 add_error_flash_message('Účastníkovi <strong>' . $person->name . ' ' . $person->surname . '</strong> sa nepodarilo odobrať <strong>' . $total_amount . '</strong> ' . get_inflection_ledcoin((double) $total_amount) . '.');
                 redirect(site_url('operations/new_operation'));
             }
         }
     } else {
         $this->db->trans_rollback();
         $this->new_operation();
     }
 }
Esempio n. 2
0
 public function overview($service_id = NULL)
 {
     if (is_null($service_id)) {
         add_error_flash_message('Služba sa nenašla.');
         redirect(site_ur('services'));
     }
     $service = new Service();
     $service->get_by_id((int) $service_id);
     if (!$service->exists()) {
         add_error_flash_message('Služba sa nenašla.');
         redirect(site_ur('services'));
     }
     $service_usages = new Service_usage();
     $service_usages->where_related_service($service);
     $service_usages->include_related('operation', array('id', 'type', 'created'));
     $service_usages->include_related('operation/person', array('name', 'surname'));
     $service_usages->include_related('operation/admin', array('name', 'surname'));
     $service_usages->include_related('operation/workplace', array('title'));
     $service_usages->order_by('created', 'desc');
     $service_usages->order_by_related('operation', 'created', 'desc');
     $service_usages->get_iterated();
     $this->parser->parse('web/controllers/services/overview.tpl', array('title' => 'Administrácia / Služby / Prehľad služby / ' . $service->title, 'service' => $service, 'service_usages' => $service_usages, 'back_url' => site_url('services')));
 }