【问题标题】:How can i get the correct qty with FIFO? PHP我如何使用 FIFO 获得正确的数量? PHP
【发布时间】:2022-12-04 00:31:49
【问题描述】:

每次我在我的销售中处理我如何触发以获得总和而不仅仅是我想要的表的第一行我有相同的产品名称和数量这是我的产品库存

例如,我有 50 个,在第 2 项中我有 100 个这样的

name qty date
milktea 50 december 3 2022
milktea 100 december 1 2022

在我的 SALES 中,如果我输入 140,它应该是

name qty date
milktea 0 december 3 2022
milktea 10 december 1 2022

但它只是删除第一个数量,第二个 1 保持这样

name qty date
milktea 0 december 3 2022
milktea 100 december 1 2022

我的代码在这里

 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;
    }
}

【问题讨论】:

    标签: php mysql codeigniter post codeigniter-3


    【解决方案1】:

    看起来您遇到的问题是您的代码没有正确更新 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);
    }
    
    

    【讨论】:

      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 2019-09-03
      • 2021-12-31
      • 2019-12-14
      • 2016-05-15
      相关资源
      最近更新 更多