Jump to content

product_quantity_refunded not updated for refunded orders


yhenden

Recommended Posts

Prestashop 1.7.5.2

The refunded orders are counted as "reserved products" by stock management, therefore the quantities in stock are false (Available quantity = physical_quantity - reserved_quantity) 

The refunded products are substracted from the ordered products :

classes/stock/StockManager.php :

 // Gets client_orders_qty
            $query = new DbQuery();
            $query->select('od.product_quantity, od.product_quantity_refunded');
            $query->from('order_detail', 'od');
            $query->leftjoin('orders', 'o', 'o.id_order = od.id_order');
            $query->where('od.product_id = ' . (int) $id_product);
            if (0 != $id_product_attribute) {
                $query->where('od.product_attribute_id = ' . (int) $id_product_attribute);
            }
            $query->leftJoin('order_history', 'oh', 'oh.id_order = o.id_order AND oh.id_order_state = o.current_state');
            $query->leftJoin('order_state', 'os', 'os.id_order_state = oh.id_order_state');
            $query->where('os.shipped != 1');
            $query->where('o.valid = 1 OR (os.id_order_state != ' . (int) Configuration::get('PS_OS_ERROR') . '
                           AND os.id_order_state != ' . (int) Configuration::get('PS_OS_CANCELED') . ')');
            $query->groupBy('od.id_order_detail');
            if (count($ids_warehouse)) {
                $query->where('od.id_warehouse IN(' . implode(', ', $ids_warehouse) . ')');
            }
            $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
            if (count($res)) {
                foreach ($res as $row) {
                    $client_orders_qty += ($row['product_quantity'] - $row['product_quantity_refunded']);
                }
            }
        }

that should have solved our problem... but this column is not updated when an order is refunded.

The method that updates product_quantity_refunded :

classes/Order.php :

    /**
     * Does NOT delete a product but "cancel" it (which means return/refund/delete it depending of the case).
     *
     * @param $order
     * @param OrderDetail $order_detail
     * @param int $quantity
     *
     * @return bool
     *
     * @throws PrestaShopException
     */
    public function deleteProduct($order, $order_detail, $quantity)
    {
        if (!(int) $this->getCurrentState() || !validate::isLoadedObject($order_detail)) {
            return false;
        }

        if ($this->hasBeenDelivered()) {
            if (!Configuration::get('PS_ORDER_RETURN', null, null, $this->id_shop)) {
                throw new PrestaShopException('PS_ORDER_RETURN is not defined in table configuration');
            }
            $order_detail->product_quantity_return += (int) $quantity;

            return $order_detail->update();
        } elseif ($this->hasBeenPaid()) {
            $order_detail->product_quantity_refunded += (int) $quantity;

            return $order_detail->update();
        }

        return $this->_deleteProduct($order_detail, (int) $quantity);
    }

The only case where that method is called is when a product is canceled :

controllers/admin/AdminOrdersController :

     

	// Delete product
	$order_detail = new OrderDetail((int) $id_order_detail);
	if (!$order->deleteProduct($order, $order_detail, $qty_cancel_product)) {
		$this->errors[] = $this->trans('An error occurred while attempting to delete the product.', array(), 'Admin.Orderscustomers.Notification') . ' <span class="bold">' . $order_detail->product_name . '</span>';
	}


... I don't understand... Anyone has a clue ?

 

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...