Bug Report: Cannot Edit Purchase for Deleted Products

Cannot Edit Purchase for Deleted Products

If I add a purchase of any product and then delete the product from "Products", I can no longer edit the purchase. The "Edit Purchase" module crashes. Please see the screenshot where I have

HS

Harpreet Singh

Reported
If I add a purchase of any product and then delete the product from "Products", I can no longer edit the purchase. The "Edit Purchase" module crashes.

Please see the screenshot where I have added a purchase of FFR01 and then deleted the product.

Ideally, the system must not delete the product. Product can only be "Deactivated" so that data consistency is maintained.
  • HS

    Harpreet Singh

    Answered
    Sorry, forgot to attach screenshot.
  • MS

    Mian Saleem

    Answered
    There is no such option but will check in future updates. Thank you
  • HS

    Harpreet Singh

    Answered
    Thanks.

    In the mean time, anybody looking for a solution, I have created a MySQL trigger to prevent deletion if there are any purchases of the product.

    ```
    DELIMITER $$

    CREATE TRIGGER AKA_Prevent_Delete
    BEFORE DELETE ON sma_products
    FOR EACH ROW
    BEGIN
    IF OLD.id IN (SELECT `sma_purchase_items`.`product_id` FROM `sma_purchase_items` WHERE `sma_purchase_items`.`product_id` = OLD.id) THEN
    SIGNAL SQLSTATE '45000'
    SET MESSAGE_TEXT = 'Cannot delete purchased product. Delete purchase first.';
    END IF;
    END
    $$

    DELIMITER ;
    ```

    > PS: Review the code before using.
  • Login to Reply