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(); } }
public function do_batch_stock_addition() { $this->db->trans_begin(); build_validator_from_form($this->get_batch_stock_addition_form()); if ($this->form_validation->run()) { $products = new Product(); $products->get_iterated(); $product_quantity_data = $this->input->post('product_quantity_addition'); $success_messages = array(); $error_messages = array(); $added = 0; $failed = 0; foreach ($products as $product) { if (isset($product_quantity_data[$product->id]['quantity']) && (int) $product_quantity_data[$product->id]['quantity'] > 0) { $product_quantity = new Product_quantity(); $product_quantity->type = Product_quantity::TYPE_ADDITION; $product_quantity->quantity = (int) $product_quantity_data[$product->id]['quantity']; $product_quantity->price = NULL; $product_quantity->operation_id = NULL; if ($product_quantity->save($product) && $this->db->trans_status()) { $added++; $success_messages[] = 'Produktu <strong>' . $product->title . '</strong> ' . get_inflection_by_numbers((int) $product_quantity->quantity, 'bolo pridaných', 'bol pridaný', 'boli pridané', 'boli pridané', 'boli pridané', 'bolo pridaných') . ' <strong>' . $product_quantity->quantity . '</strong> ' . get_inflection_by_numbers((int) $product_quantity->quantity, 'kusov', 'kus', 'kusy', 'kusy', 'kusy', 'kusov') . ' zásob na sklad.'; } else { $failed++; $error_messages[] = 'Produktu <strong>' . $product->title . '</strong> sa nepodarilo pridať <strong>' . $product_quantity->quantity . '</strong> ' . get_inflection_by_numbers((int) $product_quantity->quantity, 'kusov', 'kus', 'kusy', 'kusy', 'kusy', 'kusov') . ' zásob na sklad.'; } } } if ($added == 0 && $failed == 0) { $this->db->trans_rollback(); add_common_flash_message('Nebolo nič pridané, keďže bol odoslaný prázdny formulár.'); redirect(site_url('products')); } elseif ($added == 0 && $failed > 0) { $this->db->trans_rollback(); add_error_flash_message('Nepodarilo sa pridať žiadne zásoby na sklad:<br /><br />' . implode('<br />', $error_messages)); redirect(site_url('products/batch_stock_addition')); } else { $this->db->trans_commit(); if ($added > 0) { add_success_flash_message('Boli pridané zásoby k celkovo <strong>' . $added . '</strong> ' . get_inflection_by_numbers($added, 'produktom', 'produktu', 'produktom', 'produktom', 'produktom', 'produktom') . ':<br /><br />' . implode('<br />', $success_messages)); } if ($failed > 0) { add_error_flash_message('K <strong>' . $failed . '</strong> ' . get_inflection_by_numbers($failed, 'produktom', 'produktu', 'produktom', 'produktom', 'produktom', 'produktom') . ' sa nepodarilo pridat zásoby:<br /><br />' . implode('<br />', $error_messages)); } redirect(site_url('products')); } } else { $this->db->trans_rollback(); $this->batch_stock_addition(); } }