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) {