codeigniter 如何检查所有选中的值数量?

raogr8fs  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(121)

任何人都可以帮助我吗?我怎么才能检查所有的产品选择上的选择或在选项?我正在使用codeigniter 3,我想检查是否有一个可用的股票或没有,但我是新的,不知道如何做到这一点,我希望有人能帮助我。首先,如果我选择数量为0的产品,它将不会处理,但如果我选择的第一个产品的现有数量大于0并选择了第二个空的数量,它仍然会处理,因为它只检查库存的第一个产品。我如何检查所有的数量选择在我的选择选项?
我的模特:

public function create()
{

    $user_id = $this->session->userdata('id');
    // get store id from user id 
    $user_data = $this->model_users->getUserData($user_id);
    
    $bill_no = 'BUBBLE-'.strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4));
    $data = array(
        'bill_no' => $bill_no,
        'date_time' => strtotime(date('Y-m-d h:i:s a')),
        'gross_amount' => $this->input->post('gross_amount_value'),
        'service_charge_rate' => $this->input->post('service_charge_rate'),
        'service_charge_amount' => ($this->input->post('service_charge_value') > 0) ?$this->input->post('service_charge_value'):0,
        'vat_charge_rate' => $this->input->post('vat_charge_rate'),
        'vat_charge_amount' => ($this->input->post('vat_charge_value') > 0) ? $this->input->post('vat_charge_value') : 0,
        'net_amount' => $this->input->post('net_amount_value'),
        'discount' => $this->input->post('discount_amount_value'),
        'paid_status' => 3,
        'user_id' => $user_id,
        'table_id' => $this->input->post('table_name'),
        'discount_id' => json_encode($this->input->post('discount')),
        'discount_percent' => $this->input->post('discount_perct_value'),
        'datetoday' => date('Y-m-d h:i:s a'),
        'payment_id' => json_encode($this->input->post('payment')),

    );
    $count_product = count($this->input->post('product'));
    for($x = 0; $x < $count_product; $x++) {

        $prodid = $this->input->post('product')[$x];
        $prod_quan = $this->model_products->getProductData($prodid);

        $inputQuantity = (int)$this->input->post('qty')[$x];
        $check = $this->model_orders->check_stock($prodid, $inputQuantity);
        if($check == TRUE)
        {
            $insert = $this->db->insert('orders', $data);
            $order_id = $this->db->insert_id();

            $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],
                    'qty' => $this->input->post('qty')[$x],
                    'rate' => $this->input->post('rate_value')[$x],
                    'amount' => $this->input->post('amount_value')[$x],
                );
                    $this->db->insert('order_items', $items);
            }
            $this->load->model('model_tables');
            $this->model_tables->update($this->input->post('table_name'), array('available' => 2));

            return ($order_id) ? $order_id : false;
        }           
        else
        {
            return false;
        }
    
    }
    
}
public function check_stock($productId, $inputQuantity)
{
   $product = $this->db->get_where('products', ['id' => $productId])->row();

   if(isset($product))
   {
      if($product->qty < $inputQuantity)
      {
           $this->session->set_flashdata('error','No more stock on some selected items'); 
           return false;
           
      }
      else
      {
        return true;
      }
   }
}

我的选择视图是这样的:

<select class="form-control select_group update-select" data-row-id="row_1" id="product_1" name="product[]" style="width:100%;" onchange="getProductData(1)" required>
                            <option disabled selected hidden value=''>--Select Products--</option>
                            <?php foreach ($products as $k => $v): ?>
                              <option value="<?php echo $v['id'] ?>"><?php echo $v['name'] ?></option>
                            <?php endforeach ?>
                          </select>
epfja78i

epfja78i1#

最简单的解决方案是在您发现某个产品缺货时立即返回false,如下所示:
遍历所选产品,并检查每个产品的库存。如果遇到缺货的产品,则返回false
如果所有产品都有库存,请创建订单,然后再次循环选择的产品,并将这些产品添加到数据库中的订单中。
我没有测试下面的代码,所以可能有错误。我只修改了if($check == TRUE)部分和一些花括号。

public function create()
{

    $user_id = $this->session->userdata('id');
    // get store id from user id 
    $user_data = $this->model_users->getUserData($user_id);
    
    $bill_no = 'BUBBLE-'.strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4));
    $data = array(
        'bill_no' => $bill_no,
        'date_time' => strtotime(date('Y-m-d h:i:s a')),
        'gross_amount' => $this->input->post('gross_amount_value'),
        'service_charge_rate' => $this->input->post('service_charge_rate'),
        'service_charge_amount' => ($this->input->post('service_charge_value') > 0) ?$this->input->post('service_charge_value'):0,
        'vat_charge_rate' => $this->input->post('vat_charge_rate'),
        'vat_charge_amount' => ($this->input->post('vat_charge_value') > 0) ? $this->input->post('vat_charge_value') : 0,
        'net_amount' => $this->input->post('net_amount_value'),
        'discount' => $this->input->post('discount_amount_value'),
        'paid_status' => 3,
        'user_id' => $user_id,
        'table_id' => $this->input->post('table_name'),
        'discount_id' => json_encode($this->input->post('discount')),
        'discount_percent' => $this->input->post('discount_perct_value'),
        'datetoday' => date('Y-m-d h:i:s a'),
        'payment_id' => json_encode($this->input->post('payment')),

    );
    $count_product = count($this->input->post('product'));
    for($x = 0; $x < $count_product; $x++) {

        $prodid = $this->input->post('product')[$x];
        $prod_quan = $this->model_products->getProductData($prodid);

        $inputQuantity = (int)$this->input->post('qty')[$x];
        $check = $this->model_orders->check_stock($prodid, $inputQuantity);
        if($check != TRUE)
        {
            return false;
        }
    }
    $insert = $this->db->insert('orders', $data);
    $order_id = $this->db->insert_id();

    $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],
            'qty' => $this->input->post('qty')[$x],
            'rate' => $this->input->post('rate_value')[$x],
            'amount' => $this->input->post('amount_value')[$x],
        );
            $this->db->insert('order_items', $items);
    }
    $this->load->model('model_tables');
    $this->model_tables->update($this->input->post('table_name'), array('available' => 2));

    return ($order_id) ? $order_id : false;
    
}

相关问题