From 2e6b27f09423cdf7039e9a8f5ea1ec0e06eed0e7 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 15 Nov 2016 11:29:12 +0100 Subject: [PATCH] Delete from product_batch on product delete --- htdocs/product/class/product.class.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 4af929b8cba..078c9f0ef7c 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -924,7 +924,23 @@ class Product extends CommonObject if ($result < 0) { $error++; } // End call triggers } - + + // Delete from product_batch on product delete + if (! $error && $this->hasbatch()) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX.'product_batch'; + $sql.= " WHERE fk_product_stock IN ("; + $sql.= "SELECT rowid FROM ".MAIN_DB_PREFIX.'product_stock'; + $sql.= " WHERE fk_product = ".$id.")"; + dol_syslog(get_class($this).'::delete', LOG_DEBUG); + $result = $this->db->query($sql); + if (! $result) + { + $error++; + $this->errors[] = $this->db->lasterror(); + } + } + // Delete all child tables if (! $error) {