public function update_product_quantity($product_id = NULL, $product_quantity_id = NULL) { if (is_null($product_id)) { add_error_flash_message('Produkt sa nenašiel.'); redirect(site_url('products')); } $this->db->trans_begin(); $product = new Product(); $product->get_by_id((int) $product_id); if (!$product->exists()) { add_error_flash_message('Produkt sa nenašiel.'); redirect(site_url('products')); } if (is_null($product_quantity_id)) { add_error_flash_message('Záznam o množstve tovaru sa nenašiel.'); redirect(site_url('products/stock/' . (int) $product->id)); } $product_quantity = new Product_quantity(); $product_quantity->where_related_product($product); $product_quantity->where('type', Product_quantity::TYPE_ADDITION); $product_quantity->get_by_id((int) $product_quantity_id); if (!$product_quantity->exists()) { add_error_flash_message('Záznam o množstve tovaru sa nenašiel.'); redirect(site_url('products/stock/' . (int) $product->id)); } build_validator_from_form($this->get_product_quantity_form()); if ($this->form_validation->run()) { $product_quantity_data = $this->input->post('product_quantity'); $product_quantity->from_array($product_quantity_data, array('quantity')); if ($product_quantity->save()) { $product_quantities_addition = new Product_quantity(); $product_quantities_addition->where('type', Product_quantity::TYPE_ADDITION); $product_quantities_addition->where_related_product($product); $product_quantities_addition->select_func('SUM', array('@quantity'), 'quantity_sum'); $product_quantities_addition->get(); $product_quantities_subtraction = new Product_quantity(); $product_quantities_subtraction->where('type', Product_quantity::TYPE_SUBTRACTION); $product_quantities_subtraction->where_related_product($product); $product_quantities_subtraction->select_func('SUM', array('@quantity'), 'quantity_sum'); $product_quantities_subtraction->get(); if ((int) $product_quantities_addition->quantity_sum >= (int) $product_quantities_subtraction->quantity_sum) { $this->db->trans_commit(); add_success_flash_message('Množstvo úspešne aktualizované na <strong>' . $product_quantity->quantity . '</strong> ' . get_inflection_by_numbers((int) $product_quantity->quantity, 'kusov', 'kus', 'kusy', 'kusy', 'kusy', 'kusov') . ' v záznamoch o produkte <strong>' . $product->title . '</strong>.'); redirect(site_url('products/stock/' . (int) $product->id)); } else { $this->db->trans_rollback(); add_error_flash_message('Nové množstvo spôsobuje, že je v celkovej evidencii menej produktov pridaných ako predaných. Množstvo nemôže byť aktualizované na <strong>' . $product_quantity->quantity . '</strong>.'); redirect(site_url('products/edit_product_quantity/' . (int) $product->id . '/' . (int) $product_quantity->id)); } } else { $this->db->trans_rollback(); add_error_flash_message('Nepodarilo sa aktualizovať množstvo produktu <strong>' . $product->title . '</strong>.'); redirect(site_url('products/edit_product_quantity/' . (int) $product->id . '/' . (int) $product_quantity->id)); } } else { $this->db->trans_rollback(); $this->edit_product_quantity($product_id, $product_quantity_id); } }