我创建了一个IMS系统。因为我的股票是负面的而且我必须停止它所以我的主要目的是在输入的产品超过可用库存时生成警报(例如,如果用户输入12个数量且库存仅有10个数量)如果输入的数量超过可用库存,则应生成警报消息,因此您无法生成订单。。下面是我的代码
public function create()
{
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT bill_no FROM orders ORDER BY id DESC LIMIT 1");
$result = $query->row()->bill_no;
$result++;
//echo $result;
//end();
//$curYear = date('Y');
$invoice_no = $result;
//$invoice['invoice_no'] = $invoice_no;
$data = array(
'po_no' => $this->input->post('po_no'),
'po_date' => $this->input->post('po_date'),
'challan_no' => $this->input->post('challan_no'),
'challan_date' => $this->input->post('challan_date'),
'bill_no' => $invoice_no,
'bill_date' => $this->input->post('bill_date'),
'terms' => $this->input->post('terms'),
'dispatch' => $this->input->post('dispatch'),
'party_id' => $this->input->post('id'),
'name' => $this->input->post('name_value'),
'address' => $this->input->post('address_value'),
'gstin' => $this->input->post('gstin_value'),
'mobile' => $this->input->post('mobile_value'),
'date_time' => strtotime(date('Y-m-d h:i:s a')),
'qty' => $this->input->post('qty_value'),
'gross_amount' => $this->input->post('gross_amount_value'),
'central_amount' => $this->input->post('central_amount_value'),
'net_amount' => $this->input->post('net_amount_value'),
'round_amount' =>$this->input->post('round_amount_value'),
'round_amount_words' => $this->input->post('round_amount_words'),
'paid_status' => 2,
'user_id' => $user_id
);
$insert = $this->db->insert('orders', $data);
$order_id = $this->db->insert_id();
$this->load->model('model_products');
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$items = array(
'order_id' => $order_id,
'product_id' => $this->input->post('product')[$x],
'hsn' => $this->input->post('hsn_value')[$x],
'rate' => $this->input->post('rate')[$x],
'qty' => $this->input->post('qty')[$x],
'unit' => $this->input->post('unit_value')[$x],
'amount' => $this->input->post('amount_value')[$x],
'gst' => $this->input->post('gst_value')[$x],
'gst_amount' => $this->input->post('gst_amount_value')[$x],
'last_amount' => $this->input->post('last_amount_value')[$x],
);
$this->db->insert('orders_item', $items);
// now decrease the stock from the product
$product_data = $this->model_products->getProductData($this->input->post('product')[$x]);
$qty = (int) $product_data['qty'] - (int) $this->input->post('qty')[$x];
$update_product = array('qty' => $qty);
$this->model_products->update($update_product, $this->input->post('product')[$x]);
}
return ($order_id) ? $order_id : false;
}
答案 0 :(得分:0)
在product_model
中,您可以添加一种方法来返回产品计数。
class Products_model extends CI_Model
{
...
/**
* Returns product quantity in db
* @param string $product_id
* @return integer
*/
public function productQty($product_id)
{
$qty = $this->db->query("SELECT `qty` FROM `products`"
. " WHERE product_id = `$product_id` ...")->result_array();
return (int) $qty[0]['qty'];
}
...
}
然后在上面的create()
中,您只需检查订购数量是否小于现有数量。
...
for($x = 0; $x < $count_product; $x++) {
$qtyOrdered = (int) $this->input->post('qty')[$x];
if ($this->model_products->productQuantity(
$this->input->post('product')[$x]
) < $qtyOrdered) {
// Quantity is less. Do something like alert concerned people
} else {
// Continue
}
}
...