codeigniter 我如何才能得到正确的数量与先进先出?PHP

64jmpszr  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(115)

每次我在我的销售中处理时,我如何触发以获得总和,而不仅仅是我想要的表的第一行,我有相同的产品名称和数量,这是我的产品库存
例如,我有一个数量为50和项目2我有100这样
| 姓名|数量|日期|
| - -|- -|- -|
| 奶茶|五十个|二零二二年十二月三日|
| 奶茶|100个|二零二二年十二月一日|
在我的销售中,如果我输入140,它必须被认为是
| 姓名|数量|日期|
| - -|- -|- -|
| 奶茶|第0页|二零二二年十二月三日|
| 奶茶|10个|二零二二年十二月一日|
但它只是删除了第一个数量,第二个数量保留为1,就像这样
| 姓名|数量|日期|
| - -|- -|- -|
| 奶茶|第0页|二零二二年十二月三日|
| 奶茶|100个|二零二二年十二月一日|
我代码在这里

public function update($id)
    {
        if($id) {
            $user_id = $this->session->userdata('id');
            $user_data = $this->model_users->getUserData($user_id);
            // update the table info

            $order_data = $this->getOrdersData($id);
            $data = $this->model_tables->update($order_data['table_id'], array('available' => 1));
        if($this->input->post('paid_status') == 1) {
            $this->model_tables->update($this->input->post('table_name'), array('available' => 1)); 
        }
        else {
            $this->model_tables->update($this->input->post('table_name'), array('available' => 2)); 
        }

        $data = array(
            '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' => ($this->input->post('change_value') > 0) ? $this->input->post('paid_status') : 2,
            'user_id' => $user_id,
            'table_id' => $this->input->post('table_name'),
            'cash_tendered' => $this->input->post('cash_tendered'),
            'total_change' => ($this->input->post('change_value') > 0) ? $this->input->post('change_value') : 0,
            'discount_id' => json_encode($this->input->post('discount')),
            'discount_percent' => $this->input->post('discount_perct_value'),
            'remarks' => $this->input->post('remarks'),
        );

        $this->db->where('id', $id);
        $update = $this->db->update('orders', $data);

        // now remove the order item data 
        $this->db->where('order_id', $id);
        $this->db->delete('order_items');

        $count_product = count($this->input->post('product'));
        for($x = 0; $x < $count_product; $x++) {
            $items = array(
                'order_id' => $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);

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

            $inputQuantity = (int)$this->input->post('qty')[$x];
            $this->db->where('qty >', 0);
            $this->db->order_by('expiration_date', 'ASC'); 
            $this->db->limit(1);
            $this->db->set('qty', "qty-$inputQuantity", false);
            $this->db->update('product_inventory');
        }
        return true;
    }
}
iqih9akk

iqih9akk1#

您遇到的问题似乎是代码没有正确更新order_items表中的数量。在代码中,您循环遍历订单中的每个项目,并用新数量更新order_items表。但是,您没有考虑到同一产品可能在订单中多次出现这一事实。
解决此问题的一种方法是循环遍历订单项并更新每个项的数量,同时跟踪每个产品的总数量。然后,在循环结束时,可以使用每个产品的正确数量更新order_items表。
下面是一个如何实现这一点的示例:

Copy code
// Loop through the order items and keep track of the total quantity for each product
$product_quantities = array();
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
    $product_id = $this->input->post('product')[$x];
    $qty = $this->input->post('qty')[$x];

    // If this is the first time we are seeing this product, initialize its quantity to 0
    if (!isset($product_quantities[$product_id])) {
        $product_quantities[$product_id] = 0;
    }

    // Add the quantity of this item to the total quantity for this product
    $product_quantities[$product_id] += $qty;
}

// Loop through the product quantities and update the `order_items` table
foreach ($product_quantities as $product_id => $qty) {
    $data = array(
        'qty' => $qty
    );
    $this->db->where('product_id', $product_id);
    $this->db->where('order_id', $id);
    $this->db->update('order_items', $data);
}

相关问题