From a6cbf9a7b7aedfd4595392da37fcc8f9e97fbdc3 Mon Sep 17 00:00:00 2001 From: Teddy Middleton-Smith Date: Fri, 21 Mar 2025 13:33:29 +0000 Subject: [PATCH] Fix: Cleanup \n 1. Remove PostgreSQL \n 2. Remove comments from Python \n 3. Remove non-demo ERP Python code \n 4. Remove deprecated Python code --- .gitignore | 7 +- README.md | 59 +- app.py | 27 +- business_objects/address.py | 2 - business_objects/base.py | 4 - business_objects/currency.py | 23 - business_objects/db_base.py | 19 - business_objects/region.py | 2 - business_objects/sql_error.py | 18 - business_objects/store/basket.py | 184 - business_objects/store/delivery_option.py | 98 - business_objects/store/image.py | 18 - .../store/manufacturing_purchase_order.py | 12 - business_objects/store/order.py | 89 - business_objects/store/product.py | 490 +- business_objects/store/product_category.py | 29 +- business_objects/store/product_variation.py | 32 - .../store/product_variation_tree.py | 12 - .../store/product_variation_type.py | 12 - business_objects/store/stock_item.py | 23 - business_objects/store/store_base.py | 43 +- business_objects/store/stripe.py | 161 - business_objects/user.py | 42 - config.py | 104 +- controllers/core.py | 6 +- controllers/store/pay_stripe.py | 167 - controllers/store/product.py | 3 - controllers/store/product_category.py | 19 - controllers/store/product_permutation.py | 4 - controllers/store/product_variation.py | 2 - controllers/store/stock_item.py | 25 - controllers/user.py | 49 +- datastores/datastore_base.py | 75 +- datastores/datastore_store_base.py | 65 +- datastores/datastore_store_basket.py | 143 - ...tore_store_manufacturing_purchase_order.py | 2 +- datastores/datastore_store_product.py | 63 +- .../datastore_store_product_category.py | 21 +- .../datastore_store_product_permutation.py | 44 +- datastores/datastore_store_stock_item.py | 32 +- datastores/datastore_store_stripe.py | 195 - datastores/datastore_store_supplier.py | 4 +- ...datastore_store_supplier_purchase_order.py | 2 +- datastores/datastore_user.py | 105 +- forms/base.py | 38 +- forms/forms.py | 35 +- forms/store/product_category.py | 9 +- forms/store/product_permutation.py | 13 +- forms/store/stock_item.py | 24 +- .../__pycache__/helper_abc.cpython-312.pyc | Bin 2161 -> 0 bytes helpers/DEPRECATED/helper_abc.py | 38 - helpers/DEPRECATED/helper_db_postgresql.py | 34 - helpers/helper_app.py | 1 - helpers/helper_db_mysql.py | 3 +- lib/argument_validation.py | 328 +- lib/data_types.py | 30 - pay_stripe.py | 182 - robots.txt | 4 +- routes.py | 54 - run.py | 6 +- static/PostgreSQL/000_combine.sql | 15113 ---------------- static/PostgreSQL/001_destroy.sql | 293 - .../100.0_tbl_Shop_Product_Change_Set.sql | 13 - .../100.1_tbl_Shop_User_Change_Set.sql | 13 - .../100.2_tbl_Shop_Access_Level.sql | 21 - ...l_Shop_Sales_And_Purchasing_Change_Set.sql | 13 - .../100.3_tbl_Shop_Access_Level_Audit.sql | 22 - static/PostgreSQL/100_tbl_Msg_Error_Type.sql | 13 - static/PostgreSQL/102_tbl_File_Type.sql | 17 - static/PostgreSQL/103_tbl_File_Type_Audit.sql | 22 - static/PostgreSQL/104_tbl_Shop_General.sql | 17 - .../PostgreSQL/105_tbl_Shop_General_Audit.sql | 22 - static/PostgreSQL/106_tbl_Shop_Category.sql | 21 - .../107_tbl_Shop_Category_Audit.sql | 22 - .../108_tbl_Shop_Recurrence_Interval.sql | 20 - ...109_tbl_Shop_Recurrence_Interval_Audit.sql | 22 - static/PostgreSQL/110.0_tbl_Shop_Region.sql | 20 - .../110.1_tbl_Shop_Region_Audit.sql | 22 - .../110.2_tbl_Shop_Region_Branch.sql | 29 - .../110.3_tbl_Shop_Region_Branch_Audit.sql | 22 - static/PostgreSQL/110.4_tbl_Shop_Currency.sql | 23 - .../110.5_tbl_Shop_Currency_Audit.sql | 23 - .../110.6_tbl_Shop_Tax_Or_Surcharge.sql | 38 - .../110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql | 23 - static/PostgreSQL/110.8_tbl_Shop_Product.sql | 48 - .../110.9_tbl_Shop_Product_Audit.sql | 22 - .../112_tbl_Shop_Variation_Type.sql | 21 - .../113.0_tbl_Shop_Variation_Type_Audit.sql | 22 - static/PostgreSQL/114_tbl_Shop_Variation.sql | 25 - .../115_tbl_Shop_Variation_Audit.sql | 22 - .../117.1_tbl_Shop_Product_Permutation.sql | 47 - ...7.2_tbl_Shop_Product_Permutation_Audit.sql | 23 - ...hop_Product_Permutation_Variation_Link.sql | 28 - ...oduct_Permutation_Variation_Link_Audit.sql | 23 - ..._tbl_Shop_Product_Currency_Region_Link.sql | 40 - ...hop_Product_Currency_Region_Link_Audit.sql | 22 - static/PostgreSQL/118_tbl_Shop_Image_Type.sql | 21 - .../119_tbl_Shop_Image_Type_Audit.sql | 22 - static/PostgreSQL/120_tbl_Shop_Image.sql | 35 - .../PostgreSQL/121_tbl_Shop_Image_Audit.sql | 21 - .../122_tbl_Shop_Delivery_Option.sql | 25 - .../123_tbl_Shop_Delivery_Option_Audit.sql | 22 - ..._tbl_Shop_Product_Delivery_Option_Link.sql | 44 - ...hop_Product_Delivery_Option_Link_Audit.sql | 22 - static/PostgreSQL/130.4_tbl_Shop_Discount.sql | 48 - .../131_tbl_Shop_Discount_Audit.sql | 23 - ...tbl_Shop_Discount_Region_Currency_Link.sql | 32 - ...op_Discount_Region_Currency_Link_Audit.sql | 23 - .../153_tbl_Shop_Permission_Group.sql | 21 - .../154_tbl_Shop_Permission_Group_Audit.sql | 23 - static/PostgreSQL/155_tbl_Shop_Permission.sql | 29 - .../156_tbl_Shop_Permission_Audit.sql | 23 - static/PostgreSQL/157_tbl_Shop_Role.sql | 20 - static/PostgreSQL/158_tbl_Shop_Role_Audit.sql | 23 - .../159_tbl_Shop_Role_Permission_Link.sql | 31 - ...60_tbl_Shop_Role_Permission_Link_Audit.sql | 22 - static/PostgreSQL/161_tbl_Shop_User.sql | 22 - static/PostgreSQL/162_tbl_Shop_User_Audit.sql | 23 - .../163_tbl_Shop_User_Role_Link.sql | 26 - .../164_tbl_Shop_User_Role_Link_Audit.sql | 22 - static/PostgreSQL/165_tbl_Shop_Address.sql | 33 - .../PostgreSQL/166_tbl_Shop_Address_Audit.sql | 23 - .../PostgreSQL/167_tbl_Shop_User_Basket.sql | 39 - .../168_tbl_Shop_User_Basket_Audit.sql | 27 - .../169_tbl_Shop_User_Order_Status.sql | 21 - .../170_tbl_Shop_User_Order_Status_Audit.sql | 22 - static/PostgreSQL/181.0_tbl_Shop_Supplier.sql | 32 - .../181.1_tbl_Shop_Supplier_Audit.sql | 22 - .../181.2_tbl_Shop_Unit_Measurement.sql | 22 - .../181.3_tbl_Shop_Unit_Measurement_Audit.sql | 22 - ...4_tbl_Shop_Unit_Measurement_Conversion.sql | 22 - ...Shop_Unit_Measurement_Conversion_Audit.sql | 22 - ...181.6_tbl_Shop_Supplier_Purchase_Order.sql | 41 - ...tbl_Shop_Supplier_Purchase_Order_Audit.sql | 22 - ...p_Supplier_Purchase_Order_Product_Link.sql | 37 - ...lier_Purchase_Order_Product_Link_Audit.sql | 22 - ...plier_Purchase_Order_Product_Link_Temp.sql | 34 - ..._tbl_Shop_Manufacturing_Purchase_Order.sql | 33 - ...hop_Manufacturing_Purchase_Order_Audit.sql | 22 - ...ufacturing_Purchase_Order_Product_Link.sql | 38 - ...uring_Purchase_Order_Product_Link_Temp.sql | 35 - ...ring_Purchase_Order_Product_Link_Audit.sql | 22 - static/PostgreSQL/187.0_tbl_Shop_Customer.sql | 29 - .../187.1_tbl_Shop_Customer_Audit.sql | 22 - .../187.2_tbl_Shop_Customer_Sales_Order.sql | 35 - ...88_tbl_Shop_Customer_Sales_Order_Audit.sql | 22 - ...Shop_Customer_Sales_Order_Product_Link.sql | 38 - ...ustomer_Sales_Order_Product_Link_Audit.sql | 22 - ...Customer_Sales_Order_Product_Link_Temp.sql | 34 - ...i_Shop_Sales_And_Purchasing_Change_Set.sql | 26 - .../301.1_tri_Shop_User_Change_Set.sql | 20 - .../301.2_tri_Shop_Access_Level.sql | 66 - .../301_tri_Shop_Product_Change_Set.sql | 22 - static/PostgreSQL/302_tri_File_Type.sql | 48 - static/PostgreSQL/303_tri_File_Type_Audit.sql | 33 - static/PostgreSQL/304_tri_Shop_General.sql | 47 - static/PostgreSQL/306_tri_Shop_Category.sql | 63 - .../308_tri_Shop_Recurrence_Interval.sql | 59 - static/PostgreSQL/310.0_tri_Shop_Region.sql | 59 - .../310.2_tri_Shop_Region_Branch.sql | 57 - static/PostgreSQL/310.4_tri_Shop_Currency.sql | 71 - .../310.6_tri_Shop_Tax_Or_Surcharge.sql | 80 - static/PostgreSQL/310.8_tri_Shop_Product.sql | 173 - .../312_tri_Shop_Variation_Type.sql | 63 - static/PostgreSQL/314_tri_Shop_Variation.sql | 59 - .../317.1_tri_Shop_Product_Permutation.sql | 133 - ...hop_Product_Permutation_Variation_Link.sql | 65 - ..._tri_Shop_Product_Currency_Region_Link.sql | 97 - static/PostgreSQL/318_tri_Shop_Image_Type.sql | 63 - static/PostgreSQL/320_tri_Shop_Image.sql | 75 - .../322_tri_Shop_Delivery_Option.sql | 75 - ..._tri_Shop_Product_Delivery_Option_Link.sql | 77 - static/PostgreSQL/330_tri_Shop_Discount.sql | 87 - ...tri_Shop_Discount_Region_Currency_Link.sql | 61 - .../353_tri_Shop_Permission_Group.sql | 63 - static/PostgreSQL/355_tri_Shop_Permission.sql | 71 - static/PostgreSQL/357_tri_Shop_Role.sql | 63 - .../359_tri_Shop_Role_Permission_Link.sql | 65 - static/PostgreSQL/361_tri_Shop_User.sql | 71 - .../363_tri_Shop_User_Role_Link.sql | 51 - static/PostgreSQL/365_tri_Shop_Address.sql | 83 - .../PostgreSQL/367_tri_Shop_User_Basket.sql | 63 - .../369_tri_Shop_User_Order_Status.sql | 63 - static/PostgreSQL/381.0_tri_Shop_Supplier.sql | 83 - .../381.2_tri_Shop_Unit_Measurement.sql | 67 - ...ri_Shop_Unit_Of_Measurement_Conversion.sql | 71 - ...381.6_tri_Shop_Supplier_Purchase_Order.sql | 78 - ...p_Supplier_Purchase_Order_Product_Link.sql | 87 - ..._tri_Shop_Manufacturing_Purchase_Order.sql | 63 - ...ufacturing_Purchase_Order_Product_Link.sql | 87 - static/PostgreSQL/387.0_tri_Shop_Customer.sql | 79 - .../387.2_tri_Shop_Customer_Sales_Order.sql | 62 - ...Shop_Customer_Sales_Order_Product_Link.sql | 86 - static/PostgreSQL/600_p_shop_save_product.sql | 257 - static/PostgreSQL/600_p_shop_user_eval.sql | 730 - .../602_p_save_supplier_purchase_order.sql | 515 - .../PostgreSQL/602_p_shop_save_supplier.sql | 306 - ...shop_save_manufacturing_purchase_order.sql | 606 - .../PostgreSQL/605_p_shop_save_customer.sql | 313 - .../606_p_shop_save_customer_sales_order.sql | 555 - static/PostgreSQL/610_p_shop_save_user.sql | 166 - .../611_p_shop_save_user_basket.sql | 833 - .../700_p_shop_get_many_product.sql | 1231 -- .../701_p_shop_get_many_role_permission.sql | 152 - .../702.1_p_shop_get_many_currency.sql | 57 - .../702.2_p_shop_get_many_region.sql | 60 - .../703_p_shop_get_many_user_order.sql | 283 - ...704_p_shop_get_many_stripe_product_new.sql | 316 - .../705_p_shop_get_many_stripe_price_new.sql | 253 - .../706_p_shop_get_many_supplier.sql | 247 - ..._shop_get_many_supplier_purchase_order.sql | 709 - ..._get_many_manufacturing_purchase_order.sql | 601 - .../709_p_shop_get_many_customer.sql | 249 - ...0_p_shop_get_many_customer_sales_order.sql | 718 - static/PostgreSQL/900_populate.sql | 656 - static/PostgreSQL/901_view.sql | 188 - static/PostgreSQL/910_anal.sql | 26 - static/PostgreSQL/920_edit_permissions.sql | 83 - static/PostgreSQL/temp.txt | 153 - static/js/pages/base.js | 13 +- 220 files changed, 151 insertions(+), 34140 deletions(-) delete mode 100644 business_objects/store/basket.py delete mode 100644 business_objects/store/delivery_option.py delete mode 100644 business_objects/store/order.py delete mode 100644 business_objects/store/stripe.py delete mode 100644 controllers/store/pay_stripe.py delete mode 100644 datastores/datastore_store_basket.py delete mode 100644 datastores/datastore_store_stripe.py delete mode 100644 helpers/DEPRECATED/__pycache__/helper_abc.cpython-312.pyc delete mode 100644 helpers/DEPRECATED/helper_abc.py delete mode 100644 helpers/DEPRECATED/helper_db_postgresql.py delete mode 100644 pay_stripe.py delete mode 100644 routes.py delete mode 100644 static/PostgreSQL/000_combine.sql delete mode 100644 static/PostgreSQL/001_destroy.sql delete mode 100644 static/PostgreSQL/100.0_tbl_Shop_Product_Change_Set.sql delete mode 100644 static/PostgreSQL/100.1_tbl_Shop_User_Change_Set.sql delete mode 100644 static/PostgreSQL/100.2_tbl_Shop_Access_Level.sql delete mode 100644 static/PostgreSQL/100.2_tbl_Shop_Sales_And_Purchasing_Change_Set.sql delete mode 100644 static/PostgreSQL/100.3_tbl_Shop_Access_Level_Audit.sql delete mode 100644 static/PostgreSQL/100_tbl_Msg_Error_Type.sql delete mode 100644 static/PostgreSQL/102_tbl_File_Type.sql delete mode 100644 static/PostgreSQL/103_tbl_File_Type_Audit.sql delete mode 100644 static/PostgreSQL/104_tbl_Shop_General.sql delete mode 100644 static/PostgreSQL/105_tbl_Shop_General_Audit.sql delete mode 100644 static/PostgreSQL/106_tbl_Shop_Category.sql delete mode 100644 static/PostgreSQL/107_tbl_Shop_Category_Audit.sql delete mode 100644 static/PostgreSQL/108_tbl_Shop_Recurrence_Interval.sql delete mode 100644 static/PostgreSQL/109_tbl_Shop_Recurrence_Interval_Audit.sql delete mode 100644 static/PostgreSQL/110.0_tbl_Shop_Region.sql delete mode 100644 static/PostgreSQL/110.1_tbl_Shop_Region_Audit.sql delete mode 100644 static/PostgreSQL/110.2_tbl_Shop_Region_Branch.sql delete mode 100644 static/PostgreSQL/110.3_tbl_Shop_Region_Branch_Audit.sql delete mode 100644 static/PostgreSQL/110.4_tbl_Shop_Currency.sql delete mode 100644 static/PostgreSQL/110.5_tbl_Shop_Currency_Audit.sql delete mode 100644 static/PostgreSQL/110.6_tbl_Shop_Tax_Or_Surcharge.sql delete mode 100644 static/PostgreSQL/110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql delete mode 100644 static/PostgreSQL/110.8_tbl_Shop_Product.sql delete mode 100644 static/PostgreSQL/110.9_tbl_Shop_Product_Audit.sql delete mode 100644 static/PostgreSQL/112_tbl_Shop_Variation_Type.sql delete mode 100644 static/PostgreSQL/113.0_tbl_Shop_Variation_Type_Audit.sql delete mode 100644 static/PostgreSQL/114_tbl_Shop_Variation.sql delete mode 100644 static/PostgreSQL/115_tbl_Shop_Variation_Audit.sql delete mode 100644 static/PostgreSQL/117.1_tbl_Shop_Product_Permutation.sql delete mode 100644 static/PostgreSQL/117.2_tbl_Shop_Product_Permutation_Audit.sql delete mode 100644 static/PostgreSQL/117.3_tbl_Shop_Product_Permutation_Variation_Link.sql delete mode 100644 static/PostgreSQL/117.4_tbl_Shop_Product_Permutation_Variation_Link_Audit.sql delete mode 100644 static/PostgreSQL/117.5_tbl_Shop_Product_Currency_Region_Link.sql delete mode 100644 static/PostgreSQL/117.6_tbl_Shop_Product_Currency_Region_Link_Audit.sql delete mode 100644 static/PostgreSQL/118_tbl_Shop_Image_Type.sql delete mode 100644 static/PostgreSQL/119_tbl_Shop_Image_Type_Audit.sql delete mode 100644 static/PostgreSQL/120_tbl_Shop_Image.sql delete mode 100644 static/PostgreSQL/121_tbl_Shop_Image_Audit.sql delete mode 100644 static/PostgreSQL/122_tbl_Shop_Delivery_Option.sql delete mode 100644 static/PostgreSQL/123_tbl_Shop_Delivery_Option_Audit.sql delete mode 100644 static/PostgreSQL/124_tbl_Shop_Product_Delivery_Option_Link.sql delete mode 100644 static/PostgreSQL/125_tbl_Shop_Product_Delivery_Option_Link_Audit.sql delete mode 100644 static/PostgreSQL/130.4_tbl_Shop_Discount.sql delete mode 100644 static/PostgreSQL/131_tbl_Shop_Discount_Audit.sql delete mode 100644 static/PostgreSQL/132_tbl_Shop_Discount_Region_Currency_Link.sql delete mode 100644 static/PostgreSQL/133_tbl_Shop_Discount_Region_Currency_Link_Audit.sql delete mode 100644 static/PostgreSQL/153_tbl_Shop_Permission_Group.sql delete mode 100644 static/PostgreSQL/154_tbl_Shop_Permission_Group_Audit.sql delete mode 100644 static/PostgreSQL/155_tbl_Shop_Permission.sql delete mode 100644 static/PostgreSQL/156_tbl_Shop_Permission_Audit.sql delete mode 100644 static/PostgreSQL/157_tbl_Shop_Role.sql delete mode 100644 static/PostgreSQL/158_tbl_Shop_Role_Audit.sql delete mode 100644 static/PostgreSQL/159_tbl_Shop_Role_Permission_Link.sql delete mode 100644 static/PostgreSQL/160_tbl_Shop_Role_Permission_Link_Audit.sql delete mode 100644 static/PostgreSQL/161_tbl_Shop_User.sql delete mode 100644 static/PostgreSQL/162_tbl_Shop_User_Audit.sql delete mode 100644 static/PostgreSQL/163_tbl_Shop_User_Role_Link.sql delete mode 100644 static/PostgreSQL/164_tbl_Shop_User_Role_Link_Audit.sql delete mode 100644 static/PostgreSQL/165_tbl_Shop_Address.sql delete mode 100644 static/PostgreSQL/166_tbl_Shop_Address_Audit.sql delete mode 100644 static/PostgreSQL/167_tbl_Shop_User_Basket.sql delete mode 100644 static/PostgreSQL/168_tbl_Shop_User_Basket_Audit.sql delete mode 100644 static/PostgreSQL/169_tbl_Shop_User_Order_Status.sql delete mode 100644 static/PostgreSQL/170_tbl_Shop_User_Order_Status_Audit.sql delete mode 100644 static/PostgreSQL/181.0_tbl_Shop_Supplier.sql delete mode 100644 static/PostgreSQL/181.1_tbl_Shop_Supplier_Audit.sql delete mode 100644 static/PostgreSQL/181.2_tbl_Shop_Unit_Measurement.sql delete mode 100644 static/PostgreSQL/181.3_tbl_Shop_Unit_Measurement_Audit.sql delete mode 100644 static/PostgreSQL/181.4_tbl_Shop_Unit_Measurement_Conversion.sql delete mode 100644 static/PostgreSQL/181.5_tbl_Shop_Unit_Measurement_Conversion_Audit.sql delete mode 100644 static/PostgreSQL/181.6_tbl_Shop_Supplier_Purchase_Order.sql delete mode 100644 static/PostgreSQL/181.7_tbl_Shop_Supplier_Purchase_Order_Audit.sql delete mode 100644 static/PostgreSQL/181.8_tbl_Shop_Supplier_Purchase_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/181.9_tbl_Shop_Supplier_Purchase_Order_Product_Link_Audit.sql delete mode 100644 static/PostgreSQL/182.0_tbl_Shop_Supplier_Purchase_Order_Product_Link_Temp.sql delete mode 100644 static/PostgreSQL/183_tbl_Shop_Manufacturing_Purchase_Order.sql delete mode 100644 static/PostgreSQL/184_tbl_Shop_Manufacturing_Purchase_Order_Audit.sql delete mode 100644 static/PostgreSQL/185_tbl_Shop_Manufacturing_Purchase_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/186.1_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Temp.sql delete mode 100644 static/PostgreSQL/186_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Audit.sql delete mode 100644 static/PostgreSQL/187.0_tbl_Shop_Customer.sql delete mode 100644 static/PostgreSQL/187.1_tbl_Shop_Customer_Audit.sql delete mode 100644 static/PostgreSQL/187.2_tbl_Shop_Customer_Sales_Order.sql delete mode 100644 static/PostgreSQL/188_tbl_Shop_Customer_Sales_Order_Audit.sql delete mode 100644 static/PostgreSQL/189_tbl_Shop_Customer_Sales_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/190_tbl_Shop_Customer_Sales_Order_Product_Link_Audit.sql delete mode 100644 static/PostgreSQL/191_tbl_Shop_Customer_Sales_Order_Product_Link_Temp.sql delete mode 100644 static/PostgreSQL/300.2_tri_Shop_Sales_And_Purchasing_Change_Set.sql delete mode 100644 static/PostgreSQL/301.1_tri_Shop_User_Change_Set.sql delete mode 100644 static/PostgreSQL/301.2_tri_Shop_Access_Level.sql delete mode 100644 static/PostgreSQL/301_tri_Shop_Product_Change_Set.sql delete mode 100644 static/PostgreSQL/302_tri_File_Type.sql delete mode 100644 static/PostgreSQL/303_tri_File_Type_Audit.sql delete mode 100644 static/PostgreSQL/304_tri_Shop_General.sql delete mode 100644 static/PostgreSQL/306_tri_Shop_Category.sql delete mode 100644 static/PostgreSQL/308_tri_Shop_Recurrence_Interval.sql delete mode 100644 static/PostgreSQL/310.0_tri_Shop_Region.sql delete mode 100644 static/PostgreSQL/310.2_tri_Shop_Region_Branch.sql delete mode 100644 static/PostgreSQL/310.4_tri_Shop_Currency.sql delete mode 100644 static/PostgreSQL/310.6_tri_Shop_Tax_Or_Surcharge.sql delete mode 100644 static/PostgreSQL/310.8_tri_Shop_Product.sql delete mode 100644 static/PostgreSQL/312_tri_Shop_Variation_Type.sql delete mode 100644 static/PostgreSQL/314_tri_Shop_Variation.sql delete mode 100644 static/PostgreSQL/317.1_tri_Shop_Product_Permutation.sql delete mode 100644 static/PostgreSQL/317.3_tri_Shop_Product_Permutation_Variation_Link.sql delete mode 100644 static/PostgreSQL/317.5_tri_Shop_Product_Currency_Region_Link.sql delete mode 100644 static/PostgreSQL/318_tri_Shop_Image_Type.sql delete mode 100644 static/PostgreSQL/320_tri_Shop_Image.sql delete mode 100644 static/PostgreSQL/322_tri_Shop_Delivery_Option.sql delete mode 100644 static/PostgreSQL/324_tri_Shop_Product_Delivery_Option_Link.sql delete mode 100644 static/PostgreSQL/330_tri_Shop_Discount.sql delete mode 100644 static/PostgreSQL/332_tri_Shop_Discount_Region_Currency_Link.sql delete mode 100644 static/PostgreSQL/353_tri_Shop_Permission_Group.sql delete mode 100644 static/PostgreSQL/355_tri_Shop_Permission.sql delete mode 100644 static/PostgreSQL/357_tri_Shop_Role.sql delete mode 100644 static/PostgreSQL/359_tri_Shop_Role_Permission_Link.sql delete mode 100644 static/PostgreSQL/361_tri_Shop_User.sql delete mode 100644 static/PostgreSQL/363_tri_Shop_User_Role_Link.sql delete mode 100644 static/PostgreSQL/365_tri_Shop_Address.sql delete mode 100644 static/PostgreSQL/367_tri_Shop_User_Basket.sql delete mode 100644 static/PostgreSQL/369_tri_Shop_User_Order_Status.sql delete mode 100644 static/PostgreSQL/381.0_tri_Shop_Supplier.sql delete mode 100644 static/PostgreSQL/381.2_tri_Shop_Unit_Measurement.sql delete mode 100644 static/PostgreSQL/381.4_tri_Shop_Unit_Of_Measurement_Conversion.sql delete mode 100644 static/PostgreSQL/381.6_tri_Shop_Supplier_Purchase_Order.sql delete mode 100644 static/PostgreSQL/381.8_tri_Shop_Supplier_Purchase_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/383_tri_Shop_Manufacturing_Purchase_Order.sql delete mode 100644 static/PostgreSQL/385_tri_Shop_Manufacturing_Purchase_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/387.0_tri_Shop_Customer.sql delete mode 100644 static/PostgreSQL/387.2_tri_Shop_Customer_Sales_Order.sql delete mode 100644 static/PostgreSQL/389_tri_Shop_Customer_Sales_Order_Product_Link.sql delete mode 100644 static/PostgreSQL/600_p_shop_save_product.sql delete mode 100644 static/PostgreSQL/600_p_shop_user_eval.sql delete mode 100644 static/PostgreSQL/602_p_save_supplier_purchase_order.sql delete mode 100644 static/PostgreSQL/602_p_shop_save_supplier.sql delete mode 100644 static/PostgreSQL/604_p_shop_save_manufacturing_purchase_order.sql delete mode 100644 static/PostgreSQL/605_p_shop_save_customer.sql delete mode 100644 static/PostgreSQL/606_p_shop_save_customer_sales_order.sql delete mode 100644 static/PostgreSQL/610_p_shop_save_user.sql delete mode 100644 static/PostgreSQL/611_p_shop_save_user_basket.sql delete mode 100644 static/PostgreSQL/700_p_shop_get_many_product.sql delete mode 100644 static/PostgreSQL/701_p_shop_get_many_role_permission.sql delete mode 100644 static/PostgreSQL/702.1_p_shop_get_many_currency.sql delete mode 100644 static/PostgreSQL/702.2_p_shop_get_many_region.sql delete mode 100644 static/PostgreSQL/703_p_shop_get_many_user_order.sql delete mode 100644 static/PostgreSQL/704_p_shop_get_many_stripe_product_new.sql delete mode 100644 static/PostgreSQL/705_p_shop_get_many_stripe_price_new.sql delete mode 100644 static/PostgreSQL/706_p_shop_get_many_supplier.sql delete mode 100644 static/PostgreSQL/706_p_shop_get_many_supplier_purchase_order.sql delete mode 100644 static/PostgreSQL/708_p_shop_get_many_manufacturing_purchase_order.sql delete mode 100644 static/PostgreSQL/709_p_shop_get_many_customer.sql delete mode 100644 static/PostgreSQL/710_p_shop_get_many_customer_sales_order.sql delete mode 100644 static/PostgreSQL/900_populate.sql delete mode 100644 static/PostgreSQL/901_view.sql delete mode 100644 static/PostgreSQL/910_anal.sql delete mode 100644 static/PostgreSQL/920_edit_permissions.sql delete mode 100644 static/PostgreSQL/temp.txt diff --git a/.gitignore b/.gitignore index 861528f2..cd7358bd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ __pycache__/ # Ignore logs and databases *.log +*.log.* # Ignore logs and databases # *.sql @@ -58,13 +59,13 @@ TODO.md tmp/ # Ignore all .txt files in the doc/ directory -doc/*.txt +static/docs/*.txt # But don't ignore doc/important.txt, even though we're ignoring .txt files above -!doc/important.txt +!static/docs/important.txt # Ignore all .pdf files in the doc/ directory and any of its subdirectories -doc/**/*.pdf +static/docs/**/*.pdf # Ignore deprecated files DEPRECATED/ diff --git a/README.md b/README.md index 9a4f186e..bfd89738 100644 --- a/README.md +++ b/README.md @@ -8,49 +8,40 @@ NOTE: ALL INSTRUCTIONS ARE FOR LINUX. - Python 3.10 - npm - Locally hosted MySQL database +- Auth0 application for project +- (Optional) Google ReCAPTCHA public and secret keys +- (Optional) Bot email account -## 1. Create Auth0 application for project +## 1. Create and populate database +1.1. Sign into your local MySQL workspace +1.2. Run script static/MySQL/0000_combined.sql -## 2. (Optional) Create Google reCAPTCHA service -NOTE: IF CONTACT US PAGE FORM FUNCTIONALITY IS REQUIRED, COMPLETE THIS STEP. -2.1. Go to the reCAPTCHA Admin Console and register your site: https://www.google.com/recaptcha/admin -2.2. Choose reCAPTCH v2 -2.3. Generate and save keys for step 10 - -## 3. (Optional) Create bot email account -NOTE: IF CONTACT US PAGE FORM FUNCTIONALITY IS REQUIRED, COMPLETE THIS STEP. -3.1. Using your chosen email provider, create a new account for sending emails from the web server - -## 4. Create and populate database -4.1. Sign into your local MySQL workspace -4.2. Run script static/MySQL/0000_combined.sql - -## 5. Open the project in a new terminal window -5.1. Open a new terminal window -5.2. Navigate to the project with the following command +## 2. Open the project in a new terminal window +2.1. Open a new terminal window +2.2. Navigate to the project with the following command - cd path/to/project/main/directory -## 6. Create virtual environment -6.1. Terminal command +## 3. Create virtual environment +3.1. Terminal command - python3 -m venv env_demo_partsERP -## 7. Enter virtual environment -7.1. Terminal command +## 4. Enter virtual environment +4.1. Terminal command - source env_demo_partsERP/bin/activate -## 8. Run module bundler -8.1. Terminal command +## 5. Run module bundler +5.1. Terminal command - npm run build -## 9. Install required python packages -9.1. Terminal command +## 6. Install required python packages +6.1. Terminal command - pip3 install -r requirements.txt -## 10. Set required environment variables -10.1. Terminal command +## 7. Set required environment variables +7.1. Terminal command - export variable_name=variable_value -10.2. Required variables +7.2. Required variables | Name | Purpose | Example Value | | -------- | -------- | -------- | | KEY_SECRET_FLASK | Private key for server encryption. | password123 | @@ -60,7 +51,7 @@ NOTE: IF CONTACT US PAGE FORM FUNCTIONALITY IS REQUIRED, COMPLETE THIS STEP. | SQLALCHEMY_DATABASE_URI | Database connection string for SQLAlchemy. | mysql://db_user_name:db_password@127.0.0.1:3306/db_name | | URL_HOST | Domain (and port as necessary) that the project is hosted on. | https://127.0.0.1:5000/ | -10.3. Optional variables +7.3. Optional variables | Name | Purpose | Example Value | | -------- | -------- | -------- | | MAIL_DEFAULT_SENDER | Bot email address. Required for sending internal emails following form completion on Contact Us page. | bot@partsltd.co.uk | @@ -69,12 +60,12 @@ NOTE: IF CONTACT US PAGE FORM FUNCTIONALITY IS REQUIRED, COMPLETE THIS STEP. | RECAPTCHA_PUBLIC_KEY | Public key for Google reCAPTCHA. Required for bot-prevention mesaures on Contact Us page form.| erDasdku8asdncuSAAS88... | | RECAPTCHA_PRIVATE_KEY | Private key for Google reCAPTCHA. Required for bot-prevention mesaures on Contact Us page form. | erDasdku8asdncuSAAS8... | -## 11. Host project -NOTE: DO EITHER 11.1 OR 11.2. -11.1. Host for local machine +## 8. Host project +NOTE: DO EITHER 8.1 OR 8.2. +8.1. Host for local machine - python3 -m flask run -11.2. Host for local network +8.2. Host for local network - python3 -m flask run --host=0.0.0.0 diff --git a/app.py b/app.py index 79640e41..7cba9681 100644 --- a/app.py +++ b/app.py @@ -10,11 +10,6 @@ Description: Initializes the Flask application, sets the configuration based on the environment, and defines two routes (/ and /about) that render templates with the specified titles. """ -# IMPORTS -# VARIABLE INSTANTIATION -# METHODS - -# IMPORTS # internal from config import app_config, Config from controllers.core import routes_core @@ -33,7 +28,6 @@ from extensions import db, csrf, mail, oauth from helpers.helper_app import Helper_App # external from flask import Flask, render_template, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session -# from flask_appconfig import AppConfig from flask_cors import CORS from flask_sqlalchemy import SQLAlchemy from flask_mail import Mail, Message @@ -46,17 +40,13 @@ from logging.handlers import RotatingFileHandler import traceback import logging -sys.path.insert(0, os.path.dirname(__file__)) # Todo: why? - +sys.path.insert(0, os.path.dirname(__file__)) app = Flask(__name__) -# AppConfig(app) -app.config.from_object(app_config) # for db init with required keys +app.config.from_object(app_config) app.app_config = app_config -# app.config["config"] = app_config() -# logging handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=3) handler.setLevel(logging.DEBUG) app.logger.addHandler(handler) @@ -82,6 +72,8 @@ def make_session_permanent(): session.permanent = True csrf = CSRFProtect() +csrf.init_app(app) + cors = CORS(app, resources={ r"/static/*": { "origins": [app.config["URL_HOST"]], @@ -89,15 +81,15 @@ cors = CORS(app, resources={ "max_age": 3600 } }) - -csrf.init_app(app) cors.init_app(app) + db.init_app(app) -mail.init_app(app) + oauth.init_app(app) +mail.init_app(app) + with app.app_context(): - # config = app.config["config"] db.create_all() db.engine.url = app.config['SQLALCHEMY_DATABASE_URI'] @@ -114,7 +106,6 @@ with app.app_context(): access_token_url = f'https://{app.config["DOMAIN_AUTH0"]}/oauth/token', ) - app.register_blueprint(routes_core) app.register_blueprint(routes_legal) app.register_blueprint(routes_store) @@ -138,9 +129,7 @@ def console_log(value): @app.after_request def add_cache_headers(response): if request.path.startswith('/static/'): - # Cache static assets response.headers['Cache-Control'] = 'public, max-age=31536000' else: - # No caching for dynamic content response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0' return response \ No newline at end of file diff --git a/business_objects/address.py b/business_objects/address.py index 6805fb66..ca0f46c9 100644 --- a/business_objects/address.py +++ b/business_objects/address.py @@ -37,8 +37,6 @@ class Address(db.Model, Base): county = db.Column(db.String(256)) active = db.Column(db.Boolean) - # region = None - def __init__(self): super().__init__() Base.__init__(self) diff --git a/business_objects/base.py b/business_objects/base.py index c2ea34d7..c512f474 100644 --- a/business_objects/base.py +++ b/business_objects/base.py @@ -67,10 +67,6 @@ class Base(): FLAG_VALUE_LOCAL_VAT_EXCL: ClassVar[str] = 'value_local_vat_excl' FLAG_VALUE_LOCAL_VAT_INCL: ClassVar[str] = 'value_local_vat_incl' FLAG_WEBSITE: ClassVar[str] = 'website' - """ - NAME_ATTR_OPTION_TEXT: ClassVar[str] = 'name-attribute-option-text' - NAME_ATTR_OPTION_VALUE: ClassVar[str] = 'name-attribute-option-value' - """ def __repr__(self): attrs = '\n'.join(f'{k}={v!r}' for k, v in self.__dict__.items()) return f'<{self.__class__.__name__}(\n{attrs}\n)>' diff --git a/business_objects/currency.py b/business_objects/currency.py index 28af019f..60cc3341 100644 --- a/business_objects/currency.py +++ b/business_objects/currency.py @@ -17,29 +17,6 @@ from lib import argument_validation as av # external from typing import ClassVar -# CLASSES -""" -class Currency_Enum(Enum): - GBP = 1 - - def text(self): - return Currency_Enum.Currency_Enum_Text(self) - - def Currency_Enum_Text(currency): - av.val_instance(currency, 'currency', 'Currency_Enum_Text', Currency_Enum) - if currency == Currency_Enum.GBP: - return 'GBP' - else: - # return 'Unknown' - raise ValueError("Unknown Currency Enum.") - - def get_member_by_text(text): - for member in Resolution_Level_Enum.__members__.values(): - if member.name == text: - return member - raise ValueError("Unknown Currency Enum.") - # return Resolution_Level_Enum.HIGH -""" class Currency(db.Model, Store_Base): FLAG_FACTOR_FROM_GBP: ClassVar[str] = 'factor-from-GBP' diff --git a/business_objects/db_base.py b/business_objects/db_base.py index b711a9fd..4307116b 100644 --- a/business_objects/db_base.py +++ b/business_objects/db_base.py @@ -32,28 +32,13 @@ class Get_Many_Parameters_Base(BaseModel, metaclass=ABCMeta): pass def to_json(self): return self.dict() - """ - @classmethod - @abstractmethod - def from_json(cls, json): - pass - """ - """ - @classmethod - @abstractmethod - def from_form(cls, form): - pass - """ -# db = SQLAlchemy() -# Base = declarative_base() class SQLAlchemy_ABCMeta(db.Model.__class__, ABCMeta): pass class SQLAlchemy_ABC(db.Model, metaclass=SQLAlchemy_ABCMeta): __abstract__ = True - # id = db.Column(db.Integer, primary_key=True) def __init__(self): pass def __repr__(self): @@ -63,10 +48,6 @@ class SQLAlchemy_ABC(db.Model, metaclass=SQLAlchemy_ABCMeta): @classmethod def from_json(cls, json): pass - """ - def to_json_option(self): - pass - """ def to_temporary_record(self): pass def to_object_with_missing_attributes(self, excluded_attributes): diff --git a/business_objects/region.py b/business_objects/region.py index b5029e06..466fd629 100644 --- a/business_objects/region.py +++ b/business_objects/region.py @@ -27,8 +27,6 @@ class Region(db.Model, Base): code = db.Column(db.String(50)) name = db.Column(db.String(200)) active = db.Column(db.Boolean) - - # region = None def __init__(self): super().__init__() diff --git a/business_objects/sql_error.py b/business_objects/sql_error.py index d7214428..bd99bada 100644 --- a/business_objects/sql_error.py +++ b/business_objects/sql_error.py @@ -22,11 +22,9 @@ import locale from flask_sqlalchemy import SQLAlchemy -# VARIABLE INSTANTIATION db = SQLAlchemy() -# CLASSES class SQL_Error(db.Model): display_order = db.Column(db.Integer, primary_key=True) id_type = db.Column(db.Integer) @@ -35,22 +33,6 @@ class SQL_Error(db.Model): name = db.Column(db.String(500)) description = db.Column(db.String(4000)) - """ - def __new__(cls, display_order, code, msg): - _m = 'SQL_Error.__new__' - v_arg_type = 'class attribute' - av.val_int(display_order, 'display_order', _m) - av.val_str(code, 'code', _m, max_len=50, v_arg_type=v_arg_type) - av.val_str(msg, 'msg', _m, max_len=4000, v_arg_type=v_arg_type) - return super(SQL_Error, cls).__new__(cls) - - def __init__(self, display_order, code, msg): - self.display_order = display_order - self.code = code - self.msg = msg - super().__init__() - """ - def from_DB_record(record): error = SQL_Error() error.display_order = record[0] diff --git a/business_objects/store/basket.py b/business_objects/store/basket.py deleted file mode 100644 index 574033d4..00000000 --- a/business_objects/store/basket.py +++ /dev/null @@ -1,184 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: Business Objects -Feature: Basket Business Object - -Description: -Business object for basket -""" - -# IMPORTS -# VARIABLE INSTANTIATION -# CLASSES -# METHODS - -# IMPORTS -# internal -import lib.argument_validation as av -# from lib import data_types -from business_objects.store.product import Product #, Parameters_Product -from business_objects.store.discount import Discount -from business_objects.store.delivery_option import Delivery_Option -from business_objects.store.store_base import Store_Base -# from forms import Form_Product -# from models.model_view_store import Model_View_Store # circular -# from datastores.datastore_store import DataStore_Store # circular -# from forms import Form_Basket_Add, Form_Basket_Edit # possibly circular -from helpers.helper_app import Helper_App -# external -# from enum import Enum -from flask import jsonify -import locale - - -# VARIABLE INSTANTIATION - -# CLASSES -class Basket_Item(): - product: Product - quantity: int - delivery_option: Delivery_Option - discounts: list - # form: Form_Product - is_included_VAT: bool - - """ - def __init__(self): - self.is_unavailable_in_currency_or_region = False - self.is_available = True - """ - - def from_product_and_quantity_and_VAT_included(product, quantity, is_included_VAT): - # Initialiser - validation - _m = 'Basket_Item.from_product_and_quantity' - v_arg_type = 'class attribute' - av.val_instance(product, 'product', _m, Product, v_arg_type=v_arg_type) - av.full_val_float(quantity, 'quantity', _m, product.get_quantity_min(), v_arg_type=v_arg_type) - basket_item = Basket_Item() - basket_item.product = product - basket_item.quantity = quantity - basket_item.is_included_VAT = is_included_VAT - return basket_item - - def add_product_price_discount(self, discount): - av.val_instance(discount, 'discount', 'Basket_Item.add_product_price_discount', Discount, v_arg_type='class attribute') - self.discounts.append(discount) - - def set_delivery_option(self, delivery_option): - av.val_instance(delivery_option, 'delivery_option', 'Basket_Item.set_delivery_option', Delivery_Option, v_arg_type='class attribute') - self.delivery_option = delivery_option - - def update_quantity(self, quantity): - _m = 'Basket_Item.update_quantity' - v_arg_type = 'class attribute' - av.full_val_float(quantity, 'quantity', _m, self.product.get_quantity_min(), v_arg_type=v_arg_type) - self.quantity = quantity - - def jsonify(self): - return jsonify(self) - - def to_json(self): - permutation = self.product.get_permutation_selected() - return { - 'product_id': self.product.id_product, - 'permutation_id': permutation.id_permutation, - 'price': permutation.output_price(self.is_included_VAT), - 'quantity': self.quantity - } - - def get_subtotal(self): - permutation = self.product.get_permutation_selected() - return round(self.product.get_price_local(self.is_included_VAT) * self.quantity, 2) if permutation.is_available else 0 - - def output_currency(self): - return self.product.output_currency() - - def output_subtotal(self): - locale.setlocale(locale.LC_ALL, '') - subtotal = self.get_subtotal() - permutation = self.product.get_permutation_selected() - return 'Not available in this currency or region' if permutation.is_unavailable_in_currency_or_region else 'Not available' if not permutation.is_available else f'{self.product.output_currency()} {locale.format_string("%d", subtotal, grouping=True)}' - - def __repr__(self): - return f''' - product: {self.product} - quantity: {self.quantity} - subtotal: {self.get_subtotal()} - ''' - -class Basket(Store_Base): - KEY_BASKET: str = 'basket' - KEY_ID_CURRENCY: str = 'id_currency' - KEY_ID_REGION_DELIVERY: str = 'id_region_delivery' - KEY_IS_INCLUDED_VAT: str = 'is_included_VAT' - KEY_ITEMS: str = 'items' - items: list - is_included_VAT: bool - id_region_delivery: int - id_currency: int - - def __init__(self, is_included_VAT, id_currency, id_region_delivery): - self.items = [] - self.is_included_VAT = is_included_VAT - self.id_currency = id_currency - self.id_region_delivery = id_region_delivery - def add_item(self, item): - av.val_instance(item, 'item', 'Basket.add_item', Basket_Item) - self.items.append(item) - def to_csv(self): - ids_permutation = '' - quantities_permutation = '' - for b_i in range(len(self.items)): - basket_item = self.items[b_i] - product = basket_item.product - if b_i > 0: - ids_permutation += ',' - quantities_permutation += ',' - ids_permutation += str(product.get_id_permutation()) - quantities_permutation += str(basket_item.quantity) - Helper_App.console_log(f'ids_permutation_basket = {ids_permutation}') - Helper_App.console_log(f'quantities_permutation_basket = {quantities_permutation}') - return ids_permutation, quantities_permutation - def to_json_list(self): - json_list = [] - for item in self.items: - json_list.append(item.to_json()) - return json_list - def to_json(self): - return { - **self.get_shared_json_attributes(self), - Basket.KEY_ITEMS: self.to_json_list(), - Basket.KEY_IS_INCLUDED_VAT: self.is_included_VAT, - Basket.KEY_ID_CURRENCY: self.id_currency, - Basket.KEY_ID_REGION_DELIVERY: self.id_region_delivery - } - def output_total(self): - sum = 0 - for b_i in self.items: - sum += b_i.get_subtotal() - symbol = self.items[0].output_currency() if len(self.items) > 0 else '' - - return f'{symbol} {locale.format_string("%d", sum, grouping=True)}' - def len(self): - return len(self.items) - """ - def get_key_product_index_from_ids_product_permutation(id_product, id_permutation): - return f'{id_product},{"" if id_permutation is None else id_permutation}' - """ - def __repr__(self): - repr = f'Basket:' - for basket_item in self.items: - Helper_App.console_log(f'{basket_item}') - repr = f'{repr}\n{basket_item}' - return repr - - def get_ids_permutation_unavailable(self): - ids_permutation = [] - for item in self.items: - permutation = item.product.get_permutation_selected() - if not permutation.is_available: - ids_permutation.append(permutation.id_permutation) - return ids_permutation \ No newline at end of file diff --git a/business_objects/store/delivery_option.py b/business_objects/store/delivery_option.py deleted file mode 100644 index 2dd7ab6c..00000000 --- a/business_objects/store/delivery_option.py +++ /dev/null @@ -1,98 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: Business Objects -Feature: Product Delivery Option Business Object - -Description: -Business object for delivery option -""" - -# internal -from extensions import db - - -# CLASSES -""" -class Delivery_Option(): - name: str - delay_min: int # days - delay_max: int - quantity_min: float - quantity_max: float - regions: list # [Enum_Region] - cost: float - - def __new__(cls, name, delay_min, delay_max, quantity_min, quantity_max, regions, cost): - _m = 'Delivery_Option.__new__' - v_arg_type = 'class attribute' - av.val_str(name, 'name', _m, v_arg_type = v_arg_type) - av.val_int(delay_min, 'delay_min', _m, 0, v_arg_type = v_arg_type) - av.val_int(delay_max, 'delay_max', _m, 0, v_arg_type = v_arg_type) - av.val_float(quantity_min, 'quantity_min', _m, 0, v_arg_type = v_arg_type) - av.val_float(quantity_max, 'quantity_max', _m, 0, v_arg_type = v_arg_type) - av.val_list_instances(regions, 'regions', _m, Enum_Region, v_arg_type = v_arg_type) - av.val_float(cost, 'cost', _m, 0, v_arg_type = v_arg_type) - return super(Delivery_Option, cls).__new__(cls) - - def __init__(self, name, delay_min, delay_max, quantity_min, quantity_max, regions, cost): - self.name = name - self.delay_min = delay_min - self.delay_max = delay_max - self.quantity_min = quantity_min - self.quantity_max = quantity_max - self.regions = regions - self.cost = cost -""" -class Delivery_Option(db.Model): - id_option = db.Column(db.Integer, primary_key=True) - id_product = db.Column(db.Integer) - id_permutation = db.Column(db.Integer) - id_category = db.Column(db.Integer) - code = db.Column(db.String(50)) - name = db.Column(db.String(100)) - latency_min = db.Column(db.Integer) - latency_max = db.Column(db.Integer) - quantity_min = db.Column(db.Integer) - quantity_max = db.Column(db.Integer) - codes_region = db.Column(db.String(4000)) - names_region = db.Column(db.String(4000)) - price_GBP = db.Column(db.Float) - display_order = db.Column(db.Integer) - def __init__(self): - self.delivery_regions = [] - def from_DB_get_many_product_catalogue(query_row): - option = Delivery_Option() - option.id_option = query_row[0] - option.id_product = query_row[1] - option.id_permutation = query_row[2] - option.id_category = query_row[3] - option.code = query_row[4] - option.name = query_row[5] - option.latency_min = query_row[6] - option.latency_max = query_row[7] - option.quantity_min = query_row[8] - option.quantity_max = query_row[9] - option.codes_region = query_row[10] - option.names_region = query_row[11] - option.price_GBP = query_row[12] - option.display_order = query_row[13] - return option - def __repr__(self): - return f''' - id: {self.id_option} - id_product: {self.id_product} - id_category: {self.id_category} - name: {self.name} - code: {self.code} - latency_min: {self.latency_min} - latency_max: {self.latency_max} - quantity_min: {self.quantity_min} - quantity_max: {self.quantity_max} - codes_region: {self.codes_region} - names_region: {self.names_region} - price_GBP: {self.price_GBP} - display_order: {self.display_order} - ''' diff --git a/business_objects/store/image.py b/business_objects/store/image.py index 8b4d301c..96b46f54 100644 --- a/business_objects/store/image.py +++ b/business_objects/store/image.py @@ -59,25 +59,7 @@ class Image(db.Model, Store_Base): url = db.Column(db.String(255)) active = db.Column(db.Boolean) display_order = db.Column(db.Integer) - """ - def __new__(cls, id, id_product, id_category, url, display_order): - _m = 'Image.__new__' - v_arg_type = 'class attribute' - av.val_int(id, 'id', _m, 0, v_arg_type=v_arg_type) - av.val_int(id_product, 'id_product', _m, 0, v_arg_type=v_arg_type) - av.val_int(id_category, 'id_category', _m, 0, v_arg_type=v_arg_type) - av.val_str(url, 'url', _m, max_len=254, v_arg_type=v_arg_type) - av.val_int(display_order, 'display_order', _m, v_arg_type=v_arg_type) - return super(Image, cls).__new__(cls) - def __init__(self, id, id_product, id_category, url, display_order): - self.id_image = id - self.id_product = id_product - self.id_category = id_category - self.url = url - self.display_order = display_order - super().__init__() -""" def from_DB_get_many_product_catalogue(query_row): _m = 'Image.from_DB_get_many_product_catalogue' # Helper_App.console_log(f'image: {query_row}') diff --git a/business_objects/store/manufacturing_purchase_order.py b/business_objects/store/manufacturing_purchase_order.py index cd7d6a2e..647c935e 100644 --- a/business_objects/store/manufacturing_purchase_order.py +++ b/business_objects/store/manufacturing_purchase_order.py @@ -302,12 +302,6 @@ class Manufacturing_Purchase_Order_Product_Link_Temp(db.Model, Store_Base): id_unit_latency_manufacture = db.Column(db.Integer) latency_manufacture: int = db.Column(db.Integer) display_order: int = db.Column(db.Integer) - """ - cost_unit_local_VAT_excl: float = db.Column(db.Float) - cost_unit_local_VAT_incl: float = db.Column(db.Float) - price_unit_local_VAT_excl: float = db.Column(db.Float) - price_unit_local_VAT_incl: float = db.Column(db.Float) - """ active: bool = db.Column(db.Boolean) guid: str = db.Column(db.String(36)) def __init__(self): @@ -328,12 +322,6 @@ class Manufacturing_Purchase_Order_Product_Link_Temp(db.Model, Store_Base): row.id_unit_latency_manufacture = manufacturing_purchase_order_product_link.id_unit_latency_manufacture row.latency_manufacture = manufacturing_purchase_order_product_link.latency_manufacture row.display_order = manufacturing_purchase_order_product_link.display_order - """ - row.cost_unit_local_VAT_excl = manufacturing_purchase_order_product_link.cost_unit_local_VAT_excl - row.cost_unit_local_VAT_incl = manufacturing_purchase_order_product_link.cost_unit_local_VAT_incl - row.price_unit_local_VAT_excl = manufacturing_purchase_order_product_link.price_unit_local_VAT_excl - row.price_unit_local_VAT_incl = manufacturing_purchase_order_product_link.price_unit_local_VAT_incl - """ row.active = 1 if manufacturing_purchase_order_product_link.active else 0 return row def __repr__(self): diff --git a/business_objects/store/order.py b/business_objects/store/order.py deleted file mode 100644 index 12e02bb8..00000000 --- a/business_objects/store/order.py +++ /dev/null @@ -1,89 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: Business Objects -Feature: Order Business Object - -Description: -Business object for order -""" - -# internal -import lib.argument_validation as av -# from lib import data_types -from business_objects.store.product import Product -from business_objects.store.delivery_option import Delivery_Option -from business_objects.store.store_base import Store_Base -# from forms import Form_Product -# from models.model_view_store import Model_View_Store # circular -# external -# from enum import Enum -from flask import jsonify -import locale - - -# VARIABLE INSTANTIATION - -# CLASSES -class Order(Store_Base): - category: str - product: Product - quantity: int - subtotal: float - delivery_option: Delivery_Option - # form: Form_Product - - def __new__(cls, category, product, quantity): - # Initialiser - validation - _m = 'Product.__new__' - v_arg_type = 'class attribute' - av.val_str(category, 'category', _m, v_arg_type=v_arg_type) - av.val_instance(product, 'product', _m, Product, v_arg_type=v_arg_type) - av.full_val_float(quantity, 'quantity', _m, product.quantity_min, v_arg_type=v_arg_type) - return super(Basket_Item, cls).__new__(cls) - - def __init__(self, category, product, quantity): - # Constructor - self.category = category - self.product = product - self.quantity = quantity - self.subtotal = round(self.product.price_GBP_full * self.quantity, 2) - """ - self.form = Form_Product() - if self.form.validate_on_submit(): - # Handle form submission - - pass - """ - - def update_quantity(self, quantity): - _m = 'Basket_Item.update_quantity' - v_arg_type = 'class attribute' - av.full_val_float(quantity, 'quantity', _m, self.product.quantity_min, v_arg_type=v_arg_type) - self.quantity = quantity - self.subtotal = round(self.product.price_GBP_full * self.quantity, 2) - - def jsonify(self): - return jsonify(self) - - def to_json(self): - return { - **self.get_shared_json_attributes(self), - 'product_id': self.product.id_product, - 'price': self.product.price_GBP_full, - 'quantity': self.quantity - } - - def output_subtotal(self): - locale.setlocale(locale.LC_ALL, '') - return locale.format_string("%d", self.subtotal, grouping=True) - - def __repr__(self): - return f''' - category: {self.category} - product: {self.product} - quantity: {self.quantity} - subtotal: {self.subtotal} - ''' \ No newline at end of file diff --git a/business_objects/store/product.py b/business_objects/store/product.py index 4888216d..de6db3e7 100644 --- a/business_objects/store/product.py +++ b/business_objects/store/product.py @@ -32,28 +32,7 @@ from helpers.helper_app import Helper_App # external from dataclasses import dataclass from typing import ClassVar, List -""" -class Enum_Status_Stock(Enum): - OUT = 0 - LOW = 1 - IN = 99 - def text(self): - return Enum_Status_Stock.Enum_Status_Stock_Text(self) - - def Enum_Status_Stock_Text(status): - av.val_instance(status, 'category', 'Enum_Status_Stock_Text', Enum_Status_Stock) - if status == Enum_Status_Stock.OUT: - return 'Out of stock' - elif status == Enum_Status_Stock.LOW: - return 'Low on stock' - else: - return 'Fully stocked' - - def get_member_by_text(text): - return data_types.get_enum_member_by_text(Enum_Status_Stock, text.upper()) - -""" class Product(SQLAlchemy_ABC, Store_Base): NAME_ATTR_OPTION_VALUE: ClassVar[str] = Store_Base.ATTR_ID_PRODUCT NAME_ATTR_OPTION_TEXT = Store_Base.FLAG_NAME @@ -70,16 +49,6 @@ class Product(SQLAlchemy_ABC, Store_Base): can_view = db.Column(db.Boolean) can_edit = db.Column(db.Boolean) can_admin = db.Column(db.Boolean) - # form_basket_add: Form_Basket_Add - # form_basket_edit: Form_Basket_Edit - # has_variations: bool - # index_permutation_selected: int - - """ - permutations: list = None - permutation_index: dict = None - variation_trees: list = None - """ def __init__(self): self.permutations = [] @@ -109,24 +78,6 @@ class Product(SQLAlchemy_ABC, Store_Base): product.can_edit = av.input_bool(query_row[9], "can_edit", _m, v_arg_type=v_arg_type) product.can_admin = av.input_bool(query_row[10], "can_admin", _m, v_arg_type=v_arg_type) return product - """ - def from_permutation(permutation, has_variations = False): - _m = 'Product.from_permutation' - v_arg_type = 'class attribute' - av.val_instance(permutation, 'permutation', _m, Product_Permutation, v_arg_type=v_arg_type) - product = Product() - product.has_variations = has_variations - product.index_permutation_selected = 0 - product.id_product = permutation.id_product - product.id_category = permutation.id_category - product.display_order = permutation.display_order - product.can_view = permutation.can_view - product.can_edit = permutation.can_edit - product.can_admin = permutation.can_admin - product.permutations.append(permutation) - # product.get_variation_trees() - return product - """ def add_product_permutation(self, permutation): _m = 'Product.add_product_permutation' av.val_instance(permutation, 'permutation', _m, Product_Permutation) @@ -136,36 +87,9 @@ class Product(SQLAlchemy_ABC, Store_Base): except KeyError: self.permutation_index[permutation.id_permutation] = len(self.permutations) self.permutations.append(permutation) - """ - if self.has_variations: - self.has_variations = False - """ if self.index_permutation_selected is None: self.index_permutation_selected = self.permutation_index[permutation.id_permutation] - Helper_App.console_log(f'setting selected permutation for product {self.id_product} to {self.index_permutation_selected}') # :\n{self.permutations[self.index_permutation_selected]} - """ - def from_permutations(permutations): - _m = 'Product.from_permutations' - v_arg_type = 'class attribute' - if len(permutations) == 0: - raise ValueError(av.error_msg_str(permutations, 'permutations', _m, list, v_arg_type=v_arg_type)) - product = Product() - product.has_variations = True - product.index_permutation_selected = 0 - product.id_product = permutations[0].id_product - product.id_category = permutations[0].id_category - product.display_order = permutations[0].display_order - product.can_view = True - product.can_edit = True - product.can_admin = True - for permutation in permutations: - product.can_view &= permutation.can_view - product.can_edit &= permutation.can_edit - product.can_admin &= permutation.can_admin - product.permutations.append(permutations) - product.get_variation_trees() - return product - """ + Helper_App.console_log(f'setting selected permutation for product {self.id_product} to {self.index_permutation_selected}') def get_variation_trees(self): self.variation_trees = [] for index_permutation in range(len(self.permutations)): @@ -186,12 +110,6 @@ class Product(SQLAlchemy_ABC, Store_Base): permutation = Product_Permutation.from_DB_Stripe_price(query_row) product = Product.from_permutation(permutation) return product - """ - def from_json(json_basket_item, key_id_product, key_id_permutation): - permutation = Product_Permutation.from_json(json_basket_item, key_id_product, key_id_permutation) - product = Product.from_permutation(permutation) - return product - """ @classmethod def from_json(cls, json): product = cls() @@ -242,13 +160,7 @@ class Product(SQLAlchemy_ABC, Store_Base): return self.get_permutation_selected().description def output_currency(self): return self.get_permutation_selected().get_price().symbol_currency - """ - def add_form_basket_add(self): - self.form_basket_add = None - - def add_form_basket_edit(self): - self.form_basket_edit = None - """ + def __repr__(self): return f'''Product id_product: {self.id_product} @@ -263,23 +175,7 @@ class Product(SQLAlchemy_ABC, Store_Base): variation trees: {self.variation_trees} active: {self.active} ''' - """ - def get_index_permutation_from_id(self, id_permutation): - if id_permutation is None and not self.has_variations: - return 0 - for index_permutation in range(len(self.permutations)): - permutation = self.permutations[index_permutation] - if permutation.id_permutation == id_permutation: - return index_permutation - raise ValueError(f"{av.error_msg_str(id_permutation, 'id_permutation', 'Product.get_index_permutation_from_id', int)}\nPermutation ID not found.") - """ - """ - def add_product_variation(self, variation): - av.val_instance(variation, 'variation', 'Product.add_product_variation', Product_Variation) - # Helper_App.console_log(f'variation: {variation}') - index_permutation = self.permutation_index[variation.id_permutation] # self.get_index_permutation_from_id(variation.id_permutation) - self.permutations[index_permutation].add_product_variation(variation) - """ + def add_product_variation_type(self, variation_type): variation = variation_type.variations[0] index_permutation = self.permutation_index[variation.id_permutation] @@ -318,27 +214,7 @@ class Product(SQLAlchemy_ABC, Store_Base): for permutation in self.permutations: list_rows.append(permutation.to_row_permutation()) return list_rows - """ - @classmethod - def from_json(cls, json): - product = cls() - product.id_product = json[cls.ATTR_ID_PRODUCT] - product.id_category = json[cls.ATTR_ID_PRODUCT_CATEGORY] - product.name = json[cls.FLAG_NAME] - product.display_order = json[cls.FLAG_DISPLAY_ORDER] - product.can_view = json[cls.FLAG_CAN_VIEW] - product.can_edit = json[cls.FLAG_CAN_EDIT] - product.can_admin = json[cls.FLAG_CAN_ADMIN] - product.has_variations = json[cls.FLAG_HAS_VARIATIONS] - product.index_permutation_selected = json[cls.FLAG_INDEX_PERMUTATION_SELECTED] - product.permutations = [] - for json_permutation in json[cls.ATTR_ID_PRODUCT_PERMUTATION]: - product.permutations.append(Product_Permutation.from_json(json_permutation)) - product.variation_trees = [] - for json_tree in json[cls.FLAG_PRODUCT_VARIATION_TREES]: - product.variation_trees.append(Product_Variation_Tree.from_json(json_tree)) - return product - """ + def to_json(self): return { **self.get_shared_json_attributes(self), @@ -366,22 +242,7 @@ class Product(SQLAlchemy_ABC, Store_Base): if type not in list_types: list_types.append(type) return list_types - """ - def get_json_str_types_variation_trees(self): - json_str = '' - for tree in self.variation_trees: - if json_str != '': - json_str += '\n' - json_str += tree.get_json_str_types() - return json_str - def get_text_input_variation_types(self): - text_input = '' - for tree in self.variation_trees: - if text_input != '': - text_input += '\n' - text_input += tree.get_text_input_types() - return text_input - """ + def get_csv_ids_permutation(self): csv = '' for permutation in self.permutations: @@ -392,36 +253,18 @@ class Product(SQLAlchemy_ABC, Store_Base): class Parameters_Product(Get_Many_Parameters_Base): - # id_user: str get_all_product_category: bool get_inactive_product_category: bool - # get_first_product_category_only: bool ids_product_category: str get_all_product: bool get_inactive_product: bool - # get_first_product_only: bool ids_product: str get_all_permutation: bool get_inactive_permutation: bool - # get_first_permutation_only: bool ids_permutation: str get_all_image: bool get_inactive_image: bool - # get_first_image_only: bool ids_image: str - """ - get_all_region: bool - get_inactive_region: bool - get_first_region_only: bool - ids_region: str - get_all_currency: bool - get_inactive_currency: bool - get_first_currency_only: bool - ids_currency: str - get_all_discount: bool - get_inactive_discount: bool - ids_discount: str - """ get_products_quantity_stock_below_min: bool def to_json(self): @@ -429,37 +272,21 @@ class Parameters_Product(Get_Many_Parameters_Base): # 'a_id_user': None, 'a_get_all_product_category': self.get_all_product_category, 'a_get_inactive_product_category': self.get_inactive_product_category, - # 'a_get_first_product_category_only': self.get_first_product_category_only, 'a_ids_product_category': self.ids_product_category, 'a_get_all_product': self.get_all_product, 'a_get_inactive_product': self.get_inactive_product, - # 'a_get_first_product_only': self.get_first_product_only, 'a_ids_product': self.ids_product, 'a_get_all_permutation': self.get_all_permutation, 'a_get_inactive_permutation': self.get_inactive_permutation, - # 'a_get_first_permutation_only': self.get_first_permutation_only, 'a_ids_permutation': self.ids_permutation, 'a_get_all_image': self.get_all_image, 'a_get_inactive_image': self.get_inactive_image, - # 'a_get_first_image_only': self.get_first_image_only, 'a_ids_image': self.ids_image, - # 'a_get_all_delivery_region': self.get_all_region, - # 'a_get_inactive_delivery_region': self.get_inactive_region, - # 'a_get_first_delivery_region_only': self.get_first_region_only, - # 'a_ids_delivery_region': self.ids_region, - # 'a_get_all_currency': self.get_all_currency, - # 'a_get_inactive_currency': self.get_inactive_currency, - # 'a_get_first_currency_only': self.get_first_currency_only, - # 'a_ids_currency': self.ids_currency, - # 'a_get_all_discount': self.get_all_discount, - # 'a_get_inactive_discount': self.get_inactive_discount, - # 'a_ids_discount': self.ids_discount, 'a_get_products_quantity_stock_below_min': self.get_products_quantity_stock_below_min } @staticmethod def from_form_filters_product(form): - # if not (form is Filters_Product_Permutation): raise ValueError(f'Invalid form type: {type(form)}') av.val_instance(form, 'form', 'Parameters_Product.from_form', Filters_Product) has_filter_category = not (form.id_category.data == '0' or form.id_category.data == '' or form.id_category.data is None) is_not_empty = av.input_bool(form.is_not_empty.data, "is_not_empty", "Parameters_Product.from_form_filters_product") @@ -467,37 +294,20 @@ class Parameters_Product(Get_Many_Parameters_Base): return Parameters_Product( get_all_product_category = not has_filter_category, get_inactive_product_category = not active, - # get_first_product_category_only = False, ids_product_category = form.id_category.data if form.id_category.data is not None else '', get_all_product = True, get_inactive_product = not active, - # get_first_product_only = False, ids_product = '', get_all_permutation = True, get_inactive_permutation = not active, - # get_first_permutation_only = False, ids_permutation = '', get_all_image = False, get_inactive_image = False, - # get_first_image_only = False, ids_image = '', - # get_all_region = False, - # get_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = False, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = False, - # get_inactive_discount = False, - # ids_discount = '', get_products_quantity_stock_below_min = False ) @staticmethod def from_form_filters_product_permutation(form): - # if not (form is Filters_Product_Permutation): raise ValueError(f'Invalid form type: {type(form)}') - # av.val_instance(form, 'form', 'Parameters_Product.from_form', Filters_Product_Permutation) has_category_filter = not (form.id_category.data == '0' or form.id_category.data == '' or form.id_category.data is None) has_product_filter = not (form.id_product.data == '0' or form.id_product.data == '' or form.id_product.data is None) get_permutations_stock_below_min = av.input_bool(form.is_out_of_stock.data, "is_out_of_stock", "Parameters_Product.from_form_filters_product_permutation") @@ -506,31 +316,16 @@ class Parameters_Product(Get_Many_Parameters_Base): return Parameters_Product( get_all_product_category = not has_category_filter, get_inactive_product_category = get_inactive, - # get_first_product_category_only = False, ids_product_category = form.id_category.data if form.id_category.data is not None else '', get_all_product = not has_product_filter, get_inactive_product = get_inactive, - # get_first_product_only = False, ids_product = form.id_product.data if form.id_product.data is not None else '', get_all_permutation = not get_permutations_stock_below_min, get_inactive_permutation = get_inactive, - # get_first_permutation_only = False, ids_permutation = '', get_all_image = False, get_inactive_image = False, - # get_first_image_only = False, ids_image = '', - # get_all_region = False, - # get_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = False, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = False, - # get_inactive_discount = False, - # ids_discount = '', get_products_quantity_stock_below_min = get_permutations_stock_below_min ) @@ -539,31 +334,16 @@ class Parameters_Product(Get_Many_Parameters_Base): return Parameters_Product( get_all_product_category = True, get_inactive_product_category = False, - # get_first_product_category_only = False, ids_product_category = '', get_all_product = True, get_inactive_product = False, - # get_first_product_only = False, ids_product = '', get_all_permutation = True, get_inactive_permutation = False, - # get_first_permutation_only = False, ids_permutation = '', get_all_image = True, get_inactive_image = False, - # get_first_image_only = False, ids_image = '', - # get_all_region = True, - # et_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = True, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = True, - # get_inactive_discount = False, - # ids_discount = '', get_products_quantity_stock_below_min = True ) @@ -572,31 +352,16 @@ class Parameters_Product(Get_Many_Parameters_Base): return cls( get_all_product_category = json.get('a_get_all_product_category', False), get_inactive_product_category = json.get('a_get_inactive_product_category', False), - # get_first_product_category_only = json.get('a_get_first_product_category_only', False), ids_product_category = json.get('a_ids_product_category', ''), get_all_product = json.get('a_get_all_product', False), get_inactive_product = json.get('a_get_inactive_product', False), - # get_first_product_only = json.get('a_get_first_product_only', False), ids_product = json.get('a_ids_product', ''), get_all_permutation = json.get('a_get_all_permutation', False), get_inactive_permutation = json.get('a_get_inactive_permutation', False), - # get_first_permutation_only = json.get('a_get_first_permutation_only', False), ids_permutation = json.get('a_ids_permutation', ''), get_all_image = json.get('a_get_all_image', False), get_inactive_image = json.get('a_get_inactive_image', False), - # get_first_image_only = json.get('a_get_first_image_only', False), ids_image = json.get('a_ids_image', ''), - # get_all_region = json.get('a_get_all_region', False), - # get_inactive_region = json.get('a_get_inactive_region', False), - # get_first_region_only = json.get('a_get_first_region_only', False), - # ids_region = json.get('a_ids_region', ''), - # get_all_currency = json.get('a_get_all_currency', False), - # get_inactive_currency = json.get('a_get_inactive_currency', False), - # get_first_currency_only = json.get('a_get_first_currency_only', False), - # ids_currency = json.get('a_ids_currency', ''), - # get_all_discount = json.get('a_get_all_discount', False), - # get_inactive_discount = json.get('a_get_inactive_discount', False), - # ids_discount = json.get('a_ids_discount', ''), get_products_quantity_stock_below_min = json.get('a_get_products_quantity_stock_below_min', False) ) @@ -621,242 +386,6 @@ class Parameters_Product(Get_Many_Parameters_Base): def from_filters_stock_item(cls, filters_stock_item): return cls.from_form_filters_product_permutation(filters_stock_item) -""" -class Parameters_Product(Get_Many_Parameters_Base): - # id_user: str - get_all_product_category: bool - get_inactive_product_category: bool - # get_first_product_category_only: bool - ids_product_category: str - get_all_product: bool - get_inactive_product: bool - # get_first_product_only: bool - ids_product: str - get_all_permutation: bool - get_inactive_permutation: bool - # get_first_permutation_only: bool - ids_permutation: str - get_all_image: bool - get_inactive_image: bool - # get_first_image_only: bool - ids_image: str - "" - get_all_region: bool - get_inactive_region: bool - get_first_region_only: bool - ids_region: str - get_all_currency: bool - get_inactive_currency: bool - get_first_currency_only: bool - ids_currency: str - get_all_discount: bool - get_inactive_discount: bool - ids_discount: str - "" - get_products_quantity_stock_below_min: bool - - def __init__(self, a_id_user, **kwargs): - super().__init__(a_id_user, **kwargs) - - def to_json(self): - return { - 'a_id_user': self.a_id_user, - 'a_get_all_product_category': self.get_all_product_category, - 'a_get_inactive_product_category': self.get_inactive_product_category, - # 'a_get_first_product_category_only': self.get_first_product_category_only, - 'a_ids_product_category': self.ids_product_category, - 'a_get_all_product': self.get_all_product, - 'a_get_inactive_product': self.get_inactive_product, - # 'a_get_first_product_only': self.get_first_product_only, - 'a_ids_product': self.ids_product, - 'a_get_all_permutation': self.get_all_permutation, - 'a_get_inactive_permutation': self.get_inactive_permutation, - # 'a_get_first_permutation_only': self.get_first_permutation_only, - 'a_ids_permutation': self.ids_permutation, - 'a_get_all_image': self.get_all_image, - 'a_get_inactive_image': self.get_inactive_image, - # 'a_get_first_image_only': self.get_first_image_only, - 'a_ids_image': self.ids_image, - # 'a_get_all_delivery_region': self.get_all_region, - # 'a_get_inactive_delivery_region': self.get_inactive_region, - # 'a_get_first_delivery_region_only': self.get_first_region_only, - # 'a_ids_delivery_region': self.ids_region, - # 'a_get_all_currency': self.get_all_currency, - # 'a_get_inactive_currency': self.get_inactive_currency, - # 'a_get_first_currency_only': self.get_first_currency_only, - # 'a_ids_currency': self.ids_currency, - # 'a_get_all_discount': self.get_all_discount, - # 'a_get_inactive_discount': self.get_inactive_discount, - # 'a_ids_discount': self.ids_discount, - 'a_get_products_quantity_stock_below_min': self.get_products_quantity_stock_below_min - } - - @staticmethod - def from_form_filters_product(form, id_user): - # if not (form is Filters_Product_Permutation): raise ValueError(f'Invalid form type: {type(form)}') - av.val_instance(form, 'form', 'Parameters_Product.from_form', Filters_Product) - has_filter_category = not (form.id_category.data == '0' or form.id_category.data == '') - is_not_empty = av.input_bool(form.is_not_empty.data, "is_not_empty", "Parameters_Product.from_form_filters_product") - active = av.input_bool(form.active.data, "active", "Parameters_Product.from_form_filters_product") - return Parameters_Product( - a_id_user = id_user, - get_all_product_category = not has_filter_category, - get_inactive_product_category = not active, - # get_first_product_category_only = False, - ids_product_category = str(form.id_category.data), - get_all_product = True, - get_inactive_product = not active, - # get_first_product_only = False, - ids_product = '', - get_all_permutation = True, - get_inactive_permutation = not active, - # get_first_permutation_only = False, - ids_permutation = '', - get_all_image = False, - get_inactive_image = False, - # get_first_image_only = False, - ids_image = '', - # get_all_region = False, - # get_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = False, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = False, - # get_inactive_discount = False, - # ids_discount = '', - get_products_quantity_stock_below_min = False - ) - @staticmethod - def from_form_filters_product_permutation(form): - # if not (form is Filters_Product_Permutation): raise ValueError(f'Invalid form type: {type(form)}') - # av.val_instance(form, 'form', 'Parameters_Product.from_form', Form_Base) - has_category_filter = not (form.id_category.data is None or form.id_category.data == '0' or form.id_category.data == '') - has_product_filter = not (form.id_product.data is None or form.id_product.data == '0' or form.id_product.data == '') - get_permutations_stock_below_min = av.input_bool(form.is_out_of_stock.data, "is_out_of_stock", "Parameters_Product.from_form") - Helper_App.console_log(f'form question: {type(form.is_out_of_stock)}\nbool interpretted: {get_permutations_stock_below_min}\type form: {type(form)}') - return Parameters_Product( - get_all_product_category = not has_category_filter, - get_inactive_product_category = False, - # get_first_product_category_only = False, - ids_product_category = str(form.id_category.data) if has_category_filter else '', - get_all_product = not has_product_filter, - get_inactive_product = False, - # get_first_product_only = False, - ids_product = str(form.id_product.data) if has_product_filter else '', - get_all_permutation = not get_permutations_stock_below_min, - get_inactive_permutation = False, - # get_first_permutation_only = False, - ids_permutation = '', - get_all_image = False, - get_inactive_image = False, - # get_first_image_only = False, - ids_image = '', - # get_all_region = False, - # get_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = False, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = False, - # get_inactive_discount = False, - # ids_discount = '', - get_products_quantity_stock_below_min = get_permutations_stock_below_min - ) - - @classmethod - def get_default(cls, id_user): - return cls( - a_id_user = id_user, - get_all_product_category = True, - get_inactive_product_category = False, - # get_first_product_category_only = False, - ids_product_category = '', - get_all_product = True, - get_inactive_product = False, - # get_first_product_only = False, - ids_product = '', - get_all_permutation = True, - get_inactive_permutation = False, - # get_first_permutation_only = False, - ids_permutation = '', - get_all_image = True, - get_inactive_image = False, - # get_first_image_only = False, - ids_image = '', - # get_all_region = True, - # et_inactive_region = False, - # get_first_region_only = False, - # ids_region = '', - # get_all_currency = True, - # get_inactive_currency = False, - # get_first_currency_only = False, - # ids_currency = '', - # get_all_discount = True, - # get_inactive_discount = False, - # ids_discount = '', - get_products_quantity_stock_below_min = True - ) - - @classmethod - def from_json(cls, json): - return cls( - get_all_product_category = json.get('a_get_all_product_category', False), - get_inactive_product_category = json.get('a_get_inactive_product_category', False), - # get_first_product_category_only = json.get('a_get_first_product_category_only', False), - ids_product_category = json.get('a_ids_product_category', ''), - get_all_product = json.get('a_get_all_product', False), - get_inactive_product = json.get('a_get_inactive_product', False), - # get_first_product_only = json.get('a_get_first_product_only', False), - ids_product = json.get('a_ids_product', ''), - get_all_permutation = json.get('a_get_all_permutation', False), - get_inactive_permutation = json.get('a_get_inactive_permutation', False), - # get_first_permutation_only = json.get('a_get_first_permutation_only', False), - ids_permutation = json.get('a_ids_permutation', ''), - get_all_image = json.get('a_get_all_image', False), - get_inactive_image = json.get('a_get_inactive_image', False), - # get_first_image_only = json.get('a_get_first_image_only', False), - ids_image = json.get('a_ids_image', ''), - # get_all_region = json.get('a_get_all_region', False), - # get_inactive_region = json.get('a_get_inactive_region', False), - # get_first_region_only = json.get('a_get_first_region_only', False), - # ids_region = json.get('a_ids_region', ''), - # get_all_currency = json.get('a_get_all_currency', False), - # get_inactive_currency = json.get('a_get_inactive_currency', False), - # get_first_currency_only = json.get('a_get_first_currency_only', False), - # ids_currency = json.get('a_ids_currency', ''), - # get_all_discount = json.get('a_get_all_discount', False), - # get_inactive_discount = json.get('a_get_inactive_discount', False), - # ids_discount = json.get('a_ids_discount', ''), - get_products_quantity_stock_below_min = json.get('a_get_products_quantity_stock_below_min', False) - ) - - @classmethod - def from_filters_product_category(cls, filters_category): - return cls( - get_all_product_category = True, - get_inactive_product_category = filters_category.active.data, - ids_product_category = '', - get_all_product = True, - get_inactive_product = False, - ids_product = '', - get_all_permutation = True, - get_inactive_permutation = False, - ids_permutation = '', - get_all_image = False, - get_inactive_image = False, - ids_image = '', - get_products_quantity_stock_below_min = False - ) - @classmethod - def from_filters_stock_item(cls, filters_stock_item): - return cls.from_form_filters_product_permutation(filters_stock_item) -""" - class Product_Temp(db.Model, Store_Base): __tablename__ = 'Shop_Product_Temp' __table_args__ = { 'extend_existing': True } @@ -883,11 +412,6 @@ class Product_Temp(db.Model, Store_Base): row.id_access_level_required = product.id_access_level_required[0] if isinstance(product.id_access_level_required, tuple) else product.id_access_level_required row.active = product.active row.display_order = product.display_order - """ - row.guid = product.guid - row.created_on = product.created_on - row.created_by = product.created_by - """ return row def to_json(self): return { @@ -900,10 +424,6 @@ class Product_Temp(db.Model, Store_Base): 'display_order': self.display_order, 'guid': self.guid, } - """ - 'created_on': self.created_on, - 'created_by': self.created_by - """ def __repr__(self): return f'''Product_Temp id_product: {self.id_product} diff --git a/business_objects/store/product_category.py b/business_objects/store/product_category.py index e5f78c97..f1e6dfb4 100644 --- a/business_objects/store/product_category.py +++ b/business_objects/store/product_category.py @@ -463,19 +463,6 @@ class Product_Category_Container(Store_Base): return ','.join(list_ids) -""" -class Table_Shop_Product_Category(db.Model): - __tablename__ = 'Shop_Product_Category' - id_category: int = db.Column(db.Integer, primary_key=True) - code: str = db.Column(db.String(50)) - name: str = db.Column(db.String(255)) - description: str = db.Column(db.String(4000)) - active: bool = db.Column(db.Boolean) - display_order: int = db.Column(db.Integer) - created_on: datetime = db.Column(db.DateTime) - created_by: int = db.Column(db.Integer) - id_change_set: int = db.Column(db.Integer) -""" class Product_Category_Temp(db.Model, Store_Base): __tablename__ = 'Shop_Product_Category_Temp' __table_args__ = { 'extend_existing': True } @@ -488,11 +475,11 @@ class Product_Category_Temp(db.Model, Store_Base): active: bool = db.Column(db.Boolean) display_order: int = db.Column(db.Integer) guid: str = db.Column(db.String(36)) - # created_on: datetime = db.Column(db.DateTime) - # created_by: int = db.Column(db.Integer) + def __init__(self): super().__init__() self.id_temp = None + @classmethod def from_product_category(cls, product_category): row = cls() @@ -503,12 +490,8 @@ class Product_Category_Temp(db.Model, Store_Base): row.id_access_level_required = product_category.id_access_level_required[0] if isinstance(product_category.id_access_level_required, tuple) else product_category.id_access_level_required row.active = product_category.active row.display_order = product_category.display_order - """ - row.guid = product_category.guid - row.created_on = product_category.created_on - row.created_by = product_category.created_by - """ return row + def to_json(self): return { 'id_category': self.id_category, @@ -520,10 +503,6 @@ class Product_Category_Temp(db.Model, Store_Base): 'display_order': self.display_order, 'guid': self.guid, } - """ - 'created_on': self.created_on, - 'created_by': self.created_by - """ - + def __repr__(self): return str(self.__dict__) \ No newline at end of file diff --git a/business_objects/store/product_variation.py b/business_objects/store/product_variation.py index e5ae26a8..38ed515a 100644 --- a/business_objects/store/product_variation.py +++ b/business_objects/store/product_variation.py @@ -145,38 +145,6 @@ class Parameters_Product_Variation(Get_Many_Parameters_Base): parameters.a_get_inactive_variation = get_inactive return parameters -""" -class Product_Variation_Container(BaseModel): - variation_types: list = [] - variations: list = [] - - def add_product_variation_type(self, variation_type): - av.val_instance(variation_type, 'variation_type', 'Product_Variation_Container.add_product_variation_type', Product_Variation_Type) - self.variations.append(variation_type) - def add_product_variation(self, variation): - av.val_instance(variation, 'variation', 'Product_Variation_Container.add_product_variation', Product_Variation) - if variation.variation_type is None: - variation_type = next(filterfalse(lambda x: x.id_type != variation.id_type, self.variation_types), None) - if variation_type is not None: - variation.variation_type = variation_type - self.variations.append(variation) - - def __repr__(self): - return f'Product_Variation_Container:\nvariations_types: {self.variation_types}\nvariations: {self.variations}' - - def to_list_variation_options(self): - list_variations = [] - for variation in self.variations: - list_variations.append(variation.to_json_option()) - Helper_App.console_log(f'list_variations: {list_variations}') - return list_variations - def to_list_variation_type_options(self): - list_variation_types = [] - for variation_type in self.variation_types: - list_variation_types.append(variation_type.to_json_option()) - return list_variation_types -""" - class Product_Variation_Temp(db.Model, Store_Base): __tablename__ = 'Shop_Variation_Temp' __table_args__ = { 'extend_existing': True } diff --git a/business_objects/store/product_variation_tree.py b/business_objects/store/product_variation_tree.py index 00a3647e..a8d994b8 100644 --- a/business_objects/store/product_variation_tree.py +++ b/business_objects/store/product_variation_tree.py @@ -123,18 +123,6 @@ class Product_Variation_Tree(): at_leaf_node = node.is_leaf() types.append(node.variation_type) return types - """ - def get_product_variations(self): - variations = [] - node = self.node_root - at_leaf_node = node.is_leaf() - variations.append(node.variation) - while not at_leaf_node: - node = node.nodes_child[0] - at_leaf_node = node.is_leaf() - variations.append(node.variation) - return variations - """ def to_preview_str(self): Helper_App.console_log(f'Product_Variation_Tree.to_preview_str') variation_types = self.get_product_variation_types() diff --git a/business_objects/store/product_variation_type.py b/business_objects/store/product_variation_type.py index 7e799893..d3e9d2ba 100644 --- a/business_objects/store/product_variation_type.py +++ b/business_objects/store/product_variation_type.py @@ -107,18 +107,6 @@ class Product_Variation_Type(db.Model, Store_Base): 'value': self.id_type, 'text': self.name_singular } - """ - def get_preview_variations(self): - preview = '' - if len(self.variations) > 0: - # preview = '\n'.join([variation.name for variation in self.variations]) - preview = '

' + '

'.join([variation.name for variation in self.variations]) + '

' - return preview - def get_str_list_ids_variation(self): - if self.variations is None or len(self.variations) == 0: - return '' - return ','.join([str(variation.id_variation) for variation in self.variations]) - """ class Product_Variation_Type_Temp(db.Model, Store_Base): __tablename__ = 'Shop_Variation_Type_Temp' diff --git a/business_objects/store/stock_item.py b/business_objects/store/stock_item.py index 77fabadc..9acb25c5 100644 --- a/business_objects/store/stock_item.py +++ b/business_objects/store/stock_item.py @@ -56,12 +56,6 @@ class Stock_Item(db.Model, Store_Base): is_consumed = db.Column(db.Boolean) date_consumed = db.Column(db.DateTime) active = db.Column(db.Boolean) - """ - can_view = db.Column(db.Boolean) - can_edit = db.Column(db.Boolean) - can_admin = db.Column(db.Boolean) - """ - # variation_tree: Product_Variation_Tree = None def __init__(self): super().__init__() @@ -93,11 +87,6 @@ class Stock_Item(db.Model, Store_Base): stock_item.is_consumed = av.input_bool(query_row[22], "is_consumed", _m, v_arg_type=v_arg_type) stock_item.date_consumed = query_row[23] stock_item.active = av.input_bool(query_row[24], "active", _m, v_arg_type=v_arg_type) - """ - stock_item.can_view = av.input_bool(query_row[24], "can_view", _m, v_arg_type=v_arg_type) - stock_item.can_edit = av.input_bool(query_row[25], "can_edit", _m, v_arg_type=v_arg_type) - stock_item.can_admin = av.input_bool(query_row[26], "can_admin", _m, v_arg_type=v_arg_type) - """ return stock_item @classmethod @@ -163,18 +152,6 @@ class Stock_Item(db.Model, Store_Base): if permutation.is_available(): return True return False - """ - def to_permutation_row_list(self): - list_rows = [] - for permutation in self.permutations: - list_rows.append(permutation.to_row_permutation()) - return list_rows - def to_json_option(self): - return { - 'value': self.id_stock_item, - 'text': self.id_stock_item - } - """ class Parameters_Stock_Item(Get_Many_Parameters_Base): a_get_all_product_permutation: bool diff --git a/business_objects/store/store_base.py b/business_objects/store/store_base.py index 4eac4567..e47f11f1 100644 --- a/business_objects/store/store_base.py +++ b/business_objects/store/store_base.py @@ -18,43 +18,6 @@ import lib.argument_validation as av # external from typing import ClassVar -""" -class I_Store_Base(): - @abstractmethod - def __repr__(self): - pass - @classmethod - @abstractmethod - def from_json(cls, json): - pass - @abstractmethod - def to_json(self): - pass - @abstractmethod - def to_json_option(self): - pass - @abstractmethod - def test_69 (self): - pass - "" - def __init_subclass__(cls, **kwargs): - super().__init_subclass__(**kwargs) - for name, value in vars(Store_Base).items(): - if getattr(value, "__isabstractmethod__", False): - if name not in cls.__dict__: - raise TypeError(f"Can't instantiate class {cls.__name__} " - f"without implementation of abstract method {name}") - subclass_value = cls.__dict__[name] - if (isinstance(value, (staticmethod, classmethod)) and - not isinstance(subclass_value, type(value))): - raise TypeError(f"Abstract {type(value).__name__} {name} in {cls.__name__} " - f"must be implemented as a {type(value).__name__}") - def __new__(cls, *args, **kwargs): - if cls is Store_Base: - raise TypeError("Can't instantiate abstract class Store_Base directly") - return super().__new__(cls) - "" -""" class Store_Base(Base): # ATTR_ID_CURRENCY_COST: ClassVar[str] = 'id_currency_cost' @@ -124,6 +87,10 @@ class Store_Base(Base): FLAG_UNIT_MEASUREMENT_LATENCY_MANUFACTURE: ClassVar[str] = 'unit_measurement_latency_manufacture' FLAG_UNIT_MEASUREMENT_QUANTITY: ClassVar[str] = 'unit_measurement_quantity' FLAG_VALUE_TEXT: ClassVar[str] = 'value_text' - + KEY_BASKET: ClassVar[str] = 'basket' + KEY_IS_INCLUDED_VAT: ClassVar[str] = 'is-included_vat' + KEY_ID_CURRENCY: ClassVar[str] = 'currency' + KEY_ID_REGION_DELIVERY: ClassVar[str] = 'id-region-delivery' + def __repr__(self): return str(self.__dict__) \ No newline at end of file diff --git a/business_objects/store/stripe.py b/business_objects/store/stripe.py deleted file mode 100644 index 3ed8c54d..00000000 --- a/business_objects/store/stripe.py +++ /dev/null @@ -1,161 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: Business Objects -Feature: Stripe Business Object - -Description: -Business objects for Stripe -""" - -# internal -import lib.argument_validation as av -from lib import data_types -from forms.forms import Form_Basket_Add, Form_Basket_Edit # Form_Product -from extensions import db -from helpers.helper_app import Helper_App -# external -from datetime import datetime, timedelta -import locale - -class Stripe_Product(db.Model): - id_product = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(255)) - description = db.Column(db.String(4000)) - price_GBP_full = db.Column(db.Float) - id_category = db.Column(db.Integer) - lead_time_manuf = db.Column(db.Integer) - quantity_min = db.Column(db.Float) - quantity_max = db.Column(db.Float) - quantity_step = db.Column(db.Float) - quantity_stock = db.Column(db.Float) - id_stripe_product = db.Column(db.String(255)) - id_stripe_price = db.Column(db.String(255)) - is_subscription = db.Column(db.Boolean) - name_recurring_interval = db.Column(db.String(255)) - name_plural_recurring_interval = db.Column(db.String(256)) - count_recurring_interval = db.Column(db.Integer) - display_order = db.Column(db.Integer) - can_view = db.Column(db.Boolean) - can_edit = db.Column(db.Boolean) - can_admin = db.Column(db.Boolean) - # form_basket_add: Form_Basket_Add - # form_basket_edit: Form_Basket_Edit - - def __new__(cls, id, name, description, price_GBP_full, id_category, lead_time_manuf, quantity_min, quantity_max, quantity_step, quantity_stock, id_stripe_product, id_stripe_price, - is_subscription, name_recurring_interval, name_plural_recurring_interval, count_recurring_interval, display_order, can_view, can_edit, can_admin): - _m = 'Product.__new__' - v_arg_type = 'class attribute' - av.val_int(id, 'id', _m, 0, v_arg_type=v_arg_type) - av.val_str(name, 'name', _m, max_len=256, v_arg_type=v_arg_type) - av.val_str(description, 'description', _m, max_len=4000, v_arg_type=v_arg_type) - av.full_val_float(price_GBP_full, 'price_GBP_full', _m, 0., v_arg_type=v_arg_type) - av.val_int(id_category, 'id_category', _m, 0, v_arg_type=v_arg_type) - av.val_int(lead_time_manuf, 'lead_time_manuf', _m, 0, v_arg_type=v_arg_type) - av.full_val_float(quantity_step, 'quantity_step', _m, 0., v_arg_type=v_arg_type) - av.full_val_float(quantity_min, 'quantity_min', _m, quantity_step, v_arg_type=v_arg_type) - av.full_val_float(quantity_max, 'quantity_max', _m, quantity_min, v_arg_type=v_arg_type) - av.full_val_float(quantity_stock, 'quantity_stock', _m, 0, v_arg_type=v_arg_type) - av.val_str(id_stripe_product, 'id_stripe_product', _m, max_len=100, v_arg_type=v_arg_type) - av.val_str(id_stripe_price, 'id_stripe_price', _m, max_len=100, v_arg_type=v_arg_type) - av.full_val_bool(is_subscription, 'is_subscription', _m, v_arg_type=v_arg_type) - Helper_App.console_log(f'is_subscription: {is_subscription}, {av.input_bool(is_subscription, "is_subscription", _m, v_arg_type=v_arg_type)}') - is_subscription = av.input_bool(is_subscription, "is_subscription", _m, v_arg_type=v_arg_type) - if is_subscription: - av.val_str(name_recurring_interval, 'name_recurring_interval', _m, max_len=255, v_arg_type=v_arg_type) - av.val_str(name_plural_recurring_interval, 'name_plural_recurring_interval', _m, max_len=256, v_arg_type=v_arg_type) - av.val_int(count_recurring_interval, 'count_recurring_interval', _m, 0, v_arg_type=v_arg_type) - av.val_int(display_order, 'display_order', _m, v_arg_type=v_arg_type) - av.full_val_bool(can_view, 'can_view', _m, v_arg_type=v_arg_type) - # can_view = av.input_bool(can_view, "can_view", _m, v_arg_type=v_arg_type) - av.full_val_bool(can_edit, 'can_edit', _m, v_arg_type=v_arg_type) - # can_edit = av.input_bool(can_edit, "can_edit", _m, v_arg_type=v_arg_type) - av.full_val_bool(can_admin, 'can_admin', _m, v_arg_type=v_arg_type) - # can_admin = av.input_bool(can_admin, "can_admin", _m, v_arg_type=v_arg_type) - return super(Product, cls).__new__(cls) # , id, name, description, price_GBP, id_category, lead_time_manuf, quantity_min, quantity_max, quantity_step, quantity_stock, id_stripe_product, id_stripe_price, - # is_subscription, name_recurring_interval, name_plural_recurring_interval, count_recurring_interval, can_view, can_edit, can_admin) - - def __init__(self, id, name, description, price_GBP_full, id_category, lead_time_manuf, quantity_min, quantity_max, quantity_step, quantity_stock, id_stripe_product, id_stripe_price, - is_subscription, name_recurring_interval, name_plural_recurring_interval, count_recurring_interval, display_order, can_view, can_edit, can_admin): - _m = 'Product.__new__' - v_arg_type = 'class attribute' - self.id_product = id - self.name = name - self.description = description - self.price_GBP_full = price_GBP_full - self.id_category = id_category - self.lead_time_manuf = lead_time_manuf - self.quantity_min = quantity_min - self.quantity_max = quantity_max - self.quantity_step = quantity_step - self.quantity_stock = quantity_stock - self.id_stripe_product = id_stripe_product - self.id_stripe_price = id_stripe_price - self.is_subscription = av.input_bool(is_subscription, "is_subscription", _m, v_arg_type=v_arg_type) - self.name_recurring_interval = name_recurring_interval - self.name_plural_recurring_interval = name_plural_recurring_interval - self.count_recurring_interval = count_recurring_interval - self.display_order = display_order - self.can_view = av.input_bool(can_view, "can_view", _m, v_arg_type=v_arg_type) - self.can_edit = av.input_bool(can_edit, "can_edit", _m, v_arg_type=v_arg_type) - self.can_admin = av.input_bool(can_admin, "can_admin", _m, v_arg_type=v_arg_type) - self.variations = [] - self.images = [] - self.delivery_options = [] - self.discounts = [] - self.discount_index = {} - super().__init__() - self.form_basket_add = Form_Basket_Add() - self.form_basket_edit = Form_Basket_Edit() - - def output_lead_time(self): - return '1 day' if self.lead_time_manuf == 1 else f'{self.lead_time_manuf} days' - - def output_delivery_date(self): - return (datetime.now() + timedelta(days=self.lead_time_manuf)).strftime('%A, %d %B %Y') - - def output_price(self): - locale.setlocale(locale.LC_ALL, '') - return locale.format_string("%d", self.price_GBP_full, grouping=True) - """ - def add_form_basket_add(self): - self.form_basket_add = None - - def add_form_basket_edit(self): - self.form_basket_edit = None - """ - def __repr__(self): - return f'''Product - id: {self.id_product} - name: {self.name} - description: {self.description} - price_GBP_full: {self.price_GBP_full} - id_category: {self.id_category} - lead_time_manuf: {self.lead_time_manuf} - quantity_min: {self.quantity_min} - quantity_max: {self.quantity_max} - quantity_step: {self.quantity_step} - quantity_stock: {self.quantity_stock} - id_stripe_product: {self.id_stripe_product} - id_stripe_price: {self.id_stripe_price} - is_subscription: {self.is_subscription} - name_recurring_interval: {self.name_recurring_interval} - name_plural_recurring_interval: {self.name_plural_recurring_interval} - count_recurring_interval: {self.count_recurring_interval} - display_order: {self.display_order} - can_view: {self.can_view} - can_edit: {self.can_edit} - can_admin: {self.can_admin} - variations: {self.variations} - images: {self.images} - delivery_options: {self.delivery_options} - ''' - - def add_product_price_discount(self, discount): - _m = 'Category.add_product' - av.val_instance(discount, 'discount', _m, Discount) - # self.product_index.append(len(self.products)) - self.discount_index[discount.id_discount] = len(self.discounts) - self.discounts.append(discount) \ No newline at end of file diff --git a/business_objects/user.py b/business_objects/user.py index c198bbb2..2b098ad2 100644 --- a/business_objects/user.py +++ b/business_objects/user.py @@ -185,48 +185,6 @@ class Parameters_User(Get_Many_Parameters_Base): ids_user = '', ids_user_auth0 = '' ) -""" User_Eval -@dataclass -class User_Filters(): - ids_user: str - get_inactive_users: bool - ids_permission: str - ids_access_level: str - ids_product: str - - def to_json(self): - return { - **self.get_shared_json_attributes(self), - 'a_ids_user': self.ids_user, - 'a_get_inactive_users': self.get_inactive_users, - 'a_ids_permission': self.ids_permission, - 'a_ids_access_level': self.ids_access_level, - 'a_ids_product': self.ids_product, - } - - @staticmethod - def from_form(form): - av.val_instance(form, 'form', 'User_Filters.from_form', Form_Filters_User) - get_inactive = av.input_bool(form.active.data, "active", "User_Filters.from_form") - return User_Filters( - ids_user = form.id_user.data, - get_inactive_users = get_inactive, - ids_permission = form.ids_permission.data, - ids_access_level = form.ids_access_level.data, - ids_product = form.ids_product.data, - ) - - @staticmethod - def get_default(datastore_store): - is_user_logged_in, id_user = datastore_store.get_login_user() - return User_Filters( - ids_user = id_user, - get_inactive_users = False, - ids_permission = '', - ids_access_level = '', - ids_product = '', - ) -""" class User_Permission_Evaluation(db.Model): id_evaluation = db.Column(db.Integer, primary_key=True) diff --git a/config.py b/config.py index 6091513f..2a453875 100644 --- a/config.py +++ b/config.py @@ -10,7 +10,6 @@ Description: Configuration variables """ -# IMPORTS from lib import argument_validation as av import os from dotenv import load_dotenv, find_dotenv @@ -18,17 +17,35 @@ from flask import current_app load_dotenv(find_dotenv()) -# CLASSES class Config: is_development = False is_production = False - # Miscellaneous - DEBUG = False # av.input_bool(os.getenv('DEBUG'), 'DEBUG', 'Config') - TESTING = False - URL_HOST = os.getenv('URL_HOST') - SECRET_KEY = os.getenv('KEY_SECRET_FLASK') # gen cmd: openssl rand -hex 32 - # Add other configuration variables as needed - # MySQL + + DB_HOST = os.getenv('DB_HOST') + DB_NAME = os.getenv('partsltd_prod') + DB_PASSWORD = os.getenv('DB_PASSWORD') + DB_USER = os.getenv('DB_USER') + DEBUG = False + DOMAIN_AUTH0 = os.getenv('DOMAIN_AUTH0') + ID_AUTH0_CLIENT = os.getenv('ID_AUTH0_CLIENT') + ID_AUTH0_CLIENT_SECRET = os.getenv('ID_AUTH0_CLIENT_SECRET') + ID_TOKEN_USER = 'user' + MAIL_DEBUG = False + MAIL_SERVER = 'mail.partsltd.co.uk' + MAIL_PORT = 465 + MAIL_USE_TLS = False + MAIL_USE_SSL = True + MAIL_USERNAME = os.getenv('MAIL_DEFAULT_SENDER') + MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') + MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER') + MAIL_CONTACT_PUBLIC = os.getenv('MAIL_CONTACT_PUBLIC') + RECAPTCHA_PUBLIC_KEY = os.getenv('RECAPTCHA_PUBLIC_KEY') + RECAPTCHA_PRIVATE_KEY = os.getenv('RECAPTCHA_PRIVATE_KEY') + REMEMBER_COOKIE_SECURE = True + SECRET_KEY = os.getenv('KEY_SECRET_FLASK') + SESSION_COOKIE_SECURE = True + SESSION_COOKIE_HTTPONLY = True + SESSION_COOKIE_SAMESITE = 'Lax' # 'Strict' # Strict is preferable for security, but Lax is required for OAuth functionality SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI') SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_ENGINE_OPTIONS = { @@ -37,91 +54,28 @@ class Config: 'pool_pre_ping': True, 'pool_timeout': 30, } - # Auth0 - SESSION_COOKIE_SECURE = True - SESSION_COOKIE_HTTPONLY = True - SESSION_COOKIE_SAMESITE = 'Lax' # 'Strict' # Strict is preferable for security, but Lax is required for OAuth functionality - REMEMBER_COOKIE_SECURE = True - # PERMANENT_SESSION_LIFETIME = 3600 + TESTING = False + URL_HOST = os.getenv('URL_HOST') WTF_CSRF_ENABLED = True - # WTF_CSRF_CHECK_DEFAULT = False # We'll check it manually for API routes - # WTF_CSRF_HEADERS = ['X-CSRFToken'] # Accept CSRF token from this header + WTF_CSRF_SSL_STRICT = False WTF_CSRF_TIME_LIMIT = None - WTF_CSRF_SSL_STRICT = False # Allows testing without HTTPS - ID_AUTH0_CLIENT = os.getenv('ID_AUTH0_CLIENT') - ID_AUTH0_CLIENT_SECRET = os.getenv('ID_AUTH0_CLIENT_SECRET') - DOMAIN_AUTH0 = os.getenv('DOMAIN_AUTH0') - ID_TOKEN_USER = 'user' - # PostgreSQL - DB_NAME = os.getenv('partsltd') - DB_USER = os.getenv('DB_USER') - DB_PASSWORD = os.getenv('DB_PASSWORD') - DB_HOST = os.getenv('DB_HOST') - # DB_PORT = os.getenv('DB_PORT') - # Store - # is_included_VAT = True - """ - KEY_IS_INCLUDED_VAT = 'is_included_VAT' - code_currency = 1 - KEY_CODE_CURRENCY = 'id_currency' - code_region_delivery = 1 - KEY_CODE_REGION_DELIVERY = 'id_region_delivery' - KEY_ID_CURRENCY = 'id_currency' - KEY_ID_REGION_DELIVERY = 'id_region_delivery' - """ - # id_currency = 1 - # id_region_delivery = 1 - # Mail - MAIL_DEBUG = False # av.input_bool(os.getenv('DEBUG'), 'DEBUG', 'Config') - MAIL_SERVER = 'mail.partsltd.co.uk' # 'smtp.gmail.com' - MAIL_PORT = 465 # 587 - MAIL_USE_TLS = False - MAIL_USE_SSL = True - MAIL_USERNAME = os.getenv('MAIL_DEFAULT_SENDER') - MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') - MAIL_DEFAULT_SENDER = os.getenv('MAIL_DEFAULT_SENDER') - MAIL_CONTACT_PUBLIC = os.getenv('MAIL_CONTACT_PUBLIC') - # Recaptcha - RECAPTCHA_PUBLIC_KEY = os.getenv('RECAPTCHA_PUBLIC_KEY') - RECAPTCHA_PRIVATE_KEY = os.getenv('RECAPTCHA_PRIVATE_KEY') class DevelopmentConfig(Config): is_development = True - # Add development-specific configuration variables DEBUG = True MAIL_DEBUG = True SESSION_COOKIE_SECURE = False class ProductionConfig(Config): is_production = True - # Add production-specific configuration variables - pass -# Set the configuration class based on the environment -# You can change 'development' to 'production' when deploying config_env = os.getenv('FLASK_ENV', "production") with open('app.log', 'a') as f: print(f'config_env: {config_env}') f.write(f'config_env: {config_env}\n') - # current_app.logger.error(f'config_env: {config_env}') # logger not yet initialised if config_env == 'development': app_config = DevelopmentConfig elif config_env == 'production': app_config = ProductionConfig else: raise ValueError("Invalid configuration environment") - -# environment variables -""" -SET KEY_SECRET_FLASK=nips -SET ID_AUTH0_CLIENT= -SET ID_AUTH0_CLIENT_SECRET= -SET DOMAIN_AUTH0= -SET MAIL_PASSWORD= -SET RECAPTCHA_PUBLIC_KEY= -SET RECAPTCHA_PRIVATE_KEY= -SET SQLALCHEMY_DATABASE_URI= -SET URL_HOST= -""" -# SET SQLALCHEMY_DATABASE_URI = mysql://username:password@localhost/dbname - # Replace 'username', 'password', 'localhost', and 'dbname' with your actual database credentials \ No newline at end of file diff --git a/controllers/core.py b/controllers/core.py index a654d981..8bf8d961 100644 --- a/controllers/core.py +++ b/controllers/core.py @@ -59,18 +59,15 @@ def contact_post(): try: form = Form_Contact() if form.validate_on_submit(): - # Handle form submission email = form.email.data - CC = form.CC.data # not in use + # CC = form.CC.data # not in use name = form.name.data message = form.message.data - # send email mailItem = Message("PARTS Website Contact Us Message", recipients=[current_app.config['MAIL_CONTACT_PUBLIC']]) mailItem.body = f"Dear Lord Edward Middleton-Smith,\n\n{message}\n\nKind regards,\n{name}\n{email}" mail.send(mailItem) return "Submitted." return "Invalid. Failed to submit." - # html_body = render_template('pages/core/_contact.html', model = model) except Exception as e: return jsonify(error=str(e)), 403 @@ -88,7 +85,6 @@ def admin_home(): try: model = Model_View_Admin_Home() if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', callback = Model_View_Admin_Home.HASH_PAGE_ADMIN_HOME)) return redirect(url_for('routes_core.home')) if not (model.user.can_admin_store or model.user.can_admin_user): return redirect(url_for('routes_core.home')) diff --git a/controllers/store/pay_stripe.py b/controllers/store/pay_stripe.py deleted file mode 100644 index 9ae08d65..00000000 --- a/controllers/store/pay_stripe.py +++ /dev/null @@ -1,167 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: App General -Feature: App - -Description: -Initializes the Flask application, sets the configuration based on the environment, and defines two routes (/ and /about) that render templates with the specified titles. -""" - -# IMPORTS -from helpers.helper_app import Helper_App -import os -import stripe -import json -from flask import Flask, render_template, render_template_string, jsonify, request, send_from_directory, redirect -from dotenv import load_dotenv, find_dotenv - - -# VARIABLE INSTANTIATION -key_secret = os.environ.get("KEY_SECRET_STRIPE") -key_public = os.environ.get("KEY_PUBLIC_STRIPE") # 'pk_test_51OGrxlL7BuLKjoMpfpfw7bSmZZK1MhqMoQ5VhW2jUj7YtoEejO4vqnxKPiqTHHuh9U4qqkywbPCSI9TpFKtr4SYH007KHMWs68' - -# METHODS -def create_product_price(): - Helper_App.console_log(f'stripe.api_key = {stripe.api_key}') - starter_subscription = stripe.Product.create( - name="Starter Subscription", - description="$12/Month subscription", - ) - - starter_subscription_price = stripe.Price.create( - unit_amount=1200, - currency="usd", - recurring={"interval": "month"}, - product=starter_subscription['id'], - ) - - # Save these identifiers - Helper_App.console_log(f"Success! Here is your starter subscription product id: {starter_subscription.id}") - Helper_App.console_log(f"Success! Here is your starter subscription price id: {starter_subscription_price.id}") - - return starter_subscription_price.id - -def get_file_str(f_address): - f = open(f_address) - return f.read() - -# Ensure environment variables are set. -price = os.getenv('PRICE') -if price is None or price == 'price_12345' or price == '': - Helper_App.console_log('You must set a Price ID in .env. Please see the README.') - exit(0) - -# For sample support and debugging, not required for production: -stripe.set_app_info( - 'stripe-samples/checkout-one-time-payments', - version='0.0.1', - url='https://github.com/stripe-samples/checkout-one-time-payments') - -# stripe.api_version = '2020-08-27' -stripe.api_key = key_secret # os.getenv('KEY_SECRET_STRIPE') - -app_dir = str(os.path.abspath(os.path.join( - __file__, "..", ".."))) -static_dir = str(os.path.abspath(os.path.join( - app_dir, os.getenv("STATIC_DIR")))) -app = Flask(__name__, static_folder=static_dir, - static_url_path="", template_folder=static_dir) - - -@app.route('/', methods=['GET']) -def get_example(): - # return render_template(f'{app_dir}\\templates\\_home.html') # f'{app_dir}\\templates\\layout.html') - # return render_template_string(get_file_str(f'{app_dir}\\templates\\_home.html')) # f'{app_dir}\\templates\\layout.html') - return render_template('../templates/_home.html') - - -@app.route('/config', methods=['GET']) -def get_publishable_key(): - price = stripe.Price.retrieve(os.getenv('PRICE')) - return jsonify({ - 'publicKey': key_public, # os.getenv('KEY_PUBLIC_STRIPE'), - 'unitAmount': price['unit_amount'], - 'currency': price['currency'] - }) - -# Fetch the Checkout Session to display the JSON result on the success page -@app.route('/checkout-session', methods=['GET']) -def get_checkout_session(): - id = request.args.get('sessionId') - checkout_session = stripe.checkout.Session.retrieve(id) - return jsonify(checkout_session) - - -@app.route('/create-checkout-session', methods=['POST']) -def create_checkout_session(): - quantity = request.form.get('quantity', 1) - domain_url = os.getenv('DOMAIN') - - try: - # Create new Checkout Session for the order - # Other optional params include: - # [billing_address_collection] - to display billing address details on the page - # [customer] - if you have an existing Stripe Customer ID - # [payment_intent_data] - lets capture the payment later - # [customer_email] - lets you prefill the email input in the form - # [automatic_tax] - to automatically calculate sales tax, VAT and GST in the checkout page - # For full details see https://stripe.com/docs/api/checkout/sessions/create - - # ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param - checkout_session = stripe.checkout.Session.create( - success_url=domain_url + '/success.html?session_id={CHECKOUT_SESSION_ID}', - cancel_url=domain_url + '/canceled.html', - mode='payment', - # automatic_tax={'enabled': True}, - line_items=[{ - 'price': os.getenv('PRICE'), - 'quantity': quantity, - }] - ) - return redirect(checkout_session.url, code=303) - except Exception as e: - return jsonify(error=str(e)), 403 - - -@app.route('/webhook', methods=['POST']) -def webhook_received(): - # You can use webhooks to receive information about asynchronous payment events. - # For more about our webhook events check out https://stripe.com/docs/webhooks. - webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET') - request_data = json.loads(request.data) - - if webhook_secret: - # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured. - signature = request.headers.get('stripe-signature') - try: - event = stripe.Webhook.construct_event( - payload=request.data, sig_header=signature, secret=webhook_secret) - data = event['data'] - except Exception as e: - return e - # Get the type of webhook event sent - used to check the status of PaymentIntents. - event_type = event['type'] - else: - data = request_data['data'] - event_type = request_data['type'] - data_object = data['object'] - - Helper_App.console_log('event ' + event_type) - - if event_type == 'checkout.session.completed': - Helper_App.console_log('🔔 Payment succeeded!') - - return jsonify({Model_View_Base.FLAG_STATUS: Model_View_Base.FLAG_SUCCESS}) - - -if __name__ == '__main__': - # stripe.api_key = key_secret - - # create_product_price() - - # Setup Stripe python client library. - load_dotenv(find_dotenv()) - app.run(port=4242, debug=True) \ No newline at end of file diff --git a/controllers/store/product.py b/controllers/store/product.py index 7d29403f..24b7bea8 100644 --- a/controllers/store/product.py +++ b/controllers/store/product.py @@ -39,7 +39,6 @@ def products(): Helper_App.console_log(f'form_filters={form_filters}') model = Model_View_Store_Product(form_filters) if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', data = jsonify({ Model_View_Store_Product.FLAG_CALLBACK: Model_View_Store_Product.HASH_PAGE_STORE_PRODUCTS }))) return redirect(url_for('routes_core.home')) return render_template('pages/store/_products.html', model = model) @@ -54,7 +53,6 @@ def save_product(): Model_View_Store_Product.FLAG_STATUS: Model_View_Store_Product.FLAG_FAILURE, Model_View_Store_Product.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}' }) - # filters_form = Filters_Product.from_form(form_filters) Helper_App.console_log(f'form_filters: {form_filters}') products = data[Model_View_Store_Product.FLAG_PRODUCT] @@ -66,7 +64,6 @@ def save_product(): objsProduct = [] for product in products: objsProduct.append(Product.from_json(product)) - # model_save = Model_View_Store_Product() # filters_product=filters_form) Helper_App.console_log(f'objsProduct={objsProduct}') save_errors = Model_View_Store_Product.save_products(data.get('comment', 'No comment'), objsProduct) diff --git a/controllers/store/product_category.py b/controllers/store/product_category.py index 2dc443b6..61ac9380 100644 --- a/controllers/store/product_category.py +++ b/controllers/store/product_category.py @@ -31,31 +31,14 @@ routes_store_product_category = Blueprint('routes_store_product_category', __nam @routes_store_product_category.route(Model_View_Store_Product_Category.HASH_PAGE_STORE_PRODUCT_CATEGORIES, methods=['GET']) def categories(): Helper_App.console_log('categories') - # data = Helper_App.get_request_data(request) try: form_filters = Filters_Product_Category.from_json(request.args) except Exception as e: Helper_App.console_log(f'Error: {e}') form_filters = Filters_Product_Category() Helper_App.console_log(f'form_filters={form_filters}') - """ - filters = Filters_Product_Category.get_default() - have_changed_filters = False - arg_filter_is_not_empty = request.args.get(Model_View_Store_Product_Category.FLAG_IS_NOT_EMPTY, None) - have_changed_filters = have_changed_filters or arg_filter_is_not_empty is None - Helper_App.console_log(f'arg_filter_is_not_empty={arg_filter_is_not_empty}') - filters.is_not_empty = filters.is_not_empty if arg_filter_is_not_empty is None else av.input_bool(arg_filter_is_not_empty, 'is_not_empty', 'filter_category') - arg_filter_active = request.args.get(Model_View_Store_Product_Category.FLAG_ACTIVE, None) - have_changed_filters = have_changed_filters or arg_filter_active is None - Helper_App.console_log(f'arg_filter_active={arg_filter_active}') - filters.active = filters.active if arg_filter_active is None else av.input_bool(arg_filter_active, 'active', 'filter_category') - if have_changed_filters: - Helper_App.console_log('redirecting') - return redirect(url_for('routes_store_product_category.categories', **filters.to_json())) - """ model = Model_View_Store_Product_Category(form_filters) if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', data = jsonify({ Model_View_Store_Product_Category.FLAG_CALLBACK: Model_View_Store_Product_Category.HASH_PAGE_STORE_PRODUCT_CATEGORIES }))) return redirect(url_for('routes_core.home')) return render_template('pages/store/_product_categories.html', model = model) @@ -69,7 +52,6 @@ def save_category(): Model_View_Store_Product_Category.FLAG_STATUS: Model_View_Store_Product_Category.FLAG_FAILURE, Model_View_Store_Product_Category.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}' }) - # filters_form = Filters_Product_Category.from_form(form_filters) categories = data[Model_View_Store_Product_Category.FLAG_PRODUCT_CATEGORY] if len(categories) == 0: @@ -80,7 +62,6 @@ def save_category(): objsCategory = [] for category in categories: objsCategory.append(Product_Category.from_json(category)) - # model_save = Model_View_Store_Product_Category() # filters_product=filters_form) Helper_App.console_log(f'objsCategory={objsCategory}') errors = Model_View_Store_Product_Category.save_categories(data.get('comment', 'No comment'), objsCategory) diff --git a/controllers/store/product_permutation.py b/controllers/store/product_permutation.py index 65448ba1..3269fd6b 100644 --- a/controllers/store/product_permutation.py +++ b/controllers/store/product_permutation.py @@ -34,7 +34,6 @@ routes_store_product_permutation = Blueprint('routes_store_product_permutation', def permutations(): Helper_App.console_log('permutations') data = request.args - # Helper_App.console_log(f'data={data}\nrequest.args={request.args}\nrequest.form={request.form}\nrequest.data={request.data}\nrequest.values={request.values}\nrequest.headers={request.headers}') try: form_filters = Filters_Product_Permutation.from_json(data) except Exception as e: @@ -43,7 +42,6 @@ def permutations(): Helper_App.console_log(f'form_filters={form_filters}') model = Model_View_Store_Product_Permutation(form_filters) if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', data = jsonify({ Model_View_Store_Product_Permutation.FLAG_CALLBACK: Model_View_Store_Product_Permutation.HASH_PAGE_STORE_PRODUCT_PERMUTATIONS }))) return redirect(url_for('routes_core.home')) return render_template('pages/store/_product_permutations.html', model = model) @@ -58,7 +56,6 @@ def save_permutation(): Model_View_Store_Product_Permutation.FLAG_STATUS: Model_View_Store_Product_Permutation.FLAG_FAILURE, Model_View_Store_Product_Permutation.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}' }) - # filters_form = Filters_Product_Permutation.from_form(form_filters) Helper_App.console_log(f'form_filters: {form_filters}') permutations = data[Model_View_Store_Product_Permutation.FLAG_PRODUCT_PERMUTATION] @@ -70,7 +67,6 @@ def save_permutation(): objsPermutation = [] for permutation in permutations: objsPermutation.append(Product_Permutation.from_json(permutation)) - # model_save = Model_View_Store_Product_Permutation() # filters_product=filters_form) Helper_App.console_log(f'objsPermutation={objsPermutation}') Model_View_Store_Product_Permutation.save_permutations(data.get('comment', 'No comment'), objsPermutation) diff --git a/controllers/store/product_variation.py b/controllers/store/product_variation.py index 88a8e134..b9874bbe 100644 --- a/controllers/store/product_variation.py +++ b/controllers/store/product_variation.py @@ -55,7 +55,6 @@ def save_product_variation(): Model_View_Store_Product_Variation.FLAG_STATUS: Model_View_Store_Product_Variation.FLAG_FAILURE, Model_View_Store_Product_Variation.FLAG_MESSAGE: f'Filters form invalid.\n{form_filters.errors}' }) - # filters_form = Filters_Product_Variation.from_form(form_filters) Helper_App.console_log(f'form_filters: {form_filters}') product_variation_types = data[Model_View_Store_Product_Variation.FLAG_PRODUCT_VARIATION_TYPE] @@ -67,7 +66,6 @@ def save_product_variation(): objs_product_variation_type = [] for product_variation_type in product_variation_types: objs_product_variation_type.append(Product_Variation_Type.from_json(product_variation_type)) - # model_save = Model_View_Store_Product_Variation() # filters_product_variation=filters_form) Helper_App.console_log(f'objs_product_variation_type={objs_product_variation_type}') save_errors = Model_View_Store_Product_Variation.save_product_variations(data.get('comment', 'No comment'), objs_product_variation_type) diff --git a/controllers/store/stock_item.py b/controllers/store/stock_item.py index 613347d9..ff780ff6 100644 --- a/controllers/store/stock_item.py +++ b/controllers/store/stock_item.py @@ -39,8 +39,6 @@ def stock_items(): Helper_App.console_log(f'form_filters={form_filters}') model = Model_View_Store_Stock_Item(form_filters) if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', data = jsonify({ Model_View_Store_Stock_Item.FLAG_CALLBACK: Model_View_Store_Stock_Item.HASH_PAGE_STORE_STOCK_ITEMS }))) - # return requests.post(f"{current_app.config['URL_HOST']}{url_for('routes_user.login')}", json={ Model_View_Store_Stock_Item.FLAG_CALLBACK: Model_View_Store_Stock_Item.HASH_PAGE_STORE_STOCK_ITEMS }) return redirect(url_for('routes_core.home')) return render_template('pages/store/_stock_items.html', model = model, datetime = datetime) @@ -50,28 +48,6 @@ def save_stock_item(): data = Helper_App.get_request_data(request) try: form_filters = Filters_Stock_Item.from_json(data[Model_View_Store_Stock_Item.FLAG_FORM_FILTERS]) - """ - if not form_filters.validate_on_submit(): - error_keys = list(form_filters.errors.keys()) - try: - error_keys.remove(Stock_Item.ATTR_ID_PRODUCT_CATEGORY) - "" - if not av.val_int(form_filters.id_product_category.data): - form_filters.errors[Stock_Item.ATTR_ID_PRODUCT_CATEGORY] = ['Invalid category.'] - "" - except: - pass - try: - error_keys.remove(Stock_Item.ATTR_ID_PRODUCT) - except: - pass - if error_keys: - return jsonify({ - Model_View_Store_Stock_Item.FLAG_STATUS: Model_View_Store_Stock_Item.FLAG_FAILURE, - Model_View_Store_Stock_Item.FLAG_MESSAGE: f'Form invalid.\n{form_filters.errors}' - }) - """ - # filters_form = Filters_Stock_Item.from_form(form_filters) Helper_App.console_log(f'form_filters: {form_filters}') stock_items = data[Model_View_Store_Stock_Item.FLAG_STOCK_ITEM] @@ -84,7 +60,6 @@ def save_stock_item(): objs_stock_item = [] for stock_item in stock_items: objs_stock_item.append(Stock_Item.from_json(stock_item)) - # model_save = Model_View_Store_Stock_Item() # filters_product=filters_form) Helper_App.console_log(f'objs_stock_item={objs_stock_item}') save_errors = Model_View_Store_Stock_Item.save_stock_items(data.get('comment', 'No comment'), objs_stock_item) if len(save_errors) > 0: diff --git a/controllers/user.py b/controllers/user.py index bc0bc673..1a64a895 100644 --- a/controllers/user.py +++ b/controllers/user.py @@ -116,7 +116,7 @@ def login(): return jsonify({'status': 'error', 'message': str(e)}), 400 -@routes_user.route("/login_callback") # / +@routes_user.route("/login_callback") @handle_db_disconnect def login_callback(): Helper_App.console_log('login_callback') @@ -128,25 +128,13 @@ def login_callback(): error_text = f'Error: {error_state}: {error_description}' Helper_App.console_log(error_text) return login() - # Helper_App.console_log(f'code: {code}') token = None try: token = oauth.auth0.authorize_access_token() except Exception as e: - # Log the error for debugging Helper_App.console_log(f"Error: {str(e)}") session[current_app.config['ID_TOKEN_USER']] = token - # import user id - """ - Helper_App.console_log(f'str(type(token)) = {str(type(token))}') - Helper_App.console_log(f'token = {token}') - userinfo = token.get('userinfo') - Helper_App.console_log(f'user info: {userinfo}') - # id_user = token.get('sub') - id_user = userinfo.get('sub') - Helper_App.console_log(f'user ID: {id_user}') - """ - user = User.from_json_auth0(token) # datastore_user.get_user_auth0() + user = User.from_json_auth0(token) Helper_App.console_log(f'user: {user}') filters = Parameters_User.from_user(user) datastore_user = DataStore_User() @@ -168,13 +156,10 @@ def login_callback(): Helper_App.console_log('hash is none') state = request.args.get('state') Helper_App.console_log(f'state: {state}') - hash_callback = state # .get('hash_callback') + hash_callback = state Helper_App.console_log(f'hash_callback: {hash_callback}') except: Helper_App.console_log("get hash callback failed") - # id_user = get_id_user() - # add user to database - # DataStore_Store().add_new_user(id_user) # this is part of get basket - should occur on page load Helper_App.console_log(f'user session: {session[Model_View_Base.FLAG_USER]}') return redirect(f"{current_app.config['URL_HOST']}{hash_callback}") @@ -188,38 +173,15 @@ def logout(): { "returnTo": url_for("routes_user.logout_callback", _external=True), "client_id": current_app.config['ID_AUTH0_CLIENT'], - }# , - # quote_via=quote_plus, + } ) Helper_App.console_log(f"Redirecting to {url_logout}") return redirect(url_logout) -@routes_user.route("/logout_callback") # / +@routes_user.route("/logout_callback") @handle_db_disconnect def logout_callback(): return redirect(url_for('routes_core.home')) - try: - session[current_app.ID_TOKEN_USER] = None - user = User() - try: - hash_callback = token.get('hash_callback') - if hash_callback is None: - Helper_App.console_log('hash is none') - state = request.args.get('state') - Helper_App.console_log(f'state: {state}') - hash_callback = state # .get('hash_callback') - Helper_App.console_log(f'hash_callback: {hash_callback}') - except: - Helper_App.console_log("get hash callback failed") - # id_user = get_id_user() - # add user to database - # DataStore_Store().add_new_user(id_user) # this is part of get basket - should occur on page load - - Helper_App.console_log(f'user session: {session[Model_View_Base.FLAG_USER]}') - return redirect(f'{current_app.URL_HOST}{hash_callback}') - except Exception as e: - return jsonify({Model_View_Base.FLAG_STATUS: Model_View_Base.FLAG_FAILURE, Model_View_Base.FLAG_MESSAGE: f'Controller error.\n{e}'}) - @routes_user.route("/user") def user(): @@ -235,7 +197,6 @@ def user(): break model.users = [model.user] if not model.is_user_logged_in: - # return redirect(url_for('routes_user.login', data = jsonify({ Model_View_User.FLAG_CALLBACK: Model_View_User.HASH_PAGE_USER_ACCOUNT }))) return redirect(url_for('routes_core.home')) html_body = render_template('pages/user/_user.html', model = model) except Exception as e: diff --git a/datastores/datastore_base.py b/datastores/datastore_base.py index 2d486680..4c3a297f 100644 --- a/datastores/datastore_base.py +++ b/datastores/datastore_base.py @@ -14,16 +14,6 @@ Datastore for Store # from routes import bp_home import lib.argument_validation as av from business_objects.store.access_level import Access_Level -""" -from business_objects.store.basket import Basket, Basket_Item -from business_objects.store.product_category import Product_Category_Container, Product_Category -from business_objects.store.currency import Currency -from business_objects.store.image import Image -from business_objects.store.delivery_option import Delivery_Option -from business_objects.store.discount import Discount -from business_objects.store.order import Order -from business_objects.store.product import Product, Product_Permutation, Product_Price, Parameters_Product # Permutation_Variation_Link -""" from business_objects.region import Region from business_objects.sql_error import SQL_Error from business_objects.store.stock_item import Stock_Item @@ -48,29 +38,11 @@ from datetime import datetime import time from sqlalchemy.exc import OperationalError -# db = SQLAlchemy() - class DataStore_Base(BaseModel): - # Global constants - # Attributes - """ - app: Flask = None - db: SQLAlchemy = None - session: object = None - """ - - # model_config = ConfigDict(arbitrary_types_allowed=True) - def __init__(self, **kwargs): super().__init__(**kwargs) - # Constructor - """ - self.db = db - self.app = current_app - with self.app.app_context(): - self.session = session - """ + @staticmethod def db_procedure_execute(proc_name, argument_dict_list = None): # Argument validation @@ -78,9 +50,7 @@ class DataStore_Base(BaseModel): av.val_str(proc_name, 'proc_name', _m) has_arguments = not str(type(argument_dict_list)) == "" if has_arguments: - # av.val_list_instances(argument_dict_list, 'argument_dict_list', _m, dict) pass - # Methods proc_string = f'CALL {proc_name}(' if has_arguments: arg_keys = list(argument_dict_list.keys()) @@ -90,15 +60,12 @@ class DataStore_Base(BaseModel): proc_string = text(proc_string) Helper_App.console_log(f'{_m}\nproc_string: {proc_string}\nargs: {argument_dict_list}') - # with self.db.session.begin() as session: - # conn = Helper_DB_MySQL(self.app).get_db_connection() - if has_arguments: result = db.session.execute(proc_string, argument_dict_list) else: result = db.session.execute(proc_string) Helper_App.console_log(f'result: {result}') - # conn.session.remove() + return result cursor = result.cursor result_set_1 = cursor.fetchall() @@ -200,7 +167,7 @@ class DataStore_Base(BaseModel): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -217,7 +184,6 @@ class DataStore_Base(BaseModel): if db.session.dirty or db.session.new or db.session.deleted: Helper_App.console_log("Session is not clean") return - # Assuming `permanent_table_name` is a string representing the table name table_object = db.metadata.tables.get(permanent_table_name) if table_object is None: Helper_App.console_log(f"Table {permanent_table_name} not found in metadata.") @@ -225,30 +191,7 @@ class DataStore_Base(BaseModel): else: expected_columns = set(column.name for column in db.inspect(table_object).columns) Helper_App.console_log(f'expected_columns: {expected_columns}') - """ v1, v2 - try: - for i in range(0, len(records), batch_size): - "" v1 - batch = records[i:i+batch_size] - Helper_App.console_log(f'batch: {batch}') - db.session.bulk_save_objects(batch) - "" - "" - data = [object.to_json() for object in batch] - Helper_App.console_log(f'data: {data}') - for row in data: - row_keys = set(row.keys()) - if row_keys != expected_columns: - Helper_App.console_log(f"Column mismatch in row: {row}") - Helper_App.console_log(f'missing columns: {expected_columns - row_keys}') - Helper_App.console_log(f'extra columns: {row_keys - expected_columns}') - # db.session.bulk_insert_mappings(permanent_table_name, data) - "" - except Exception as e: - Helper_App.console_log(f'{_m}\n{e}') - db.session.rollback() - raise e - """ + max_retries = 3 initial_backoff = 1 for i in range(0, len(records), batch_size): @@ -269,7 +212,6 @@ class DataStore_Base(BaseModel): time.sleep(wait_time) retries += 1 - # Ensure the session is clean for the retry db.session.rollback() except Exception as e: db.session.rollback() @@ -282,8 +224,6 @@ class DataStore_Base(BaseModel): filters = Filters_Access_Level() av.val_instance(filters, 'filters', _m, Filters_Access_Level) argument_dict = filters.to_json() - # user = cls.get_user_session() - # argument_dict['a_id_user'] = 1 # 'auth0|6582b95c895d09a70ba10fef' # id_user Helper_App.console_log(f'argument_dict: {argument_dict}') Helper_App.console_log('executing p_shop_get_many_access_level') result = cls.db_procedure_execute('p_shop_get_many_access_level', argument_dict) @@ -304,7 +244,7 @@ class DataStore_Base(BaseModel): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -319,8 +259,6 @@ class DataStore_Base(BaseModel): filters = Filters_Unit_Measurement() av.val_instance(filters, 'filters', _m, Filters_Unit_Measurement) argument_dict = filters.to_json() - # user = cls.get_user_session() - # argument_dict['a_id_user'] = 1 # 'auth0|6582b95c895d09a70ba10fef' # id_user Helper_App.console_log(f'argument_dict: {argument_dict}') Helper_App.console_log('executing p_shop_get_many_unit_measurement') result = cls.db_procedure_execute('p_shop_get_many_unit_measurement', argument_dict) @@ -341,7 +279,7 @@ class DataStore_Base(BaseModel): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -364,7 +302,6 @@ class DataStore_Base(BaseModel): cursor = result.cursor Helper_App.console_log('data received') - # cursor.nextset() result_set_1 = cursor.fetchall() regions = [] for row in result_set_1: diff --git a/datastores/datastore_store_base.py b/datastores/datastore_store_base.py index 235fd74e..da2909d3 100644 --- a/datastores/datastore_store_base.py +++ b/datastores/datastore_store_base.py @@ -12,18 +12,16 @@ Datastore for Store # internal # from routes import bp_home -from business_objects.store.basket import Basket, Basket_Item from business_objects.store.product_category import Product_Category_Container, Product_Category from business_objects.currency import Currency from business_objects.store.image import Image -from business_objects.store.delivery_option import Delivery_Option from business_objects.region import Region from business_objects.store.discount import Discount -from business_objects.store.order import Order from business_objects.store.plant import Plant from business_objects.store.product import Product, Product_Permutation, Parameters_Product from business_objects.sql_error import SQL_Error from business_objects.store.stock_item import Stock_Item +from business_objects.store.store_base import Store_Base from business_objects.store.storage_location import Storage_Location from business_objects.store.product_variation import Product_Variation, Parameters_Product_Variation from business_objects.store.product_variation_type import Product_Variation_Type @@ -44,30 +42,22 @@ from pydantic import BaseModel, ConfigDict from typing import ClassVar from datetime import datetime -# db = SQLAlchemy() class DataStore_Store_Base(DataStore_Base): - # Global constants - KEY_BASKET: ClassVar[str] = Basket.KEY_BASKET - KEY_IS_INCLUDED_VAT: ClassVar[str] = Basket.KEY_IS_INCLUDED_VAT # 'is_included_VAT' - KEY_ID_CURRENCY: ClassVar[str] = Basket.KEY_ID_CURRENCY # 'id_currency' - KEY_ID_REGION_DELIVERY: ClassVar[str] = Basket.KEY_ID_REGION_DELIVERY # 'id_region_delivery' - # Attributes + KEY_BASKET: ClassVar[str] = Store_Base.KEY_BASKET + KEY_IS_INCLUDED_VAT: ClassVar[str] = Store_Base.KEY_IS_INCLUDED_VAT + KEY_ID_CURRENCY: ClassVar[str] = Store_Base.KEY_ID_CURRENCY + KEY_ID_REGION_DELIVERY: ClassVar[str] = Store_Base.KEY_ID_REGION_DELIVERY def __init__(self, **kwargs): super().__init__(**kwargs) @classmethod def get_many_product(cls, product_filters): - # redundant argument validation? _m = 'DataStore_Store_Base.get_many_product' av.val_instance(product_filters, 'product_filters', _m, Parameters_Product) argument_dict = product_filters.to_json() user = cls.get_user_session() - """ - argument_dict['a_id_user'] = user.id_user # 'auth0|6582b95c895d09a70ba10fef' # id_user - argument_dict['a_debug'] = 0 - """ argument_dict = { 'a_id_user': user.id_user , **argument_dict @@ -139,18 +129,11 @@ class DataStore_Store_Base(DataStore_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") category_list.get_all_product_variation_trees() - """ - for category in category_list.categories: - Helper_App.console_log(f'category: {category.name}') - for product in category.products: - permutation = product.get_permutation_selected() - Helper_App.console_log(f'product: {product.name}\nselected permutation: {permutation}') - """ if len(errors) > 0: for error in errors: @@ -172,18 +155,8 @@ class DataStore_Store_Base(DataStore_Base): cursor.close() Helper_App.console_log(f'get many category_list: {category_list}') - return category_list, errors # categories, category_index + return category_list, errors - """ - def get_many_id_price(self, product_ids): - _m = 'DataStore_Store_Base.get_many_id_price' - av.val_str(product_ids, 'product_ids', _m) - price_ids = [] - for product_id in product_ids.split(','): - if product_id == 'prod_PB0NUOSEs06ymG': - price_ids.append() # get price id - return price_ids - """ @staticmethod def get_ids_permutation_from_error_availability(msg_error_availability): ids_permutation = [] @@ -210,7 +183,6 @@ class DataStore_Store_Base(DataStore_Base): cursor = result.cursor Helper_App.console_log('data received') - # cursor.nextset() result_set_1 = cursor.fetchall() plants = [] for row in result_set_1: @@ -236,7 +208,6 @@ class DataStore_Store_Base(DataStore_Base): cursor = result.cursor Helper_App.console_log('data received') - # cursor.nextset() result_set_1 = cursor.fetchall() storage_locations = [] for row in result_set_1: @@ -262,7 +233,6 @@ class DataStore_Store_Base(DataStore_Base): cursor = result.cursor Helper_App.console_log('data received') - # cursor.nextset() result_set_1 = cursor.fetchall() currencies = [] for row in result_set_1: @@ -287,36 +257,22 @@ class DataStore_Store_Base(DataStore_Base): av.val_instance(variation_filters, 'variation_filters', _m, Parameters_Product_Variation) guid = Helper_DB_MySQL.create_guid() - # now = datetime.now() - # user = self.get_user_session() - - """ - argument_dict_list = { - 'a_id_user': id_user, - 'a_comment': comment, - 'a_guid': guid - } - """ user = cls.get_user_session() argument_dict_list = { - # 'a_guid': guid 'a_id_user': user.id_user , **variation_filters.to_json() , 'a_debug': 0 } - # argument_dict_list['a_guid'] = guid result = cls.db_procedure_execute('p_shop_get_many_product_variation', argument_dict_list) cursor = result.cursor result_set_vt = cursor.fetchall() # Product_Variation Types - # variation_container = Product_Variation_Container() variation_types = [] index_variation_type = {} for row in result_set_vt: - new_variation_type = Product_Variation_Type.from_DB_get_many_product_variation(row) - # variation_container.add_product_variation_type(new_variation_type) + new_variation_type = Product_Variation_Type.from_DB_get_many_product_variation(row) index_variation_type[new_variation_type.id_type] = len(variation_types) variation_types.append(new_variation_type) @@ -325,12 +281,9 @@ class DataStore_Store_Base(DataStore_Base): # Product_Variations cursor.nextset() result_set_v = cursor.fetchall() - # variations = Product_Variation_Container() variations = [] for row in result_set_v: new_variation = Product_Variation.from_DB_get_many_product_variation(row) - # new_variation.variation_type = variation_types_dict[new_variation.id_type] - # variation_container.add_product_variation(new_variation) variation_types[index_variation_type[new_variation.id_type]].variations.append(new_variation) variations.append(new_variation) @@ -339,7 +292,7 @@ class DataStore_Store_Base(DataStore_Base): result_set_e = cursor.fetchall() Helper_App.console_log(f'raw errors: {result_set_e}') if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/datastores/datastore_store_basket.py b/datastores/datastore_store_basket.py deleted file mode 100644 index e7731cbb..00000000 --- a/datastores/datastore_store_basket.py +++ /dev/null @@ -1,143 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: DataStores -Feature: Store Basket DataStore - -Description: -Datastore for Store Baskets -""" - -# internal -# from routes import bp_home -import lib.argument_validation as av -from business_objects.store.basket import Basket, Basket_Item -from business_objects.sql_error import SQL_Error -from datastores.datastore_store_base import DataStore_Store_Base -from helpers.helper_app import Helper_App -# from helpers.helper_db_mysql import Helper_DB_MySQL -# from models.model_view_store_checkout import Model_View_Store_Checkout # circular! -from extensions import db -# external -# from abc import ABC, abstractmethod, abstractproperty -from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import text -import stripe -import os -from flask import Flask, session, current_app -from pydantic import BaseModel, ConfigDict -from typing import ClassVar -from datetime import datetime - -# db = SQLAlchemy() - - -class DataStore_Store_Basket(DataStore_Store_Base): - # Global constants - KEY_BASKET: ClassVar[str] = Basket.KEY_BASKET - # Attributes - - def __init__(self, **kwargs): - super().__init__(**kwargs) - - def get_metadata_basket(json_request): - try: - basket = json_request[DataStore_Store_Basket.KEY_BASKET] - except KeyError: - basket = {DataStore_Store_Basket.KEY_IS_INCLUDED_VAT: True, DataStore_Store_Basket.KEY_ID_CURRENCY: 1, DataStore_Store_Basket.KEY_ID_REGION_DELIVERY: 1} - is_included_VAT = basket[DataStore_Store_Basket.KEY_IS_INCLUDED_VAT] - id_currency = basket[DataStore_Store_Basket.KEY_ID_CURRENCY] - id_region_delivery = basket[DataStore_Store_Basket.KEY_ID_REGION_DELIVERY] - return id_currency, id_region_delivery, is_included_VAT - - def edit_basket(self, ids_permutation_basket, quantities_permutation_basket, id_permutation_edit, quantity_permutation_edit, sum_not_edit, id_currency, id_region_delivery, is_included_VAT): - # redundant argument validation? - _m = 'DataStore_Store_Base.edit_basket' - Helper_App.console_log(f'{_m}\nstarting...') - # av.val_instance(filters, 'filters', _m, Parameters_Product_Category) - # av.val_str(ids_product_basket, 'ids_product_basket', _m) - av.val_str(ids_permutation_basket, 'ids_permutation_basket', _m) - # av.val_str(quantities_product_basket, 'quantities_product_basket', _m) - av.val_str(quantities_permutation_basket, 'quantities_permutation_basket', _m) - """ - if id_product_edit == 'None': - id_product_edit = None - else: - Helper_App.console_log(f'id_product_edit: {id_product_edit}') - av.val_int(id_product_edit, 'id_product_edit', _m) - """ - if id_permutation_edit == 'None' or str(type(id_permutation_edit)) =="": - id_permutation_edit = None - else: - Helper_App.console_log(f'id_permutation_edit: {id_permutation_edit}') - Helper_App.console_log(str(type(id_permutation_edit))) - av.val_int(id_permutation_edit, 'id_permutation_edit', _m) - if quantity_permutation_edit == 'None' or str(type(quantity_permutation_edit)) =="": - quantity_permutation_edit = None - else: - Helper_App.console_log(f'quantity_permutation_edit: {quantity_permutation_edit}') - av.val_int(quantity_permutation_edit, 'quantity_permutation_edit', _m) - if sum_not_edit == 'None': - sum_not_edit = None - else: - Helper_App.console_log(f'sum_not_edit: {sum_not_edit}') - av.val_bool(sum_not_edit, 'sum_not_edit', _m) - - argument_dict_list = { - 'a_id_user': self.info_user.get('sub'), - # 'a_ids_product_basket': ids_product_basket, - 'a_ids_permutation_basket': ids_permutation_basket, - # 'a_quantities_product_basket': quantities_product_basket, - 'a_quantities_permutation_basket': quantities_permutation_basket, - # 'a_id_product_edit': id_product_edit if id_permutation_edit is None else None, - 'a_id_permutation_edit': id_permutation_edit, - 'a_quantity_permutation_edit': quantity_permutation_edit, - 'a_sum_not_edit': 1 if sum_not_edit else 0, - 'a_id_currency': id_currency, - 'a_id_region_purchase': id_region_delivery - } - - result = self.db_procedure_execute('p_shop_edit_user_basket', argument_dict_list) - Helper_App.console_log('data received') - - cursor = result.cursor - - # categories, category_index = DataStore_Store_Base.input_many_product(cursor) - category_list, errors = DataStore_Store_Base.input_many_product(cursor) - - Helper_App.console_log(f'cursor: {str(cursor)}') - - # Basket - if not cursor.nextset(): - raise Exception("No more query results! Cannot open basket contents") - result_set = cursor.fetchall() - Helper_App.console_log(f'raw basket: {result_set}') - # Helper_App.console_log(f'variations: {result_set_3}') - # variations = [Product_Variation(**row) for row in result_set_3] - basket = Basket(is_included_VAT, id_currency, id_region_delivery) - for row in result_set: - index_category = category_list.get_index_category_from_id(row[0]) - category = category_list.categories[index_category] - index_product = category.get_index_product_from_id(row[1]) - product = category.products[index_product] - basket_item = Basket_Item.from_product_and_quantity_and_VAT_included(product, row[7], self.app.is_included_VAT) - Helper_App.console_log(f'adding basket item: {row}') - Helper_App.console_log(f'basket item: {basket_item}') - basket.add_item(basket_item) # basket.append(basket_item) # Basket_Item(category.name, product, row[4])) - - Helper_App.console_log(f'basket: {basket}') - - # Errors - cursor.nextset() - result_set_e = cursor.fetchall() - Helper_App.console_log(f'raw errors: {result_set_e}') - if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_2] - for error in errors: - Helper_App.console_log(f"Error [{error.code}]: {error.msg}") - - DataStore_Store_Base.db_cursor_clear(cursor) - - return basket diff --git a/datastores/datastore_store_manufacturing_purchase_order.py b/datastores/datastore_store_manufacturing_purchase_order.py index f0840480..e18c5fb2 100644 --- a/datastores/datastore_store_manufacturing_purchase_order.py +++ b/datastores/datastore_store_manufacturing_purchase_order.py @@ -78,7 +78,7 @@ class DataStore_Store_Manufacturing_Purchase_Order(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/datastores/datastore_store_product.py b/datastores/datastore_store_product.py index 7dea7b47..497d25f5 100644 --- a/datastores/datastore_store_product.py +++ b/datastores/datastore_store_product.py @@ -21,65 +21,7 @@ from helpers.helper_db_mysql import Helper_DB_MySQL from extensions import db # external # from abc import ABC, abstractmethod, abstractproperty -from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import text -import stripe -import os -from flask import Flask, session, current_app -from pydantic import BaseModel, ConfigDict -from typing import ClassVar -from datetime import datetime -# db = SQLAlchemy() - -""" -class Table_Shop_Product_Category(db.Model): - __tablename__ = 'Shop_Product_Category' - id_category: int = db.Column(db.Integer, primary_key=True) - code: str = db.Column(db.String(50)) - name: str = db.Column(db.String(255)) - description: str = db.Column(db.String(4000)) - active: bool = db.Column(db.Boolean) - display_order: int = db.Column(db.Integer) - created_on: datetime = db.Column(db.DateTime) - created_by: int = db.Column(db.Integer) - id_change_set: int = db.Column(db.Integer) -""" -""" -class Row_Shop_Product_Temp(db.Model): - __tablename__ = 'Shop_Product_Temp' - __table_args__ = { 'extend_existing': True } - id_product: int = db.Column(db.Integer, primary_key=True) - id_category: int = db.Column(db.Integer) - name: str = db.Column(db.String(50)) - has_variations: str = db.Column(db.String(255)) - id_access_level_required: int = db.Column(db.Integer) - active: bool = db.Column(db.Boolean) - display_order: int = db.Column(db.Integer) - guid: str = db.Column(db.BINARY(36)) - - @classmethod - def from_product(cls, product): - row = cls() - row.id_product = product.id_product[0] if isinstance(product.id_product, tuple) else product.id_product - row.id_category = product.id_category[0] if isinstance(product.id_category, tuple) else product.id_category - row.name = product.name[0] if isinstance(product.name, tuple) else product.name - row.id_access_level_required = product.id_access_level_required[0] if isinstance(product.id_access_level_required, tuple) else product.id_access_level_required - row.active = product.active - row.display_order = product.display_order - return row - def to_json(self): - return { - 'id_product': self.id_product, - 'id_category': self.id_category, - 'name': self.name, - 'has_variations': self.has_variations, - 'id_access_level_required': self.id_access_level_required, - 'active': av.input_bool(self.active, self.FLAG_ACTIVE, f'{self.__class__.__name__}.to_json'), - 'display_order': self.display_order, - 'guid': self.guid, - } -""" class DataStore_Store_Product(DataStore_Store_Base): def __init__(self): @@ -115,16 +57,15 @@ class DataStore_Store_Product(DataStore_Store_Base): } save_result = cls.db_procedure_execute('p_shop_save_product', argument_dict_list) - cursor = save_result # .cursor + cursor = save_result Helper_App.console_log('data received') # Errors - # cursor.nextset() result_set_e = cursor.fetchall() Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") try: diff --git a/datastores/datastore_store_product_category.py b/datastores/datastore_store_product_category.py index 6a32c4e9..e22a4ee6 100644 --- a/datastores/datastore_store_product_category.py +++ b/datastores/datastore_store_product_category.py @@ -40,8 +40,6 @@ class DataStore_Store_Product_Category(DataStore_Store_Base): _m = 'DataStore_Store_Product_Category.save_categories' Helper_App.console_log(f'{_m}\nstarting...') Helper_App.console_log(f'comment: {comment}\ncategories: {categories}') - # av.val_str(comment, 'comment', _m) - # av.val_list_instances(categories, 'categories', _m, Product_Category, 1) guid = Helper_DB_MySQL.create_guid() now = datetime.now() @@ -50,32 +48,15 @@ class DataStore_Store_Product_Category(DataStore_Store_Base): id_category_new = 0 for category in categories: row = Product_Category_Temp.from_product_category(category) - # row = category.to_temporary_record() - # id_tmp = if row.id_category == '': id_category_new -= 1 row.id_category = id_category_new else: Helper_App.console_log(f'row.id_category: {row.id_category}') row.guid = guid - # row.created_on = now - # row.created_by = user.id_user rows.append(row) Helper_App.console_log(f'rows: {rows}') - """ - cursor = db.cursor() - Helper_App.console_log('cursor created') - cursor.executemany( - 'INSERT INTO Shop_Product_Category_Temp (id_category, code, name, description, active, display_order, guid, created_on, created_by) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', - categories - ) - Helper_App.console_log('bulk upload executed') - db.commit() - Helper_App.console_log('bulk upload committed') - cursor.close() - Helper_App.console_log('cursor closed') - """ DataStore_Store_Base.upload_bulk(Product_Category_Temp.__tablename__, rows, 1000) argument_dict_list = { @@ -92,7 +73,7 @@ class DataStore_Store_Product_Category(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/datastores/datastore_store_product_permutation.py b/datastores/datastore_store_product_permutation.py index 2d9dc6b7..e296d754 100644 --- a/datastores/datastore_store_product_permutation.py +++ b/datastores/datastore_store_product_permutation.py @@ -42,58 +42,17 @@ class DataStore_Store_Product_Permutation(DataStore_Store_Base): def save_permutations(cls, comment, permutations): _m = 'DataStore_Store_Product_Permutation.save_permutations' av.val_str(comment, 'comment', _m) - # av.val_list(permutations, 'list_permutations', _m, Product_Permutation, 1) guid = Helper_DB_MySQL.create_guid_str() now = datetime.now() user = cls.get_user_session() rows = [] for permutation in permutations: - # row = permutation.to_temporary_record() row = Product_Permutation_Temp.from_product_permutation(permutation) row.guid = guid rows.append(row) - - Helper_App.console_log(f'rows: {rows}') - - """ - cursor = db.cursor() - Helper_App.console_log('cursor created') - cursor.executemany( - '''INSERT INTO Shop_Product_Permutation_Temp ( - id_permutation, - id_product, - description, - cost_local, - id_currency_cost, - profit_local_min, - latency_manufacture, - id_unit_measurement_quantity, - count_unit_measurement_quantity, - quantity_min, - quantity_max, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - does_expire_faster_once_unsealed, - id_unit_measurement_interval_expiration_unsealed, - count_interval_expiration_unsealed, - active, - guid - ) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', - rows - ) - Helper_App.console_log('cursor executed') - db.commit() - Helper_App.console_log('cursor committed') - cursor.close() - Helper_App.console_log('cursor closed') - """ + DataStore_Store_Base.upload_bulk(Product_Permutation_Temp.__tablename__, rows, 1000) - Helper_App.console_log('bulk uploaded') argument_dict_list = { 'a_comment': comment, @@ -103,4 +62,3 @@ class DataStore_Store_Product_Permutation(DataStore_Store_Base): } results = cls.db_procedure_execute('p_shop_save_product_permutation', argument_dict_list) DataStore_Store_Base.db_cursor_clear(results.cursor) - Helper_App.console_log('saved product permutations') diff --git a/datastores/datastore_store_stock_item.py b/datastores/datastore_store_stock_item.py index fbc933e4..09c860fa 100644 --- a/datastores/datastore_store_stock_item.py +++ b/datastores/datastore_store_stock_item.py @@ -31,19 +31,12 @@ from pydantic import BaseModel, ConfigDict from typing import ClassVar from datetime import datetime -# db = SQLAlchemy() - class DataStore_Store_Stock_Item(DataStore_Store_Base): - # Global constants - # Attributes - def __init__(self): super().__init__() - # Stock Items def get_many_stock_item(self, parameters_stock_item, category_list): - # redundant argument validation? _m = 'DataStore_Store_Stock_Item.get_many_stock_item' av.val_instance(parameters_stock_item, 'parameters_stock_item', _m, Parameters_Stock_Item) argument_dict = parameters_stock_item.to_json() @@ -73,7 +66,7 @@ class DataStore_Store_Stock_Item(DataStore_Store_Base): Helper_App.console_log(f'raw categories: {result_set_1}') for row in result_set_1: new_stock_item = Stock_Item.from_DB_stock_item(row) - category_list.add_stock_item(new_stock_item) # , row) + category_list.add_stock_item(new_stock_item) # Errors cursor.nextset() @@ -81,27 +74,11 @@ class DataStore_Store_Stock_Item(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") - """ - if len(errors) > 0: - for error in errors: - if error.code == 'PRODUCT_AVAILABILITY': - ids_permutation_unavailable = DataStore_Store_Stock_Item.get_ids_permutation_from_error_availability(error.msg) - for id_permutation in ids_permutation_unavailable: - index_category = category_list.get_index_category_from_id_permutation(id_permutation) - category = category_list.categories[index_category] - index_product = category.get_index_product_from_id_permutation(id_permutation) - product = category.products[index_product] - index_permutation = product.get_index_permutation_from_id(id_permutation) - permutation = product.permutations[index_permutation] - permutation.is_available = False - if 'region' in error.msg or 'currency' in error.msg: - permutation.is_unavailable_in_currency_or_region = True - """ DataStore_Store_Stock_Item.db_cursor_clear(cursor) - return category_list, errors # categories, category_index + return category_list, errors @classmethod def save_stock_items(cls, comment, stock_items): @@ -113,7 +90,6 @@ class DataStore_Store_Stock_Item(DataStore_Store_Base): user = cls.get_user_session() rows = [] for stock_item in stock_items: - # row = permutation.to_temporary_record() row = Stock_Item_Temp.from_stock_item(stock_item) row.guid = guid rows.append(row) @@ -139,7 +115,7 @@ class DataStore_Store_Stock_Item(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") DataStore_Store_Stock_Item.db_cursor_clear(cursor) diff --git a/datastores/datastore_store_stripe.py b/datastores/datastore_store_stripe.py deleted file mode 100644 index 93da1d13..00000000 --- a/datastores/datastore_store_stripe.py +++ /dev/null @@ -1,195 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: DataStores -Feature: Store Stripe DataStore - -Description: -Datastore for Store Stripe service -""" - -# internal -# from routes import bp_home -import lib.argument_validation as av -from business_objects.store.basket import Basket, Basket_Item -from business_objects.store.product import Product, Product_Permutation, Product_Price, Parameters_Product -from business_objects.sql_error import SQL_Error -from datastores.datastore_store_base import DataStore_Store_Base -# from helpers.helper_db_mysql import Helper_DB_MySQL -# from models.model_view_store_checkout import Model_View_Store_Checkout # circular! -from extensions import db -from helpers.helper_app import Helper_App -# external -# from abc import ABC, abstractmethod, abstractproperty -from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import text -import stripe -import os -from flask import Flask, session, current_app -from pydantic import BaseModel, ConfigDict -from typing import ClassVar -from datetime import datetime - -# db = SQLAlchemy() - - -class DataStore_Store_Stripe(DataStore_Store_Base): - # Global constants - # Attributes - key_public_stripe: str = None - key_secret_stripe: str = None - - def __init__(self): - super().__init__() - self.key_secret_stripe = os.environ.get("KEY_SECRET_STRIPE") - self.key_public_stripe = os.environ.get("KEY_PUBLIC_STRIPE") - - # For sample support and debugging, not required for production: - stripe.set_app_info( - 'stripe-samples/checkout-one-time-payments', - version='0.0.1', - url='https://github.com/stripe-samples/checkout-one-time-payments') - stripe.api_key = self.key_secret_stripe - - def get_many_stripe_product_new(self): - _m = 'DataStore_Store_Stripe.get_many_stripe_product_new' - _m_db = 'p_shop_get_many_stripe_product_new' - # av.val_str(id_user) - # validation conducted by server - - argument_dict_list = { - 'a_id_user': self.info_user - } - - Helper_App.console_log(f'executing {_m_db}') - result = self.db_procedure_execute(_m_db, argument_dict_list) - cursor = result.cursor - Helper_App.console_log('data received') - - - # Products - cursor.nextset() - result_set_1 = cursor.fetchall() - products = [] - for row in result_set_1: - new_product = Product.from_DB_Stripe_product(row) # Product(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]) - products.append(new_product) - Helper_App.console_log(f'products: {products}') - - # Errors - cursor.nextset() - result_set_e = cursor.fetchall() - Helper_App.console_log(f'raw errors: {result_set_e}') - if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] - for error in errors: - Helper_App.console_log(f"Error [{error.code}]: {error.msg}") - - DataStore_Store_Stripe.db_cursor_clear(cursor) - - return products - - def get_many_stripe_price_new(self): - _m = 'DataStore_Store_Stripe.get_many_stripe_price_new' - _m_db = 'p_shop_get_many_stripe_price_new' - # av.val_str(id_user) - # validation conducted by server - - argument_dict_list = { - 'a_id_user': self.info_user - } - - Helper_App.console_log(f'executing {_m_db}') - result = self.db_procedure_execute(_m_db, argument_dict_list) - cursor = result.cursor - Helper_App.console_log('data received') - - - # Products - cursor.nextset() - result_set_1 = cursor.fetchall() - products = [] - for row in result_set_1: - new_product = Product.from_DB_Stripe_price(row) # Product(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]) - products.append(new_product) - Helper_App.console_log(f'products: {products}') - - # Errors - cursor.nextset() - result_set_e = cursor.fetchall() - Helper_App.console_log(f'raw errors: {result_set_e}') - if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] - for error in errors: - Helper_App.console_log(f"Error [{error.code}]: {error.msg}") - - DataStore_Store_Stripe.db_cursor_clear(cursor) - - return products - - def get_many_product_new(self): - _m = 'DataStore_Store_Stripe.get_many_product_new' - # Stripe - new_products = self.get_many_stripe_product_new() - for product in new_products: - product.id_stripe_product = self.create_stripe_product(product) - return new_products - - def get_many_price_new(self): - _m = 'DataStore_Store_Stripe.get_many_product_new' - # Stripe - new_products = self.get_many_stripe_price_new() - for product in new_products: - product.id_stripe_price = self.create_stripe_price(product) - return new_products - - # Stripe - def create_stripe_product(self, product): # _name, product_description): - _m = 'DataStore_Store_Stripe_Checkout.create_stripe_product' - # av.val_str(product_name, 'product_name', _m) - # av.val_str(product_description, 'product_description', _m) - av.val_instance(product, 'product', _m, Product) - - Helper_App.console_log(f'stripe.api_key = {stripe.api_key}') - new_product = stripe.Product.create( - name = product.name, - description = product.description, - ) - - # Save these identifiers - Helper_App.console_log(f"Success! Here is your new Stripe product id: {new_product.id}") - - return new_product.id - - def create_stripe_price(self, product, currency): # product_id, product_price, product_currency, product_is_subscription, product_recurring_interval = '', product_interval_count = 0): - _m = 'DataStore_Store_Stripe_Checkout.create_stripe_price' - """ - av.val_str(p_id, 'p_id', _m) - av.full_val_float(p_price, 'p_price', _m, 0.01) - p_price = round(p_price, 2) - av.val_str(p_currency, 'p_currency', _m) - av.full_val_bool(p_is_subscription, 'p_is_subscription', _m) - p_is_subscription = bool(p_is_subscription) - av.val_str(p_recurring_interval, 'p_recurring_interval', _m) - av.full_val_int(p_interval_count, 'p_interval_count', _m, 1 if p_is_subscription else 0) - p_interval_count = int(p_interval_count) - """ - av.val_instance(product, 'product', _m, Product) - av.val_str(currency, 'currency', _m) - - Helper_App.console_log(f'stripe.api_key = {stripe.api_key}') - - new_product_price = stripe.Price.create( - unit_amount = product.unit_price, - currency = currency, - recurring = { "interval": product.name_recurring_interval, "interval_count": product.count_recurring_interval } if product.is_subscription else None, - product = product.id_stripe_product - ) - - # Save these identifiers - Helper_App.console_log(f"Success! Here is your Stripe product price id: {new_product_price.id} for {product.name}") - - return new_product_price.id - \ No newline at end of file diff --git a/datastores/datastore_store_supplier.py b/datastores/datastore_store_supplier.py index 1a9538ea..5538e0c9 100644 --- a/datastores/datastore_store_supplier.py +++ b/datastores/datastore_store_supplier.py @@ -79,7 +79,7 @@ class DataStore_Store_Supplier(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -135,7 +135,7 @@ class DataStore_Store_Supplier(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/datastores/datastore_store_supplier_purchase_order.py b/datastores/datastore_store_supplier_purchase_order.py index 03e2854b..93996bd9 100644 --- a/datastores/datastore_store_supplier_purchase_order.py +++ b/datastores/datastore_store_supplier_purchase_order.py @@ -79,7 +79,7 @@ class DataStore_Store_Supplier_Purchase_Order(DataStore_Store_Base): Helper_App.console_log(f'raw errors: {result_set_e}') errors = [] if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # (row[0], row[1]) + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/datastores/datastore_user.py b/datastores/datastore_user.py index 42e1a523..86ece1b0 100644 --- a/datastores/datastore_user.py +++ b/datastores/datastore_user.py @@ -37,16 +37,11 @@ db = SQLAlchemy() class DataStore_User(DataStore_Store_Base): - # Global constants - # Attributes - def __init__(self): super().__init__() def edit_user(self): - # redundant argument validation? _m = 'DataStore_User.edit_user' - # av.val_instance(filters, 'filters', _m, Filters_Product_Category) argument_dict_list = { 'a_id_user': self.info_user.get('sub'), @@ -66,7 +61,7 @@ class DataStore_User(DataStore_Store_Base): result_set_e = cursor.fetchall() Helper_App.console_log(f'raw errors: {result_set_e}') if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_2] + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -74,94 +69,28 @@ class DataStore_User(DataStore_Store_Base): return (result_set_1[0][1] == b'\x01') - """ - def get_many_user_order(self, id_user, ids_order, n_order_max, id_checkout_session): - _m = 'DataStore_User.get_many_user_order' - # av.val_str(id_user) - # validation conducted by server - - argument_dict_list = { - 'a_id_user': id_user, - 'a_ids_order': ids_order, - 'a_n_order_max': n_order_max, - 'a_id_checkout_session': id_checkout_session - } - - Helper_App.console_log('executing p_shop_get_many_user_order') - result = self.db_procedure_execute('p_shop_get_many_user_order', argument_dict_list) - cursor = result.cursor - Helper_App.console_log('data received') - - - # Discount Delivery Regions - cursor.nextset() - result_set_1 = cursor.fetchall() - orders = [] - for row in result_set_1: - new_order = Order(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) - orders.append(new_order) - Helper_App.console_log(f'orders: {orders}') - - # Errors - cursor.nextset() - result_set_e = cursor.fetchall() - Helper_App.console_log(f'raw errors: {result_set_e}') - if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] - for error in errors: - Helper_App.console_log(f"Error [{error.code}]: {error.msg}") - - DataStore_User.db_cursor_clear(cursor) - - return orders - """ - def get_many_user(self, user_filters, user=None): _m = 'DataStore_User.get_many_user' Helper_App.console_log(_m) - # av.val_str(user_filters, 'user_filters', _m) - # av.val_list(permutations, 'list_permutations', _m, Product_Permutation, 1) av.val_instance(user_filters, 'user_filters', _m, Parameters_User) guid = Helper_DB_MySQL.create_guid() - # now = datetime.now() - # user = self.get_user_session() - - """ - argument_dict_list = { - 'a_id_user': id_user, - 'a_comment': comment, - 'a_guid': guid - } - """ + if user is None: user = self.get_user_session() argument_dict_list = { - # 'a_guid': guid 'a_id_user': user.id_user , 'a_id_user_auth0': user.id_user_auth0 , **user_filters.to_json() , 'a_debug': 0 } - # argument_dict_list['a_guid'] = guid result = self.db_procedure_execute('p_get_many_user', argument_dict_list) - """ - query = text(f"SELECT * FROM Shop_Calc_User_Temp UE_T WHERE UE_T.guid = '{guid}'") - result = self.db.session.execute(query) - """ cursor = result.cursor result_set = cursor.fetchall() Helper_App.console_log(f'raw users: {result_set}') Helper_App.console_log(f'type result set: {str(type(result_set))}') Helper_App.console_log(f'len result set: {len(result_set)}') - """ - user_permission_evals = [] - for row in result_set: - user_permission_eval = User_Permission_Evaluation.from_DB_user_eval(row) - user_permission_evals.append(user_permission_eval) - Helper_App.console_log(f'user_permission_evals: {user_permission_evals}') - """ users = [] if len(result_set) > 0: for row in result_set: @@ -170,13 +99,12 @@ class DataStore_User(DataStore_Store_Base): users.append(user) Helper_App.console_log(f'user {str(type(user))}: {user}') Helper_App.console_log(f'type users: {str(type(users))}\n type user 0: {str(type(None if len(users) == 0 else users[0]))}') - # error_list, cursor = self.get_error_list_from_cursor(cursor) errors = [] cursor.nextset() result_set_e = cursor.fetchall() Helper_App.console_log(f'raw errors: {result_set_e}') if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") @@ -187,49 +115,25 @@ class DataStore_User(DataStore_Store_Base): def get_many_user(self, user_filters, user=None): _m = 'DataStore_User.get_many_user' Helper_App.console_log(_m) - # av.val_str(user_filters, 'user_filters', _m) - # av.val_list(permutations, 'list_permutations', _m, Product_Permutation, 1) av.val_instance(user_filters, 'user_filters', _m, Parameters_User) guid = Helper_DB_MySQL.create_guid() - # now = datetime.now() - # user = self.get_user_session() - """ - argument_dict_list = { - 'a_id_user': id_user, - 'a_comment': comment, - 'a_guid': guid - } - """ if user is None: user = self.get_user_session() argument_dict_list = { - # 'a_guid': guid 'a_id_user': user.id_user , 'a_id_user_auth0': user.id_user_auth0 , **user_filters.to_json() , 'a_debug': 0 } - # argument_dict_list['a_guid'] = guid result = self.db_procedure_execute('p_get_many_user', argument_dict_list) - """ - query = text(f"SELECT * FROM Shop_Calc_User_Temp UE_T WHERE UE_T.guid = '{guid}'") - result = self.db.session.execute(query) - """ cursor = result.cursor result_set = cursor.fetchall() Helper_App.console_log(f'raw users: {result_set}') Helper_App.console_log(f'type result set: {str(type(result_set))}') Helper_App.console_log(f'len result set: {len(result_set)}') - """ - user_permission_evals = [] - for row in result_set: - user_permission_eval = User_Permission_Evaluation.from_DB_user_eval(row) - user_permission_evals.append(user_permission_eval) - Helper_App.console_log(f'user_permission_evals: {user_permission_evals}') - """ users = [] if len(result_set) > 0: for row in result_set: @@ -238,13 +142,12 @@ class DataStore_User(DataStore_Store_Base): users.append(user) Helper_App.console_log(f'user {str(type(user))}: {user}') Helper_App.console_log(f'type users: {str(type(users))}\n type user 0: {str(type(None if len(users) == 0 else users[0]))}') - # error_list, cursor = self.get_error_list_from_cursor(cursor) errors = [] cursor.nextset() result_set_e = cursor.fetchall() Helper_App.console_log(f'raw errors: {result_set_e}') if len(result_set_e) > 0: - errors = [SQL_Error.from_DB_record(row) for row in result_set_e] # [SQL_Error(row[0], row[1]) for row in result_set_e] + errors = [SQL_Error.from_DB_record(row) for row in result_set_e] for error in errors: Helper_App.console_log(f"Error [{error.code}]: {error.msg}") diff --git a/forms/base.py b/forms/base.py index 567803a0..04f676c7 100644 --- a/forms/base.py +++ b/forms/base.py @@ -21,15 +21,7 @@ class Form_Base_Meta(type(FlaskForm), ABCMeta): class Form_Base(FlaskForm, metaclass=Form_Base_Meta): - """ - @classmethod - @abstractmethod - def from_filters(cls, filters): - pass - @abstractmethod - def __repr__(self): - pass - """ + def __repr__(self): fields = ', '.join( f"{name}={field.data}" for name, field in self._fields.items() @@ -42,37 +34,9 @@ class Form_Base(FlaskForm, metaclass=Form_Base_Meta): @classmethod def get_default(cls): return cls() - """ - @abstractmethod - def test_69(self): - pass - - def get_Filters_Product_Category(data_request): - data_form = data_request[Model_View_Store_Product_Category.FLAG_FORM] - form_filters = Filters_Product_Category(**data_form) - form_filters.is_not_empty.data = av.input_bool(data_form['is_not_empty'], 'is_not_empty', 'filter_category') - form_filters.active.data = av.input_bool(data_form['active'], 'active', 'filter_category') - return form_filters - """ @classmethod def get_choices_blank(cls): return [('', 'Select')] @classmethod def get_choice_all(cls): return ('', 'All') - -''' -class Filters_Stored_Procedure_Base(Form_Base): - """ - @abstractmethod - def __repr__(self): - pass - @classmethod - @abstractmethod - def from_json(cls, json): - pass - """ - @abstractmethod - def to_json(self): - pass -''' \ No newline at end of file diff --git a/forms/forms.py b/forms/forms.py index 93419717..ccfaccb8 100644 --- a/forms/forms.py +++ b/forms/forms.py @@ -40,43 +40,24 @@ class Form_Register(FlaskForm): submit = SubmitField('Submit') -""" -class Form_Product(FlaskForm): # for basket, product tiles, product add - # PositiveIntegerField with validation constraints +class Form_Basket_Add(FlaskForm): quantity = IntegerField( 'Quantity', - validators=[ - # InputRequired(message='Quantity'), + validators = [ NumberRange(min=1, message='Please enter a positive integer') ], - default=1 - ) -""" - -class Form_Basket_Add(FlaskForm): # for basket, product tiles, product add - # PositiveIntegerField with validation constraints - quantity = IntegerField( - 'Quantity', - validators=[ - # InputRequired(message='Quantity'), - NumberRange(min=1, message='Please enter a positive integer') - ], - default=1 - # render_kw={'id-product': ''} # {Model_View_Store.attr_id_product: ''} + default = 1 ) submit = SubmitField('Add') form_type = 'Form_Basket_Add' -class Form_Basket_Edit(FlaskForm): # for basket, product tiles, product add - # PositiveIntegerField with validation constraints +class Form_Basket_Edit(FlaskForm): quantity = IntegerField( 'Quantity', - validators=[ - # InputRequired(message='Quantity'), + validators = [ NumberRange(min=1, message='Please enter a positive integer') ], - default=1 - # render_kw={'id-product': ''} # {Model_View_Store.attr_id_product: ''} + default = 1 ) submit = SubmitField('Update') form_type = 'Form_Basket_Edit' @@ -109,7 +90,6 @@ class Form_Currency(FlaskForm): id_currency = SelectField('Currency', id='id_currency') -# Store class Form_Supplier(FlaskForm): id_id_supplier = 'id_supplier' id_supplier = SelectField('Supplier', id='id_supplier') @@ -124,11 +104,8 @@ class Form_Supplier(FlaskForm): id_currency = SelectField('Currency ID') is_active = BooleanField('Active', default = True) -# class Form_Supplier_Purchase_Order(FlaskForm): - -# User class Form_Filters_User(FlaskForm): active = BooleanField('Active only?', default = True) id_user = SelectField('User ID', validators=[Optional()], choices=[]) \ No newline at end of file diff --git a/forms/store/product_category.py b/forms/store/product_category.py index 463540ab..5229e275 100644 --- a/forms/store/product_category.py +++ b/forms/store/product_category.py @@ -28,14 +28,7 @@ from abc import ABCMeta, abstractmethod class Filters_Product_Category(Form_Base): is_not_empty = BooleanField('Not empty only?') active = BooleanField("Active only?", default = True) - """ - @classmethod - def from_filters(cls, filters): - form = Filters_Product_Category() - form.is_not_empty.data = filters.is_not_empty - form.active.data = filters.active - return form - """ + def __repr__(self): return f'Filters_Product_Category(is_not_empty={self.is_not_empty.data}, active={self.active.data})' @classmethod diff --git a/forms/store/product_permutation.py b/forms/store/product_permutation.py index 2fcd9d91..14314b97 100644 --- a/forms/store/product_permutation.py +++ b/forms/store/product_permutation.py @@ -32,18 +32,7 @@ class Filters_Product_Permutation(Form_Base): active = BooleanField('Active only?', default=True) quantity_min = FloatField('Min stock') quantity_max = FloatField('Max stock') - # submit = SubmitField('Submit') - """ - @classmethod - def from_filters(cls, filters): - form = Filters_Product_Permutation() - form.id_category.choices = Store_Base.convert_list_objects_to_list_options(filters.categories) - form.id_product.choices = Store_Base.convert_list_objects_to_list_options(filters.products) - form.is_out_of_stock.data = filters.is_out_of_stock - form.quantity_min.data = filters.quantity_min - form.quantity_max.data = filters.quantity_max - return form - """ + def __repr__(self): return f''' Filters_Product_Permutation( diff --git a/forms/store/stock_item.py b/forms/store/stock_item.py index 59697782..ab35ddee 100644 --- a/forms/store/stock_item.py +++ b/forms/store/stock_item.py @@ -31,24 +31,11 @@ class Filters_Stock_Item(Form_Base): quantity_min = FloatField('Min stock') quantity_max = FloatField('Max stock') active = BooleanField("Active") - # submit = SubmitField('Submit') - """ - def __repr__(self): - return f''' - {self.__class__.__name__}( - id_category={self.id_category.data}, - id_product={self.id_product.data}, - is_out_of_stock={self.is_out_of_stock.data}, - quantity_min={self.quantity_min.data}, - quantity_max={self.quantity_max.data}) - ''' - """ + @classmethod def from_json(cls, json): form = cls() - # form.id_category.choices = [(json[Store_Base.ATTR_ID_PRODUCT_CATEGORY], json[Store_Base.ATTR_ID_PRODUCT_CATEGORY])] form.id_category.data = json[Store_Base.ATTR_ID_PRODUCT_CATEGORY] - # form.id_product.choices = [(json[Store_Base.ATTR_ID_PRODUCT], json[Store_Base.ATTR_ID_PRODUCT])] form.id_product.data = json[Store_Base.ATTR_ID_PRODUCT] form.is_out_of_stock.data = av.input_bool(json[Store_Base.FLAG_IS_OUT_OF_STOCK], Store_Base.FLAG_IS_OUT_OF_STOCK, f'{cls.__name__}.from_json') form.quantity_min.data = json[Store_Base.FLAG_QUANTITY_MIN] @@ -69,11 +56,4 @@ class Filters_Stock_Item(Form_Base): filters = cls() filters.id_category.choices = cls.get_choices_blank() filters.id_product.choices = cls.get_choices_blank() - """ - def import_values(self, form_filters): - self.id_category.data = form_filters.id_category.data - self.id_product.data = form_filters.id_product.data - self.is_out_of_stock.data = form_filters.is_out_of_stock.data - self.quantity_min.data = form_filters.quantity_min.data - self.quantity_max.data = form_filters.quantity_max.data - """ \ No newline at end of file + \ No newline at end of file diff --git a/helpers/DEPRECATED/__pycache__/helper_abc.cpython-312.pyc b/helpers/DEPRECATED/__pycache__/helper_abc.cpython-312.pyc deleted file mode 100644 index 51b38b363c1b96632ffcef9211e9a5c8dabfc9b6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2161 zcma)7O>7fK6rTNYY~v8yiOD~(DA3?QBbv7Sv=M^F4oPWKO40-zhwWx}1Dkri?#wzU zj#E&DL{jBIRaJ>pA^E_eReI=w1BV`RL@I}M;>yw1NJ#CaH;aJUa_Y>+>o^oDp0u-X zXTF*D-uK?y`DN$M9RSPEuJwthRRH`>K5U^&%0>rP7J&pLDh5<4LQzDgW3%B*8r=%cbuS7z+Uq#ZJ#sv$XE8VQE^TBzn2D6HyBR-MO`oGFj%`?rDuS=TvSsWk$x+sOSun@WfeBiwy5X8-(##+TItR=E&nsv< zDLmh6dc>H3&?ZS{HPQuL5ldfHo9}IeRbvn8@Uzj1UH+C{1oP}*@2+LTZBne8cdR;JSX4boTq6#GHD8r?7ZV8DC)M`PS}@}?zWGqw1+97CC6t6 z0f2{fPZ`1JBsNRRp0(;;dk%X>ELXkMKkBw)%QCj$e1Qd@ecJz6aJ{kx_X{kPW6;aC zCg`%YT{_8~C6fVL+j^%NI|4{8Jp51WJ6_E*7Nh|#InTf|sy)@*&Z$YHbBdaX$*LUJ zIlBb86wS4#YTLO9sHvP1*JU^%h%(m>NsFXfOB=^!PT(qP6eBT&bWUn_C8V-)H!&pn zsit=Bih%Hhs6i-;I;T%3WNSU>F&&C7tH^X>grMuttQ*kue%n!53PY$t)5Y^~L6v#l zbY7NmdtlrS1u1cCFfPiLv!;6ppK8G^&rBy?PGD;UIgyNux}wE{Ri+Ci$E{Ex@y+yX z_7_iFGHdKLeoBZXWz%Q##BZXI$%rw8NN;dhu3vwq^}G}P1j!IZW$CH_qaVfP5LBk* zXh;*2glZH$2er#kP*o*E!$w2zVs<)s6HeualNuin! ztAJn&S#87vBG=KUxMohlW`f$eEq9Q^a|D5=@bC}LRzs7)1_})=OFiFAFJAll+FHZG z$H3|6n)l#qMJ{-1Z@q0aANlFPub$%tZ`0C&J1sfy>+8XzhL6hxR(-D({4Go2JKZ_| zk@es)d-7564O=aG@nKJE>l}^64p6-d8-uObG>($Ng$BtTVY;oZ(X<$XEdGY%w`F", suppress_errors)) return False - # method valid = True if str(type(method)) != v_type: valid = False @@ -246,10 +211,8 @@ def val_str(v_input, v_name, method, min_len = -1, max_len = -1, suppress_errors return False raise ValueError(error_msg) # v_input -# VARIABLE INSTANTIATION v_type = "" valid = True -# METHODS if str(type(v_input)) != v_type: valid = False else: @@ -267,31 +230,11 @@ def val_str(v_input, v_name, method, min_len = -1, max_len = -1, suppress_errors print(error_msg) return False raise ValueError(error_msg) -# RETURNS return True -# def val_none(v_input, v_name, method, v_arg_type = 'argument', suppress_errors = False, suppress_console_outputs = False): -# # FUNCTION -# # evaluate if v_input is None -# # ARGUMENTS -# # ARGUMENT VALIDATION -# # VARIABLE INSTANTIATION -# # METHODS -# # RETURNS def val_int(v_input, v_name, method, v_min: Optional[int] = None, v_max: Optional[int] = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that myinput is of type int, and if not None, limited by v_min and v_max -# ARGUMENTS - # int (hopefully) myinput - # str v_name - # str method - # optional int v_min - # optional int v_max - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_int' # suppress_errors val_bool(suppress_errors, 'suppress_errors', my_f) @@ -315,10 +258,8 @@ def val_int(v_input, v_name, method, v_min: Optional[int] = None, v_max: Optiona if not val_int(v_max, 'v_max', my_f, None, None, suppress_errors, suppress_console_outputs): return False # v_input -# VARIABLE INSTANTIATION - mytype = "" # str(type(myinput)) + mytype = "" error_msg = error_msg_str(v_input, v_name, method, mytype, suppress_errors, suppress_console_outputs, v_arg_type) -# METHODS if not mytype == str(type(v_input)): if suppress_errors: if not suppress_console_outputs: @@ -346,23 +287,11 @@ def val_int(v_input, v_name, method, v_min: Optional[int] = None, v_max: Optiona print(error_msg + f"\nValue greater than maximum {v_max}.") return False raise ValueError(error_msg + f"\nValue greater than maximum {v_max}.") -# RETURNS return True def val_float(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type float, and if not None, limited by v_min and v_max -# ARGUMENTS - # float (hopefully) v_input - # str v_name - # str method - # optional float v_min - # optional float v_max - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_float' # suppress_errors val_bool(suppress_errors, 'suppress_errors', my_f) @@ -386,10 +315,8 @@ def val_float(v_input, v_name, method, v_min = None, v_max = None, suppress_erro if not val_float(v_max, 'v_max', my_f, None, None, suppress_errors, suppress_console_outputs): return False # v_input -# VARIABLE INSTANTIATION - mytype = "" # str(type(myinput)) + mytype = "" error_msg = error_msg_str(v_input, v_name, method, mytype, suppress_errors, suppress_console_outputs, v_arg_type) -# METHODS if not mytype == str(type(v_input)): if suppress_errors: if not suppress_console_outputs: @@ -417,20 +344,10 @@ def val_float(v_input, v_name, method, v_min = None, v_max = None, suppress_erro print(error_msg + f"\nValue greater than maximum {v_max}.") return False raise ValueError(error_msg + f"\nValue greater than maximum {v_max}.") -# RETURNS return True def input_bool(v_input, v_name, method, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # input valid str, int, or bool representation of bool, or else None -# ARGUMENTS - # bool (hopefully) v_input - # str v_name - # str method - # optional str v_arg_type - # optional bool suppress_errors - # optional bool suppress_console_outputs -# ARGUMENT VALIDATION my_f = 'input_bool' if v_input is None: return False # suppress_errors @@ -446,7 +363,6 @@ def input_bool(v_input, v_name, method, suppress_errors = False, suppress_consol my_f = method + '.' + my_f # v_arg_type if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return None -# METHODS if not val_bool(v_input, v_name, my_f, True, True): if not val_int(v_input, v_name, my_f, 0, 1, True, True): # if str(type(v_input)) == "": @@ -476,20 +392,10 @@ def input_bool(v_input, v_name, method, suppress_errors = False, suppress_consol raise ValueError(error_msg) else: return False if v_input == 0 else True -# RETURNS return bool(v_input) def full_val_bool(v_input, v_name, method, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that bool input is bool or valid equivalent -# ARGUMENTS - # bool (hopefully) my_input - # str v_name - # str method - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'full_val_bool' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -502,23 +408,11 @@ def full_val_bool(v_input, v_name, method, suppress_errors = False, suppress_con if not val_str(v_name, 'v_name', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False # v_arg_type if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False -# RETURNS return not (str(type(input_bool(v_input, v_name, method, suppress_errors, suppress_console_outputs, v_arg_type))) == "") def input_int(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # input int or valid equivalent, or else None -# ARGUMENTS - # int or str v_input - # str v_name - # str method - # v_min - # v_min - # bool suppress_errors - # bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'input_int' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -539,7 +433,6 @@ def input_int(v_input, v_name, method, v_min = None, v_max = None, suppress_erro if not str(type(v_max)) == "": v_max = input_int(v_max, 'v_min', my_f, v_min, None, suppress_errors, suppress_console_outputs) if str(type(v_max)) == "": return None -# METHODS error_msg = error_msg_str(v_input, v_name, method, "", suppress_errors, suppress_console_outputs, v_arg_type) # v_input try: @@ -562,23 +455,11 @@ def input_int(v_input, v_name, method, v_min = None, v_max = None, suppress_erro if not suppress_console_outputs: Helper_App.console_log(f"{error_msg}\nInt input greater than maximum value. Value = {v_input}, maximum = {v_max}.") return None -# RETURNS return my_int def full_val_int(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is int or equivalent, else False, limited by v_min and v_max -# ARGUMENTS - # int (hopefully) v_input - # str v_name - # str method - # optional float v_min - # optional float v_max - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'full_val_int' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -596,23 +477,11 @@ def full_val_int(v_input, v_name, method, v_min = None, v_max = None, suppress_e if not str(type(v_max)) == "": v_max = input_int(v_max, 'v_min', method, v_min, None, suppress_errors, suppress_console_outputs) if str(type(v_max)) == "": return False -# RETURNS return not (str(type(input_int(v_input, v_name, method, v_min, v_max, suppress_errors, suppress_console_outputs))) == "") def input_float(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # input float, else return None -# ARGUMENTS - # float/int/str(numeric) v_input - # str v_name - # str method - # optional float v_min - # optional float v_min - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'input_float' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -633,7 +502,6 @@ def input_float(v_input, v_name, method, v_min = None, v_max = None, suppress_er if not str(type(v_max)) == "": v_max = input_float(v_max, 'v_min', my_f, v_min, None, suppress_errors, suppress_console_outputs) if str(type(v_max)) == "": return None -# METHODS error_msg = error_msg_str(v_input, v_name, method, "", suppress_errors, suppress_console_outputs, v_arg_type) # v_input try: @@ -656,22 +524,11 @@ def input_float(v_input, v_name, method, v_min = None, v_max = None, suppress_er if not suppress_console_outputs: Helper_App.console_log(f"{error_msg}\nInt input greater than maximum value. Value = {v_input}, maximum = {v_max}.") return None -# RETURNS return my_float def full_val_float(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is numeric, and if not False, limited by v_min and v_max -# ARGUMENTS - # float (hopefully) v_input - # str v_name - # str method - # optional float v_min - # optional float v_max - # optional bool suppress_errors - # optional bool suppress_console_outputs -# ARGUMENT VALIDATION my_f = 'full_val_float' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -692,40 +549,22 @@ def full_val_float(v_input, v_name, method, v_min = None, v_max = None, suppress if not str(type(v_max)) == "": v_max = input_float(v_max, 'v_min', method, v_min, None, suppress_errors, suppress_console_outputs) if str(type(v_max)) == "": return False -# RETURNS return not (str(type(input_float(v_input, v_name, method, v_min, v_max, suppress_errors, suppress_console_outputs))) == "") def make_ordinal(n): -# FUNCTION # Get ordinal representation of number -# ARGUMENTS - # int n -# ARGUMENT VALIDATION full_val_int(n, 'n', 'make_ordinal', 0) -# VARIABLE INSTANTIATION n = int(n) -# METHODS if 11 <= (n % 100): suffix= 'th' else: suffix = ['th', 'st', 'nd', 'rd', 'th'][min(n % 10, 4)] -# RETURNS return str(n) + suffix def val_type(v_input, v_name, method, v_type, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type v_type -# ARGUMENTS - # v_type (hopefully) v_input - # str v_name - # str method - # str v_type - # optional bool suppress_errors - # optional bool suppress_console_outputs - # str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_type' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -751,22 +590,11 @@ def val_type(v_input, v_name, method, v_type, suppress_errors = False, suppress_ Helper_App.console_log(error_message) return False raise ValueError(error_message) -# RETURNS return True def val_instance(v_input, v_name, method, v_type, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type v_type -# ARGUMENTS - # v_type (hopefully) v_input - # str v_name - # str method - # type v_type - # optional bool suppress_errors - # optional bool suppress_console_outputs - # str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_type' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -781,41 +609,25 @@ def val_instance(v_input, v_name, method, v_type, suppress_errors = False, suppr if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False # v_type error_message = error_msg_str(v_type, 'v_type', my_f, v_arg_type) - if not isinstance(v_type, type): # mytype == v_type: # f"": - if suppress_errors: - if not suppress_console_outputs: - Helper_App.console_log(error_message) - return False - raise ValueError(error_message) # val_str(v_type, 'v_type', my_f, 6, -1, suppress_errors, suppress_console_outputs): return False - # v_input - error_message = error_msg_str(v_input, v_name, method, v_arg_type) - # mytype = str(type(v_input)) - # if not (v_type == 'int' or v_type == 'bool' or v_type == 'float' or v_type == 'complex' or v_type == 'str' or v_type == 'NoneType'): - if not isinstance(v_input, v_type): # mytype == v_type: # f"": + if not isinstance(v_type, type): + if suppress_errors: + if not suppress_console_outputs: + Helper_App.console_log(error_message) + return False + raise ValueError(error_message) + # v_input + error_message = error_msg_str(v_input, v_name, method, v_arg_type) + if not isinstance(v_input, v_type): if suppress_errors: if not suppress_console_outputs: Helper_App.console_log(error_message) return False raise ValueError(error_message) -# RETURNS return True def val_list(v_input, v_name, method, v_type = '', min_len = -1, max_len = -1, suppress_errors = False, suppress_console_outputs: bool = False, allow_nuns = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type list, and if defined: has v_len elements of type v_type -# ARGUMENTS - # list[v_type] (hopefully) v_input - # str v_name - variable name - # str method - parent method - # str v_type - type of list items - # int min_len - minimum length of list - # int max_len - maximum length of list - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional bool allow_nuns - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_list' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -829,21 +641,14 @@ def val_list(v_input, v_name, method, v_type = '', min_len = -1, max_len = -1, s # v_arg_type if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False # v_type - # if not val_type(v_type, 'v_type', my_f, type, suppress_errors, suppress_console_outputs): return False if not val_str(v_type, 'v_type', my_f, -1, -1, suppress_errors, suppress_console_outputs): return False # min_len if not full_val_int(min_len, 'min_len', my_f, None, None if max_len == -1 else max_len, suppress_errors, suppress_console_outputs): return False - # min_len = input_int(min_len, 'min_len', method, None, max_len, suppress_errors, suppress_console_outputs) - # if str(type(min_len)) == "": return False # max_len if not full_val_int(max_len, 'max_len', my_f, None if max_len == -1 else (None if min_len == -1 else min_len), None, suppress_errors, suppress_console_outputs): return False - # if not str(type(max_len)) == "": - # max_len = input_int(max_len, 'max_len', method, min_len, None, suppress_errors, suppress_console_outputs) - # if str(type(max_len)) == "": return False # allow_nuns if not val_bool(allow_nuns, 'allow_nuns', my_f, suppress_errors, suppress_console_outputs): return False # v_input - # mytype = str(type(v_input)) error_msg = error_msg_str(v_input, v_name, method, "") if not str(type(v_input)) == "": if suppress_errors: @@ -866,7 +671,6 @@ def val_list(v_input, v_name, method, v_type = '', min_len = -1, max_len = -1, s raise ValueError(error_msg + f'\nInvalid list length. Minimum = {min_len}, length = {L}') if v_type != '' and L > 0: for i in range(L): - # mytype = str(type(v_input[i])) if not (val_type(v_input[i], f'{v_name}[{i}]', my_f, v_type, True) or allow_nuns and val_type(v_input[i], f'{v_name}[{i}]', my_f, "", True)): # mytype == v_type: error_msg = error_msg + '\n' + error_msg_str(v_input[i], f'{v_name}[{i}]', my_f, v_type, False, False, 'list element') if suppress_errors: @@ -874,25 +678,11 @@ def val_list(v_input, v_name, method, v_type = '', min_len = -1, max_len = -1, s Helper_App.console_log(error_msg) return False raise ValueError(error_msg) -# RETURNS return True def val_list_instances(v_input, v_name, method, v_type = None, min_len = -1, max_len = -1, suppress_errors = False, suppress_console_outputs: bool = False, allow_nuns = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type list, and if defined: has v_len elements of type v_type -# ARGUMENTS - # list[v_type] (hopefully) v_input - # str v_name - variable name - # str method - parent method - # type v_type - type of list items - # int min_len - minimum length of list - # int max_len - maximum length of list - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional bool allow_nuns - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_list_instances' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -907,20 +697,12 @@ def val_list_instances(v_input, v_name, method, v_type = None, min_len = -1, max if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False # v_type if not val_type(v_type, 'v_type', my_f, type, suppress_errors, suppress_console_outputs): return False - # if not val_str(v_type, 'v_type', my_f, 6, -1, suppress_errors, suppress_console_outputs): return False # min_len if not full_val_int(min_len, 'min_len', my_f, None, None if max_len == -1 else max_len, suppress_errors, suppress_console_outputs): return False - # min_len = input_int(min_len, 'min_len', method, None, max_len, suppress_errors, suppress_console_outputs) - # if str(type(min_len)) == "": return False # max_len if not full_val_int(max_len, 'max_len', my_f, None if max_len == -1 else (None if min_len == -1 else min_len), None, suppress_errors, suppress_console_outputs): return False - # if not str(type(max_len)) == "": - # max_len = input_int(max_len, 'max_len', method, min_len, None, suppress_errors, suppress_console_outputs) - # if str(type(max_len)) == "": return False - # allow_nuns if not val_bool(allow_nuns, 'allow_nuns', my_f, suppress_errors, suppress_console_outputs): return False # v_input - # mytype = str(type(v_input)) error_msg = error_msg_str(v_input, v_name, method, "") if not str(type(v_input)) == "": if suppress_errors: @@ -943,38 +725,19 @@ def val_list_instances(v_input, v_name, method, v_type = None, min_len = -1, max raise ValueError(error_msg + f'\nInvalid list length. Minimum = {min_len}, length = {L}') if v_type != '' and L > 0: for i in range(L): - # mytype = str(type(v_input[i])) - if not (val_instance(v_input[i], f'{v_name}[{i}]', my_f, v_type, True) or allow_nuns and val_instance(v_input[i], f'{v_name}[{i}]', my_f, type(None), True)): # mytype == v_type: + if not (val_instance(v_input[i], f'{v_name}[{i}]', my_f, v_type, True) or allow_nuns and val_instance(v_input[i], f'{v_name}[{i}]', my_f, type(None), True)): error_msg = error_msg + '\n' + error_msg_str(v_input[i], f'{v_name}[{i}]', my_f, v_type, False, False, 'list element') if suppress_errors: if not suppress_console_outputs: Helper_App.console_log(error_msg) return False raise ValueError(error_msg) -# RETURNS return True def val_nested_list(v_input, v_name, method, depth_i, depth_max, v_type = '', v_min = -1, v_mins = [], v_max = -1, v_maxs = [], suppress_errors = False, suppress_console_outputs = False, allow_nuns = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type list, and if defined: has v_len elements of type v_type # for nested list of nested-index i -# ARGUMENTS - # list[v_type] (hopefully) v_input - # str v_name - # str method - # int depth_i - current depth of nesting of lists - # int depth_max - maximum depth of nesting of lists - base 0 - # Optional[str] v_type - type of list items - # Optional[int] v_min - minimum sublist size - # Optional[list[int]] v_mins - minimum list sizes - # Optional[int] v_max - maximum sublist size - # Optional[list[int]] v_maxs - maximum list sizes - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional bool allow_nuns - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_nested_list' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -1017,7 +780,6 @@ def val_nested_list(v_input, v_name, method, depth_i, depth_max, v_type = '', v_ if not suppress_console_outputs: Helper_App.console_log(error_msg) return False -# METHODS L = len(v_input) if L == 0: if v_min > -1: @@ -1048,30 +810,12 @@ def val_nested_list(v_input, v_name, method, depth_i, depth_max, v_type = '', v_ if not suppress_console_outputs: Helper_App.console_log(error_msg) return False -# RETURNS return True def val_nested_list_instances(v_input, v_name, method, depth_i, depth_max, v_type = '', v_min = -1, v_mins = [], v_max = -1, v_maxs = [], suppress_errors = False, suppress_console_outputs = False, allow_nuns = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type list, and if defined: has v_len elements of type v_type # for nested list of nested-index i -# ARGUMENTS - # list[v_type] (hopefully) v_input - # str v_name - # str method - # int depth_i - current depth of nesting of lists - # int depth_max - maximum depth of nesting of lists - base 0 - # Optional[str] v_type - type of list items - # Optional[int] v_min - minimum sublist size - # Optional[list[int]] v_mins - minimum list sizes - # Optional[int] v_max - maximum sublist size - # Optional[list[int]] v_maxs - maximum list sizes - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional bool allow_nuns - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_nested_list' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -1085,7 +829,6 @@ def val_nested_list_instances(v_input, v_name, method, depth_i, depth_max, v_typ # v_arg_type if not val_str(v_arg_type, 'v_arg_type', my_f, 1, -1, suppress_errors, suppress_console_outputs): return False # v_type - # if not val_str(v_type, 'v_type', my_f, -1, -1, suppress_errors, suppress_console_outputs): return False if not val_type(v_type, 'v_type', my_f, type, suppress_errors, suppress_console_outputs): return False # v_min if not val_int(v_min, 'v_min', my_f, -1, None, suppress_errors, suppress_console_outputs): return False @@ -1108,13 +851,12 @@ def val_nested_list_instances(v_input, v_name, method, depth_i, depth_max, v_typ # allow_nuns if not val_bool(allow_nuns, 'allow_nuns', my_f, suppress_errors, suppress_console_outputs): return False # v_input - mytype = v_type if depth_i == depth_max else list # "" + mytype = v_type if depth_i == depth_max else list error_msg = error_msg_str(v_input, v_name, method, mytype, suppress_errors, suppress_console_outputs, v_arg_type) if not val_list_instances(v_input, v_name, method, mytype, v_min, v_max, suppress_errors, suppress_console_outputs, allow_nuns, v_arg_type): if not suppress_console_outputs: Helper_App.console_log(error_msg) return False -# METHODS L = len(v_input) if L == 0: if v_min > -1: @@ -1145,23 +887,11 @@ def val_nested_list_instances(v_input, v_name, method, depth_i, depth_max, v_typ if not suppress_console_outputs: Helper_App.console_log(error_msg) return False -# RETURNS return True def val_url(v_input, v_name, method, min_len = 12, max_len = -1, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# FUNCTION # validate that v_input is of type str -# ARGUMENTS - # str (hopefully) v_input - # str v_name - # str method - # optional int min_len - # optional int max_len - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION _m = 'val_url' v_type = "" val_bool(suppress_errors, 'suppress_errors', _m) @@ -1181,7 +911,6 @@ def val_url(v_input, v_name, method, min_len = 12, max_len = -1, suppress_errors if not val_int(max_len, 'max_len', _m, -1, None, suppress_errors, suppress_console_outputs): return False # v_input if not val_str(v_input, v_name, method, min_len, -1, suppress_errors, suppress_console_outputs): return False -# METHODS error_msg = error_msg_str(v_input, 'v_input', _m, "") if not (v_input[:8] == r'https://' or v_input[:7] == r'http://'): if suppress_errors: @@ -1189,40 +918,11 @@ def val_url(v_input, v_name, method, min_len = 12, max_len = -1, suppress_errors Helper_App.console_log(error_msg) return False raise ValueError(error_msg) -# RETURNS return True -# def val_nparray(v_input, v_name, method, v_min = None, v_max = None, suppress_errors = False, suppress_console_outputs = False, v_arg_type = 'argument'): -# # FUNCTION -# # validate v_input is numpy ndarray -# # ARGUMENTS -# # numpy.ndarray v_input -# # str v_name -# # str method -# # -# # ARGUMENT VALIDATION -# # METHODS -# # RETURNS - - def val_DataFrame(v_input, v_name, method, v_types=[], min_col=-1, max_col=-1, cols=[], min_sz=-1, max_sz=-1, suppress_errors=False, suppress_console_outputs=False, v_arg_type='argument'): -# FUNCTION # validate that v_input is of type list, and if defined: has v_len elements of type v_type # for nested list of nested-index i -# ARGUMENTS - # pandas.DataFrame (hopefully) v_input - # str v_name - # str method - # optional list[str] v_type - column datatypes - # optional int min_col - minimum number of columns - # optional int max_col - maximum number of columns - # optional list[str] cols - column names - # optional int min_sz - minimum length of columns - # optional int max_sz - maximum length of columns - # optional bool suppress_errors - # optional bool suppress_console_outputs - # optional str v_arg_type -# ARGUMENT VALIDATION my_f = 'val_DataFrame' val_bool(suppress_errors, 'suppress_errors', my_f) if not val_bool(suppress_console_outputs, 'suppress_console_outputs', my_f, suppress_errors): @@ -1259,7 +959,6 @@ def val_DataFrame(v_input, v_name, method, v_types=[], min_col=-1, max_col=-1, c raise ValueError(error_msg) # v_input if not val_type(v_input, v_name, my_f, "", suppress_errors, suppress_console_outputs, v_arg_type): return False -# METHODS error_msg = error_msg_str(v_input, v_name, my_f, "", v_arg_type=v_arg_type) n_c = len(v_input.columns) if (min_col > 0 and n_c < min_col) or (max_col > 0 and n_c > max_col): @@ -1291,7 +990,6 @@ def val_DataFrame(v_input, v_name, method, v_types=[], min_col=-1, max_col=-1, c Helper_App.console_log(error_msg + f'\nInvalid column heading for column {v_input.columns[col_i]}') return False raise ValueError(error_msg + f'\nInvalid column heading for column {v_input.columns[col_i]}') -# RETURNS return True diff --git a/lib/data_types.py b/lib/data_types.py index 53ddd590..edb2cf41 100644 --- a/lib/data_types.py +++ b/lib/data_types.py @@ -1,35 +1,5 @@ -# -*- coding: utf-8 -*- -""" -Created on Thu Apr 27 12:33:59 2023 -@author: Edward Middleton-Smith -Argument Validation -""" - -# CLASSES -# ATTRIBUTE DECLARATION -# METHODS - # FUNCTION - # ARGUMENTS - # ARGUMENT VALIDATION - # ATTRIBUTE + VARIABLE INSTANTIATION - # METHODS - # RETURNS - -# NORMAL METHODS -# FUNCTION -# ARGUMENTS -# ARGUMENT VALIDATION -# VARIABLE INSTANTIATION -# METHODS -# RETURNS - -# IMPORTS - -# CLASSES - -# METHODS def get_enum_member_by_text(enum_class, text): for member in enum_class.__members__.values(): if member.name == text: diff --git a/pay_stripe.py b/pay_stripe.py deleted file mode 100644 index 821e3bde..00000000 --- a/pay_stripe.py +++ /dev/null @@ -1,182 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: App General -Feature: App - -Description: -Initializes the Flask application, sets the configuration based on the environment, and defines two routes (/ and /about) that render templates with the specified titles. -""" - -# IMPORTS -# VARIABLE INSTANTIATION -# METHODS - -# IMPORTS -import os -import stripe -import json -from flask import Flask, render_template, render_template_string, jsonify, request, send_from_directory, redirect -from dotenv import load_dotenv, find_dotenv - -from config import app_config - -# VARIABLE INSTANTIATION -key_secret = os.environ.get("KEY_SECRET_STRIPE") -key_public = os.environ.get("KEY_PUBLIC_STRIPE") # 'pk_test_51OGrxlL7BuLKjoMpfpfw7bSmZZK1MhqMoQ5VhW2jUj7YtoEejO4vqnxKPiqTHHuh9U4qqkywbPCSI9TpFKtr4SYH007KHMWs68' - -# METHODS -def create_product_price(): - print(f'stripe.api_key = {stripe.api_key}') - starter_subscription = stripe.Product.create( - name="Starter Subscription", - description="$12/Month subscription", - ) - - starter_subscription_price = stripe.Price.create( - unit_amount=1200, - currency="usd", - recurring={"interval": "month"}, - product=starter_subscription['id'], - ) - - # Save these identifiers - print(f"Success! Here is your starter subscription product id: {starter_subscription.id}") - print(f"Success! Here is your starter subscription price id: {starter_subscription_price.id}") - - return starter_subscription_price.id - -def get_file_str(f_address): - f = open(f_address) - return f.read() - -# Ensure environment variables are set. -price = os.getenv('PRICE') -if price is None or price == 'price_12345' or price == '': - print('You must set a Price ID in .env. Please see the README.') - exit(0) - -# For sample support and debugging, not required for production: -stripe.set_app_info( - 'stripe-samples/checkout-one-time-payments', - version='0.0.1', - url='https://github.com/stripe-samples/checkout-one-time-payments') - -# stripe.api_version = '2020-08-27' -stripe.api_key = key_secret # os.getenv('KEY_SECRET_STRIPE') - -# app_dir = str(os.path.abspath(os.path.join( -# __file__, "..", ".."))) -# static_dir = str(os.path.abspath(os.path.join( -# app_dir, os.getenv("STATIC_DIR")))) -# template_dir = str(os.path.abspath(os.path.join( -# app_dir, os.getenv("TEMPLATE_DIR")))) -app = Flask(__name__) # , static_folder=static_dir, - # static_url_path="", template_folder=template_dir) -app.config.from_object(app_config) - -@app.route('/', methods=['GET']) -def home(): - # return render_template(f'{app_dir}\\templates\\_home.html') # f'{app_dir}\\templates\\layout.html') - # return render_template_string(get_file_str(f'{app_dir}\\templates\\_home.html')) # f'{app_dir}\\templates\\layout.html') - return render_template('_home.html', title='Home') - -@app.route('/store', methods=['GET']) -def store_home(): - return render_template('_store_home.html', title='Store Home') - -@app.route('/contact') -def contact(): - return render_template('_contact.html', title='Contact Us') - - -@app.route('/config', methods=['GET']) -def get_publishable_key(): - price = stripe.Price.retrieve(os.getenv('PRICE')) - return jsonify({ - 'publicKey': key_public, # os.getenv('KEY_PUBLIC_STRIPE'), - 'unitAmount': price['unit_amount'], - 'currency': price['currency'] - }) - -# Fetch the Checkout Session to display the JSON result on the success page -@app.route('/checkout-session', methods=['GET']) -def get_checkout_session(): - id = request.args.get('sessionId') - print(f'checkout session id: {id}') - checkout_session = stripe.checkout.Session.retrieve(id) - return jsonify(checkout_session) - - -@app.route('/create-checkout-session', methods=['POST']) -def create_checkout_session(): - quantity = request.form.get('quantity', 1) - domain_url = os.getenv('DOMAIN') - - try: - # Create new Checkout Session for the order - # Other optional params include: - # [billing_address_collection] - to display billing address details on the page - # [customer] - if you have an existing Stripe Customer ID - # [payment_intent_data] - lets capture the payment later - # [customer_email] - lets you prefill the email input in the form - # [automatic_tax] - to automatically calculate sales tax, VAT and GST in the checkout page - # For full details see https://stripe.com/docs/api/checkout/sessions/create - - # ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param - checkout_session = stripe.checkout.Session.create( - success_url=domain_url + '/success.html?session_id={CHECKOUT_SESSION_ID}', - cancel_url=domain_url + '/canceled.html', - mode='subscription', # 'payment', - # automatic_tax={'enabled': True}, - line_items=[{ - 'price': os.getenv('PRICE'), - 'quantity': quantity, - }] - ) - return redirect(checkout_session.url, code=303) - except Exception as e: - return jsonify(error=str(e)), 403 - - -@app.route('/webhook', methods=['POST']) -def webhook_received(): - # You can use webhooks to receive information about asynchronous payment events. - # For more about our webhook events check out https://stripe.com/docs/webhooks. - webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET') - request_data = json.loads(request.data) - - if webhook_secret: - # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured. - signature = request.headers.get('stripe-signature') - try: - event = stripe.Webhook.construct_event( - payload=request.data, sig_header=signature, secret=webhook_secret) - data = event['data'] - except Exception as e: - return e - # Get the type of webhook event sent - used to check the status of PaymentIntents. - event_type = event['type'] - else: - data = request_data['data'] - event_type = request_data['type'] - data_object = data['object'] - - print('event ' + event_type) - - if event_type == 'checkout.session.completed': - print('🔔 Payment succeeded!') - - return jsonify({Model_View_Base.FLAG_STATUS: Model_View_Base.FLAG_SUCCESS}) - - -if __name__ == '__main__': - # stripe.api_key = key_secret - - # create_product_price() - - # Setup Stripe python client library. - load_dotenv(find_dotenv()) - app.run(port=4242, debug=True) \ No newline at end of file diff --git a/robots.txt b/robots.txt index 27300052..76cd6f70 100644 --- a/robots.txt +++ b/robots.txt @@ -1,3 +1,5 @@ User-agent: * Disallow: /qa -Disallow: /dev \ No newline at end of file +Disallow: /dev +Disallow: /contact +Disallow: /*.partsltd.co.uk/ \ No newline at end of file diff --git a/routes.py b/routes.py deleted file mode 100644 index d93a97ad..00000000 --- a/routes.py +++ /dev/null @@ -1,54 +0,0 @@ -""" -Project: PARTS Website -Author: Edward Middleton-Smith - Precision And Research Technology Systems Limited - -Technology: Backend -Feature: Controller - Webpage routing - -Description: -Defines the routes and view functions for each page. -Manages the interaction between the frontend and backend. -""" - -from flask import render_template, url_for, Blueprint -from app import app -from app.forms import Form_Contact -# from forms import MyForm -# from app import MyForm -from model_view_contact import Model_View_Contact - -""" -@app.route('/', methods=['GET']) -def home(): - return render_template('_home.html', title='Home') - -@app.route('/store', methods=['GET']) -def store_home(): - return render_template('_store_home.html', title='Store Home') - -@app.route('/contact', methods=['GET', 'POST']) -def contact(): - form = Form_Contact() - if form.validate_on_submit(): - # Handle form submission - email = form.sender_email.data - CC = form.sender_CC.data - name = form.sender_name.data - msg = form.sender_message.data - # return render_template('contact.html', form=form) - # return render_template('_contact.html', title='Contact Us') - return render_template('contact.html', model=Model_View_Contact(form)) - -@app.route('/about') -def about(): - return render_template('about.html') - -@app.route('/contact', methods=['GET', 'POST']) -def contact(): - form = MyForm() - if form.validate_on_submit(): - # Handle form submission - pass - return render_template('contact.html', form=form) -""" \ No newline at end of file diff --git a/run.py b/run.py index bac5ef04..1d406903 100644 --- a/run.py +++ b/run.py @@ -11,7 +11,11 @@ Runs project. """ from app import app +import os +from dotenv import load_dotenv, find_dotenv + +load_dotenv(find_dotenv()) if __name__ == '__main__': - app.run(debug=True) + app.run(debug=(os.getenv('FLASK_ENV') == 'development')) # app.run(debug=True, host="0.0.0.0", port=5000) \ No newline at end of file diff --git a/static/PostgreSQL/000_combine.sql b/static/PostgreSQL/000_combine.sql deleted file mode 100644 index 5164cc8e..00000000 --- a/static/PostgreSQL/000_combine.sql +++ /dev/null @@ -1,15113 +0,0 @@ - -/* Clear Store DataBase */ - - - --- Drop dependencies -DROP TABLE IF EXISTS Shop_Calc_User_Temp; -DROP TABLE IF EXISTS tmp_Msg_Error; -DROP TABLE IF EXISTS tmp_Currency; -DROP TABLE IF EXISTS tmp_Delivery_Region; -DROP TABLE IF EXISTS tmp_Region; -DROP TABLE IF EXISTS tmp_Shop_User; -DROP TABLE IF EXISTS tmp_Shop_Order; -DROP TABLE IF EXISTS tmp_Shop_Product; -DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; -DROP TABLE IF EXISTS tmp_Shop_Image; -DROP TABLE IF EXISTS tmp_Shop_Variation; -DROP TABLE IF EXISTS tmp_Shop_Discount; -DROP TABLE IF EXISTS tmp_Discount; -DROP TABLE IF EXISTS tmp_Shop_Product_Category; -DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Region_Link; -DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; -DROP TABLE IF EXISTS tmp_User_Role_Link; -DROP TABLE IF EXISTS tmp_Shop_Basket; -DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; -DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; -DROP TABLE IF EXISTS tmp_Shop_Supplier; -DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; -DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; -DROP TABLE IF EXISTS tmp_Shop_Customer; - - - --- Delete old tables -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Audit; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order; - -DROP TABLE IF EXISTS Shop_Customer_Audit; -DROP TABLE IF EXISTS Shop_Customer; - -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Audit; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order; - -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Audit; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order; - -DROP TABLE IF EXISTS Shop_Unit_Measurement_Conversion_Audit; -DROP TABLE IF EXISTS Shop_Unit_Measurement_Conversion; - -DROP TABLE IF EXISTS Shop_Unit_Measurement_Audit; -DROP TABLE IF EXISTS Shop_Unit_Measurement; - -DROP TABLE IF EXISTS Shop_Supplier_Audit; -DROP TABLE IF EXISTS Shop_Supplier; - -DROP TABLE IF EXISTS Shop_User_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_User_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_User_Order_Audit; -DROP TABLE IF EXISTS Shop_User_Order; - -DROP TABLE IF EXISTS Shop_User_Order_Status_Audit; -DROP TABLE IF EXISTS Shop_User_Order_Status; - -DROP TABLE IF EXISTS Shop_User_Basket_Audit; -DROP TABLE IF EXISTS Shop_User_Basket; - -DROP TABLE IF EXISTS Shop_Address_Audit; -DROP TABLE IF EXISTS Shop_Address; - -DROP TABLE IF EXISTS Shop_User_Role_Link_Audit; -DROP TABLE IF EXISTS Shop_User_Role_Link; - -DROP TABLE IF EXISTS Shop_User_Audit; -DROP TABLE IF EXISTS Shop_User; - -DROP TABLE IF EXISTS Shop_Role_Permission_Link_Audit; -DROP TABLE IF EXISTS Shop_Role_Permission_Link; - -DROP TABLE IF EXISTS Shop_Role_Audit; -DROP TABLE IF EXISTS Shop_Role; - -DROP TABLE IF EXISTS Shop_Permission_Audit; -DROP TABLE IF EXISTS Shop_Permission; - -DROP TABLE IF EXISTS Shop_Permission_Group_Audit; -DROP TABLE IF EXISTS Shop_Permission_Group; - - -DROP TABLE IF EXISTS Shop_Discount_Region_Currency_Link_Audit; -DROP TABLE IF EXISTS Shop_Discount_Region_Currency_Link; - -DROP TABLE IF EXISTS Shop_Discount_Audit; -DROP TABLE IF EXISTS Shop_Discount; - -DROP TABLE IF EXISTS Shop_Product_Permutation_Delivery_Option_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation_Delivery_Option_Link; - -DROP TABLE IF EXISTS Shop_Delivery_Option_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Option; - -DROP TABLE IF EXISTS Shop_Image_Audit; -DROP TABLE IF EXISTS Shop_Image; - -DROP TABLE IF EXISTS Shop_Image_Type_Audit; -DROP TABLE IF EXISTS Shop_Image_Type; - -DROP TABLE IF EXISTS Shop_Product_Currency_Region_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Currency_Region_Link; -DROP TABLE IF EXISTS Shop_Product_Currency_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Currency_Link; - -DROP TABLE IF EXISTS Shop_Product_Variation_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Variation_Link; -DROP TABLE IF EXISTS Shop_Product_Permutation_Variation_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation_Variation_Link; - -DROP TABLE IF EXISTS Shop_Product_Permutation_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation; - -DROP TABLE IF EXISTS Shop_Variation_Audit; -DROP TABLE IF EXISTS Shop_Variation; -DROP TABLE IF EXISTS Shop_Product_Variation_Type_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Variation_Type_Link; - -DROP TABLE IF EXISTS Shop_Variation_Type_Audit; -DROP TABLE IF EXISTS Shop_Variation_Type; - -DROP TABLE IF EXISTS Shop_Product_Audit; -DROP TABLE IF EXISTS Shop_Product; - -DROP TABLE IF EXISTS Shop_Tax_Or_Surcharge_Audit; -DROP TABLE IF EXISTS Shop_Tax_Or_Surcharge; - -DROP TABLE IF EXISTS Shop_Currency_Audit; -DROP TABLE IF EXISTS Shop_Currency; - -DROP TABLE IF EXISTS Shop_Delivery_Region_Branch_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Region_Branch; -DROP TABLE IF EXISTS Shop_Region_Branch_Audit; -DROP TABLE IF EXISTS Shop_Region_Branch; - -DROP TABLE IF EXISTS Shop_Delivery_Region_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Region; -DROP TABLE IF EXISTS Shop_Region_Audit; -DROP TABLE IF EXISTS Shop_Region; - -DROP TABLE IF EXISTS Shop_Interval_Recurrence_Audit; -DROP TABLE IF EXISTS Shop_Interval_Recurrence; - -DROP TABLE IF EXISTS Shop_Product_Category_Audit; -DROP TABLE IF EXISTS Shop_Product_Category; - -DROP TABLE IF EXISTS Shop_General_Audit; -DROP TABLE IF EXISTS Shop_General; - -DROP TABLE IF EXISTS File_Type_Audit; -DROP TABLE IF EXISTS File_Type; - -DROP TABLE IF EXISTS Msg_Error_Type; - -DROP TABLE IF EXISTS Shop_Access_Level_Audit; -DROP TABLE IF EXISTS Shop_Access_Level; - -DROP TABLE IF EXISTS Shop_Sales_And_Purchasing_Change_Set; -DROP TABLE IF EXISTS Shop_User_Change_Set; - -DROP TABLE IF EXISTS Shop_Msg_Error_Type; - -DROP TABLE IF EXISTS Shop_Product_Change_Set; - -DO $$ -BEGIN - RAISE NOTICE 'TABLE DELETION COMPLETE'; -END $$; - - -DROP FUNCTION IF EXISTS fn_shop_user_eval; -DROP FUNCTION IF EXISTS p_shop_calc_user; -DROP PROCEDURE IF EXISTS fn_shop_user_eval; -DROP PROCEDURE IF EXISTS p_shop_calc_user; - -DROP FUNCTION IF EXISTS fn_shop_save_product; -DROP FUNCTION IF EXISTS p_shop_save_product; -DROP PROCEDURE IF EXISTS fn_shop_save_product; -DROP PROCEDURE IF EXISTS p_shop_save_product; - -DROP FUNCTION IF EXISTS fn_shop_save_supplier; -DROP FUNCTION IF EXISTS p_shop_save_supplier; -DROP PROCEDURE IF EXISTS fn_shop_save_supplier; -DROP PROCEDURE IF EXISTS p_shop_save_supplier; - -DROP FUNCTION IF EXISTS fn_shop_save_supplier_purchase_order; -DROP FUNCTION IF EXISTS p_shop_save_supplier_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_save_supplier_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_save_supplier_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_save_manufacturing_purchase_order; -DROP FUNCTION IF EXISTS p_shop_save_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_save_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_save_manufacturing_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_save_customer; -DROP FUNCTION IF EXISTS p_shop_save_customer; -DROP PROCEDURE IF EXISTS fn_shop_save_customer; -DROP PROCEDURE IF EXISTS p_shop_save_customer; - -DROP FUNCTION IF EXISTS fn_shop_save_customer_sales_order; -DROP FUNCTION IF EXISTS p_shop_save_customer_sales_order; -DROP PROCEDURE IF EXISTS fn_shop_save_customer_sales_order; -DROP PROCEDURE IF EXISTS p_shop_save_customer_sales_order; - -DROP FUNCTION IF EXISTS fn_shop_save_user; -DROP FUNCTION IF EXISTS p_shop_save_user; -DROP PROCEDURE IF EXISTS fn_shop_save_user; -DROP PROCEDURE IF EXISTS p_shop_save_user; - -DROP FUNCTION IF EXISTS fn_shop_save_user_basket; -DROP FUNCTION IF EXISTS p_shop_save_user_basket; -DROP PROCEDURE IF EXISTS fn_shop_save_user_basket; -DROP PROCEDURE IF EXISTS p_shop_save_user_basket; - -DROP FUNCTION IF EXISTS fn_shop_get_many_product; -DROP FUNCTION IF EXISTS p_shop_get_many_product; -DROP PROCEDURE IF EXISTS fn_shop_get_many_product; -DROP PROCEDURE IF EXISTS p_shop_get_many_product; - -DROP FUNCTION IF EXISTS fn_shop_get_many_role_permission; -DROP FUNCTION IF EXISTS p_shop_get_many_role_permission; -DROP PROCEDURE IF EXISTS fn_shop_get_many_role_permission; -DROP PROCEDURE IF EXISTS p_shop_get_many_role_permission; - -DROP FUNCTION IF EXISTS fn_shop_get_many_currency; -DROP FUNCTION IF EXISTS p_shop_get_many_currency; -DROP PROCEDURE IF EXISTS fn_shop_get_many_currency; -DROP PROCEDURE IF EXISTS p_shop_get_many_currency; - -DROP FUNCTION IF EXISTS fn_shop_get_many_region; -DROP FUNCTION IF EXISTS p_shop_get_many_region; -DROP PROCEDURE IF EXISTS fn_shop_get_many_region; -DROP PROCEDURE IF EXISTS p_shop_get_many_region; - -DROP FUNCTION IF EXISTS fn_shop_get_many_user_order; -DROP FUNCTION IF EXISTS p_shop_get_many_user_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_user_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_user_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_stripe_product_new; -DROP FUNCTION IF EXISTS p_shop_get_many_stripe_product_new; -DROP PROCEDURE IF EXISTS fn_shop_get_many_stripe_product_new; -DROP PROCEDURE IF EXISTS p_shop_get_many_stripe_product_new; - -DROP FUNCTION IF EXISTS fn_shop_get_many_stripe_price_new; -DROP FUNCTION IF EXISTS p_shop_get_many_stripe_price_new; -DROP PROCEDURE IF EXISTS fn_shop_get_many_stripe_price_new; -DROP PROCEDURE IF EXISTS p_shop_get_many_stripe_price_new; - -DROP FUNCTION IF EXISTS fn_shop_get_many_supplier; -DROP FUNCTION IF EXISTS p_shop_get_many_supplier; -DROP PROCEDURE IF EXISTS fn_shop_get_many_supplier; -DROP PROCEDURE IF EXISTS p_shop_get_many_supplier; - -DROP FUNCTION IF EXISTS fn_shop_get_many_supplier_purchase_order; -DROP FUNCTION IF EXISTS p_shop_get_many_supplier_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_supplier_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_supplier_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_manufacturing_purchase_order; -DROP FUNCTION IF EXISTS p_shop_get_many_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_manufacturing_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_customer; -DROP FUNCTION IF EXISTS p_shop_get_many_customer; -DROP PROCEDURE IF EXISTS fn_shop_get_many_customer; -DROP PROCEDURE IF EXISTS p_shop_get_many_customer; - -DROP FUNCTION IF EXISTS fn_shop_get_many_customer_sales_order; -DROP FUNCTION IF EXISTS p_shop_get_many_customer_sales_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_customer_sales_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_customer_sales_order; --- Product Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Change_Set'; - -CREATE TABLE Shop_Product_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); --- User Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Change_Set'; - -CREATE TABLE IF NOT EXISTS Shop_User_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); --- Access Levels - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Access_Level'; - -CREATE TABLE IF NOT EXISTS Shop_Access_Level ( - id_access_level INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - priority INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Access_Level_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Sales And Purchasing Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Sales_And_Purchasing_Change_Set'; - -CREATE TABLE Shop_Sales_And_Purchasing_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); --- Access Level Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Access_Level_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Access_Level_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_access_level INTEGER NOT NULL, - CONSTRAINT FK_Shop_Access_Level_Audit_id_access_level - FOREIGN KEY (id_access_level) - REFERENCES Shop_Access_Level(id_access_level) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Access_Level_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Error Message Type - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Msg_Error_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Msg_Error_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - description VARCHAR(1000) -); - --- File Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'File_Type'; - -CREATE TABLE IF NOT EXISTS File_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(100), - extension VARCHAR(50), - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); - --- File Type Audit - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'File_Type_Audit'; - -CREATE TABLE IF NOT EXISTS File_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_File_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES File_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); --- Generic / shared properties - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_General'; - -CREATE TABLE IF NOT EXISTS Shop_General ( - id_general INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - quantity_max REAL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT CHK_Shop_General_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Shop General Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_General_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_General_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_general INTEGER NOT NULL, - CONSTRAINT FK_Shop_General_Audit_id_general - FOREIGN KEY (id_general) - REFERENCES Shop_General(id_general) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_General_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Categories - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Category'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Category ( - id_category INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - description VARCHAR(4000), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Category_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Category Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Category_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Category_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_category INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Category_Audit_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Category_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Recurrence Interval - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Interval_Recurrence'; - -CREATE TABLE IF NOT EXISTS Shop_Interval_Recurrence ( - id_interval INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Interval_Recurrence_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Recurrence Interval Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Interval_Recurrence_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Interval_Recurrence_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_interval INTEGER NOT NULL, - CONSTRAINT FK_Shop_Interval_Recurrence_Audit_id_interval - FOREIGN KEY (id_interval) - REFERENCES Shop_Interval_Recurrence(id_interval) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Interval_Recurrence_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Regions - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region'; - -CREATE TABLE IF NOT EXISTS Shop_Region ( - id_region INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Region_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Region Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Audit_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Region Branchs - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Branch'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Branch ( - id_branch INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_region_parent INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_id_region_parent - FOREIGN KEY (id_region_parent) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_region_child INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_id_region_child - FOREIGN KEY (id_region_child) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - -- depth INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Region_Branch_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Region Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Branch_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Branch_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_branch INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_Audit_id_branch - FOREIGN KEY (id_branch) - REFERENCES Shop_Region_Branch(id_branch) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Currencies - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Currency'; - -CREATE TABLE IF NOT EXISTS Shop_Currency ( - id_currency INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(255) NOT NULL, - symbol VARCHAR(1) NOT NULL, - factor_from_GBP REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Currency_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Currency Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Currency_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Currency_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Currency_Audit_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Currency_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Taxes and Surcharges - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Tax_Or_Surcharge'; - -CREATE TABLE Shop_Tax_Or_Surcharge ( - id_tax INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - id_region_buyer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_region_buyer - FOREIGN KEY (id_region_buyer) - REFERENCES Shop_Region(id_region), - id_region_seller INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_region_seller - FOREIGN KEY (id_region_seller) - REFERENCES Shop_Region(id_region), - id_currency INTEGER, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - fixed_fee REAL NOT NULL DEFAULT 0, - multiplier REAL NOT NULL DEFAULT 1 CHECK (multiplier > 0), - apply_fixed_fee_before_multiplier BOOLEAN NOT NULL DEFAULT TRUE, - quantity_min REAL NOT NULL DEFAULT 0, - quantity_max REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Tax Or Surcharge Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Tax_Or_Surcharge_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Tax_Or_Surcharge_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_tax INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_Audit_id_discount - FOREIGN KEY (id_tax) - REFERENCES Shop_Tax_Or_Surcharge(id_tax) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Products - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product'; - -CREATE TABLE IF NOT EXISTS Shop_Product ( - id_product INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000), - id_category INTEGER NOT NULL, - has_variations BOOLEAN NOT NULL, - /* - price_GBP_full REAL, - price_GBP_min REAL, - -- ratio_discount_overall REAL NOT NULL DEFAULT 0, - CONSTRAINT FK_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category) - ON UPDATE RESTRICT, - latency_manuf INTEGER, - quantity_min REAL, - quantity_max REAL, - quantity_step REAL, - quantity_stock REAL, - is_subscription BOOLEAN, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - */ - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - -- id_stripe_product VARCHAR(100), - -- id_stripe_price VARCHAR(100) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Products - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Audit_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Variation Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Variation_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Variation Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Type_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Variation_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Type_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Variations - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation'; - -CREATE TABLE Shop_Variation ( - id_variation INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Variation_Type(id_type) - ON UPDATE RESTRICT, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Variation_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Variation Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_variation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Audit_id_variation - FOREIGN KEY (id_variation) - REFERENCES Shop_Variation(id_variation) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Product Permutation - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation ( - id_permutation INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Variation_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - -- name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - cost_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - profit_local_min REAL NOT NULL, - -- id_currency_profit_min INTEGER NOT NULL, - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - /* - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - */ - id_stripe_product VARCHAR(100) NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Variation_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Product Permutation Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Audit_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); - --- Product Permutation Variation Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Variation_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Variation_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_variation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_variation - FOREIGN KEY (id_variation) - REFERENCES Shop_Variation(id_variation) - ON UPDATE RESTRICT, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Product Permutation Variation Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Variation_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Variation_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Permutation_Variation_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Product Currency Region link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Currency_Region_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Currency_Region_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - id_region_purchase INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_region_purchase - FOREIGN KEY (id_region_purchase) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - price_local_VAT_incl REAL NULL, - price_local_VAT_excl REAL NULL, - id_stripe_price VARCHAR(200), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Product Currency Region Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Currency_Region_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Currency_Region_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Currency_Region_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Image Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Image_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Image Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Type_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Image_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Type_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Images - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image'; - -CREATE TABLE IF NOT EXISTS Shop_Image ( - id_image INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type_image INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_id_type_image - FOREIGN KEY (id_type_image) - REFERENCES Shop_Image_Type(id_type), - id_type_file INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_id_type_file - FOREIGN KEY (id_type_file) - REFERENCES File_Type(id_type), - id_product INTEGER NULL, - CONSTRAINT FK_Shop_Image_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Image_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - url VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Image_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Image Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_image INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Audit_id_image - FOREIGN KEY (id_image) - REFERENCES Shop_Image(id_image), - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Delivery Options - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Delivery_Option'; - -CREATE TABLE IF NOT EXISTS Shop_Delivery_Option ( - id_option INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(100) NOT NULL, - description VARCHAR(4000), - latency_delivery_min INTEGER NOT NULL, - latency_delivery_max INTEGER NOT NULL, - quantity_min INTEGER NOT NULL, - quantity_max INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Delivery_Option_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Delivery Option Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Delivery_Option_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Delivery_Option_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_option INTEGER NOT NULL, - CONSTRAINT FK_Shop_Delivery_Option_Audit_id_option - FOREIGN KEY (id_option) - REFERENCES Shop_Delivery_Option(id_option) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Delivery_Option_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Delivery Option - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Delivery_Option_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Delivery_Option_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_delivery_option INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_delivery_option - FOREIGN KEY (id_delivery_option) - REFERENCES Shop_Delivery_Option(id_option) - ON UPDATE RESTRICT, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - price_local REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Delivery Option Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Delivery_Option_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Delivery_Option_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Permutation_Delivery_Option_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Discounts - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount'; - -CREATE TABLE Shop_Discount ( - id_discount INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER, - CONSTRAINT FK_Shop_Discount_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - /* - id_delivery_region INTEGER, - CONSTRAINT FK_Shop_Discount_id_delivery_region - FOREIGN KEY (id_delivery_region) - REFERENCES Shop_Delivery_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER, - CONSTRAINT FK_Shop_Discount_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - */ - multiplier REAL NOT NULL DEFAULT 1 CHECK (multiplier > 0), - subtractor REAL NOT NULL DEFAULT 0, - apply_multiplier_first BOOLEAN NOT NULL DEFAULT TRUE, - quantity_min REAL NOT NULL DEFAULT 0, - quantity_max REAL NOT NULL, - date_start TIMESTAMP NOT NULL, - date_end TIMESTAMP NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Discount_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Discount Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_discount INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Audit_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Discount Region Currency Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Region_Currency_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Region_Currency_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_discount INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount) - ON UPDATE RESTRICT, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Discount Region Currency Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Region_Currency_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Region_Currency_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Discount_Region_Currency_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Permission Groups - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Group'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Group ( - id_group INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Permission_Group_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Permission Group Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Group_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Group_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_group INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Group_Audit_id_group - FOREIGN KEY (id_group) - REFERENCES Shop_Permission_Group(id_group) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Group_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Permissions - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission'; - -CREATE TABLE IF NOT EXISTS Shop_Permission ( - id_permission INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - id_permission_group INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_id_permission_group - FOREIGN KEY (id_permission_group) - REFERENCES Shop_Permission_Group(id_group) - ON UPDATE RESTRICT, - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Permission_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Permission Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permission INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Audit_id_permission - FOREIGN KEY (id_permission) - REFERENCES Shop_Permission(id_permission) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Roles - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role'; - -CREATE TABLE IF NOT EXISTS Shop_Role ( - id_role INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Role_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Role Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Audit'; - -CREATE TABLE Shop_Role_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_role INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Audit_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- Role Permission link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Permission_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Role_Permission_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_role INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role) - ON UPDATE RESTRICT, - id_permission INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_permission - FOREIGN KEY (id_permission) - REFERENCES Shop_Permission(id_permission) - ON UPDATE RESTRICT, - id_access_level INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_access_level - FOREIGN KEY (id_access_level) - REFERENCES Shop_Access_Level(id_access_level), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Role Permission link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Permission_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Role_Permission_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Permission_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Role_Permission_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Permission_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Users - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User'; - -CREATE TABLE IF NOT EXISTS Shop_User ( - id_user INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user_oauth VARCHAR(200) NOT NULL, - name VARCHAR(255) NOT NULL, - email VARCHAR(254) NOT NULL, - is_email_verified BOOLEAN NOT NULL DEFAULT FALSE, - is_super_user BOOLEAN NOT NULL DEFAULT FALSE, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); - --- User Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Audit_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- User Role link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Role_Link'; - -CREATE TABLE IF NOT EXISTS Shop_User_Role_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Role_Link_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - id_role INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_Role_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- User Role Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Role_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Role_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_User_Role_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); - --- Addresses - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Address'; - -CREATE TABLE Shop_Address ( - id_address INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - /* - a_id_user INTEGER, - CONSTRAINT FK_Shop_Address_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - */ - -- region VARCHAR(100) NOT NULL, - id_region INTEGER NOT NULL, - name_full VARCHAR(255) NOT NULL, - phone_number VARCHAR(20) NOT NULL, - postcode VARCHAR(20) NOT NULL, - address_line_1 VARCHAR(100) NOT NULL, - address_line_2 VARCHAR(100) NOT NULL, - city VARCHAR(50) NOT NULL, - county VARCHAR(100) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Address_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); --- Address Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Address_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Address_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Address_Audit_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Address_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); --- User Basket (Product Link) - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Basket'; - -CREATE TABLE IF NOT EXISTS Shop_User_Basket ( - id_item INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Basket_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - quantity INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_change_set_user - FOREIGN KEY (id_change_set_user) - REFERENCES Shop_User_Change_Set(id_change_set) - /* - id_change_set_product INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_change_set_product - FOREIGN KEY (id_change_set_product) - REFERENCES Shop_Product_Change_Set(id_change_set) - */ -); - --- Product Basket Audits - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Basket_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Basket_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_item INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Basket_Audit_id_link - FOREIGN KEY (id_item) - REFERENCES Shop_User_Basket(id_item) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_Audit_id_change_set_user - FOREIGN KEY (id_change_set_user) - REFERENCES Shop_User_Change_Set(id_change_set) - /* - id_change_set_product INTEGER, - CONSTRAINT FK_Shop_User_Basket_Audit_id_change_set_product - FOREIGN KEY (id_change_set_product) - REFERENCES Shop_Product_Change_Set(id_change_set) - */ -); - --- User Order Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Order_Status'; - -CREATE TABLE IF NOT EXISTS Shop_User_Order_Status ( - id_status INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_Order_Status_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); - --- Order Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Order_Status_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Order_Status_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_status INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Order_Status_Audit_id_status - FOREIGN KEY (id_status) - REFERENCES Shop_User_Order_Status(id_status) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Order_Status_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); --- Supplier - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier ( - id_supplier INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Supplier_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); - --- Supplier Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Audit_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); - --- Unit of Measurement - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement ( - id_unit_measurement INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_singular VARCHAR(255) NOT NULL, - name_plural VARCHAR(256) NOT NULL, - symbol VARCHAR(50) NOT NULL, - is_base_unit BOOLEAN NOT NULL DEFAULT FALSE, - - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Unit_Measurement_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Unit of Measurement Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_unit_measurement INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Audit_id_unit_measurement - FOREIGN KEY (id_unit_measurement) - REFERENCES Shop_Unit_Measurement(id_unit_measurement) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Unit of Measurement Conversion - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Conversion'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Conversion ( - id_conversion INTEGER NOT NULL PRIMARY KEY, - id_unit_derived INTEGER NOT NULL, - id_unit_base INTEGER NOT NULL, - power_unit_base REAL NOT NULL, - multiplier_unit_base REAL NOT NULL, - increment_unit_base REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Unit of Measurement Conversion Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Conversion_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Conversion_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_conversion INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_Audit_id_conversion - FOREIGN KEY (id_conversion) - REFERENCES Shop_Unit_Measurement_Conversion(id_conversion) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); - --- Supplier Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - /* - id_supplier_fulfilled INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_supplier_fulfilled - FOREIGN KEY (id_supplier_fulfilled) - REFERENCES Shop_Supplier(id_supplier), - */ - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - -- quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Supplier Purchase Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Supplier Purchase Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Supplier Purchase Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purch_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Supplier_Purchase_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purch_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Supplier Purchase Order Product Link Temp - - - --- drop table Shop_Supplier_Purchase_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); - - --- Manufacturing Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Manufacturing Purchase Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Manufacturing Purchase Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - latency_manufacture INTEGER NOT NULL, - quantity_produced REAL NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Manufacturing Purchase Order Product Link Temp - - - --- DROP TABLE Shop_Manufacturing_Purchase_Order_Product_Link_Temp; --- SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_manufacture INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); - --- Manufacturing Purchase Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purch_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Manufacturing_Purchase_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purch_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); --- Customer - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer'; - -CREATE TABLE IF NOT EXISTS Shop_Customer ( - id_customer INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Customer_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_customer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Audit_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Sales Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_customer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Sales Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Sales Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NOT NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Sales Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Customer_Sales_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); - --- Customer Sales Order Product Link Temp - - - --- DROP TABLE Shop_Customer_Sales_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); - -DO $$ -BEGIN - RAISE NOTICE 'TABLE CREATION COMPLETE'; -END $$; - --- Product Change Set - -CREATE OR REPLACE FUNCTION before_insert_Shop_Sales_And_Purchasing_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Sales_And_Purchasing_Change_Set -BEFORE INSERT ON Shop_Sales_And_Purchasing_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Sales_And_Purchasing_Change_Set(); - - --- Shop User Change Set - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Change_Set -BEFORE INSERT ON Shop_User_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Change_Set(); - --- Shop Access Level - -CREATE OR REPLACE FUNCTION before_insert_Shop_Access_Level() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Access_Level -BEFORE INSERT ON Shop_Access_Level -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Access_Level(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Access_Level() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Access_Level_Audit ( - id_access_level, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_access_level, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT (OLD.code <=> NEW.code) - UNION - -- Changed name - SELECT NEW.id_access_level, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - -- Changed priority - SELECT NEW.id_access_level, 'priority', CONVERT(OLD.priority, CHAR), CONVERT(NEW.priority, CHAR), NEW.id_change_set - WHERE NOT (OLD.priority <=> NEW.priority) - UNION - -- Changed active - SELECT NEW.id_access_level, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_access_level, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Access_Level -BEFORE UPDATE ON Shop_Access_Level -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Access_Level(); - --- Product Change Set - - - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Change_Set -BEFORE INSERT ON Shop_Product_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Change_Set(); - --- File Type - -CREATE OR REPLACE FUNCTION before_insert_File_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_File_Type -BEFORE INSERT ON File_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_File_Type(); - - -CREATE OR REPLACE FUNCTION before_update_File_Type() -RETURNS TRIGGER AS $$ -BEGIN - INSERT INTO File_Type_Audit ( - id_type, - name_field, - value_prev, - value_new - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed extension - SELECT NEW.id_type, 'extension', CONVERT(OLD.extension, CHAR), CONVERT(NEW.extension, CHAR) - WHERE NOT OLD.extension <=> NEW.extension - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_File_Type -BEFORE UPDATE ON File_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_File_Type(); - --- File Type Audits - -CREATE OR REPLACE FUNCTION before_insert_File_Type_Audit() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_File_Type_Audit -BEFORE INSERT ON File_Type_Audit -FOR EACH ROW -EXECUTE FUNCTION before_insert_File_Type_Audit(); - - -CREATE OR REPLACE FUNCTION before_update_File_Type_Audit() -RETURNS TRIGGER AS $$ -BEGIN - NEW.updated_last_on = CURRENT_TIMESTAMP; - NEW.updated_last_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_File_Type_Audit -BEFORE UPDATE ON File_Type_Audit -FOR EACH ROW -EXECUTE FUNCTION before_update_File_Type_Audit(); --- Shop General - -CREATE OR REPLACE FUNCTION before_insert_Shop_General() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_General -BEFORE INSERT ON Shop_General -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_General(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_General() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_General_Audit ( - id_general, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed quantity max - SELECT NEW.id_general, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_General -BEFORE UPDATE ON Shop_General -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_General(); --- Shop Category - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Category() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Category -BEFORE INSERT ON Shop_Product_Category -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Category(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Category() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Category_Audit ( - id_category, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_category, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_category, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed description - SELECT NEW.id_category, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT OLD.description <=> NEW.description - UNION - -- Changed active - SELECT NEW.id_category, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_category, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Category -BEFORE UPDATE ON Shop_Product_Category -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Category(); - --- Shop Recurrence Interval - -CREATE OR REPLACE FUNCTION before_insert_Shop_Interval_Recurrence() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Interval_Recurrence -BEFORE INSERT ON Shop_Interval_Recurrence -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Interval_Recurrence(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Interval_Recurrence() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Interval_Recurrence_Audit ( - id_interval, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_interval, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_interval, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_interval, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed name - SELECT NEW.id_interval, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Interval_Recurrence -BEFORE UPDATE ON Shop_Interval_Recurrence -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Interval_Recurrence(); - --- Shop Delivery Region - -CREATE OR REPLACE FUNCTION before_insert_Shop_Region() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Region -BEFORE INSERT ON Shop_Region -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Region(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Region() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Region_Audit ( - id_region, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_region, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_region, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_region, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_region, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Region -BEFORE UPDATE ON Shop_Region -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Region(); - --- Shop Region Branch - -CREATE OR REPLACE FUNCTION before_insert_Shop_Region_Branch() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Region_Branch -BEFORE INSERT ON Shop_Region_Branch -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Region_Branch(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Region_Branch() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Region_Branch_Audit ( - id_branch, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed depth - SELECT NEW.id_branch, 'depth', CONVERT(OLD.depth, CHAR), CONVERT(NEW.depth, CHAR), NEW.id_change_set - WHERE NOT OLD.depth <=> NEW.depth - UNION - */ - -- Changed active - SELECT NEW.id_branch, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_branch, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Region_Branch -BEFORE UPDATE ON Shop_Region_Branch -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Region_Branch(); - --- Shop Currency - -CREATE OR REPLACE FUNCTION before_insert_Shop_Currency() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Currency -BEFORE INSERT ON Shop_Currency -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Currency(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Currency() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Currency_Audit ( - id_currency, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_currency, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_currency, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed symbol - SELECT NEW.id_currency, 'symbol', OLD.symbol, NEW.symbol, NEW.id_change_set - WHERE NOT OLD.symbol <=> NEW.symbol - UNION - -- Changed ratio_2_GBP - SELECT NEW.id_currency, 'factor_from_GBP', OLD.factor_from_GBP, NEW.factor_from_GBP, NEW.id_change_set - WHERE NOT OLD.factor_from_GBP <=> NEW.factor_from_GBP - UNION - -- Changed active - SELECT NEW.id_currency, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_currency, 'display_order', CONVERT(display_order, CHAR), CONVERT(display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Currency -BEFORE UPDATE ON Shop_Currency -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Currency(); - --- Shop Tax_Or_Surcharge - -CREATE OR REPLACE FUNCTION before_insert_Shop_Tax_Or_Surcharge() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Tax_Or_Surcharge -BEFORE INSERT ON Shop_Tax_Or_Surcharge -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Tax_Or_Surcharge(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Tax_Or_Surcharge() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Tax_Or_Surcharge_Audit ( - id_tax, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_tax, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_tax, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed fixed_fee - SELECT NEW.id_tax, 'fixed_fee', OLD.fixed_fee, NEW.fixed_fee, NEW.id_change_set - WHERE NOT OLD.fixed_fee <=> NEW.fixed_fee - UNION - -- Changed multiplier - SELECT NEW.id_tax, 'multiplier', OLD.multiplier, NEW.multiplier, NEW.id_change_set - WHERE NOT OLD.multiplier <=> NEW.multiplier - UNION - -- Changed apply_fixed_fee_before_multiplier - SELECT NEW.id_tax, 'apply_fixed_fee_before_multiplier', CONVERT(CONVERT(OLD.apply_fixed_fee_before_multiplier, SIGNED), CHAR), CONVERT(CONVERT(NEW.apply_fixed_fee_before_multiplier, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.apply_fixed_fee_before_multiplier <=> NEW.apply_fixed_fee_before_multiplier - UNION - -- Changed quantity_min - SELECT NEW.id_tax, 'quantity_min', OLD.quantity_min, NEW.quantity_min, NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_tax, 'quantity_max', OLD.quantity_max, NEW.quantity_max, NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed display_order - SELECT NEW.id_tax, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_tax, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Tax_Or_Surcharge -BEFORE UPDATE ON Shop_Tax_Or_Surcharge -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Tax_Or_Surcharge(); - - --- Shop Product - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product -BEFORE INSERT ON Shop_Product -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - /* - IF NOT NEW.has_variations THEN - IF ISNULL(NEW.price_GBP_full) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have price or variations (with prices).'; - END IF; - IF ISNULL(NEW.price_GBP_min) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have minimum price or variations (with prices).'; - END IF; - IF ISNULL(NEW.latency_manuf) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have manufacturing latency or variations (with manufacturing latencies).'; - END IF; - IF ISNULL(NEW.quantity_min) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have minimum quantity or variations (with minimum quantities).'; - END IF; - IF ISNULL(NEW.quantity_max) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have maximum quantity or variations (with maximum quantities).'; - END IF; - IF ISNULL(NEW.quantity_step) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have increment of quantity or variations (with increments of quantities).'; - END IF; - IF ISNULL(NEW.quantity_stock) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have stock quantity or variations (with stock quantities).'; - END IF; - IF ISNULL(NEW.is_subscription) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have subscription status or variations (with subscription statuses).'; - END IF; - IF ISNULL(NEW.id_unit_measurement_interval_recurrence) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have recurrence interval or variations (with recurrence intervals).'; - END IF; - IF ISNULL(NEW.count_interval_recurrence) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have recurrence interval count or variations (with recurrence interval counts).'; - END IF; - END IF; - */ - - INSERT INTO Shop_Product_Audit ( - id_product, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name - SELECT NEW.id_product, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - /* - UNION - -- Changed description - SELECT NEW.id_product, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT OLD.description <=> NEW.description - UNION - -- Changed price_GBP_full - SELECT NEW.id_product, 'price_GBP_full', CONVERT(OLD.price_GBP_full, CHAR), CONVERT(NEW.price_GBP_full, CHAR), NEW.id_change_set - WHERE NOT OLD.price_GBP_full <=> NEW.price_GBP_full - UNION - -- Changed price_GBP_min - SELECT NEW.id_product, 'price_GBP_min', CONVERT(OLD.price_GBP_min, CHAR), CONVERT(NEW.price_GBP_min, CHAR), NEW.id_change_set - WHERE NOT OLD.price_GBP_min <=> NEW.price_GBP_min - UNION - / - -- Changed discount - SELECT NEW.id_product, 'discount', CONVERT(OLD.discount, CHAR), CONVERT(NEW.discount, CHAR), NEW.id_change_set - WHERE NOT OLD.discount <=> NEW.discount - */ - UNION - -- Changed id_category - SELECT NEW.id_product, 'id_category', CONVERT(OLD.id_category, CHAR), CONVERT(NEW.id_category, CHAR), NEW.id_change_set - WHERE NOT OLD.id_category <=> NEW.id_category - UNION - -- Changed has_variations - SELECT NEW.id_product, 'has_variations', CONVERT(CONVERT(NEW.has_variations, SIGNED), CHAR), CONVERT(CONVERT(NEW.has_variations, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.has_variations <=> NEW.has_variations - /* - UNION - -- Changed latency_manuf - SELECT NEW.id_product, 'latency_manuf', CONVERT(OLD.latency_manuf, CHAR), CONVERT(NEW.latency_manuf, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_manuf <=> NEW.latency_manuf - UNION - -- Changed quantity_min - SELECT NEW.id_product, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_product, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed quantity_step - SELECT NEW.id_product, 'quantity_step', CONVERT(OLD.quantity_step, CHAR), CONVERT(NEW.quantity_step, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_step <=> NEW.quantity_step - UNION - -- Changed quantity_stock - SELECT NEW.id_product, 'quantity_stock', CONVERT(OLD.quantity_stock, CHAR), CONVERT(NEW.quantity_stock, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_stock <=> NEW.quantity_stock - UNION - -- Changed is_subscription - SELECT NEW.id_product, 'is_subscription', CONVERT(CONVERT(OLD.is_subscription, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_subscription, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.is_subscription <=> NEW.is_subscription - UNION - -- Changed id_unit_measurement_interval_recurrence - SELECT NEW.id_product, 'id_unit_measurement_interval_recurrence', CONVERT(OLD.id_unit_measurement_interval_recurrence, CHAR), CONVERT(NEW.id_unit_measurement_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.id_unit_measurement_interval_recurrence <=> NEW.id_unit_measurement_interval_recurrence - UNION - -- Changed count_interval_recurrence - SELECT NEW.id_product, 'count_interval_recurrence', CONVERT(OLD.count_interval_recurrence, CHAR), CONVERT(NEW.count_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.count_interval_recurrence <=> NEW.count_interval_recurrence - UNION - -- Changed id_stripe_product - SELECT NEW.id_product, 'id_stripe_product', OLD.id_stripe_product, NEW.id_stripe_product, NEW.id_change_set - WHERE NOT OLD.id_stripe_product <=> NEW.id_stripe_product - / - UNION - -- Changed id_stripe_price - SELECT NEW.id_product, 'id_stripe_price', OLD.id_stripe_price, NEW.id_stripe_price, NEW.id_change_set - WHERE NOT OLD.id_stripe_price <=> NEW.id_stripe_price - */ - UNION - -- Changed id_access_level_required - SELECT NEW.id_product, 'id_access_level_required', CONVERT(OLD.id_access_level_required, CHAR), CONVERT(NEW.id_access_level_required, CHAR), NEW.id_change_set - WHERE NOT OLD.id_access_level_required <=> NEW.id_access_level_required - UNION - -- Changed active - SELECT NEW.id_product, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_product, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product -BEFORE UPDATE ON Shop_Product -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product(); - --- Shop Variation Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Variation_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Variation_Type -BEFORE INSERT ON Shop_Variation_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Variation_Type(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Variation_Type() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Variation_Type_Audit ( - id_type, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_type, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_type, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_type, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Variation_Type -BEFORE UPDATE ON Shop_Variation_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Variation_Type(); - --- Shop Variation - -CREATE OR REPLACE FUNCTION before_insert_Shop_Variation() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Variation -BEFORE INSERT ON Shop_Variation -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Variation(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Variation() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Variation_Audit ( - id_variation, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_variation, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_variation, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_variation, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_variation, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Variation -BEFORE UPDATE ON Shop_Variation -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Variation(); - --- Shop Product Permutation - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation -BEFORE INSERT ON Shop_Product_Permutation -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Audit ( - id_permutation, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_permutation, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_variation - SELECT NEW.id_permutation, 'id_variation', OLD.id_variation, NEW.id_variation, NEW.id_change_set - WHERE NOT OLD.id_variation <=> NEW.id_variation - UNION - -- Changed name - SELECT NEW.id_permutation, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - */ - -- Changed description - SELECT NEW.id_permutation, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT (OLD.description <=> NEW.description) - UNION - -- Changed cost_local - SELECT NEW.id_permutation, 'cost_local', CONVERT(OLD.cost_local, CHAR), CONVERT(NEW.cost_local, CHAR), NEW.id_change_set - WHERE NOT (OLD.cost_local <=> NEW.cost_local) - UNION - -- Changed id_currency_cost - SELECT NEW.id_permutation, 'id_currency_cost', CONVERT(OLD.id_currency_cost, CHAR), CONVERT(NEW.id_currency_cost, CHAR), NEW.id_change_set - WHERE NOT (OLD.id_currency_cost <=> NEW.id_currency_cost) - UNION - -- Changed profit_local_min - SELECT NEW.id_permutation, 'profit_local_min', CONVERT(OLD.profit_local_min, CHAR), CONVERT(NEW.profit_local_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.profit_local_min <=> NEW.profit_local_min) - UNION - /* - -- Changed id_currency_profit_min - SELECT NEW.id_permutation, 'id_currency_profit_min', CONVERT(OLD.id_currency_profit_min, CHAR), CONVERT(NEW.id_currency_profit_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.id_currency_profit_min <=> NEW.id_currency_profit_min) - UNION - */ - /* - -- Changed price_GBP_min - SELECT NEW.id_permutation, 'price_GBP_min', CONVERT(OLD.price_GBP_min, CHAR), CONVERT(NEW.price_GBP_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.price_GBP_min <=> NEW.price_GBP_min) - UNION - */ - -- Changed latency_manufacture - SELECT NEW.id_product, 'latency_manufacture', CONVERT(OLD.latency_manufacture, CHAR), CONVERT(NEW.latency_manufacture, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_manufacture <=> NEW.latency_manufacture - UNION - -- Changed quantity_min - SELECT NEW.id_product, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_product, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed quantity_step - SELECT NEW.id_product, 'quantity_step', CONVERT(OLD.quantity_step, CHAR), CONVERT(NEW.quantity_step, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_step <=> NEW.quantity_step - UNION - -- Changed quantity_stock - SELECT NEW.id_product, 'quantity_stock', CONVERT(OLD.quantity_stock, CHAR), CONVERT(NEW.quantity_stock, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_stock <=> NEW.quantity_stock - UNION - -- Changed is_subscription - SELECT NEW.id_product, 'is_subscription', CONVERT(CONVERT(OLD.is_subscription, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_subscription, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.is_subscription <=> NEW.is_subscription - UNION - -- Changed id_unit_measurement_interval_recurrence - SELECT NEW.id_product, 'id_unit_measurement_interval_recurrence', CONVERT(OLD.id_unit_measurement_interval_recurrence, CHAR), CONVERT(NEW.id_unit_measurement_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.id_unit_measurement_interval_recurrence <=> NEW.id_unit_measurement_interval_recurrence - UNION - -- Changed count_interval_recurrence - SELECT NEW.id_product, 'count_interval_recurrence', CONVERT(OLD.count_interval_recurrence, CHAR), CONVERT(NEW.count_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.count_interval_recurrence <=> NEW.count_interval_recurrence - UNION - -- Changed id_stripe_product - SELECT NEW.id_permutation, 'id_stripe_product', OLD.id_stripe_product, NEW.id_stripe_product, NEW.id_change_set - WHERE NOT (OLD.id_stripe_product <=> NEW.id_stripe_product) - UNION - -- Changed active - SELECT NEW.id_permutation, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_permutation, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation -BEFORE UPDATE ON Shop_Product_Permutation -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation(); - --- Shop Product Permutation Variation Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation_Variation_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation_Variation_Link -BEFORE INSERT ON Shop_Product_Permutation_Variation_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation_Variation_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation_Variation_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Variation_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_variation - SELECT NEW.id_link, 'id_variation', OLD.id_variation, NEW.id_variation, NEW.id_change_set - WHERE NOT OLD.id_variation <=> NEW.id_variation - UNION - */ - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation_Variation_Link -BEFORE UPDATE ON Shop_Product_Permutation_Variation_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation_Variation_Link(); - --- Shop Product Currency Region Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Currency_Region_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - /* - NEW.price_local = ( - SELECT PP.price_GBP_full * C.factor_from_GBP - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Currency C ON NEW.id_currency = C.id_currency - WHERE NEW.id_product = P.id_product - LIMIT 1 - ); - */ - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Currency_Region_Link -BEFORE INSERT ON Shop_Product_Currency_Region_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Currency_Region_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Currency_Region_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - /* - NEW.price_local = ( - SELECT P.price_GBP_full * C.factor_from_GBP - FROM Shop_Product P - INNER JOIN Shop_Currency C ON NEW.id_currency = C.id_currency - WHERE NEW.id_product = P.id_product - LIMIT 1 - ); - */ - - INSERT INTO Shop_Product_Currency_Region_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_currency - SELECT NEW.id_link, 'id_currency', CONVERT(OLD.id_currency, CHAR), CONVERT(NEW.id_currency, CHAR), NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - UNION - -- Changed price_local - SELECT NEW.id_link, 'price_local', OLD.price_local, NEW.price_local, NEW.id_change_set - WHERE NOT OLD.price_local <=> NEW.price_local - UNION - */ - -- Changed price_local_VAT_incl - SELECT NEW.id_link, 'price_local_VAT_incl', OLD.price_local_VAT_incl, NEW.price_local_VAT_incl, NEW.id_change_set - WHERE NOT OLD.price_local_VAT_incl <=> NEW.price_local_VAT_incl - UNION - -- Changed price_local_VAT_excl - SELECT NEW.id_link, 'price_local_VAT_excl', OLD.price_local_VAT_excl, NEW.price_local_VAT_excl, NEW.id_change_set - WHERE NOT OLD.price_local_VAT_excl <=> NEW.price_local_VAT_excl - UNION - -- Changed id_stripe_price - SELECT NEW.id_link, 'id_stripe_price', OLD.id_stripe_price, NEW.id_stripe_price, NEW.id_change_set - WHERE NOT OLD.id_stripe_price <=> NEW.id_stripe_price - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Currency_Region_Link -BEFORE UPDATE ON Shop_Product_Currency_Region_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Currency_Region_Link(); - --- Shop Image Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Image_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Image_Type -BEFORE INSERT ON Shop_Image_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Image_Type(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Image_Type() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Image_Type_Audit ( - id_type, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_type, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_type, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_type, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Image_Type -BEFORE UPDATE ON Shop_Image_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Image_Type(); - --- Shop Image - -CREATE OR REPLACE FUNCTION before_insert_Shop_Image() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Image -BEFORE INSERT ON Shop_Image -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Image(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Image() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - IF ISNULL(NEW.id_product) AND ISNULL(NEW.id_permutation) THEN - RAISE EXCEPTION 'Image must NOT have ID for product AND product permutation.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Image_Audit ( - id_image, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_type_image - SELECT NEW.id_image, 'id_type_image', CONVERT(OLD.id_type_image, CHAR), CONVERT(NEW.id_type_image, CHAR), NEW.id_change_set - WHERE NOT OLD.id_type_image <=> NEW.id_type_image - UNION - -- Changed id_type_file - SELECT NEW.id_image, 'id_type_file', CONVERT(OLD.id_type_file, CHAR), CONVERT(NEW.id_type_file, CHAR), NEW.id_change_set - WHERE NOT OLD.id_type_file <=> NEW.id_type_file - UNION - -- Changed id_product - SELECT NEW.id_image, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_permutation - SELECT NEW.id_image, 'id_permutation', CONVERT(OLD.id_permutation, CHAR), CONVERT(NEW.id_permutation, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed url - SELECT NEW.id_image, 'url', OLD.url, NEW.url, NEW.id_change_set - WHERE NOT OLD.url <=> NEW.url - UNION - -- Changed active - SELECT NEW.id_image, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_image, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Image -BEFORE UPDATE ON Shop_Image -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Image(); - --- Shop Delivery Option Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Delivery_Option() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Delivery_Option -BEFORE INSERT ON Shop_Delivery_Option -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Delivery_Option(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Delivery_Option() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Delivery_Option_Audit ( - id_option, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_option, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_option, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed latency_delivery_min - SELECT NEW.id_option, 'latency_delivery_min', CONVERT(OLD.latency_delivery_min, CHAR), CONVERT(NEW.latency_delivery_min, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_delivery_min <=> NEW.latency_delivery_min - UNION - -- Changed latency_delivery_max - SELECT NEW.id_option, 'latency_delivery_max', CONVERT(OLD.latency_delivery_max, CHAR), CONVERT(NEW.latency_delivery_max, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_delivery_max <=> NEW.latency_delivery_max - UNION - -- Changed quantity_min - SELECT NEW.id_option, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_option, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed active - SELECT NEW.id_option, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_option, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Delivery_Option -BEFORE UPDATE ON Shop_Delivery_Option -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Delivery_Option(); - --- Shop Product Delivery Option Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation_Delivery_Option_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation_Delivery_Option_Link -BEFORE INSERT ON Shop_Product_Permutation_Delivery_Option_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation_Delivery_Option_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation_Delivery_Option_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Delivery_Option_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', CONVERT(OLD.id_permutation, CHAR), CONVERT(NEW.id_permutation, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed id_option - SELECT NEW.id_link, 'id_option', CONVERT(OLD.id_option, CHAR), CONVERT(NEW.id_option, CHAR), NEW.id_change_set - WHERE NOT OLD.id_option <=> NEW.id_option - UNION - -- Changed id_region - SELECT NEW.id_link, 'id_region', CONVERT(OLD.id_region, CHAR), CONVERT(NEW.id_region, CHAR), NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - */ - -- Changed price_local - SELECT NEW.id_link, 'price_local', CONVERT(OLD.price_local, CHAR), CONVERT(NEW.price_local, CHAR), NEW.id_change_set - WHERE NOT OLD.price_local <=> NEW.price_local - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation_Delivery_Option_Link -BEFORE UPDATE ON Shop_Product_Permutation_Delivery_Option_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation_Delivery_Option_Link(); - --- Shop Discount - -CREATE OR REPLACE FUNCTION before_insert_Shop_Discount() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Discount -BEFORE INSERT ON Shop_Discount -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Discount(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Discount() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Discount_Audit ( - id_discount, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_discount, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_discount, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed multiplier - SELECT NEW.id_discount, 'multiplier', OLD.multiplier, NEW.multiplier, NEW.id_change_set - WHERE NOT OLD.multiplier <=> NEW.multiplier - UNION - -- Changed subtractor - SELECT NEW.id_discount, 'subtractor', OLD.subtractor, NEW.subtractor, NEW.id_change_set - WHERE NOT OLD.subtractor <=> NEW.subtractor - UNION - -- Changed apply_multiplier_first - SELECT NEW.id_discount, 'apply_multiplier_first', CONVERT(CONVERT(OLD.apply_multiplier_first, SIGNED), CHAR), CONVERT(CONVERT(NEW.apply_multiplier_first, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.apply_multiplier_first <=> NEW.apply_multiplier_first - UNION - -- Changed quantity_min - SELECT NEW.id_discount, 'quantity_min', OLD.quantity_min, NEW.quantity_min, NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_discount, 'quantity_max', OLD.quantity_max, NEW.quantity_max, NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed date_start - SELECT NEW.id_discount, 'date_start', OLD.date_start, NEW.date_start, NEW.id_change_set - WHERE NOT OLD.date_start <=> NEW.date_start - UNION - -- Changed date_end - SELECT NEW.id_discount, 'date_end', OLD.date_end, NEW.date_end, NEW.id_change_set - WHERE NOT OLD.date_end <=> NEW.date_end - UNION - -- Changed display_order - SELECT NEW.id_discount, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_discount, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Discount -BEFORE UPDATE ON Shop_Discount -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Discount(); - --- Shop Discount Region Currency Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Discount_Region_Currency_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Discount_Region_Currency_Link -BEFORE INSERT ON Shop_Discount_Region_Currency_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Discount_Region_Currency_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Discount_Region_Currency_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Discount_Region_Currency_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_discount - SELECT NEW.id_link, 'id_discount', CONVERT(OLD.id_discount, CHAR), CONVERT(NEW.id_discount, CHAR), NEW.id_change_set - WHERE NOT OLD.id_discount <=> NEW.id_discount - UNION - -- Changed id_region - SELECT NEW.id_link, 'id_region', CONVERT(OLD.id_region, CHAR), CONVERT(NEW.id_region, CHAR), NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - */ - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Discount_Region_Currency_Link -BEFORE UPDATE ON Shop_Discount_Region_Currency_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Discount_Region_Currency_Link(); - --- Shop Permission Group - -CREATE OR REPLACE FUNCTION before_insert_Shop_Permission_Group() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Permission_Group -BEFORE INSERT ON Shop_Permission_Group -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Permission_Group(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Permission_Group() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Permission_Group_Audit ( - id_group, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_group, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_group, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_group, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_group, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Permission_Group -BEFORE UPDATE ON Shop_Permission_Group -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Permission_Group(); - --- Shop Permission - -CREATE OR REPLACE FUNCTION before_insert_Shop_Permission() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Permission -BEFORE INSERT ON Shop_Permission -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Permission(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Permission() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Permission_Audit ( - id_permission, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_permission, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_permission, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed id_permission_group - SELECT NEW.id_permission, 'id_permission_group', CONVERT(OLD.id_permission_group, CHAR), CONVERT(NEW.id_permission_group, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permission_group <=> NEW.id_permission_group - UNION - -- Changed Id_access_level_required - SELECT NEW.id_permission, 'Id_access_level_required', CONVERT(OLD.Id_access_level_required, CHAR), CONVERT(NEW.Id_access_level_required, CHAR), NEW.id_change_set - WHERE NOT OLD.Id_access_level_required <=> NEW.Id_access_level_required - UNION - -- Changed active - SELECT NEW.id_permission, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_permission, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Permission -BEFORE UPDATE ON Shop_Permission -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Permission(); - --- Shop Role - -CREATE OR REPLACE FUNCTION before_insert_Shop_Role() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Role -BEFORE INSERT ON Shop_Role -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Role(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Role() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Role_Audit ( - id_role, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_role, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_role, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_role, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_role, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Role -BEFORE UPDATE ON Shop_Role -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Role(); - --- Shop Role Permission Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Role_Permission_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Role_Permission_Link -BEFORE INSERT ON Shop_Role_Permission_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Role_Permission_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Role_Permission_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Role_Permission_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_role - SELECT NEW.id_link, 'id_role', CONVERT(OLD.id_role, CHAR), CONVERT(NEW.id_role, CHAR), NEW.id_change_set - WHERE NOT OLD.id_role <=> NEW.id_role - UNION - -- Changed id_permission - SELECT NEW.id_link, 'id_permission', CONVERT(OLD.id_permission, CHAR), CONVERT(NEW.id_permission, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permission <=> NEW.id_permission - UNION - */ - -- Changed id_access_level - SELECT NEW.id_link, 'id_access_level', CONVERT(OLD.id_access_level, CHAR), CONVERT(NEW.id_access_level, CHAR), NEW.id_change_set - WHERE NOT OLD.id_access_level <=> NEW.id_access_level - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Role_Permission_Link -BEFORE UPDATE ON Shop_Role_Permission_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Role_Permission_Link(); - --- Shop User - -CREATE OR REPLACE FUNCTION before_insert_Shop_User() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User -BEFORE INSERT ON Shop_User -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Audit ( - id_user, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_user_oauth - SELECT NEW.id_user, 'id_user_oauth', OLD.id_user_oauth, NEW.id_user_oauth, NEW.id_change_set - WHERE NOT (OLD.id_user_oauth <=> NEW.id_user_oauth) - UNION - -- Changed name - SELECT NEW.id_user, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - -- Changed email - SELECT NEW.id_user, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT (OLD.email <=> NEW.email) - UNION - -- Changed is_email_verified - SELECT NEW.id_user, 'is_email_verified', OLD.is_email_verified, NEW.is_email_verified, NEW.id_change_set - WHERE NOT (OLD.is_email_verified <=> NEW.is_email_verified) - UNION - -- Changed is_super_user - SELECT NEW.id_user, 'is_super_user', CONVERT(CONVERT(OLD.is_super_user, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_super_user, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.is_super_user <=> NEW.is_super_user) - UNION - -- Changed active - SELECT NEW.id_user, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User -BEFORE UPDATE ON Shop_User -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User(); - --- Shop User Role Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Role_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Role_Link -BEFORE INSERT ON Shop_User_Role_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Role_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Role_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Role_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Role_Link -BEFORE UPDATE ON Shop_User_Role_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Role_Link(); - --- Shop Address - -CREATE OR REPLACE FUNCTION before_insert_Shop_Address() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Address -BEFORE INSERT ON Shop_Address -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Address(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Address() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Address_Audit ( - id_address, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed region - SELECT NEW.id_address, 'id_region', OLD.id_region, NEW.id_region, NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - -- Changed name_full - SELECT NEW.id_address, 'name_full', OLD.name_full, NEW.name_full, NEW.id_change_set - WHERE NOT OLD.name_full <=> NEW.name_full - UNION - -- Changed phone_number - SELECT NEW.id_address, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed postcode - SELECT NEW.id_address, 'postcode', OLD.postcode, NEW.postcode, NEW.id_change_set - WHERE NOT OLD.postcode <=> NEW.postcode - UNION - -- Changed address_line_1 - SELECT NEW.id_address, 'address_line_1', OLD.address_line_1, NEW.address_line_1, NEW.id_change_set - WHERE NOT OLD.address_line_1 <=> NEW.address_line_1 - UNION - -- Changed address_line_2 - SELECT NEW.id_address, 'address_line_2', OLD.address_line_2, NEW.address_line_2, NEW.id_change_set - WHERE NOT OLD.address_line_2 <=> NEW.address_line_2 - UNION - -- Changed city - SELECT NEW.id_address, 'city', OLD.city, NEW.city, NEW.id_change_set - WHERE NOT OLD.city <=> NEW.city - UNION - -- Changed county - SELECT NEW.id_address, 'county', OLD.county, NEW.county, NEW.id_change_set - WHERE NOT OLD.county <=> NEW.county - UNION - -- Changed active - SELECT NEW.id_address, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Address -BEFORE UPDATE ON Shop_Address -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Address(); --- Shop Product Variation Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Basket() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Basket -BEFORE INSERT ON Shop_User_Basket -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Basket(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Basket() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.id_change_set_user <=> OLD.id_change_set_user THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Basket_Audit ( - id_item, - name_field, - value_prev, - value_new, - id_change_set_user - ) - -- Changed id_user - SELECT NEW.id_item, 'id_user', OLD.id_user, NEW.id_user, NEW.id_change_set_user - WHERE NOT OLD.id_user <=> NEW.id_user - UNION - -- Changed id_product - SELECT NEW.id_item, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set_user - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed quantity - SELECT NEW.id_item, 'quantity', CONVERT(OLD.quantity, CHAR), CONVERT(NEW.quantity, CHAR), NEW.id_change_set_user - WHERE NOT (OLD.quantity <=> NEW.quantity) - UNION - -- Changed active - SELECT NEW.id_item, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set_user - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Basket -BEFORE UPDATE ON Shop_User_Basket -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Basket(); - --- Shop User Order Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Order_Status() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Order_Status -BEFORE INSERT ON Shop_User_Order_Status -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Order_Status(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Order_Status() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Order_Status_Audit ( - id_Status, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_Status, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_Status, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_Status, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_Status, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_Status, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Order_Status -BEFORE UPDATE ON Shop_User_Order_Status -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Order_Status(); - --- Shop Supplier - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier -BEFORE INSERT ON Shop_Supplier -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Audit ( - id_supplier, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_company - SELECT NEW.id_supplier, 'name_company', OLD.name_company, NEW.name_company, NEW.id_change_set - WHERE NOT OLD.name_company <=> NEW.name_company - UNION - -- Changed name_contact - SELECT NEW.id_supplier, 'name_contact', OLD.name_contact, NEW.name_contact, NEW.id_change_set - WHERE NOT OLD.name_contact <=> NEW.name_contact - UNION - -- Changed department_contact - SELECT NEW.id_supplier, 'department_contact', OLD.department_contact, NEW.department_contact, NEW.id_change_set - WHERE NOT OLD.department_contact <=> NEW.department_contact - UNION - -- Changed id_address - SELECT NEW.id_supplier, 'id_address', OLD.id_address, NEW.id_address, NEW.id_change_set - WHERE NOT OLD.id_address <=> NEW.id_address - UNION - -- Changed phone_number - SELECT NEW.id_supplier, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed fax - SELECT NEW.id_supplier, 'fax', OLD.fax, NEW.fax, NEW.id_change_set - WHERE NOT OLD.fax <=> NEW.fax - UNION - -- Changed email - SELECT NEW.id_supplier, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT OLD.email <=> NEW.email - UNION - -- Changed website - SELECT NEW.id_supplier, 'website', OLD.website, NEW.website, NEW.id_change_set - WHERE NOT OLD.website <=> NEW.website - UNION - -- Changed id_currency - SELECT NEW.id_supplier, 'id_currency', OLD.id_currency, NEW.id_currency, NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier -BEFORE UPDATE ON Shop_Supplier -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier(); - --- Shop Unit of Measurement - -CREATE OR REPLACE FUNCTION before_insert_Shop_Unit_Measurement() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Unit_Measurement -BEFORE INSERT ON Shop_Unit_Measurement -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Unit_Measurement(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Unit_Measurement() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Unit_Measurement_Audit ( - id_unit_measurement, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_singular - SELECT NEW.id_unit_measurement, 'name_singular', OLD.name_singular, NEW.name_singular, NEW.id_change_set - WHERE NOT OLD.name_singular <=> NEW.name_singular - UNION - -- Changed name_plural - SELECT NEW.id_unit_measurement, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed symbol - SELECT NEW.id_unit_measurement, 'symbol', OLD.symbol, NEW.symbol, NEW.id_change_set - WHERE NOT OLD.symbol <=> NEW.symbol - UNION - -- Changed is_base_unit - SELECT NEW.id_unit_measurement, 'is_base_unit', OLD.is_base_unit, NEW.is_base_unit, NEW.id_change_set - WHERE NOT OLD.is_base_unit <=> NEW.is_base_unit - UNION - -- Changed active - SELECT NEW.id_unit_measurement, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Unit_Measurement -BEFORE UPDATE ON Shop_Unit_Measurement -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Unit_Measurement(); - --- Shop Unit of Measurement Conversion - -CREATE OR REPLACE FUNCTION before_insert_Shop_Unit_Measurement_Conversion() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Unit_Measurement_Conversion -BEFORE INSERT ON Shop_Unit_Measurement_Conversion -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Unit_Measurement_Conversion(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Unit_Measurement_Conversion() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Unit_Measurement_Conversion_Audit ( - id_conversion, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_unit_derived - SELECT NEW.id_conversion, 'id_unit_derived', OLD.id_unit_derived, NEW.id_unit_derived, NEW.id_change_set - WHERE NOT OLD.id_unit_derived <=> NEW.id_unit_derived - UNION - -- Changed id_unit_base - SELECT NEW.id_conversion, 'id_unit_base', OLD.id_unit_base, NEW.id_unit_base, NEW.id_change_set - WHERE NOT OLD.id_unit_base <=> NEW.id_unit_base - UNION - -- Changed power_unit_base - SELECT NEW.id_conversion, 'power_unit_base', OLD.power_unit_base, NEW.power_unit_base, NEW.id_change_set - WHERE NOT OLD.power_unit_base <=> NEW.power_unit_base - UNION - -- Changed multiplier_unit_base - SELECT NEW.id_conversion, 'multiplier_unit_base', OLD.multiplier_unit_base, NEW.multiplier_unit_base, NEW.id_change_set - WHERE NOT OLD.multiplier_unit_base <=> NEW.multiplier_unit_base - UNION - -- Changed increment_unit_base - SELECT NEW.id_conversion, 'active', OLD.increment_unit_base, NEW.increment_unit_base, NEW.id_change_set - WHERE NOT OLD.increment_unit_base <=> NEW.increment_unit_base - UNION - -- Changed active - SELECT NEW.id_conversion, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Unit_Measurement_Conversion -BEFORE UPDATE ON Shop_Unit_Measurement_Conversion -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Unit_Measurement_Conversion(); - --- Shop Supplier Purchase Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier_Purchase_Order -BEFORE INSERT ON Shop_Supplier_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier_Purchase_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Purchase_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_supplier_ordered - SELECT NEW.id_order, 'id_supplier_ordered', OLD.id_supplier_ordered, NEW.id_supplier_ordered, NEW.id_change_set - WHERE NOT OLD.id_supplier_ordered <=> NEW.id_supplier_ordered - UNION - -- Changed cost_total_local - SELECT NEW.id_order, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_order, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - /* - UNION - -- Changed latency_delivery - SELECT NEW.id_order, 'latency_delivery', OLD.latency_delivery, NEW.latency_delivery, NEW.id_change_set - WHERE NOT OLD.latency_delivery <=> NEW.latency_delivery - UNION - -- Changed quantity_ordered - SELECT NEW.id_order, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_order, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_received - SELECT NEW.id_order, 'quantity_received', OLD.quantity_received, NEW.quantity_received, NEW.id_change_set - WHERE NOT OLD.quantity_received <=> NEW.quantity_received - */ - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier_Purchase_Order -BEFORE UPDATE ON Shop_Supplier_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier_Purchase_Order(); - - --- Shop Supplier Purchase Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier_Purchase_Order_Product_Link -BEFORE INSERT ON Shop_Supplier_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier_Purchase_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed cost_total_local - SELECT NEW.id_link, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_link, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed quantity_ordered - SELECT NEW.id_link, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_received - SELECT NEW.id_link, 'quantity_received', OLD.quantity_received, NEW.quantity_received, NEW.id_change_set - WHERE NOT OLD.quantity_received <=> NEW.quantity_received - UNION - -- Changed latency_delivery_days - SELECT NEW.id_link, 'latency_delivery_days', OLD.latency_delivery_days, NEW.latency_delivery_days, NEW.id_change_set - WHERE NOT OLD.latency_delivery_days <=> NEW.latency_delivery_days - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier_Purchase_Order_Product_Link -BEFORE UPDATE ON Shop_Supplier_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier_Purchase_Order_Product_Link(); - --- Shop Manufacturing Purchase Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Manufacturing_Purchase_Order -BEFORE INSERT ON Shop_Manufacturing_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Manufacturing_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Manufacturing_Purchase_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed cost_total_local - SELECT NEW.id_order, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed value_produced_total_local - SELECT NEW.id_order, 'value_produced_total_local', OLD.value_produced_total_local, NEW.value_produced_total_local, NEW.id_change_set - WHERE NOT OLD.value_produced_total_local <=> NEW.value_produced_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_order, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed active - SELECT NEW.id_order, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Manufacturing_Purchase_Order -BEFORE UPDATE ON Shop_Manufacturing_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Manufacturing_Purchase_Order(); - --- Shop Manufacturing Purchase Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Manufacturing_Purch_Order_Product_Link -BEFORE INSERT ON Shop_Manufacturing_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Manufacturing_Purch_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed cost_total_local - SELECT NEW.id_link, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_link, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed quantity_used - SELECT NEW.id_link, 'quantity_used', OLD.quantity_used, NEW.quantity_used, NEW.id_change_set - WHERE NOT OLD.quantity_used <=> NEW.quantity_used - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_produced - SELECT NEW.id_link, 'quantity_produced', OLD.quantity_produced, NEW.quantity_produced, NEW.id_change_set - WHERE NOT OLD.quantity_produced <=> NEW.quantity_produced - UNION - -- Changed latency_manufacture - SELECT NEW.id_link, 'latency_manufacture', OLD.latency_manufacture, NEW.latency_manufacture, NEW.id_change_set - WHERE NOT OLD.latency_manufacture <=> NEW.latency_manufacture - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Manufacturing_Purch_Order_Product_Link -BEFORE UPDATE ON Shop_Manufacturing_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Manufacturing_Purch_Order_Product_Link(); - - --- Shop Customer - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer -BEFORE INSERT ON Shop_Customer -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Audit ( - id_customer, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_company - SELECT NEW.id_customer, 'name_company', OLD.name_company, NEW.name_company, NEW.id_change_set - WHERE NOT OLD.name_company <=> NEW.name_company - UNION - -- Changed name_contact - SELECT NEW.id_customer, 'name_contact', OLD.name_contact, NEW.name_contact, NEW.id_change_set - WHERE NOT OLD.name_contact <=> NEW.name_contact - UNION - -- Changed department_contact - SELECT NEW.id_customer, 'department_contact', OLD.department_contact, NEW.department_contact, NEW.id_change_set - WHERE NOT OLD.department_contact <=> NEW.department_contact - UNION - -- Changed id_address - SELECT NEW.id_customer, 'id_address', OLD.id_address, NEW.id_address, NEW.id_change_set - WHERE NOT OLD.id_address <=> NEW.id_address - UNION - -- Changed phone_number - SELECT NEW.id_customer, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed email - SELECT NEW.id_customer, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT OLD.email <=> NEW.email - UNION - -- Changed id_currency - SELECT NEW.id_customer, 'id_currency', OLD.id_currency, NEW.id_currency, NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - UNION - -- Changed active - SELECT NEW.id_customer, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer -BEFORE UPDATE ON Shop_Customer -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer(); - - --- Shop Customer Sales Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer_Sales_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer_Sales_Order -BEFORE INSERT ON Shop_Customer_Sales_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer_Sales_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer_Sales_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Sales_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_customer - SELECT NEW.id_order, 'id_customer', OLD.id_customer, NEW.id_customer, NEW.id_change_set - WHERE NOT OLD.id_customer <=> NEW.id_customer - UNION - -- Changed price_total_local - SELECT NEW.id_order, 'price_total_local', OLD.price_total_local, NEW.price_total_local, NEW.id_change_set - WHERE NOT OLD.price_total_local <=> NEW.price_total_local - UNION - -- Changed id_currency_price - SELECT NEW.id_order, 'id_currency_price', OLD.id_currency_price, NEW.id_currency_price, NEW.id_change_set - WHERE NOT OLD.id_currency_price <=> NEW.id_currency_price - UNION - -- Changed active - SELECT NEW.id_order, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer_Sales_Order -BEFORE UPDATE ON Shop_Customer_Sales_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer_Sales_Order(); - --- Shop Customer Sales Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer_Sales_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer_Sales_Order_Product_Link -BEFORE INSERT ON Shop_Customer_Sales_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer_Sales_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer_Sales_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Sales_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed price_total_local - SELECT NEW.id_link, 'price_total_local', OLD.price_total_local, NEW.price_total_local, NEW.id_change_set - WHERE NOT OLD.price_total_local <=> NEW.price_total_local - UNION - -- Changed id_currency_price - SELECT NEW.id_link, 'id_currency_price', OLD.id_currency_price, NEW.id_currency_price, NEW.id_change_set - WHERE NOT OLD.id_currency_price <=> NEW.id_currency_price - UNION - -- Changed quantity_ordered - SELECT NEW.id_link, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_delivered - SELECT NEW.id_link, 'quantity_delivered', OLD.quantity_delivered, NEW.quantity_delivered, NEW.id_change_set - WHERE NOT OLD.quantity_delivered <=> NEW.quantity_delivered - UNION - -- Changed latency_delivery_days - SELECT NEW.id_link, 'latency_delivery_days', OLD.latency_delivery_days, NEW.latency_delivery_days, NEW.id_change_set - WHERE NOT OLD.latency_delivery_days <=> NEW.latency_delivery_days - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer_Sales_Order_Product_Link -BEFORE UPDATE ON Shop_Customer_Sales_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer_Sales_Order_Product_Link(); - -/* - -CALL p_shop_calc_user ( - gen_random_uuid(), -- a_guid - '', -- a_id_user - 0, -- a_get_inactive_users - '1', -- a_ids_permission - '', -- a_ids_access_level - '1' -- a_ids_product -) - -*/ - -CREATE OR REPLACE PROCEDURE p_shop_calc_user ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_get_inactive_users BOOLEAN, - IN a_ids_permission INTEGER[], - IN a_ids_access_level INTEGER[], - IN a_ids_product INTEGER[] -- VARCHAR(4000) -- IN a_ids_permutation VARCHAR(4000) - /* - OUT result_errors TABLE ( - guid UUID, - id_type INTEGER, - code VARCHAR(50), - msg VARCHAR(4000) - ) - */ - -- INOUT a_error_msg TEXT -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_get_inactive_users BOOLEAN; - v_ids_permission INTEGER[]; - v_ids_access_level INTEGER[]; - v_ids_product INTEGER[]; -- TEXT; -- VARCHAR(4000); -- IN a_ids_permutation VARCHAR(4000) - v_has_filter_user BOOLEAN; - v_has_filter_permission BOOLEAN; - v_has_filter_access_level BOOLEAN; - -- v_has_filter_permutation BOOLEAN; - v_has_filter_product BOOLEAN; - v_id_permission_product INTEGER; - v_id_permission INTEGER; - -- v_ids_product UUID; - v_id_access_level_view INTEGER; - -- v_id_access_level_product_required INTEGER; - v_priority_access_level_view INTEGER; - v_priority_access_level_edit INTEGER; - v_priority_access_level_admin INTEGER; - v_id_access_level INTEGER; - v_priority_access_level INTEGER; - v_now TIMESTAMP; - v_ids_row_delete UUID; - v_code_error_data VARCHAR(200); - v_id_error_data INTEGER; - v_code_error_permission VARCHAR(200); - -- result_errors REFCURSOR; - -- v_error_msg TEXT := NULL; -BEGIN - -- Parse arguments + get default values - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := CASE WHEN a_id_user IS NULL THEN '' ELSE TRIM(a_id_user) END; - v_get_inactive_users := COALESCE(a_get_inactive_users, FALSE); - v_ids_permission := COALESCE(a_ids_permission, ARRAY[]::INTEGER[]); - v_ids_access_level := COALESCE(a_ids_access_level, ARRAY[]::INTEGER[]); - -- v_ids_permutation := CASE WHEN a_ids_permutation IS NULL THEN '' ELSE TRIM(a_ids_permutation) END; - v_ids_product := COALESCE(a_ids_product, ARRAY[]::INTEGER[]); - - v_id_error_data := 1; - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = v_id_error_data); - - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - - v_has_filter_user := (v_id_user <= 0); - v_has_filter_permission := (CARDINALITY(v_ids_permission) > 0); - v_has_filter_access_level := (CARDINALITY(v_ids_access_level) > 0); - /* - v_has_filter_permutation := CASE WHEN v_ids_permutation = '' THEN FALSE ELSE TRUE END; - */ - v_has_filter_product := (CARDINALITY(v_ids_product) = 0); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - v_priority_access_level_view := (SELECT priority FROM Shop_Access_Level WHERE id_access_level = v_id_access_level_view); - v_priority_access_level_edit := (SELECT priority FROM Shop_Access_Level WHERE code = 'EDIT' LIMIT 1); - v_priority_access_level_admin := (SELECT priority FROM Shop_Access_Level WHERE code = 'ADMIN' LIMIT 1); - - v_id_permission_product := (SELECT v_id_permission FROM Shop_Permission WHERE code = 'SHOP_PRODUCT' LIMIT 1); - - -- Clear previous proc results - -- DROP TABLE IF EXISTS tmp_User_Role_Link; - -- DROP TEMPORARY TABLE IF EXISTS tmp_User_Role_Link; - DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - - - -- Permanent Table - CREATE TABLE IF NOT EXISTS Shop_Calc_User_Temp ( - id_row INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_user INTEGER, - CONSTRAINT FK_Shop_Calc_User_Temp_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User (id_user), - id_permission_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_permission_required - FOREIGN KEY (id_permission_required) - REFERENCES Shop_Permission (id_permission), - /* - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level (id_access_level), - */ - priority_access_level_required INTEGER NOT NULL, - /* - CONSTRAINT FK_Shop_Calc_User_Temp_priority_access_level_required - FOREIGN KEY (priority_access_level_required) - REFERENCES Shop_Access_Level (priority), - */ - id_product INTEGER NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product (id_product), - /* - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES parts.Shop_Product_Permutation (id_permutation), - */ - is_super_user BOOLEAN NULL, - priority_access_level_user INTEGER NULL, - /* - CONSTRAINT FK_Shop_Calc_User_Temp_priority_access_level_minimum - FOREIGN KEY (priority_access_level_minimum) - REFERENCES Shop_Access_Level (priority) - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BOOLEAN, -- DEFAULT 0 - name_error VARCHAR(200) NULL - ); - - -- Temporary tables - CREATE TEMPORARY TABLE tmp_Shop_Product_p_shop_calc_user ( - id_row INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_product FOREIGN KEY (id_product) - REFERENCES Shop_Product (id_product), - /* - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES parts.Shop_Product_Permutation (id_permutation), - */ - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level (id_access_level), - guid UUID NOT NULL, - rank_product INTEGER NOT NULL - ); - - /* - CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Permission IDs - IF v_has_filter_permission THEN - -- CALL p_split(a_guid, v_ids_permission, ','); - - -- Invalid - IF EXISTS ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - ) THEN -- (SELECT PERM.id_permission FROM Split_Temp ST LEFT JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission WHERE ISNULL(PERM.id_permission)) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid permission IDs: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - LEFT JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission - WHERE ISNULL(PERM.id_permission) - ; - */ - RAISE EXCEPTION 'Invalid permission IDs: %', ( - SELECT STRING_AGG(id_permission, ', ') - FROM ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - ) Permission - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Inactive - IF EXISTS ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - WHERE active - ) THEN -- (SELECT PERM.id_permission FROM Split_Temp ST INNER JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission WHERE PERM.active = FALSE) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'The following permissions are not active: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - INNER JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission - WHERE PERM.active = FALSE - ; - */ - RAISE EXCEPTION 'Inactive permission IDs: %', ( - SELECT STRING_AGG(id_permission, ', ') - FROM ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - WHERE active - ) Permission - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Get the permission with the highest priority access level required - v_id_permission := ( - SELECT PERMS.id_permission - FROM ( - SELECT PERM2.id_permission - FROM Split_Temp ST - INNER JOIN Shop_Permission PERM2 ON ST.substring = PERM2.id_permission - WHERE PERM.active - UNION - SELECT v_id_permission_product - ) PERMS - INNER JOIN Shop_Permission PERM1 ON PERMS.id_permission = PERM1.id_permission - INNER JOIN Shop_Access_Level AL ON PERM1.id_access_level_required = AL.id_access_level - ORDER BY AL.priority ASC - LIMIT 1 - ); - - -- DROP TABLE Split_Temp; - ELSIF v_has_filter_product THEN - v_id_permission := v_id_permission_product; - ELSE - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Permission ID required' - ) - ; - */ - RAISE EXCEPTION 'Permission ID required.' - USING ERRCODE = '22000' - ; - END IF; - - -- access level - IF v_has_filter_access_level THEN - IF EXISTS ( - /* - SELECT ST.substring - FROM Split_Temp ST - LEFT JOIN Shop_Access_Level AL - ON ST.substring = AL.id_access_level - WHERE - ISNULL(AL.id_access_level) - OR AL.active = FALSE - */ - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid access level IDs: ' || STRING_AGG(ST.substring, ', ') - FROM Split_Temp ST - LEFT JOIN Shop_Access_Level AL ON ST.substring = AL.id_access_level - WHERE ISNULL(AL.id_access_level) - ; - */ - RAISE EXCEPTION 'Invalid access level IDs: %', ( - SELECT STRING_AGG(id_access_level, ', ') - FROM ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) AL - ) - USING ERRCODE = '22000' - ; - END IF; - - IF EXISTS ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - WHERE active - ) THEN - RAISE EXCEPTION 'Inactive access level IDs: %', ( - SELECT STRING_AGG(id_access_level, ', ') - FROM ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) AL - ) - USING ERRCODE = '22000' - ; - END IF; - - v_id_access_level := ( - SELECT AL.id_access_level - FROM Shop_Access_Level AL - WHERE - AL.active - AND AL.id_access_level = ANY(v_ids_access_level) - ORDER BY AL.priority ASC - LIMIT 1 - ); - ELSE - v_id_access_level := ( - SELECT id_access_level_required AS id_access_level - FROM ( - SELECT id_access_level - FROM Shop_Permission PERM - WHERE - PERM.id_permission = v_id_permission - UNION - SELECT v_id_access_level_view AS id_access_level - ) PERMS - INNER JOIN Shop_Access_Level AL ON PERMS.id_access_level = AL.id_access_level - ORDER BY AL.priority ASC - LIMIT 1 - ); -- v_id_access_level_view; - END IF; - - v_priority_access_level := (SELECT priority FROM Shop_Access_Level WHERE id_access_level = v_id_access_level); - - -- Invalid user ID - IF v_has_filter_user THEN - IF ISNULL((SELECT id_user FROM Shop_User WHERE id_user = v_id_user)) THEN -- NOT v_has_filter_user THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid user ID: ' || COALESCE(v_id_user, 'NULL') - ) - ; - */ - RAISE EXCEPTION 'Invalid user ID: %', v_id_user - USING ERRCODE = '22000' - ; - END IF; - - IF ISNULL((SELECT id_user FROM Shop_User WHERE id_user = v_id_user AND active)) THEN - RAISE EXCEPTION 'Inactive user ID: %', v_id_user - USING ERRCODE = '22000' - ; - END IF; - END IF; - - - -- Invalid products - IF v_has_filter_product THEN - -- Invalid product IDs - IF EXISTS ( - SELECT UNNEST(v_ids_product) AS id_product - EXCEPT - SELECT id_product FROM Shop_Product - ) THEN -- (SELECT * FROM Split_Temp ST LEFT JOIN Shop_Product P ON ST.substring = P.id_product WHERE ISNULL(P.id_product)) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid product IDs: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - LEFT JOIN Shop_Product P ON ST.substring = P.id_product - WHERE ISNULL(P.id_product) - ; - */ - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT STRING_AGG(id_product, ', ') - FROM ( - SELECT UNNEST(v_ids_product) AS id_product - EXCEPT - SELECT id_product FROM Shop_Product - ) Product - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Product_p_shop_calc_user ( - id_product, - -- id_permutation, - id_access_level_required, - guid, - rank_product -- rank_permutation - ) - SELECT - DISTINCT P.id_product, - -- PP.id_permutation, - P.id_access_level_required, - v_guid, - RANK() OVER (ORDER BY C.display_order, P.display_order) AS rank_product - FROM Shop_Product P -- ON ST.substring = P.id_product -- Shop_Product_Permutation PP - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - INNER JOIN Shop_Access_Level AL - ON P.id_access_level_required = AL.id_access_level - AND AL.active - WHERE -- FIND_IN_SET(P.id_product, v_ids_product) > 0 -- FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - P.id_product = ANY(v_ids_product) - -- AND P.active -- not worried as we want users to be able to see their order history - ; - /* - DELETE FROM tmp_Shop_Product_p_shop_calc_user - WHERE rank_permutation > 1 - ; - */ - -- v_has_filter_product := EXISTS (SELECT * FROM tmp_Shop_Product_p_shop_calc_user WHERE v_guid = guid); - END IF; - - -- User permissions - /* - IF v_has_filter_product THEN - INSERT INTO Shop_Calc_User_Temp ( - guid, - id_user, - id_permission_required, - id_product, - -- id_permutation, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - P.id_product, - -- t_P.id_permutation, - CASE WHEN v_priority_access_level <= AL_P.priority THEN v_priority_access_level ELSE AL_P.priority END AS priority_access_level_required, - AL_U.priority AS priority_access_level_user, - U.is_super_user, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_view AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_view, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_edit AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_edit, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_admin AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_admin - FROM Shop_User U - /* - ON U.id_user = v_id_user - AND U.active - */ - LEFT JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - LEFT JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL_U - ON RPL.id_access_leveL = AL_U.id_access_level - AND AL_U.active - INNER JOIN tmp_Shop_Product_p_shop_calc_user t_P - ON t_P.guid = v_guid - AND AL.id_access_level = t_P.id_access_leveL_required - INNER JOIN Shop_Access_Level AL_P - ON t_P.id_access_leveL_required = AL_P.id_access_level - AND AL_P.active - WHERE - v_guid = t_P.guid - AND U.active - AND U.id_user = v_id_user - ; - ELSE - INSERT INTO Shop_Calc_User_Temp (--UE_T - guid, - id_user, - id_permission_required, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - v_priority_access_level AS priority_access_level_required, - AL.priority AS priority_access_level_user, - U.is_super_user, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_view AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_view, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_edit AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_edit, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_admin AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_admin - FROM Shop_User U - INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL - ON RPL.id_access_level = AL.id_access_level - AND AL.active - WHERE - U.id_user = v_id_user - AND U.active - AND RPL.id_permission = v_id_permission - ORDER BY AL.priority ASC - ; - END IF; - */ - INSERT INTO Shop_Calc_User_Temp (--UE_T - guid, - id_user, - id_permission_required, - id_product, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin, - name_error - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - t_P.id_product, - MIN(v_priority_access_level, AL_P.priority) AS priority_access_level_required, - AL_U.priority AS priority_access_level_user, - U.is_super_user, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_view AND priority_access_level_user <= priority_access_level_required) AS can_view, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_edit AND priority_access_level_user <= priority_access_level_required) AS can_edit, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_admin AND priority_access_level_user <= priority_access_level_required) AS can_admin, - Permission.name || ' ' || (SELECT name FROM Shop_Access_Level WHERE priority = priority_access_level_required ORDER BY id_access_level ASC LIMIT 1) || ' permissions' || CASE WHEN ISNULL(t_P.id_product) THEN '' ELSE ' for product ' || P.name END AS name_error - FROM Shop_User U - INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL_U - ON RPL.id_access_level = AL_U.id_access_level - AND AL_U.active - INNER JOIN Shop_Permission Permission - ON RPL.id_permission = Permission.id_permission - AND Permission.active - CROSS JOIN tmp_Shop_Product_p_shop_calc_user t_P -- ON t_P.guid = v_guid - INNER JOIN Shop_Product P ON t_P.id_product = P.id_product - INNER JOIN Shop_Access_Level AL_P - ON t_P.id_access_level_required = AL_P.id_access_level - -- AND AL_P.active - WHERE - U.id_user = v_id_user - AND U.active - AND RPL.id_permission = v_id_permission - AND t_P.guid = v_guid - ORDER BY AL_P.priority ASC, t_P.rank_product ASC - ; - - -- IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE GUID = v_guid) THEN - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - WHERE GUID = v_guid - ; - -- RETURN NEXT result_errors; - -- result_errors - a_error_msg := ( - SELECT - -- GUID, id_type, code, - msg - FROM tmp_Msg_Error - WHERE GUID = v_guid - LIMIT 1 - ); - */ - - -- select * from tmp_Shop_Product_p_shop_calc_user; - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; - -- DROP TEMPORARY TABLE IF EXISTS tmp_User_Role_Link; - -- DROP TABLE IF EXISTS tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_calc_user ( - '56c9dfc1-e22f-11ee-aab4-b42e9986184a', -- v_guid - '', -- v_id_user -- 'auth0|6582b95c895d09a70ba10fef', - false, -- v_get_inactive_users - '4,5', -- v_ids_permission - '1', -- v_ids_access_level - -- null, -- v_ids_product - '1,2,3' -- v_ids_permutation -); - -SELECT * -FROM Shop_Calc_User_Temp -; - -DROP TABLE Shop_Calc_User_Temp; - -SELECT * -FROM Shop_Permission -; - -SELECT * -FROM Shop_Access_Level -; - -SELECT * -FROM Shop_Product -; - -SELECT * -FROM Shop_Product_Permutation -; - - -*/ - -/* -SELECT 'NOODS' AS guid, - U.id_user AS id_user, - P.id_permission AS id_permission_required, - AL.id_access_level AS id_access_level_required, - /* - v_id_permission, - v_id_access_level, - */ - AL.priority, -- MIN(AL.priority), - U.is_super_user - /* - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_view THEN TRUE ELSE FALSE END END, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_edit THEN TRUE ELSE FALSE END END, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_admin THEN TRUE ELSE FALSE END END - */ -FROM parts.Shop_User U -INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active -INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active -INNER JOIN Shop_Permission P - ON RPL.id_permission = P.id_permission - AND P.active -inner JOIN Shop_Access_Level AL - -- ON P.id_access_level_required = AL.id_access_level - ON RPL.id_access_level = AL.id_access_level - AND AL.active -WHERE U.id_user = 'auth0|6582b95c895d09a70ba10fef' - AND U.active - AND FIND_IN_SET(P.id_permission, '1,2') > 0 - -- AND v_id_access_level = AL.id_access_leveld --- GROUP BY U.id_user, P.id_permission, AL.id_access_level -- , is_super_user - -*/ - - - --- DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_supplier_purchase_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_order INTEGER, - IN a_id_supplier_ordered INTEGER, - IN a_id_currency_cost INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_supplier_ordered INTEGER; - v_id_currency_cost INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - v_guid_permission UUID; - -- v_id_user VARCHAR(100); - v_id_permission_supplier_purchase_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_supplier_purchase_order BOOLEAN; - -- result_orders REFCURSOR; - -- result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_supplier_ordered := a_id_supplier_ordered; - v_id_currency_cost := a_id_currency_cost; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission = 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - v_guid_permission = gen_random_uuid(); - -- v_id_user = CURRENT_USER; - v_id_permission_supplier_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER_PURCHASE_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - - v_is_new_supplier_purchase_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - RAISE EXCEPTION 'Invalid User ID: %', COALESCE(v_id_user, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Order ID - IF ((v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Supplier_Purchase_Order WHERE id_order = v_id_order)) THEN - RAISE EXCEPTION 'Invalid Supplier Purchase Order ID: %', COALESCE(v_id_order, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Supplier ID - IF ISNULL(v_id_supplier_ordered) OR NOT EXISTS (SELECT * FROM Shop_Supplier WHERE id_supplier = v_id_supplier_ordered) THEN - RAISE EXCEPTION 'Invalid Supplier ID: %', COALESCE(v_id_supplier_ordered, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Currency ID - IF ISNULL(v_id_currency_cost) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_cost) THEN - RAISE EXCEPTION 'Invalid currency ID: %', COALESCE(v_id_currency, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Comment - IF v_comment = '' THEN - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - -- Get data from Temp table - INSERT INTO tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - name_error - ) - SELECT - SPOPL_T.id_link, - SPOPL_T.id_order, - SPOPL_T.id_permutation, - PP.cost_local * quantity_ordered AS cost_total_local, - SPOPL_T.id_currency_cost, - SPOPL_T.quantity_ordered, - SPOPL_T.id_unit_quantity, - SPOPL_T.quantity_received, - SPOPL_T.latency_delivery_days, - SPOPL_T.display_order, - SPOPL_T.active, - CAST(PP.id_permutation AS VARCHAR(10)) || ' - ' || COALESCE(PP.name ,'') AS name_error - FROM Shop_Supplier_Purchase_Order_Product_Link_Temp SPOPL_T - INNER JOIN Shop_Product_Permutation PP ON SPOPL_T.id_permutation = PP.id_permutation - WHERE SPOPL_T.GUID = v_guid - ; - DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp SPOPL_T - WHERE SPOPL_T.GUID = v_guid - ; - - /* - UPDATE tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - - cost_total_local - */ - - -- Invalid quantity ordered - IF EXISTS ( - SELECT * - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_ordered) - AND quantity_ordered < 0 - ) THEN - RAISE EXCEPTION 'Invalid quantity ordered property for the following permutations: %', ( - SELECT STRING_AGG(t_SPOPL.name_error, ', ') - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - -- INNER JOIN Shop_Product_Permutation PP ON t_SPOPL.id_permutation = PP.id_permutation - WHERE t_SPOPL.quantity_ordered < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Duplicates - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - RAISE EXCEPTION 'Duplicate records: %', || ( - SELECT STRING_AGG(t_SPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_SPOPLC - ) - USING ERRCODE = '22000' - ; - END IF; - - - - -- Permissions - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPO - INNER JOIN Shop_Product_Permutation PP ON t_SPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_supplier_purchase_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier_purchase_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - RAISE EXCEPTION 'You do not have permission to edit the following product IDs: %', v_ids_product_no_permission - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - -- Transaction - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_supplier_purchase_order = TRUE THEN 'new ' ELSE '' END - || 'Supplier Purchase Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_supplier_purchase_order = 1) THEN - INSERT INTO Shop_Supplier_Purchase_Order ( - id_supplier_ordered, - cost_total_local, - id_currency_cost, - created_by, - id_change_set, - active - ) - SELECT - v_id_supplier_ordered, - SUM(t_SPOPL.cost_total_local), - v_id_currency_cost, - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Supplier_Purchase_Order ORDER BY id_order DESC LIMIT 1); - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ; - ELSE - UPDATE Shop_Supplier_Purchase_Order SPO - SET - SPO.id_supplier_ordered = v_id_supplier_ordered, - SPO.cost_total_local = SUM(t_SPOPL.cost_total_local), - SPO.id_currency = v_id_currency_cost, - SPO.id_change_set = v_id_change_set, - SPO.active = v_active - FROM Shop_Supplier_Purchase_Order SPO - INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL ON SPO.id_order = t_SPOPL.id_order - WHERE SPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL INNER JOIN Shop_Supplier_Purchase_Order_Product_Link SPOPL ON t_SPOPL.id_link = SPOPL.id_link) THEN - UPDATE Shop_Supplier_Purchase_Order_Product_Link SPOPL - SET - SPOPL.id_order = t_SPOPL.id_order, - SPOPL.id_permutation = t_SPOPL.id_permutation, - SPOPL.cost_total_local = t_SPOPL.cost_total_local, - SPOPL.id_currency_cost = t_SPOPL.id_currency_cost, - SPOPL.quantity_ordered = t_SPOPL.quantity_ordered, - SPOPL.id_unit_quantity = t_SPOPL.id_unit_quantity, - SPOPL.quantity_received = t_SPOPL.quantity_received, - SPOPL.latency_delivery_days = t_SPOPL.latency_delivery_days, - SPOPL.display_order = t_SPOPL.display_order, - SPOPL.active = t_SPOPL.active, - SPOPL.id_change_set = v_id_change_set - FROM Shop_Supplier_Purchase_Order_Product_Link SPOPL - INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ON SPOPL.id_link = t_SPOPL.id_link - ; - ELSE - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - WHERE t_SPOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error) THEN - ROLLBACK; - ELSE - COMMIT; - END IF; - */ - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - /* - -- Supplier Purchase Orders - OPEN result_orders FOR - SELECT * - FROM Shop_Supplier_Purchase_Order - WHERE id_order = v_id_order - ; - -- RETURN NEXT result_orders; - - -- Supplier Purchase Order Product Links - OPEN result_order_product_links FOR - SELECT * - FROM Shop_Supplier_Purchase_Order_Product_Link - WHERE id_order = v_id_order - ; - -- RETURN NEXT result_order_product_links; - */ - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Supplier_Purchase_Order; - DROP TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Supplier_Purchase_Order_Audit; -DELETE FROM Shop_Supplier_Purchase_Order; - -INSERT INTO Shop_Supplier_Purchase_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- cost_total_local, - 1, -- id_currency_cost, - 1, -- quantity_ordered, - 1, -- id_unit_quantity, - 1, -- quantity_received, - 14, -- latency_delivery_days , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; - -CALL p_shop_save_supplier_purchase_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - -1, -- a_id_order - 1, -- a_id_supplier_ordered - 1 -- a_id_currency_cost -); - -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; - -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Supplier_Purchase_Order_Audit; -DELETE FROM Shop_Supplier_Purchase_Order; - - -*/ - - - - - -CREATE OR REPLACE PROCEDURE p_shop_save_supplier ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_supplier INTEGER, - IN a_name_company VARCHAR(256), - IN a_name_contact VARCHAR(256), - IN a_department_contact VARCHAR(256), - IN a_id_address INTEGER, - IN a_phone_number VARCHAR(20), - IN a_fax VARCHAR(20), - IN a_email VARCHAR(515), - IN a_website VARCHAR(300), - IN a_id_currency INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_supplier INTEGER; - v_name_company VARCHAR(256); - v_name_contact VARCHAR(256); - v_department_contact VARCHAR(256); - v_id_address INTEGER; - v_phone_number VARCHAR(256); - v_fax VARCHAR(256); - v_email VARCHAR(256); - v_website VARCHAR(256); - v_id_currency INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_id_error_type_no_permission INTEGER; - v_guid_permission UUID; - v_id_permission_supplier INTEGER; - -- v_id_access_level_EDIT INTEGER; - v_has_permission BOOLEAN; - v_id_change_set INTEGER; - v_is_new_supplier BOOLEAN; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_supplier := COALESCE(a_id_supplier, -1); - v_name_company := TRIM(COALESCE(a_name_company, '')); - v_name_contact := TRIM(COALESCE(a_name_contact, '')); - v_department_contact := TRIM(COALESCE(a_department_contact, '')); - v_id_address := a_id_address; - v_phone_number := TRIM(COALESCE(a_phone_number, '')); - v_fax := TRIM(COALESCE(a_fax, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_website := TRIM(COALESCE(a_website, '')); - v_id_currency := a_id_currency; - v_active := COALESCE(a_active, FALSE); - - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA'); - v_guid_permission = gen_random_uuid(); - v_id_user = CURRENT_USER; - v_id_permission_supplier = (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER' LIMIT 1); - -- v_id_access_level_EDIT = (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_supplier := CASE WHEN v_id_supplier <= 0 THEN TRUE ELSE FALSE END; - - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - */ - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - - - -- Argument validation - IF v_name_company = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Supplier company name must be provided') - ; - */ - RAISE EXCEPTION 'Supplier company name must be provided' - USING ERRCODE = '22000' - ; - END IF; - - IF v_id_address IS NULL THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Address ID must be provided') - ; - */ - RAISE EXCEPTION 'Address ID must be provided' - USING ERRCODE = '22000' - ; - END IF; - - IF v_email = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Email must be provided') - ; - */ - RAISE EXCEPTION 'Email must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - IF v_comment = '' THEN - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - IF (v_is_new_supplier = FALSE AND NOT EXISTS (SELECT * FROM Shop_Supplier S WHERE S.id_supplier = v_id_supplier)) THEN - RAISE EXCEPTION 'Invalid supplier ID: %', v_id_supplier - USING ERRCODE = '22000' - ; - END IF; - - /* - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Supplier ( - id_supplier, name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active - ) - VALUES - (v_id_supplier, v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active) - /* - FROM Shop_Supplier S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - END IF; - */ - - -- Permissions - CALL p_shop_calc_user(v_guid_permission, v_id_user, v_id_permission_supplier, ''); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - v_has_permission := (SELECT can_edit FROM Shop_Calc_User_Temp WHERE GUID = v_guid_permission); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - 'You do not have %' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier - ; - */ - RAISE EXCEPTION 'No permission: %', ( - SELECT name_error - FROM Shop_Calc_User_Temp - WHERE GUID = v_guid_permission - ) - USING ERRCODE = '42501' - ; - END IF; - - -- CALL p_shop_clear_calc_user(v_guid_permission); - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - - -- Transaction - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_supplier = TRUE THEN 'new ' ELSE '' END - || 'Supplier - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - START TRANSACTION; - IF (v_is_new_supplier = TRUE) THEN - INSERT INTO Shop_Supplier ( - -- id_supplier, - name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active, id_change_set - ) - VALUES - ( - -- v_id_supplier, - v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active, v_id_change_set - ) - /* - FROM Shop_Supplier S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - ELSE - UPDATE Shop_Supplier S - -- INNER JOIN tmp_Shop_Supplier t_S ON S.id_supplier = t_S.id_supplier - SET - /* - S.name_company = t_S.name_company, - S.name_contact = t_S.name_contact, - S.department_contact = t_S.department_contact, - S.id_address = t_S.id_address, - S.phone_number = t_S.phone_number, - S.fax = t_S.fax, - S.email = t_S.email, - S.website = t_S.website, - S.id_currency = t_S.id_currency, - S.active = t_S.active - */ - S.name_company = v_name_company, - S.name_contact = v_name_contact, - S.department_contact = v_department_contact, - S.id_address = v_id_address, - S.phone_number = v_phone_number, - S.fax = v_fax, - S.email = v_email, - S.website = v_website, - S.id_currency = v_id_currency, - S.active = v_active, - S.id_change_set = v_id_change_set - ; - END IF; - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - DROP TABLE tmp_Shop_Supplier; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - - - - - --- DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_manufacturing_purchase_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_id_order INTEGER, - -- IN a_id_supplier_ordered INTEGER, - IN a_id_currency_cost INTEGER, - IN a_active BOOLEAN, - IN a_comment UUID -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_currency_cost INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - v_guid_permission UUID; - -- v_id_user VARCHAR(100); - v_id_permission_manufacturing_purchase_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_manufacturing_purchase_order BOOLEAN; - result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_currency_cost := a_id_currency_cost; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission = 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - v_guid_permission = gen_random_uuid(); - -- v_id_user = CURRENT_USER; - v_id_permission_manufacturing_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_MANUFACTURING_PURCHASE_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_manufacturing_purchase_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Manuf_Purch_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Manuf_Purch_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_manufacture INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - RAISE EXCEPTION 'Invalid User ID: %', COALESCE(v_id_user, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Order ID - IF ((v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Manufacturing_Purchase_Order WHERE id_order = v_id_order)) THEN - RAISE EXCEPTION 'Invalid Manufacturing Purchase Order ID: %', COALESCE(v_id_order, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - /* - -- Supplier ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_supplier_ordered) OR NOT EXISTS (SELECT * FROM Shop_Supplier WHERE id_supplier = v_id_supplier_ordered) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid supplier ID: ', COALESCE(v_id_supplier_ordered, 'NULL'))) - ; - END IF; - END IF; - */ - - -- Currency ID - IF ISNULL(v_id_currency_cost) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_cost) THEN - RAISE EXCEPTION 'Invalid currency ID: %', COALESCE(v_id_currency, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Comment - IF v_comment = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, 'A comment must be provided.') - ; - */ - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - -- Get data from Temp table - INSERT INTO tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - value_produced_total_local, - latency_manufacture, - display_order, - active, - name_error - ) - SELECT - MPOPL_T.id_link, - MPOPL_T.id_order, - MPOPL_T.id_permutation, - PP.cost_local * MPOPL_T.quantity_used AS cost_total_local, - MPOPL_T.id_currency_cost, - MPOPL_T.quantity_used, - MPOPL_T.id_unit_quantity, - MPOPL_T.quantity_produced, - (PP.cost_local + PP.profit_local_min) * MPOPL_T.quantity_produced AS value_produced_total_local, - MPOPL_T.latency_manufacture, - MPOPL_T.display_order, - MPOPL_T.active, - PP.id_permutation, ' - ' || COALESCE(P.name ,'') AS name_error - FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp MPOPL_T - INNER JOIN Shop_Product_Permutation PP ON MPOPL_T.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - WHERE MPOPL_T.GUID = v_guid - -- GROUP BY MPOPL_T.id_order, name_error, MPOPL_T.id_link - /* - group by - MPOPL_T.id_link, - MPOPL_T.id_order, - MPOPL_T.id_permutation, - cost_total_local, - MPOPL_T.id_currency_cost, - MPOPL_T.quantity_used, - MPOPL_T.id_unit_quantity, - MPOPL_T.quantity_produced, - value_produced_total_local, - MPOPL_T.latency_manufacture, - MPOPL_T.display_order, - MPOPL_T.active, - name_error - */ - -- GROUP BY id_link, P.id_product, PP.id_permutation - -- GROUP BY name_error, ID_LINK, cost_total_local, value_produced_total_local - ; - DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp MPOPL_T - WHERE MPOPL_T.GUID = v_guid - ; - - -- Invalid quantity used - IF EXISTS ( - SELECT * - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_used) - AND quantity_used < 0 - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity used property for the following permutations: ' || STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_used < 0 - ; - */ - RAISE EXCEPTION 'Invalid quantity used property for the following permutations: %', ( - SELECT STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_used < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Invalid quantity produced - IF EXISTS ( - SELECT * - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_produced) - AND quantity_produced < 0 - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity produced property for the following permutations: ' || STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_produced < 0 - ; - */ - RAISE EXCEPTION 'Invalid quantity produced property for the following permutations: %', ( - SELECT STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_produced < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Duplicates - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Duplicate records: ' || STRING_AGG(t_MPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_MPOPLC - ; - */ - RAISE EXCEPTION 'Duplicate records: %', ( - SELECT STRING_AGG(t_MPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_MPOPLC - ) - USING ERRCODE = '22000' - ; - END IF; - - - -- Permissions - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPO - INNER JOIN Shop_Product_Permutation PP ON t_MPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_manufacturing_purchase_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_manufacturing_purchase_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES ( - v_guid, - v_id_error_type_no_permission, - v_code_error_type_no_permission, - */ - RAISE EXCEPTION 'You do not have permission to edit the following product IDs: %', v_ids_product_no_permission - USING ERRCODE = '42501' - ; - END IF; - - -- Transaction - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_manufacturing_purchase_order = TRUE THEN 'new ' ELSE '' END - || 'Manufacturing Purchase Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_manufacturing_purchase_order = 1) THEN - INSERT INTO Shop_Manufacturing_Purchase_Order ( - -- id_supplier_ordered, - cost_total_local, - id_currency_cost, - value_produced_total_local, - created_by, - id_change_set, - active - ) - SELECT - -- v_id_supplier_ordered, - SUM(t_MPOPL.cost_total_local), - v_id_currency_cost, - SUM(t_MPOPL.value_produced_total_local), - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Manufacturing_Purchase_Order ORDER BY id_order DESC LIMIT 1); - - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ; - ELSE - UPDATE Shop_Manufacturing_Purchase_Order MPO - SET - -- MPO.id_supplier_ordered = v_id_supplier_ordered, - MPO.cost_total_local = SUM(t_MPOPL.cost_total_local), - MPO.value_produced_total_local = SUM(t_MPOPL.value_produced_total_local), - MPO.id_currency = v_id_currency_cost, - MPO.id_change_set = v_id_change_set, - MPO.active = v_active - FROM Shop_Manufacturing_Purchase_Order MPO - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL ON MPO.id_order = t_MPOPL.id_order - WHERE MPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL INNER JOIN Shop_Manufacturing_Purchase_Order_Product_Link MPOPL ON t_MPOPL.id_link = MPOPL.id_link) THEN - UPDATE Shop_Manufacturing_Purchase_Order_Product_Link MPOPL - SET - MPOPL.id_order = t_MPOPL.id_order, - MPOPL.id_permutation = t_MPOPL.id_permutation, - MPOPL.cost_total_local = t_MPOPL.cost_total_local, - MPOPL.value_produced_total_local = t_MPOPL.value_produced_total_local, - MPOPL.id_currency_cost = t_MPOPL.id_currency_cost, - MPOPL.quantity_used = t_MPOPL.quantity_used, - MPOPL.id_unit_quantity = t_MPOPL.id_unit_quantity, - MPOPL.quantity_produced = t_MPOPL.quantity_produced, - MPOPL.latency_manufacture = t_MPOPL.latency_manufacture, - MPOPL.display_order = t_MPOPL.display_order, - MPOPL.active = t_MPOPL.active, - MPOPL.id_change_set = v_id_change_set - FROM Shop_Manufacturing_Purchase_Order_Product_Link MPOPL - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ON MPOPL.id_link = t_MPOPL.id_link - ; - ELSE - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - /* - -- Manufacturing Purchase Orders - SELECT * - FROM Shop_Manufacturing_Purchase_Order - WHERE - id_order = v_id_order - -- GUID = v_guid - ; - - -- Manufacturing Purchase Order Product Links - SELECT * - FROM Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - id_order = v_id_order - -- GUID = v_guid - ; - */ - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Manufacturing_Purchase_Order_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order; - -INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- cost_total_local, - 1, -- id_currency_cost, - 1, -- quantity_used, - 1, -- id_unit_quantity, - 1, -- quantity_produced, - 14, -- latency_manufacture , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -CALL p_shop_save_manufacturing_purchase_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - -1, -- a_id_order - 1, -- a_id_currency_cost - 1, -- a_active - 'Initial data' -- a_comment -); - -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Manufacturing_Purchase_Order_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order; - - -*/ - - - - - -CREATE OR REPLACE PROCEDURE p_shop_save_customer ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_customer INTEGER, - IN a_name_company VARCHAR(256), - IN a_name_contact VARCHAR(256), - IN a_department_contact VARCHAR(256), - IN a_id_address INTEGER, - IN a_phone_number VARCHAR(20), - IN a_email VARCHAR(515), - IN a_id_currency INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_customer INTEGER; - v_name_company VARCHAR(256); - v_name_contact VARCHAR(256); - v_department_contact VARCHAR(256); - v_id_address INTEGER; - v_phone_number VARCHAR(256); - v_email VARCHAR(256); - v_id_currency INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_id_error_type_no_permission INTEGER; - v_guid_permission UUID; - v_id_permission_customer INTEGER; - v_id_access_level_EDIT INTEGER; - v_has_permission BOOLEAN; - v_id_change_set INTEGER; - v_is_new_customer BOOLEAN; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_customer := COALESCE(a_id_customer, -1); - v_name_company := TRIM(COALESCE(a_name_company, '')); - v_name_contact := TRIM(COALESCE(a_name_contact, '')); - v_department_contact := TRIM(COALESCE(a_department_contact, '')); - v_id_address := a_id_address; - v_phone_number := TRIM(COALESCE(a_phone_number, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_id_currency := a_id_currency; - v_active := COALESCE(a_active, FALSE); - - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA'); - v_guid_permission = gen_random_uuid(); - v_id_permission_customer = (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER' LIMIT 1); - v_id_access_level_EDIT = (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_customer := CASE WHEN v_id_customer <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - */ - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- Argument validation - IF v_name_company = '' THEN - RAISE EXCEPTION 'Customer company name must be provided' - USING ERRCODE = '22000' - ; - END IF; - IF v_id_address IS NULL THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Customer address ID must be provided') - ; - */ - RAISE EXCEPTION 'Customer address ID must be provided' - USING ERRCODE = '22000' - ; - END IF; - IF v_email = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Customer email must be provided') - ; - */ - RAISE EXCEPTION 'Customer email must be provided' - USING ERRCODE = '22000' - ; - END IF; - - - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - /* - IF (v_is_new_customer = FALSE AND NOT EXISTS (SELECT * FROM Shop_Customer C WHERE C.id_customer = v_id_customer)) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Invalid customer ID: ' || v_id_customer) - ; - END IF; - */ - RAISE EXCEPTION 'Invalid customer ID: %', v_id_customer - USING ERRCODE = '22000' - ; - END IF; - - /* - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Customer ( - id_customer, name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active - ) - VALUES - (v_id_customer, v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active) - /* - FROM Shop_Customer S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - END IF; - */ - - -- Permissions - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_customer, v_id_access_level_edit, ''); - - /* - UPDATE tmp_Shop_Customer t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - v_has_permission := (SELECT can_edit FROM Shop_Calc_User_Temp WHERE GUID = v_guid_permission); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - 'You do not have ' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - ; - RAISE EXCEPTION 'You do not have ' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - USING ERRCODE = '22000' - ; - */ - END IF; - - -- CALL p_shop_clear_calc_user(v_guid_permission); - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - - -- Transaction - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_customer = TRUE THEN 'new ' ELSE '' END - || 'Customer - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - START TRANSACTION; - IF (v_is_new_customer = TRUE) THEN - INSERT INTO Shop_Customer ( - -- id_customer, - name_company, name_contact, department_contact, id_address, phone_number, email, id_currency, active, id_change_set - ) - VALUES - ( - -- v_id_customer, - v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_email, v_id_currency, v_active, v_id_change_set - ) - /* - FROM Shop_Customer S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - ELSE - UPDATE Shop_Customer C - -- INNER JOIN tmp_Shop_Customer t_S ON S.id_customer = t_S.id_customer - SET - /* - S.name_company = t_S.name_company, - S.name_contact = t_S.name_contact, - S.department_contact = t_S.department_contact, - S.id_address = t_S.id_address, - S.phone_number = t_S.phone_number, - S.fax = t_S.fax, - S.email = t_S.email, - S.website = t_S.website, - S.id_currency = t_S.id_currency, - S.active = t_S.active - */ - C.name_company = v_name_company, - C.name_contact = v_name_contact, - C.department_contact = v_department_contact, - C.id_address = v_id_address, - C.phone_number = v_phone_number, - C.email = v_email, - C.id_currency = v_id_currency, - C.active = v_active, - C.id_change_set = v_id_change_set - ; - END IF; - - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Customer; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_save_customer ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 'Initial Customer', -- a_comment - '-1', -- a_id_customer - 'good co', -- a_name_company - 'teddy', -- a_name_contact - 'manufacturing', -- a_department_contact - 1, -- a_id_address - 'BRING BRING', -- a_phone_number - 'e@mail.com', -- a_email - 1, -- a_id_currency_cost - 1 -- a_active -); - -SELECT * FROM Shop_Customer -; - -DELETE FROM Shop_Customer -; - -*/ - --- DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_customer_sales_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment VARCHAR(4000), - IN a_id_order INTEGER, - IN a_id_customer INTEGER, - IN a_id_currency_price INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_customer INTEGER; - v_id_currency_price INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - -- v_guid_permission UUID; - v_id_permission_Customer_Sales_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_Customer_Sales_order BOOLEAN; - result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_customer := a_id_customer; - v_id_currency_price := a_id_currency_price; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data := 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission := 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - -- v_guid_permission := gen_random_uuid(); - v_id_permission_Customer_Sales_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER_SALES_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - - v_is_new_Customer_Sales_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_Sales_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid User ID: ', COALESCE(v_id_user, 'NULL'))) - ; - END IF; - END IF; - - -- Order ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF (v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Customer_Sales_Order WHERE id_order = v_id_order) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid Customer Sales Order ID: ', COALESCE(v_id_order, 'NULL'))) - ; - END IF; - END IF; - - -- Customer ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_customer) OR NOT EXISTS (SELECT * FROM Shop_Customer WHERE id_customer = v_id_customer) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid Customer ID: ', COALESCE(v_id_customer, 'NULL'))) - ; - END IF; - END IF; - - -- Currency ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_currency_price) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_price) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid currency ID: ', COALESCE(v_id_currency, 'NULL'))) - ; - END IF; - END IF; - - -- Comment - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF v_comment = '' THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, 'A comment must be provided.') - ; - END IF; - END IF; - - - -- Get data from Temp table - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link, - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - name_error - ) - SELECT - CSOPL_T.id_link, - CSOPL_T.id_order, - CSOPL_T.id_permutation, - (PP.cost_local + PP.profit_local_min) * quantity_ordered AS price_total_local, - CSOPL_T.id_currency_price, - CSOPL_T.quantity_ordered, - CSOPL_T.id_unit_quantity, - CSOPL_T.quantity_delivered, - CSOPL_T.latency_delivery_days, - CSOPL_T.display_order, - CSOPL_T.active, - PP.id_permutation || ' - ' || COALESCE(P.name ,'') AS name_error - FROM Shop_Customer_Sales_Order_Product_Link_Temp CSOPL_T - INNER JOIN Shop_Product_Permutation PP ON CSOPL_T.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - WHERE CSOPL_T.GUID = v_guid - ; - DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp CSOPL_T - WHERE CSOPL_T.GUID = v_guid - ; - - /* - UPDATE tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - SET - price_total_local - */ - END IF; - - -- Invalid quantity ordered - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Customer_Sales_Order_Product_Link - WHERE - NOT ISNULL(quantity_ordered) - AND quantity_ordered < 0 - ) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity ordered property for the following permutations: ' || STRING_AGG(t_CSOPL.name_error, ', ') - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - -- INNER JOIN Shop_Product_Permutation PP ON t_CSOPL.id_permutation = PP.id_permutation - WHERE t_CSOPL.quantity_ordered < 0 - ; - END IF; - END IF; - - -- Duplicates - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Duplicate records: ' || STRING_AGG(t_CSOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_CSOPLC - ; - END IF; - END IF; - - - - -- Permissions - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_SPO - INNER JOIN Shop_Product_Permutation PP ON t_SPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_Customer_Sales_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - SET v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_Customer_Sales_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES ( - v_guid, - v_id_error_type_no_permission, - v_code_error_type_no_permission, - 'You do not have permission to edit the following product IDs: ' || v_ids_product_no_permission - ) - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - END IF; - - -- Transaction - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_Customer_Sales_order = TRUE THEN 'new ' ELSE '' END - || 'Customer Sales Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_Customer_Sales_order = 1) THEN - INSERT INTO Shop_Customer_Sales_Order ( - id_customer, - price_total_local, - id_currency_price, - created_by, - id_change_set, - active - ) - SELECT - v_id_customer, - SUM(t_CSOPL.price_total_local), - v_id_currency_price, - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Customer_Sales_Order ORDER BY id_order DESC LIMIT 1); - INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ; - ELSE - UPDATE Shop_Customer_Sales_Order CSO - SET - CSO.id_customer = v_id_customer, - CSO.price_total_local = SUM(t_CSOPL.price_total_local), - CSO.id_currency = v_id_currency_price, - CSO.id_change_set = v_id_change_set, - CSO.active = v_active - FROM Shop_Customer_Sales_Order CSO - INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL ON CSO.id_order = t_CSOPL.id_order - WHERE SPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL INNER JOIN Shop_Customer_Sales_Order_Product_Link CSOPL ON t_CSOPL.id_link = CSOPL.id_link) THEN - UPDATE Shop_Customer_Sales_Order_Product_Link CSOPL - SET - CSOPL.id_order = t_CSOPL.id_order, - CSOPL.id_permutation = t_CSOPL.id_permutation, - CSOPL.price_total_local = t_CSOPL.price_total_local, - CSOPL.id_currency_price = t_CSOPL.id_currency_price, - CSOPL.quantity_ordered = t_CSOPL.quantity_ordered, - CSOPL.id_unit_quantity = t_CSOPL.id_unit_quantity, - CSOPL.quantity_delivered = t_CSOPL.quantity_delivered, - CSOPL.latency_delivery_days = t_CSOPL.latency_delivery_days, - CSOPL.display_order = t_CSOPL.display_order, - CSOPL.active = t_CSOPL.active, - CSOPL.id_change_set = v_id_change_set - FROM Shop_Customer_Sales_Order_Product_Link CSOPL - INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ON CSOPL.id_link = t_CSOPL.id_link - ; - ELSE - INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - WHERE t_CSOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error) THEN - ROLLBACK; - ELSE - COMMIT; - END IF; - */ - END IF; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - /* - -- Supplier Purchase Orders - SELECT * - FROM Shop_Customer_Sales_Order - WHERE id_order = v_id_order - ; - - -- Supplier Purchase Order Product Links - SELECT * - FROM Shop_Customer_Sales_Order_Product_Link - WHERE id_order = v_id_order - ; - */ - - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - - -- DROP TABLE tmp_Shop_Customer_Sales_Order; - DROP TABLE tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Audit; -DELETE FROM Shop_Customer_Sales_Order_Product_Link; -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp; -DELETE FROM Shop_Customer_Sales_Order_Audit; -DELETE FROM Shop_Customer_Sales_Order; - -INSERT INTO Shop_Customer_Sales_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- price_total_local, - 1, -- id_currency_price, - 1, -- quantity_ordered, - 1, -- id_unit_quantity, - 1, -- quantity_delivered, - 14, -- latency_delivery_days , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Temp; - -CALL p_shop_save_customer_sales_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 'Initial customer', -- a_comment - -1, -- a_id_order - 4, -- a_id_customer - 1, -- a_id_currency_price - 1 -- a_active -); - -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Temp; - -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Audit; -DELETE FROM Shop_Customer_Sales_Order_Product_Link; -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp; -DELETE FROM Shop_Customer_Sales_Order_Audit; -DELETE FROM Shop_Customer_Sales_Order; - - -*/ - - - - -/* - -CALL p_shop_save_user ( - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - '', -- a_name - '', -- a_email - 0 -- a_email_verified -) - -*/ - - -CREATE OR REPLACE PROCEDURE p_shop_save_user ( - IN a_id_user INTEGER, - IN a_name VARCHAR(255), - IN a_email VARCHAR(254), - IN a_email_verified BIT -) -AS $$ -DECLARE - v_id_user INTEGER; - v_name VARCHAR(255); - v_email VARCHAR(254); - v_email_verified BIT; - v_has_filter_user BOOLEAN; - result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_name := TRIM(COALESCE(a_name, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_email_verified := COALESCE(a_email_verified, FALSE); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TABLE tmp_Shop_User ( - id_user INTEGER, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - -- code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - msg VARCHAR(4000) NOT NULL - ); - - - -- Parse filters - - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - IF NOT EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1) THEN - INSERT INTO Shop_User ( - id_user, - name, - email, - email_verified - ) - VALUES ( - v_id_user, - v_name, - v_email, - v_email_verified - ); - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - END IF; - - v_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - ELSE - INSERT INTO tmp_Msg_Error ( - id_type, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - 'No user ID provided.' - ) - ; - END IF; - - - /* - IF NOT EXISTS (SELECT msg FROM tmp_Msg_Error LIMIT 1) THEN - END IF; - */ - - - -- Returns - /* - -- User - SELECT * - FROM tmp_Shop_User - ; - */ - - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - - /* - -- Return arguments for test - SELECT a_id_user, - a_name, - a_email, - a_email_verified - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_save_user ( - '', - '', - '', - 0 -) - -*/ - - - -/* - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '', -- a_ids_permutation_basket - '', -- a_quantities_permutation_basket - 1, -- a_id_permutation_edit - NULL, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -) - -* - - -CREATE OR REPLACE PROCEDURE p_shop_edit_user_basket ( - IN a_id_user INTEGER, - IN a_ids_permutation_basket VARCHAR(4000), - IN a_quantities_permutation_basket VARCHAR(4000), - IN a_id_permutation_edit INTEGER, - IN a_quantity_permutation_edit INTEGER, - IN a_sum_not_edit BOOLEAN, - IN a_id_currency INTEGER, - IN a_id_region_purchase INT -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_ids_permutation_basket BOOLEAN; - v_quantities_permutation_basket VARCHAR -- REMAKE WITH TEMP TABLE -BEGIN - -- Argument redeclaration - -- Variable declaration - DECLARE v_has_filter_user BOOLEAN; - DECLARE v_has_filter_permutation_basket BOOLEAN; - DECLARE v_has_filter_permutation_edit BOOLEAN; - DECLARE v_has_filter_region BOOLEAN; - DECLARE v_has_filter_currency BOOLEAN; - DECLARE v_n_id_permutation_basket INTEGER; - DECLARE v_n_quantity_permutation_basket INTEGER; - DECLARE v_row_number INTEGER; - DECLARE v_guid UUID; - -- DECLARE v_id_user VARCHAR(100); - DECLARE v_id_permission_product INTEGER; - DECLARE v_ids_permutation_permission VARCHAR(4000); - DECLARE v_now TIMESTAMP; - -- DECLARE v_quantity_new INTEGER; - DECLARE v_change_set_used BOOLEAN; - DECLARE v_id_change_set INTEGER; - - SET v_guid = gen_random_uuid(); - - -- Argument validation + default values - IF a_id_user IS NULL THEN - SET a_id_user = ''; - ELSE - SET a_id_user = TRIM(a_id_user); - END IF; - IF a_ids_permutation_basket IS NULL THEN - SET a_ids_permutation_basket = ''; - ELSE - SET a_ids_permutation_basket = TRIM(a_ids_permutation_basket); - END IF; - IF a_quantities_permutation_basket IS NULL THEN - SET a_quantities_permutation_basket = ''; - ELSE - SET a_quantities_permutation_basket = TRIM(a_quantities_permutation_basket); - END IF; - IF a_sum_not_edit IS NULL THEN - SET a_sum_not_edit = TRUE; - END IF; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_Basket; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Quantity; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TABLE tmp_Shop_User ( - id_user INTEGER, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Shop_Product ( - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - display_order INTEGER NOT NULL, - active INTEGER NOT NULL DEFAULT 1 - ); - - CREATE TEMPORARY TABLE tmp_Shop_Quantity( - quantity INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active INTEGER NOT NULL DEFAULT 1 - ); - - CREATE TABLE tmp_Shop_Basket ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - id_region_purchase INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_region_purchase - FOREIGN KEY (id_region_purchase) - REFERENCES Shop_Region(id_region), - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - quantity INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE - /* - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL - */ - ); - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - -- code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - msg VARCHAR(4000) NOT NULL - ); - - - -- Parse filters - SET v_has_filter_user = NOT (a_id_user = ''); - SET v_has_filter_permutation_basket = NOT (a_ids_permutation_basket = ''); - SET v_has_filter_permutation_edit = NOT ISNULL(a_id_permutation_edit); - SET v_has_filter_currency = NOT ISNULL(a_id_currency); - SET v_has_filter_region = NOT ISNULL(a_id_region_purchase); - -- SET v_quantity_new = CASE WHEN a_sum_not_edit THEN quantity + a_quantity_product_edit ELSE a_quantity_product_edit END; - /* - SELECT v_has_filter_user, v_has_filter_basket - ; - - */ - - -- Currency - IF NOT v_has_filter_currency THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Currency ID not provided.' - ) - ; - END IF; - IF v_has_filter_currency AND NOT EXISTS ( SELECT * FROM Shop_Currency WHERE id_currency = a_id_currency) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Currency ID not found: ', a_id_currency, '.') - ) - ; - END IF; - - -- Region - IF NOT v_has_filter_region THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Region ID not provided.' - ) - ; - END IF; - IF v_has_filter_region AND NOT EXISTS ( SELECT * FROM Shop_Region WHERE id_region = a_id_region_purchase) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Region ID not found: ', a_id_region_purchase, '.') - ) - ; - END IF; - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user LIKE CONCAT('%', a_id_user, '%') - AND active - LIMIT 1 - ; - - IF NOT EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1) THEN - SET v_has_filter_user = FALSE; - - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('User ID not found: ', a_id_user, '.') - ) - ; - END IF; - - SET a_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - END IF; - - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - SET v_change_set_used = FALSE; - INSERT INTO Shop_User_Change_Set ( - comment - ) - VALUES ( - 'edit basket' - ); - SET v_id_change_set := (SELECT id_change_set FROM Shop_User_Change_Set ORDER BY id_change_set DESC LIMIT 1); - END IF; - - -- Get basket - -- User - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - /* - display_order_category, - display_order_product - */ - ) - SELECT - C.id_category, - UB.id_product, - UB.id_permutation, - UB.id_region_purchase, - UB.id_currency, - UB.quantity, - UB.active - /* - C.display_order, - P.display_order - */ - FROM Shop_User_Basket UB - /* - INNER JOIN tmp_Shop_User t_U - ON UB.id_user = t_U.id_user - */ - INNER JOIN Shop_Product_Permutation PP - ON UB.id_product = PP.id_product - AND PP.active - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - AND C.active - WHERE UB.id_user = a_id_user - ; - END IF; - - -- Currency - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) - AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active AND id_currency != a_id_currency) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT( - 'Currency ID does not match currency of other items in basket. Basket currency: ', - (SELECT code FROM Shop_Currency WHERE id_currency = ( - SELECT - id_currency - FROM tmp_Shop_Basket - WHERE active - AND id_currency != a_id_currency - LIMIT 1 - )), - ', new currency: ', - (SELECT code FROM Shop_Currency WHERE id_currency = a_id_currency), - '.' - ) - ) - ; - END IF; - END IF; - - -- Region - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) - AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket - WHERE - active - AND id_region_purchase != a_id_region_purchase - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Purchase region ID does not match region of other items in basket. Basket currency: ', - (SELECT code FROM Shop_Region WHERE id_region = ( - SELECT - id_region_purchase - FROM tmp_Shop_Basket - WHERE active - AND id_region != a_id_region_purchase - LIMIT 1 - )), - ', new currency: ', - (SELECT code FROM Shop_Region WHERE id_region = a_id_region_purchase), - '.' - ) - ) - ; - END IF; - END IF; - - -- String product id, permutation id, quantity list - IF NOT EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN -- NOT v_has_filter_user AND - -- Get product ids - CALL p_split(a_guid, a_ids_permutation_basket, ','); - INSERT INTO tmp_Shop_Product ( - id_product, id_permutation, display_order - ) - SELECT PP.id_product, ST.substring, ST.display_order - FROM Split_Temp ST - INNER JOIN Shop_Product_Permutation PP - ON ST.substring = PP.id_permutation - -- AND PP.active - ; - /* - SELECT substring as id_product, display_order - FROM Split_Temp - ; - */ - DROP TABLE Split_Temp; - - -- Get product quantities - CALL p_split(a_guid, a_quantities_permutation_basket, ','); - INSERT INTO tmp_Shop_Quantity ( - quantity, display_order - ) - SELECT substring, display_order - FROM Split_Temp - ; - /* - SELECT substring AS quantity_product, display_order - FROM Split_Temp - ; - */ - DROP TABLE Split_Temp; - - -- Compare number of product ids to number of quantities - SET v_n_id_permutation_basket := (SELECT display_order FROM tmp_Shop_Product ORDER BY display_order DESC LIMIT 1); - SET v_n_quantity_permutation_basket := (SELECT display_order FROM tmp_Shop_Quantity ORDER BY display_order DESC LIMIT 1); - IF NOT v_n_id_permutation_basket = v_n_quantity_permutation_basket THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Number of permutations (', v_n_id_permutation_basket, ') does not equal number of quantities (', v_n_quantity_permutation_basket, ') for basket.') - ) - ; - ELSE - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity - ) - SELECT - C.id_category, - P.id_product, - t_P.id_permutation, - a_id_region_purchase, - a_id_currency, - t_Q.quantity - FROM tmp_Shop_Product t_P - INNER JOIN tmp_Shop_Quantity t_Q - ON t_P.display_order = t_Q.display_order - INNER JOIN Shop_Product_Permutation PP - ON t_P.id_permutation = PP.id_permutation - AND PP.active - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - AND C.active - -- RIGHT JOIN tmp_Shop_Basket t_UB ON ISNULL(t_UB.id_product) - -- WHERE t_P.id_product NOT IN (SELECT id_product FROM tmp_Shop_Basket) - ; - - /* - IF EXISTS( - SELECT * - FROM Shop_Product P - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - INNER JOIN tmp_Shop_Basket t_B - ON P.id_product = t_B.id_product - WHERE C.active = FALSE OR P.active = FALSE LIMIT 1 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('No valid product IDs in list: ', a_ids_permutation_basket, '.') - ) - ; - END IF; - */ - END IF; - END IF; - - /* - select v_has_filter_edit; - select * from tmp_Shop_Basket; - select * from tmp_Msg_Error; - */ - - - -- Edit basket product - IF v_has_filter_permutation_edit AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - ( - C.active = FALSE - OR P.active = FALSE - OR PP.active = FALSE - ) - AND PP.id_permutation = a_id_permutation_edit - LIMIT 1 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Invalid product ID to edit: ', a_id_product_edit, '.') - ) - ; - END IF; - END IF; - IF v_has_filter_permutation_edit AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket - WHERE - id_permutation = a_id_permutation_edit - ) THEN - UPDATE tmp_Shop_Basket - SET quantity = CASE WHEN a_sum_not_edit = TRUE THEN COALESCE(quantity, 0) + a_quantity_permutation_edit ELSE a_quantity_permutation_edit END, - active = CASE WHEN CASE WHEN a_sum_not_edit = TRUE THEN COALESCE(quantity, 0) + a_quantity_permutation_edit ELSE a_quantity_permutation_edit END = FALSE THEN FALSE ELSE TRUE END - WHERE id_permutation = a_id_permutation_edit - ; - - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket t_B - WHERE t_B.quantity < 0 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Invalid basket quantity.' - ) - ; - END IF; - - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - SET v_change_set_used = TRUE; - - UPDATE Shop_User_Basket UB - INNER JOIN tmp_Shop_Basket t_UB - ON UB.id_permutation = a_id_permutation_edit - SET UB.quantity = t_UB.quantity, - UB.active = t_UB.active, - UB.id_change_set_user = v_id_change_set - WHERE UB.id_permutation = a_id_permutation_edit - AND id_user = a_id_user - ; - END IF; - ELSE - IF a_quantity_permutation_edit < 0 THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Invalid basket quantity.' - ) - ; - ELSE - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - ) - SELECT - P.id_category, - P.id_product, - PP.id_permutation, - a_id_region_purchase, - a_id_currency, - a_quantity_permutation_edit, - CASE WHEN a_quantity_permutation_edit > 0 THEN TRUE ELSE FALSE END - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - WHERE id_permutation = a_id_permutation_edit - ; - IF v_has_filter_user THEN - IF EXISTS ( - SELECT * - FROM Shop_User_Basket UB - WHERE - UB.id_permutation = a_id_permutation_edit - ) THEN - SET v_change_set_used = TRUE; - - UPDATE Shop_User_Basket - INNER JOIN tmp_Shop_Basket t_UB ON UB.id_permutation = t_UB.id_permutation - SET UB.quantity = t_UB.quantity, - UB.active = t_UB.active, - UB.id_change_set_user = v_id_change_set - WHERE UB.id_permutation = a_id_permutation_edit - AND id_user = a_id_user - ; - ELSE - INSERT INTO Shop_User_Basket ( - id_user, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - ) - SELECT a_id_user, - t_UB.id_product, - t_UB.id_permutation, - t_UB.id_region_purchase, - t_UB.id_currency, - t_UB.quantity, - t_UB.active - FROM tmp_Shop_Basket t_UB - WHERE id_permutation = a_id_permutation_edit - ; - END IF; - END IF; - END IF; - END IF; - END IF; - - - -- Checks - /* - SELECT * FROM tmp_Shop_Basket; - SELECT - STRING_AGG(t_UB.id_product, ',') AS basket_product_ids - FROM tmp_Shop_Basket t_UB - -- WHERE ISNULL(t_UB.id_permutation) - ; - SELECT - STRING_AGG(t_UB.id_permutation, ',') AS basket_permutation_ids - FROM tmp_Shop_Basket t_UB - WHERE NOT ISNULL(t_UB.id_permutation) - ; - */ - -- Returns - CALL p_shop_get_many_product ( - a_id_user, -- a_id_user - 1, -- a_get_all_categories - '', -- a_ids_category - 0, -- a_get_inactive_categories - 0, -- a_get_all_products - ( - SELECT - STRING_AGG(t_B.id_product, ',') - FROM tmp_Shop_Basket t_B - WHERE active = TRUE - ), -- a_ids_product - 0, -- a_get_inactive_products - 0, -- a_get_first_product_only - 0, -- a_get_all_product_permutations - ( - SELECT - STRING_AGG(t_B.id_permutation, ',') - FROM tmp_Shop_Basket t_B - WHERE NOT ISNULL(t_B.id_permutation) - AND active = TRUE - ), -- a_ids_permutation - 0, -- a_get_inactive_permutations - 0, -- a_get_all_images - '', -- a_ids_image - 0, -- a_get_inactive_images - 1, -- a_get_first_image_only - 0, -- a_get_all_delivery_region - a_id_region_purchase, -- a_ids_delivery_region - 0, -- a_get_inactive_delivery_region - 0, -- a_get_all_currency - a_id_currency, -- a_ids_currency - 0, -- a_get_inactive_currency - 1, -- a_get_all_discount - '', -- a_ids_discount - 0 -- a_get_inactive_discount - ); - - -- Basket - SELECT t_UB.id_category, - t_UB.id_product, - t_UB.id_permutation, - P.name, - PCL.price_local_VAT_incl, - PCL.price_local_VAT_excl, - PCL.id_currency, - t_UB.quantity - FROM tmp_Shop_Basket t_UB - INNER JOIN Shop_Product_Permutation PP - ON t_UB.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - INNER JOIN Shop_Product_Currency_Link PCL - ON PP.id_permutation = PCL.id_permutation - AND PCL.id_region_purchase = a_id_region_purchase - AND PCL.id_currency = a_id_currency - WHERE t_UB.active = TRUE - ORDER BY C.display_order, P.display_order - ; - - -- Errors - /* Completed by product get many */ - SELECT - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE GUID = v_guid - ; - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_categories, - a_ids_product, - a_get_inactive_products, - a_get_first_product_only, - a_get_all_products, - a_ids_image, - a_get_inactive_images, - a_get_first_image_only, - a_get_all_images - ; - */ - - -- Clean up - IF NOT v_change_set_used THEN - DELETE FROM Shop_User_Change_Set - WHERE id_change_set = v_id_change_set - ; - END IF; - - -- DROP TABLE IF EXISTS tmp_Msg_Error; - DELETE FROM tmp_Msg_Error WHERE guid = v_guid; - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - DROP TABLE tmp_Msg_Error; - END IF; - DROP TABLE IF EXISTS tmp_Shop_Basket; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Quantity; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - -*/ - - -/* - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '', -- a_ids_permutation_basket - '', -- a_quantities_permutation_basket - 2, -- a_id_permutation_edit - 1, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 2, -- a_id_currency_edit - 1 -- a_id_region_purchase -); - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '1', -- a_ids_permutation_basket - '9', -- a_quantities_permutation_basket - 1, -- a_id_permutation_edit - 69, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -); -CALL p_shop_edit_user_basket ( - 'auth0|6582b95c895d09a70ba10feF', -- a_id_user - '2', -- a_ids_permutation_basket - '7', -- a_quantities_permutation_basket - 2, -- a_id_permutation_edit - NULL, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -); - - - {'a_id_user': 'auth0|6582b95c895d09a70ba10fef', - 'a_ids_permutation_basket': '1', - '7', -- a_quantities_permutation_basket - 'a_id_permutation_edit': 1, - 'a_quantity_permutation_edit': 1, - 'a_sum_not_edit': 1} - - select * from shop_user_basket; - insert into shop_user_change_set (comment) - values( 'deactivate duplicates'); - update SHOP_USER_BASKET - set active = FALSE, - id_change_set_user = (select id_change_set from shop_user_change_set order by id_change_set desc limit 1) - where id_user = 'auth0|6582b95c895d09a70ba10fef' - and id_product = 1 - ; - select * from shop_user_basket; -*/ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_product ( - IN a_id_user INTEGER, - IN a_get_all_category BOOLEAN, - IN a_get_inactive_category BOOLEAN, - IN a_get_first_category_only BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_all_product BOOLEAN, - IN a_get_inactive_product BOOLEAN, - IN a_get_first_product_only BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_all_product_permutation BOOLEAN, - IN a_get_inactive_permutation BOOLEAN, - IN a_get_first_permutation_only BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_get_all_image BOOLEAN, - IN a_get_inactive_image BOOLEAN, - IN a_get_first_image_only BOOLEAN, - IN a_ids_image INTEGER[], - IN a_get_all_delivery_region BOOLEAN, - IN a_get_inactive_delivery_region BOOLEAN, - IN a_ids_delivery_region INTEGER[], - IN a_get_all_currency BOOLEAN, - IN a_get_inactive_currency BOOLEAN, - IN a_ids_currency INTEGER[], - IN a_get_all_discount BOOLEAN, - IN a_get_inactive_discount BOOLEAN, - IN a_ids_discount INTEGER[] -) -RETURNS SETOF REFCURSOR -- categories, SETOF products, SETOF variations, SETOF prices, SETOF images, SETOF delivery_options, SETOF discounts -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_category BOOLEAN; - v_get_inactive_category BOOLEAN; - v_get_first_category_only BOOLEAN; - v_ids_category INTEGER[]; - v_get_all_product BOOLEAN; - v_get_inactive_product BOOLEAN; - v_get_first_product_only BOOLEAN; - v_ids_product INTEGER[]; - v_get_all_product_permutation BOOLEAN; - v_get_inactive_permutation BOOLEAN; - v_get_first_permutation_only BOOLEAN; - v_ids_permutation INTEGER[]; - v_get_all_image BOOLEAN; - v_get_inactive_image BOOLEAN; - v_get_first_image_only BOOLEAN; - v_ids_image INTEGER[]; - v_get_all_delivery_region BOOLEAN; - v_get_inactive_delivery_region BOOLEAN; - v_ids_delivery_region INTEGER[]; - v_get_all_currency BOOLEAN; - v_get_inactive_currency BOOLEAN; - v_ids_currency INTEGER[]; - v_get_all_discount BOOLEAN; - v_get_inactive_discount BOOLEAN; - v_ids_discount INTEGER[]; - - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_image BOOLEAN; - v_has_filter_delivery_region BOOLEAN; - v_has_filter_currency BOOLEAN; - v_has_filter_discount BOOLEAN; - v_guid UUID; - -- v_id_user VARCHAR(100); - v_ids_permutation_unavailable VARCHAR(4000); - v_id_permission_product INTEGER; - v_ids_product_permission VARCHAR(4000); - -- v_ids_permutation_permission VARCHAR(4000); - v_id_access_level_view INTEGER; - -- v_now TIMESTAMP; - v_id_minimum INTEGER; - - result_categories REFCURSOR; - result_products REFCURSOR; - result_variations REFCURSOR; - result_prices REFCURSOR; - result_images REFCURSOR; - result_delivery_options REFCURSOR; - result_discounts REFCURSOR; - /* - -- result_errors REFCURSOR; - */ -BEGIN - v_id_user := a_id_user; - v_get_all_category := COALESCE(a_get_all_category, FALSE); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_get_first_category_only := COALESCE(a_get_first_category_only, TRUE); - v_ids_category := COALESCE(a_ids_category, ARRAY[]::INTEGER[]); - v_get_all_product := COALESCE(a_get_all_product, FALSE); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_get_first_product_only := COALESCE(a_get_first_product_only, TRUE); - v_ids_product := COALESCE(a_ids_product, ARRAY[]::INTEGER[]); - v_get_all_product_permutation := COALESCE(a_get_all_product_permutation, FALSE); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_get_first_permutation_only := COALESCE(a_get_first_permutation_only, TRUE); - v_ids_permutation := COALESCE(a_ids_permutation, ARRAY[]::INTEGER[]); - v_get_all_image := COALESCE(a_get_all_image, TRUE); - v_get_inactive_image := COALESCE(a_get_inactive_image, FALSE); - v_get_first_image_only := COALESCE(a_get_first_image_only, FALSE); - v_ids_image := COALESCE(a_ids_image, ARRAY[]::INTEGER[]); - v_get_all_delivery_region := COALESCE(a_get_all_delivery_region, TRUE); - v_get_inactive_delivery_region := COALESCE(a_get_inactive_delivery_region, FALSE); - v_ids_delivery_region := COALESCE(a_ids_delivery_region, ARRAY[]::INTEGER[]); - v_get_all_currency := COALESCE(a_get_all_currency, TRUE); - v_get_inactive_currency := COALESCE(a_get_inactive_currency, FALSE); - v_ids_currency := COALESCE(a_ids_currency, ARRAY[]::INTEGER[]); - v_get_all_discount := COALESCE(a_get_all_discount, TRUE); - v_get_inactive_discount := COALESCE(a_get_inactive_discount, FALSE); - v_ids_discount := COALESCE(a_ids_discount, ARRAY[]::INTEGER[]); - /* - ROLLBACK; - */ - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - - v_has_filter_category = (CARDINALITY(v_ids_category) > 0); - v_has_filter_product = (CARDINALITY(v_ids_product) > 0); - v_has_filter_permutation = (CARDINALITY(v_ids_permutation) > 0); - v_has_filter_image = (CARDINALITY(v_ids_image) > 0); - v_has_filter_delivery_region = (CARDINALITY(v_ids_delivery_region) > 0); - v_has_filter_currency = (CARDINALITY(v_ids_currency) > 0); - v_has_filter_discount = (CARDINALITY(v_ids_discount) > 0); - - /* - SELECT v_id_user, v_get_all_category, v_ids_category, v_get_inactive_category, v_get_all_product, - v_ids_product, v_get_inactive_product, v_get_first_product_only, v_get_all_product_permutation, v_ids_permutation, - v_get_inactive_permutation, v_get_all_image, v_ids_image, v_get_inactive_image, v_get_first_image_only, - v_get_all_delivery_region, v_ids_delivery_region, v_get_inactive_delivery_region, v_get_all_currency, v_ids_currency, - v_get_inactive_currency, v_get_all_discount, v_ids_discount, v_get_inactive_discount - ; - */ - - -- Temporary tables - /* - DROP TEMPORARY TABLE IF EXISTS tmp_Discount; - DROP TEMPORARY TABLE IF EXISTS tmp_Currency; - DROP TEMPORARY TABLE IF EXISTS tmp_Delivery_Region; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Image; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Variation; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product_Category; - */ - DROP TABLE IF EXISTS tmp_Discount; - DROP TABLE IF EXISTS tmp_Currency; - DROP TABLE IF EXISTS tmp_Delivery_Region; - DROP TABLE IF EXISTS tmp_Shop_Image; - DROP TABLE IF EXISTS tmp_Shop_Variation; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_Product_Category; - - CREATE TEMPORARY TABLE tmp_Shop_Product_Category ( - id_category INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_Category_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - */ - id_product INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - */ - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - */ - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - -- rank_permutation INTEGER NOT NULL, -- _in_category - rank_category INTEGER NOT NULL, - rank_product INTEGER NOT NULL, - rank_permutation INTEGER NOT NULL, - name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - /* - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - */ - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations BOOLEAN NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BOOLEAN - ); - - /* - CREATE TEMPORARY TABLE tmp_Shop_Variation ( - id_variation INTEGER NOT NULL, - id_product INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TEMPORARY TABLE tmp_Shop_Image ( - id_image INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_image - FOREIGN KEY (id_image) - REFERENCES Shop_Image(id_image), - */ - id_product INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - */ - id_permutation INTEGER NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - rank_in_product_permutation INTEGER NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Delivery_Region ( - id_region INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Delivery_Region_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - requires_delivery_option BOOLEAN NOT NULL DEFAULT FALSE - ); - - CREATE TEMPORARY TABLE tmp_Currency ( - id_currency INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Currency_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Discount ( - id_discount INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Discount_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL - ); - - /* - CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - */ - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation, - -- rank_permutation, - rank_category, - rank_product, - rank_permutation, - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation, - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, -- PARTITION BY P.id_category -- _in_category - RANK() OVER (ORDER BY C.display_order) AS rank_category, - RANK() OVER (PARTITION BY P.id_category ORDER BY P.display_order) AS rank_product, - RANK() OVER (PARTITION BY P.id_category, P.id_product ORDER BY PP.display_order) AS rank_permutation, - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - v_get_all_product_permutation - OR ( - v_has_filter_permutation - -- AND FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - AND PP.id_permutation = ANY(v_ids_permutation) - ) - ) - AND (v_get_inactive_permutation OR PP.active) - ) - -- categories - AND ( - ( - v_get_all_category - OR ( - v_has_filter_category - -- AND FIND_IN_SET(P.id_category, v_ids_category) > 0 - AND C.id_category = ANY(v_ids_category) - ) - ) - AND (v_get_inactive_category OR C.active) - ) - -- products - AND ( - ( - v_get_all_product - OR ( - v_has_filter_product - -- AND FIND_IN_SET(P.id_product, v_ids_product) > 0 - AND P.id_product = ANY(v_ids_product) - ) - ) - AND (v_get_inactive_product OR P.active) - ) - ; - - -- select * from tmp_Shop_Product; - - IF v_get_first_category_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_category > 1 - ; - END IF; - - IF v_get_first_product_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_product > 1 - ; - END IF; - - IF v_get_first_permutation_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_permutation > 1 - ; - END IF; - - - INSERT INTO tmp_Shop_Product_Category ( - id_category, - active, - display_order - ) - SELECT DISTINCT C.id_category, - C.active, - C.display_order - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product_Category C - ON t_P.id_category = C.id_category - ORDER BY C.display_order - ; - - /* - INSERT INTO tmp_Shop_Variation ( - id_variation, id_product -- , display_order - ) - SELECT P.id_variation, P.id_product -- , P.display_order - FROM Shop_Variation V - INNER JOIN tmp_Shop_Product t_P - ON V.id_product = t_P.id_product - WHERE V.active; - */ - - -- Product Images - INSERT INTO tmp_Shop_Image ( - id_product, - id_permutation, - id_image, - active, - display_order, - rank_in_product_permutation - ) - SELECT id_product, - id_permutation, - id_image, - active, - ROW_NUMBER() OVER (ORDER BY display_order_product_temp, display_order_image), - RANK() OVER (PARTITION BY id_product, id_permutation ORDER BY display_order_product_temp, display_order_image) - FROM ( - SELECT t_P.id_product, - I.id_permutation, - I.id_image, - I.active, - I.display_order AS display_order_image, - t_P.rank_permutation AS display_order_product_temp - FROM Shop_Image I - INNER JOIN tmp_Shop_Product t_P - ON I.id_product = t_P.id_product - AND NOT t_P.product_has_variations - UNION - SELECT t_P.id_product, - I.id_permutation, - I.id_image, - I.active, - I.display_order AS display_order_image, - t_P.rank_permutation AS display_order_product_temp - FROM Shop_Image I - INNER JOIN tmp_Shop_Product t_P - ON I.id_permutation = t_P.id_permutation - AND t_P.product_has_variations - ) IPP - WHERE - ( - v_get_all_image - OR v_get_first_image_only - -- OR FIND_IN_SET(id_image, v_ids_image) > 0 - OR IPP.id_image = ANY(v_ids_image) - ) - AND (v_get_inactive_image OR IPP.active) - ; - - IF v_get_first_image_only THEN - DELETE FROM tmp_Shop_Image - WHERE rank_in_product_permutation > 1 - ; - END IF; - - /* - IF v_has_filter_image THEN - DELETE FROM tmp_Shop_Product - WHERE id_product NOT IN (SELECT DISTINCT id_product FROM tmp_Shop_Image); - DELETE FROM tmp_Shop_Product_Category - WHERE id_category NOT IN (SELECT DISTINCT id_category FROM tmp_Shop_Product); - END IF; - */ - - -- Delivery Regions - INSERT INTO tmp_Delivery_Region ( - id_region, - active, - display_order, - requires_delivery_option - ) - WITH RECURSIVE Recursive_CTE_Delivery_Region AS ( - SELECT - CAST(NULL AS INTEGER) AS id_region_parent, - DR.id_region AS id_region_child, - -- CASE WHEN FIND_IN_SET(DR.id_region, v_ids_delivery_region) > 0 THEN TRUE ELSE FALSE END AS requires_delivery_option - (DR.id_region = ANY(v_ids_delivery_region)) AS requires_delivery_option - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN Shop_Currency C ON PCRL.id_currency = C.id_currency - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - INNER JOIN Shop_Region DR ON PCRL.id_region_purchase = DR.id_region - WHERE - ( - v_get_all_delivery_region - -- OR FIND_IN_SET(DR.id_region, v_ids_delivery_region) > 0 - OR DR.id_region = ANY(v_ids_delivery_region) - ) - AND ( - v_get_inactive_delivery_region - OR DR.active = TRUE - ) - UNION - SELECT - DRB.id_region_parent, - DRB.id_region_child, - FALSE AS requires_delivery_option - FROM Shop_Region_Branch DRB - INNER JOIN Recursive_CTE_Delivery_Region r_DR - ON DRB.id_region_parent = r_DR.id_region_child - WHERE ( - v_get_inactive_delivery_region - OR DRB.active = TRUE - ) - ) - SELECT - DR.id_region, - DR.active, - DR.display_order, - requires_delivery_option - FROM Shop_Region DR - INNER JOIN Recursive_CTE_Delivery_Region r_DR - ON DR.id_region = r_DR.id_region_parent - OR DR.id_region = r_DR.id_region_child - ; - /* - select * from tmp_delivery_region; - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_product - OR v_get_all_product_permutation - ) */ - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - ; - */ - - IF v_has_filter_delivery_region THEN - v_ids_permutation_unavailable = ( - SELECT STRING_AGG(t_P.id_permutation, ', ') - FROM ( - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_produc - OR v_get_all_product_permutation - ) - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - */ - t_P.id_category = ANY(v_ids_category) - OR t_P.id_product = ANY(v_ids_product) - OR t_P.id_permutation = ANY(v_ids_permutation) - ) t_P - LEFT JOIN ( - SELECT * - FROM Shop_Product_Currency_Region_Link PCRL - WHERE - v_get_all_delivery_region - -- OR FIND_IN_SET(PCRL.id_region_purchase, v_ids_delivery_region) > 0 - OR PCRL.id_region_purchase = ANY(v_ids_delivery_region) - ) PCRL - ON t_P.id_product = PCRL.id_product - AND t_P.id_permutation = PCRL.id_permutation - LEFT JOIN tmp_Delivery_Region t_DR - ON PCRL.id_region_purchase = t_DR.id_region - AND t_DR.requires_delivery_option - WHERE - ISNULL(t_DR.id_region) - ); - IF NOT ISNULL(v_ids_permutation_unavailable) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'PRODUCT_AVAILABILITY' LIMIT 1), - 'PRODUCT_AVAILABILITY', - 'Error: The following permutation IDs are not available in this region: ' || COALESCE(v_ids_permutation_unavailable, 'NULL') - ); - */ - RAISE EXCEPTION 'The following permutation IDs are not available in this region: %', COALESCE(v_ids_permutation_unavailable, 'NULL') - USING ERRCODE = '22000'; - END IF; - /* - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.id_permutation NOT IN ( - SELECT - id_permutation - FROM Shop_Product_Currency_Region_Link PCL - INNER JOIN tmp_Delivery_Region t_DR - ON PCRL.id_region_purchase = t_DR.id_region - ); - */ - END IF; - - -- select * from tmp_Shop_Product; - - -- Currencies - INSERT INTO tmp_Currency ( - id_currency, - active, - display_order - ) - SELECT - C.id_currency, - C.active, - C.display_order - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN Shop_Currency C ON PCRL.id_currency = C.id_currency - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Delivery_Region t_DR ON PCRL.id_region_purchase = t_DR.id_region - WHERE - ( - v_get_all_currency - -- R FIND_IN_SET(C.id_currency, v_ids_currency) > 0 - OR C.id_currency = ANY(v_ids_currency) - ) - AND ( - v_get_inactive_currency - OR ( - C.active - AND PCRL.active - ) - ) - ; - - -- select * from tmp_Currency; - - IF v_has_filter_currency THEN - v_ids_permutation_unavailable = ( - SELECT STRING_AGG(t_P.id_permutation, ', ') - FROM ( - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_product - OR v_get_all_product_permutation - ) - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - */ - t_P.id_category = ANY(v_ids_category) - OR t_P.id_product = ANY(v_ids_product) - OR t_P.id_permutation = ANY(v_ids_permutation) - ) t_P - INNER JOIN ( - SELECT * - FROM Shop_Product_Currency_Region_Link PCRL - WHERE - ( - v_get_all_currency - -- OR FIND_IN_SET(PCRL.id_currency, v_ids_currency) > 0 - OR PCRL.id_currency = ANY(v_ids_currency) - ) - ) PCRL - ON t_P.id_permutation = PCRL.id_permutation - LEFT JOIN tmp_Currency t_C - ON PCRL.id_currency = t_C.id_currency - WHERE ISNULL(t_C.id_currency) - ); - IF NOT ISNULL(v_ids_permutation_unavailable) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'PRODUCT_AVAILABILITY' LIMIT 1), - 'PRODUCT_AVAILABILITY', - 'Error: The following permutation IDs are not available in this currency: ' || COALESCE(v_ids_permutation_unavailable, 'NULL') - ); - */ - RAISE EXCEPTION 'The following permutation IDs are not available in this currency: %', COALESCE(v_ids_permutation_unavailable, 'NULL') - USING ERRCODE = '22000'; - END IF; - /* - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.id_permutation NOT IN ( - SELECT - id_permutation - FROM Shop_Product_Currency_Region_Link PCL - INNER JOIN tmp_Currency t_C - ON PCRL.id_currency = t_C.id_currency - ); - */ - END IF; - - -- Discounts - INSERT INTO tmp_Discount ( - id_discount, - active, - display_order - ) - SELECT - D.id_discount, - D.active, - D.display_order - FROM Shop_Discount D - INNER JOIN tmp_Shop_Product t_P - ON D.id_product = t_P.id_product - AND D.id_permutation = t_P.id_permutation - WHERE - ( - v_get_all_discount - -- OR FIND_IN_SET(D.id_discount, v_ids_discount) > 0 - OR D.id_discount = ANY(v_ids_discount) - ) - AND ( - v_get_inactive_discount - OR D.active - ) - ; - -- select 'pre-permission results'; - -- select * from tmp_Shop_Product; - - -- Permissions - IF EXISTS (SELECT * FROM tmp_Shop_Product_Category LIMIT 1) THEN - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_product := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_PRODUCT' LIMIT 1); - v_ids_product_permission := (SELECT STRING_AGG(id_product, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_product_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product - AND UE_T.GUID = v_guid - ; - -- select * from Shop_Calc_User_Temp; - -- select * from tmp_Shop_Product; - - DELETE FROM tmp_Shop_Product t_P - WHERE - -- FIND_IN_SET(t_P.id_product, (SELECT STRING_AGG(UET.id_product, ',') FROM Shop_Calc_User_Temp UET)) = FALSE -- id_product NOT LIKE CONCAT('%', (SELECT STRING_AGG(id_product, '|') FROM Shop_Calc_User_Temp), '%'); - t_P.id_product NOT IN ( - SELECT id_product - FROM Shop_Calc_User_Temp UET - WHERE UET.GUID = v_guid - ) - OR ISNULL(t_P.can_view) - OR t_P.can_view = FALSE - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- Categories - OPEN result_categories FOR - -- RETURN QUERY - SELECT - DISTINCT t_C.id_category, - C.name, - C.description, - C.display_order - FROM tmp_Shop_Product_Category t_C - INNER JOIN Shop_Product_Category C - ON t_C.id_category = C.id_category - INNER JOIN tmp_Shop_Product t_P - ON t_C.id_category = t_P.id_category - ORDER BY C.display_order - ; - RETURN NEXT result_categories; - -- CLOSE result_categories; - - -- Products - OPEN result_products FOR - -- RETURN QUERY - SELECT - t_P.id_product, - t_P.id_permutation, - t_P.name, - t_P.description, - P.has_variations, - P.id_category, - PP.cost_local, - PP.id_currency_cost, - PP.profit_local_min, - t_P.latency_manufacture, - t_P.quantity_min, - t_P.quantity_max, - t_P.quantity_step, - t_P.quantity_stock, - t_P.id_stripe_product, - t_P.is_subscription, - RI.name AS name_interval_recurrence, - RI.name_plural AS name_plural_interval_recurrence, - t_P.count_interval_recurrence, - t_P.display_order_category, - t_P.display_order_product, - t_P.display_order_permutation, - COALESCE(t_P.can_view, FALSE), - COALESCE(t_P.can_edit, FALSE), - COALESCE(t_P.can_admin, FALSE) - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product P ON t_P.id_product = P.id_product - INNER JOIN Shop_Product_Permutation PP ON t_P.id_permutation = PP.id_permutation - LEFT JOIN Shop_Interval_Recurrence RI ON t_P.id_unit_measurement_interval_recurrence = RI.id_interval - ORDER BY t_P.rank_permutation - ; - RETURN NEXT result_products; - -- CLOSE result_products; - - -- Variations - OPEN result_variations FOR - -- RETURN QUERY - SELECT - V.id_variation, - t_P.id_product, - t_P.id_permutation, - t_P.id_category, - VT.code AS code_variation_type, - VT.name AS name_variation_type, - V.code AS code_variation, - V.name AS name_variation, - RANK() OVER (ORDER BY t_P.rank_permutation, PPVL.display_order) AS display_order - FROM Shop_Variation V - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL ON V.id_variation = PPVL.id_variation - INNER JOIN tmp_Shop_Product t_P ON PPVL.id_permutation = t_P.id_permutation - WHERE V.active - AND PPVL.active - ; - RETURN NEXT result_variations; - -- CLOSE result_variations; - - /* - -- Permutation variations output - SELECT t_P.id_permutation, - t_P.id_product, - t_P.id_category, - id_variation - FROM Shop_Product_Permutation_Variation_Link PPVL - INNER JOIN tmp_Shop_Product t_P - ON t_P.id_permutation = PPVL.id_permutation - ORDER BY t_P.display_order - ; - */ - -- select * from Shop_Product_Currency_Region_Link; - -- select * from shop_currency; - /* - select * from tmp_Currency; - select * from tmp_delivery_region; - select * from tmp_shop_product; - */ - - -- Product Price - OPEN result_prices FOR - -- RETURN QUERY - SELECT - PCRL.id_link AS id_price, - t_P.id_permutation, - t_P.id_product, - t_P.id_category, - t_C.id_currency, - C.code AS code_currency, - C.name AS name_currency, - C.symbol AS symbol_currency, - t_DR.id_region, - PCRL.price_local_VAT_incl, - PCRL.price_local_VAT_excl, - ROW_NUMBER() OVER(ORDER BY t_P.rank_permutation, C.display_order) AS display_order - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - -- INNER JOIN Shop_Product P ON PCRL.id_product = P.id_product - INNER JOIN tmp_Currency t_C ON PCRL.id_currency = t_C.id_currency - INNER JOIN Shop_Currency C ON t_C.id_currency = C.id_currency - INNER JOIN tmp_Delivery_Region t_DR ON PCRL.id_region_purchase = t_DR.id_region - WHERE ( - v_get_inactive_product - AND v_get_inactive_permutation - AND v_get_inactive_currency - AND v_get_inactive_delivery_region - OR PCRL.active - ) - ORDER BY t_P.rank_permutation - ; - RETURN NEXT result_prices; - -- CLOSE result_prices; - - /* - -- Currency - SELECT - DISTINCT C.id_currency, - C.code, - C.name, - C.factor_from_GBP, - t_C.display_order - FROM Shop_Currency C - INNER JOIN tmp_Currency t_C ON C.id_currency = t_C.id_currency - GROUP BY C.id_currency, t_C.display_order - ORDER BY t_C.display_order - ; - */ - - -- Images - OPEN result_images FOR - -- RETURN QUERY - SELECT - t_I.id_image, - t_I.id_product, - t_I.id_permutation, - t_P.id_category, - I.url, - I.active, - I.display_order - FROM tmp_Shop_Image t_I - INNER JOIN Shop_Image I - ON t_I.id_image = I.id_image - INNER JOIN tmp_Shop_Product t_P - ON t_I.id_product = t_P.id_product - AND t_I.id_permutation = t_P.id_permutation - ORDER BY t_P.rank_permutation, I.display_order - ; - RETURN NEXT result_images; - -- CLOSE result_images; - - -- Delivery options - OPEN result_delivery_options FOR - -- RETURN QUERY - SELECT - _DO.id_option, - PDOL.id_product, - PDOL.id_permutation, - t_P.id_category, - _DO.code, - _DO.name, - _DO.latency_delivery_min, - _DO.latency_delivery_max, - _DO.quantity_min, - _DO.quantity_max, - STRING_AGG(DR.code, ',') AS codes_region, - STRING_AGG(DR.name, ',') AS names_region, - PDOL.price_local, - PDOL.display_order - FROM Shop_Delivery_Option _DO - INNER JOIN Shop_Product_Permutation_Delivery_Option_Link PDOL - ON _DO.id_option = PDOL.id_delivery_option - AND ( - v_get_inactive_delivery_region - OR PDOL.active - ) - INNER JOIN tmp_Shop_Product t_P - ON PDOL.id_product = t_P.id_product - AND PDOL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Delivery_Region t_DR ON PDOL.id_region = t_DR.id_region - INNER JOIN Shop_Region DR ON t_DR.id_region = DR.id_region - WHERE ( - v_get_inactive_delivery_region - OR _DO.active - ) - GROUP BY t_P.id_category, t_P.id_product, PDOL.id_permutation, t_P.rank_permutation, DR.id_region, _DO.id_option, PDOL.id_link - ORDER BY t_P.rank_permutation, PDOL.display_order - ; - RETURN NEXT result_delivery_options; - -- CLOSE result_delivery_options; - - -- Discounts - OPEN result_discounts FOR - -- RETURN QUERY - SELECT - D.id_discount, - P.id_category, - D.id_product, - D.id_permutation, - DR.id_region, - C.id_currency, - D.code AS code_discount, - D.name AS name_discount, - D.multiplier, - D.subtractor, - D.apply_multiplier_first, - D.quantity_min, - D.quantity_max, - D.date_start, - D.date_end, - STRING_AGG(DR.code, ', ') OVER(PARTITION BY D.id_discount) AS codes_region, - STRING_AGG(DR.name, ', ') OVER(PARTITION BY D.id_discount) AS names_region, - STRING_AGG(C.code, ', ') OVER(PARTITION BY D.id_discount) AS codes_currency, - STRING_AGG(C.name, ', ') OVER(PARTITION BY D.id_discount) AS names_currency, - ROW_NUMBER() OVER(ORDER BY D.display_order) AS display_order - FROM tmp_Discount t_D - INNER JOIN Shop_Discount D ON t_D.id_discount = D.id_discount - INNER JOIN Shop_Product P ON D.id_product = P.id_product - INNER JOIN tmp_Shop_Product t_P - ON D.id_product = t_P.id_product - -- AND D.id_permutation = t_P.id_permutation - INNER JOIN Shop_Discount_Region_Currency_Link DRCL - ON D.id_discount = DRCL.id_discount - INNER JOIN tmp_Delivery_Region t_DR ON DRCL.id_region = t_DR.id_region - INNER JOIN Shop_Region DR ON t_DR.id_region = DR.id_region - INNER JOIN tmp_Currency t_C ON DRCL.id_currency = t_C.id_currency - INNER JOIN Shop_Currency C ON t_C.id_currency = C.id_currency - GROUP BY D.id_discount, DR.id_region, C.id_currency, P.id_category, P.id_product, D.id_permutation - ORDER BY D.display_order, DR.display_order, C.display_order - ; - RETURN NEXT result_discounts; - -- CLOSE result_discounts; - /* - -- Delivery Regions - SELECT - t_DR.id_region, - t_P.id_category, - t_P.id_product, - t_P.id_permutation, - DR.code, - DR.name - FROM tmp_Delivery_Region t_DR - INNER JOIN Shop_Delivery_Region DR ON t_DR.id_region = DR.id_region - INNER JOIN Shop_Product_Region_Currency_Link PDRL - ON DR.id_region = PDRL.id_region - AND ( - v_get_inactive_delivery_region - OR PDRL.active - ) - INNER JOIN tmp_Shop_Product t_P - ON PDRL.id_product = t_P.id_product - AND PDRL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Currency t_C ON PDRL.id_currency = t_C.id_currency - ORDER BY t_DR.display_order - ; - */ - - -- Errors - /* - OPEN result_errors FOR - RETURN QUERY - SELECT - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - /* - DROP TEMPORARY TABLE IF EXISTS tmp_Discount; - DROP TEMPORARY TABLE IF EXISTS tmp_Currency; - DROP TEMPORARY TABLE IF EXISTS tmp_Delivery_Region; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Image; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Variation; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product_Category; - DROP TABLE IF EXISTS tmp_Discount; - DROP TABLE IF EXISTS tmp_Currency; - DROP TABLE IF EXISTS tmp_Delivery_Region; - DROP TABLE IF EXISTS tmp_Shop_Image; - DROP TABLE IF EXISTS tmp_Shop_Variation; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_Product_Category; - */ -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_product ( - 1, -- a_id_user - TRUE, -- a_get_all_category - FALSE, -- a_get_inactive_category - FALSE, -- a_get_first_category_only - ARRAY[]::INTEGER[], -- a_ids_category - TRUE, -- a_get_all_product - FALSE, -- a_get_inactive_product - FALSE, -- a_get_first_product_only - ARRAY[]::INTEGER[], -- a_ids_product - TRUE, -- a_get_all_product_permutation - FALSE, -- a_get_inactive_permutation - FALSE, -- a_get_first_permutation_only - ARRAY[1, 2, 3, 4, 5]::INTEGER[], -- a_ids_permutation - FALSE, -- a_get_all_image - FALSE, -- a_get_inactive_image - TRUE, -- a_get_first_image_only - ARRAY[]::INTEGER[], -- a_ids_image - FALSE, -- a_get_all_delivery_region - FALSE, -- a_get_inactive_delivery_region - ARRAY[]::INTEGER[], -- a_ids_delivery_region - FALSE, -- a_get_all_currency - FALSE, -- a_get_inactive_currency - ARRAY[]::INTEGER[], -- a_ids_currency - TRUE, -- a_get_all_discount - FALSE, -- a_get_inactive_discount - ARRAY[]::INTEGER[] -- a_ids_discount - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_currency ( - IN a_get_inactive_currency BOOLEAN -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_get_inactive_currency BOOLEAN; - result_currency REFCURSOR; -BEGIN - v_get_inactive_currency := COALESCE(a_get_inactive_currency, FALSE); - - OPEN result_currency FOR - SELECT - C.id_currency, - C.code, - C.name, - C.factor_from_GBP, - C.active, - C.display_order - FROM Shop_Currency C - WHERE v_get_inactive_currency - OR C.active - ORDER BY C.display_order - ; - RETURN NEXT result_currency; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_currency ( - FALSE -- a_get_inactive_currency - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_region ( - IN a_get_inactive_region BOOLEAN -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_get_inactive_region BOOLEAN; - result_region REFCURSOR; -BEGIN - v_get_inactive_region := COALESCE(a_get_inactive_region, FALSE); - - OPEN result_region FOR - SELECT - R.id_region, - R.code, - R.name, - R.active, - R.display_order - FROM Shop_Region R - WHERE v_get_inactive_region - OR R.active - ORDER BY R.display_order - ; - -- RETURN NEXT result_region; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_region ( - FALSE -- a_get_inactive_region - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_user_order ( - IN a_id_user INTEGER, - IN a_ids_order VARCHAR(4000), - IN a_n_order_max INTEGER, - IN a_id_checkout_session VARCHAR(200) -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_ids_order VARCHAR(4000); - v_n_order_max INTEGER; - v_id_checkout_session VARCHAR(200); - v_has_filter_user BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_session BOOLEAN; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_orders REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_n_order_max := a_n_order_max; - v_id_checkout_session := TRIM(COALESCE(a_id_checkout_session, '')); - - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - v_ids_order = REPLACE(v_ids_order, '|', ','); - v_has_filter_order = CASE WHEN v_ids_order = '' THEN FALSE ELSE TRUE END; - v_has_filter_session = CASE WHEN v_id_checkout_session = '' THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_User; - DROP TABLE IF EXISTS tmp_Shop_Order; - - /* - CREATE TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - */ - - CREATE TEMPORARY TABLE tmp_Shop_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_Order_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_User_Order(id_order), - active BOOLEAN NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - -- id_type INTEGER NOT NULL, - -- CONSTRAINT FK_tmp_Msg_Error_id_type FOREIGN KEY (id_type) - -- REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50), - msg VARCHAR(4000) NOT NULL - ); - */ - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - v_has_filter_user = EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1); - v_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - ELSE - RAISE EXCEPTION 'Valid user ID must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - -- Permissions - CALL p_shop_calc_user ( - v_guid, -- a_guid - a_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_USER' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' AND active), -- a_ids_access_level - '', -- a_ids_product - '' -- a_ids_permutation - ); - - IF NOT (SELECT can_edit FROM Shop_Calc_User_Temp WHERE guid = v_guid) THEN - RAISE EXCEPTION 'User ID does not have permission to access orders.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - -- Invalid Order IDs - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_order = ANY(v_ids_order) - LIMIT 1 - ) THEN -- id_order LIKE CONCAT('%', v_ids_order, '%') LIMIT 1) THEN - RAISE EXCEPTION 'You do not have access to the following order IDs: %', ( - SELECT STRING_AGG(id_order, ', ') - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_order = ANY(v_ids_order) - ) - USING ERRCODE = '22000' - ; - END IF; - -- Invalid Checkout Session IDs - IF v_has_filter_session AND EXISTS ( - SELECT * - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_checkout_session = v_id_checkout_session - ) THEN - RAISE EXCEPTION 'You do not have access to the following checkout session IDs: %', ( - SELECT STRING_AGG(id_order, ', ') - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_checkout_session = v_id_checkout_session - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Valid Orders - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - - INSERT INTO tmp_Shop_Order ( - id_order, - active - ) - SELECT UO.id_order, - UO.active - FROM Shop_User_Order UO - INNER JOIN tmp_Shop_User t_U - ON UO.id_user = t_U.id_user - AND t_U.active - WHERE ((NOT v_has_filter_order OR FIND_IN_SET(UO.id_order, v_ids_order) > 0) -- UO.id_order LIKE CONCAT('%', v_ids_order, '%')) - OR (NOT v_has_filter_session OR UO.id_checkout_session = v_id_checkout_session)) - AND UO.active - ; - END IF; - - - - -- Returns - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - OPEN result_orders FOR - SELECT t_O.id_order, - UOPL.id_product, - UOPL.id_permutation, - UOPL.quantity - FROM tmp_Shop_Order t_O - INNER JOIN Shop_User_Order UO - ON t_O.id_order = UO.id_order - INNER JOIN Shop_User_Order_Product_Link UOPL - ON UO.id_order = UOPL.id_order - WHERE t_O.active - ; - RETURN NEXT result_orders; - - /* - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user, - v_ids_order, - v_n_order_max, - v_id_checkout_session - ; - */ - - -- Clean up - -- DROP TABLE IF EXISTS tmp_Shop_User; - -- DROP TABLE IF EXISTS tmp_Shop_Order; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_user_order ( - 'auth0|6582b95c895d09a70ba10fef', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* - -CALL p_shop_get_many_user_order ( - 'auth0|6582b95c895d09a70ba10fef', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session -); - -CALL p_shop_get_many_user_order ( - '', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session -); - -insert into shop_product_change_set (comment) - values ('set product not subscription - test bool output to python'); - update shop_product - set is_subscription = 0, - id_change_set = (select id_change_set from shop_product_change_set order by id_change_set desc limit 1) - where id_product = 1 -select * from shop_User; -select * from shop_User_oRDER; -*/ - - -/* - -CALL p_shop_get_many_stripe_product_new ( - '' -) - -*/ - -CREATE OR REPLACE FUNCTION p_shop_get_many_stripe_product_new ( - IN a_id_user INTEGER -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_products REFCURSOR; - result_product_variation_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := a_id_user; - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TEMPORARY TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product ( - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active BOOLEAN NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NOT NULL, - name VARCHAR(200) NOT NULL, - description VARCHAR(4000) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( -- IF NOT EXISTS - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - /* - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - */ - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- User - IF NOT EXISTS( - SELECT * - FROM Shop_User - WHERE - id_user = v_id_user - AND active - ) THEN - RAISE EXCEPTION 'Valid user ID required.' - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - -- Get products - INSERT INTO tmp_Shop_Product ( - id_product, - id_permutation, - active, - display_order_product, - display_order_permutation, - name, - description - ) - SELECT id_product, - id_permutation, - active, - display_order_product, - display_order_permutation, - name, - description - FROM ( - SELECT id_product, - NULL AS id_permutation, - active, - display_order AS display_order_product, - NULL AS display_order_permutation, - name, - description, - id_stripe_product - FROM Shop_Product P - UNION - SELECT t_PPPV.id_product, - id_permutation, - t_PPPV.active, - display_order_product, - display_order_permutation, - P.name, ': ' || names_variation AS name, - P.description || ' With variations: ' || type_name_pairs_variation AS description, - t_PPPV.id_stripe_product - FROM ( - SELECT P.id_product, - PP.id_permutation, - PP.active, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation, - STRING_AGG(V.name, ' ') AS names_variation, - STRING_AGG(VT.name || ': ' || V.name, ', ') AS type_name_pairs_variation, - PP.id_stripe_product - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL - ON PP.id_permutation = PPVL.id_permutation - AND PPVL.active - INNER JOIN Shop_Variation V - ON PPVL.id_variation = V.id_variation - AND V.active - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - AND VT.active - GROUP BY id_product, id_permutation -- , VT.id_type, V.id_variation - ) t_PPPV - INNER JOIN Shop_Product P - ON t_PPPV.id_product = P.id_product - ) t_PPP - WHERE ISNULL(id_stripe_product) - AND active - ; - - -- Permissions - CALL p_shop_calc_user ( - v_guid, -- a_guid - v_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_ADMIN' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'ADMIN' AND active), -- a_ids_access_level - (SELECT STRING_AGG(id_product, ',') From tmp_Shop_Product), -- a_ids_product - (SELECT STRING_AGG(id_permutation, ',') From tmp_Shop_Product) -- a_ids_permutation -- WHERE NOT ISNULL(id_permutation) - ); - - IF EXISTS (SELECT can_admin FROM Shop_Calc_User_Temp WHERE guid = v_guid AND NOT can_admin) THEN - RAISE EXCEPTION 'User ID does not have permission to get all new stripe products.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - - - - -- Returns - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - DELETE FROM tmp_Shop_Product; - END IF; - */ - - OPEN result_products FOR - SELECT id_product, - id_permutation, - name, - description - FROM tmp_Shop_Product - ORDER BY display_order_product, display_order_permutation - ; - RETURN NEXT result_products; - - OPEN result_product_variation_links FOR - SELECT PP.id_permutation, - V.id_variation, - V.name AS name_variation, - VT.id_type AS id_type_variation, - VT.name as name_variation_type - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product_Permutation PP - ON t_P.id_permutation = PP.id_permutation - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL - ON PP.id_permutation = PPVL.id_permutation - AND PPVL.active - INNER JOIN Shop_Variation V - ON PPVL.id_variation = V.id_variation - AND V.active - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - AND VT.active - ; - RETURN NEXT result_product_variation_links; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_stripe_product_new ( - 'auth0|6582b95c895d09a70ba10fef' - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* -CALL p_shop_get_many_stripe_product_new ( - '' -); - -CALL p_shop_get_many_stripe_product_new ( - 'auth0|6582b95c895d09a70ba10fef' -); - - - -select * from shop_product; -select * from shop_product_permutation_variation_link; - -CALL p_shop_calc_user ( - 'ead789a1-c7ac-11ee-a256-b42e9986184a', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 0, -- a_get_inactive_users - '4', -- a_ids_permission - '3', -- a_ids_access_level - '1', -- a_ids_product - '1' -- a_ids_permutation -- WHERE NOT ISNULL(id_permutation) - ); - -*/ - - - -/* - -CALL p_shop_get_many_stripe_price_new ( - '' -) - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_stripe_price_new ( - IN a_id_user INTEGER -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_has_filter_user BOOLEAN; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_products REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TEMPORARY TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product_Currency_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Currency_Region_Link(id_link), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_CurrencyLink_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( -- IF NOT EXISTS - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - /* - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - */ - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- User permissions - IF NOT EXISTS( - SELECT * - FROM Shop_User - WHERE - id_user = v_id_user - AND active - ) THEN - RAISE EXCEPTION 'Valid user ID required.' - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - -- Get products - INSERT INTO tmp_Shop_Product_Currency_Link ( - id_link, - id_product, - id_permutation, - id_currency, - active - ) - SELECT id_link, - id_product, - id_permutation, - id_currency, - active - FROM Shop_Product_Currency_Region_Link - WHERE ISNULL(id_stripe_price) - AND active - ; - - -- Permissions - -- SELECT * FROM tmp_Msg_Error LIMIT 1; - CALL p_shop_calc_user ( - v_guid, -- a_guid - v_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_ADMIN' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'ADMIN' AND active), -- a_ids_access_level - (SELECT STRING_AGG(DISTINCT id_product, ',') FROM tmp_Shop_Product_Currency_Link), -- (SELECT DISTINCT id_product FROM tmp_Shop_Product_Currency_Link) calc_PCL) -- a_ids_product - (SELECT STRING_AGG(DISTINCT id_permutation, ',') FROM tmp_Shop_Product_Currency_Link) -- a_ids_permutation - ); - -- SELECT * FROM tmp_Msg_Error LIMIT 1; - - IF EXISTS (SELECT can_admin FROM Shop_Calc_User_Temp WHERE guid = v_guid AND NOT can_admin LIMIT 1) THEN - RAISE EXCEPTION 'User ID does not have permission to get all new stripe prices.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - - - -- Returns - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - DELETE FROM tmp_Shop_Product_Currency_Link; - END IF; - */ - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - OPEN result_products FOR - SELECT t_PCL.id_product, - t_PCL.id_permutation, - P.price_GBP_full * C.factor_from_GBP AS unit_price, - C.code AS code_currency, - P.id_stripe_product, - P.is_subscription, - LOWER(RI.code) AS name_recurring_interval, - P.count_interval_recurrence - FROM tmp_Shop_Product_Currency_Link t_PCL - INNER JOIN Shop_Product P - ON t_PCL.id_product = P.id_product - AND P.active - INNER JOIN Shop_Interval_Recurrence RI - ON P.id_unit_measurement_interval_recurrence = RI.id_interval - AND RI.active - INNER JOIN Shop_Currency C - ON t_PCL.id_currency = C.id_currency - AND C.active - WHERE t_PCL.active - ; - RETURN NEXT result_products; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_User; - DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_stripe_price_new ( - 'auth0|6582b95c895d09a70ba10fef' - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* -CALL p_shop_get_many_stripe_price_new ( - '' -); - -CALL p_shop_get_many_stripe_price_new ( - 'auth0|6582b95c895d09a70ba10fef' -); - -*/ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_supplier ( - IN a_id_user INTEGER, - IN a_get_all_supplier BOOLEAN, - IN a_get_inactive_supplier BOOLEAN, - IN a_get_first_supplier_only BOOLEAN, - IN a_ids_supplier INTEGER[] -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_supplier BOOLEAN; - v_get_inactive_supplier BOOLEAN; - v_get_first_supplier_only BOOLEAN; - v_ids_supplier INTEGER[]; - v_has_filter_supplier BOOLEAN; - v_guid UUID; - v_id_permission_supplier INTEGER; - v_id_access_level_view INTEGER; - v_id_minimum INTEGER; - result_suppliers REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_supplier := COALESCE(a_get_all_supplier, TRUE); - v_get_inactive_supplier := COALESCE(a_get_inactive_supplier, FALSE); - v_get_first_supplier_only := COALESCE(a_get_first_supplier_only, FALSE); - v_ids_supplier := TRIM(COALESCE(a_ids_supplier, '')); - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - v_has_filter_supplier = NOT (a_ids_supplier = ''); - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Supplier; - - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier), - active BOOLEAN NOT NULL, - rank_supplier INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_supplier THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - ) THEN - RAISE EXCEPTION 'Invalid supplier IDs: %', ( - SELECT STRING_AGG(Supplier_Id, ', ') - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Supplier ( - id_supplier, - active, - rank_supplier - ) - SELECT - S.id_supplier, - S.active, - RANK() OVER (ORDER BY id_supplier ASC) AS rank_supplier - FROM Shop_Supplier S - WHERE - ( - a_get_inactive_supplier - OR S.active = TRUE - ) - AND ( - a_get_all_supplier - OR S.id_supplier = ANY(v_ids_supplier) - ) - ; - END IF; - - IF a_get_first_supplier_only THEN - DELETE FROM tmp_Shop_Supplier t_S - WHERE t_S.rank_supplier > ( - SELECT MIN(t_S.rank_supplier) - FROM tmp_Shop_Supplier t_S - ) - ; - END IF; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_supplier := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER' LIMIT 1); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_id_permission_supplier, v_id_access_level_view, ''); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT name - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier - LIMIT 1 - ) - USING ERRCODE = '42501' - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - - -- Suppliers - OPEN result_suppliers FOR - SELECT - t_S.id_supplier, - S.name_company, - name_contact, - department_contact, - id_address, - phone_number, - fax, - email, - website, - id_currency, - active - FROM tmp_Shop_Supplier t_S - INNER JOIN Shop_Supplier S - ON t_S.id_supplier = S.id_supplier - ; - RETURN NEXT result_suppliers; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Supplier; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_supplier ( - '', -- a_id_user - TRUE, -- a_get_all_supplier - FALSE, -- a_get_inactive_supplier - FALSE, -- a_get_first_supplier_only - '' -- a_ids_supplier - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_supplier_purchase_order ( - IN a_id_user INTEGER, - IN a_get_all_supplier BOOLEAN, - IN a_get_inactive_supplier BOOLEAN, - IN a_get_first_supplier_only BOOLEAN, - IN a_ids_supplier INTEGER[], - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_supplier BOOLEAN; - v_get_inactive_supplier BOOLEAN; - v_get_first_supplier_only BOOLEAN; - v_ids_supplier INTEGER[]; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - v_has_filter_supplier BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - v_ids_permission_supplier_purchase_order INTEGER[]; - v_ids_product_permission INTEGER[]; - v_id_access_level_view INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - result_suppliers REFCURSOR; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_supplier := COALESCE(a_get_all_supplier, TRUE); - v_get_inactive_supplier := COALESCE(a_get_inactive_supplier, FALSE); - v_get_first_supplier_only := COALESCE(a_get_first_supplier_only, FALSE); - v_ids_supplier := TRIM(COALESCE(a_ids_supplier, '')); - v_get_all_order := COALESCE(a_get_all_order, TRUE); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_supplier_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'SHOP_SUPPLIER_PURCHASE_ORDER' LIMIT 1); - v_code_error_data = 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - v_has_filter_supplier = (CARDINALITY(v_ids_supplier) > 0); - v_has_filter_order = (CARDINALITY(v_ids_order) > 0); - v_has_filter_category = (CARDINALITY(v_ids_category) > 0); - v_has_filter_product = (CARDINALITY(v_ids_product) > 0); - v_has_filter_permutation = (CARDINALITY(v_ids_permutation) > 0); - v_has_filter_date_from = CASE WHEN ISNULL(v_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to = CASE WHEN ISNULL(v_date_to) THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Supplier; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier), - active BOOLEAN NOT NULL, - rank_supplier INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_supplier THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid supplier IDs: %', ( - SELECT STRING_AGG(Supplier_Id, ', ') - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Supplier ( - id_supplier, - active, - rank_supplier - ) - SELECT - S.id_supplier, - S.active, - RANK() OVER (ORDER BY id_supplier ASC) AS rank_supplier - FROM Shop_Supplier S - INNER JOIN Split_Temp TS ON S.id_supplier = TS.substring - WHERE - ( - v_get_inactive_supplier - OR S.active = TRUE - ) - ; - END IF; - - IF v_get_first_supplier_only THEN - DELETE FROM tmp_Shop_Supplier t_S - WHERE t_S.rank_supplier > ( - SELECT MIN(t_S.rank_supplier) - FROM tmp_Shop_Supplier t_S - ) - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_product = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT STRING_AGG(Product_Id, ', ') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_permutation = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - ) - AND ( - v_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, v_ids_category) > 0 - ) - AND ( - v_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, v_ids_product) > 0 - ) - AND ( - v_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - -- SELECT * FROM Split_Temp TS LEFT JOIN Shop_Supplier_Purchase_Order SPO ON TS.substring = SPO.id_order WHERE ISNULL(SPO.id_order) - SELECT * - FROM UNNEST(v_ids_order) Order_Id - - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(TS.substring, ', ') - FROM UNNEST(v_ids_order) - LEFT JOIN Shop_Supplier_Purchase_Order SPO ON TS.substring = SPO.id_order - WHERE ISNULL(SPO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Supplier_Purchase_Order ( -- _Product_Link - id_order, - -- active, - rank_order - ) - SELECT - SPO.id_order, - -- SPO.active, - RANK() OVER (ORDER BY SPO.id_order ASC) AS rank_order - FROM Shop_Supplier_Purchase_Order SPO - -- INNER JOIN Split_Temp TS ON SPO.id_order = TS.substring - INNER JOIN Shop_Supplier_Purchase_Order_Product_Link SPOPL ON SPO.id_order = SPOPL.id_order - INNER JOIN Shop_Supplier S ON SPO.id_supplier_ordered = S.id_supplier - INNER JOIN Shop_Product_Permutation PP ON SPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON SPOPL.id_permutation = t_P.id_permutation - LEFT JOIN tmp_Shop_Supplier t_S ON SPO.id_supplier_ordered = t_S.id_supplier - WHERE - -- supplier - ( - v_has_filter_supplier = FALSE - OR NOT ISNULL(t_S.id_supplier) -- SPO.id_supplier_ordered IN (SELECT DISTINCT id_supplier FROM tmp_Shop_Supplier) - ) - -- order - AND ( - ( - v_has_filter_order = FALSE - OR ( - -- ID - -- FIND_IN_SET(SPO.id_order, v_ids_order) > 0 - SPO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = FALSE - OR SPO.created_on > v_date_from - ) - AND ( - v_has_filter_date_to = FALSE - OR SPO.created_on < v_date_to - ) - ) - ) - ) - -- active - /* - AND ( - v_get_inactive_order - OR SPO.active = TRUE - ) - */ - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = FALSE - ) - OR NOT ISNULL(t_P.id_permutation) -- SPO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF v_get_first_order_only THEN - DELETE FROM tmp_Shop_Supplier_Purchase_Order t_SPO - WHERE t_SPO.rank_order > ( - SELECT MIN(t_SPO.rank_order) - FROM tmp_Shop_Supplier_Purchase_Order t_SPO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_supplier_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_SUPPLIER', 'STORE_SUPPLIER_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(DISTINCT t_P.id_product, ',') FROM tmp_Shop_Product t_P WHERE NOT ISNULL(t_P.id_product)); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, FALSE, v_ids_permission_supplier_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT STRING_AGG(name, ', ') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_supplier_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- Suppliers - OPEN result_suppliers FOR - SELECT - t_S.id_supplier, - S.name_company, - S.name_contact, - S.department_contact, - S.id_address, - S.phone_number, - S.fax, - S.email, - S.website, - S.id_currency, - t_S.active - FROM tmp_Shop_Supplier t_S - INNER JOIN Shop_Supplier S - ON t_S.id_supplier = S.id_supplier - ; - RETURN NEXT result_suppliers; - - -- Supplier Purchase Order - OPEN result_orders FOR - SELECT -- * - t_SPO.id_order, - SPO.id_supplier_ordered, - SPO.cost_total_local, - SPO.id_currency_cost, - t_SPO.active - FROM Shop_Supplier_Purchase_Order SPO - INNER JOIN tmp_Shop_Supplier_Purchase_Order t_SPO ON SPO.id_order = t_SPO.id_order - ; - RETURN NEXT result_orders; - - -- Supplier Purchase Order Product Link - OPEN result_order_product_links FOR - SELECT - SPOPL.id_link, - SPOPL.id_order, - SPOPL.id_permutation, - P.name as name_product, - SPOPL.cost_total_local, - SPOPL.id_currency_cost, - SPOPL.quantity_ordered, - SPOPL.id_unit_quantity, - SPOPL.quantity_received, - SPOPL.latency_delivery_days, - SPOPL.display_order - FROM Shop_Supplier_Purchase_Order_Product_Link SPOPL - -- INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL ON SPOPL.id_link = t_SPOPL.id_link - INNER JOIN tmp_Shop_Supplier_Purchase_Order t_SPO ON SPOPL.id_order = t_SPO.id_order - INNER JOIN Shop_Product_Permutation PP ON SPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY SPOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - */ - - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Supplier; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_supplier_purchase_order ( - '', -- a_id_user - TRUE, -- a_get_all_supplier - FALSE, -- a_get_inactive_supplier - FALSE, -- a_get_first_supplier_only - '', -- a_ids_supplier - TRUE, -- a_get_all_order - -- FALSE, -- a_get_inactive_order - FALSE, -- a_get_first_order_only - '', -- a_ids_order - FALSE, -- a_get_inactive_category - '', -- a_ids_category - FALSE, -- a_get_inactive_product - '', -- a_ids_product - FALSE, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_manufacturing_purchase_order ( - IN a_id_user INTEGER, - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - v_id_access_level_view INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - v_ids_permission_manufacturing_purchase_order VARCHAR(4000); - v_ids_product_permission INTEGER[]; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_order := COALESCE(a_get_all_order, TRUE); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_manufacturing_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'SHOP_manufacturing_PURCHASE_ORDER' LIMIT 1); - v_code_error_data = 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - - v_has_filter_order = CASE WHEN v_ids_order = '' THEN FALSE ELSE TRUE END; - v_has_filter_category = CASE WHEN v_ids_category = '' THEN FALSE ELSE TRUE END; - v_has_filter_product = CASE WHEN v_ids_product = '' THEN FALSE ELSE TRUE END; - v_has_filter_permutation = CASE WHEN v_ids_permutation = '' THEN FALSE ELSE TRUE END; - v_has_filter_date_from = CASE WHEN ISNULL(v_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to = CASE WHEN ISNULL(v_date_to) THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - /* - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Manufacturing_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - */ - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_manufacturing_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - -- name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - /* - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_category = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT COALESCE(STRING_AGG(Category_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_product = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT COALESCE(STRING_AGG(Product_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_permutation = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - ) - AND ( - v_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, v_ids_category) > 0 - ) - AND ( - v_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, v_ids_product) > 0 - ) - AND ( - v_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Manufacturing_Purchase_Order MPO ON Order_Id = MPO.id_order - WHERE ISNULL(MPO.id_order) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(Order_Id, ', ') - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Manufacturing_Purchase_Order MPO ON Order_Id = MPO.id_order - WHERE ISNULL(MPO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Manufacturing_Purchase_Order ( -- _Product_Link - id_order, - -- active, - rank_order - ) - SELECT - MPO.id_order, - -- MPO.active, - RANK() OVER (ORDER BY MPO.id_order ASC) AS rank_order - FROM Shop_Manufacturing_Purchase_Order MPO - -- INNER JOIN Split_Temp TS ON MPO.id_order = TS.substring - INNER JOIN Shop_manufacturing_Purchase_Order_Product_Link MPOPL ON MPO.id_order = MPOPL.id_order - INNER JOIN Shop_Product_Permutation PP ON MPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON MPOPL.id_permutation = t_P.id_permutation - WHERE - -- order - ( - ( - v_has_filter_order = 0 - OR ( - -- ID - -- FIND_IN_SET(MPO.id_order, v_ids_order) > 0 - MPO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = 0 - OR MPO.created_on > v_date_from - ) - AND ( - v_has_filter_date_to = 0 - OR MPO.created_on < v_date_to - ) - ) - ) - ) - -- active - /* - AND ( - v_get_inactive_order - OR MPO.active = TRUE - ) - */ - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = 0 - ) - OR NOT ISNULL(t_P.id_permutation) -- MPO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF v_get_first_order_only THEN - DELETE FROM tmp_Shop_Manufacturing_Purchase_Order t_MPO - WHERE t_MPO.rank_order > ( - SELECT MIN(t_MPO.rank_order) - FROM tmp_Shop_Manufacturing_Purchase_Order t_MPO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_manufacturing_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_manufacturing', 'STORE_manufacturing_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(P.id_product, ',') FROM (SELECT DISTINCT id_product FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)) P); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, FALSE, v_ids_permission_manufacturing_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT STRING_AGG(name, ', ') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_manufacturing_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Product; - - -- Returns - - -- manufacturing Purchase Order - OPEN result_orders FOR - SELECT -- * - t_MPO.id_order, - MPO.cost_total_local, - MPO.id_currency_cost, - MPO.value_produced_total_local, - t_MPO.active - FROM Shop_Manufacturing_Purchase_Order MPO - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order t_MPO ON MPO.id_order = t_MPO.id_order - ; - RETURN NEXT result_orders; - - -- manufacturing Purchase Order Product Link - OPEN result_order_product_links FOR - SELECT - MPOPL.id_link, - MPOPL.id_order, - MPOPL.id_permutation, - P.name as name_product, - MPOPL.cost_total_local, - MPOPL.id_currency_cost, - MPOPL.value_produced_total_local, - MPOPL.quantity_used, - MPOPL.id_unit_quantity, - MPOPL.quantity_produced, - MPOPL.latency_manufacture, - MPOPL.display_order - FROM Shop_manufacturing_Purchase_Order_Product_Link MPOPL - -- INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL ON MPOPL.id_link = t_MPOPL.id_link - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order t_MPO ON MPOPL.id_order = t_MPO.id_order - INNER JOIN Shop_Product_Permutation PP ON MPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY MPOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_manufacturing_purchase_order ( - '', -- a_id_user - TRUE, -- a_get_all_order - FALSE, -- a_get_inactive_order - FALSE, -- a_get_first_order_only - '', -- a_ids_order - FALSE, -- a_get_inactive_category - '', -- a_ids_category - FALSE, -- a_get_inactive_product - '', -- a_ids_product - FALSE, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_customer ( - IN a_id_user INTEGER, - IN a_get_all_customer BOOLEAN, - IN a_get_inactive_customer BOOLEAN, - IN a_get_first_customer_only BOOLEAN, - IN a_ids_customer INTEGER[] -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_customer BOOLEAN; - v_get_inactive_customer BOOLEAN; - v_get_first_customer_only BOOLEAN; - v_ids_customer INTEGER[]; - v_has_filter_customer BOOLEAN; - v_guid UUID; - v_id_permission_customer INTEGER; - v_id_access_level_view INTEGER; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - result_customers REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_inactive_customer := COALESCE(a_get_inactive_customer, FALSE); - v_get_first_customer_only := COALESCE(a_get_first_customer_only, FALSE); - v_ids_customer := TRIM(COALESCE(a_ids_customer, '')); - v_get_all_customer := COALESCE(a_get_all_customer, CASE WHEN v_ids_customer = '' THEN TRUE ELSE FALSE END); - - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - - v_has_filter_customer = CASE WHEN a_ids_customer = '' THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Customer; - - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - active BOOLEAN NOT NULL, - rank_customer INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_customer = TRUE OR a_get_all_customer = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) THEN - RAISE EXCEPTION 'Invalid customer IDs: %', ( - SELECT STRING_AGG(Customer_Id, ', ') - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Customer ( - id_customer, - active, - rank_customer - ) - SELECT - C.id_customer, - C.active, - RANK() OVER (ORDER BY C.id_customer ASC) AS rank_customer - FROM Shop_Customer C - LEFT JOIN Split_Temp S_T ON C.id_customer = S_T.substring - WHERE - ( - a_get_all_customer = 1 - OR NOT ISNULL(S_T.substring) - ) - AND ( - a_get_inactive_customer = 1 - OR C.active = TRUE - ) - ; - END IF; - - IF a_get_first_customer_only = TRUE THEN - DELETE FROM tmp_Shop_Customer t_C - WHERE t_C.rank_customer > ( - SELECT MIN(t_C.rank_customer) - FROM tmp_Shop_Customer t_C - ) - ; - END IF; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_customer := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER' LIMIT 1); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_id_permission_customer, v_id_access_level_view, ''); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT COALESCE(STRING_AGG(name, ', '), 'NULL') - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - ) - USING ERRCODE = '42501' - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- customers - OPEN result_customers FOR - SELECT - t_C.id_customer, - C.name_company, - C.name_contact, - C.department_contact, - C.id_address, - C.phone_number, - C.email, - C.id_currency, - C.active - FROM tmp_Shop_Customer t_C - INNER JOIN Shop_Customer C ON t_C.id_customer = C.id_customer - ; - RETURN NEXT result_customers; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Customer; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_customer ( - '', -- a_id_user - 1, -- a_get_all_customer - 0, -- a_get_inactive_customer - 0, -- a_get_first_customer_only - '' -- a_ids_customer - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_customer_sales_order ( - IN a_id_user INTEGER, - IN a_get_all_customer BOOLEAN, - IN a_get_inactive_customer BOOLEAN, - IN a_get_first_customer_only BOOLEAN, - IN a_ids_customer INTEGER[], - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_customer BOOLEAN; - v_get_inactive_customer BOOLEAN; - v_get_first_customer_only BOOLEAN; - v_ids_customer INTEGER[]; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - -- Argument redeclaration - -- Variable declaration - v_has_filter_customer BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - -- v_id_user VARCHAR(100); - -- v_ids_permutation_unavailable VARCHAR(4000); - v_ids_permission_customer_purchase_order VARCHAR(4000); - v_ids_product_permission VARCHAR(4000); - -- v_ids_permutation_permission VARCHAR(4000); - v_id_access_level_view INTEGER; - -- v_now TIMESTAMP; - -- v_id_minimum INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - result_customers REFCURSOR; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_inactive_customer := COALESCE(a_get_inactive_customer, FALSE); - v_get_first_customer_only := COALESCE(a_get_first_customer_only, FALSE); - v_ids_customer := TRIM(COALESCE(a_ids_customer, '')); - v_get_all_customer := COALESCE(a_get_all_customer, CASE WHEN v_ids_customer = '' THEN TRUE ELSE FALSE END); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_all_order := COALESCE(a_get_all_order, CASE WHEN v_ids_order = '' THEN TRUE ELSE FALSE END); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_customer_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'Shop_Customer_Sales_ORDER' LIMIT 1); - v_code_error_data := 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - - v_has_filter_category := CASE WHEN a_ids_category = '' THEN FALSE ELSE TRUE END; - v_has_filter_product := CASE WHEN a_ids_product = '' THEN FALSE ELSE TRUE END; - v_has_filter_permutation := CASE WHEN a_ids_permutation = '' THEN FALSE ELSE TRUE END; - v_has_filter_date_from := CASE WHEN ISNULL(a_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to := CASE WHEN ISNULL(a_date_to) THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order; - DROP TABLE IF EXISTS tmp_Shop_Customer; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - active BOOLEAN NOT NULL, - rank_customer INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TABLE tmp_Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - /* - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_Sales_Order_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - -- name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - /* - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_customer = TRUE OR a_get_all_customer = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid customer IDs: %', ( - SELECT STRING_AGG(Customer_Id, ', ') - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Customer ( - id_customer, - active, - rank_customer - ) - SELECT - C.id_customer, - C.active, - RANK() OVER (ORDER BY id_customer ASC) AS rank_customer - FROM Shop_Customer C - -- LEFT JOIN Split_Temp S_T ON C.id_customer = S_T.substring - WHERE - ( - a_get_all_customer = TRUE - -- OR NOT ISNULL(S_T.substring) - OR C.id_customer = ANY(v_ids_customer) - ) - AND ( - a_get_inactive_customer - OR C.active = TRUE - ) - ; - END IF; - - IF a_get_first_customer_only THEN - DELETE FROM tmp_Shop_Customer t_C - WHERE t_C.rank_customer > ( - SELECT MIN(t_C.rank_customer) - FROM tmp_Shop_Customer t_C - ) - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE AND EXISTS ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_product = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT COALESCE(STRING_AGG(Product_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_permutation = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, a_ids_permutation) > 0 - ) - AND ( - a_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, a_ids_category) > 0 - ) - AND ( - a_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, a_ids_product) > 0 - ) - AND ( - a_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Customer_Sales_Order CSO ON Order_Id = CSO.id_order - WHERE ISNULL(CSO.id_order) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(Order_Id, ', ') - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Customer_Sales_Order CSO ON Order_Id = CSO.id_order - WHERE ISNULL(CSO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Customer_Sales_Order ( -- _Product_Link - id_order, - active, - rank_order - ) - SELECT - CSO.id_order, - CSO.active, - RANK() OVER (ORDER BY CSO.id_order ASC) AS rank_order - FROM Shop_Customer_Sales_Order CSO - -- LEFT JOIN Split_Temp S_T ON CSO.id_order = S_T.substring - INNER JOIN Shop_Customer_Sales_Order_Product_Link CSOPL ON CSO.id_order = CSOPL.id_order - INNER JOIN Shop_Customer S ON CSO.id_customer = S.id_customer - INNER JOIN Shop_Product_Permutation PP ON CSOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON CSOPL.id_permutation = t_P.id_permutation - LEFT JOIN tmp_Shop_Customer t_S ON CSO.id_customer = t_S.id_customer - WHERE - -- customer - /* - ( - a_get_all_customer = 1 - OR NOT ISNULL(t_S.id_customer) -- CSO.id_customer IN (SELECT DISTINCT id_customer FROM tmp_Shop_Customer) - ) - */ - NOT ISNULL(t_S.id_customer) - -- order - AND ( - ( - a_get_all_order = 1 - OR ( - -- ID - -- FIND_IN_SET(CSO.id_order, a_ids_order) > 0 - -- NOT ISNULL(S_T.substring) - CSO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = 0 - OR CSO.created_on > a_date_from - ) - AND ( - v_has_filter_date_to = 0 - OR CSO.created_on < a_date_to - ) - ) - ) - ) - -- active - AND ( - a_get_inactive_order - OR CSO.active = TRUE - ) - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = 0 - ) - OR NOT ISNULL(t_P.id_permutation) -- CSO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF a_get_first_order_only THEN - DELETE FROM tmp_Shop_Customer_Sales_Order t_CSO - WHERE t_CSO.rank_order > ( - SELECT MIN(t_CSO.rank_order) - FROM tmp_Shop_Customer_Sales_Order t_CSO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_customer_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_customer', 'STORE_customer_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(P.id_product, ',') FROM (SELECT DISTINCT id_product FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)) P); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_ids_permission_customer_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT COALESCE(STRING_AGG(name, ', '), 'NULL') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_customer_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Customer; - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- customers - OPEN result_customers FOR - SELECT - t_S.id_customer, - S.name_company, - S.name_contact, - S.department_contact, - S.id_address, - S.phone_number, - S.email, - S.id_currency, - t_S.active - FROM tmp_Shop_Customer t_S - INNER JOIN Shop_Customer S - ON t_S.id_customer = S.id_customer - ; - RETURN NEXT result_customers; - - -- Customer Sales Order - OPEN result_orders FOR - SELECT -- * - t_CSO.id_order, - CSO.id_customer, - CSO.price_total_local, - CSO.id_currency_price, - t_CSO.active - FROM Shop_Customer_Sales_Order CSO - INNER JOIN tmp_Shop_Customer_Sales_Order t_CSO ON CSO.id_order = t_CSO.id_order - ; - RETURN NEXT result_orders; - - -- Customer Sales Order Product Link - OPEN result_order_product_links FOR - SELECT - CSOPL.id_link, - CSOPL.id_order, - CSOPL.id_permutation, - P.name as name_product, - CSOPL.price_total_local, - CSOPL.id_currency_price, - CSOPL.quantity_ordered, - CSOPL.id_unit_quantity, - CSOPL.quantity_delivered, - CSOPL.latency_delivery_days, - CSOPL.display_order - FROM Shop_Customer_Sales_Order_Product_Link CSOPL - -- INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL ON CSOPL.id_link = t_CSOPL.id_link - INNER JOIN tmp_Shop_Customer_Sales_Order t_CSO ON CSOPL.id_order = t_CSO.id_order - INNER JOIN Shop_Product_Permutation PP ON CSOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY CSOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order; - DROP TABLE IF EXISTS tmp_Shop_Customer; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_customer_sales_order ( - '', -- a_id_user - 1, -- a_get_all_customer - 0, -- a_get_inactive_customer - 0, -- a_get_first_customer_only - '', -- a_ids_customer - 1, -- a_get_all_order - 0, -- a_get_inactive_order - 0, -- a_get_first_order_only - '', -- a_ids_order - 0, -- a_get_inactive_category - '', -- a_ids_category - 0, -- a_get_inactive_product - '', -- a_ids_product - 0, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - - -DO $$ -BEGIN - RAISE NOTICE 'PROCEDURE CREATION COMPLETE'; -END $$; - -/* - -CALL p_populate_database () - -*/ - -/* --- Remove previous proc -DROP PROCEDURE IF EXISTS p_populate_database; - - -DELIMITER // -CREATE OR REPLACE PROCEDURE p_populate_database () -BEGIN -*/ - - --- Access Levels -INSERT INTO Shop_Access_Level ( - display_order, code, name, priority -) -VALUES - (1, 'VIEW', 'View', 3), - (2, 'EDIT', 'Edit', 2), - (3, 'ADMIN', 'Admin', 1) -; - --- Error Message Types -INSERT INTO Shop_Msg_Error_Type ( - code, name, description -) -VALUES - ('BAD_DATA', 'Invalid data', 'Rubbish data'), - ('NO_PERMISSION', 'No permission', 'Not authorised'), - ('PRODUCT_AVAILABILITY', 'Product not available', 'Product not available') -; - --- File Types -INSERT INTO File_Type ( - code, name, extension -) -VALUES - ('JPEG', 'Joint Photographic Export Group', 'jpg'), - ('PNG', 'Portable Network Graphic', 'png'), - ('GIF', 'GIF', 'gif'), - ('MPEG-4', 'Multimedia Photographic Export Group 4', 'mp4') -; - --- Generic / shared properties -INSERT INTO Shop_General ( - quantity_max -) -VALUES ( - 10 -); - --- Categories -INSERT INTO Shop_Product_Category ( - display_order, - code, - name, - description -) -VALUES - (1, 'ASS', 'Assistive Devices', 'Braille product line and other assistive devices'), - (99, 'MISC', 'Miscellaneous', 'Not category allocated products'), - (2, 'TECH', 'Technology', 'Technological devices') -; - --- Recurrence Interval -INSERT INTO Shop_Interval_Recurrence ( - code, name, name_plural -) -VALUES - ('WEEK', 'Week', 'Weeks'), - ('MONTH', 'Month', 'Months'), - ('YEAR', 'Year', 'Years') -; - -INSERT INTO Shop_Region ( - display_order, code, name -) -VALUES - (1, 'UK', 'United Kingdom') -; - -/* -INSERT INTO Shop_Region_Branch ( - display_order, id_region_parent, id_region_child -) -VALUES - (1, 1, 2) -; -*/ - --- Currency -INSERT INTO Shop_Currency ( - display_order, code, name, symbol, factor_from_GBP -) -VALUES - (1, 'GBP', 'Great British Pound', '£', 1), - (2, 'EUR', 'Euro', '€', 1.17) -; - --- Taxes and Surcharges -INSERT INTO Shop_Tax_Or_Surcharge ( - display_order, - code, - name, - id_region_buyer, - id_region_seller, - fixed_fee, - multiplier, - apply_fixed_fee_before_multiplier, - quantity_min, - quantity_max -) -VALUES - (1, 'VAT', 'Value Added Tax', 1, 1, 0, 0.2, TRUE, 0, 1) -; - --- Products -INSERT INTO Shop_Product ( - display_order, - id_category, - name, - has_variations, - id_access_level_required -) -VALUES - ( - 1, - 1, - 'Braille Keyboard Translator', - TRUE, - 3 - ), - ( - 2, - 2, - 'Test product 1', - FALSE, - 3 - ), - ( - 3, - 3, - 'Phone', - FALSE, - 1 - ), - ( - 4, - 3, - 'Laptop', - FALSE, - 1 - ), - ( - 5, - 3, - 'Smart Watch', - FALSE, - 1 - ) -; - --- Variation Types -INSERT INTO Shop_Variation_Type ( - display_order, code, name, name_plural -) -VALUES - (1, 'COLOUR', 'Colour', 'Colours') -; - --- Variations -INSERT INTO Shop_Variation ( - display_order, id_type, code, name -) -VALUES - (1, 1, 'RED', 'Red'), - (2, 1, 'BLUE', 'Blue') -; - --- Product Permutations -INSERT INTO Shop_Product_Permutation ( - display_order, - id_product, - description, - cost_local, - id_currency_cost, - profit_local_min, - -- id_currency_profit_min, - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - -- id_access_level_required, - id_stripe_product -) -VALUES - ( - 1, - 1, - 'Good Red', - 5, - 1, - 3, - -- 1, - 14, - 1, - 3, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 2, - 1, - 'Good Blue', - 6, - 1, - 4, - -- 1, - 14, - 1, - 3, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 3, - 2, - 'Test product describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 4, - 3, - 'Phone describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 5, - 4, - 'Laptop describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 6, - 5, - 'Smart watch describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ) -; - --- Product Permutation Variation Links -INSERT INTO Shop_Product_Permutation_Variation_Link ( - display_order, id_permutation, id_variation -) -VALUES - (1, 1, 1), - (2, 2, 2) -; - --- Product Currency Link -INSERT INTO Shop_Product_Currency_Region_Link ( - id_product, id_permutation, id_currency, id_region_purchase, price_local_VAT_incl, price_local_VAT_excl -) -VALUES - (1, 1, 1, 1, 24, 20), - (1, 1, 2, 1, 48, 40), - (1, 2, 1, 1, 96, 80), - (2, 3, 1, 1, 144, 120), - (3, 4, 1, 1, 600, 500), - (4, 5, 1, 1, 1500, 1200), - (5, 6, 1, 1, 180, 150) -; - -INSERT INTO Shop_Image_Type ( - display_order, code, name, name_plural -) -VALUES - (1, 'FULL', 'Full Quality Image', 'Full Quality Images'), - (2, 'LOW', 'Low Quality Image', 'Low Quality Images'), - (3, 'THUMBNAIL', 'Thumbnail Image', 'Thumbnail Images') -; - -INSERT INTO Shop_Image ( - display_order, id_product, id_permutation, id_type_image, id_type_file, url -) -VALUES - (1, 1, 1, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - -- (1, NULL, 1, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - (2, 1, 2, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - -- (1, NULL, 2, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg') - (3, 2, 3, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - (4, 3, 4, 1, 1, '/static/images/prod_.jpg'), - (5, 4, 5, 1, 1, '/static/images/prod_1.jpg'), - (6, 5, 6, 1, 1, '/static/images/prod_2.jpg') -; - -INSERT INTO Shop_Delivery_Option ( - display_order, code, name, latency_delivery_min, latency_delivery_max, quantity_min, quantity_max -) -VALUES - (1, 'COLLECT', 'Collection', 0, 0, 0, 1), - (2, 'SIGNED_1', 'First Class Signed-For', 2, 4, 0, 1) -; - -INSERT INTO Shop_Product_Permutation_Delivery_Option_Link ( - display_order, id_product, id_permutation, id_delivery_option, id_region, id_currency, price_local -) -VALUES - (1, 1, 1, 1, 1, 1, 5), - (2, 1, 2, 1, 1, 1, 9), - (3, 2, NULL, 1, 1, 1, 10), - (4, 3, 4, 1, 1, 1, 10), - (5, 4, 5, 1, 1, 1, 10), - (6, 5, 6, 1, 1, 1, 10) -; - --- Discounts -INSERT INTO Shop_Discount ( - id_product, - id_permutation, - code, - name, - multiplier, - quantity_min, - quantity_max, - date_start, - date_end, - display_order -) -VALUES - (1, 1, 'CRIMBO50', 'Christmas 50% off sale!', 0.5, 3, 9, CURRENT_TIMESTAMP, '2023-12-31 23:59:59', 1), - (1, 2, 'CRIMBO50', 'Christmas 50% off sale!', 0.5, 3, 9, CURRENT_TIMESTAMP, '2023-12-31 23:59:59', 1) -; - --- Discount Delivery Region Links -INSERT INTO Shop_Discount_Region_Currency_Link ( - id_discount, - id_region, - id_currency -) -VALUES - (1, 1, 1), - (2, 1, 1), - (1, 1, 2), - (2, 1, 2) -; - --- Permission Groups -INSERT INTO Shop_Permission_Group ( - display_order, code, name -) -VALUES - (0, 'ADMIN', 'Website Admin'), - (1, 'HOME', 'Home, Contact Us, and other public information'), - (2, 'PRODUCT', 'Store Products'), - (3, 'USER', 'Store User'), - (4, 'SALES_AND_PURCHASING', 'Sales and Purchasing'), - (5, 'MANUFACTURING', 'Manufacturing') -; - --- Permissions -INSERT INTO Shop_Permission ( - display_order, code, name, id_permission_group, id_access_level_required -) -VALUES - (1, 'HOME', 'Home Page', 2, 1), - (2, 'STORE_PRODUCT', 'Store Product Page', 3, 1), - (3, 'STORE_USER', 'Store User Account Page', 4, 2), - (4, 'STORE_ADMIN', 'Store Admin Page', 1, 3), - (5, 'STORE_SUPPLIER', 'Store Supplier Page', 4, 2), - (6, 'STORE_SUPPLIER_PURCHASE_ORDER', 'Store Supplier Purchase Order Page', 4, 2), - (7, 'STORE_MANUFACTURING_PURCHASE_ORDER', 'Store Manufacturing Purchase Order Page', 5, 2), - (8, 'STORE_CUSTOMER', 'Store Customer Page', 4, 2), - (9, 'STORE_CUSTOMER_SALES_ORDER', 'Store Customer Sales Order Page', 4, 2), - (99, 'CONTACT_US', 'Contact Us Page', 2, 1) -; - --- Roles -INSERT INTO Shop_Role ( - display_order, - code, - name -) -VALUES - (1, 'DIRECTOR', 'Director'), - (2, 'USER', 'User') -; - --- Role Permission link -INSERT INTO Shop_Role_Permission_Link ( - id_role, id_permission, id_access_level -) -VALUES - (1, 1, 3), - (1, 2, 3), - (1, 3, 3), - (1, 4, 3), - (1, 5, 3), - (2, 1, 1), - (2, 2, 1), - (2, 3, 1), - (2, 4, 1), - (2, 5, 1) -; - --- Users -INSERT INTO Shop_User ( - id_user_oauth, - name, - email, - -- is_email_verified, - is_super_user -) -VALUES - ('auth0|6582b95c895d09a70ba10fef', 'Teddy', 'edward.middletonsmith@gmail.com', TRUE), - ('parts_guest', 'Guest', '', FALSE) -; - --- User Role link -INSERT INTO Shop_User_Role_Link ( - id_user, id_role -) -VALUES - (1, 1) -; - --- Addresses -INSERT INTO Shop_Address ( - -- id_user, - id_region, name_full, phone_number, postcode, address_line_1, address_line_2, city, county -) -VALUES (1, 'Edward M-S', '07375 571430', 'CV22 6DN', '53 Alfred Green Close', '', 'Rugby', 'Warwickshire') -/* -SELECT U.id_user, 1, U.name, '07375 571430', 'CV22 6DN', '53 Alfred Green Close', '', 'Rugby', 'Warwickshire' - FROM Shop_User U -*/ -; - --- User Basket -INSERT INTO Shop_User_Basket ( - id_user, - id_product, - id_permutation, - quantity -) -VALUES - (1, 1, 1, 69) -; - --- User Order Status -INSERT INTO Shop_User_Order_Status ( - display_order, code, name, name_plural -) -VALUES - (1, 'SUCCESS', 'Success', 'Successes'), - (2, 'FAIL', 'Failure', 'Failures') -; - -/* --- User Order -INSERT INTO Shop_User_Order ( - id_user, value_total, id_order_status, id_checkout_session, id_currency -) -VALUES - (1, 25, 1, 'noods', 1), - (1, 25, 1, 'noods', 1) -; - --- User Order Product Link -INSERT INTO Shop_User_Order_Product_Link ( - id_order, id_product, id_permutation, quantity -) -VALUES - (1, 1, 1, 69), - (1, 2, NULL, 69), - (1, 1, 2, 69) -; -*/ - --- Supplier -INSERT INTO Shop_Supplier ( - name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency -) -VALUES - ('Precision And Research Technology Systems Limited', 'Teddy Middleton-Smith', 'Executive Management', 1, '07375571430', '', 'teddy@partsltd.co.uk', 'www.partsltd.co.uk', 1) -; - --- Unit of Measurement -INSERT INTO Shop_Unit_Measurement ( - name_singular, name_plural, symbol, is_base_unit -) -VALUES - ('metre', 'metres', 'm', TRUE), - ('kilogram', 'kilograms', 'kg', TRUE), - ('item', 'items', 'x', FALSE) -; - -/* --- Unit of Measurement Conversion -INSERT INTO Shop_Unit_Measurement_Conversion ( - id_unit_derived, id_unit_base, power_unit_base, multiplier_unit_base, increment_unit_base -) -VALUES - -; -*/ - -/* --- Supplier Purchase Order -INSERT INTO Shop_Supplier_Purchase_Order ( - id_supplier, value_total, id_order_status, id_checkout_session, id_currency -) -VALUES -; - --- Supplier Purchase Order Product Link -INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_ordered, id_unit_quantity, quantity_received, latency_delivery, display_order -) -VALUES -; -*/ - -/* --- Manufacturing Purchase Order -INSERT INTO Shop_Manufacturing_Purchase_Order ( - cost_total_local, id_currency_cost -) -VALUES -; - --- Manufacturing Purchase Order Product Link -INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_used, id_unit_quantity, quantity_produced, latency_manufacturing_days, display_order -) -VALUES -; -*/ - -/* --- Customer -INSERT INTO Shop_Customer ( - name_company, name_contact, department_contact, id_address, phone_number, email, id_currency -) -VALUES - -; -*/ - -/* --- Customer Sales Order -INSERT INTO Shop_Customer_Sales_Order ( - cost_total_local, id_currency_cost -) -VALUES -; - --- Customer Sales Order Product Link -INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_ordered, id_unit_quantity, quantity_delivered, latency_delivery_days, display_order -) -VALUES -; -*/ - - -/* - -- Clean up -END // -DELIMITER ; - - --- Call -CALL p_populate_database(); - --- Remove proc -DROP PROCEDURE IF EXISTS p_populate_database; -*/ - -DO $$ -BEGIN - RAISE NOTICE 'TABLE POPULATION COMPLETE'; -END $$; - --- Product Change Sets -SELECT * FROM Shop_Product_Change_Set; - --- User Change Sets -SELECT * FROM Shop_User_Change_Set; - --- Access Levels -SELECT * FROM Shop_Access_Level; -SELECT * FROM Shop_Access_Level_Audit; - --- Error Message type -SELECT * FROM Shop_Msg_Error_Type; - --- File Types -SELECT * FROM File_Type; -SELECT * FROM File_Type_Audit; - --- Generic / shared properties -SELECT * FROM Shop_General; -SELECT * FROM Shop_General_Audit; - --- Categories -SELECT * FROM Shop_Product_Category; -SELECT * FROM Shop_Product_Category_Audit; - --- Recurrence Interval -SELECT * FROM Shop_Interval_Recurrence; -SELECT * FROM Shop_Interval_Recurrence_Audit; - --- Region -SELECT * FROM Shop_Region; -SELECT * FROM Shop_Region_Audit; - --- Region Branch -SELECT * FROM Shop_Region_Branch; -SELECT * FROM Shop_Region_Branch_Audit; - --- Currency -SELECT * FROM Shop_Currency; -SELECT * FROM Shop_Currency_Audit; - --- Taxes and Surcharges -SELECT * FROM Shop_Tax_Or_Surcharge; -SELECT * FROM Shop_Tax_Or_Surcharge_Audit; - --- Products -SELECT * FROM Shop_Product; -SELECT * FROM Shop_Product_Audit; - --- Variation Types -SELECT * FROM Shop_Variation_Type; -SELECT * FROM Shop_Variation_Type_Audit; - --- Variations -SELECT * FROM Shop_Variation; -SELECT * FROM Shop_Variation_Audit; - --- Permutations -SELECT * FROM Shop_Product_Permutation; -SELECT * FROM Shop_Product_Permutation_Audit; - --- Permutation Variation Links -SELECT * FROM Shop_Product_Permutation_Variation_Link; -SELECT * FROM Shop_Product_Permutation_Variation_Link_Audit; - --- Product Currency Links -SELECT * FROM Shop_Product_Currency_Region_Link; -SELECT * FROM Shop_Product_Currency_Region_Link_Audit; - --- Image Types -SELECT * FROM Shop_Image_Type; -SELECT * FROM Shop_Image_Type_Audit; - --- Images -SELECT * FROM Shop_Image; -SELECT * FROM Shop_Image_Audit; - --- Delivery Option Types -SELECT * FROM Shop_Delivery_Option; -SELECT * FROM Shop_Delivery_Option_Audit; - --- Delivery Options -SELECT * FROM Shop_Product_Permutation_Delivery_Option_Link; -SELECT * FROM Shop_Product_Permutation_Delivery_Option_Link_Audit; - --- Discounts -SELECT * FROM Shop_Discount; -SELECT * FROM Shop_Discount_Audit; - --- Discount Delivery Region Links -SELECT * FROM Shop_Discount_Region_Currency_Link; -SELECT * FROM Shop_Discount_Region_Currency_Link_Audit; - - --- Permission Groups -SELECT * FROM Shop_Permission_Group; -SELECT * FROM Shop_Permission_Group_Audit; - --- Permissions -SELECT * FROM Shop_Permission; -SELECT * FROM Shop_Permission_Audit; - --- Roles -SELECT * FROM Shop_Role; -SELECT * FROM Shop_Role_Audit; - --- Role Permission link -SELECT * FROM Shop_Role_Permission_Link; -SELECT * FROM Shop_Role_Permission_Link_Audit; - --- Users -SELECT * FROM Shop_User; -SELECT * FROM Shop_User_Audit; - --- User Role link -SELECT * FROM Shop_User_Role_Link; -SELECT * FROM Shop_User_Role_Link_Audit; - - --- Addresses -SELECT * FROM Shop_Address; -SELECT * FROM Shop_Address_Audit; - --- Basket -SELECT * FROM Shop_User_Basket; -SELECT * FROM Shop_User_Basket_Audit; - --- Order Statuses -SELECT * FROM Shop_User_Order_Status; -SELECT * FROM Shop_User_Order_Status_Audit; - -/* --- Orders -SELECT * FROM Shop_User_Order; -SELECT * FROM Shop_User_Order_Audit; - --- Order Products -SELECT * FROM Shop_User_Order_Product_Link; -SELECT * FROM Shop_User_Order_Product_Link_Audit; -*/ - --- Supplier -SELECT * FROM Shop_Supplier; -SELECT * FROM Shop_Supplier_Audit; - --- Unit Of Measurement -SELECT * FROM Shop_Unit_Measurement; -SELECT * FROM Shop_Unit_Measurement_Audit; - --- Unit of Measurement Conversion -SELECT * FROM Shop_Unit_Measurement_Conversion; -SELECT * FROM Shop_Unit_Measurement_Conversion_Audit; - --- Supplier Purchase Order -SELECT * FROM Shop_Supplier_Purchase_Order; -SELECT * FROM Shop_Supplier_Purchase_Order_Audit; - --- Supplier Purchase Order Product Link -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link; -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; - --- Manufacturing Purchase Order -SELECT * FROM Shop_Manufacturing_Purchase_Order; -SELECT * FROM Shop_Manufacturing_Purchase_Order_Audit; - --- Manufacturing Purchase Order Product Link -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link; -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; - --- Customers -SELECT * FROM Shop_Customer; -SELECT * FROM Shop_Customer_Audit; - --- Customer Sales Order -SELECT * FROM Shop_Customer_Sales_Order; -SELECT * FROM Shop_Customer_Sales_Order_Audit; - --- Customer Sales Order Product Link -SELECT * FROM Shop_Customer_Sales_Order_Product_Link; -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Audit; - diff --git a/static/PostgreSQL/001_destroy.sql b/static/PostgreSQL/001_destroy.sql deleted file mode 100644 index da1807be..00000000 --- a/static/PostgreSQL/001_destroy.sql +++ /dev/null @@ -1,293 +0,0 @@ - -/* Clear Store DataBase */ - - - --- Drop dependencies -DROP TABLE IF EXISTS Shop_Calc_User_Temp; -DROP TABLE IF EXISTS tmp_Msg_Error; -DROP TABLE IF EXISTS tmp_Currency; -DROP TABLE IF EXISTS tmp_Delivery_Region; -DROP TABLE IF EXISTS tmp_Region; -DROP TABLE IF EXISTS tmp_Shop_User; -DROP TABLE IF EXISTS tmp_Shop_Order; -DROP TABLE IF EXISTS tmp_Shop_Product; -DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; -DROP TABLE IF EXISTS tmp_Shop_Image; -DROP TABLE IF EXISTS tmp_Shop_Variation; -DROP TABLE IF EXISTS tmp_Shop_Discount; -DROP TABLE IF EXISTS tmp_Discount; -DROP TABLE IF EXISTS tmp_Shop_Product_Category; -DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Region_Link; -DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; -DROP TABLE IF EXISTS tmp_User_Role_Link; -DROP TABLE IF EXISTS tmp_Shop_Basket; -DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; -DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; -DROP TABLE IF EXISTS tmp_Shop_Supplier; -DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; -DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; -DROP TABLE IF EXISTS tmp_Shop_Customer; - - - --- Delete old tables -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Customer_Sales_Order_Audit; -DROP TABLE IF EXISTS Shop_Customer_Sales_Order; - -DROP TABLE IF EXISTS Shop_Customer_Audit; -DROP TABLE IF EXISTS Shop_Customer; - -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order_Audit; -DROP TABLE IF EXISTS Shop_Manufacturing_Purchase_Order; - -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link_Temp; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order_Audit; -DROP TABLE IF EXISTS Shop_Supplier_Purchase_Order; - -DROP TABLE IF EXISTS Shop_Unit_Measurement_Conversion_Audit; -DROP TABLE IF EXISTS Shop_Unit_Measurement_Conversion; - -DROP TABLE IF EXISTS Shop_Unit_Measurement_Audit; -DROP TABLE IF EXISTS Shop_Unit_Measurement; - -DROP TABLE IF EXISTS Shop_Supplier_Audit; -DROP TABLE IF EXISTS Shop_Supplier; - -DROP TABLE IF EXISTS Shop_User_Order_Product_Link_Audit; -DROP TABLE IF EXISTS Shop_User_Order_Product_Link; - -DROP TABLE IF EXISTS Shop_User_Order_Audit; -DROP TABLE IF EXISTS Shop_User_Order; - -DROP TABLE IF EXISTS Shop_User_Order_Status_Audit; -DROP TABLE IF EXISTS Shop_User_Order_Status; - -DROP TABLE IF EXISTS Shop_User_Basket_Audit; -DROP TABLE IF EXISTS Shop_User_Basket; - -DROP TABLE IF EXISTS Shop_Address_Audit; -DROP TABLE IF EXISTS Shop_Address; - -DROP TABLE IF EXISTS Shop_User_Role_Link_Audit; -DROP TABLE IF EXISTS Shop_User_Role_Link; - -DROP TABLE IF EXISTS Shop_User_Audit; -DROP TABLE IF EXISTS Shop_User; - -DROP TABLE IF EXISTS Shop_Role_Permission_Link_Audit; -DROP TABLE IF EXISTS Shop_Role_Permission_Link; - -DROP TABLE IF EXISTS Shop_Role_Audit; -DROP TABLE IF EXISTS Shop_Role; - -DROP TABLE IF EXISTS Shop_Permission_Audit; -DROP TABLE IF EXISTS Shop_Permission; - -DROP TABLE IF EXISTS Shop_Permission_Group_Audit; -DROP TABLE IF EXISTS Shop_Permission_Group; - - -DROP TABLE IF EXISTS Shop_Discount_Region_Currency_Link_Audit; -DROP TABLE IF EXISTS Shop_Discount_Region_Currency_Link; - -DROP TABLE IF EXISTS Shop_Discount_Audit; -DROP TABLE IF EXISTS Shop_Discount; - -DROP TABLE IF EXISTS Shop_Product_Permutation_Delivery_Option_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation_Delivery_Option_Link; - -DROP TABLE IF EXISTS Shop_Delivery_Option_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Option; - -DROP TABLE IF EXISTS Shop_Image_Audit; -DROP TABLE IF EXISTS Shop_Image; - -DROP TABLE IF EXISTS Shop_Image_Type_Audit; -DROP TABLE IF EXISTS Shop_Image_Type; - -DROP TABLE IF EXISTS Shop_Product_Currency_Region_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Currency_Region_Link; -DROP TABLE IF EXISTS Shop_Product_Currency_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Currency_Link; - -DROP TABLE IF EXISTS Shop_Product_Variation_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Variation_Link; -DROP TABLE IF EXISTS Shop_Product_Permutation_Variation_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation_Variation_Link; - -DROP TABLE IF EXISTS Shop_Product_Permutation_Audit; -DROP TABLE IF EXISTS Shop_Product_Permutation; - -DROP TABLE IF EXISTS Shop_Variation_Audit; -DROP TABLE IF EXISTS Shop_Variation; -DROP TABLE IF EXISTS Shop_Product_Variation_Type_Link_Audit; -DROP TABLE IF EXISTS Shop_Product_Variation_Type_Link; - -DROP TABLE IF EXISTS Shop_Variation_Type_Audit; -DROP TABLE IF EXISTS Shop_Variation_Type; - -DROP TABLE IF EXISTS Shop_Product_Audit; -DROP TABLE IF EXISTS Shop_Product; - -DROP TABLE IF EXISTS Shop_Tax_Or_Surcharge_Audit; -DROP TABLE IF EXISTS Shop_Tax_Or_Surcharge; - -DROP TABLE IF EXISTS Shop_Currency_Audit; -DROP TABLE IF EXISTS Shop_Currency; - -DROP TABLE IF EXISTS Shop_Delivery_Region_Branch_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Region_Branch; -DROP TABLE IF EXISTS Shop_Region_Branch_Audit; -DROP TABLE IF EXISTS Shop_Region_Branch; - -DROP TABLE IF EXISTS Shop_Delivery_Region_Audit; -DROP TABLE IF EXISTS Shop_Delivery_Region; -DROP TABLE IF EXISTS Shop_Region_Audit; -DROP TABLE IF EXISTS Shop_Region; - -DROP TABLE IF EXISTS Shop_Interval_Recurrence_Audit; -DROP TABLE IF EXISTS Shop_Interval_Recurrence; - -DROP TABLE IF EXISTS Shop_Product_Category_Audit; -DROP TABLE IF EXISTS Shop_Product_Category; - -DROP TABLE IF EXISTS Shop_General_Audit; -DROP TABLE IF EXISTS Shop_General; - -DROP TABLE IF EXISTS File_Type_Audit; -DROP TABLE IF EXISTS File_Type; - -DROP TABLE IF EXISTS Msg_Error_Type; - -DROP TABLE IF EXISTS Shop_Access_Level_Audit; -DROP TABLE IF EXISTS Shop_Access_Level; - -DROP TABLE IF EXISTS Shop_Sales_And_Purchasing_Change_Set; -DROP TABLE IF EXISTS Shop_User_Change_Set; - -DROP TABLE IF EXISTS Shop_Msg_Error_Type; - -DROP TABLE IF EXISTS Shop_Product_Change_Set; - -DO $$ -BEGIN - RAISE NOTICE 'TABLE DELETION COMPLETE'; -END $$; - - -DROP FUNCTION IF EXISTS fn_shop_user_eval; -DROP FUNCTION IF EXISTS p_shop_calc_user; -DROP PROCEDURE IF EXISTS fn_shop_user_eval; -DROP PROCEDURE IF EXISTS p_shop_calc_user; - -DROP FUNCTION IF EXISTS fn_shop_save_product; -DROP FUNCTION IF EXISTS p_shop_save_product; -DROP PROCEDURE IF EXISTS fn_shop_save_product; -DROP PROCEDURE IF EXISTS p_shop_save_product; - -DROP FUNCTION IF EXISTS fn_shop_save_supplier; -DROP FUNCTION IF EXISTS p_shop_save_supplier; -DROP PROCEDURE IF EXISTS fn_shop_save_supplier; -DROP PROCEDURE IF EXISTS p_shop_save_supplier; - -DROP FUNCTION IF EXISTS fn_shop_save_supplier_purchase_order; -DROP FUNCTION IF EXISTS p_shop_save_supplier_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_save_supplier_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_save_supplier_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_save_manufacturing_purchase_order; -DROP FUNCTION IF EXISTS p_shop_save_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_save_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_save_manufacturing_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_save_customer; -DROP FUNCTION IF EXISTS p_shop_save_customer; -DROP PROCEDURE IF EXISTS fn_shop_save_customer; -DROP PROCEDURE IF EXISTS p_shop_save_customer; - -DROP FUNCTION IF EXISTS fn_shop_save_customer_sales_order; -DROP FUNCTION IF EXISTS p_shop_save_customer_sales_order; -DROP PROCEDURE IF EXISTS fn_shop_save_customer_sales_order; -DROP PROCEDURE IF EXISTS p_shop_save_customer_sales_order; - -DROP FUNCTION IF EXISTS fn_shop_save_user; -DROP FUNCTION IF EXISTS p_shop_save_user; -DROP PROCEDURE IF EXISTS fn_shop_save_user; -DROP PROCEDURE IF EXISTS p_shop_save_user; - -DROP FUNCTION IF EXISTS fn_shop_save_user_basket; -DROP FUNCTION IF EXISTS p_shop_save_user_basket; -DROP PROCEDURE IF EXISTS fn_shop_save_user_basket; -DROP PROCEDURE IF EXISTS p_shop_save_user_basket; - -DROP FUNCTION IF EXISTS fn_shop_get_many_product; -DROP FUNCTION IF EXISTS p_shop_get_many_product; -DROP PROCEDURE IF EXISTS fn_shop_get_many_product; -DROP PROCEDURE IF EXISTS p_shop_get_many_product; - -DROP FUNCTION IF EXISTS fn_shop_get_many_role_permission; -DROP FUNCTION IF EXISTS p_shop_get_many_role_permission; -DROP PROCEDURE IF EXISTS fn_shop_get_many_role_permission; -DROP PROCEDURE IF EXISTS p_shop_get_many_role_permission; - -DROP FUNCTION IF EXISTS fn_shop_get_many_currency; -DROP FUNCTION IF EXISTS p_shop_get_many_currency; -DROP PROCEDURE IF EXISTS fn_shop_get_many_currency; -DROP PROCEDURE IF EXISTS p_shop_get_many_currency; - -DROP FUNCTION IF EXISTS fn_shop_get_many_region; -DROP FUNCTION IF EXISTS p_shop_get_many_region; -DROP PROCEDURE IF EXISTS fn_shop_get_many_region; -DROP PROCEDURE IF EXISTS p_shop_get_many_region; - -DROP FUNCTION IF EXISTS fn_shop_get_many_user_order; -DROP FUNCTION IF EXISTS p_shop_get_many_user_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_user_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_user_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_stripe_product_new; -DROP FUNCTION IF EXISTS p_shop_get_many_stripe_product_new; -DROP PROCEDURE IF EXISTS fn_shop_get_many_stripe_product_new; -DROP PROCEDURE IF EXISTS p_shop_get_many_stripe_product_new; - -DROP FUNCTION IF EXISTS fn_shop_get_many_stripe_price_new; -DROP FUNCTION IF EXISTS p_shop_get_many_stripe_price_new; -DROP PROCEDURE IF EXISTS fn_shop_get_many_stripe_price_new; -DROP PROCEDURE IF EXISTS p_shop_get_many_stripe_price_new; - -DROP FUNCTION IF EXISTS fn_shop_get_many_supplier; -DROP FUNCTION IF EXISTS p_shop_get_many_supplier; -DROP PROCEDURE IF EXISTS fn_shop_get_many_supplier; -DROP PROCEDURE IF EXISTS p_shop_get_many_supplier; - -DROP FUNCTION IF EXISTS fn_shop_get_many_supplier_purchase_order; -DROP FUNCTION IF EXISTS p_shop_get_many_supplier_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_supplier_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_supplier_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_manufacturing_purchase_order; -DROP FUNCTION IF EXISTS p_shop_get_many_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_manufacturing_purchase_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_manufacturing_purchase_order; - -DROP FUNCTION IF EXISTS fn_shop_get_many_customer; -DROP FUNCTION IF EXISTS p_shop_get_many_customer; -DROP PROCEDURE IF EXISTS fn_shop_get_many_customer; -DROP PROCEDURE IF EXISTS p_shop_get_many_customer; - -DROP FUNCTION IF EXISTS fn_shop_get_many_customer_sales_order; -DROP FUNCTION IF EXISTS p_shop_get_many_customer_sales_order; -DROP PROCEDURE IF EXISTS fn_shop_get_many_customer_sales_order; -DROP PROCEDURE IF EXISTS p_shop_get_many_customer_sales_order; \ No newline at end of file diff --git a/static/PostgreSQL/100.0_tbl_Shop_Product_Change_Set.sql b/static/PostgreSQL/100.0_tbl_Shop_Product_Change_Set.sql deleted file mode 100644 index b1dcc507..00000000 --- a/static/PostgreSQL/100.0_tbl_Shop_Product_Change_Set.sql +++ /dev/null @@ -1,13 +0,0 @@ - --- Product Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Change_Set'; - -CREATE TABLE Shop_Product_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); \ No newline at end of file diff --git a/static/PostgreSQL/100.1_tbl_Shop_User_Change_Set.sql b/static/PostgreSQL/100.1_tbl_Shop_User_Change_Set.sql deleted file mode 100644 index ed444f86..00000000 --- a/static/PostgreSQL/100.1_tbl_Shop_User_Change_Set.sql +++ /dev/null @@ -1,13 +0,0 @@ - --- User Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Change_Set'; - -CREATE TABLE IF NOT EXISTS Shop_User_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); \ No newline at end of file diff --git a/static/PostgreSQL/100.2_tbl_Shop_Access_Level.sql b/static/PostgreSQL/100.2_tbl_Shop_Access_Level.sql deleted file mode 100644 index 4921d6d2..00000000 --- a/static/PostgreSQL/100.2_tbl_Shop_Access_Level.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Access Levels - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Access_Level'; - -CREATE TABLE IF NOT EXISTS Shop_Access_Level ( - id_access_level INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - priority INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Access_Level_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/100.2_tbl_Shop_Sales_And_Purchasing_Change_Set.sql b/static/PostgreSQL/100.2_tbl_Shop_Sales_And_Purchasing_Change_Set.sql deleted file mode 100644 index 754d2a92..00000000 --- a/static/PostgreSQL/100.2_tbl_Shop_Sales_And_Purchasing_Change_Set.sql +++ /dev/null @@ -1,13 +0,0 @@ - --- Sales And Purchasing Change Sets - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Sales_And_Purchasing_Change_Set'; - -CREATE TABLE Shop_Sales_And_Purchasing_Change_Set ( - id_change_set INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - comment VARCHAR(4000), - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); \ No newline at end of file diff --git a/static/PostgreSQL/100.3_tbl_Shop_Access_Level_Audit.sql b/static/PostgreSQL/100.3_tbl_Shop_Access_Level_Audit.sql deleted file mode 100644 index 930b0167..00000000 --- a/static/PostgreSQL/100.3_tbl_Shop_Access_Level_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Access Level Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Access_Level_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Access_Level_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_access_level INTEGER NOT NULL, - CONSTRAINT FK_Shop_Access_Level_Audit_id_access_level - FOREIGN KEY (id_access_level) - REFERENCES Shop_Access_Level(id_access_level) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Access_Level_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/100_tbl_Msg_Error_Type.sql b/static/PostgreSQL/100_tbl_Msg_Error_Type.sql deleted file mode 100644 index e5268d39..00000000 --- a/static/PostgreSQL/100_tbl_Msg_Error_Type.sql +++ /dev/null @@ -1,13 +0,0 @@ - --- Error Message Type - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Msg_Error_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Msg_Error_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - description VARCHAR(1000) -); diff --git a/static/PostgreSQL/102_tbl_File_Type.sql b/static/PostgreSQL/102_tbl_File_Type.sql deleted file mode 100644 index 24823251..00000000 --- a/static/PostgreSQL/102_tbl_File_Type.sql +++ /dev/null @@ -1,17 +0,0 @@ - --- File Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'File_Type'; - -CREATE TABLE IF NOT EXISTS File_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(100), - extension VARCHAR(50), - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); diff --git a/static/PostgreSQL/103_tbl_File_Type_Audit.sql b/static/PostgreSQL/103_tbl_File_Type_Audit.sql deleted file mode 100644 index 895a3f45..00000000 --- a/static/PostgreSQL/103_tbl_File_Type_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- File Type Audit - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'File_Type_Audit'; - -CREATE TABLE IF NOT EXISTS File_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_File_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES File_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP, - updated_last_by VARCHAR(100) -); \ No newline at end of file diff --git a/static/PostgreSQL/104_tbl_Shop_General.sql b/static/PostgreSQL/104_tbl_Shop_General.sql deleted file mode 100644 index 087e8209..00000000 --- a/static/PostgreSQL/104_tbl_Shop_General.sql +++ /dev/null @@ -1,17 +0,0 @@ - --- Generic / shared properties - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_General'; - -CREATE TABLE IF NOT EXISTS Shop_General ( - id_general INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - quantity_max REAL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT CHK_Shop_General_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/105_tbl_Shop_General_Audit.sql b/static/PostgreSQL/105_tbl_Shop_General_Audit.sql deleted file mode 100644 index 36e5a3a4..00000000 --- a/static/PostgreSQL/105_tbl_Shop_General_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Shop General Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_General_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_General_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_general INTEGER NOT NULL, - CONSTRAINT FK_Shop_General_Audit_id_general - FOREIGN KEY (id_general) - REFERENCES Shop_General(id_general) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_General_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/106_tbl_Shop_Category.sql b/static/PostgreSQL/106_tbl_Shop_Category.sql deleted file mode 100644 index 28e3e698..00000000 --- a/static/PostgreSQL/106_tbl_Shop_Category.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Categories - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Category'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Category ( - id_category INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - description VARCHAR(4000), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Category_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/107_tbl_Shop_Category_Audit.sql b/static/PostgreSQL/107_tbl_Shop_Category_Audit.sql deleted file mode 100644 index ba0ec8be..00000000 --- a/static/PostgreSQL/107_tbl_Shop_Category_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Category Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Category_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Category_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_category INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Category_Audit_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Category_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/108_tbl_Shop_Recurrence_Interval.sql b/static/PostgreSQL/108_tbl_Shop_Recurrence_Interval.sql deleted file mode 100644 index 4075a57e..00000000 --- a/static/PostgreSQL/108_tbl_Shop_Recurrence_Interval.sql +++ /dev/null @@ -1,20 +0,0 @@ - --- Recurrence Interval - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Interval_Recurrence'; - -CREATE TABLE IF NOT EXISTS Shop_Interval_Recurrence ( - id_interval INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Interval_Recurrence_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/109_tbl_Shop_Recurrence_Interval_Audit.sql b/static/PostgreSQL/109_tbl_Shop_Recurrence_Interval_Audit.sql deleted file mode 100644 index 8ca9184b..00000000 --- a/static/PostgreSQL/109_tbl_Shop_Recurrence_Interval_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Recurrence Interval Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Interval_Recurrence_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Interval_Recurrence_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_interval INTEGER NOT NULL, - CONSTRAINT FK_Shop_Interval_Recurrence_Audit_id_interval - FOREIGN KEY (id_interval) - REFERENCES Shop_Interval_Recurrence(id_interval) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Interval_Recurrence_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/110.0_tbl_Shop_Region.sql b/static/PostgreSQL/110.0_tbl_Shop_Region.sql deleted file mode 100644 index c5246629..00000000 --- a/static/PostgreSQL/110.0_tbl_Shop_Region.sql +++ /dev/null @@ -1,20 +0,0 @@ - --- Regions - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region'; - -CREATE TABLE IF NOT EXISTS Shop_Region ( - id_region INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Region_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/110.1_tbl_Shop_Region_Audit.sql b/static/PostgreSQL/110.1_tbl_Shop_Region_Audit.sql deleted file mode 100644 index 07a36839..00000000 --- a/static/PostgreSQL/110.1_tbl_Shop_Region_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Region Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Audit_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/110.2_tbl_Shop_Region_Branch.sql b/static/PostgreSQL/110.2_tbl_Shop_Region_Branch.sql deleted file mode 100644 index 42e1ae32..00000000 --- a/static/PostgreSQL/110.2_tbl_Shop_Region_Branch.sql +++ /dev/null @@ -1,29 +0,0 @@ - --- Region Branchs - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Branch'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Branch ( - id_branch INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_region_parent INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_id_region_parent - FOREIGN KEY (id_region_parent) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_region_child INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_id_region_child - FOREIGN KEY (id_region_child) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - -- depth INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Region_Branch_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/110.3_tbl_Shop_Region_Branch_Audit.sql b/static/PostgreSQL/110.3_tbl_Shop_Region_Branch_Audit.sql deleted file mode 100644 index fed4446a..00000000 --- a/static/PostgreSQL/110.3_tbl_Shop_Region_Branch_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Region Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Region_Branch_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Region_Branch_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_branch INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_Audit_id_branch - FOREIGN KEY (id_branch) - REFERENCES Shop_Region_Branch(id_branch) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Region_Branch_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/110.4_tbl_Shop_Currency.sql b/static/PostgreSQL/110.4_tbl_Shop_Currency.sql deleted file mode 100644 index 5e3b6530..00000000 --- a/static/PostgreSQL/110.4_tbl_Shop_Currency.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Currencies - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Currency'; - -CREATE TABLE IF NOT EXISTS Shop_Currency ( - id_currency INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(255) NOT NULL, - symbol VARCHAR(1) NOT NULL, - factor_from_GBP REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Currency_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/110.5_tbl_Shop_Currency_Audit.sql b/static/PostgreSQL/110.5_tbl_Shop_Currency_Audit.sql deleted file mode 100644 index cb68b4ae..00000000 --- a/static/PostgreSQL/110.5_tbl_Shop_Currency_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Currency Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Currency_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Currency_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Currency_Audit_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Currency_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/110.6_tbl_Shop_Tax_Or_Surcharge.sql b/static/PostgreSQL/110.6_tbl_Shop_Tax_Or_Surcharge.sql deleted file mode 100644 index 77ba8f90..00000000 --- a/static/PostgreSQL/110.6_tbl_Shop_Tax_Or_Surcharge.sql +++ /dev/null @@ -1,38 +0,0 @@ - --- Taxes and Surcharges - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Tax_Or_Surcharge'; - -CREATE TABLE Shop_Tax_Or_Surcharge ( - id_tax INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - id_region_buyer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_region_buyer - FOREIGN KEY (id_region_buyer) - REFERENCES Shop_Region(id_region), - id_region_seller INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_region_seller - FOREIGN KEY (id_region_seller) - REFERENCES Shop_Region(id_region), - id_currency INTEGER, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - fixed_fee REAL NOT NULL DEFAULT 0, - multiplier REAL NOT NULL DEFAULT 1 CHECK (multiplier > 0), - apply_fixed_fee_before_multiplier BOOLEAN NOT NULL DEFAULT TRUE, - quantity_min REAL NOT NULL DEFAULT 0, - quantity_max REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql b/static/PostgreSQL/110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql deleted file mode 100644 index c753c207..00000000 --- a/static/PostgreSQL/110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Tax Or Surcharge Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Tax_Or_Surcharge_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Tax_Or_Surcharge_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_tax INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_Audit_id_discount - FOREIGN KEY (id_tax) - REFERENCES Shop_Tax_Or_Surcharge(id_tax) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Tax_Or_Surcharge_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/110.8_tbl_Shop_Product.sql b/static/PostgreSQL/110.8_tbl_Shop_Product.sql deleted file mode 100644 index ed44d554..00000000 --- a/static/PostgreSQL/110.8_tbl_Shop_Product.sql +++ /dev/null @@ -1,48 +0,0 @@ - --- Products - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product'; - -CREATE TABLE IF NOT EXISTS Shop_Product ( - id_product INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000), - id_category INTEGER NOT NULL, - has_variations BOOLEAN NOT NULL, - /* - price_GBP_full REAL, - price_GBP_min REAL, - -- ratio_discount_overall REAL NOT NULL DEFAULT 0, - CONSTRAINT FK_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category) - ON UPDATE RESTRICT, - latency_manuf INTEGER, - quantity_min REAL, - quantity_max REAL, - quantity_step REAL, - quantity_stock REAL, - is_subscription BOOLEAN, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - */ - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - -- id_stripe_product VARCHAR(100), - -- id_stripe_price VARCHAR(100) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/110.9_tbl_Shop_Product_Audit.sql b/static/PostgreSQL/110.9_tbl_Shop_Product_Audit.sql deleted file mode 100644 index 932c2b0d..00000000 --- a/static/PostgreSQL/110.9_tbl_Shop_Product_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Products - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Audit_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/112_tbl_Shop_Variation_Type.sql b/static/PostgreSQL/112_tbl_Shop_Variation_Type.sql deleted file mode 100644 index 8a77d963..00000000 --- a/static/PostgreSQL/112_tbl_Shop_Variation_Type.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Variation Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Variation_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/113.0_tbl_Shop_Variation_Type_Audit.sql b/static/PostgreSQL/113.0_tbl_Shop_Variation_Type_Audit.sql deleted file mode 100644 index 1ae8c678..00000000 --- a/static/PostgreSQL/113.0_tbl_Shop_Variation_Type_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Variation Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Type_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Variation_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Type_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/114_tbl_Shop_Variation.sql b/static/PostgreSQL/114_tbl_Shop_Variation.sql deleted file mode 100644 index 8f0ae1ae..00000000 --- a/static/PostgreSQL/114_tbl_Shop_Variation.sql +++ /dev/null @@ -1,25 +0,0 @@ - --- Variations - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation'; - -CREATE TABLE Shop_Variation ( - id_variation INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Variation_Type(id_type) - ON UPDATE RESTRICT, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Variation_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/115_tbl_Shop_Variation_Audit.sql b/static/PostgreSQL/115_tbl_Shop_Variation_Audit.sql deleted file mode 100644 index f439d2b8..00000000 --- a/static/PostgreSQL/115_tbl_Shop_Variation_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Variation Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Variation_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Variation_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_variation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Audit_id_variation - FOREIGN KEY (id_variation) - REFERENCES Shop_Variation(id_variation) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Variation_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/117.1_tbl_Shop_Product_Permutation.sql b/static/PostgreSQL/117.1_tbl_Shop_Product_Permutation.sql deleted file mode 100644 index b7a0ebd8..00000000 --- a/static/PostgreSQL/117.1_tbl_Shop_Product_Permutation.sql +++ /dev/null @@ -1,47 +0,0 @@ - --- Product Permutation - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation ( - id_permutation INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Variation_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - -- name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - cost_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - profit_local_min REAL NOT NULL, - -- id_currency_profit_min INTEGER NOT NULL, - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - /* - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - */ - id_stripe_product VARCHAR(100) NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Variation_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/117.2_tbl_Shop_Product_Permutation_Audit.sql b/static/PostgreSQL/117.2_tbl_Shop_Product_Permutation_Audit.sql deleted file mode 100644 index d4d4c443..00000000 --- a/static/PostgreSQL/117.2_tbl_Shop_Product_Permutation_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Product Permutation Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Audit_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); diff --git a/static/PostgreSQL/117.3_tbl_Shop_Product_Permutation_Variation_Link.sql b/static/PostgreSQL/117.3_tbl_Shop_Product_Permutation_Variation_Link.sql deleted file mode 100644 index 50b0e03c..00000000 --- a/static/PostgreSQL/117.3_tbl_Shop_Product_Permutation_Variation_Link.sql +++ /dev/null @@ -1,28 +0,0 @@ - --- Product Permutation Variation Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Variation_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Variation_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_variation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_variation - FOREIGN KEY (id_variation) - REFERENCES Shop_Variation(id_variation) - ON UPDATE RESTRICT, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/117.4_tbl_Shop_Product_Permutation_Variation_Link_Audit.sql b/static/PostgreSQL/117.4_tbl_Shop_Product_Permutation_Variation_Link_Audit.sql deleted file mode 100644 index 3cce2d5c..00000000 --- a/static/PostgreSQL/117.4_tbl_Shop_Product_Permutation_Variation_Link_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Product Permutation Variation Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Variation_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Variation_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Permutation_Variation_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Variation_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/117.5_tbl_Shop_Product_Currency_Region_Link.sql b/static/PostgreSQL/117.5_tbl_Shop_Product_Currency_Region_Link.sql deleted file mode 100644 index 0055dc9d..00000000 --- a/static/PostgreSQL/117.5_tbl_Shop_Product_Currency_Region_Link.sql +++ /dev/null @@ -1,40 +0,0 @@ - --- Product Currency Region link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Currency_Region_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Currency_Region_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - id_region_purchase INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_region_purchase - FOREIGN KEY (id_region_purchase) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - price_local_VAT_incl REAL NULL, - price_local_VAT_excl REAL NULL, - id_stripe_price VARCHAR(200), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/117.6_tbl_Shop_Product_Currency_Region_Link_Audit.sql b/static/PostgreSQL/117.6_tbl_Shop_Product_Currency_Region_Link_Audit.sql deleted file mode 100644 index 2aa7d47b..00000000 --- a/static/PostgreSQL/117.6_tbl_Shop_Product_Currency_Region_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Product Currency Region Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Currency_Region_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Currency_Region_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Currency_Region_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Currency_Region_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/118_tbl_Shop_Image_Type.sql b/static/PostgreSQL/118_tbl_Shop_Image_Type.sql deleted file mode 100644 index 4a8a8d71..00000000 --- a/static/PostgreSQL/118_tbl_Shop_Image_Type.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Image Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Type'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Type ( - id_type INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Image_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/119_tbl_Shop_Image_Type_Audit.sql b/static/PostgreSQL/119_tbl_Shop_Image_Type_Audit.sql deleted file mode 100644 index bebb9ccd..00000000 --- a/static/PostgreSQL/119_tbl_Shop_Image_Type_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Image Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Type_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Type_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Type_Audit_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Image_Type(id_type) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Type_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/120_tbl_Shop_Image.sql b/static/PostgreSQL/120_tbl_Shop_Image.sql deleted file mode 100644 index acf18eba..00000000 --- a/static/PostgreSQL/120_tbl_Shop_Image.sql +++ /dev/null @@ -1,35 +0,0 @@ - --- Images - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image'; - -CREATE TABLE IF NOT EXISTS Shop_Image ( - id_image INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type_image INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_id_type_image - FOREIGN KEY (id_type_image) - REFERENCES Shop_Image_Type(id_type), - id_type_file INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_id_type_file - FOREIGN KEY (id_type_file) - REFERENCES File_Type(id_type), - id_product INTEGER NULL, - CONSTRAINT FK_Shop_Image_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Image_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - url VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Image_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/121_tbl_Shop_Image_Audit.sql b/static/PostgreSQL/121_tbl_Shop_Image_Audit.sql deleted file mode 100644 index 23060b27..00000000 --- a/static/PostgreSQL/121_tbl_Shop_Image_Audit.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Image Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Image_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Image_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_image INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Audit_id_image - FOREIGN KEY (id_image) - REFERENCES Shop_Image(id_image), - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Image_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/122_tbl_Shop_Delivery_Option.sql b/static/PostgreSQL/122_tbl_Shop_Delivery_Option.sql deleted file mode 100644 index d4241ff4..00000000 --- a/static/PostgreSQL/122_tbl_Shop_Delivery_Option.sql +++ /dev/null @@ -1,25 +0,0 @@ - --- Delivery Options - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Delivery_Option'; - -CREATE TABLE IF NOT EXISTS Shop_Delivery_Option ( - id_option INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(100) NOT NULL, - description VARCHAR(4000), - latency_delivery_min INTEGER NOT NULL, - latency_delivery_max INTEGER NOT NULL, - quantity_min INTEGER NOT NULL, - quantity_max INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Delivery_Option_Type_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/123_tbl_Shop_Delivery_Option_Audit.sql b/static/PostgreSQL/123_tbl_Shop_Delivery_Option_Audit.sql deleted file mode 100644 index 5c0a8e06..00000000 --- a/static/PostgreSQL/123_tbl_Shop_Delivery_Option_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Delivery Option Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Delivery_Option_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Delivery_Option_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_option INTEGER NOT NULL, - CONSTRAINT FK_Shop_Delivery_Option_Audit_id_option - FOREIGN KEY (id_option) - REFERENCES Shop_Delivery_Option(id_option) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(4000), - value_new VARCHAR(4000), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Delivery_Option_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/124_tbl_Shop_Product_Delivery_Option_Link.sql b/static/PostgreSQL/124_tbl_Shop_Product_Delivery_Option_Link.sql deleted file mode 100644 index 3f5b9952..00000000 --- a/static/PostgreSQL/124_tbl_Shop_Product_Delivery_Option_Link.sql +++ /dev/null @@ -1,44 +0,0 @@ - --- Delivery Option - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Delivery_Option_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Delivery_Option_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - id_delivery_option INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_delivery_option - FOREIGN KEY (id_delivery_option) - REFERENCES Shop_Delivery_Option(id_option) - ON UPDATE RESTRICT, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - price_local REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/125_tbl_Shop_Product_Delivery_Option_Link_Audit.sql b/static/PostgreSQL/125_tbl_Shop_Product_Delivery_Option_Link_Audit.sql deleted file mode 100644 index 1101429d..00000000 --- a/static/PostgreSQL/125_tbl_Shop_Product_Delivery_Option_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Delivery Option Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Product_Permutation_Delivery_Option_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Product_Permutation_Delivery_Option_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Permutation_Delivery_Option_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(64) NOT NULL, - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Product_Permutation_Delivery_Option_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/130.4_tbl_Shop_Discount.sql b/static/PostgreSQL/130.4_tbl_Shop_Discount.sql deleted file mode 100644 index c7641a47..00000000 --- a/static/PostgreSQL/130.4_tbl_Shop_Discount.sql +++ /dev/null @@ -1,48 +0,0 @@ - --- Discounts - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount'; - -CREATE TABLE Shop_Discount ( - id_discount INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50) NOT NULL, - name VARCHAR(200) NOT NULL, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER, - CONSTRAINT FK_Shop_Discount_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - /* - id_delivery_region INTEGER, - CONSTRAINT FK_Shop_Discount_id_delivery_region - FOREIGN KEY (id_delivery_region) - REFERENCES Shop_Delivery_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER, - CONSTRAINT FK_Shop_Discount_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - */ - multiplier REAL NOT NULL DEFAULT 1 CHECK (multiplier > 0), - subtractor REAL NOT NULL DEFAULT 0, - apply_multiplier_first BOOLEAN NOT NULL DEFAULT TRUE, - quantity_min REAL NOT NULL DEFAULT 0, - quantity_max REAL NOT NULL, - date_start TIMESTAMP NOT NULL, - date_end TIMESTAMP NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Discount_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/131_tbl_Shop_Discount_Audit.sql b/static/PostgreSQL/131_tbl_Shop_Discount_Audit.sql deleted file mode 100644 index 71a27cb5..00000000 --- a/static/PostgreSQL/131_tbl_Shop_Discount_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Discount Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_discount INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Audit_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/132_tbl_Shop_Discount_Region_Currency_Link.sql b/static/PostgreSQL/132_tbl_Shop_Discount_Region_Currency_Link.sql deleted file mode 100644 index a03740e2..00000000 --- a/static/PostgreSQL/132_tbl_Shop_Discount_Region_Currency_Link.sql +++ /dev/null @@ -1,32 +0,0 @@ - --- Discount Region Currency Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Region_Currency_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Region_Currency_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_discount INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount) - ON UPDATE RESTRICT, - id_region INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region) - ON UPDATE RESTRICT, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency) - ON UPDATE RESTRICT, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/133_tbl_Shop_Discount_Region_Currency_Link_Audit.sql b/static/PostgreSQL/133_tbl_Shop_Discount_Region_Currency_Link_Audit.sql deleted file mode 100644 index 5640d207..00000000 --- a/static/PostgreSQL/133_tbl_Shop_Discount_Region_Currency_Link_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Discount Region Currency Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Discount_Region_Currency_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Discount_Region_Currency_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Discount_Region_Currency_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Discount_Region_Currency_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/153_tbl_Shop_Permission_Group.sql b/static/PostgreSQL/153_tbl_Shop_Permission_Group.sql deleted file mode 100644 index 38fd7f44..00000000 --- a/static/PostgreSQL/153_tbl_Shop_Permission_Group.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- Permission Groups - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Group'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Group ( - id_group INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Permission_Group_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/154_tbl_Shop_Permission_Group_Audit.sql b/static/PostgreSQL/154_tbl_Shop_Permission_Group_Audit.sql deleted file mode 100644 index 9fa6120f..00000000 --- a/static/PostgreSQL/154_tbl_Shop_Permission_Group_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Permission Group Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Group_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Group_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_group INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Group_Audit_id_group - FOREIGN KEY (id_group) - REFERENCES Shop_Permission_Group(id_group) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Group_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/155_tbl_Shop_Permission.sql b/static/PostgreSQL/155_tbl_Shop_Permission.sql deleted file mode 100644 index bf9cb136..00000000 --- a/static/PostgreSQL/155_tbl_Shop_Permission.sql +++ /dev/null @@ -1,29 +0,0 @@ - --- Permissions - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission'; - -CREATE TABLE IF NOT EXISTS Shop_Permission ( - id_permission INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - id_permission_group INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_id_permission_group - FOREIGN KEY (id_permission_group) - REFERENCES Shop_Permission_Group(id_group) - ON UPDATE RESTRICT, - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level(id_access_level), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Permission_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/156_tbl_Shop_Permission_Audit.sql b/static/PostgreSQL/156_tbl_Shop_Permission_Audit.sql deleted file mode 100644 index ad7ce0bc..00000000 --- a/static/PostgreSQL/156_tbl_Shop_Permission_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Permission Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Permission_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Permission_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_permission INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Audit_id_permission - FOREIGN KEY (id_permission) - REFERENCES Shop_Permission(id_permission) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Permission_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/157_tbl_Shop_Role.sql b/static/PostgreSQL/157_tbl_Shop_Role.sql deleted file mode 100644 index b2a1bdfb..00000000 --- a/static/PostgreSQL/157_tbl_Shop_Role.sql +++ /dev/null @@ -1,20 +0,0 @@ - --- Roles - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role'; - -CREATE TABLE IF NOT EXISTS Shop_Role ( - id_role INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Role_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/158_tbl_Shop_Role_Audit.sql b/static/PostgreSQL/158_tbl_Shop_Role_Audit.sql deleted file mode 100644 index f8385f8c..00000000 --- a/static/PostgreSQL/158_tbl_Shop_Role_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Role Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Audit'; - -CREATE TABLE Shop_Role_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_role INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Audit_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/159_tbl_Shop_Role_Permission_Link.sql b/static/PostgreSQL/159_tbl_Shop_Role_Permission_Link.sql deleted file mode 100644 index 2fa67c58..00000000 --- a/static/PostgreSQL/159_tbl_Shop_Role_Permission_Link.sql +++ /dev/null @@ -1,31 +0,0 @@ - --- Role Permission link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Permission_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Role_Permission_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_role INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role) - ON UPDATE RESTRICT, - id_permission INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_permission - FOREIGN KEY (id_permission) - REFERENCES Shop_Permission(id_permission) - ON UPDATE RESTRICT, - id_access_level INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_access_level - FOREIGN KEY (id_access_level) - REFERENCES Shop_Access_Level(id_access_level), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Role_Permission_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/160_tbl_Shop_Role_Permission_Link_Audit.sql b/static/PostgreSQL/160_tbl_Shop_Role_Permission_Link_Audit.sql deleted file mode 100644 index 8793c4e8..00000000 --- a/static/PostgreSQL/160_tbl_Shop_Role_Permission_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Role Permission link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Role_Permission_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Role_Permission_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Permission_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Role_Permission_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Role_Permission_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/161_tbl_Shop_User.sql b/static/PostgreSQL/161_tbl_Shop_User.sql deleted file mode 100644 index 197a472e..00000000 --- a/static/PostgreSQL/161_tbl_Shop_User.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Users - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User'; - -CREATE TABLE IF NOT EXISTS Shop_User ( - id_user INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user_oauth VARCHAR(200) NOT NULL, - name VARCHAR(255) NOT NULL, - email VARCHAR(254) NOT NULL, - is_email_verified BOOLEAN NOT NULL DEFAULT FALSE, - is_super_user BOOLEAN NOT NULL DEFAULT FALSE, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/162_tbl_Shop_User_Audit.sql b/static/PostgreSQL/162_tbl_Shop_User_Audit.sql deleted file mode 100644 index d7544cf7..00000000 --- a/static/PostgreSQL/162_tbl_Shop_User_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- User Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Audit_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/163_tbl_Shop_User_Role_Link.sql b/static/PostgreSQL/163_tbl_Shop_User_Role_Link.sql deleted file mode 100644 index 55b92c30..00000000 --- a/static/PostgreSQL/163_tbl_Shop_User_Role_Link.sql +++ /dev/null @@ -1,26 +0,0 @@ - --- User Role link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Role_Link'; - -CREATE TABLE IF NOT EXISTS Shop_User_Role_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Role_Link_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - id_role INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_Role_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/164_tbl_Shop_User_Role_Link_Audit.sql b/static/PostgreSQL/164_tbl_Shop_User_Role_Link_Audit.sql deleted file mode 100644 index 3d5a4d38..00000000 --- a/static/PostgreSQL/164_tbl_Shop_User_Role_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- User Role Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Role_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Role_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_User_Role_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Role_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/165_tbl_Shop_Address.sql b/static/PostgreSQL/165_tbl_Shop_Address.sql deleted file mode 100644 index 01024659..00000000 --- a/static/PostgreSQL/165_tbl_Shop_Address.sql +++ /dev/null @@ -1,33 +0,0 @@ - --- Addresses - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Address'; - -CREATE TABLE Shop_Address ( - id_address INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - /* - a_id_user INTEGER, - CONSTRAINT FK_Shop_Address_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - */ - -- region VARCHAR(100) NOT NULL, - id_region INTEGER NOT NULL, - name_full VARCHAR(255) NOT NULL, - phone_number VARCHAR(20) NOT NULL, - postcode VARCHAR(20) NOT NULL, - address_line_1 VARCHAR(100) NOT NULL, - address_line_2 VARCHAR(100) NOT NULL, - city VARCHAR(50) NOT NULL, - county VARCHAR(100) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Address_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/166_tbl_Shop_Address_Audit.sql b/static/PostgreSQL/166_tbl_Shop_Address_Audit.sql deleted file mode 100644 index 948a70c3..00000000 --- a/static/PostgreSQL/166_tbl_Shop_Address_Audit.sql +++ /dev/null @@ -1,23 +0,0 @@ - --- Address Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Address_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Address_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Address_Audit_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Address_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) - ON UPDATE RESTRICT -); \ No newline at end of file diff --git a/static/PostgreSQL/167_tbl_Shop_User_Basket.sql b/static/PostgreSQL/167_tbl_Shop_User_Basket.sql deleted file mode 100644 index 4dcbf836..00000000 --- a/static/PostgreSQL/167_tbl_Shop_User_Basket.sql +++ /dev/null @@ -1,39 +0,0 @@ - --- User Basket (Product Link) - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Basket'; - -CREATE TABLE IF NOT EXISTS Shop_User_Basket ( - id_item INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user) - ON UPDATE RESTRICT, - id_product INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Basket_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product) - ON UPDATE RESTRICT, - id_permutation INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation) - ON UPDATE RESTRICT, - quantity INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_change_set_user - FOREIGN KEY (id_change_set_user) - REFERENCES Shop_User_Change_Set(id_change_set) - /* - id_change_set_product INTEGER, - CONSTRAINT FK_Shop_User_Basket_id_change_set_product - FOREIGN KEY (id_change_set_product) - REFERENCES Shop_Product_Change_Set(id_change_set) - */ -); diff --git a/static/PostgreSQL/168_tbl_Shop_User_Basket_Audit.sql b/static/PostgreSQL/168_tbl_Shop_User_Basket_Audit.sql deleted file mode 100644 index 7c512907..00000000 --- a/static/PostgreSQL/168_tbl_Shop_User_Basket_Audit.sql +++ /dev/null @@ -1,27 +0,0 @@ - --- Product Basket Audits - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Basket_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Basket_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_item INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Basket_Audit_id_link - FOREIGN KEY (id_item) - REFERENCES Shop_User_Basket(id_item) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(200), - value_new VARCHAR(200), - id_change_set_user INTEGER, - CONSTRAINT FK_Shop_User_Basket_Audit_id_change_set_user - FOREIGN KEY (id_change_set_user) - REFERENCES Shop_User_Change_Set(id_change_set) - /* - id_change_set_product INTEGER, - CONSTRAINT FK_Shop_User_Basket_Audit_id_change_set_product - FOREIGN KEY (id_change_set_product) - REFERENCES Shop_Product_Change_Set(id_change_set) - */ -); diff --git a/static/PostgreSQL/169_tbl_Shop_User_Order_Status.sql b/static/PostgreSQL/169_tbl_Shop_User_Order_Status.sql deleted file mode 100644 index 0a51f5b9..00000000 --- a/static/PostgreSQL/169_tbl_Shop_User_Order_Status.sql +++ /dev/null @@ -1,21 +0,0 @@ - --- User Order Types - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Order_Status'; - -CREATE TABLE IF NOT EXISTS Shop_User_Order_Status ( - id_status INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - code VARCHAR(50), - name VARCHAR(255), - name_plural VARCHAR(256), - active BOOLEAN NOT NULL DEFAULT TRUE, - display_order INTEGER NOT NULL, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_User_Order_Status_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/170_tbl_Shop_User_Order_Status_Audit.sql b/static/PostgreSQL/170_tbl_Shop_User_Order_Status_Audit.sql deleted file mode 100644 index 25dbb368..00000000 --- a/static/PostgreSQL/170_tbl_Shop_User_Order_Status_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Order Type Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_User_Order_Status_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_User_Order_Status_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_status INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Order_Status_Audit_id_status - FOREIGN KEY (id_status) - REFERENCES Shop_User_Order_Status(id_status) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_User_Order_Status_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); \ No newline at end of file diff --git a/static/PostgreSQL/181.0_tbl_Shop_Supplier.sql b/static/PostgreSQL/181.0_tbl_Shop_Supplier.sql deleted file mode 100644 index 6f45d42f..00000000 --- a/static/PostgreSQL/181.0_tbl_Shop_Supplier.sql +++ /dev/null @@ -1,32 +0,0 @@ - --- Supplier - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier ( - id_supplier INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Supplier_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.1_tbl_Shop_Supplier_Audit.sql b/static/PostgreSQL/181.1_tbl_Shop_Supplier_Audit.sql deleted file mode 100644 index ca866eea..00000000 --- a/static/PostgreSQL/181.1_tbl_Shop_Supplier_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Supplier Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Audit_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_User_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.2_tbl_Shop_Unit_Measurement.sql b/static/PostgreSQL/181.2_tbl_Shop_Unit_Measurement.sql deleted file mode 100644 index f26169fc..00000000 --- a/static/PostgreSQL/181.2_tbl_Shop_Unit_Measurement.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Unit of Measurement - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement ( - id_unit_measurement INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_singular VARCHAR(255) NOT NULL, - name_plural VARCHAR(256) NOT NULL, - symbol VARCHAR(50) NOT NULL, - is_base_unit BOOLEAN NOT NULL DEFAULT FALSE, - - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Unit_Measurement_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.3_tbl_Shop_Unit_Measurement_Audit.sql b/static/PostgreSQL/181.3_tbl_Shop_Unit_Measurement_Audit.sql deleted file mode 100644 index 9ee19084..00000000 --- a/static/PostgreSQL/181.3_tbl_Shop_Unit_Measurement_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Unit of Measurement Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_unit_measurement INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Audit_id_unit_measurement - FOREIGN KEY (id_unit_measurement) - REFERENCES Shop_Unit_Measurement(id_unit_measurement) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(256), - value_new VARCHAR(256), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.4_tbl_Shop_Unit_Measurement_Conversion.sql b/static/PostgreSQL/181.4_tbl_Shop_Unit_Measurement_Conversion.sql deleted file mode 100644 index fafb6fd3..00000000 --- a/static/PostgreSQL/181.4_tbl_Shop_Unit_Measurement_Conversion.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Unit of Measurement Conversion - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Conversion'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Conversion ( - id_conversion INTEGER NOT NULL PRIMARY KEY, - id_unit_derived INTEGER NOT NULL, - id_unit_base INTEGER NOT NULL, - power_unit_base REAL NOT NULL, - multiplier_unit_base REAL NOT NULL, - increment_unit_base REAL NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.5_tbl_Shop_Unit_Measurement_Conversion_Audit.sql b/static/PostgreSQL/181.5_tbl_Shop_Unit_Measurement_Conversion_Audit.sql deleted file mode 100644 index 6ec246ff..00000000 --- a/static/PostgreSQL/181.5_tbl_Shop_Unit_Measurement_Conversion_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Unit of Measurement Conversion Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Unit_Measurement_Conversion_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Unit_Measurement_Conversion_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_conversion INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_Audit_id_conversion - FOREIGN KEY (id_conversion) - REFERENCES Shop_Unit_Measurement_Conversion(id_conversion) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(100), - value_new VARCHAR(100), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Unit_Measurement_Conversion_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Product_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.6_tbl_Shop_Supplier_Purchase_Order.sql b/static/PostgreSQL/181.6_tbl_Shop_Supplier_Purchase_Order.sql deleted file mode 100644 index a36cfa4c..00000000 --- a/static/PostgreSQL/181.6_tbl_Shop_Supplier_Purchase_Order.sql +++ /dev/null @@ -1,41 +0,0 @@ - --- Supplier Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - /* - id_supplier_fulfilled INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_supplier_fulfilled - FOREIGN KEY (id_supplier_fulfilled) - REFERENCES Shop_Supplier(id_supplier), - */ - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - -- quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.7_tbl_Shop_Supplier_Purchase_Order_Audit.sql b/static/PostgreSQL/181.7_tbl_Shop_Supplier_Purchase_Order_Audit.sql deleted file mode 100644 index 2b303dce..00000000 --- a/static/PostgreSQL/181.7_tbl_Shop_Supplier_Purchase_Order_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Supplier Purchase Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.8_tbl_Shop_Supplier_Purchase_Order_Product_Link.sql b/static/PostgreSQL/181.8_tbl_Shop_Supplier_Purchase_Order_Product_Link.sql deleted file mode 100644 index 0dbd93eb..00000000 --- a/static/PostgreSQL/181.8_tbl_Shop_Supplier_Purchase_Order_Product_Link.sql +++ /dev/null @@ -1,37 +0,0 @@ - --- Supplier Purchase Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Supplier_Purchase_Order_Product_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/181.9_tbl_Shop_Supplier_Purchase_Order_Product_Link_Audit.sql b/static/PostgreSQL/181.9_tbl_Shop_Supplier_Purchase_Order_Product_Link_Audit.sql deleted file mode 100644 index 66520dfa..00000000 --- a/static/PostgreSQL/181.9_tbl_Shop_Supplier_Purchase_Order_Product_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Supplier Purchase Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purch_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Supplier_Purchase_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purch_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/182.0_tbl_Shop_Supplier_Purchase_Order_Product_Link_Temp.sql b/static/PostgreSQL/182.0_tbl_Shop_Supplier_Purchase_Order_Product_Link_Temp.sql deleted file mode 100644 index d088fbb0..00000000 --- a/static/PostgreSQL/182.0_tbl_Shop_Supplier_Purchase_Order_Product_Link_Temp.sql +++ /dev/null @@ -1,34 +0,0 @@ - --- Supplier Purchase Order Product Link Temp - - - --- drop table Shop_Supplier_Purchase_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Supplier_Purchase_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Supplier_Purchase_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Supplier_Purchase_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); diff --git a/static/PostgreSQL/183_tbl_Shop_Manufacturing_Purchase_Order.sql b/static/PostgreSQL/183_tbl_Shop_Manufacturing_Purchase_Order.sql deleted file mode 100644 index 0870e058..00000000 --- a/static/PostgreSQL/183_tbl_Shop_Manufacturing_Purchase_Order.sql +++ /dev/null @@ -1,33 +0,0 @@ - - --- Manufacturing Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/184_tbl_Shop_Manufacturing_Purchase_Order_Audit.sql b/static/PostgreSQL/184_tbl_Shop_Manufacturing_Purchase_Order_Audit.sql deleted file mode 100644 index d50b5156..00000000 --- a/static/PostgreSQL/184_tbl_Shop_Manufacturing_Purchase_Order_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Manufacturing Purchase Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Manufacturing_Purchase_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/185_tbl_Shop_Manufacturing_Purchase_Order_Product_Link.sql b/static/PostgreSQL/185_tbl_Shop_Manufacturing_Purchase_Order_Product_Link.sql deleted file mode 100644 index 6ff6f469..00000000 --- a/static/PostgreSQL/185_tbl_Shop_Manufacturing_Purchase_Order_Product_Link.sql +++ /dev/null @@ -1,38 +0,0 @@ - --- Manufacturing Purchase Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - latency_manufacture INTEGER NOT NULL, - quantity_produced REAL NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Manufacturing_Purchase_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/186.1_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Temp.sql b/static/PostgreSQL/186.1_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Temp.sql deleted file mode 100644 index 7237a1aa..00000000 --- a/static/PostgreSQL/186.1_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Temp.sql +++ /dev/null @@ -1,35 +0,0 @@ - --- Manufacturing Purchase Order Product Link Temp - - - --- DROP TABLE Shop_Manufacturing_Purchase_Order_Product_Link_Temp; --- SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Manuf_Purch_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_manufacture INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); diff --git a/static/PostgreSQL/186_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Audit.sql b/static/PostgreSQL/186_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Audit.sql deleted file mode 100644 index f6507628..00000000 --- a/static/PostgreSQL/186_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Manufacturing Purchase Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Manufacturing_Purchase_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Manufacturing_Purchase_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purch_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Manufacturing_Purchase_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Manufacturing_Purch_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/187.0_tbl_Shop_Customer.sql b/static/PostgreSQL/187.0_tbl_Shop_Customer.sql deleted file mode 100644 index 39ecf58d..00000000 --- a/static/PostgreSQL/187.0_tbl_Shop_Customer.sql +++ /dev/null @@ -1,29 +0,0 @@ --- Customer - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer'; - -CREATE TABLE IF NOT EXISTS Shop_Customer ( - id_customer INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - id_change_set INTEGER, - CONSTRAINT FK_Shop_Customer_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/187.1_tbl_Shop_Customer_Audit.sql b/static/PostgreSQL/187.1_tbl_Shop_Customer_Audit.sql deleted file mode 100644 index 5ed1e9ca..00000000 --- a/static/PostgreSQL/187.1_tbl_Shop_Customer_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Customer Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_customer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Audit_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(255), - value_new VARCHAR(255), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/187.2_tbl_Shop_Customer_Sales_Order.sql b/static/PostgreSQL/187.2_tbl_Shop_Customer_Sales_Order.sql deleted file mode 100644 index ab831c86..00000000 --- a/static/PostgreSQL/187.2_tbl_Shop_Customer_Sales_Order.sql +++ /dev/null @@ -1,35 +0,0 @@ - --- Customer Sales Purchase Order - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_customer INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - /* - latency_delivery INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit), - quantity_received INTEGER NULL, - display_order INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL DEFAULT TRUE, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/188_tbl_Shop_Customer_Sales_Order_Audit.sql b/static/PostgreSQL/188_tbl_Shop_Customer_Sales_Order_Audit.sql deleted file mode 100644 index ff12450c..00000000 --- a/static/PostgreSQL/188_tbl_Shop_Customer_Sales_Order_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Customer Sales Order Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Audit_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/189_tbl_Shop_Customer_Sales_Order_Product_Link.sql b/static/PostgreSQL/189_tbl_Shop_Customer_Sales_Order_Product_Link.sql deleted file mode 100644 index 058f16b7..00000000 --- a/static/PostgreSQL/189_tbl_Shop_Customer_Sales_Order_Product_Link.sql +++ /dev/null @@ -1,38 +0,0 @@ - --- Customer Sales Order Product Link - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NOT NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - - active BOOLEAN NOT NULL, - created_on TIMESTAMP, - created_by INT, - updated_last_on TIMESTAMP NULL, - created_last_by VARCHAR(100) NULL, - id_change_set INTEGER NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/190_tbl_Shop_Customer_Sales_Order_Product_Link_Audit.sql b/static/PostgreSQL/190_tbl_Shop_Customer_Sales_Order_Product_Link_Audit.sql deleted file mode 100644 index a2da163b..00000000 --- a/static/PostgreSQL/190_tbl_Shop_Customer_Sales_Order_Product_Link_Audit.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Customer Sales Order Product Link Audits - - - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link_Audit'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link_Audit ( - id_audit INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_link INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_Audit_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Customer_Sales_Order_Product_Link(id_link) - ON UPDATE RESTRICT, - name_field VARCHAR(50), - value_prev VARCHAR(10), - value_new VARCHAR(10), - id_change_set INTEGER NOT NULL, - CONSTRAINT FK_Shop_Customer_Sales_Order_Product_Link_Audit_id_change_set - FOREIGN KEY (id_change_set) - REFERENCES Shop_Sales_And_Purchasing_Change_Set(id_change_set) -); diff --git a/static/PostgreSQL/191_tbl_Shop_Customer_Sales_Order_Product_Link_Temp.sql b/static/PostgreSQL/191_tbl_Shop_Customer_Sales_Order_Product_Link_Temp.sql deleted file mode 100644 index 3a6e352c..00000000 --- a/static/PostgreSQL/191_tbl_Shop_Customer_Sales_Order_Product_Link_Temp.sql +++ /dev/null @@ -1,34 +0,0 @@ - --- Customer Sales Order Product Link Temp - - - --- DROP TABLE Shop_Customer_Sales_Order_Product_Link_Temp; - -SELECT CONCAT('WARNING: Table ', TABLE_NAME, ' already exists.') AS msg_warning FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Shop_Customer_Sales_Order_Product_Link_Temp'; - -CREATE TABLE IF NOT EXISTS Shop_Customer_Sales_Order_Product_Link_Temp ( - id_link INTEGER NOT NULL PRIMARY KEY, - GUID UUID NOT NULL, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_Customer_Sales_Order_Product_Link_Temp_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL -); diff --git a/static/PostgreSQL/300.2_tri_Shop_Sales_And_Purchasing_Change_Set.sql b/static/PostgreSQL/300.2_tri_Shop_Sales_And_Purchasing_Change_Set.sql deleted file mode 100644 index 044661bf..00000000 --- a/static/PostgreSQL/300.2_tri_Shop_Sales_And_Purchasing_Change_Set.sql +++ /dev/null @@ -1,26 +0,0 @@ - -DO $$ -BEGIN - RAISE NOTICE 'TABLE CREATION COMPLETE'; -END $$; - --- Product Change Set - -CREATE OR REPLACE FUNCTION before_insert_Shop_Sales_And_Purchasing_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Sales_And_Purchasing_Change_Set -BEFORE INSERT ON Shop_Sales_And_Purchasing_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Sales_And_Purchasing_Change_Set(); - diff --git a/static/PostgreSQL/301.1_tri_Shop_User_Change_Set.sql b/static/PostgreSQL/301.1_tri_Shop_User_Change_Set.sql deleted file mode 100644 index 0e5fdb16..00000000 --- a/static/PostgreSQL/301.1_tri_Shop_User_Change_Set.sql +++ /dev/null @@ -1,20 +0,0 @@ - --- Shop User Change Set - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Change_Set -BEFORE INSERT ON Shop_User_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Change_Set(); diff --git a/static/PostgreSQL/301.2_tri_Shop_Access_Level.sql b/static/PostgreSQL/301.2_tri_Shop_Access_Level.sql deleted file mode 100644 index d4175132..00000000 --- a/static/PostgreSQL/301.2_tri_Shop_Access_Level.sql +++ /dev/null @@ -1,66 +0,0 @@ - --- Shop Access Level - -CREATE OR REPLACE FUNCTION before_insert_Shop_Access_Level() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Access_Level -BEFORE INSERT ON Shop_Access_Level -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Access_Level(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Access_Level() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Access_Level_Audit ( - id_access_level, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_access_level, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT (OLD.code <=> NEW.code) - UNION - -- Changed name - SELECT NEW.id_access_level, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - -- Changed priority - SELECT NEW.id_access_level, 'priority', CONVERT(OLD.priority, CHAR), CONVERT(NEW.priority, CHAR), NEW.id_change_set - WHERE NOT (OLD.priority <=> NEW.priority) - UNION - -- Changed active - SELECT NEW.id_access_level, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_access_level, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Access_Level -BEFORE UPDATE ON Shop_Access_Level -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Access_Level(); diff --git a/static/PostgreSQL/301_tri_Shop_Product_Change_Set.sql b/static/PostgreSQL/301_tri_Shop_Product_Change_Set.sql deleted file mode 100644 index d2a67f54..00000000 --- a/static/PostgreSQL/301_tri_Shop_Product_Change_Set.sql +++ /dev/null @@ -1,22 +0,0 @@ - --- Product Change Set - - - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Change_Set() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.updated_last_on IS NULL THEN - NEW.updated_last_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.updated_last_by IS NULL THEN - NEW.updated_last_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Change_Set -BEFORE INSERT ON Shop_Product_Change_Set -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Change_Set(); diff --git a/static/PostgreSQL/302_tri_File_Type.sql b/static/PostgreSQL/302_tri_File_Type.sql deleted file mode 100644 index 4eacb9a8..00000000 --- a/static/PostgreSQL/302_tri_File_Type.sql +++ /dev/null @@ -1,48 +0,0 @@ - --- File Type - -CREATE OR REPLACE FUNCTION before_insert_File_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_File_Type -BEFORE INSERT ON File_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_File_Type(); - - -CREATE OR REPLACE FUNCTION before_update_File_Type() -RETURNS TRIGGER AS $$ -BEGIN - INSERT INTO File_Type_Audit ( - id_type, - name_field, - value_prev, - value_new - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed extension - SELECT NEW.id_type, 'extension', CONVERT(OLD.extension, CHAR), CONVERT(NEW.extension, CHAR) - WHERE NOT OLD.extension <=> NEW.extension - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_File_Type -BEFORE UPDATE ON File_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_File_Type(); diff --git a/static/PostgreSQL/303_tri_File_Type_Audit.sql b/static/PostgreSQL/303_tri_File_Type_Audit.sql deleted file mode 100644 index 7c8ba354..00000000 --- a/static/PostgreSQL/303_tri_File_Type_Audit.sql +++ /dev/null @@ -1,33 +0,0 @@ - --- File Type Audits - -CREATE OR REPLACE FUNCTION before_insert_File_Type_Audit() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_File_Type_Audit -BEFORE INSERT ON File_Type_Audit -FOR EACH ROW -EXECUTE FUNCTION before_insert_File_Type_Audit(); - - -CREATE OR REPLACE FUNCTION before_update_File_Type_Audit() -RETURNS TRIGGER AS $$ -BEGIN - NEW.updated_last_on = CURRENT_TIMESTAMP; - NEW.updated_last_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_File_Type_Audit -BEFORE UPDATE ON File_Type_Audit -FOR EACH ROW -EXECUTE FUNCTION before_update_File_Type_Audit(); \ No newline at end of file diff --git a/static/PostgreSQL/304_tri_Shop_General.sql b/static/PostgreSQL/304_tri_Shop_General.sql deleted file mode 100644 index 3628912b..00000000 --- a/static/PostgreSQL/304_tri_Shop_General.sql +++ /dev/null @@ -1,47 +0,0 @@ - --- Shop General - -CREATE OR REPLACE FUNCTION before_insert_Shop_General() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_General -BEFORE INSERT ON Shop_General -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_General(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_General() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_General_Audit ( - id_general, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed quantity max - SELECT NEW.id_general, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_General -BEFORE UPDATE ON Shop_General -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_General(); \ No newline at end of file diff --git a/static/PostgreSQL/306_tri_Shop_Category.sql b/static/PostgreSQL/306_tri_Shop_Category.sql deleted file mode 100644 index 727212ba..00000000 --- a/static/PostgreSQL/306_tri_Shop_Category.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Category - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Category() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Category -BEFORE INSERT ON Shop_Product_Category -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Category(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Category() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Category_Audit ( - id_category, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_category, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_category, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed description - SELECT NEW.id_category, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT OLD.description <=> NEW.description - UNION - -- Changed active - SELECT NEW.id_category, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_category, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Category -BEFORE UPDATE ON Shop_Product_Category -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Category(); diff --git a/static/PostgreSQL/308_tri_Shop_Recurrence_Interval.sql b/static/PostgreSQL/308_tri_Shop_Recurrence_Interval.sql deleted file mode 100644 index 0a82210f..00000000 --- a/static/PostgreSQL/308_tri_Shop_Recurrence_Interval.sql +++ /dev/null @@ -1,59 +0,0 @@ - --- Shop Recurrence Interval - -CREATE OR REPLACE FUNCTION before_insert_Shop_Interval_Recurrence() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Interval_Recurrence -BEFORE INSERT ON Shop_Interval_Recurrence -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Interval_Recurrence(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Interval_Recurrence() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Interval_Recurrence_Audit ( - id_interval, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_interval, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_interval, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_interval, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed name - SELECT NEW.id_interval, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Interval_Recurrence -BEFORE UPDATE ON Shop_Interval_Recurrence -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Interval_Recurrence(); diff --git a/static/PostgreSQL/310.0_tri_Shop_Region.sql b/static/PostgreSQL/310.0_tri_Shop_Region.sql deleted file mode 100644 index cb76b90c..00000000 --- a/static/PostgreSQL/310.0_tri_Shop_Region.sql +++ /dev/null @@ -1,59 +0,0 @@ - --- Shop Delivery Region - -CREATE OR REPLACE FUNCTION before_insert_Shop_Region() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Region -BEFORE INSERT ON Shop_Region -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Region(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Region() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Region_Audit ( - id_region, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_region, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_region, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_region, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_region, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Region -BEFORE UPDATE ON Shop_Region -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Region(); diff --git a/static/PostgreSQL/310.2_tri_Shop_Region_Branch.sql b/static/PostgreSQL/310.2_tri_Shop_Region_Branch.sql deleted file mode 100644 index 8192c4c7..00000000 --- a/static/PostgreSQL/310.2_tri_Shop_Region_Branch.sql +++ /dev/null @@ -1,57 +0,0 @@ - --- Shop Region Branch - -CREATE OR REPLACE FUNCTION before_insert_Shop_Region_Branch() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Region_Branch -BEFORE INSERT ON Shop_Region_Branch -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Region_Branch(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Region_Branch() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Region_Branch_Audit ( - id_branch, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed depth - SELECT NEW.id_branch, 'depth', CONVERT(OLD.depth, CHAR), CONVERT(NEW.depth, CHAR), NEW.id_change_set - WHERE NOT OLD.depth <=> NEW.depth - UNION - */ - -- Changed active - SELECT NEW.id_branch, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_branch, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Region_Branch -BEFORE UPDATE ON Shop_Region_Branch -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Region_Branch(); diff --git a/static/PostgreSQL/310.4_tri_Shop_Currency.sql b/static/PostgreSQL/310.4_tri_Shop_Currency.sql deleted file mode 100644 index 325344e6..00000000 --- a/static/PostgreSQL/310.4_tri_Shop_Currency.sql +++ /dev/null @@ -1,71 +0,0 @@ - --- Shop Currency - -CREATE OR REPLACE FUNCTION before_insert_Shop_Currency() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Currency -BEFORE INSERT ON Shop_Currency -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Currency(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Currency() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Currency_Audit ( - id_currency, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_currency, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_currency, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed symbol - SELECT NEW.id_currency, 'symbol', OLD.symbol, NEW.symbol, NEW.id_change_set - WHERE NOT OLD.symbol <=> NEW.symbol - UNION - -- Changed ratio_2_GBP - SELECT NEW.id_currency, 'factor_from_GBP', OLD.factor_from_GBP, NEW.factor_from_GBP, NEW.id_change_set - WHERE NOT OLD.factor_from_GBP <=> NEW.factor_from_GBP - UNION - -- Changed active - SELECT NEW.id_currency, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_currency, 'display_order', CONVERT(display_order, CHAR), CONVERT(display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Currency -BEFORE UPDATE ON Shop_Currency -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Currency(); diff --git a/static/PostgreSQL/310.6_tri_Shop_Tax_Or_Surcharge.sql b/static/PostgreSQL/310.6_tri_Shop_Tax_Or_Surcharge.sql deleted file mode 100644 index bb1a86ca..00000000 --- a/static/PostgreSQL/310.6_tri_Shop_Tax_Or_Surcharge.sql +++ /dev/null @@ -1,80 +0,0 @@ - --- Shop Tax_Or_Surcharge - -CREATE OR REPLACE FUNCTION before_insert_Shop_Tax_Or_Surcharge() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Tax_Or_Surcharge -BEFORE INSERT ON Shop_Tax_Or_Surcharge -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Tax_Or_Surcharge(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Tax_Or_Surcharge() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Tax_Or_Surcharge_Audit ( - id_tax, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_tax, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_tax, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed fixed_fee - SELECT NEW.id_tax, 'fixed_fee', OLD.fixed_fee, NEW.fixed_fee, NEW.id_change_set - WHERE NOT OLD.fixed_fee <=> NEW.fixed_fee - UNION - -- Changed multiplier - SELECT NEW.id_tax, 'multiplier', OLD.multiplier, NEW.multiplier, NEW.id_change_set - WHERE NOT OLD.multiplier <=> NEW.multiplier - UNION - -- Changed apply_fixed_fee_before_multiplier - SELECT NEW.id_tax, 'apply_fixed_fee_before_multiplier', CONVERT(CONVERT(OLD.apply_fixed_fee_before_multiplier, SIGNED), CHAR), CONVERT(CONVERT(NEW.apply_fixed_fee_before_multiplier, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.apply_fixed_fee_before_multiplier <=> NEW.apply_fixed_fee_before_multiplier - UNION - -- Changed quantity_min - SELECT NEW.id_tax, 'quantity_min', OLD.quantity_min, NEW.quantity_min, NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_tax, 'quantity_max', OLD.quantity_max, NEW.quantity_max, NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed display_order - SELECT NEW.id_tax, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_tax, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Tax_Or_Surcharge -BEFORE UPDATE ON Shop_Tax_Or_Surcharge -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Tax_Or_Surcharge(); - diff --git a/static/PostgreSQL/310.8_tri_Shop_Product.sql b/static/PostgreSQL/310.8_tri_Shop_Product.sql deleted file mode 100644 index 4f25d19a..00000000 --- a/static/PostgreSQL/310.8_tri_Shop_Product.sql +++ /dev/null @@ -1,173 +0,0 @@ - --- Shop Product - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product -BEFORE INSERT ON Shop_Product -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - /* - IF NOT NEW.has_variations THEN - IF ISNULL(NEW.price_GBP_full) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have price or variations (with prices).'; - END IF; - IF ISNULL(NEW.price_GBP_min) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have minimum price or variations (with prices).'; - END IF; - IF ISNULL(NEW.latency_manuf) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have manufacturing latency or variations (with manufacturing latencies).'; - END IF; - IF ISNULL(NEW.quantity_min) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have minimum quantity or variations (with minimum quantities).'; - END IF; - IF ISNULL(NEW.quantity_max) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have maximum quantity or variations (with maximum quantities).'; - END IF; - IF ISNULL(NEW.quantity_step) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have increment of quantity or variations (with increments of quantities).'; - END IF; - IF ISNULL(NEW.quantity_stock) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have stock quantity or variations (with stock quantities).'; - END IF; - IF ISNULL(NEW.is_subscription) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have subscription status or variations (with subscription statuses).'; - END IF; - IF ISNULL(NEW.id_unit_measurement_interval_recurrence) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have recurrence interval or variations (with recurrence intervals).'; - END IF; - IF ISNULL(NEW.count_interval_recurrence) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Product must have recurrence interval count or variations (with recurrence interval counts).'; - END IF; - END IF; - */ - - INSERT INTO Shop_Product_Audit ( - id_product, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name - SELECT NEW.id_product, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - /* - UNION - -- Changed description - SELECT NEW.id_product, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT OLD.description <=> NEW.description - UNION - -- Changed price_GBP_full - SELECT NEW.id_product, 'price_GBP_full', CONVERT(OLD.price_GBP_full, CHAR), CONVERT(NEW.price_GBP_full, CHAR), NEW.id_change_set - WHERE NOT OLD.price_GBP_full <=> NEW.price_GBP_full - UNION - -- Changed price_GBP_min - SELECT NEW.id_product, 'price_GBP_min', CONVERT(OLD.price_GBP_min, CHAR), CONVERT(NEW.price_GBP_min, CHAR), NEW.id_change_set - WHERE NOT OLD.price_GBP_min <=> NEW.price_GBP_min - UNION - / - -- Changed discount - SELECT NEW.id_product, 'discount', CONVERT(OLD.discount, CHAR), CONVERT(NEW.discount, CHAR), NEW.id_change_set - WHERE NOT OLD.discount <=> NEW.discount - */ - UNION - -- Changed id_category - SELECT NEW.id_product, 'id_category', CONVERT(OLD.id_category, CHAR), CONVERT(NEW.id_category, CHAR), NEW.id_change_set - WHERE NOT OLD.id_category <=> NEW.id_category - UNION - -- Changed has_variations - SELECT NEW.id_product, 'has_variations', CONVERT(CONVERT(NEW.has_variations, SIGNED), CHAR), CONVERT(CONVERT(NEW.has_variations, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.has_variations <=> NEW.has_variations - /* - UNION - -- Changed latency_manuf - SELECT NEW.id_product, 'latency_manuf', CONVERT(OLD.latency_manuf, CHAR), CONVERT(NEW.latency_manuf, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_manuf <=> NEW.latency_manuf - UNION - -- Changed quantity_min - SELECT NEW.id_product, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_product, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed quantity_step - SELECT NEW.id_product, 'quantity_step', CONVERT(OLD.quantity_step, CHAR), CONVERT(NEW.quantity_step, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_step <=> NEW.quantity_step - UNION - -- Changed quantity_stock - SELECT NEW.id_product, 'quantity_stock', CONVERT(OLD.quantity_stock, CHAR), CONVERT(NEW.quantity_stock, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_stock <=> NEW.quantity_stock - UNION - -- Changed is_subscription - SELECT NEW.id_product, 'is_subscription', CONVERT(CONVERT(OLD.is_subscription, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_subscription, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.is_subscription <=> NEW.is_subscription - UNION - -- Changed id_unit_measurement_interval_recurrence - SELECT NEW.id_product, 'id_unit_measurement_interval_recurrence', CONVERT(OLD.id_unit_measurement_interval_recurrence, CHAR), CONVERT(NEW.id_unit_measurement_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.id_unit_measurement_interval_recurrence <=> NEW.id_unit_measurement_interval_recurrence - UNION - -- Changed count_interval_recurrence - SELECT NEW.id_product, 'count_interval_recurrence', CONVERT(OLD.count_interval_recurrence, CHAR), CONVERT(NEW.count_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.count_interval_recurrence <=> NEW.count_interval_recurrence - UNION - -- Changed id_stripe_product - SELECT NEW.id_product, 'id_stripe_product', OLD.id_stripe_product, NEW.id_stripe_product, NEW.id_change_set - WHERE NOT OLD.id_stripe_product <=> NEW.id_stripe_product - / - UNION - -- Changed id_stripe_price - SELECT NEW.id_product, 'id_stripe_price', OLD.id_stripe_price, NEW.id_stripe_price, NEW.id_change_set - WHERE NOT OLD.id_stripe_price <=> NEW.id_stripe_price - */ - UNION - -- Changed id_access_level_required - SELECT NEW.id_product, 'id_access_level_required', CONVERT(OLD.id_access_level_required, CHAR), CONVERT(NEW.id_access_level_required, CHAR), NEW.id_change_set - WHERE NOT OLD.id_access_level_required <=> NEW.id_access_level_required - UNION - -- Changed active - SELECT NEW.id_product, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_product, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product -BEFORE UPDATE ON Shop_Product -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product(); diff --git a/static/PostgreSQL/312_tri_Shop_Variation_Type.sql b/static/PostgreSQL/312_tri_Shop_Variation_Type.sql deleted file mode 100644 index fa23aed3..00000000 --- a/static/PostgreSQL/312_tri_Shop_Variation_Type.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Variation Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Variation_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Variation_Type -BEFORE INSERT ON Shop_Variation_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Variation_Type(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Variation_Type() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Variation_Type_Audit ( - id_type, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_type, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_type, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_type, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Variation_Type -BEFORE UPDATE ON Shop_Variation_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Variation_Type(); diff --git a/static/PostgreSQL/314_tri_Shop_Variation.sql b/static/PostgreSQL/314_tri_Shop_Variation.sql deleted file mode 100644 index ee5b384f..00000000 --- a/static/PostgreSQL/314_tri_Shop_Variation.sql +++ /dev/null @@ -1,59 +0,0 @@ - --- Shop Variation - -CREATE OR REPLACE FUNCTION before_insert_Shop_Variation() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Variation -BEFORE INSERT ON Shop_Variation -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Variation(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Variation() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Variation_Audit ( - id_variation, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_variation, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_variation, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_variation, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_variation, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Variation -BEFORE UPDATE ON Shop_Variation -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Variation(); diff --git a/static/PostgreSQL/317.1_tri_Shop_Product_Permutation.sql b/static/PostgreSQL/317.1_tri_Shop_Product_Permutation.sql deleted file mode 100644 index 7a43d1e7..00000000 --- a/static/PostgreSQL/317.1_tri_Shop_Product_Permutation.sql +++ /dev/null @@ -1,133 +0,0 @@ - --- Shop Product Permutation - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation -BEFORE INSERT ON Shop_Product_Permutation -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Audit ( - id_permutation, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_permutation, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_variation - SELECT NEW.id_permutation, 'id_variation', OLD.id_variation, NEW.id_variation, NEW.id_change_set - WHERE NOT OLD.id_variation <=> NEW.id_variation - UNION - -- Changed name - SELECT NEW.id_permutation, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - */ - -- Changed description - SELECT NEW.id_permutation, 'description', OLD.description, NEW.description, NEW.id_change_set - WHERE NOT (OLD.description <=> NEW.description) - UNION - -- Changed cost_local - SELECT NEW.id_permutation, 'cost_local', CONVERT(OLD.cost_local, CHAR), CONVERT(NEW.cost_local, CHAR), NEW.id_change_set - WHERE NOT (OLD.cost_local <=> NEW.cost_local) - UNION - -- Changed id_currency_cost - SELECT NEW.id_permutation, 'id_currency_cost', CONVERT(OLD.id_currency_cost, CHAR), CONVERT(NEW.id_currency_cost, CHAR), NEW.id_change_set - WHERE NOT (OLD.id_currency_cost <=> NEW.id_currency_cost) - UNION - -- Changed profit_local_min - SELECT NEW.id_permutation, 'profit_local_min', CONVERT(OLD.profit_local_min, CHAR), CONVERT(NEW.profit_local_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.profit_local_min <=> NEW.profit_local_min) - UNION - /* - -- Changed id_currency_profit_min - SELECT NEW.id_permutation, 'id_currency_profit_min', CONVERT(OLD.id_currency_profit_min, CHAR), CONVERT(NEW.id_currency_profit_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.id_currency_profit_min <=> NEW.id_currency_profit_min) - UNION - */ - /* - -- Changed price_GBP_min - SELECT NEW.id_permutation, 'price_GBP_min', CONVERT(OLD.price_GBP_min, CHAR), CONVERT(NEW.price_GBP_min, CHAR), NEW.id_change_set - WHERE NOT (OLD.price_GBP_min <=> NEW.price_GBP_min) - UNION - */ - -- Changed latency_manufacture - SELECT NEW.id_product, 'latency_manufacture', CONVERT(OLD.latency_manufacture, CHAR), CONVERT(NEW.latency_manufacture, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_manufacture <=> NEW.latency_manufacture - UNION - -- Changed quantity_min - SELECT NEW.id_product, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_product, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed quantity_step - SELECT NEW.id_product, 'quantity_step', CONVERT(OLD.quantity_step, CHAR), CONVERT(NEW.quantity_step, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_step <=> NEW.quantity_step - UNION - -- Changed quantity_stock - SELECT NEW.id_product, 'quantity_stock', CONVERT(OLD.quantity_stock, CHAR), CONVERT(NEW.quantity_stock, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_stock <=> NEW.quantity_stock - UNION - -- Changed is_subscription - SELECT NEW.id_product, 'is_subscription', CONVERT(CONVERT(OLD.is_subscription, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_subscription, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.is_subscription <=> NEW.is_subscription - UNION - -- Changed id_unit_measurement_interval_recurrence - SELECT NEW.id_product, 'id_unit_measurement_interval_recurrence', CONVERT(OLD.id_unit_measurement_interval_recurrence, CHAR), CONVERT(NEW.id_unit_measurement_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.id_unit_measurement_interval_recurrence <=> NEW.id_unit_measurement_interval_recurrence - UNION - -- Changed count_interval_recurrence - SELECT NEW.id_product, 'count_interval_recurrence', CONVERT(OLD.count_interval_recurrence, CHAR), CONVERT(NEW.count_interval_recurrence, CHAR), NEW.id_change_set - WHERE NOT OLD.count_interval_recurrence <=> NEW.count_interval_recurrence - UNION - -- Changed id_stripe_product - SELECT NEW.id_permutation, 'id_stripe_product', OLD.id_stripe_product, NEW.id_stripe_product, NEW.id_change_set - WHERE NOT (OLD.id_stripe_product <=> NEW.id_stripe_product) - UNION - -- Changed active - SELECT NEW.id_permutation, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_permutation, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation -BEFORE UPDATE ON Shop_Product_Permutation -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation(); diff --git a/static/PostgreSQL/317.3_tri_Shop_Product_Permutation_Variation_Link.sql b/static/PostgreSQL/317.3_tri_Shop_Product_Permutation_Variation_Link.sql deleted file mode 100644 index ef28a99b..00000000 --- a/static/PostgreSQL/317.3_tri_Shop_Product_Permutation_Variation_Link.sql +++ /dev/null @@ -1,65 +0,0 @@ - --- Shop Product Permutation Variation Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation_Variation_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation_Variation_Link -BEFORE INSERT ON Shop_Product_Permutation_Variation_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation_Variation_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation_Variation_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Variation_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_variation - SELECT NEW.id_link, 'id_variation', OLD.id_variation, NEW.id_variation, NEW.id_change_set - WHERE NOT OLD.id_variation <=> NEW.id_variation - UNION - */ - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation_Variation_Link -BEFORE UPDATE ON Shop_Product_Permutation_Variation_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation_Variation_Link(); diff --git a/static/PostgreSQL/317.5_tri_Shop_Product_Currency_Region_Link.sql b/static/PostgreSQL/317.5_tri_Shop_Product_Currency_Region_Link.sql deleted file mode 100644 index 6542497a..00000000 --- a/static/PostgreSQL/317.5_tri_Shop_Product_Currency_Region_Link.sql +++ /dev/null @@ -1,97 +0,0 @@ - --- Shop Product Currency Region Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Currency_Region_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - /* - NEW.price_local = ( - SELECT PP.price_GBP_full * C.factor_from_GBP - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Currency C ON NEW.id_currency = C.id_currency - WHERE NEW.id_product = P.id_product - LIMIT 1 - ); - */ - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Currency_Region_Link -BEFORE INSERT ON Shop_Product_Currency_Region_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Currency_Region_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Currency_Region_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - /* - NEW.price_local = ( - SELECT P.price_GBP_full * C.factor_from_GBP - FROM Shop_Product P - INNER JOIN Shop_Currency C ON NEW.id_currency = C.id_currency - WHERE NEW.id_product = P.id_product - LIMIT 1 - ); - */ - - INSERT INTO Shop_Product_Currency_Region_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_currency - SELECT NEW.id_link, 'id_currency', CONVERT(OLD.id_currency, CHAR), CONVERT(NEW.id_currency, CHAR), NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - UNION - -- Changed price_local - SELECT NEW.id_link, 'price_local', OLD.price_local, NEW.price_local, NEW.id_change_set - WHERE NOT OLD.price_local <=> NEW.price_local - UNION - */ - -- Changed price_local_VAT_incl - SELECT NEW.id_link, 'price_local_VAT_incl', OLD.price_local_VAT_incl, NEW.price_local_VAT_incl, NEW.id_change_set - WHERE NOT OLD.price_local_VAT_incl <=> NEW.price_local_VAT_incl - UNION - -- Changed price_local_VAT_excl - SELECT NEW.id_link, 'price_local_VAT_excl', OLD.price_local_VAT_excl, NEW.price_local_VAT_excl, NEW.id_change_set - WHERE NOT OLD.price_local_VAT_excl <=> NEW.price_local_VAT_excl - UNION - -- Changed id_stripe_price - SELECT NEW.id_link, 'id_stripe_price', OLD.id_stripe_price, NEW.id_stripe_price, NEW.id_change_set - WHERE NOT OLD.id_stripe_price <=> NEW.id_stripe_price - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Currency_Region_Link -BEFORE UPDATE ON Shop_Product_Currency_Region_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Currency_Region_Link(); diff --git a/static/PostgreSQL/318_tri_Shop_Image_Type.sql b/static/PostgreSQL/318_tri_Shop_Image_Type.sql deleted file mode 100644 index 038f9c34..00000000 --- a/static/PostgreSQL/318_tri_Shop_Image_Type.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Image Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Image_Type() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Image_Type -BEFORE INSERT ON Shop_Image_Type -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Image_Type(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Image_Type() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Image_Type_Audit ( - id_type, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_type, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_type, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_type, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_type, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_type, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Image_Type -BEFORE UPDATE ON Shop_Image_Type -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Image_Type(); diff --git a/static/PostgreSQL/320_tri_Shop_Image.sql b/static/PostgreSQL/320_tri_Shop_Image.sql deleted file mode 100644 index 1bb31cbe..00000000 --- a/static/PostgreSQL/320_tri_Shop_Image.sql +++ /dev/null @@ -1,75 +0,0 @@ - --- Shop Image - -CREATE OR REPLACE FUNCTION before_insert_Shop_Image() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Image -BEFORE INSERT ON Shop_Image -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Image(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Image() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - IF ISNULL(NEW.id_product) AND ISNULL(NEW.id_permutation) THEN - RAISE EXCEPTION 'Image must NOT have ID for product AND product permutation.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Image_Audit ( - id_image, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_type_image - SELECT NEW.id_image, 'id_type_image', CONVERT(OLD.id_type_image, CHAR), CONVERT(NEW.id_type_image, CHAR), NEW.id_change_set - WHERE NOT OLD.id_type_image <=> NEW.id_type_image - UNION - -- Changed id_type_file - SELECT NEW.id_image, 'id_type_file', CONVERT(OLD.id_type_file, CHAR), CONVERT(NEW.id_type_file, CHAR), NEW.id_change_set - WHERE NOT OLD.id_type_file <=> NEW.id_type_file - UNION - -- Changed id_product - SELECT NEW.id_image, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_permutation - SELECT NEW.id_image, 'id_permutation', CONVERT(OLD.id_permutation, CHAR), CONVERT(NEW.id_permutation, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed url - SELECT NEW.id_image, 'url', OLD.url, NEW.url, NEW.id_change_set - WHERE NOT OLD.url <=> NEW.url - UNION - -- Changed active - SELECT NEW.id_image, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_image, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Image -BEFORE UPDATE ON Shop_Image -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Image(); diff --git a/static/PostgreSQL/322_tri_Shop_Delivery_Option.sql b/static/PostgreSQL/322_tri_Shop_Delivery_Option.sql deleted file mode 100644 index 7d3a1fb7..00000000 --- a/static/PostgreSQL/322_tri_Shop_Delivery_Option.sql +++ /dev/null @@ -1,75 +0,0 @@ - --- Shop Delivery Option Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_Delivery_Option() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Delivery_Option -BEFORE INSERT ON Shop_Delivery_Option -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Delivery_Option(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Delivery_Option() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Delivery_Option_Audit ( - id_option, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_option, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_option, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed latency_delivery_min - SELECT NEW.id_option, 'latency_delivery_min', CONVERT(OLD.latency_delivery_min, CHAR), CONVERT(NEW.latency_delivery_min, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_delivery_min <=> NEW.latency_delivery_min - UNION - -- Changed latency_delivery_max - SELECT NEW.id_option, 'latency_delivery_max', CONVERT(OLD.latency_delivery_max, CHAR), CONVERT(NEW.latency_delivery_max, CHAR), NEW.id_change_set - WHERE NOT OLD.latency_delivery_max <=> NEW.latency_delivery_max - UNION - -- Changed quantity_min - SELECT NEW.id_option, 'quantity_min', CONVERT(OLD.quantity_min, CHAR), CONVERT(NEW.quantity_min, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_option, 'quantity_max', CONVERT(OLD.quantity_max, CHAR), CONVERT(NEW.quantity_max, CHAR), NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed active - SELECT NEW.id_option, 'active', CONVERT(OLD.active, CHAR), CONVERT(NEW.active, CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - UNION - -- Changed display_order - SELECT NEW.id_option, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Delivery_Option -BEFORE UPDATE ON Shop_Delivery_Option -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Delivery_Option(); diff --git a/static/PostgreSQL/324_tri_Shop_Product_Delivery_Option_Link.sql b/static/PostgreSQL/324_tri_Shop_Product_Delivery_Option_Link.sql deleted file mode 100644 index 1a021d03..00000000 --- a/static/PostgreSQL/324_tri_Shop_Product_Delivery_Option_Link.sql +++ /dev/null @@ -1,77 +0,0 @@ - --- Shop Product Delivery Option Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Product_Permutation_Delivery_Option_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Product_Permutation_Delivery_Option_Link -BEFORE INSERT ON Shop_Product_Permutation_Delivery_Option_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Product_Permutation_Delivery_Option_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Product_Permutation_Delivery_Option_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Product_Permutation_Delivery_Option_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_product - SELECT NEW.id_link, 'id_product', CONVERT(OLD.id_product, CHAR), CONVERT(NEW.id_product, CHAR), NEW.id_change_set - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', CONVERT(OLD.id_permutation, CHAR), CONVERT(NEW.id_permutation, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed id_option - SELECT NEW.id_link, 'id_option', CONVERT(OLD.id_option, CHAR), CONVERT(NEW.id_option, CHAR), NEW.id_change_set - WHERE NOT OLD.id_option <=> NEW.id_option - UNION - -- Changed id_region - SELECT NEW.id_link, 'id_region', CONVERT(OLD.id_region, CHAR), CONVERT(NEW.id_region, CHAR), NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - */ - -- Changed price_local - SELECT NEW.id_link, 'price_local', CONVERT(OLD.price_local, CHAR), CONVERT(NEW.price_local, CHAR), NEW.id_change_set - WHERE NOT OLD.price_local <=> NEW.price_local - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Product_Permutation_Delivery_Option_Link -BEFORE UPDATE ON Shop_Product_Permutation_Delivery_Option_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Product_Permutation_Delivery_Option_Link(); diff --git a/static/PostgreSQL/330_tri_Shop_Discount.sql b/static/PostgreSQL/330_tri_Shop_Discount.sql deleted file mode 100644 index 9ab188a1..00000000 --- a/static/PostgreSQL/330_tri_Shop_Discount.sql +++ /dev/null @@ -1,87 +0,0 @@ - --- Shop Discount - -CREATE OR REPLACE FUNCTION before_insert_Shop_Discount() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Discount -BEFORE INSERT ON Shop_Discount -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Discount(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Discount() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Discount_Audit ( - id_discount, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_discount, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_discount, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed multiplier - SELECT NEW.id_discount, 'multiplier', OLD.multiplier, NEW.multiplier, NEW.id_change_set - WHERE NOT OLD.multiplier <=> NEW.multiplier - UNION - -- Changed subtractor - SELECT NEW.id_discount, 'subtractor', OLD.subtractor, NEW.subtractor, NEW.id_change_set - WHERE NOT OLD.subtractor <=> NEW.subtractor - UNION - -- Changed apply_multiplier_first - SELECT NEW.id_discount, 'apply_multiplier_first', CONVERT(CONVERT(OLD.apply_multiplier_first, SIGNED), CHAR), CONVERT(CONVERT(NEW.apply_multiplier_first, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.apply_multiplier_first <=> NEW.apply_multiplier_first - UNION - -- Changed quantity_min - SELECT NEW.id_discount, 'quantity_min', OLD.quantity_min, NEW.quantity_min, NEW.id_change_set - WHERE NOT OLD.quantity_min <=> NEW.quantity_min - UNION - -- Changed quantity_max - SELECT NEW.id_discount, 'quantity_max', OLD.quantity_max, NEW.quantity_max, NEW.id_change_set - WHERE NOT OLD.quantity_max <=> NEW.quantity_max - UNION - -- Changed date_start - SELECT NEW.id_discount, 'date_start', OLD.date_start, NEW.date_start, NEW.id_change_set - WHERE NOT OLD.date_start <=> NEW.date_start - UNION - -- Changed date_end - SELECT NEW.id_discount, 'date_end', OLD.date_end, NEW.date_end, NEW.id_change_set - WHERE NOT OLD.date_end <=> NEW.date_end - UNION - -- Changed display_order - SELECT NEW.id_discount, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_discount, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Discount -BEFORE UPDATE ON Shop_Discount -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Discount(); diff --git a/static/PostgreSQL/332_tri_Shop_Discount_Region_Currency_Link.sql b/static/PostgreSQL/332_tri_Shop_Discount_Region_Currency_Link.sql deleted file mode 100644 index 9176681b..00000000 --- a/static/PostgreSQL/332_tri_Shop_Discount_Region_Currency_Link.sql +++ /dev/null @@ -1,61 +0,0 @@ - --- Shop Discount Region Currency Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Discount_Region_Currency_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Discount_Region_Currency_Link -BEFORE INSERT ON Shop_Discount_Region_Currency_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Discount_Region_Currency_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Discount_Region_Currency_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Discount_Region_Currency_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_discount - SELECT NEW.id_link, 'id_discount', CONVERT(OLD.id_discount, CHAR), CONVERT(NEW.id_discount, CHAR), NEW.id_change_set - WHERE NOT OLD.id_discount <=> NEW.id_discount - UNION - -- Changed id_region - SELECT NEW.id_link, 'id_region', CONVERT(OLD.id_region, CHAR), CONVERT(NEW.id_region, CHAR), NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - */ - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Discount_Region_Currency_Link -BEFORE UPDATE ON Shop_Discount_Region_Currency_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Discount_Region_Currency_Link(); diff --git a/static/PostgreSQL/353_tri_Shop_Permission_Group.sql b/static/PostgreSQL/353_tri_Shop_Permission_Group.sql deleted file mode 100644 index aaec1645..00000000 --- a/static/PostgreSQL/353_tri_Shop_Permission_Group.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Permission Group - -CREATE OR REPLACE FUNCTION before_insert_Shop_Permission_Group() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Permission_Group -BEFORE INSERT ON Shop_Permission_Group -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Permission_Group(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Permission_Group() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Permission_Group_Audit ( - id_group, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_group, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_group, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_group, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_group, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Permission_Group -BEFORE UPDATE ON Shop_Permission_Group -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Permission_Group(); diff --git a/static/PostgreSQL/355_tri_Shop_Permission.sql b/static/PostgreSQL/355_tri_Shop_Permission.sql deleted file mode 100644 index 65d22c36..00000000 --- a/static/PostgreSQL/355_tri_Shop_Permission.sql +++ /dev/null @@ -1,71 +0,0 @@ - --- Shop Permission - -CREATE OR REPLACE FUNCTION before_insert_Shop_Permission() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Permission -BEFORE INSERT ON Shop_Permission -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Permission(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Permission() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Permission_Audit ( - id_permission, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_permission, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_permission, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed id_permission_group - SELECT NEW.id_permission, 'id_permission_group', CONVERT(OLD.id_permission_group, CHAR), CONVERT(NEW.id_permission_group, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permission_group <=> NEW.id_permission_group - UNION - -- Changed Id_access_level_required - SELECT NEW.id_permission, 'Id_access_level_required', CONVERT(OLD.Id_access_level_required, CHAR), CONVERT(NEW.Id_access_level_required, CHAR), NEW.id_change_set - WHERE NOT OLD.Id_access_level_required <=> NEW.Id_access_level_required - UNION - -- Changed active - SELECT NEW.id_permission, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_permission, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Permission -BEFORE UPDATE ON Shop_Permission -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Permission(); diff --git a/static/PostgreSQL/357_tri_Shop_Role.sql b/static/PostgreSQL/357_tri_Shop_Role.sql deleted file mode 100644 index 89979d74..00000000 --- a/static/PostgreSQL/357_tri_Shop_Role.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Role - -CREATE OR REPLACE FUNCTION before_insert_Shop_Role() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Role -BEFORE INSERT ON Shop_Role -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Role(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Role() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Role_Audit ( - id_role, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_role, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_role, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed active - SELECT NEW.id_role, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_role, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Role -BEFORE UPDATE ON Shop_Role -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Role(); diff --git a/static/PostgreSQL/359_tri_Shop_Role_Permission_Link.sql b/static/PostgreSQL/359_tri_Shop_Role_Permission_Link.sql deleted file mode 100644 index cefe3ce1..00000000 --- a/static/PostgreSQL/359_tri_Shop_Role_Permission_Link.sql +++ /dev/null @@ -1,65 +0,0 @@ - --- Shop Role Permission Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Role_Permission_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Role_Permission_Link -BEFORE INSERT ON Shop_Role_Permission_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Role_Permission_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Role_Permission_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Role_Permission_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - /* - -- Changed id_role - SELECT NEW.id_link, 'id_role', CONVERT(OLD.id_role, CHAR), CONVERT(NEW.id_role, CHAR), NEW.id_change_set - WHERE NOT OLD.id_role <=> NEW.id_role - UNION - -- Changed id_permission - SELECT NEW.id_link, 'id_permission', CONVERT(OLD.id_permission, CHAR), CONVERT(NEW.id_permission, CHAR), NEW.id_change_set - WHERE NOT OLD.id_permission <=> NEW.id_permission - UNION - */ - -- Changed id_access_level - SELECT NEW.id_link, 'id_access_level', CONVERT(OLD.id_access_level, CHAR), CONVERT(NEW.id_access_level, CHAR), NEW.id_change_set - WHERE NOT OLD.id_access_level <=> NEW.id_access_level - UNION - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Role_Permission_Link -BEFORE UPDATE ON Shop_Role_Permission_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Role_Permission_Link(); diff --git a/static/PostgreSQL/361_tri_Shop_User.sql b/static/PostgreSQL/361_tri_Shop_User.sql deleted file mode 100644 index 55ca4803..00000000 --- a/static/PostgreSQL/361_tri_Shop_User.sql +++ /dev/null @@ -1,71 +0,0 @@ - --- Shop User - -CREATE OR REPLACE FUNCTION before_insert_Shop_User() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User -BEFORE INSERT ON Shop_User -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Audit ( - id_user, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_user_oauth - SELECT NEW.id_user, 'id_user_oauth', OLD.id_user_oauth, NEW.id_user_oauth, NEW.id_change_set - WHERE NOT (OLD.id_user_oauth <=> NEW.id_user_oauth) - UNION - -- Changed name - SELECT NEW.id_user, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT (OLD.name <=> NEW.name) - UNION - -- Changed email - SELECT NEW.id_user, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT (OLD.email <=> NEW.email) - UNION - -- Changed is_email_verified - SELECT NEW.id_user, 'is_email_verified', OLD.is_email_verified, NEW.is_email_verified, NEW.id_change_set - WHERE NOT (OLD.is_email_verified <=> NEW.is_email_verified) - UNION - -- Changed is_super_user - SELECT NEW.id_user, 'is_super_user', CONVERT(CONVERT(OLD.is_super_user, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_super_user, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.is_super_user <=> NEW.is_super_user) - UNION - -- Changed active - SELECT NEW.id_user, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User -BEFORE UPDATE ON Shop_User -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User(); diff --git a/static/PostgreSQL/363_tri_Shop_User_Role_Link.sql b/static/PostgreSQL/363_tri_Shop_User_Role_Link.sql deleted file mode 100644 index 6ac8b839..00000000 --- a/static/PostgreSQL/363_tri_Shop_User_Role_Link.sql +++ /dev/null @@ -1,51 +0,0 @@ - --- Shop User Role Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Role_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Role_Link -BEFORE INSERT ON Shop_User_Role_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Role_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Role_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Role_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed active - SELECT NEW.id_link, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Role_Link -BEFORE UPDATE ON Shop_User_Role_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Role_Link(); diff --git a/static/PostgreSQL/365_tri_Shop_Address.sql b/static/PostgreSQL/365_tri_Shop_Address.sql deleted file mode 100644 index 037d5e49..00000000 --- a/static/PostgreSQL/365_tri_Shop_Address.sql +++ /dev/null @@ -1,83 +0,0 @@ - --- Shop Address - -CREATE OR REPLACE FUNCTION before_insert_Shop_Address() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Address -BEFORE INSERT ON Shop_Address -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Address(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Address() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Address_Audit ( - id_address, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed region - SELECT NEW.id_address, 'id_region', OLD.id_region, NEW.id_region, NEW.id_change_set - WHERE NOT OLD.id_region <=> NEW.id_region - UNION - -- Changed name_full - SELECT NEW.id_address, 'name_full', OLD.name_full, NEW.name_full, NEW.id_change_set - WHERE NOT OLD.name_full <=> NEW.name_full - UNION - -- Changed phone_number - SELECT NEW.id_address, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed postcode - SELECT NEW.id_address, 'postcode', OLD.postcode, NEW.postcode, NEW.id_change_set - WHERE NOT OLD.postcode <=> NEW.postcode - UNION - -- Changed address_line_1 - SELECT NEW.id_address, 'address_line_1', OLD.address_line_1, NEW.address_line_1, NEW.id_change_set - WHERE NOT OLD.address_line_1 <=> NEW.address_line_1 - UNION - -- Changed address_line_2 - SELECT NEW.id_address, 'address_line_2', OLD.address_line_2, NEW.address_line_2, NEW.id_change_set - WHERE NOT OLD.address_line_2 <=> NEW.address_line_2 - UNION - -- Changed city - SELECT NEW.id_address, 'city', OLD.city, NEW.city, NEW.id_change_set - WHERE NOT OLD.city <=> NEW.city - UNION - -- Changed county - SELECT NEW.id_address, 'county', OLD.county, NEW.county, NEW.id_change_set - WHERE NOT OLD.county <=> NEW.county - UNION - -- Changed active - SELECT NEW.id_address, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Address -BEFORE UPDATE ON Shop_Address -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Address(); \ No newline at end of file diff --git a/static/PostgreSQL/367_tri_Shop_User_Basket.sql b/static/PostgreSQL/367_tri_Shop_User_Basket.sql deleted file mode 100644 index 0884a259..00000000 --- a/static/PostgreSQL/367_tri_Shop_User_Basket.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Product Variation Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Basket() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Basket -BEFORE INSERT ON Shop_User_Basket -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Basket(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Basket() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.id_change_set_user <=> OLD.id_change_set_user THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Basket_Audit ( - id_item, - name_field, - value_prev, - value_new, - id_change_set_user - ) - -- Changed id_user - SELECT NEW.id_item, 'id_user', OLD.id_user, NEW.id_user, NEW.id_change_set_user - WHERE NOT OLD.id_user <=> NEW.id_user - UNION - -- Changed id_product - SELECT NEW.id_item, 'id_product', OLD.id_product, NEW.id_product, NEW.id_change_set_user - WHERE NOT OLD.id_product <=> NEW.id_product - UNION - -- Changed quantity - SELECT NEW.id_item, 'quantity', CONVERT(OLD.quantity, CHAR), CONVERT(NEW.quantity, CHAR), NEW.id_change_set_user - WHERE NOT (OLD.quantity <=> NEW.quantity) - UNION - -- Changed active - SELECT NEW.id_item, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set_user - WHERE NOT (OLD.active <=> NEW.active) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Basket -BEFORE UPDATE ON Shop_User_Basket -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Basket(); diff --git a/static/PostgreSQL/369_tri_Shop_User_Order_Status.sql b/static/PostgreSQL/369_tri_Shop_User_Order_Status.sql deleted file mode 100644 index 74560390..00000000 --- a/static/PostgreSQL/369_tri_Shop_User_Order_Status.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop User Order Type - -CREATE OR REPLACE FUNCTION before_insert_Shop_User_Order_Status() -RETURNS TRIGGER AS $$ -BEGIN - NEW.created_on = CURRENT_TIMESTAMP; - NEW.created_by = CURRENT_USER; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_User_Order_Status -BEFORE INSERT ON Shop_User_Order_Status -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_User_Order_Status(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_User_Order_Status() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_User_Order_Status_Audit ( - id_Status, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed code - SELECT NEW.id_Status, 'code', OLD.code, NEW.code, NEW.id_change_set - WHERE NOT OLD.code <=> NEW.code - UNION - -- Changed name - SELECT NEW.id_Status, 'name', OLD.name, NEW.name, NEW.id_change_set - WHERE NOT OLD.name <=> NEW.name - UNION - -- Changed name_plural - SELECT NEW.id_Status, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed active - SELECT NEW.id_Status, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set - WHERE NOT (OLD.active <=> NEW.active) - UNION - -- Changed display_order - SELECT NEW.id_Status, 'display_order', CONVERT(OLD.display_order, CHAR), CONVERT(NEW.display_order, CHAR), NEW.id_change_set - WHERE NOT (OLD.display_order <=> NEW.display_order) - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_User_Order_Status -BEFORE UPDATE ON Shop_User_Order_Status -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_User_Order_Status(); diff --git a/static/PostgreSQL/381.0_tri_Shop_Supplier.sql b/static/PostgreSQL/381.0_tri_Shop_Supplier.sql deleted file mode 100644 index 307b0688..00000000 --- a/static/PostgreSQL/381.0_tri_Shop_Supplier.sql +++ /dev/null @@ -1,83 +0,0 @@ - --- Shop Supplier - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier -BEFORE INSERT ON Shop_Supplier -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Audit ( - id_supplier, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_company - SELECT NEW.id_supplier, 'name_company', OLD.name_company, NEW.name_company, NEW.id_change_set - WHERE NOT OLD.name_company <=> NEW.name_company - UNION - -- Changed name_contact - SELECT NEW.id_supplier, 'name_contact', OLD.name_contact, NEW.name_contact, NEW.id_change_set - WHERE NOT OLD.name_contact <=> NEW.name_contact - UNION - -- Changed department_contact - SELECT NEW.id_supplier, 'department_contact', OLD.department_contact, NEW.department_contact, NEW.id_change_set - WHERE NOT OLD.department_contact <=> NEW.department_contact - UNION - -- Changed id_address - SELECT NEW.id_supplier, 'id_address', OLD.id_address, NEW.id_address, NEW.id_change_set - WHERE NOT OLD.id_address <=> NEW.id_address - UNION - -- Changed phone_number - SELECT NEW.id_supplier, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed fax - SELECT NEW.id_supplier, 'fax', OLD.fax, NEW.fax, NEW.id_change_set - WHERE NOT OLD.fax <=> NEW.fax - UNION - -- Changed email - SELECT NEW.id_supplier, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT OLD.email <=> NEW.email - UNION - -- Changed website - SELECT NEW.id_supplier, 'website', OLD.website, NEW.website, NEW.id_change_set - WHERE NOT OLD.website <=> NEW.website - UNION - -- Changed id_currency - SELECT NEW.id_supplier, 'id_currency', OLD.id_currency, NEW.id_currency, NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier -BEFORE UPDATE ON Shop_Supplier -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier(); diff --git a/static/PostgreSQL/381.2_tri_Shop_Unit_Measurement.sql b/static/PostgreSQL/381.2_tri_Shop_Unit_Measurement.sql deleted file mode 100644 index 87aa9ebc..00000000 --- a/static/PostgreSQL/381.2_tri_Shop_Unit_Measurement.sql +++ /dev/null @@ -1,67 +0,0 @@ - --- Shop Unit of Measurement - -CREATE OR REPLACE FUNCTION before_insert_Shop_Unit_Measurement() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Unit_Measurement -BEFORE INSERT ON Shop_Unit_Measurement -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Unit_Measurement(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Unit_Measurement() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Unit_Measurement_Audit ( - id_unit_measurement, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_singular - SELECT NEW.id_unit_measurement, 'name_singular', OLD.name_singular, NEW.name_singular, NEW.id_change_set - WHERE NOT OLD.name_singular <=> NEW.name_singular - UNION - -- Changed name_plural - SELECT NEW.id_unit_measurement, 'name_plural', OLD.name_plural, NEW.name_plural, NEW.id_change_set - WHERE NOT OLD.name_plural <=> NEW.name_plural - UNION - -- Changed symbol - SELECT NEW.id_unit_measurement, 'symbol', OLD.symbol, NEW.symbol, NEW.id_change_set - WHERE NOT OLD.symbol <=> NEW.symbol - UNION - -- Changed is_base_unit - SELECT NEW.id_unit_measurement, 'is_base_unit', OLD.is_base_unit, NEW.is_base_unit, NEW.id_change_set - WHERE NOT OLD.is_base_unit <=> NEW.is_base_unit - UNION - -- Changed active - SELECT NEW.id_unit_measurement, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Unit_Measurement -BEFORE UPDATE ON Shop_Unit_Measurement -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Unit_Measurement(); diff --git a/static/PostgreSQL/381.4_tri_Shop_Unit_Of_Measurement_Conversion.sql b/static/PostgreSQL/381.4_tri_Shop_Unit_Of_Measurement_Conversion.sql deleted file mode 100644 index bbc78792..00000000 --- a/static/PostgreSQL/381.4_tri_Shop_Unit_Of_Measurement_Conversion.sql +++ /dev/null @@ -1,71 +0,0 @@ - --- Shop Unit of Measurement Conversion - -CREATE OR REPLACE FUNCTION before_insert_Shop_Unit_Measurement_Conversion() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Unit_Measurement_Conversion -BEFORE INSERT ON Shop_Unit_Measurement_Conversion -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Unit_Measurement_Conversion(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Unit_Measurement_Conversion() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Unit_Measurement_Conversion_Audit ( - id_conversion, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_unit_derived - SELECT NEW.id_conversion, 'id_unit_derived', OLD.id_unit_derived, NEW.id_unit_derived, NEW.id_change_set - WHERE NOT OLD.id_unit_derived <=> NEW.id_unit_derived - UNION - -- Changed id_unit_base - SELECT NEW.id_conversion, 'id_unit_base', OLD.id_unit_base, NEW.id_unit_base, NEW.id_change_set - WHERE NOT OLD.id_unit_base <=> NEW.id_unit_base - UNION - -- Changed power_unit_base - SELECT NEW.id_conversion, 'power_unit_base', OLD.power_unit_base, NEW.power_unit_base, NEW.id_change_set - WHERE NOT OLD.power_unit_base <=> NEW.power_unit_base - UNION - -- Changed multiplier_unit_base - SELECT NEW.id_conversion, 'multiplier_unit_base', OLD.multiplier_unit_base, NEW.multiplier_unit_base, NEW.id_change_set - WHERE NOT OLD.multiplier_unit_base <=> NEW.multiplier_unit_base - UNION - -- Changed increment_unit_base - SELECT NEW.id_conversion, 'active', OLD.increment_unit_base, NEW.increment_unit_base, NEW.id_change_set - WHERE NOT OLD.increment_unit_base <=> NEW.increment_unit_base - UNION - -- Changed active - SELECT NEW.id_conversion, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Unit_Measurement_Conversion -BEFORE UPDATE ON Shop_Unit_Measurement_Conversion -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Unit_Measurement_Conversion(); diff --git a/static/PostgreSQL/381.6_tri_Shop_Supplier_Purchase_Order.sql b/static/PostgreSQL/381.6_tri_Shop_Supplier_Purchase_Order.sql deleted file mode 100644 index 4c2cc08c..00000000 --- a/static/PostgreSQL/381.6_tri_Shop_Supplier_Purchase_Order.sql +++ /dev/null @@ -1,78 +0,0 @@ - --- Shop Supplier Purchase Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier_Purchase_Order -BEFORE INSERT ON Shop_Supplier_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier_Purchase_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Purchase_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_supplier_ordered - SELECT NEW.id_order, 'id_supplier_ordered', OLD.id_supplier_ordered, NEW.id_supplier_ordered, NEW.id_change_set - WHERE NOT OLD.id_supplier_ordered <=> NEW.id_supplier_ordered - UNION - -- Changed cost_total_local - SELECT NEW.id_order, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_order, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - /* - UNION - -- Changed latency_delivery - SELECT NEW.id_order, 'latency_delivery', OLD.latency_delivery, NEW.latency_delivery, NEW.id_change_set - WHERE NOT OLD.latency_delivery <=> NEW.latency_delivery - UNION - -- Changed quantity_ordered - SELECT NEW.id_order, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_order, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_received - SELECT NEW.id_order, 'quantity_received', OLD.quantity_received, NEW.quantity_received, NEW.id_change_set - WHERE NOT OLD.quantity_received <=> NEW.quantity_received - */ - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier_Purchase_Order -BEFORE UPDATE ON Shop_Supplier_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier_Purchase_Order(); - diff --git a/static/PostgreSQL/381.8_tri_Shop_Supplier_Purchase_Order_Product_Link.sql b/static/PostgreSQL/381.8_tri_Shop_Supplier_Purchase_Order_Product_Link.sql deleted file mode 100644 index 12c95581..00000000 --- a/static/PostgreSQL/381.8_tri_Shop_Supplier_Purchase_Order_Product_Link.sql +++ /dev/null @@ -1,87 +0,0 @@ - --- Shop Supplier Purchase Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Supplier_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Supplier_Purchase_Order_Product_Link -BEFORE INSERT ON Shop_Supplier_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Supplier_Purchase_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Supplier_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed cost_total_local - SELECT NEW.id_link, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_link, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed quantity_ordered - SELECT NEW.id_link, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_received - SELECT NEW.id_link, 'quantity_received', OLD.quantity_received, NEW.quantity_received, NEW.id_change_set - WHERE NOT OLD.quantity_received <=> NEW.quantity_received - UNION - -- Changed latency_delivery_days - SELECT NEW.id_link, 'latency_delivery_days', OLD.latency_delivery_days, NEW.latency_delivery_days, NEW.id_change_set - WHERE NOT OLD.latency_delivery_days <=> NEW.latency_delivery_days - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Supplier_Purchase_Order_Product_Link -BEFORE UPDATE ON Shop_Supplier_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Supplier_Purchase_Order_Product_Link(); diff --git a/static/PostgreSQL/383_tri_Shop_Manufacturing_Purchase_Order.sql b/static/PostgreSQL/383_tri_Shop_Manufacturing_Purchase_Order.sql deleted file mode 100644 index ab23291e..00000000 --- a/static/PostgreSQL/383_tri_Shop_Manufacturing_Purchase_Order.sql +++ /dev/null @@ -1,63 +0,0 @@ - --- Shop Manufacturing Purchase Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Manufacturing_Purchase_Order -BEFORE INSERT ON Shop_Manufacturing_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Manufacturing_Purchase_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Manufacturing_Purchase_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed cost_total_local - SELECT NEW.id_order, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed value_produced_total_local - SELECT NEW.id_order, 'value_produced_total_local', OLD.value_produced_total_local, NEW.value_produced_total_local, NEW.id_change_set - WHERE NOT OLD.value_produced_total_local <=> NEW.value_produced_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_order, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed active - SELECT NEW.id_order, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Manufacturing_Purchase_Order -BEFORE UPDATE ON Shop_Manufacturing_Purchase_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Manufacturing_Purchase_Order(); diff --git a/static/PostgreSQL/385_tri_Shop_Manufacturing_Purchase_Order_Product_Link.sql b/static/PostgreSQL/385_tri_Shop_Manufacturing_Purchase_Order_Product_Link.sql deleted file mode 100644 index ed7062e0..00000000 --- a/static/PostgreSQL/385_tri_Shop_Manufacturing_Purchase_Order_Product_Link.sql +++ /dev/null @@ -1,87 +0,0 @@ - --- Shop Manufacturing Purchase Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Manufacturing_Purch_Order_Product_Link -BEFORE INSERT ON Shop_Manufacturing_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Manufacturing_Purchase_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Manufacturing_Purch_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed cost_total_local - SELECT NEW.id_link, 'cost_total_local', OLD.cost_total_local, NEW.cost_total_local, NEW.id_change_set - WHERE NOT OLD.cost_total_local <=> NEW.cost_total_local - UNION - -- Changed id_currency_cost - SELECT NEW.id_link, 'id_currency_cost', OLD.id_currency_cost, NEW.id_currency_cost, NEW.id_change_set - WHERE NOT OLD.id_currency_cost <=> NEW.id_currency_cost - UNION - -- Changed quantity_used - SELECT NEW.id_link, 'quantity_used', OLD.quantity_used, NEW.quantity_used, NEW.id_change_set - WHERE NOT OLD.quantity_used <=> NEW.quantity_used - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_produced - SELECT NEW.id_link, 'quantity_produced', OLD.quantity_produced, NEW.quantity_produced, NEW.id_change_set - WHERE NOT OLD.quantity_produced <=> NEW.quantity_produced - UNION - -- Changed latency_manufacture - SELECT NEW.id_link, 'latency_manufacture', OLD.latency_manufacture, NEW.latency_manufacture, NEW.id_change_set - WHERE NOT OLD.latency_manufacture <=> NEW.latency_manufacture - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Manufacturing_Purch_Order_Product_Link -BEFORE UPDATE ON Shop_Manufacturing_Purchase_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Manufacturing_Purch_Order_Product_Link(); - diff --git a/static/PostgreSQL/387.0_tri_Shop_Customer.sql b/static/PostgreSQL/387.0_tri_Shop_Customer.sql deleted file mode 100644 index 4c318e17..00000000 --- a/static/PostgreSQL/387.0_tri_Shop_Customer.sql +++ /dev/null @@ -1,79 +0,0 @@ - --- Shop Customer - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer -BEFORE INSERT ON Shop_Customer -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Audit ( - id_customer, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed name_company - SELECT NEW.id_customer, 'name_company', OLD.name_company, NEW.name_company, NEW.id_change_set - WHERE NOT OLD.name_company <=> NEW.name_company - UNION - -- Changed name_contact - SELECT NEW.id_customer, 'name_contact', OLD.name_contact, NEW.name_contact, NEW.id_change_set - WHERE NOT OLD.name_contact <=> NEW.name_contact - UNION - -- Changed department_contact - SELECT NEW.id_customer, 'department_contact', OLD.department_contact, NEW.department_contact, NEW.id_change_set - WHERE NOT OLD.department_contact <=> NEW.department_contact - UNION - -- Changed id_address - SELECT NEW.id_customer, 'id_address', OLD.id_address, NEW.id_address, NEW.id_change_set - WHERE NOT OLD.id_address <=> NEW.id_address - UNION - -- Changed phone_number - SELECT NEW.id_customer, 'phone_number', OLD.phone_number, NEW.phone_number, NEW.id_change_set - WHERE NOT OLD.phone_number <=> NEW.phone_number - UNION - -- Changed email - SELECT NEW.id_customer, 'email', OLD.email, NEW.email, NEW.id_change_set - WHERE NOT OLD.email <=> NEW.email - UNION - -- Changed id_currency - SELECT NEW.id_customer, 'id_currency', OLD.id_currency, NEW.id_currency, NEW.id_change_set - WHERE NOT OLD.id_currency <=> NEW.id_currency - UNION - -- Changed active - SELECT NEW.id_customer, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer -BEFORE UPDATE ON Shop_Customer -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer(); - diff --git a/static/PostgreSQL/387.2_tri_Shop_Customer_Sales_Order.sql b/static/PostgreSQL/387.2_tri_Shop_Customer_Sales_Order.sql deleted file mode 100644 index f752f5e7..00000000 --- a/static/PostgreSQL/387.2_tri_Shop_Customer_Sales_Order.sql +++ /dev/null @@ -1,62 +0,0 @@ - --- Shop Customer Sales Order - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer_Sales_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer_Sales_Order -BEFORE INSERT ON Shop_Customer_Sales_Order -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer_Sales_Order(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer_Sales_Order() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Sales_Order_Audit ( - id_order, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_customer - SELECT NEW.id_order, 'id_customer', OLD.id_customer, NEW.id_customer, NEW.id_change_set - WHERE NOT OLD.id_customer <=> NEW.id_customer - UNION - -- Changed price_total_local - SELECT NEW.id_order, 'price_total_local', OLD.price_total_local, NEW.price_total_local, NEW.id_change_set - WHERE NOT OLD.price_total_local <=> NEW.price_total_local - UNION - -- Changed id_currency_price - SELECT NEW.id_order, 'id_currency_price', OLD.id_currency_price, NEW.id_currency_price, NEW.id_change_set - WHERE NOT OLD.id_currency_price <=> NEW.id_currency_price - UNION - -- Changed active - SELECT NEW.id_order, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer_Sales_Order -BEFORE UPDATE ON Shop_Customer_Sales_Order -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer_Sales_Order(); diff --git a/static/PostgreSQL/389_tri_Shop_Customer_Sales_Order_Product_Link.sql b/static/PostgreSQL/389_tri_Shop_Customer_Sales_Order_Product_Link.sql deleted file mode 100644 index 80a03098..00000000 --- a/static/PostgreSQL/389_tri_Shop_Customer_Sales_Order_Product_Link.sql +++ /dev/null @@ -1,86 +0,0 @@ - --- Shop Customer Sales Order Product Link - -CREATE OR REPLACE FUNCTION before_insert_Shop_Customer_Sales_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF NEW.created_on IS NULL THEN - NEW.created_on = CURRENT_TIMESTAMP; - END IF; - IF NEW.created_by IS NULL THEN - NEW.created_by = CURRENT_USER; - END IF; - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_insert_Shop_Customer_Sales_Order_Product_Link -BEFORE INSERT ON Shop_Customer_Sales_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_insert_Shop_Customer_Sales_Order_Product_Link(); - - -CREATE OR REPLACE FUNCTION before_update_Shop_Customer_Sales_Order_Product_Link() -RETURNS TRIGGER AS $$ -BEGIN - IF OLD.id_change_set IS NOT DISTINCT FROM NEW.id_change_set THEN - RAISE EXCEPTION 'New change Set ID must be provided.' - USING ERRCODE = '45000'; - END IF; - - INSERT INTO Shop_Customer_Sales_Order_Product_Link_Audit ( - id_link, - name_field, - value_prev, - value_new, - id_change_set - ) - -- Changed id_order - SELECT NEW.id_link, 'id_order', OLD.id_order, NEW.id_order, NEW.id_change_set - WHERE NOT OLD.id_order <=> NEW.id_order - UNION - -- Changed id_permutation - SELECT NEW.id_link, 'id_permutation', OLD.id_permutation, NEW.id_permutation, NEW.id_change_set - WHERE NOT OLD.id_permutation <=> NEW.id_permutation - UNION - -- Changed price_total_local - SELECT NEW.id_link, 'price_total_local', OLD.price_total_local, NEW.price_total_local, NEW.id_change_set - WHERE NOT OLD.price_total_local <=> NEW.price_total_local - UNION - -- Changed id_currency_price - SELECT NEW.id_link, 'id_currency_price', OLD.id_currency_price, NEW.id_currency_price, NEW.id_change_set - WHERE NOT OLD.id_currency_price <=> NEW.id_currency_price - UNION - -- Changed quantity_ordered - SELECT NEW.id_link, 'quantity_ordered', OLD.quantity_ordered, NEW.quantity_ordered, NEW.id_change_set - WHERE NOT OLD.quantity_ordered <=> NEW.quantity_ordered - UNION - -- Changed id_unit_quantity - SELECT NEW.id_link, 'id_unit_quantity', OLD.id_unit_quantity, NEW.id_unit_quantity, NEW.id_change_set - WHERE NOT OLD.id_unit_quantity <=> NEW.id_unit_quantity - UNION - -- Changed quantity_delivered - SELECT NEW.id_link, 'quantity_delivered', OLD.quantity_delivered, NEW.quantity_delivered, NEW.id_change_set - WHERE NOT OLD.quantity_delivered <=> NEW.quantity_delivered - UNION - -- Changed latency_delivery_days - SELECT NEW.id_link, 'latency_delivery_days', OLD.latency_delivery_days, NEW.latency_delivery_days, NEW.id_change_set - WHERE NOT OLD.latency_delivery_days <=> NEW.latency_delivery_days - UNION - -- Changed display_order - SELECT NEW.id_link, 'display_order', OLD.display_order, NEW.display_order, NEW.id_change_set - WHERE NOT OLD.display_order <=> NEW.display_order - UNION - -- Changed active - SELECT NEW.id_link, 'active', OLD.active, NEW.active, NEW.id_change_set - WHERE NOT OLD.active <=> NEW.active - ; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE TRIGGER tri_before_update_Shop_Customer_Sales_Order_Product_Link -BEFORE UPDATE ON Shop_Customer_Sales_Order_Product_Link -FOR EACH ROW -EXECUTE FUNCTION before_update_Shop_Customer_Sales_Order_Product_Link(); diff --git a/static/PostgreSQL/600_p_shop_save_product.sql b/static/PostgreSQL/600_p_shop_save_product.sql deleted file mode 100644 index d29a1d12..00000000 --- a/static/PostgreSQL/600_p_shop_save_product.sql +++ /dev/null @@ -1,257 +0,0 @@ - - -DO $$ -BEGIN - RAISE NOTICE 'TRIGGER CREATION COMPLETE'; -END $$; - -/* -CREATE OR REPLACE PROCEDURE p_save_product ( - IN a_guid UUID, - -) -AS $$ -BEGIN - - -- Argument default values - IF a_ids_category IS NULL THEN - SET a_ids_category = ''; - END IF; - IF a_get_inactive_categories IS NULL THEN - SET a_get_inactive_categories = FALSE; - END IF; - /* - IF a_get_all_categories IS NULL THEN - SET a_get_all_categories = FALSE; - END IF; - */ - IF a_ids_product IS NULL THEN - SET a_ids_product = ''; - END IF; - IF a_get_inactive_products IS NULL THEN - SET a_get_inactive_products = FALSE; - END IF; - IF a_get_first_product_only IS NULL THEN - SET a_get_first_product_only = TRUE; - END IF; - IF a_get_all_products IS NULL THEN - SET a_get_all_products = FALSE; - END IF; - IF a_ids_image IS NULL THEN - SET a_ids_image = ''; - END IF; - IF a_get_inactive_images IS NULL THEN - SET a_get_inactive_images = FALSE; - END IF; - IF a_get_first_image_only IS NULL THEN - SET a_get_first_image_only = TRUE; - END IF; - IF a_get_all_images IS NULL THEN - SET a_get_all_images = FALSE; - END IF; - - - -- Temporary tables - CREATE TABLE tmp_Shop_Product_Category ( - id_category INTEGER NOT NULL, - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - id_product INTEGER NOT NULL, - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Shop_Variation ( - id_variation INTEGER NOT NULL, - id_product INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - - CREATE TABLE tmp_Shop_Image ( - id_product INTEGER NOT NULL, - id_image INTEGER NOT NULL, - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL - ); - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - - - -- Parse filters - SET a_ids_category = REPLACE(a_ids_category, ',', '|'); - SET a_ids_product = REPLACE(a_ids_product, ',', '|'); - SET v_has_filter_category = CASE WHEN a_ids_category = '' THEN FALSE ELSE TRUE END; - SET v_has_filter_product = CASE WHEN a_ids_product = '' THEN FALSE ELSE TRUE END; - - INSERT INTO tmp_Shop_Product_Category ( - id_category, active, display_order - ) - SELECT C.id_category, C.active, C.display_order - FROM Shop_Product_Category C - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || a_ids_category || '%') - AND (a_get_inactive_categories OR C.active); - - INSERT INTO tmp_Shop_Product ( - id_category, id_product, active, display_order - ) - SELECT P.id_category, P.id_product, P.active, P.display_order - FROM Shop_Product P - INNER JOIN tmp_Shop_Product_Category tC - ON P.id_category = tC.id_category - WHERE (a_get_all_products OR P.id_product LIKE '%' || a_ids_product || '%') - AND (a_get_inactive_products OR P.active); - - IF a_get_first_product_only THEN - DELETE FROM tmp_Shop_Product - WHERE display_order > (SELECT display_order FROM tmp_Shop_Product ORDER BY display_order ASC LIMIT 1); - END IF; - - IF v_has_filter_product THEN - DELETE FROM tmp_Shop_Product_Category - WHERE id_category NOT IN (SELECT DISTINCT id_category FROM tmp_Shop_Product); - END IF; - - INSERT INTO tmp_Shop_Variation ( - id_variation, id_product -- , display_order - ) - SELECT P.id_variation, P.id_product -- , P.display_order - FROM Shop_Variation V - INNER JOIN tmp_Shop_Product tP - ON V.id_product = tP.id_product - WHERE V.active; - - INSERT INTO tmp_Shop_Image ( - id_product, id_image, active, display_order - ) - SELECT I.id_product, I.id_image, I.active, I.display_order - FROM Shop_Image I - INNER JOIN tmp_Shop_Product tP - ON I.id_image = tP.id_image - WHERE (a_get_all_images OR I.id_image LIKE '%' || a_ids_image || '%') - AND (a_get_inactive_images OR I.active); - - IF a_get_first_image_only THEN - DELETE FROM tmp_Shop_Image - WHERE display_order > (SELECT display_order FROM tmp_Shop_Image ORDER BY display_order ASC LIMIT 1); - END IF; - - IF v_has_filter_image THEN - DELETE FROM tmp_Shop_Product - WHERE id_product NOT IN (SELECT DISTINCT id_product FROM tmp_Shop_Image); - DELETE FROM tmp_Shop_Product_Category - WHERE id_category NOT IN (SELECT DISTINCT id_category FROM tmp_Shop_Product); - END IF; - - - -- Permissions - IF EXISTS (SELECT * FROM tmp_Shop_Product_Category LIMIT 1) THEN - SET v_guid_permission = gen_random_uuid(); - SET v_id_user = CURRENT_USER; - SET v_id_permission_product = (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_PRODUCT' LIMIT 1); - SET v_ids_product_permission = (SELECT STRING_AGG(item, '|') FROM tmp_Shop_Product); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, v_id_permission_product, v_ids_product_permission); - - UPDATE tmp_Shop_Product tP - INNER JOIN Shop_Calc_User_Temp TP - ON tP.id_product = TP.id_product - AND TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - - CALL p_shop_clear_calc_user(v_guid_permission); - END IF; - - - -- Returns - SET v_now = CURRENT_TIMESTAMP; - - -- Categories - SELECT ( - tC.id_category, - C.name, - C.description, - tC.can_view, - tC.can_edit, - tC.can_admin - ) - FROM tmp_Shop_Product_Category tC - INNER JOIN Shop_Product_Category C - ON tC.id_category = C.id_category - ; - - -- Products - SELECT - tP.id_product, - P.name, - P.price, - P.description, - C.id_category, - P.lead_time, - P.id_stripe_product, - P.id_stripe_price, - P.is_subscription, - RI.name AS name_interval_recurrence, - RI.name_plural AS name_plural_interval_recurrence, - P.count_interval_recurrence, - tP.can_view, - tP.can_edit, - tP.can_admin - FROM tmp_Shop_Product tP - INNER JOIN Shop_Product P - ON tP.id_product = P.id_product - INNER JOIN Shop_Interval_Recurrence RI - ON P.id_unit_measurement_interval_recurrence = RI.id_interval - ; - - -- Variations - SELECT - PVL.id_variation, - tV.id_product, - V.code, - V.name, - PVL.display_order - FROM tmp_Shop_Product tV - INNER JOIN Shop_Product_Variation_Link PVL - ON tV.id_product = PVL.id_product - INNER JOIN Shop_Variation V - ON PVL.id_variation = V.id_variation - WHERE V.active - AND PVL.active - ; - - -- Images - SELECT - tI.id_image, - tI.id_product, - I.url, - PIL.display_order - FROM tmp_Shop_Image tI - INNER JOIN Shop_Product_Image_Link PIL - ON tI.id_product = PIL.id_product - WHERE I.active - AND PIL.active - ; -END; -$$ LANGUAGE plpgsql; -*/ diff --git a/static/PostgreSQL/600_p_shop_user_eval.sql b/static/PostgreSQL/600_p_shop_user_eval.sql deleted file mode 100644 index f50b45c0..00000000 --- a/static/PostgreSQL/600_p_shop_user_eval.sql +++ /dev/null @@ -1,730 +0,0 @@ - -/* - -CALL p_shop_calc_user ( - gen_random_uuid(), -- a_guid - '', -- a_id_user - 0, -- a_get_inactive_users - '1', -- a_ids_permission - '', -- a_ids_access_level - '1' -- a_ids_product -) - -*/ - -CREATE OR REPLACE PROCEDURE p_shop_calc_user ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_get_inactive_users BOOLEAN, - IN a_ids_permission INTEGER[], - IN a_ids_access_level INTEGER[], - IN a_ids_product INTEGER[] -- VARCHAR(4000) -- IN a_ids_permutation VARCHAR(4000) - /* - OUT result_errors TABLE ( - guid UUID, - id_type INTEGER, - code VARCHAR(50), - msg VARCHAR(4000) - ) - */ - -- INOUT a_error_msg TEXT -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_get_inactive_users BOOLEAN; - v_ids_permission INTEGER[]; - v_ids_access_level INTEGER[]; - v_ids_product INTEGER[]; -- TEXT; -- VARCHAR(4000); -- IN a_ids_permutation VARCHAR(4000) - v_has_filter_user BOOLEAN; - v_has_filter_permission BOOLEAN; - v_has_filter_access_level BOOLEAN; - -- v_has_filter_permutation BOOLEAN; - v_has_filter_product BOOLEAN; - v_id_permission_product INTEGER; - v_id_permission INTEGER; - -- v_ids_product UUID; - v_id_access_level_view INTEGER; - -- v_id_access_level_product_required INTEGER; - v_priority_access_level_view INTEGER; - v_priority_access_level_edit INTEGER; - v_priority_access_level_admin INTEGER; - v_id_access_level INTEGER; - v_priority_access_level INTEGER; - v_now TIMESTAMP; - v_ids_row_delete UUID; - v_code_error_data VARCHAR(200); - v_id_error_data INTEGER; - v_code_error_permission VARCHAR(200); - -- result_errors REFCURSOR; - -- v_error_msg TEXT := NULL; -BEGIN - -- Parse arguments + get default values - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := CASE WHEN a_id_user IS NULL THEN '' ELSE TRIM(a_id_user) END; - v_get_inactive_users := COALESCE(a_get_inactive_users, FALSE); - v_ids_permission := COALESCE(a_ids_permission, ARRAY[]::INTEGER[]); - v_ids_access_level := COALESCE(a_ids_access_level, ARRAY[]::INTEGER[]); - -- v_ids_permutation := CASE WHEN a_ids_permutation IS NULL THEN '' ELSE TRIM(a_ids_permutation) END; - v_ids_product := COALESCE(a_ids_product, ARRAY[]::INTEGER[]); - - v_id_error_data := 1; - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = v_id_error_data); - - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - - v_has_filter_user := (v_id_user <= 0); - v_has_filter_permission := (CARDINALITY(v_ids_permission) > 0); - v_has_filter_access_level := (CARDINALITY(v_ids_access_level) > 0); - /* - v_has_filter_permutation := CASE WHEN v_ids_permutation = '' THEN FALSE ELSE TRUE END; - */ - v_has_filter_product := (CARDINALITY(v_ids_product) = 0); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - v_priority_access_level_view := (SELECT priority FROM Shop_Access_Level WHERE id_access_level = v_id_access_level_view); - v_priority_access_level_edit := (SELECT priority FROM Shop_Access_Level WHERE code = 'EDIT' LIMIT 1); - v_priority_access_level_admin := (SELECT priority FROM Shop_Access_Level WHERE code = 'ADMIN' LIMIT 1); - - v_id_permission_product := (SELECT v_id_permission FROM Shop_Permission WHERE code = 'SHOP_PRODUCT' LIMIT 1); - - -- Clear previous proc results - -- DROP TABLE IF EXISTS tmp_User_Role_Link; - -- DROP TEMPORARY TABLE IF EXISTS tmp_User_Role_Link; - DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - - - -- Permanent Table - CREATE TABLE IF NOT EXISTS Shop_Calc_User_Temp ( - id_row INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_user INTEGER, - CONSTRAINT FK_Shop_Calc_User_Temp_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User (id_user), - id_permission_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_permission_required - FOREIGN KEY (id_permission_required) - REFERENCES Shop_Permission (id_permission), - /* - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level (id_access_level), - */ - priority_access_level_required INTEGER NOT NULL, - /* - CONSTRAINT FK_Shop_Calc_User_Temp_priority_access_level_required - FOREIGN KEY (priority_access_level_required) - REFERENCES Shop_Access_Level (priority), - */ - id_product INTEGER NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product (id_product), - /* - id_permutation INTEGER NULL, - CONSTRAINT FK_Shop_Calc_User_Temp_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES parts.Shop_Product_Permutation (id_permutation), - */ - is_super_user BOOLEAN NULL, - priority_access_level_user INTEGER NULL, - /* - CONSTRAINT FK_Shop_Calc_User_Temp_priority_access_level_minimum - FOREIGN KEY (priority_access_level_minimum) - REFERENCES Shop_Access_Level (priority) - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BOOLEAN, -- DEFAULT 0 - name_error VARCHAR(200) NULL - ); - - -- Temporary tables - CREATE TEMPORARY TABLE tmp_Shop_Product_p_shop_calc_user ( - id_row INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_product FOREIGN KEY (id_product) - REFERENCES Shop_Product (id_product), - /* - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES parts.Shop_Product_Permutation (id_permutation), - */ - id_access_level_required INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_p_shop_calc_user_id_access_level_required - FOREIGN KEY (id_access_level_required) - REFERENCES Shop_Access_Level (id_access_level), - guid UUID NOT NULL, - rank_product INTEGER NOT NULL - ); - - /* - CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Permission IDs - IF v_has_filter_permission THEN - -- CALL p_split(a_guid, v_ids_permission, ','); - - -- Invalid - IF EXISTS ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - ) THEN -- (SELECT PERM.id_permission FROM Split_Temp ST LEFT JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission WHERE ISNULL(PERM.id_permission)) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid permission IDs: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - LEFT JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission - WHERE ISNULL(PERM.id_permission) - ; - */ - RAISE EXCEPTION 'Invalid permission IDs: %', ( - SELECT STRING_AGG(id_permission, ', ') - FROM ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - ) Permission - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Inactive - IF EXISTS ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - WHERE active - ) THEN -- (SELECT PERM.id_permission FROM Split_Temp ST INNER JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission WHERE PERM.active = FALSE) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'The following permissions are not active: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - INNER JOIN Shop_Permission PERM ON ST.substring = PERM.id_permission - WHERE PERM.active = FALSE - ; - */ - RAISE EXCEPTION 'Inactive permission IDs: %', ( - SELECT STRING_AGG(id_permission, ', ') - FROM ( - SELECT UNNEST(v_ids_permission) AS id_permission - EXCEPT - SELECT id_permission FROM Shop_Permission - WHERE active - ) Permission - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Get the permission with the highest priority access level required - v_id_permission := ( - SELECT PERMS.id_permission - FROM ( - SELECT PERM2.id_permission - FROM Split_Temp ST - INNER JOIN Shop_Permission PERM2 ON ST.substring = PERM2.id_permission - WHERE PERM.active - UNION - SELECT v_id_permission_product - ) PERMS - INNER JOIN Shop_Permission PERM1 ON PERMS.id_permission = PERM1.id_permission - INNER JOIN Shop_Access_Level AL ON PERM1.id_access_level_required = AL.id_access_level - ORDER BY AL.priority ASC - LIMIT 1 - ); - - -- DROP TABLE Split_Temp; - ELSIF v_has_filter_product THEN - v_id_permission := v_id_permission_product; - ELSE - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Permission ID required' - ) - ; - */ - RAISE EXCEPTION 'Permission ID required.' - USING ERRCODE = '22000' - ; - END IF; - - -- access level - IF v_has_filter_access_level THEN - IF EXISTS ( - /* - SELECT ST.substring - FROM Split_Temp ST - LEFT JOIN Shop_Access_Level AL - ON ST.substring = AL.id_access_level - WHERE - ISNULL(AL.id_access_level) - OR AL.active = FALSE - */ - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid access level IDs: ' || STRING_AGG(ST.substring, ', ') - FROM Split_Temp ST - LEFT JOIN Shop_Access_Level AL ON ST.substring = AL.id_access_level - WHERE ISNULL(AL.id_access_level) - ; - */ - RAISE EXCEPTION 'Invalid access level IDs: %', ( - SELECT STRING_AGG(id_access_level, ', ') - FROM ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) AL - ) - USING ERRCODE = '22000' - ; - END IF; - - IF EXISTS ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - WHERE active - ) THEN - RAISE EXCEPTION 'Inactive access level IDs: %', ( - SELECT STRING_AGG(id_access_level, ', ') - FROM ( - SELECT UNNEST(v_ids_access_level) AS id_access_level - EXCEPT - SELECT id_access_level FROM Shop_Access_Level - ) AL - ) - USING ERRCODE = '22000' - ; - END IF; - - v_id_access_level := ( - SELECT AL.id_access_level - FROM Shop_Access_Level AL - WHERE - AL.active - AND AL.id_access_level = ANY(v_ids_access_level) - ORDER BY AL.priority ASC - LIMIT 1 - ); - ELSE - v_id_access_level := ( - SELECT id_access_level_required AS id_access_level - FROM ( - SELECT id_access_level - FROM Shop_Permission PERM - WHERE - PERM.id_permission = v_id_permission - UNION - SELECT v_id_access_level_view AS id_access_level - ) PERMS - INNER JOIN Shop_Access_Level AL ON PERMS.id_access_level = AL.id_access_level - ORDER BY AL.priority ASC - LIMIT 1 - ); -- v_id_access_level_view; - END IF; - - v_priority_access_level := (SELECT priority FROM Shop_Access_Level WHERE id_access_level = v_id_access_level); - - -- Invalid user ID - IF v_has_filter_user THEN - IF ISNULL((SELECT id_user FROM Shop_User WHERE id_user = v_id_user)) THEN -- NOT v_has_filter_user THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid user ID: ' || COALESCE(v_id_user, 'NULL') - ) - ; - */ - RAISE EXCEPTION 'Invalid user ID: %', v_id_user - USING ERRCODE = '22000' - ; - END IF; - - IF ISNULL((SELECT id_user FROM Shop_User WHERE id_user = v_id_user AND active)) THEN - RAISE EXCEPTION 'Inactive user ID: %', v_id_user - USING ERRCODE = '22000' - ; - END IF; - END IF; - - - -- Invalid products - IF v_has_filter_product THEN - -- Invalid product IDs - IF EXISTS ( - SELECT UNNEST(v_ids_product) AS id_product - EXCEPT - SELECT id_product FROM Shop_Product - ) THEN -- (SELECT * FROM Split_Temp ST LEFT JOIN Shop_Product P ON ST.substring = P.id_product WHERE ISNULL(P.id_product)) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - SELECT - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data LIMIT 1), - v_code_error_data, - 'Invalid product IDs: ' || COALESCE(STRING_AGG(ST.substring, ', '), 'NULL') - FROM Split_Temp ST - LEFT JOIN Shop_Product P ON ST.substring = P.id_product - WHERE ISNULL(P.id_product) - ; - */ - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT STRING_AGG(id_product, ', ') - FROM ( - SELECT UNNEST(v_ids_product) AS id_product - EXCEPT - SELECT id_product FROM Shop_Product - ) Product - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Product_p_shop_calc_user ( - id_product, - -- id_permutation, - id_access_level_required, - guid, - rank_product -- rank_permutation - ) - SELECT - DISTINCT P.id_product, - -- PP.id_permutation, - P.id_access_level_required, - v_guid, - RANK() OVER (ORDER BY C.display_order, P.display_order) AS rank_product - FROM Shop_Product P -- ON ST.substring = P.id_product -- Shop_Product_Permutation PP - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - INNER JOIN Shop_Access_Level AL - ON P.id_access_level_required = AL.id_access_level - AND AL.active - WHERE -- FIND_IN_SET(P.id_product, v_ids_product) > 0 -- FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - P.id_product = ANY(v_ids_product) - -- AND P.active -- not worried as we want users to be able to see their order history - ; - /* - DELETE FROM tmp_Shop_Product_p_shop_calc_user - WHERE rank_permutation > 1 - ; - */ - -- v_has_filter_product := EXISTS (SELECT * FROM tmp_Shop_Product_p_shop_calc_user WHERE v_guid = guid); - END IF; - - -- User permissions - /* - IF v_has_filter_product THEN - INSERT INTO Shop_Calc_User_Temp ( - guid, - id_user, - id_permission_required, - id_product, - -- id_permutation, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - P.id_product, - -- t_P.id_permutation, - CASE WHEN v_priority_access_level <= AL_P.priority THEN v_priority_access_level ELSE AL_P.priority END AS priority_access_level_required, - AL_U.priority AS priority_access_level_user, - U.is_super_user, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_view AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_view, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_edit AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_edit, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL_U.priority) AND AL_U.priority <= v_priority_access_level_admin AND AL_U.priority <= priority_access_level_required THEN TRUE ELSE FALSE END END AS can_admin - FROM Shop_User U - /* - ON U.id_user = v_id_user - AND U.active - */ - LEFT JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - LEFT JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL_U - ON RPL.id_access_leveL = AL_U.id_access_level - AND AL_U.active - INNER JOIN tmp_Shop_Product_p_shop_calc_user t_P - ON t_P.guid = v_guid - AND AL.id_access_level = t_P.id_access_leveL_required - INNER JOIN Shop_Access_Level AL_P - ON t_P.id_access_leveL_required = AL_P.id_access_level - AND AL_P.active - WHERE - v_guid = t_P.guid - AND U.active - AND U.id_user = v_id_user - ; - ELSE - INSERT INTO Shop_Calc_User_Temp (--UE_T - guid, - id_user, - id_permission_required, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - v_priority_access_level AS priority_access_level_required, - AL.priority AS priority_access_level_user, - U.is_super_user, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_view AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_view, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_edit AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_edit, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN NOT ISNULL(AL.priority) AND AL.priority <= v_priority_access_level_admin AND AL.priority <= v_priority_access_level THEN TRUE ELSE FALSE END END AS can_admin - FROM Shop_User U - INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL - ON RPL.id_access_level = AL.id_access_level - AND AL.active - WHERE - U.id_user = v_id_user - AND U.active - AND RPL.id_permission = v_id_permission - ORDER BY AL.priority ASC - ; - END IF; - */ - INSERT INTO Shop_Calc_User_Temp (--UE_T - guid, - id_user, - id_permission_required, - id_product, - priority_access_level_required, - priority_access_level_user, - is_super_user, - can_view, - can_edit, - can_admin, - name_error - ) - SELECT - v_guid, - v_id_user, - v_id_permission AS id_permission_required, - t_P.id_product, - MIN(v_priority_access_level, AL_P.priority) AS priority_access_level_required, - AL_U.priority AS priority_access_level_user, - U.is_super_user, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_view AND priority_access_level_user <= priority_access_level_required) AS can_view, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_edit AND priority_access_level_user <= priority_access_level_required) AS can_edit, - (U.is_super_user AND NOT ISNULL(priority_access_level_user) AND priority_access_level_user <= v_priority_access_level_admin AND priority_access_level_user <= priority_access_level_required) AS can_admin, - Permission.name || ' ' || (SELECT name FROM Shop_Access_Level WHERE priority = priority_access_level_required ORDER BY id_access_level ASC LIMIT 1) || ' permissions' || CASE WHEN ISNULL(t_P.id_product) THEN '' ELSE ' for product ' || P.name END AS name_error - FROM Shop_User U - INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active - INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Access_Level AL_U - ON RPL.id_access_level = AL_U.id_access_level - AND AL_U.active - INNER JOIN Shop_Permission Permission - ON RPL.id_permission = Permission.id_permission - AND Permission.active - CROSS JOIN tmp_Shop_Product_p_shop_calc_user t_P -- ON t_P.guid = v_guid - INNER JOIN Shop_Product P ON t_P.id_product = P.id_product - INNER JOIN Shop_Access_Level AL_P - ON t_P.id_access_level_required = AL_P.id_access_level - -- AND AL_P.active - WHERE - U.id_user = v_id_user - AND U.active - AND RPL.id_permission = v_id_permission - AND t_P.guid = v_guid - ORDER BY AL_P.priority ASC, t_P.rank_product ASC - ; - - -- IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE GUID = v_guid) THEN - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - WHERE GUID = v_guid - ; - -- RETURN NEXT result_errors; - -- result_errors - a_error_msg := ( - SELECT - -- GUID, id_type, code, - msg - FROM tmp_Msg_Error - WHERE GUID = v_guid - LIMIT 1 - ); - */ - - -- select * from tmp_Shop_Product_p_shop_calc_user; - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Product_p_shop_calc_user; - -- DROP TEMPORARY TABLE IF EXISTS tmp_User_Role_Link; - -- DROP TABLE IF EXISTS tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_calc_user ( - '56c9dfc1-e22f-11ee-aab4-b42e9986184a', -- v_guid - '', -- v_id_user -- 'auth0|6582b95c895d09a70ba10fef', - false, -- v_get_inactive_users - '4,5', -- v_ids_permission - '1', -- v_ids_access_level - -- null, -- v_ids_product - '1,2,3' -- v_ids_permutation -); - -SELECT * -FROM Shop_Calc_User_Temp -; - -DROP TABLE Shop_Calc_User_Temp; - -SELECT * -FROM Shop_Permission -; - -SELECT * -FROM Shop_Access_Level -; - -SELECT * -FROM Shop_Product -; - -SELECT * -FROM Shop_Product_Permutation -; - - -*/ - -/* -SELECT 'NOODS' AS guid, - U.id_user AS id_user, - P.id_permission AS id_permission_required, - AL.id_access_level AS id_access_level_required, - /* - v_id_permission, - v_id_access_level, - */ - AL.priority, -- MIN(AL.priority), - U.is_super_user - /* - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_view THEN TRUE ELSE FALSE END END, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_edit THEN TRUE ELSE FALSE END END, - CASE WHEN U.is_super_user THEN TRUE ELSE CASE WHEN priority_access_level_minimum <= v_priority_access_level_admin THEN TRUE ELSE FALSE END END - */ -FROM parts.Shop_User U -INNER JOIN Shop_User_Role_Link URL - ON U.id_user = URL.id_user - AND URL.active -INNER JOIN Shop_Role_Permission_Link RPL - ON URL.id_role = RPL.id_role - AND RPL.active -INNER JOIN Shop_Permission P - ON RPL.id_permission = P.id_permission - AND P.active -inner JOIN Shop_Access_Level AL - -- ON P.id_access_level_required = AL.id_access_level - ON RPL.id_access_level = AL.id_access_level - AND AL.active -WHERE U.id_user = 'auth0|6582b95c895d09a70ba10fef' - AND U.active - AND FIND_IN_SET(P.id_permission, '1,2') > 0 - -- AND v_id_access_level = AL.id_access_leveld --- GROUP BY U.id_user, P.id_permission, AL.id_access_level -- , is_super_user - -*/ diff --git a/static/PostgreSQL/602_p_save_supplier_purchase_order.sql b/static/PostgreSQL/602_p_save_supplier_purchase_order.sql deleted file mode 100644 index 3913ecfb..00000000 --- a/static/PostgreSQL/602_p_save_supplier_purchase_order.sql +++ /dev/null @@ -1,515 +0,0 @@ - - - --- DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_supplier_purchase_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_order INTEGER, - IN a_id_supplier_ordered INTEGER, - IN a_id_currency_cost INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_supplier_ordered INTEGER; - v_id_currency_cost INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - v_guid_permission UUID; - -- v_id_user VARCHAR(100); - v_id_permission_supplier_purchase_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_supplier_purchase_order BOOLEAN; - -- result_orders REFCURSOR; - -- result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_supplier_ordered := a_id_supplier_ordered; - v_id_currency_cost := a_id_currency_cost; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission = 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - v_guid_permission = gen_random_uuid(); - -- v_id_user = CURRENT_USER; - v_id_permission_supplier_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER_PURCHASE_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - - v_is_new_supplier_purchase_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - RAISE EXCEPTION 'Invalid User ID: %', COALESCE(v_id_user, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Order ID - IF ((v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Supplier_Purchase_Order WHERE id_order = v_id_order)) THEN - RAISE EXCEPTION 'Invalid Supplier Purchase Order ID: %', COALESCE(v_id_order, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Supplier ID - IF ISNULL(v_id_supplier_ordered) OR NOT EXISTS (SELECT * FROM Shop_Supplier WHERE id_supplier = v_id_supplier_ordered) THEN - RAISE EXCEPTION 'Invalid Supplier ID: %', COALESCE(v_id_supplier_ordered, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Currency ID - IF ISNULL(v_id_currency_cost) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_cost) THEN - RAISE EXCEPTION 'Invalid currency ID: %', COALESCE(v_id_currency, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Comment - IF v_comment = '' THEN - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - -- Get data from Temp table - INSERT INTO tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - name_error - ) - SELECT - SPOPL_T.id_link, - SPOPL_T.id_order, - SPOPL_T.id_permutation, - PP.cost_local * quantity_ordered AS cost_total_local, - SPOPL_T.id_currency_cost, - SPOPL_T.quantity_ordered, - SPOPL_T.id_unit_quantity, - SPOPL_T.quantity_received, - SPOPL_T.latency_delivery_days, - SPOPL_T.display_order, - SPOPL_T.active, - CAST(PP.id_permutation AS VARCHAR(10)) || ' - ' || COALESCE(PP.name ,'') AS name_error - FROM Shop_Supplier_Purchase_Order_Product_Link_Temp SPOPL_T - INNER JOIN Shop_Product_Permutation PP ON SPOPL_T.id_permutation = PP.id_permutation - WHERE SPOPL_T.GUID = v_guid - ; - DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp SPOPL_T - WHERE SPOPL_T.GUID = v_guid - ; - - /* - UPDATE tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - - cost_total_local - */ - - -- Invalid quantity ordered - IF EXISTS ( - SELECT * - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_ordered) - AND quantity_ordered < 0 - ) THEN - RAISE EXCEPTION 'Invalid quantity ordered property for the following permutations: %', ( - SELECT STRING_AGG(t_SPOPL.name_error, ', ') - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - -- INNER JOIN Shop_Product_Permutation PP ON t_SPOPL.id_permutation = PP.id_permutation - WHERE t_SPOPL.quantity_ordered < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Duplicates - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - RAISE EXCEPTION 'Duplicate records: %', || ( - SELECT STRING_AGG(t_SPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_SPOPLC - ) - USING ERRCODE = '22000' - ; - END IF; - - - - -- Permissions - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPO - INNER JOIN Shop_Product_Permutation PP ON t_SPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_supplier_purchase_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier_purchase_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - RAISE EXCEPTION 'You do not have permission to edit the following product IDs: %', v_ids_product_no_permission - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - -- Transaction - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_supplier_purchase_order = TRUE THEN 'new ' ELSE '' END - || 'Supplier Purchase Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_supplier_purchase_order = 1) THEN - INSERT INTO Shop_Supplier_Purchase_Order ( - id_supplier_ordered, - cost_total_local, - id_currency_cost, - created_by, - id_change_set, - active - ) - SELECT - v_id_supplier_ordered, - SUM(t_SPOPL.cost_total_local), - v_id_currency_cost, - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Supplier_Purchase_Order ORDER BY id_order DESC LIMIT 1); - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ; - ELSE - UPDATE Shop_Supplier_Purchase_Order SPO - SET - SPO.id_supplier_ordered = v_id_supplier_ordered, - SPO.cost_total_local = SUM(t_SPOPL.cost_total_local), - SPO.id_currency = v_id_currency_cost, - SPO.id_change_set = v_id_change_set, - SPO.active = v_active - FROM Shop_Supplier_Purchase_Order SPO - INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL ON SPO.id_order = t_SPOPL.id_order - WHERE SPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL INNER JOIN Shop_Supplier_Purchase_Order_Product_Link SPOPL ON t_SPOPL.id_link = SPOPL.id_link) THEN - UPDATE Shop_Supplier_Purchase_Order_Product_Link SPOPL - SET - SPOPL.id_order = t_SPOPL.id_order, - SPOPL.id_permutation = t_SPOPL.id_permutation, - SPOPL.cost_total_local = t_SPOPL.cost_total_local, - SPOPL.id_currency_cost = t_SPOPL.id_currency_cost, - SPOPL.quantity_ordered = t_SPOPL.quantity_ordered, - SPOPL.id_unit_quantity = t_SPOPL.id_unit_quantity, - SPOPL.quantity_received = t_SPOPL.quantity_received, - SPOPL.latency_delivery_days = t_SPOPL.latency_delivery_days, - SPOPL.display_order = t_SPOPL.display_order, - SPOPL.active = t_SPOPL.active, - SPOPL.id_change_set = v_id_change_set - FROM Shop_Supplier_Purchase_Order_Product_Link SPOPL - INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - ON SPOPL.id_link = t_SPOPL.id_link - ; - ELSE - INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL - WHERE t_SPOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error) THEN - ROLLBACK; - ELSE - COMMIT; - END IF; - */ - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - /* - -- Supplier Purchase Orders - OPEN result_orders FOR - SELECT * - FROM Shop_Supplier_Purchase_Order - WHERE id_order = v_id_order - ; - -- RETURN NEXT result_orders; - - -- Supplier Purchase Order Product Links - OPEN result_order_product_links FOR - SELECT * - FROM Shop_Supplier_Purchase_Order_Product_Link - WHERE id_order = v_id_order - ; - -- RETURN NEXT result_order_product_links; - */ - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Supplier_Purchase_Order; - DROP TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Supplier_Purchase_Order_Audit; -DELETE FROM Shop_Supplier_Purchase_Order; - -INSERT INTO Shop_Supplier_Purchase_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_ordered, - id_unit_quantity, - quantity_received, - latency_delivery_days, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- cost_total_local, - 1, -- id_currency_cost, - 1, -- quantity_ordered, - 1, -- id_unit_quantity, - 1, -- quantity_received, - 14, -- latency_delivery_days , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; - -CALL p_shop_save_supplier_purchase_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - -1, -- a_id_order - 1, -- a_id_supplier_ordered - 1 -- a_id_currency_cost -); - -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; - -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link; -DELETE FROM Shop_Supplier_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Supplier_Purchase_Order_Audit; -DELETE FROM Shop_Supplier_Purchase_Order; - - -*/ - diff --git a/static/PostgreSQL/602_p_shop_save_supplier.sql b/static/PostgreSQL/602_p_shop_save_supplier.sql deleted file mode 100644 index 1534a0bf..00000000 --- a/static/PostgreSQL/602_p_shop_save_supplier.sql +++ /dev/null @@ -1,306 +0,0 @@ - - - - -CREATE OR REPLACE PROCEDURE p_shop_save_supplier ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_supplier INTEGER, - IN a_name_company VARCHAR(256), - IN a_name_contact VARCHAR(256), - IN a_department_contact VARCHAR(256), - IN a_id_address INTEGER, - IN a_phone_number VARCHAR(20), - IN a_fax VARCHAR(20), - IN a_email VARCHAR(515), - IN a_website VARCHAR(300), - IN a_id_currency INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_supplier INTEGER; - v_name_company VARCHAR(256); - v_name_contact VARCHAR(256); - v_department_contact VARCHAR(256); - v_id_address INTEGER; - v_phone_number VARCHAR(256); - v_fax VARCHAR(256); - v_email VARCHAR(256); - v_website VARCHAR(256); - v_id_currency INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_id_error_type_no_permission INTEGER; - v_guid_permission UUID; - v_id_permission_supplier INTEGER; - -- v_id_access_level_EDIT INTEGER; - v_has_permission BOOLEAN; - v_id_change_set INTEGER; - v_is_new_supplier BOOLEAN; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_supplier := COALESCE(a_id_supplier, -1); - v_name_company := TRIM(COALESCE(a_name_company, '')); - v_name_contact := TRIM(COALESCE(a_name_contact, '')); - v_department_contact := TRIM(COALESCE(a_department_contact, '')); - v_id_address := a_id_address; - v_phone_number := TRIM(COALESCE(a_phone_number, '')); - v_fax := TRIM(COALESCE(a_fax, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_website := TRIM(COALESCE(a_website, '')); - v_id_currency := a_id_currency; - v_active := COALESCE(a_active, FALSE); - - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA'); - v_guid_permission = gen_random_uuid(); - v_id_user = CURRENT_USER; - v_id_permission_supplier = (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER' LIMIT 1); - -- v_id_access_level_EDIT = (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_supplier := CASE WHEN v_id_supplier <= 0 THEN TRUE ELSE FALSE END; - - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - */ - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - - - -- Argument validation - IF v_name_company = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Supplier company name must be provided') - ; - */ - RAISE EXCEPTION 'Supplier company name must be provided' - USING ERRCODE = '22000' - ; - END IF; - - IF v_id_address IS NULL THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Address ID must be provided') - ; - */ - RAISE EXCEPTION 'Address ID must be provided' - USING ERRCODE = '22000' - ; - END IF; - - IF v_email = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Email must be provided') - ; - */ - RAISE EXCEPTION 'Email must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - IF v_comment = '' THEN - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - IF (v_is_new_supplier = FALSE AND NOT EXISTS (SELECT * FROM Shop_Supplier S WHERE S.id_supplier = v_id_supplier)) THEN - RAISE EXCEPTION 'Invalid supplier ID: %', v_id_supplier - USING ERRCODE = '22000' - ; - END IF; - - /* - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Supplier ( - id_supplier, name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active - ) - VALUES - (v_id_supplier, v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active) - /* - FROM Shop_Supplier S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - END IF; - */ - - -- Permissions - CALL p_shop_calc_user(v_guid_permission, v_id_user, v_id_permission_supplier, ''); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - v_has_permission := (SELECT can_edit FROM Shop_Calc_User_Temp WHERE GUID = v_guid_permission); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - 'You do not have %' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier - ; - */ - RAISE EXCEPTION 'No permission: %', ( - SELECT name_error - FROM Shop_Calc_User_Temp - WHERE GUID = v_guid_permission - ) - USING ERRCODE = '42501' - ; - END IF; - - -- CALL p_shop_clear_calc_user(v_guid_permission); - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - - -- Transaction - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_supplier = TRUE THEN 'new ' ELSE '' END - || 'Supplier - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - START TRANSACTION; - IF (v_is_new_supplier = TRUE) THEN - INSERT INTO Shop_Supplier ( - -- id_supplier, - name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active, id_change_set - ) - VALUES - ( - -- v_id_supplier, - v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active, v_id_change_set - ) - /* - FROM Shop_Supplier S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - ELSE - UPDATE Shop_Supplier S - -- INNER JOIN tmp_Shop_Supplier t_S ON S.id_supplier = t_S.id_supplier - SET - /* - S.name_company = t_S.name_company, - S.name_contact = t_S.name_contact, - S.department_contact = t_S.department_contact, - S.id_address = t_S.id_address, - S.phone_number = t_S.phone_number, - S.fax = t_S.fax, - S.email = t_S.email, - S.website = t_S.website, - S.id_currency = t_S.id_currency, - S.active = t_S.active - */ - S.name_company = v_name_company, - S.name_contact = v_name_contact, - S.department_contact = v_department_contact, - S.id_address = v_id_address, - S.phone_number = v_phone_number, - S.fax = v_fax, - S.email = v_email, - S.website = v_website, - S.id_currency = v_id_currency, - S.active = v_active, - S.id_change_set = v_id_change_set - ; - END IF; - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - DROP TABLE tmp_Shop_Supplier; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - diff --git a/static/PostgreSQL/604_p_shop_save_manufacturing_purchase_order.sql b/static/PostgreSQL/604_p_shop_save_manufacturing_purchase_order.sql deleted file mode 100644 index 5b12fecb..00000000 --- a/static/PostgreSQL/604_p_shop_save_manufacturing_purchase_order.sql +++ /dev/null @@ -1,606 +0,0 @@ - - - - --- DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_manufacturing_purchase_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_id_order INTEGER, - -- IN a_id_supplier_ordered INTEGER, - IN a_id_currency_cost INTEGER, - IN a_active BOOLEAN, - IN a_comment UUID -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_currency_cost INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - v_guid_permission UUID; - -- v_id_user VARCHAR(100); - v_id_permission_manufacturing_purchase_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_manufacturing_purchase_order BOOLEAN; - result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_currency_cost := a_id_currency_cost; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission = 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - v_guid_permission = gen_random_uuid(); - -- v_id_user = CURRENT_USER; - v_id_permission_manufacturing_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_MANUFACTURING_PURCHASE_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_manufacturing_purchase_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Manufacturing_Purchase_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Manuf_Purch_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Manuf_Purch_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_manufacture INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - RAISE EXCEPTION 'Invalid User ID: %', COALESCE(v_id_user, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Order ID - IF ((v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Manufacturing_Purchase_Order WHERE id_order = v_id_order)) THEN - RAISE EXCEPTION 'Invalid Manufacturing Purchase Order ID: %', COALESCE(v_id_order, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - /* - -- Supplier ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_supplier_ordered) OR NOT EXISTS (SELECT * FROM Shop_Supplier WHERE id_supplier = v_id_supplier_ordered) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid supplier ID: ', COALESCE(v_id_supplier_ordered, 'NULL'))) - ; - END IF; - END IF; - */ - - -- Currency ID - IF ISNULL(v_id_currency_cost) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_cost) THEN - RAISE EXCEPTION 'Invalid currency ID: %', COALESCE(v_id_currency, 'NULL') - USING ERRCODE = '22000' - ; - END IF; - - -- Comment - IF v_comment = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, 'A comment must be provided.') - ; - */ - RAISE EXCEPTION 'A comment must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - - -- Get data from Temp table - INSERT INTO tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - value_produced_total_local, - latency_manufacture, - display_order, - active, - name_error - ) - SELECT - MPOPL_T.id_link, - MPOPL_T.id_order, - MPOPL_T.id_permutation, - PP.cost_local * MPOPL_T.quantity_used AS cost_total_local, - MPOPL_T.id_currency_cost, - MPOPL_T.quantity_used, - MPOPL_T.id_unit_quantity, - MPOPL_T.quantity_produced, - (PP.cost_local + PP.profit_local_min) * MPOPL_T.quantity_produced AS value_produced_total_local, - MPOPL_T.latency_manufacture, - MPOPL_T.display_order, - MPOPL_T.active, - PP.id_permutation, ' - ' || COALESCE(P.name ,'') AS name_error - FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp MPOPL_T - INNER JOIN Shop_Product_Permutation PP ON MPOPL_T.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - WHERE MPOPL_T.GUID = v_guid - -- GROUP BY MPOPL_T.id_order, name_error, MPOPL_T.id_link - /* - group by - MPOPL_T.id_link, - MPOPL_T.id_order, - MPOPL_T.id_permutation, - cost_total_local, - MPOPL_T.id_currency_cost, - MPOPL_T.quantity_used, - MPOPL_T.id_unit_quantity, - MPOPL_T.quantity_produced, - value_produced_total_local, - MPOPL_T.latency_manufacture, - MPOPL_T.display_order, - MPOPL_T.active, - name_error - */ - -- GROUP BY id_link, P.id_product, PP.id_permutation - -- GROUP BY name_error, ID_LINK, cost_total_local, value_produced_total_local - ; - DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp MPOPL_T - WHERE MPOPL_T.GUID = v_guid - ; - - -- Invalid quantity used - IF EXISTS ( - SELECT * - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_used) - AND quantity_used < 0 - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity used property for the following permutations: ' || STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_used < 0 - ; - */ - RAISE EXCEPTION 'Invalid quantity used property for the following permutations: %', ( - SELECT STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_used < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Invalid quantity produced - IF EXISTS ( - SELECT * - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - NOT ISNULL(quantity_produced) - AND quantity_produced < 0 - ) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity produced property for the following permutations: ' || STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_produced < 0 - ; - */ - RAISE EXCEPTION 'Invalid quantity produced property for the following permutations: %', ( - SELECT STRING_AGG(t_MPOPL.name_error, ', ') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.quantity_produced < 0 - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Duplicates - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Duplicate records: ' || STRING_AGG(t_MPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_MPOPLC - ; - */ - RAISE EXCEPTION 'Duplicate records: %', ( - SELECT STRING_AGG(t_MPOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_MPOPLC - ) - USING ERRCODE = '22000' - ; - END IF; - - - -- Permissions - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPO - INNER JOIN Shop_Product_Permutation PP ON t_MPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_manufacturing_purchase_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_manufacturing_purchase_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES ( - v_guid, - v_id_error_type_no_permission, - v_code_error_type_no_permission, - */ - RAISE EXCEPTION 'You do not have permission to edit the following product IDs: %', v_ids_product_no_permission - USING ERRCODE = '42501' - ; - END IF; - - -- Transaction - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_manufacturing_purchase_order = TRUE THEN 'new ' ELSE '' END - || 'Manufacturing Purchase Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_manufacturing_purchase_order = 1) THEN - INSERT INTO Shop_Manufacturing_Purchase_Order ( - -- id_supplier_ordered, - cost_total_local, - id_currency_cost, - value_produced_total_local, - created_by, - id_change_set, - active - ) - SELECT - -- v_id_supplier_ordered, - SUM(t_MPOPL.cost_total_local), - v_id_currency_cost, - SUM(t_MPOPL.value_produced_total_local), - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Manufacturing_Purchase_Order ORDER BY id_order DESC LIMIT 1); - - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ; - ELSE - UPDATE Shop_Manufacturing_Purchase_Order MPO - SET - -- MPO.id_supplier_ordered = v_id_supplier_ordered, - MPO.cost_total_local = SUM(t_MPOPL.cost_total_local), - MPO.value_produced_total_local = SUM(t_MPOPL.value_produced_total_local), - MPO.id_currency = v_id_currency_cost, - MPO.id_change_set = v_id_change_set, - MPO.active = v_active - FROM Shop_Manufacturing_Purchase_Order MPO - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL ON MPO.id_order = t_MPOPL.id_order - WHERE MPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL INNER JOIN Shop_Manufacturing_Purchase_Order_Product_Link MPOPL ON t_MPOPL.id_link = MPOPL.id_link) THEN - UPDATE Shop_Manufacturing_Purchase_Order_Product_Link MPOPL - SET - MPOPL.id_order = t_MPOPL.id_order, - MPOPL.id_permutation = t_MPOPL.id_permutation, - MPOPL.cost_total_local = t_MPOPL.cost_total_local, - MPOPL.value_produced_total_local = t_MPOPL.value_produced_total_local, - MPOPL.id_currency_cost = t_MPOPL.id_currency_cost, - MPOPL.quantity_used = t_MPOPL.quantity_used, - MPOPL.id_unit_quantity = t_MPOPL.id_unit_quantity, - MPOPL.quantity_produced = t_MPOPL.quantity_produced, - MPOPL.latency_manufacture = t_MPOPL.latency_manufacture, - MPOPL.display_order = t_MPOPL.display_order, - MPOPL.active = t_MPOPL.active, - MPOPL.id_change_set = v_id_change_set - FROM Shop_Manufacturing_Purchase_Order_Product_Link MPOPL - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - ON MPOPL.id_link = t_MPOPL.id_link - ; - ELSE - INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - cost_total_local, - value_produced_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL - WHERE t_MPOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - /* - -- Manufacturing Purchase Orders - SELECT * - FROM Shop_Manufacturing_Purchase_Order - WHERE - id_order = v_id_order - -- GUID = v_guid - ; - - -- Manufacturing Purchase Order Product Links - SELECT * - FROM Shop_Manufacturing_Purchase_Order_Product_Link - WHERE - id_order = v_id_order - -- GUID = v_guid - ; - */ - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Manufacturing_Purchase_Order_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order; - -INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - cost_total_local, - id_currency_cost, - quantity_used, - id_unit_quantity, - quantity_produced, - latency_manufacture, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- cost_total_local, - 1, -- id_currency_cost, - 1, -- quantity_used, - 1, -- id_unit_quantity, - 1, -- quantity_produced, - 14, -- latency_manufacture , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -CALL p_shop_save_manufacturing_purchase_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - -1, -- a_id_order - 1, -- a_id_currency_cost - 1, -- a_active - 'Initial data' -- a_comment -); - -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; - -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link; -DELETE FROM Shop_Manufacturing_Purchase_Order_Product_Link_Temp; -DELETE FROM Shop_Manufacturing_Purchase_Order_Audit; -DELETE FROM Shop_Manufacturing_Purchase_Order; - - -*/ - diff --git a/static/PostgreSQL/605_p_shop_save_customer.sql b/static/PostgreSQL/605_p_shop_save_customer.sql deleted file mode 100644 index 79ec88d7..00000000 --- a/static/PostgreSQL/605_p_shop_save_customer.sql +++ /dev/null @@ -1,313 +0,0 @@ - - - - -CREATE OR REPLACE PROCEDURE p_shop_save_customer ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment UUID, - IN a_id_customer INTEGER, - IN a_name_company VARCHAR(256), - IN a_name_contact VARCHAR(256), - IN a_department_contact VARCHAR(256), - IN a_id_address INTEGER, - IN a_phone_number VARCHAR(20), - IN a_email VARCHAR(515), - IN a_id_currency INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_customer INTEGER; - v_name_company VARCHAR(256); - v_name_contact VARCHAR(256); - v_department_contact VARCHAR(256); - v_id_address INTEGER; - v_phone_number VARCHAR(256); - v_email VARCHAR(256); - v_id_currency INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_id_error_type_no_permission INTEGER; - v_guid_permission UUID; - v_id_permission_customer INTEGER; - v_id_access_level_EDIT INTEGER; - v_has_permission BOOLEAN; - v_id_change_set INTEGER; - v_is_new_customer BOOLEAN; - -- result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_customer := COALESCE(a_id_customer, -1); - v_name_company := TRIM(COALESCE(a_name_company, '')); - v_name_contact := TRIM(COALESCE(a_name_contact, '')); - v_department_contact := TRIM(COALESCE(a_department_contact, '')); - v_id_address := a_id_address; - v_phone_number := TRIM(COALESCE(a_phone_number, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_id_currency := a_id_currency; - v_active := COALESCE(a_active, FALSE); - - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA'); - v_guid_permission = gen_random_uuid(); - v_id_permission_customer = (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER' LIMIT 1); - v_id_access_level_EDIT = (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - v_is_new_customer := CASE WHEN v_id_customer <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - name_company VARCHAR(255) NOT NULL, - name_contact VARCHAR(255) NULL, - department_contact VARCHAR(255) NULL, - id_address INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_address - FOREIGN KEY (id_address) - REFERENCES Shop_Address(id_address), - phone_number VARCHAR(50) NULL, - fax VARCHAR(50) NULL, - email VARCHAR(255) NOT NULL, - website VARCHAR(255) NULL, - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL, - can_view BOOLEAN NOT NULL, - can_edit BOOLEAN NOT NULL, - can_admin BOOLEAN NOT NULL - ); - */ - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- Argument validation - IF v_name_company = '' THEN - RAISE EXCEPTION 'Customer company name must be provided' - USING ERRCODE = '22000' - ; - END IF; - IF v_id_address IS NULL THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Customer address ID must be provided') - ; - */ - RAISE EXCEPTION 'Customer address ID must be provided' - USING ERRCODE = '22000' - ; - END IF; - IF v_email = '' THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Customer email must be provided') - ; - */ - RAISE EXCEPTION 'Customer email must be provided' - USING ERRCODE = '22000' - ; - END IF; - - - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - /* - IF (v_is_new_customer = FALSE AND NOT EXISTS (SELECT * FROM Shop_Customer C WHERE C.id_customer = v_id_customer)) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, 'Invalid customer ID: ' || v_id_customer) - ; - END IF; - */ - RAISE EXCEPTION 'Invalid customer ID: %', v_id_customer - USING ERRCODE = '22000' - ; - END IF; - - /* - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Customer ( - id_customer, name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency, active - ) - VALUES - (v_id_customer, v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_fax, v_email, v_website, v_id_currency, v_active) - /* - FROM Shop_Customer S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - END IF; - */ - - -- Permissions - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_customer, v_id_access_level_edit, ''); - - /* - UPDATE tmp_Shop_Customer t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - v_has_permission := (SELECT can_edit FROM Shop_Calc_User_Temp WHERE GUID = v_guid_permission); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - /* - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - 'You do not have ' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - ; - RAISE EXCEPTION 'You do not have ' || name || ' permissions.' - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - USING ERRCODE = '22000' - ; - */ - END IF; - - -- CALL p_shop_clear_calc_user(v_guid_permission); - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - - - -- Transaction - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_customer = TRUE THEN 'new ' ELSE '' END - || 'Customer - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - START TRANSACTION; - IF (v_is_new_customer = TRUE) THEN - INSERT INTO Shop_Customer ( - -- id_customer, - name_company, name_contact, department_contact, id_address, phone_number, email, id_currency, active, id_change_set - ) - VALUES - ( - -- v_id_customer, - v_name_company, v_name_contact, v_department_contact, v_id_address, v_phone_number, v_email, v_id_currency, v_active, v_id_change_set - ) - /* - FROM Shop_Customer S - WHERE (NOT v_has_filter_category OR C.id_category LIKE '%' || v_ids_category || '%') - AND (v_get_inactive_categories OR C.active) - */ - ; - ELSE - UPDATE Shop_Customer C - -- INNER JOIN tmp_Shop_Customer t_S ON S.id_customer = t_S.id_customer - SET - /* - S.name_company = t_S.name_company, - S.name_contact = t_S.name_contact, - S.department_contact = t_S.department_contact, - S.id_address = t_S.id_address, - S.phone_number = t_S.phone_number, - S.fax = t_S.fax, - S.email = t_S.email, - S.website = t_S.website, - S.id_currency = t_S.id_currency, - S.active = t_S.active - */ - C.name_company = v_name_company, - C.name_contact = v_name_contact, - C.department_contact = v_department_contact, - C.id_address = v_id_address, - C.phone_number = v_phone_number, - C.email = v_email, - C.id_currency = v_id_currency, - C.active = v_active, - C.id_change_set = v_id_change_set - ; - END IF; - - COMMIT; - - -- Returns - -- v_now = CURRENT_TIMESTAMP; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - -- DROP TABLE tmp_Shop_Customer; - -- DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_save_customer ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 'Initial Customer', -- a_comment - '-1', -- a_id_customer - 'good co', -- a_name_company - 'teddy', -- a_name_contact - 'manufacturing', -- a_department_contact - 1, -- a_id_address - 'BRING BRING', -- a_phone_number - 'e@mail.com', -- a_email - 1, -- a_id_currency_cost - 1 -- a_active -); - -SELECT * FROM Shop_Customer -; - -DELETE FROM Shop_Customer -; - -*/ diff --git a/static/PostgreSQL/606_p_shop_save_customer_sales_order.sql b/static/PostgreSQL/606_p_shop_save_customer_sales_order.sql deleted file mode 100644 index 3c445bfb..00000000 --- a/static/PostgreSQL/606_p_shop_save_customer_sales_order.sql +++ /dev/null @@ -1,555 +0,0 @@ - --- DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; --- DROP TABLE IF EXISTS tmp_Msg_Error; - -CREATE OR REPLACE PROCEDURE p_shop_save_customer_sales_order ( - IN a_guid UUID, - IN a_id_user INTEGER, - IN a_comment VARCHAR(4000), - IN a_id_order INTEGER, - IN a_id_customer INTEGER, - IN a_id_currency_price INTEGER, - IN a_active BOOLEAN -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_comment VARCHAR(4000); - v_id_order INTEGER; - v_id_customer INTEGER; - v_id_currency_price INTEGER; - v_active BOOLEAN; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - v_id_error_type_no_permission INTEGER; - v_code_error_type_no_permission VARCHAR(50); - -- v_guid_permission UUID; - v_id_permission_Customer_Sales_order INTEGER; - v_id_access_level_EDIT INTEGER; - v_ids_product VARCHAR(4000); - v_ids_product_no_permission VARCHAR(4000); - -- v_id_order_new INTEGER; - v_id_change_set INTEGER; - v_is_new_Customer_Sales_order BOOLEAN; - result_errors REFCURSOR; -BEGIN - -- SET SESSION sql_mode = sys.list_drop(@@session.sql_mode, 'ONLY_FULL_GROUP_BY'); - - v_guid := COALESCE(a_guid, gen_random_uuid()); - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_comment := TRIM(COALESCE(a_comment, '')); - v_id_order := COALESCE(a_id_order, -1); - v_id_customer := a_id_customer; - v_id_currency_price := a_id_currency_price; - v_active := COALESCE(a_active, FALSE); - - v_code_error_type_bad_data := 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_code_error_type_no_permission := 'NO_PERMISSION'; - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_no_permission LIMIT 1); - -- v_guid_permission := gen_random_uuid(); - v_id_permission_Customer_Sales_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER_SALES_ORDER' LIMIT 1); - v_id_access_level_EDIT := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'EDIT'); - - v_is_new_Customer_Sales_order := CASE WHEN v_id_order <= 0 THEN TRUE ELSE FALSE END; - - -- Temporary tables - /* - CREATE TABLE tmp_Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_Sales_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - */ - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_delivered REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active BOOLEAN NOT NULL, - name_error VARCHAR(200) NOT NULL - ); - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - - - -- Argument validation - -- User ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF NOT EXISTS (SELECT * FROM Shop_User WHERE id_user = v_id_user) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid User ID: ', COALESCE(v_id_user, 'NULL'))) - ; - END IF; - END IF; - - -- Order ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF (v_id_order > 0) AND NOT EXISTS (SELECT * FROM Shop_Customer_Sales_Order WHERE id_order = v_id_order) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid Customer Sales Order ID: ', COALESCE(v_id_order, 'NULL'))) - ; - END IF; - END IF; - - -- Customer ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_customer) OR NOT EXISTS (SELECT * FROM Shop_Customer WHERE id_customer = v_id_customer) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid Customer ID: ', COALESCE(v_id_customer, 'NULL'))) - ; - END IF; - END IF; - - -- Currency ID - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF ISNULL(v_id_currency_price) OR NOT EXISTS (SELECT * FROM Shop_Currency WHERE id_currency = v_id_currency_price) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, CONCAT('Invalid currency ID: ', COALESCE(v_id_currency, 'NULL'))) - ; - END IF; - END IF; - - -- Comment - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF v_comment = '' THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES - (v_guid, v_id_error_type_bad_data, v_code_error_type_bad_data, 'A comment must be provided.') - ; - END IF; - END IF; - - - -- Get data from Temp table - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - INSERT INTO tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link, - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - name_error - ) - SELECT - CSOPL_T.id_link, - CSOPL_T.id_order, - CSOPL_T.id_permutation, - (PP.cost_local + PP.profit_local_min) * quantity_ordered AS price_total_local, - CSOPL_T.id_currency_price, - CSOPL_T.quantity_ordered, - CSOPL_T.id_unit_quantity, - CSOPL_T.quantity_delivered, - CSOPL_T.latency_delivery_days, - CSOPL_T.display_order, - CSOPL_T.active, - PP.id_permutation || ' - ' || COALESCE(P.name ,'') AS name_error - FROM Shop_Customer_Sales_Order_Product_Link_Temp CSOPL_T - INNER JOIN Shop_Product_Permutation PP ON CSOPL_T.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - WHERE CSOPL_T.GUID = v_guid - ; - DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp CSOPL_T - WHERE CSOPL_T.GUID = v_guid - ; - - /* - UPDATE tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - SET - price_total_local - */ - END IF; - - -- Invalid quantity ordered - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Customer_Sales_Order_Product_Link - WHERE - NOT ISNULL(quantity_ordered) - AND quantity_ordered < 0 - ) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Invalid quantity ordered property for the following permutations: ' || STRING_AGG(t_CSOPL.name_error, ', ') - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - -- INNER JOIN Shop_Product_Permutation PP ON t_CSOPL.id_permutation = PP.id_permutation - WHERE t_CSOPL.quantity_ordered < 0 - ; - END IF; - END IF; - - -- Duplicates - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - IF EXISTS (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL GROUP BY id_permutation HAVING COUNT(*) > 1) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - SELECT - v_guid, - v_id_error_type_bad_data, - v_code_error_type_bad_data, - 'Duplicate records: ' || STRING_AGG(t_CSOPLC.name_error, ', ') - FROM (SELECT id_permutation, name_error, COUNT(*) FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL GROUP BY id_permutation HAVING COUNT(*) > 1) t_CSOPLC - ; - END IF; - END IF; - - - - -- Permissions - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - v_ids_product := ( - SELECT STRING_AGG(DISTINCT PP.id_product, ',') - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_SPO - INNER JOIN Shop_Product_Permutation PP ON t_SPO.id_permutation = PP.id_permutation - ); - - CALL p_shop_calc_user(v_guid_permission, v_id_user, 0, v_id_permission_Customer_Sales_order, v_id_access_level_edit, v_ids_product); - - /* - UPDATE tmp_Shop_Supplier t_S - INNER JOIN Shop_Calc_User_Temp TP - ON TP.GUID = v_guid_permission - SET tP.can_view = TP.can_view, - tP.can_edit = TP.can_edit, - tP.can_admin = TP.can_admin; - */ - /* - SET v_has_permission := ( - SELECT can_edit - FROM Shop_Calc_User_Temp - WHERE - GUID = v_guid_permission - AND can_edit = 0 - ); - - IF v_has_permission = FALSE THEN - v_id_error_type_no_permission := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'NO_PERMISSION'); - INSERT INTO tmp_Msg_Error ( - guid, id_type, msg - ) - SELECT - v_guid, - v_id_error_type_no_permission, - CONCAT('You do not have ', name, ' permissions.') - FROM Shop_Permission - WHERE id_permission = v_id_permission_Customer_Sales_order - ; - END IF; - */ - v_ids_product_no_permission := ( - SELECT STRING_AGG(PT.id_product, ',') - FROM Shop_Calc_User_Temp PT - WHERE - PT.can_edit = 0 - AND NOT ISNULL(PT.id_product) - ); - IF NOT ISNULL(v_ids_product_no_permission) THEN - INSERT INTO tmp_Msg_Error ( - guid, id_type, code, msg - ) - VALUES ( - v_guid, - v_id_error_type_no_permission, - v_code_error_type_no_permission, - 'You do not have permission to edit the following product IDs: ' || v_ids_product_no_permission - ) - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid; - END IF; - - -- Transaction - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - START TRANSACTION; - INSERT INTO Shop_Sales_And_Purchasing_Change_Set ( - comment, - updated_last_by, - updated_last_on - ) - VALUES ( - 'Save ' - || CASE WHEN v_is_new_Customer_Sales_order = TRUE THEN 'new ' ELSE '' END - || 'Customer Sales Order - ' - || v_comment, - v_id_user, - CURRENT_TIMESTAMP - ); - - v_id_change_set := (SELECT id_change_set FROM Shop_Sales_And_Purchasing_Change_Set ORDER BY id_change_set DESC LIMIT 1); - - IF (v_is_new_Customer_Sales_order = 1) THEN - INSERT INTO Shop_Customer_Sales_Order ( - id_customer, - price_total_local, - id_currency_price, - created_by, - id_change_set, - active - ) - SELECT - v_id_customer, - SUM(t_CSOPL.price_total_local), - v_id_currency_price, - v_id_user, - v_id_change_set, - v_active - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ; - -- v_id_order_new - v_id_order := (SELECT id_order FROM Shop_Customer_Sales_Order ORDER BY id_order DESC LIMIT 1); - INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - v_id_order, -- v_id_order_new, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ; - ELSE - UPDATE Shop_Customer_Sales_Order CSO - SET - CSO.id_customer = v_id_customer, - CSO.price_total_local = SUM(t_CSOPL.price_total_local), - CSO.id_currency = v_id_currency_price, - CSO.id_change_set = v_id_change_set, - CSO.active = v_active - FROM Shop_Customer_Sales_Order CSO - INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL ON CSO.id_order = t_CSOPL.id_order - WHERE SPO.id_order = v_id_order - ; - IF EXISTS (SELECT * FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL INNER JOIN Shop_Customer_Sales_Order_Product_Link CSOPL ON t_CSOPL.id_link = CSOPL.id_link) THEN - UPDATE Shop_Customer_Sales_Order_Product_Link CSOPL - SET - CSOPL.id_order = t_CSOPL.id_order, - CSOPL.id_permutation = t_CSOPL.id_permutation, - CSOPL.price_total_local = t_CSOPL.price_total_local, - CSOPL.id_currency_price = t_CSOPL.id_currency_price, - CSOPL.quantity_ordered = t_CSOPL.quantity_ordered, - CSOPL.id_unit_quantity = t_CSOPL.id_unit_quantity, - CSOPL.quantity_delivered = t_CSOPL.quantity_delivered, - CSOPL.latency_delivery_days = t_CSOPL.latency_delivery_days, - CSOPL.display_order = t_CSOPL.display_order, - CSOPL.active = t_CSOPL.active, - CSOPL.id_change_set = v_id_change_set - FROM Shop_Customer_Sales_Order_Product_Link CSOPL - INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - ON CSOPL.id_link = t_CSOPL.id_link - ; - ELSE - INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - created_by, - id_change_set - ) - SELECT - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active, - v_id_user, - v_id_change_set - FROM tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL - WHERE t_CSOPL.id_link < 0 - ; - END IF; - END IF; - - COMMIT; - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error) THEN - ROLLBACK; - ELSE - COMMIT; - END IF; - */ - END IF; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - /* - -- Supplier Purchase Orders - SELECT * - FROM Shop_Customer_Sales_Order - WHERE id_order = v_id_order - ; - - -- Supplier Purchase Order Product Links - SELECT * - FROM Shop_Customer_Sales_Order_Product_Link - WHERE id_order = v_id_order - ; - */ - - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - - -- DROP TABLE tmp_Shop_Customer_Sales_Order; - DROP TABLE tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE tmp_Msg_Error; -END; -$$ LANGUAGE plpgsql; - - -/* - -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Audit; -DELETE FROM Shop_Customer_Sales_Order_Product_Link; -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp; -DELETE FROM Shop_Customer_Sales_Order_Audit; -DELETE FROM Shop_Customer_Sales_Order; - -INSERT INTO Shop_Customer_Sales_Order_Product_Link_Temp ( - guid, - id_link, - id_order, - id_permutation, - price_total_local, - id_currency_price, - quantity_ordered, - id_unit_quantity, - quantity_delivered, - latency_delivery_days, - display_order, - active -) -VALUES - ( - 'NIPS', -- guid - -1, -- id_link, - -1, -- id_order, - 1, -- id_permutation, - 100, -- price_total_local, - 1, -- id_currency_price, - 1, -- quantity_ordered, - 1, -- id_unit_quantity, - 1, -- quantity_delivered, - 14, -- latency_delivery_days , - 1, -- display_order - 1 -- active - ) -; - -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Temp; - -CALL p_shop_save_customer_sales_order ( - 'NIPS', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 'Initial customer', -- a_comment - -1, -- a_id_order - 4, -- a_id_customer - 1, -- a_id_currency_price - 1 -- a_active -); - -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Temp; - -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Audit; -DELETE FROM Shop_Customer_Sales_Order_Product_Link; -DELETE FROM Shop_Customer_Sales_Order_Product_Link_Temp; -DELETE FROM Shop_Customer_Sales_Order_Audit; -DELETE FROM Shop_Customer_Sales_Order; - - -*/ - diff --git a/static/PostgreSQL/610_p_shop_save_user.sql b/static/PostgreSQL/610_p_shop_save_user.sql deleted file mode 100644 index 2fcfb3f2..00000000 --- a/static/PostgreSQL/610_p_shop_save_user.sql +++ /dev/null @@ -1,166 +0,0 @@ - - - -/* - -CALL p_shop_save_user ( - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - '', -- a_name - '', -- a_email - 0 -- a_email_verified -) - -*/ - - -CREATE OR REPLACE PROCEDURE p_shop_save_user ( - IN a_id_user INTEGER, - IN a_name VARCHAR(255), - IN a_email VARCHAR(254), - IN a_email_verified BIT -) -AS $$ -DECLARE - v_id_user INTEGER; - v_name VARCHAR(255); - v_email VARCHAR(254); - v_email_verified BIT; - v_has_filter_user BOOLEAN; - result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_name := TRIM(COALESCE(a_name, '')); - v_email := TRIM(COALESCE(a_email, '')); - v_email_verified := COALESCE(a_email_verified, FALSE); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TABLE tmp_Shop_User ( - id_user INTEGER, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - id_type INTEGER NOT NULL, - -- code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - msg VARCHAR(4000) NOT NULL - ); - - - -- Parse filters - - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - IF NOT EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1) THEN - INSERT INTO Shop_User ( - id_user, - name, - email, - email_verified - ) - VALUES ( - v_id_user, - v_name, - v_email, - v_email_verified - ); - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - END IF; - - v_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - ELSE - INSERT INTO tmp_Msg_Error ( - id_type, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - 'No user ID provided.' - ) - ; - END IF; - - - /* - IF NOT EXISTS (SELECT msg FROM tmp_Msg_Error LIMIT 1) THEN - END IF; - */ - - - -- Returns - /* - -- User - SELECT * - FROM tmp_Shop_User - ; - */ - - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - - /* - -- Return arguments for test - SELECT a_id_user, - a_name, - a_email, - a_email_verified - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - - -/* - -CALL p_shop_save_user ( - '', - '', - '', - 0 -) - -*/ diff --git a/static/PostgreSQL/611_p_shop_save_user_basket.sql b/static/PostgreSQL/611_p_shop_save_user_basket.sql deleted file mode 100644 index 6952dada..00000000 --- a/static/PostgreSQL/611_p_shop_save_user_basket.sql +++ /dev/null @@ -1,833 +0,0 @@ - - - -/* - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '', -- a_ids_permutation_basket - '', -- a_quantities_permutation_basket - 1, -- a_id_permutation_edit - NULL, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -) - -* - - -CREATE OR REPLACE PROCEDURE p_shop_edit_user_basket ( - IN a_id_user INTEGER, - IN a_ids_permutation_basket VARCHAR(4000), - IN a_quantities_permutation_basket VARCHAR(4000), - IN a_id_permutation_edit INTEGER, - IN a_quantity_permutation_edit INTEGER, - IN a_sum_not_edit BOOLEAN, - IN a_id_currency INTEGER, - IN a_id_region_purchase INT -) -AS $$ -DECLARE - v_guid UUID; - v_id_user INTEGER; - v_ids_permutation_basket BOOLEAN; - v_quantities_permutation_basket VARCHAR -- REMAKE WITH TEMP TABLE -BEGIN - -- Argument redeclaration - -- Variable declaration - DECLARE v_has_filter_user BOOLEAN; - DECLARE v_has_filter_permutation_basket BOOLEAN; - DECLARE v_has_filter_permutation_edit BOOLEAN; - DECLARE v_has_filter_region BOOLEAN; - DECLARE v_has_filter_currency BOOLEAN; - DECLARE v_n_id_permutation_basket INTEGER; - DECLARE v_n_quantity_permutation_basket INTEGER; - DECLARE v_row_number INTEGER; - DECLARE v_guid UUID; - -- DECLARE v_id_user VARCHAR(100); - DECLARE v_id_permission_product INTEGER; - DECLARE v_ids_permutation_permission VARCHAR(4000); - DECLARE v_now TIMESTAMP; - -- DECLARE v_quantity_new INTEGER; - DECLARE v_change_set_used BOOLEAN; - DECLARE v_id_change_set INTEGER; - - SET v_guid = gen_random_uuid(); - - -- Argument validation + default values - IF a_id_user IS NULL THEN - SET a_id_user = ''; - ELSE - SET a_id_user = TRIM(a_id_user); - END IF; - IF a_ids_permutation_basket IS NULL THEN - SET a_ids_permutation_basket = ''; - ELSE - SET a_ids_permutation_basket = TRIM(a_ids_permutation_basket); - END IF; - IF a_quantities_permutation_basket IS NULL THEN - SET a_quantities_permutation_basket = ''; - ELSE - SET a_quantities_permutation_basket = TRIM(a_quantities_permutation_basket); - END IF; - IF a_sum_not_edit IS NULL THEN - SET a_sum_not_edit = TRUE; - END IF; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Msg_Error; - DROP TABLE IF EXISTS tmp_Shop_Basket; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Quantity; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TABLE tmp_Shop_User ( - id_user INTEGER, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TABLE tmp_Shop_Product ( - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - display_order INTEGER NOT NULL, - active INTEGER NOT NULL DEFAULT 1 - ); - - CREATE TEMPORARY TABLE tmp_Shop_Quantity( - quantity INTEGER NOT NULL, - display_order INTEGER NOT NULL, - active INTEGER NOT NULL DEFAULT 1 - ); - - CREATE TABLE tmp_Shop_Basket ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - id_region_purchase INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_region_purchase - FOREIGN KEY (id_region_purchase) - REFERENCES Shop_Region(id_region), - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Basket_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - quantity INTEGER NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE - /* - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL - */ - ); - - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - -- code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - msg VARCHAR(4000) NOT NULL - ); - - - -- Parse filters - SET v_has_filter_user = NOT (a_id_user = ''); - SET v_has_filter_permutation_basket = NOT (a_ids_permutation_basket = ''); - SET v_has_filter_permutation_edit = NOT ISNULL(a_id_permutation_edit); - SET v_has_filter_currency = NOT ISNULL(a_id_currency); - SET v_has_filter_region = NOT ISNULL(a_id_region_purchase); - -- SET v_quantity_new = CASE WHEN a_sum_not_edit THEN quantity + a_quantity_product_edit ELSE a_quantity_product_edit END; - /* - SELECT v_has_filter_user, v_has_filter_basket - ; - - */ - - -- Currency - IF NOT v_has_filter_currency THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Currency ID not provided.' - ) - ; - END IF; - IF v_has_filter_currency AND NOT EXISTS ( SELECT * FROM Shop_Currency WHERE id_currency = a_id_currency) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Currency ID not found: ', a_id_currency, '.') - ) - ; - END IF; - - -- Region - IF NOT v_has_filter_region THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Region ID not provided.' - ) - ; - END IF; - IF v_has_filter_region AND NOT EXISTS ( SELECT * FROM Shop_Region WHERE id_region = a_id_region_purchase) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Region ID not found: ', a_id_region_purchase, '.') - ) - ; - END IF; - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user LIKE CONCAT('%', a_id_user, '%') - AND active - LIMIT 1 - ; - - IF NOT EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1) THEN - SET v_has_filter_user = FALSE; - - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('User ID not found: ', a_id_user, '.') - ) - ; - END IF; - - SET a_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - END IF; - - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - SET v_change_set_used = FALSE; - INSERT INTO Shop_User_Change_Set ( - comment - ) - VALUES ( - 'edit basket' - ); - SET v_id_change_set := (SELECT id_change_set FROM Shop_User_Change_Set ORDER BY id_change_set DESC LIMIT 1); - END IF; - - -- Get basket - -- User - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - /* - display_order_category, - display_order_product - */ - ) - SELECT - C.id_category, - UB.id_product, - UB.id_permutation, - UB.id_region_purchase, - UB.id_currency, - UB.quantity, - UB.active - /* - C.display_order, - P.display_order - */ - FROM Shop_User_Basket UB - /* - INNER JOIN tmp_Shop_User t_U - ON UB.id_user = t_U.id_user - */ - INNER JOIN Shop_Product_Permutation PP - ON UB.id_product = PP.id_product - AND PP.active - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - AND C.active - WHERE UB.id_user = a_id_user - ; - END IF; - - -- Currency - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) - AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active AND id_currency != a_id_currency) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT( - 'Currency ID does not match currency of other items in basket. Basket currency: ', - (SELECT code FROM Shop_Currency WHERE id_currency = ( - SELECT - id_currency - FROM tmp_Shop_Basket - WHERE active - AND id_currency != a_id_currency - LIMIT 1 - )), - ', new currency: ', - (SELECT code FROM Shop_Currency WHERE id_currency = a_id_currency), - '.' - ) - ) - ; - END IF; - END IF; - - -- Region - IF EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) - AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket - WHERE - active - AND id_region_purchase != a_id_region_purchase - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Purchase region ID does not match region of other items in basket. Basket currency: ', - (SELECT code FROM Shop_Region WHERE id_region = ( - SELECT - id_region_purchase - FROM tmp_Shop_Basket - WHERE active - AND id_region != a_id_region_purchase - LIMIT 1 - )), - ', new currency: ', - (SELECT code FROM Shop_Region WHERE id_region = a_id_region_purchase), - '.' - ) - ) - ; - END IF; - END IF; - - -- String product id, permutation id, quantity list - IF NOT EXISTS (SELECT * FROM tmp_Shop_Basket WHERE active LIMIT 1) AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN -- NOT v_has_filter_user AND - -- Get product ids - CALL p_split(a_guid, a_ids_permutation_basket, ','); - INSERT INTO tmp_Shop_Product ( - id_product, id_permutation, display_order - ) - SELECT PP.id_product, ST.substring, ST.display_order - FROM Split_Temp ST - INNER JOIN Shop_Product_Permutation PP - ON ST.substring = PP.id_permutation - -- AND PP.active - ; - /* - SELECT substring as id_product, display_order - FROM Split_Temp - ; - */ - DROP TABLE Split_Temp; - - -- Get product quantities - CALL p_split(a_guid, a_quantities_permutation_basket, ','); - INSERT INTO tmp_Shop_Quantity ( - quantity, display_order - ) - SELECT substring, display_order - FROM Split_Temp - ; - /* - SELECT substring AS quantity_product, display_order - FROM Split_Temp - ; - */ - DROP TABLE Split_Temp; - - -- Compare number of product ids to number of quantities - SET v_n_id_permutation_basket := (SELECT display_order FROM tmp_Shop_Product ORDER BY display_order DESC LIMIT 1); - SET v_n_quantity_permutation_basket := (SELECT display_order FROM tmp_Shop_Quantity ORDER BY display_order DESC LIMIT 1); - IF NOT v_n_id_permutation_basket = v_n_quantity_permutation_basket THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Number of permutations (', v_n_id_permutation_basket, ') does not equal number of quantities (', v_n_quantity_permutation_basket, ') for basket.') - ) - ; - ELSE - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity - ) - SELECT - C.id_category, - P.id_product, - t_P.id_permutation, - a_id_region_purchase, - a_id_currency, - t_Q.quantity - FROM tmp_Shop_Product t_P - INNER JOIN tmp_Shop_Quantity t_Q - ON t_P.display_order = t_Q.display_order - INNER JOIN Shop_Product_Permutation PP - ON t_P.id_permutation = PP.id_permutation - AND PP.active - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - AND C.active - -- RIGHT JOIN tmp_Shop_Basket t_UB ON ISNULL(t_UB.id_product) - -- WHERE t_P.id_product NOT IN (SELECT id_product FROM tmp_Shop_Basket) - ; - - /* - IF EXISTS( - SELECT * - FROM Shop_Product P - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - INNER JOIN tmp_Shop_Basket t_B - ON P.id_product = t_B.id_product - WHERE C.active = FALSE OR P.active = FALSE LIMIT 1 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('No valid product IDs in list: ', a_ids_permutation_basket, '.') - ) - ; - END IF; - */ - END IF; - END IF; - - /* - select v_has_filter_edit; - select * from tmp_Shop_Basket; - select * from tmp_Msg_Error; - */ - - - -- Edit basket product - IF v_has_filter_permutation_edit AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - ( - C.active = FALSE - OR P.active = FALSE - OR PP.active = FALSE - ) - AND PP.id_permutation = a_id_permutation_edit - LIMIT 1 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - CONCAT('Invalid product ID to edit: ', a_id_product_edit, '.') - ) - ; - END IF; - END IF; - IF v_has_filter_permutation_edit AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket - WHERE - id_permutation = a_id_permutation_edit - ) THEN - UPDATE tmp_Shop_Basket - SET quantity = CASE WHEN a_sum_not_edit = TRUE THEN COALESCE(quantity, 0) + a_quantity_permutation_edit ELSE a_quantity_permutation_edit END, - active = CASE WHEN CASE WHEN a_sum_not_edit = TRUE THEN COALESCE(quantity, 0) + a_quantity_permutation_edit ELSE a_quantity_permutation_edit END = FALSE THEN FALSE ELSE TRUE END - WHERE id_permutation = a_id_permutation_edit - ; - - IF EXISTS ( - SELECT * - FROM tmp_Shop_Basket t_B - WHERE t_B.quantity < 0 - ) THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Invalid basket quantity.' - ) - ; - END IF; - - IF v_has_filter_user AND NOT EXISTS (SELECT msg FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - SET v_change_set_used = TRUE; - - UPDATE Shop_User_Basket UB - INNER JOIN tmp_Shop_Basket t_UB - ON UB.id_permutation = a_id_permutation_edit - SET UB.quantity = t_UB.quantity, - UB.active = t_UB.active, - UB.id_change_set_user = v_id_change_set - WHERE UB.id_permutation = a_id_permutation_edit - AND id_user = a_id_user - ; - END IF; - ELSE - IF a_quantity_permutation_edit < 0 THEN - INSERT INTO tmp_Msg_Error ( - id_type, - guid, - msg - ) - VALUES ( - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'BAD_DATA' LIMIT 1), - v_guid, - 'Invalid basket quantity.' - ) - ; - ELSE - INSERT INTO tmp_Shop_Basket ( - id_category, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - ) - SELECT - P.id_category, - P.id_product, - PP.id_permutation, - a_id_region_purchase, - a_id_currency, - a_quantity_permutation_edit, - CASE WHEN a_quantity_permutation_edit > 0 THEN TRUE ELSE FALSE END - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - WHERE id_permutation = a_id_permutation_edit - ; - IF v_has_filter_user THEN - IF EXISTS ( - SELECT * - FROM Shop_User_Basket UB - WHERE - UB.id_permutation = a_id_permutation_edit - ) THEN - SET v_change_set_used = TRUE; - - UPDATE Shop_User_Basket - INNER JOIN tmp_Shop_Basket t_UB ON UB.id_permutation = t_UB.id_permutation - SET UB.quantity = t_UB.quantity, - UB.active = t_UB.active, - UB.id_change_set_user = v_id_change_set - WHERE UB.id_permutation = a_id_permutation_edit - AND id_user = a_id_user - ; - ELSE - INSERT INTO Shop_User_Basket ( - id_user, - id_product, - id_permutation, - id_region_purchase, - id_currency, - quantity, - active - ) - SELECT a_id_user, - t_UB.id_product, - t_UB.id_permutation, - t_UB.id_region_purchase, - t_UB.id_currency, - t_UB.quantity, - t_UB.active - FROM tmp_Shop_Basket t_UB - WHERE id_permutation = a_id_permutation_edit - ; - END IF; - END IF; - END IF; - END IF; - END IF; - - - -- Checks - /* - SELECT * FROM tmp_Shop_Basket; - SELECT - STRING_AGG(t_UB.id_product, ',') AS basket_product_ids - FROM tmp_Shop_Basket t_UB - -- WHERE ISNULL(t_UB.id_permutation) - ; - SELECT - STRING_AGG(t_UB.id_permutation, ',') AS basket_permutation_ids - FROM tmp_Shop_Basket t_UB - WHERE NOT ISNULL(t_UB.id_permutation) - ; - */ - -- Returns - CALL p_shop_get_many_product ( - a_id_user, -- a_id_user - 1, -- a_get_all_categories - '', -- a_ids_category - 0, -- a_get_inactive_categories - 0, -- a_get_all_products - ( - SELECT - STRING_AGG(t_B.id_product, ',') - FROM tmp_Shop_Basket t_B - WHERE active = TRUE - ), -- a_ids_product - 0, -- a_get_inactive_products - 0, -- a_get_first_product_only - 0, -- a_get_all_product_permutations - ( - SELECT - STRING_AGG(t_B.id_permutation, ',') - FROM tmp_Shop_Basket t_B - WHERE NOT ISNULL(t_B.id_permutation) - AND active = TRUE - ), -- a_ids_permutation - 0, -- a_get_inactive_permutations - 0, -- a_get_all_images - '', -- a_ids_image - 0, -- a_get_inactive_images - 1, -- a_get_first_image_only - 0, -- a_get_all_delivery_region - a_id_region_purchase, -- a_ids_delivery_region - 0, -- a_get_inactive_delivery_region - 0, -- a_get_all_currency - a_id_currency, -- a_ids_currency - 0, -- a_get_inactive_currency - 1, -- a_get_all_discount - '', -- a_ids_discount - 0 -- a_get_inactive_discount - ); - - -- Basket - SELECT t_UB.id_category, - t_UB.id_product, - t_UB.id_permutation, - P.name, - PCL.price_local_VAT_incl, - PCL.price_local_VAT_excl, - PCL.id_currency, - t_UB.quantity - FROM tmp_Shop_Basket t_UB - INNER JOIN Shop_Product_Permutation PP - ON t_UB.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - INNER JOIN Shop_Product_Currency_Link PCL - ON PP.id_permutation = PCL.id_permutation - AND PCL.id_region_purchase = a_id_region_purchase - AND PCL.id_currency = a_id_currency - WHERE t_UB.active = TRUE - ORDER BY C.display_order, P.display_order - ; - - -- Errors - /* Completed by product get many */ - SELECT - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE GUID = v_guid - ; - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_categories, - a_ids_product, - a_get_inactive_products, - a_get_first_product_only, - a_get_all_products, - a_ids_image, - a_get_inactive_images, - a_get_first_image_only, - a_get_all_images - ; - */ - - -- Clean up - IF NOT v_change_set_used THEN - DELETE FROM Shop_User_Change_Set - WHERE id_change_set = v_id_change_set - ; - END IF; - - -- DROP TABLE IF EXISTS tmp_Msg_Error; - DELETE FROM tmp_Msg_Error WHERE guid = v_guid; - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error) THEN - DROP TABLE tmp_Msg_Error; - END IF; - DROP TABLE IF EXISTS tmp_Shop_Basket; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Quantity; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - -*/ - - -/* - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '', -- a_ids_permutation_basket - '', -- a_quantities_permutation_basket - 2, -- a_id_permutation_edit - 1, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 2, -- a_id_currency_edit - 1 -- a_id_region_purchase -); - -CALL p_shop_edit_user_basket ( - '', -- a_id_user - '1', -- a_ids_permutation_basket - '9', -- a_quantities_permutation_basket - 1, -- a_id_permutation_edit - 69, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -); -CALL p_shop_edit_user_basket ( - 'auth0|6582b95c895d09a70ba10feF', -- a_id_user - '2', -- a_ids_permutation_basket - '7', -- a_quantities_permutation_basket - 2, -- a_id_permutation_edit - NULL, -- a_quantity_permutation_edit - 1, -- a_sum_not_edit - 1, -- a_id_currency_edit - 1 -- a_id_region_purchase -); - - - {'a_id_user': 'auth0|6582b95c895d09a70ba10fef', - 'a_ids_permutation_basket': '1', - '7', -- a_quantities_permutation_basket - 'a_id_permutation_edit': 1, - 'a_quantity_permutation_edit': 1, - 'a_sum_not_edit': 1} - - select * from shop_user_basket; - insert into shop_user_change_set (comment) - values( 'deactivate duplicates'); - update SHOP_USER_BASKET - set active = FALSE, - id_change_set_user = (select id_change_set from shop_user_change_set order by id_change_set desc limit 1) - where id_user = 'auth0|6582b95c895d09a70ba10fef' - and id_product = 1 - ; - select * from shop_user_basket; -*/ diff --git a/static/PostgreSQL/700_p_shop_get_many_product.sql b/static/PostgreSQL/700_p_shop_get_many_product.sql deleted file mode 100644 index 0e879542..00000000 --- a/static/PostgreSQL/700_p_shop_get_many_product.sql +++ /dev/null @@ -1,1231 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_product ( - IN a_id_user INTEGER, - IN a_get_all_category BOOLEAN, - IN a_get_inactive_category BOOLEAN, - IN a_get_first_category_only BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_all_product BOOLEAN, - IN a_get_inactive_product BOOLEAN, - IN a_get_first_product_only BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_all_product_permutation BOOLEAN, - IN a_get_inactive_permutation BOOLEAN, - IN a_get_first_permutation_only BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_get_all_image BOOLEAN, - IN a_get_inactive_image BOOLEAN, - IN a_get_first_image_only BOOLEAN, - IN a_ids_image INTEGER[], - IN a_get_all_delivery_region BOOLEAN, - IN a_get_inactive_delivery_region BOOLEAN, - IN a_ids_delivery_region INTEGER[], - IN a_get_all_currency BOOLEAN, - IN a_get_inactive_currency BOOLEAN, - IN a_ids_currency INTEGER[], - IN a_get_all_discount BOOLEAN, - IN a_get_inactive_discount BOOLEAN, - IN a_ids_discount INTEGER[] -) -RETURNS SETOF REFCURSOR -- categories, SETOF products, SETOF variations, SETOF prices, SETOF images, SETOF delivery_options, SETOF discounts -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_category BOOLEAN; - v_get_inactive_category BOOLEAN; - v_get_first_category_only BOOLEAN; - v_ids_category INTEGER[]; - v_get_all_product BOOLEAN; - v_get_inactive_product BOOLEAN; - v_get_first_product_only BOOLEAN; - v_ids_product INTEGER[]; - v_get_all_product_permutation BOOLEAN; - v_get_inactive_permutation BOOLEAN; - v_get_first_permutation_only BOOLEAN; - v_ids_permutation INTEGER[]; - v_get_all_image BOOLEAN; - v_get_inactive_image BOOLEAN; - v_get_first_image_only BOOLEAN; - v_ids_image INTEGER[]; - v_get_all_delivery_region BOOLEAN; - v_get_inactive_delivery_region BOOLEAN; - v_ids_delivery_region INTEGER[]; - v_get_all_currency BOOLEAN; - v_get_inactive_currency BOOLEAN; - v_ids_currency INTEGER[]; - v_get_all_discount BOOLEAN; - v_get_inactive_discount BOOLEAN; - v_ids_discount INTEGER[]; - - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_image BOOLEAN; - v_has_filter_delivery_region BOOLEAN; - v_has_filter_currency BOOLEAN; - v_has_filter_discount BOOLEAN; - v_guid UUID; - -- v_id_user VARCHAR(100); - v_ids_permutation_unavailable VARCHAR(4000); - v_id_permission_product INTEGER; - v_ids_product_permission VARCHAR(4000); - -- v_ids_permutation_permission VARCHAR(4000); - v_id_access_level_view INTEGER; - -- v_now TIMESTAMP; - v_id_minimum INTEGER; - - result_categories REFCURSOR; - result_products REFCURSOR; - result_variations REFCURSOR; - result_prices REFCURSOR; - result_images REFCURSOR; - result_delivery_options REFCURSOR; - result_discounts REFCURSOR; - /* - -- result_errors REFCURSOR; - */ -BEGIN - v_id_user := a_id_user; - v_get_all_category := COALESCE(a_get_all_category, FALSE); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_get_first_category_only := COALESCE(a_get_first_category_only, TRUE); - v_ids_category := COALESCE(a_ids_category, ARRAY[]::INTEGER[]); - v_get_all_product := COALESCE(a_get_all_product, FALSE); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_get_first_product_only := COALESCE(a_get_first_product_only, TRUE); - v_ids_product := COALESCE(a_ids_product, ARRAY[]::INTEGER[]); - v_get_all_product_permutation := COALESCE(a_get_all_product_permutation, FALSE); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_get_first_permutation_only := COALESCE(a_get_first_permutation_only, TRUE); - v_ids_permutation := COALESCE(a_ids_permutation, ARRAY[]::INTEGER[]); - v_get_all_image := COALESCE(a_get_all_image, TRUE); - v_get_inactive_image := COALESCE(a_get_inactive_image, FALSE); - v_get_first_image_only := COALESCE(a_get_first_image_only, FALSE); - v_ids_image := COALESCE(a_ids_image, ARRAY[]::INTEGER[]); - v_get_all_delivery_region := COALESCE(a_get_all_delivery_region, TRUE); - v_get_inactive_delivery_region := COALESCE(a_get_inactive_delivery_region, FALSE); - v_ids_delivery_region := COALESCE(a_ids_delivery_region, ARRAY[]::INTEGER[]); - v_get_all_currency := COALESCE(a_get_all_currency, TRUE); - v_get_inactive_currency := COALESCE(a_get_inactive_currency, FALSE); - v_ids_currency := COALESCE(a_ids_currency, ARRAY[]::INTEGER[]); - v_get_all_discount := COALESCE(a_get_all_discount, TRUE); - v_get_inactive_discount := COALESCE(a_get_inactive_discount, FALSE); - v_ids_discount := COALESCE(a_ids_discount, ARRAY[]::INTEGER[]); - /* - ROLLBACK; - */ - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - - v_has_filter_category = (CARDINALITY(v_ids_category) > 0); - v_has_filter_product = (CARDINALITY(v_ids_product) > 0); - v_has_filter_permutation = (CARDINALITY(v_ids_permutation) > 0); - v_has_filter_image = (CARDINALITY(v_ids_image) > 0); - v_has_filter_delivery_region = (CARDINALITY(v_ids_delivery_region) > 0); - v_has_filter_currency = (CARDINALITY(v_ids_currency) > 0); - v_has_filter_discount = (CARDINALITY(v_ids_discount) > 0); - - /* - SELECT v_id_user, v_get_all_category, v_ids_category, v_get_inactive_category, v_get_all_product, - v_ids_product, v_get_inactive_product, v_get_first_product_only, v_get_all_product_permutation, v_ids_permutation, - v_get_inactive_permutation, v_get_all_image, v_ids_image, v_get_inactive_image, v_get_first_image_only, - v_get_all_delivery_region, v_ids_delivery_region, v_get_inactive_delivery_region, v_get_all_currency, v_ids_currency, - v_get_inactive_currency, v_get_all_discount, v_ids_discount, v_get_inactive_discount - ; - */ - - -- Temporary tables - /* - DROP TEMPORARY TABLE IF EXISTS tmp_Discount; - DROP TEMPORARY TABLE IF EXISTS tmp_Currency; - DROP TEMPORARY TABLE IF EXISTS tmp_Delivery_Region; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Image; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Variation; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product_Category; - */ - DROP TABLE IF EXISTS tmp_Discount; - DROP TABLE IF EXISTS tmp_Currency; - DROP TABLE IF EXISTS tmp_Delivery_Region; - DROP TABLE IF EXISTS tmp_Shop_Image; - DROP TABLE IF EXISTS tmp_Shop_Variation; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_Product_Category; - - CREATE TEMPORARY TABLE tmp_Shop_Product_Category ( - id_category INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_Category_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - */ - id_product INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - */ - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - /* - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - */ - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - -- rank_permutation INTEGER NOT NULL, -- _in_category - rank_category INTEGER NOT NULL, - rank_product INTEGER NOT NULL, - rank_permutation INTEGER NOT NULL, - name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - /* - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - */ - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations BOOLEAN NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BOOLEAN - ); - - /* - CREATE TEMPORARY TABLE tmp_Shop_Variation ( - id_variation INTEGER NOT NULL, - id_product INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TEMPORARY TABLE tmp_Shop_Image ( - id_image INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_image - FOREIGN KEY (id_image) - REFERENCES Shop_Image(id_image), - */ - id_product INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - */ - id_permutation INTEGER NULL, - /* - CONSTRAINT FK_tmp_Shop_Image_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - rank_in_product_permutation INTEGER NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Delivery_Region ( - id_region INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Delivery_Region_id_region - FOREIGN KEY (id_region) - REFERENCES Shop_Region(id_region), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL, - requires_delivery_option BOOLEAN NOT NULL DEFAULT FALSE - ); - - CREATE TEMPORARY TABLE tmp_Currency ( - id_currency INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Shop_Currency_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Discount ( - id_discount INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Discount_id_discount - FOREIGN KEY (id_discount) - REFERENCES Shop_Discount(id_discount), - */ - active BOOLEAN NOT NULL, - display_order INTEGER NOT NULL - ); - - /* - CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - /* - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - */ - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation, - -- rank_permutation, - rank_category, - rank_product, - rank_permutation, - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation, - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, -- PARTITION BY P.id_category -- _in_category - RANK() OVER (ORDER BY C.display_order) AS rank_category, - RANK() OVER (PARTITION BY P.id_category ORDER BY P.display_order) AS rank_product, - RANK() OVER (PARTITION BY P.id_category, P.id_product ORDER BY PP.display_order) AS rank_permutation, - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - v_get_all_product_permutation - OR ( - v_has_filter_permutation - -- AND FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - AND PP.id_permutation = ANY(v_ids_permutation) - ) - ) - AND (v_get_inactive_permutation OR PP.active) - ) - -- categories - AND ( - ( - v_get_all_category - OR ( - v_has_filter_category - -- AND FIND_IN_SET(P.id_category, v_ids_category) > 0 - AND C.id_category = ANY(v_ids_category) - ) - ) - AND (v_get_inactive_category OR C.active) - ) - -- products - AND ( - ( - v_get_all_product - OR ( - v_has_filter_product - -- AND FIND_IN_SET(P.id_product, v_ids_product) > 0 - AND P.id_product = ANY(v_ids_product) - ) - ) - AND (v_get_inactive_product OR P.active) - ) - ; - - -- select * from tmp_Shop_Product; - - IF v_get_first_category_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_category > 1 - ; - END IF; - - IF v_get_first_product_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_product > 1 - ; - END IF; - - IF v_get_first_permutation_only THEN - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.rank_permutation > 1 - ; - END IF; - - - INSERT INTO tmp_Shop_Product_Category ( - id_category, - active, - display_order - ) - SELECT DISTINCT C.id_category, - C.active, - C.display_order - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product_Category C - ON t_P.id_category = C.id_category - ORDER BY C.display_order - ; - - /* - INSERT INTO tmp_Shop_Variation ( - id_variation, id_product -- , display_order - ) - SELECT P.id_variation, P.id_product -- , P.display_order - FROM Shop_Variation V - INNER JOIN tmp_Shop_Product t_P - ON V.id_product = t_P.id_product - WHERE V.active; - */ - - -- Product Images - INSERT INTO tmp_Shop_Image ( - id_product, - id_permutation, - id_image, - active, - display_order, - rank_in_product_permutation - ) - SELECT id_product, - id_permutation, - id_image, - active, - ROW_NUMBER() OVER (ORDER BY display_order_product_temp, display_order_image), - RANK() OVER (PARTITION BY id_product, id_permutation ORDER BY display_order_product_temp, display_order_image) - FROM ( - SELECT t_P.id_product, - I.id_permutation, - I.id_image, - I.active, - I.display_order AS display_order_image, - t_P.rank_permutation AS display_order_product_temp - FROM Shop_Image I - INNER JOIN tmp_Shop_Product t_P - ON I.id_product = t_P.id_product - AND NOT t_P.product_has_variations - UNION - SELECT t_P.id_product, - I.id_permutation, - I.id_image, - I.active, - I.display_order AS display_order_image, - t_P.rank_permutation AS display_order_product_temp - FROM Shop_Image I - INNER JOIN tmp_Shop_Product t_P - ON I.id_permutation = t_P.id_permutation - AND t_P.product_has_variations - ) IPP - WHERE - ( - v_get_all_image - OR v_get_first_image_only - -- OR FIND_IN_SET(id_image, v_ids_image) > 0 - OR IPP.id_image = ANY(v_ids_image) - ) - AND (v_get_inactive_image OR IPP.active) - ; - - IF v_get_first_image_only THEN - DELETE FROM tmp_Shop_Image - WHERE rank_in_product_permutation > 1 - ; - END IF; - - /* - IF v_has_filter_image THEN - DELETE FROM tmp_Shop_Product - WHERE id_product NOT IN (SELECT DISTINCT id_product FROM tmp_Shop_Image); - DELETE FROM tmp_Shop_Product_Category - WHERE id_category NOT IN (SELECT DISTINCT id_category FROM tmp_Shop_Product); - END IF; - */ - - -- Delivery Regions - INSERT INTO tmp_Delivery_Region ( - id_region, - active, - display_order, - requires_delivery_option - ) - WITH RECURSIVE Recursive_CTE_Delivery_Region AS ( - SELECT - CAST(NULL AS INTEGER) AS id_region_parent, - DR.id_region AS id_region_child, - -- CASE WHEN FIND_IN_SET(DR.id_region, v_ids_delivery_region) > 0 THEN TRUE ELSE FALSE END AS requires_delivery_option - (DR.id_region = ANY(v_ids_delivery_region)) AS requires_delivery_option - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN Shop_Currency C ON PCRL.id_currency = C.id_currency - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - INNER JOIN Shop_Region DR ON PCRL.id_region_purchase = DR.id_region - WHERE - ( - v_get_all_delivery_region - -- OR FIND_IN_SET(DR.id_region, v_ids_delivery_region) > 0 - OR DR.id_region = ANY(v_ids_delivery_region) - ) - AND ( - v_get_inactive_delivery_region - OR DR.active = TRUE - ) - UNION - SELECT - DRB.id_region_parent, - DRB.id_region_child, - FALSE AS requires_delivery_option - FROM Shop_Region_Branch DRB - INNER JOIN Recursive_CTE_Delivery_Region r_DR - ON DRB.id_region_parent = r_DR.id_region_child - WHERE ( - v_get_inactive_delivery_region - OR DRB.active = TRUE - ) - ) - SELECT - DR.id_region, - DR.active, - DR.display_order, - requires_delivery_option - FROM Shop_Region DR - INNER JOIN Recursive_CTE_Delivery_Region r_DR - ON DR.id_region = r_DR.id_region_parent - OR DR.id_region = r_DR.id_region_child - ; - /* - select * from tmp_delivery_region; - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_product - OR v_get_all_product_permutation - ) */ - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - ; - */ - - IF v_has_filter_delivery_region THEN - v_ids_permutation_unavailable = ( - SELECT STRING_AGG(t_P.id_permutation, ', ') - FROM ( - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_produc - OR v_get_all_product_permutation - ) - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - */ - t_P.id_category = ANY(v_ids_category) - OR t_P.id_product = ANY(v_ids_product) - OR t_P.id_permutation = ANY(v_ids_permutation) - ) t_P - LEFT JOIN ( - SELECT * - FROM Shop_Product_Currency_Region_Link PCRL - WHERE - v_get_all_delivery_region - -- OR FIND_IN_SET(PCRL.id_region_purchase, v_ids_delivery_region) > 0 - OR PCRL.id_region_purchase = ANY(v_ids_delivery_region) - ) PCRL - ON t_P.id_product = PCRL.id_product - AND t_P.id_permutation = PCRL.id_permutation - LEFT JOIN tmp_Delivery_Region t_DR - ON PCRL.id_region_purchase = t_DR.id_region - AND t_DR.requires_delivery_option - WHERE - ISNULL(t_DR.id_region) - ); - IF NOT ISNULL(v_ids_permutation_unavailable) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'PRODUCT_AVAILABILITY' LIMIT 1), - 'PRODUCT_AVAILABILITY', - 'Error: The following permutation IDs are not available in this region: ' || COALESCE(v_ids_permutation_unavailable, 'NULL') - ); - */ - RAISE EXCEPTION 'The following permutation IDs are not available in this region: %', COALESCE(v_ids_permutation_unavailable, 'NULL') - USING ERRCODE = '22000'; - END IF; - /* - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.id_permutation NOT IN ( - SELECT - id_permutation - FROM Shop_Product_Currency_Region_Link PCL - INNER JOIN tmp_Delivery_Region t_DR - ON PCRL.id_region_purchase = t_DR.id_region - ); - */ - END IF; - - -- select * from tmp_Shop_Product; - - -- Currencies - INSERT INTO tmp_Currency ( - id_currency, - active, - display_order - ) - SELECT - C.id_currency, - C.active, - C.display_order - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN Shop_Currency C ON PCRL.id_currency = C.id_currency - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Delivery_Region t_DR ON PCRL.id_region_purchase = t_DR.id_region - WHERE - ( - v_get_all_currency - -- R FIND_IN_SET(C.id_currency, v_ids_currency) > 0 - OR C.id_currency = ANY(v_ids_currency) - ) - AND ( - v_get_inactive_currency - OR ( - C.active - AND PCRL.active - ) - ) - ; - - -- select * from tmp_Currency; - - IF v_has_filter_currency THEN - v_ids_permutation_unavailable = ( - SELECT STRING_AGG(t_P.id_permutation, ', ') - FROM ( - SELECT * - FROM tmp_Shop_Product t_P - WHERE - /*( - v_get_all_category - OR v_get_all_product - OR v_get_all_product_permutation - ) - FIND_IN_SET(t_P.id_category, v_ids_category) > 0 - OR FIND_IN_SET(t_P.id_product, v_ids_product) > 0 - OR FIND_IN_SET(t_P.id_permutation, v_ids_permutation) > 0 - */ - t_P.id_category = ANY(v_ids_category) - OR t_P.id_product = ANY(v_ids_product) - OR t_P.id_permutation = ANY(v_ids_permutation) - ) t_P - INNER JOIN ( - SELECT * - FROM Shop_Product_Currency_Region_Link PCRL - WHERE - ( - v_get_all_currency - -- OR FIND_IN_SET(PCRL.id_currency, v_ids_currency) > 0 - OR PCRL.id_currency = ANY(v_ids_currency) - ) - ) PCRL - ON t_P.id_permutation = PCRL.id_permutation - LEFT JOIN tmp_Currency t_C - ON PCRL.id_currency = t_C.id_currency - WHERE ISNULL(t_C.id_currency) - ); - IF NOT ISNULL(v_ids_permutation_unavailable) THEN - /* - INSERT INTO tmp_Msg_Error ( - guid, - id_type, - code, - msg - ) - VALUES ( - v_guid, - (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = 'PRODUCT_AVAILABILITY' LIMIT 1), - 'PRODUCT_AVAILABILITY', - 'Error: The following permutation IDs are not available in this currency: ' || COALESCE(v_ids_permutation_unavailable, 'NULL') - ); - */ - RAISE EXCEPTION 'The following permutation IDs are not available in this currency: %', COALESCE(v_ids_permutation_unavailable, 'NULL') - USING ERRCODE = '22000'; - END IF; - /* - DELETE FROM tmp_Shop_Product t_P - WHERE t_P.id_permutation NOT IN ( - SELECT - id_permutation - FROM Shop_Product_Currency_Region_Link PCL - INNER JOIN tmp_Currency t_C - ON PCRL.id_currency = t_C.id_currency - ); - */ - END IF; - - -- Discounts - INSERT INTO tmp_Discount ( - id_discount, - active, - display_order - ) - SELECT - D.id_discount, - D.active, - D.display_order - FROM Shop_Discount D - INNER JOIN tmp_Shop_Product t_P - ON D.id_product = t_P.id_product - AND D.id_permutation = t_P.id_permutation - WHERE - ( - v_get_all_discount - -- OR FIND_IN_SET(D.id_discount, v_ids_discount) > 0 - OR D.id_discount = ANY(v_ids_discount) - ) - AND ( - v_get_inactive_discount - OR D.active - ) - ; - -- select 'pre-permission results'; - -- select * from tmp_Shop_Product; - - -- Permissions - IF EXISTS (SELECT * FROM tmp_Shop_Product_Category LIMIT 1) THEN - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_product := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_PRODUCT' LIMIT 1); - v_ids_product_permission := (SELECT STRING_AGG(id_product, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_product_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product - AND UE_T.GUID = v_guid - ; - -- select * from Shop_Calc_User_Temp; - -- select * from tmp_Shop_Product; - - DELETE FROM tmp_Shop_Product t_P - WHERE - -- FIND_IN_SET(t_P.id_product, (SELECT STRING_AGG(UET.id_product, ',') FROM Shop_Calc_User_Temp UET)) = FALSE -- id_product NOT LIKE CONCAT('%', (SELECT STRING_AGG(id_product, '|') FROM Shop_Calc_User_Temp), '%'); - t_P.id_product NOT IN ( - SELECT id_product - FROM Shop_Calc_User_Temp UET - WHERE UET.GUID = v_guid - ) - OR ISNULL(t_P.can_view) - OR t_P.can_view = FALSE - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- Categories - OPEN result_categories FOR - -- RETURN QUERY - SELECT - DISTINCT t_C.id_category, - C.name, - C.description, - C.display_order - FROM tmp_Shop_Product_Category t_C - INNER JOIN Shop_Product_Category C - ON t_C.id_category = C.id_category - INNER JOIN tmp_Shop_Product t_P - ON t_C.id_category = t_P.id_category - ORDER BY C.display_order - ; - RETURN NEXT result_categories; - -- CLOSE result_categories; - - -- Products - OPEN result_products FOR - -- RETURN QUERY - SELECT - t_P.id_product, - t_P.id_permutation, - t_P.name, - t_P.description, - P.has_variations, - P.id_category, - PP.cost_local, - PP.id_currency_cost, - PP.profit_local_min, - t_P.latency_manufacture, - t_P.quantity_min, - t_P.quantity_max, - t_P.quantity_step, - t_P.quantity_stock, - t_P.id_stripe_product, - t_P.is_subscription, - RI.name AS name_interval_recurrence, - RI.name_plural AS name_plural_interval_recurrence, - t_P.count_interval_recurrence, - t_P.display_order_category, - t_P.display_order_product, - t_P.display_order_permutation, - COALESCE(t_P.can_view, FALSE), - COALESCE(t_P.can_edit, FALSE), - COALESCE(t_P.can_admin, FALSE) - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product P ON t_P.id_product = P.id_product - INNER JOIN Shop_Product_Permutation PP ON t_P.id_permutation = PP.id_permutation - LEFT JOIN Shop_Interval_Recurrence RI ON t_P.id_unit_measurement_interval_recurrence = RI.id_interval - ORDER BY t_P.rank_permutation - ; - RETURN NEXT result_products; - -- CLOSE result_products; - - -- Variations - OPEN result_variations FOR - -- RETURN QUERY - SELECT - V.id_variation, - t_P.id_product, - t_P.id_permutation, - t_P.id_category, - VT.code AS code_variation_type, - VT.name AS name_variation_type, - V.code AS code_variation, - V.name AS name_variation, - RANK() OVER (ORDER BY t_P.rank_permutation, PPVL.display_order) AS display_order - FROM Shop_Variation V - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL ON V.id_variation = PPVL.id_variation - INNER JOIN tmp_Shop_Product t_P ON PPVL.id_permutation = t_P.id_permutation - WHERE V.active - AND PPVL.active - ; - RETURN NEXT result_variations; - -- CLOSE result_variations; - - /* - -- Permutation variations output - SELECT t_P.id_permutation, - t_P.id_product, - t_P.id_category, - id_variation - FROM Shop_Product_Permutation_Variation_Link PPVL - INNER JOIN tmp_Shop_Product t_P - ON t_P.id_permutation = PPVL.id_permutation - ORDER BY t_P.display_order - ; - */ - -- select * from Shop_Product_Currency_Region_Link; - -- select * from shop_currency; - /* - select * from tmp_Currency; - select * from tmp_delivery_region; - select * from tmp_shop_product; - */ - - -- Product Price - OPEN result_prices FOR - -- RETURN QUERY - SELECT - PCRL.id_link AS id_price, - t_P.id_permutation, - t_P.id_product, - t_P.id_category, - t_C.id_currency, - C.code AS code_currency, - C.name AS name_currency, - C.symbol AS symbol_currency, - t_DR.id_region, - PCRL.price_local_VAT_incl, - PCRL.price_local_VAT_excl, - ROW_NUMBER() OVER(ORDER BY t_P.rank_permutation, C.display_order) AS display_order - FROM Shop_Product_Currency_Region_Link PCRL - INNER JOIN tmp_Shop_Product t_P - ON PCRL.id_product = t_P.id_product - AND PCRL.id_permutation = t_P.id_permutation - -- INNER JOIN Shop_Product P ON PCRL.id_product = P.id_product - INNER JOIN tmp_Currency t_C ON PCRL.id_currency = t_C.id_currency - INNER JOIN Shop_Currency C ON t_C.id_currency = C.id_currency - INNER JOIN tmp_Delivery_Region t_DR ON PCRL.id_region_purchase = t_DR.id_region - WHERE ( - v_get_inactive_product - AND v_get_inactive_permutation - AND v_get_inactive_currency - AND v_get_inactive_delivery_region - OR PCRL.active - ) - ORDER BY t_P.rank_permutation - ; - RETURN NEXT result_prices; - -- CLOSE result_prices; - - /* - -- Currency - SELECT - DISTINCT C.id_currency, - C.code, - C.name, - C.factor_from_GBP, - t_C.display_order - FROM Shop_Currency C - INNER JOIN tmp_Currency t_C ON C.id_currency = t_C.id_currency - GROUP BY C.id_currency, t_C.display_order - ORDER BY t_C.display_order - ; - */ - - -- Images - OPEN result_images FOR - -- RETURN QUERY - SELECT - t_I.id_image, - t_I.id_product, - t_I.id_permutation, - t_P.id_category, - I.url, - I.active, - I.display_order - FROM tmp_Shop_Image t_I - INNER JOIN Shop_Image I - ON t_I.id_image = I.id_image - INNER JOIN tmp_Shop_Product t_P - ON t_I.id_product = t_P.id_product - AND t_I.id_permutation = t_P.id_permutation - ORDER BY t_P.rank_permutation, I.display_order - ; - RETURN NEXT result_images; - -- CLOSE result_images; - - -- Delivery options - OPEN result_delivery_options FOR - -- RETURN QUERY - SELECT - _DO.id_option, - PDOL.id_product, - PDOL.id_permutation, - t_P.id_category, - _DO.code, - _DO.name, - _DO.latency_delivery_min, - _DO.latency_delivery_max, - _DO.quantity_min, - _DO.quantity_max, - STRING_AGG(DR.code, ',') AS codes_region, - STRING_AGG(DR.name, ',') AS names_region, - PDOL.price_local, - PDOL.display_order - FROM Shop_Delivery_Option _DO - INNER JOIN Shop_Product_Permutation_Delivery_Option_Link PDOL - ON _DO.id_option = PDOL.id_delivery_option - AND ( - v_get_inactive_delivery_region - OR PDOL.active - ) - INNER JOIN tmp_Shop_Product t_P - ON PDOL.id_product = t_P.id_product - AND PDOL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Delivery_Region t_DR ON PDOL.id_region = t_DR.id_region - INNER JOIN Shop_Region DR ON t_DR.id_region = DR.id_region - WHERE ( - v_get_inactive_delivery_region - OR _DO.active - ) - GROUP BY t_P.id_category, t_P.id_product, PDOL.id_permutation, t_P.rank_permutation, DR.id_region, _DO.id_option, PDOL.id_link - ORDER BY t_P.rank_permutation, PDOL.display_order - ; - RETURN NEXT result_delivery_options; - -- CLOSE result_delivery_options; - - -- Discounts - OPEN result_discounts FOR - -- RETURN QUERY - SELECT - D.id_discount, - P.id_category, - D.id_product, - D.id_permutation, - DR.id_region, - C.id_currency, - D.code AS code_discount, - D.name AS name_discount, - D.multiplier, - D.subtractor, - D.apply_multiplier_first, - D.quantity_min, - D.quantity_max, - D.date_start, - D.date_end, - STRING_AGG(DR.code, ', ') OVER(PARTITION BY D.id_discount) AS codes_region, - STRING_AGG(DR.name, ', ') OVER(PARTITION BY D.id_discount) AS names_region, - STRING_AGG(C.code, ', ') OVER(PARTITION BY D.id_discount) AS codes_currency, - STRING_AGG(C.name, ', ') OVER(PARTITION BY D.id_discount) AS names_currency, - ROW_NUMBER() OVER(ORDER BY D.display_order) AS display_order - FROM tmp_Discount t_D - INNER JOIN Shop_Discount D ON t_D.id_discount = D.id_discount - INNER JOIN Shop_Product P ON D.id_product = P.id_product - INNER JOIN tmp_Shop_Product t_P - ON D.id_product = t_P.id_product - -- AND D.id_permutation = t_P.id_permutation - INNER JOIN Shop_Discount_Region_Currency_Link DRCL - ON D.id_discount = DRCL.id_discount - INNER JOIN tmp_Delivery_Region t_DR ON DRCL.id_region = t_DR.id_region - INNER JOIN Shop_Region DR ON t_DR.id_region = DR.id_region - INNER JOIN tmp_Currency t_C ON DRCL.id_currency = t_C.id_currency - INNER JOIN Shop_Currency C ON t_C.id_currency = C.id_currency - GROUP BY D.id_discount, DR.id_region, C.id_currency, P.id_category, P.id_product, D.id_permutation - ORDER BY D.display_order, DR.display_order, C.display_order - ; - RETURN NEXT result_discounts; - -- CLOSE result_discounts; - /* - -- Delivery Regions - SELECT - t_DR.id_region, - t_P.id_category, - t_P.id_product, - t_P.id_permutation, - DR.code, - DR.name - FROM tmp_Delivery_Region t_DR - INNER JOIN Shop_Delivery_Region DR ON t_DR.id_region = DR.id_region - INNER JOIN Shop_Product_Region_Currency_Link PDRL - ON DR.id_region = PDRL.id_region - AND ( - v_get_inactive_delivery_region - OR PDRL.active - ) - INNER JOIN tmp_Shop_Product t_P - ON PDRL.id_product = t_P.id_product - AND PDRL.id_permutation = t_P.id_permutation - INNER JOIN tmp_Currency t_C ON PDRL.id_currency = t_C.id_currency - ORDER BY t_DR.display_order - ; - */ - - -- Errors - /* - OPEN result_errors FOR - RETURN QUERY - SELECT - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - /* - DROP TEMPORARY TABLE IF EXISTS tmp_Discount; - DROP TEMPORARY TABLE IF EXISTS tmp_Currency; - DROP TEMPORARY TABLE IF EXISTS tmp_Delivery_Region; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Image; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Variation; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product; - DROP TEMPORARY TABLE IF EXISTS tmp_Shop_Product_Category; - DROP TABLE IF EXISTS tmp_Discount; - DROP TABLE IF EXISTS tmp_Currency; - DROP TABLE IF EXISTS tmp_Delivery_Region; - DROP TABLE IF EXISTS tmp_Shop_Image; - DROP TABLE IF EXISTS tmp_Shop_Variation; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_Product_Category; - */ -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_product ( - 1, -- a_id_user - TRUE, -- a_get_all_category - FALSE, -- a_get_inactive_category - FALSE, -- a_get_first_category_only - ARRAY[]::INTEGER[], -- a_ids_category - TRUE, -- a_get_all_product - FALSE, -- a_get_inactive_product - FALSE, -- a_get_first_product_only - ARRAY[]::INTEGER[], -- a_ids_product - TRUE, -- a_get_all_product_permutation - FALSE, -- a_get_inactive_permutation - FALSE, -- a_get_first_permutation_only - ARRAY[1, 2, 3, 4, 5]::INTEGER[], -- a_ids_permutation - FALSE, -- a_get_all_image - FALSE, -- a_get_inactive_image - TRUE, -- a_get_first_image_only - ARRAY[]::INTEGER[], -- a_ids_image - FALSE, -- a_get_all_delivery_region - FALSE, -- a_get_inactive_delivery_region - ARRAY[]::INTEGER[], -- a_ids_delivery_region - FALSE, -- a_get_all_currency - FALSE, -- a_get_inactive_currency - ARRAY[]::INTEGER[], -- a_ids_currency - TRUE, -- a_get_all_discount - FALSE, -- a_get_inactive_discount - ARRAY[]::INTEGER[] -- a_ids_discount - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ diff --git a/static/PostgreSQL/701_p_shop_get_many_role_permission.sql b/static/PostgreSQL/701_p_shop_get_many_role_permission.sql deleted file mode 100644 index 27141e79..00000000 --- a/static/PostgreSQL/701_p_shop_get_many_role_permission.sql +++ /dev/null @@ -1,152 +0,0 @@ - -/* -DROP TABLE IF EXISTS tmp_Shop_Image; -DROP TABLE IF EXISTS tmp_Shop_Product; -DROP TABLE IF EXISTS tmp_Shop_Variation; -DROP TABLE IF EXISTS tmp_Shop_Product_Category; - -CREATE OR REPLACE PROCEDURE p_shop_get_many_role_permission ( - a_ids_role VARCHAR(4000), - a_get_inactive_roles BOOLEAN -) -AS $$ -DECLARE - v_ids_role VARCHAR(4000); - v_get_inactive_roles BOOLEAN; - v_has_filter_role BOOLEAN; - v_priority_view INTEGER; - v_priority_edit INTEGER; - v_priority_admin INTEGER; -BEGIN - v_ids_role := TRIM(COALESCE(a_ids_role, '')); - v_get_inactive_roles := COALESCE(a_get_inactive_roles, FALSE); - - -- v_ids_role = REPLACE(v_ids_role, '|', ',`'); - v_has_filter_role = CASE WHEN v_ids_role = '' THEN FALSE ELSE TRUE END; - - - -- Temporary tables - CREATE TABLE tmp_Permission ( - id_role INTEGER NOT NULL, - CONSTRAINT FK_tmp_User_Permission_id_role - FOREIGN KEY (id_role) - REFERENCES Shop_Role(id_role), - id_permission INTEGER, - CONSTRAINT FK_tmp_User_Permission_id_permission - FOREIGN KEY (id_permission) - REFERENCES Shop_Permission(id_permission), - id_access_level INTEGER, - CONSTRAINT FK_tmp_User_Permission_id_access_level - FOREIGN KEY (id_user) - REFERENCES Shop_Access_Level(id_user), - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - - INSERT INTO tmp_User_Permission ( - id_role, - id_permission, - id_access_level, - can_view, - can_edit, - can_admin - ) - SELECT U.id_user, - U.is_super_user, - U.is_super_user, - U.is_super_user, - U.is_super_user - FROM Shop_Role R - INNER JOIN Shop_Role_Permission_Link RPL - ON R.id_role = RPL.id_role - AND RPL.active - INNER JOIN Shop_Permission PERM - ON RPL.id_permission = PERM.id_permission - AND PERM.active - INNER JOIN Shop_Permission_Group PG - ON PERM.id_permission_group = PG.id_group - AND PG.active - LEFT JOIN Shop_Access_Level AL - ON RPL.id_access_level = AL.id_access_level - AND AL.active - WHERE FIND_IN_SET(R.id_role, v_ids_role) > 0 - AND PERM.required_access_level = FALSE OR AL. - ; - - UPDATE tmp_User_Permission t_UP - INNER JOIN Shop_Access_Level AL - ON AL.code = 'ADMIN' - SET t_UP.id_access_level = AL.id_access_level - WHERE t_UP.is_super_user - ; - - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Product_Category; - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_Image; -END; -$$ LANGUAGE plpgsql; -*/ - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_product ( - 1, -- a_id_user - TRUE, -- a_get_all_category - FALSE, -- a_get_inactive_category - FALSE, -- a_get_first_category_only - ARRAY[]::INTEGER[], -- a_ids_category - TRUE, -- a_get_all_product - FALSE, -- a_get_inactive_product - FALSE, -- a_get_first_product_only - ARRAY[]::INTEGER[], -- a_ids_product - TRUE, -- a_get_all_product_permutation - FALSE, -- a_get_inactive_permutation - FALSE, -- a_get_first_permutation_only - ARRAY[1, 2, 3, 4, 5]::INTEGER[], -- a_ids_permutation - FALSE, -- a_get_all_image - FALSE, -- a_get_inactive_image - TRUE, -- a_get_first_image_only - ARRAY[]::INTEGER[], -- a_ids_image - FALSE, -- a_get_all_delivery_region - FALSE, -- a_get_inactive_delivery_region - ARRAY[]::INTEGER[], -- a_ids_delivery_region - FALSE, -- a_get_all_currency - FALSE, -- a_get_inactive_currency - ARRAY[]::INTEGER[], -- a_ids_currency - TRUE, -- a_get_all_discount - FALSE, -- a_get_inactive_discount - ARRAY[]::INTEGER[] -- a_ids_discount - CURS1, - CURS2 - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/702.1_p_shop_get_many_currency.sql b/static/PostgreSQL/702.1_p_shop_get_many_currency.sql deleted file mode 100644 index ba8a9a43..00000000 --- a/static/PostgreSQL/702.1_p_shop_get_many_currency.sql +++ /dev/null @@ -1,57 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_currency ( - IN a_get_inactive_currency BOOLEAN -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_get_inactive_currency BOOLEAN; - result_currency REFCURSOR; -BEGIN - v_get_inactive_currency := COALESCE(a_get_inactive_currency, FALSE); - - OPEN result_currency FOR - SELECT - C.id_currency, - C.code, - C.name, - C.factor_from_GBP, - C.active, - C.display_order - FROM Shop_Currency C - WHERE v_get_inactive_currency - OR C.active - ORDER BY C.display_order - ; - RETURN NEXT result_currency; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_currency ( - FALSE -- a_get_inactive_currency - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ diff --git a/static/PostgreSQL/702.2_p_shop_get_many_region.sql b/static/PostgreSQL/702.2_p_shop_get_many_region.sql deleted file mode 100644 index 62202b1a..00000000 --- a/static/PostgreSQL/702.2_p_shop_get_many_region.sql +++ /dev/null @@ -1,60 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_region ( - IN a_get_inactive_region BOOLEAN -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_get_inactive_region BOOLEAN; - result_region REFCURSOR; -BEGIN - v_get_inactive_region := COALESCE(a_get_inactive_region, FALSE); - - OPEN result_region FOR - SELECT - R.id_region, - R.code, - R.name, - R.active, - R.display_order - FROM Shop_Region R - WHERE v_get_inactive_region - OR R.active - ORDER BY R.display_order - ; - -- RETURN NEXT result_region; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_region ( - FALSE -- a_get_inactive_region - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ diff --git a/static/PostgreSQL/703_p_shop_get_many_user_order.sql b/static/PostgreSQL/703_p_shop_get_many_user_order.sql deleted file mode 100644 index 8604c138..00000000 --- a/static/PostgreSQL/703_p_shop_get_many_user_order.sql +++ /dev/null @@ -1,283 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_user_order ( - IN a_id_user INTEGER, - IN a_ids_order VARCHAR(4000), - IN a_n_order_max INTEGER, - IN a_id_checkout_session VARCHAR(200) -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_ids_order VARCHAR(4000); - v_n_order_max INTEGER; - v_id_checkout_session VARCHAR(200); - v_has_filter_user BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_session BOOLEAN; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_orders REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_n_order_max := a_n_order_max; - v_id_checkout_session := TRIM(COALESCE(a_id_checkout_session, '')); - - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - v_ids_order = REPLACE(v_ids_order, '|', ','); - v_has_filter_order = CASE WHEN v_ids_order = '' THEN FALSE ELSE TRUE END; - v_has_filter_session = CASE WHEN v_id_checkout_session = '' THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_User; - DROP TABLE IF EXISTS tmp_Shop_Order; - - /* - CREATE TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - */ - - CREATE TEMPORARY TABLE tmp_Shop_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_Order_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_User_Order(id_order), - active BOOLEAN NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - -- id_type INTEGER NOT NULL, - -- CONSTRAINT FK_tmp_Msg_Error_id_type FOREIGN KEY (id_type) - -- REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50), - msg VARCHAR(4000) NOT NULL - ); - */ - - -- User - IF v_has_filter_user THEN - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - v_has_filter_user = EXISTS (SELECT id_user FROM tmp_Shop_User LIMIT 1); - v_id_user := (SELECT id_user FROM tmp_Shop_User LIMIT 1); - ELSE - RAISE EXCEPTION 'Valid user ID must be provided.' - USING ERRCODE = '22000' - ; - END IF; - - -- Permissions - CALL p_shop_calc_user ( - v_guid, -- a_guid - a_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_USER' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' AND active), -- a_ids_access_level - '', -- a_ids_product - '' -- a_ids_permutation - ); - - IF NOT (SELECT can_edit FROM Shop_Calc_User_Temp WHERE guid = v_guid) THEN - RAISE EXCEPTION 'User ID does not have permission to access orders.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - -- Invalid Order IDs - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_order = ANY(v_ids_order) - LIMIT 1 - ) THEN -- id_order LIKE CONCAT('%', v_ids_order, '%') LIMIT 1) THEN - RAISE EXCEPTION 'You do not have access to the following order IDs: %', ( - SELECT STRING_AGG(id_order, ', ') - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_order = ANY(v_ids_order) - ) - USING ERRCODE = '22000' - ; - END IF; - -- Invalid Checkout Session IDs - IF v_has_filter_session AND EXISTS ( - SELECT * - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_checkout_session = v_id_checkout_session - ) THEN - RAISE EXCEPTION 'You do not have access to the following checkout session IDs: %', ( - SELECT STRING_AGG(id_order, ', ') - FROM Shop_User_Order - WHERE - NOT (id_user = v_id_user) - AND id_checkout_session = v_id_checkout_session - ) - USING ERRCODE = '22000' - ; - END IF; - - -- Valid Orders - IF NOT EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - - INSERT INTO tmp_Shop_Order ( - id_order, - active - ) - SELECT UO.id_order, - UO.active - FROM Shop_User_Order UO - INNER JOIN tmp_Shop_User t_U - ON UO.id_user = t_U.id_user - AND t_U.active - WHERE ((NOT v_has_filter_order OR FIND_IN_SET(UO.id_order, v_ids_order) > 0) -- UO.id_order LIKE CONCAT('%', v_ids_order, '%')) - OR (NOT v_has_filter_session OR UO.id_checkout_session = v_id_checkout_session)) - AND UO.active - ; - END IF; - - - - -- Returns - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - OPEN result_orders FOR - SELECT t_O.id_order, - UOPL.id_product, - UOPL.id_permutation, - UOPL.quantity - FROM tmp_Shop_Order t_O - INNER JOIN Shop_User_Order UO - ON t_O.id_order = UO.id_order - INNER JOIN Shop_User_Order_Product_Link UOPL - ON UO.id_order = UOPL.id_order - WHERE t_O.active - ; - RETURN NEXT result_orders; - - /* - -- Errors - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user, - v_ids_order, - v_n_order_max, - v_id_checkout_session - ; - */ - - -- Clean up - -- DROP TABLE IF EXISTS tmp_Shop_User; - -- DROP TABLE IF EXISTS tmp_Shop_Order; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_user_order ( - 'auth0|6582b95c895d09a70ba10fef', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* - -CALL p_shop_get_many_user_order ( - 'auth0|6582b95c895d09a70ba10fef', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session -); - -CALL p_shop_get_many_user_order ( - '', # a_id_user - '1', # a_ids_order - 0, # a_n_order_max - '' # a_id_checkout_session -); - -insert into shop_product_change_set (comment) - values ('set product not subscription - test bool output to python'); - update shop_product - set is_subscription = 0, - id_change_set = (select id_change_set from shop_product_change_set order by id_change_set desc limit 1) - where id_product = 1 -select * from shop_User; -select * from shop_User_oRDER; -*/ diff --git a/static/PostgreSQL/704_p_shop_get_many_stripe_product_new.sql b/static/PostgreSQL/704_p_shop_get_many_stripe_product_new.sql deleted file mode 100644 index 2b7330ea..00000000 --- a/static/PostgreSQL/704_p_shop_get_many_stripe_product_new.sql +++ /dev/null @@ -1,316 +0,0 @@ - - -/* - -CALL p_shop_get_many_stripe_product_new ( - '' -) - -*/ - -CREATE OR REPLACE FUNCTION p_shop_get_many_stripe_product_new ( - IN a_id_user INTEGER -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_products REFCURSOR; - result_product_variation_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := a_id_user; - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TEMPORARY TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product ( - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active BOOLEAN NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NOT NULL, - name VARCHAR(200) NOT NULL, - description VARCHAR(4000) NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( -- IF NOT EXISTS - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - /* - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - */ - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- User - IF NOT EXISTS( - SELECT * - FROM Shop_User - WHERE - id_user = v_id_user - AND active - ) THEN - RAISE EXCEPTION 'Valid user ID required.' - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - -- Get products - INSERT INTO tmp_Shop_Product ( - id_product, - id_permutation, - active, - display_order_product, - display_order_permutation, - name, - description - ) - SELECT id_product, - id_permutation, - active, - display_order_product, - display_order_permutation, - name, - description - FROM ( - SELECT id_product, - NULL AS id_permutation, - active, - display_order AS display_order_product, - NULL AS display_order_permutation, - name, - description, - id_stripe_product - FROM Shop_Product P - UNION - SELECT t_PPPV.id_product, - id_permutation, - t_PPPV.active, - display_order_product, - display_order_permutation, - P.name, ': ' || names_variation AS name, - P.description || ' With variations: ' || type_name_pairs_variation AS description, - t_PPPV.id_stripe_product - FROM ( - SELECT P.id_product, - PP.id_permutation, - PP.active, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation, - STRING_AGG(V.name, ' ') AS names_variation, - STRING_AGG(VT.name || ': ' || V.name, ', ') AS type_name_pairs_variation, - PP.id_stripe_product - FROM Shop_Product_Permutation PP - INNER JOIN Shop_Product P - ON PP.id_product = P.id_product - AND P.active - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL - ON PP.id_permutation = PPVL.id_permutation - AND PPVL.active - INNER JOIN Shop_Variation V - ON PPVL.id_variation = V.id_variation - AND V.active - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - AND VT.active - GROUP BY id_product, id_permutation -- , VT.id_type, V.id_variation - ) t_PPPV - INNER JOIN Shop_Product P - ON t_PPPV.id_product = P.id_product - ) t_PPP - WHERE ISNULL(id_stripe_product) - AND active - ; - - -- Permissions - CALL p_shop_calc_user ( - v_guid, -- a_guid - v_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_ADMIN' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'ADMIN' AND active), -- a_ids_access_level - (SELECT STRING_AGG(id_product, ',') From tmp_Shop_Product), -- a_ids_product - (SELECT STRING_AGG(id_permutation, ',') From tmp_Shop_Product) -- a_ids_permutation -- WHERE NOT ISNULL(id_permutation) - ); - - IF EXISTS (SELECT can_admin FROM Shop_Calc_User_Temp WHERE guid = v_guid AND NOT can_admin) THEN - RAISE EXCEPTION 'User ID does not have permission to get all new stripe products.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - - - - -- Returns - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - DELETE FROM tmp_Shop_Product; - END IF; - */ - - OPEN result_products FOR - SELECT id_product, - id_permutation, - name, - description - FROM tmp_Shop_Product - ORDER BY display_order_product, display_order_permutation - ; - RETURN NEXT result_products; - - OPEN result_product_variation_links FOR - SELECT PP.id_permutation, - V.id_variation, - V.name AS name_variation, - VT.id_type AS id_type_variation, - VT.name as name_variation_type - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Product_Permutation PP - ON t_P.id_permutation = PP.id_permutation - INNER JOIN Shop_Product_Permutation_Variation_Link PPVL - ON PP.id_permutation = PPVL.id_permutation - AND PPVL.active - INNER JOIN Shop_Variation V - ON PPVL.id_variation = V.id_variation - AND V.active - INNER JOIN Shop_Variation_Type VT - ON V.id_type = VT.id_type - AND VT.active - ; - RETURN NEXT result_product_variation_links; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Product; - DROP TABLE IF EXISTS tmp_Shop_User; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_stripe_product_new ( - 'auth0|6582b95c895d09a70ba10fef' - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* -CALL p_shop_get_many_stripe_product_new ( - '' -); - -CALL p_shop_get_many_stripe_product_new ( - 'auth0|6582b95c895d09a70ba10fef' -); - - - -select * from shop_product; -select * from shop_product_permutation_variation_link; - -CALL p_shop_calc_user ( - 'ead789a1-c7ac-11ee-a256-b42e9986184a', -- a_guid - 'auth0|6582b95c895d09a70ba10fef', -- a_id_user - 0, -- a_get_inactive_users - '4', -- a_ids_permission - '3', -- a_ids_access_level - '1', -- a_ids_product - '1' -- a_ids_permutation -- WHERE NOT ISNULL(id_permutation) - ); - -*/ diff --git a/static/PostgreSQL/705_p_shop_get_many_stripe_price_new.sql b/static/PostgreSQL/705_p_shop_get_many_stripe_price_new.sql deleted file mode 100644 index a85fafd3..00000000 --- a/static/PostgreSQL/705_p_shop_get_many_stripe_price_new.sql +++ /dev/null @@ -1,253 +0,0 @@ - - - -/* - -CALL p_shop_get_many_stripe_price_new ( - '' -) - -*/ - - - -CREATE OR REPLACE FUNCTION p_shop_get_many_stripe_price_new ( - IN a_id_user INTEGER -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_has_filter_user BOOLEAN; - v_code_error_data VARCHAR(200); - v_code_error_permission VARCHAR(200); - v_guid UUID; - result_products REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_code_error_data := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 1); - v_code_error_permission := (SELECT code FROM Shop_Msg_Error_Type WHERE id_type = 2); - v_guid = gen_random_uuid(); - - v_has_filter_user = CASE WHEN v_id_user = '' THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; - DROP TABLE IF EXISTS tmp_Shop_User; - - CREATE TEMPORARY TABLE tmp_Shop_User( - id_user INTEGER PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_User_id_user - FOREIGN KEY (id_user) - REFERENCES Shop_User(id_user), - active BOOLEAN NOT NULL - ); - - CREATE TEMPORARY TABLE tmp_Shop_Product_Currency_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_link - FOREIGN KEY (id_link) - REFERENCES Shop_Product_Currency_Region_Link(id_link), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_CurrencyLink_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - id_currency INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_Currency_Link_id_currency - FOREIGN KEY (id_currency) - REFERENCES Shop_Currency(id_currency), - active BOOLEAN NOT NULL - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( -- IF NOT EXISTS - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - code VARCHAR(50) NOT NULL, - -- CONSTRAINT chk_tmp_Msg_Error_code CHECK (code IN (SELECT code FROM Shop_Msg_Error_Type)), - /* - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type(id_type), - */ - msg VARCHAR(4000) NOT NULL - ); - */ - - - -- User permissions - IF NOT EXISTS( - SELECT * - FROM Shop_User - WHERE - id_user = v_id_user - AND active - ) THEN - RAISE EXCEPTION 'Valid user ID required.' - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_User ( - id_user, - active - ) - SELECT id_user, - active - FROM Shop_User - WHERE id_user = v_id_user - AND active - LIMIT 1 - ; - - -- Get products - INSERT INTO tmp_Shop_Product_Currency_Link ( - id_link, - id_product, - id_permutation, - id_currency, - active - ) - SELECT id_link, - id_product, - id_permutation, - id_currency, - active - FROM Shop_Product_Currency_Region_Link - WHERE ISNULL(id_stripe_price) - AND active - ; - - -- Permissions - -- SELECT * FROM tmp_Msg_Error LIMIT 1; - CALL p_shop_calc_user ( - v_guid, -- a_guid - v_id_user, -- a_id_user - 0, -- a_get_inactive_users - CONVERT((SELECT id_permission FROM Shop_Permission WHERE 'STORE_ADMIN' = code), CHAR), -- a_ids_permission - (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'ADMIN' AND active), -- a_ids_access_level - (SELECT STRING_AGG(DISTINCT id_product, ',') FROM tmp_Shop_Product_Currency_Link), -- (SELECT DISTINCT id_product FROM tmp_Shop_Product_Currency_Link) calc_PCL) -- a_ids_product - (SELECT STRING_AGG(DISTINCT id_permutation, ',') FROM tmp_Shop_Product_Currency_Link) -- a_ids_permutation - ); - -- SELECT * FROM tmp_Msg_Error LIMIT 1; - - IF EXISTS (SELECT can_admin FROM Shop_Calc_User_Temp WHERE guid = v_guid AND NOT can_admin LIMIT 1) THEN - RAISE EXCEPTION 'User ID does not have permission to get all new stripe prices.' - USING ERRCODE = '42501' - ; - END IF; - - DELETE FROM Shop_Calc_User_Temp - WHERE guid = v_guid - ; - - - - -- Returns - /* - IF EXISTS (SELECT * FROM tmp_Msg_Error WHERE guid = v_guid LIMIT 1) THEN - DELETE FROM tmp_Shop_Product_Currency_Link; - END IF; - */ - /* - SELECT * - FROM tmp_Shop_User - ; - */ - - OPEN result_products FOR - SELECT t_PCL.id_product, - t_PCL.id_permutation, - P.price_GBP_full * C.factor_from_GBP AS unit_price, - C.code AS code_currency, - P.id_stripe_product, - P.is_subscription, - LOWER(RI.code) AS name_recurring_interval, - P.count_interval_recurrence - FROM tmp_Shop_Product_Currency_Link t_PCL - INNER JOIN Shop_Product P - ON t_PCL.id_product = P.id_product - AND P.active - INNER JOIN Shop_Interval_Recurrence RI - ON P.id_unit_measurement_interval_recurrence = RI.id_interval - AND RI.active - INNER JOIN Shop_Currency C - ON t_PCL.id_currency = C.id_currency - AND C.active - WHERE t_PCL.active - ; - RETURN NEXT result_products; - - -- Errors - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_id_user - ; - */ - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_User; - DROP TABLE IF EXISTS tmp_Shop_Product_Currency_Link; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; - curs1 refcursor; - rec1 record; - curs2 refcursor; - rec2 record; -BEGIN - FOR curs IN SELECT p_shop_get_many_stripe_price_new ( - 'auth0|6582b95c895d09a70ba10fef' - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - - -/* -CALL p_shop_get_many_stripe_price_new ( - '' -); - -CALL p_shop_get_many_stripe_price_new ( - 'auth0|6582b95c895d09a70ba10fef' -); - -*/ diff --git a/static/PostgreSQL/706_p_shop_get_many_supplier.sql b/static/PostgreSQL/706_p_shop_get_many_supplier.sql deleted file mode 100644 index 92efce78..00000000 --- a/static/PostgreSQL/706_p_shop_get_many_supplier.sql +++ /dev/null @@ -1,247 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_supplier ( - IN a_id_user INTEGER, - IN a_get_all_supplier BOOLEAN, - IN a_get_inactive_supplier BOOLEAN, - IN a_get_first_supplier_only BOOLEAN, - IN a_ids_supplier INTEGER[] -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_supplier BOOLEAN; - v_get_inactive_supplier BOOLEAN; - v_get_first_supplier_only BOOLEAN; - v_ids_supplier INTEGER[]; - v_has_filter_supplier BOOLEAN; - v_guid UUID; - v_id_permission_supplier INTEGER; - v_id_access_level_view INTEGER; - v_id_minimum INTEGER; - result_suppliers REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_supplier := COALESCE(a_get_all_supplier, TRUE); - v_get_inactive_supplier := COALESCE(a_get_inactive_supplier, FALSE); - v_get_first_supplier_only := COALESCE(a_get_first_supplier_only, FALSE); - v_ids_supplier := TRIM(COALESCE(a_ids_supplier, '')); - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - v_has_filter_supplier = NOT (a_ids_supplier = ''); - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Supplier; - - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier), - active BOOLEAN NOT NULL, - rank_supplier INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_supplier THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - ) THEN - RAISE EXCEPTION 'Invalid supplier IDs: %', ( - SELECT STRING_AGG(Supplier_Id, ', ') - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Supplier ( - id_supplier, - active, - rank_supplier - ) - SELECT - S.id_supplier, - S.active, - RANK() OVER (ORDER BY id_supplier ASC) AS rank_supplier - FROM Shop_Supplier S - WHERE - ( - a_get_inactive_supplier - OR S.active = TRUE - ) - AND ( - a_get_all_supplier - OR S.id_supplier = ANY(v_ids_supplier) - ) - ; - END IF; - - IF a_get_first_supplier_only THEN - DELETE FROM tmp_Shop_Supplier t_S - WHERE t_S.rank_supplier > ( - SELECT MIN(t_S.rank_supplier) - FROM tmp_Shop_Supplier t_S - ) - ; - END IF; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_supplier := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_SUPPLIER' LIMIT 1); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_id_permission_supplier, v_id_access_level_view, ''); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT name - FROM Shop_Permission - WHERE id_permission = v_id_permission_supplier - LIMIT 1 - ) - USING ERRCODE = '42501' - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - - -- Suppliers - OPEN result_suppliers FOR - SELECT - t_S.id_supplier, - S.name_company, - name_contact, - department_contact, - id_address, - phone_number, - fax, - email, - website, - id_currency, - active - FROM tmp_Shop_Supplier t_S - INNER JOIN Shop_Supplier S - ON t_S.id_supplier = S.id_supplier - ; - RETURN NEXT result_suppliers; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Supplier; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_supplier ( - '', -- a_id_user - TRUE, -- a_get_all_supplier - FALSE, -- a_get_inactive_supplier - FALSE, -- a_get_first_supplier_only - '' -- a_ids_supplier - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/706_p_shop_get_many_supplier_purchase_order.sql b/static/PostgreSQL/706_p_shop_get_many_supplier_purchase_order.sql deleted file mode 100644 index 8ff67675..00000000 --- a/static/PostgreSQL/706_p_shop_get_many_supplier_purchase_order.sql +++ /dev/null @@ -1,709 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_supplier_purchase_order ( - IN a_id_user INTEGER, - IN a_get_all_supplier BOOLEAN, - IN a_get_inactive_supplier BOOLEAN, - IN a_get_first_supplier_only BOOLEAN, - IN a_ids_supplier INTEGER[], - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_supplier BOOLEAN; - v_get_inactive_supplier BOOLEAN; - v_get_first_supplier_only BOOLEAN; - v_ids_supplier INTEGER[]; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - v_has_filter_supplier BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - v_ids_permission_supplier_purchase_order INTEGER[]; - v_ids_product_permission INTEGER[]; - v_id_access_level_view INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - result_suppliers REFCURSOR; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_supplier := COALESCE(a_get_all_supplier, TRUE); - v_get_inactive_supplier := COALESCE(a_get_inactive_supplier, FALSE); - v_get_first_supplier_only := COALESCE(a_get_first_supplier_only, FALSE); - v_ids_supplier := TRIM(COALESCE(a_ids_supplier, '')); - v_get_all_order := COALESCE(a_get_all_order, TRUE); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_supplier_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'SHOP_SUPPLIER_PURCHASE_ORDER' LIMIT 1); - v_code_error_data = 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - v_has_filter_supplier = (CARDINALITY(v_ids_supplier) > 0); - v_has_filter_order = (CARDINALITY(v_ids_order) > 0); - v_has_filter_category = (CARDINALITY(v_ids_category) > 0); - v_has_filter_product = (CARDINALITY(v_ids_product) > 0); - v_has_filter_permutation = (CARDINALITY(v_ids_permutation) > 0); - v_has_filter_date_from = CASE WHEN ISNULL(v_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to = CASE WHEN ISNULL(v_date_to) THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Supplier; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Supplier ( - id_supplier INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_id_supplier - FOREIGN KEY (id_supplier) - REFERENCES Shop_Supplier(id_supplier), - active BOOLEAN NOT NULL, - rank_supplier INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TABLE tmp_Shop_Supplier_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Supplier_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Supplier_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Supplier_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_Supplier_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - name VARCHAR(255) NOT NULL, - description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_supplier THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid supplier IDs: %', ( - SELECT STRING_AGG(Supplier_Id, ', ') - FROM UNNEST(v_ids_supplier) AS Supplier_Id - LEFT JOIN Shop_Supplier S ON Supplier_Id = S.id_supplier - WHERE ISNULL(S.id_supplier) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Supplier ( - id_supplier, - active, - rank_supplier - ) - SELECT - S.id_supplier, - S.active, - RANK() OVER (ORDER BY id_supplier ASC) AS rank_supplier - FROM Shop_Supplier S - INNER JOIN Split_Temp TS ON S.id_supplier = TS.substring - WHERE - ( - v_get_inactive_supplier - OR S.active = TRUE - ) - ; - END IF; - - IF v_get_first_supplier_only THEN - DELETE FROM tmp_Shop_Supplier t_S - WHERE t_S.rank_supplier > ( - SELECT MIN(t_S.rank_supplier) - FROM tmp_Shop_Supplier t_S - ) - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_product = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT STRING_AGG(Product_Id, ', ') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_permutation = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - ) - AND ( - v_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, v_ids_category) > 0 - ) - AND ( - v_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, v_ids_product) > 0 - ) - AND ( - v_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - -- SELECT * FROM Split_Temp TS LEFT JOIN Shop_Supplier_Purchase_Order SPO ON TS.substring = SPO.id_order WHERE ISNULL(SPO.id_order) - SELECT * - FROM UNNEST(v_ids_order) Order_Id - - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(TS.substring, ', ') - FROM UNNEST(v_ids_order) - LEFT JOIN Shop_Supplier_Purchase_Order SPO ON TS.substring = SPO.id_order - WHERE ISNULL(SPO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Supplier_Purchase_Order ( -- _Product_Link - id_order, - -- active, - rank_order - ) - SELECT - SPO.id_order, - -- SPO.active, - RANK() OVER (ORDER BY SPO.id_order ASC) AS rank_order - FROM Shop_Supplier_Purchase_Order SPO - -- INNER JOIN Split_Temp TS ON SPO.id_order = TS.substring - INNER JOIN Shop_Supplier_Purchase_Order_Product_Link SPOPL ON SPO.id_order = SPOPL.id_order - INNER JOIN Shop_Supplier S ON SPO.id_supplier_ordered = S.id_supplier - INNER JOIN Shop_Product_Permutation PP ON SPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON SPOPL.id_permutation = t_P.id_permutation - LEFT JOIN tmp_Shop_Supplier t_S ON SPO.id_supplier_ordered = t_S.id_supplier - WHERE - -- supplier - ( - v_has_filter_supplier = FALSE - OR NOT ISNULL(t_S.id_supplier) -- SPO.id_supplier_ordered IN (SELECT DISTINCT id_supplier FROM tmp_Shop_Supplier) - ) - -- order - AND ( - ( - v_has_filter_order = FALSE - OR ( - -- ID - -- FIND_IN_SET(SPO.id_order, v_ids_order) > 0 - SPO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = FALSE - OR SPO.created_on > v_date_from - ) - AND ( - v_has_filter_date_to = FALSE - OR SPO.created_on < v_date_to - ) - ) - ) - ) - -- active - /* - AND ( - v_get_inactive_order - OR SPO.active = TRUE - ) - */ - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = FALSE - ) - OR NOT ISNULL(t_P.id_permutation) -- SPO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF v_get_first_order_only THEN - DELETE FROM tmp_Shop_Supplier_Purchase_Order t_SPO - WHERE t_SPO.rank_order > ( - SELECT MIN(t_SPO.rank_order) - FROM tmp_Shop_Supplier_Purchase_Order t_SPO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_supplier_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_SUPPLIER', 'STORE_SUPPLIER_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(DISTINCT t_P.id_product, ',') FROM tmp_Shop_Product t_P WHERE NOT ISNULL(t_P.id_product)); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, FALSE, v_ids_permission_supplier_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT STRING_AGG(name, ', ') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_supplier_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- Suppliers - OPEN result_suppliers FOR - SELECT - t_S.id_supplier, - S.name_company, - S.name_contact, - S.department_contact, - S.id_address, - S.phone_number, - S.fax, - S.email, - S.website, - S.id_currency, - t_S.active - FROM tmp_Shop_Supplier t_S - INNER JOIN Shop_Supplier S - ON t_S.id_supplier = S.id_supplier - ; - RETURN NEXT result_suppliers; - - -- Supplier Purchase Order - OPEN result_orders FOR - SELECT -- * - t_SPO.id_order, - SPO.id_supplier_ordered, - SPO.cost_total_local, - SPO.id_currency_cost, - t_SPO.active - FROM Shop_Supplier_Purchase_Order SPO - INNER JOIN tmp_Shop_Supplier_Purchase_Order t_SPO ON SPO.id_order = t_SPO.id_order - ; - RETURN NEXT result_orders; - - -- Supplier Purchase Order Product Link - OPEN result_order_product_links FOR - SELECT - SPOPL.id_link, - SPOPL.id_order, - SPOPL.id_permutation, - P.name as name_product, - SPOPL.cost_total_local, - SPOPL.id_currency_cost, - SPOPL.quantity_ordered, - SPOPL.id_unit_quantity, - SPOPL.quantity_received, - SPOPL.latency_delivery_days, - SPOPL.display_order - FROM Shop_Supplier_Purchase_Order_Product_Link SPOPL - -- INNER JOIN tmp_Shop_Supplier_Purchase_Order_Product_Link t_SPOPL ON SPOPL.id_link = t_SPOPL.id_link - INNER JOIN tmp_Shop_Supplier_Purchase_Order t_SPO ON SPOPL.id_order = t_SPO.id_order - INNER JOIN Shop_Product_Permutation PP ON SPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY SPOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - */ - - /* - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Supplier_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Supplier; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_supplier_purchase_order ( - '', -- a_id_user - TRUE, -- a_get_all_supplier - FALSE, -- a_get_inactive_supplier - FALSE, -- a_get_first_supplier_only - '', -- a_ids_supplier - TRUE, -- a_get_all_order - -- FALSE, -- a_get_inactive_order - FALSE, -- a_get_first_order_only - '', -- a_ids_order - FALSE, -- a_get_inactive_category - '', -- a_ids_category - FALSE, -- a_get_inactive_product - '', -- a_ids_product - FALSE, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/708_p_shop_get_many_manufacturing_purchase_order.sql b/static/PostgreSQL/708_p_shop_get_many_manufacturing_purchase_order.sql deleted file mode 100644 index adad5f6c..00000000 --- a/static/PostgreSQL/708_p_shop_get_many_manufacturing_purchase_order.sql +++ /dev/null @@ -1,601 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_manufacturing_purchase_order ( - IN a_id_user INTEGER, - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - v_id_access_level_view INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - v_ids_permission_manufacturing_purchase_order VARCHAR(4000); - v_ids_product_permission INTEGER[]; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_all_order := COALESCE(a_get_all_order, TRUE); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_manufacturing_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'SHOP_manufacturing_PURCHASE_ORDER' LIMIT 1); - v_code_error_data = 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - - v_has_filter_order = CASE WHEN v_ids_order = '' THEN FALSE ELSE TRUE END; - v_has_filter_category = CASE WHEN v_ids_category = '' THEN FALSE ELSE TRUE END; - v_has_filter_product = CASE WHEN v_ids_product = '' THEN FALSE ELSE TRUE END; - v_has_filter_permutation = CASE WHEN v_ids_permutation = '' THEN FALSE ELSE TRUE END; - v_has_filter_date_from = CASE WHEN ISNULL(v_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to = CASE WHEN ISNULL(v_date_to) THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - /* - id_supplier_ordered INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Manufacturing_Purchase_Order_id_supplier_ordered - FOREIGN KEY (id_supplier_ordered) - REFERENCES Shop_Supplier(id_supplier), - */ - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - value_produced_total_local REAL NOT NULL, - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Manufacturing_Purchase_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_manufacturing_Purchase_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - cost_total_local REAL NOT NULL, - id_currency_cost INTEGER NOT NULL, - quantity_used REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_manufacturing_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_produced REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - -- name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - /* - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_category = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT COALESCE(STRING_AGG(Category_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_category - WHERE ISNULL(C.id_category) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_product = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT COALESCE(STRING_AGG(Product_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_permutation = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, v_ids_permutation) > 0 - ) - AND ( - v_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, v_ids_category) > 0 - ) - AND ( - v_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, v_ids_product) > 0 - ) - AND ( - v_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Manufacturing_Purchase_Order MPO ON Order_Id = MPO.id_order - WHERE ISNULL(MPO.id_order) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(Order_Id, ', ') - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Manufacturing_Purchase_Order MPO ON Order_Id = MPO.id_order - WHERE ISNULL(MPO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Manufacturing_Purchase_Order ( -- _Product_Link - id_order, - -- active, - rank_order - ) - SELECT - MPO.id_order, - -- MPO.active, - RANK() OVER (ORDER BY MPO.id_order ASC) AS rank_order - FROM Shop_Manufacturing_Purchase_Order MPO - -- INNER JOIN Split_Temp TS ON MPO.id_order = TS.substring - INNER JOIN Shop_manufacturing_Purchase_Order_Product_Link MPOPL ON MPO.id_order = MPOPL.id_order - INNER JOIN Shop_Product_Permutation PP ON MPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON MPOPL.id_permutation = t_P.id_permutation - WHERE - -- order - ( - ( - v_has_filter_order = 0 - OR ( - -- ID - -- FIND_IN_SET(MPO.id_order, v_ids_order) > 0 - MPO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = 0 - OR MPO.created_on > v_date_from - ) - AND ( - v_has_filter_date_to = 0 - OR MPO.created_on < v_date_to - ) - ) - ) - ) - -- active - /* - AND ( - v_get_inactive_order - OR MPO.active = TRUE - ) - */ - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = 0 - ) - OR NOT ISNULL(t_P.id_permutation) -- MPO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF v_get_first_order_only THEN - DELETE FROM tmp_Shop_Manufacturing_Purchase_Order t_MPO - WHERE t_MPO.rank_order > ( - SELECT MIN(t_MPO.rank_order) - FROM tmp_Shop_Manufacturing_Purchase_Order t_MPO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_manufacturing_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_manufacturing', 'STORE_manufacturing_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(P.id_product, ',') FROM (SELECT DISTINCT id_product FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)) P); - - -- SELECT v_guid, v_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, v_id_user, FALSE, v_ids_permission_manufacturing_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT STRING_AGG(name, ', ') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_manufacturing_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Product; - - -- Returns - - -- manufacturing Purchase Order - OPEN result_orders FOR - SELECT -- * - t_MPO.id_order, - MPO.cost_total_local, - MPO.id_currency_cost, - MPO.value_produced_total_local, - t_MPO.active - FROM Shop_Manufacturing_Purchase_Order MPO - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order t_MPO ON MPO.id_order = t_MPO.id_order - ; - RETURN NEXT result_orders; - - -- manufacturing Purchase Order Product Link - OPEN result_order_product_links FOR - SELECT - MPOPL.id_link, - MPOPL.id_order, - MPOPL.id_permutation, - P.name as name_product, - MPOPL.cost_total_local, - MPOPL.id_currency_cost, - MPOPL.value_produced_total_local, - MPOPL.quantity_used, - MPOPL.id_unit_quantity, - MPOPL.quantity_produced, - MPOPL.latency_manufacture, - MPOPL.display_order - FROM Shop_manufacturing_Purchase_Order_Product_Link MPOPL - -- INNER JOIN tmp_Shop_Manufacturing_Purchase_Order_Product_Link t_MPOPL ON MPOPL.id_link = t_MPOPL.id_link - INNER JOIN tmp_Shop_Manufacturing_Purchase_Order t_MPO ON MPOPL.id_order = t_MPO.id_order - INNER JOIN Shop_Product_Permutation PP ON MPOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY MPOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - v_ids_category, - v_get_inactive_category, - v_ids_product, - v_get_inactive_product, - v_get_first_product_only, - v_get_all_product, - v_ids_image, - v_get_inactive_image, - v_get_first_image_only, - v_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Manufacturing_Purchase_Order; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_manufacturing_purchase_order ( - '', -- a_id_user - TRUE, -- a_get_all_order - FALSE, -- a_get_inactive_order - FALSE, -- a_get_first_order_only - '', -- a_ids_order - FALSE, -- a_get_inactive_category - '', -- a_ids_category - FALSE, -- a_get_inactive_product - '', -- a_ids_product - FALSE, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/709_p_shop_get_many_customer.sql b/static/PostgreSQL/709_p_shop_get_many_customer.sql deleted file mode 100644 index d75d1f02..00000000 --- a/static/PostgreSQL/709_p_shop_get_many_customer.sql +++ /dev/null @@ -1,249 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_customer ( - IN a_id_user INTEGER, - IN a_get_all_customer BOOLEAN, - IN a_get_inactive_customer BOOLEAN, - IN a_get_first_customer_only BOOLEAN, - IN a_ids_customer INTEGER[] -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_customer BOOLEAN; - v_get_inactive_customer BOOLEAN; - v_get_first_customer_only BOOLEAN; - v_ids_customer INTEGER[]; - v_has_filter_customer BOOLEAN; - v_guid UUID; - v_id_permission_customer INTEGER; - v_id_access_level_view INTEGER; - v_id_error_type_bad_data INTEGER; - v_code_error_type_bad_data VARCHAR(50); - result_customers REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_inactive_customer := COALESCE(a_get_inactive_customer, FALSE); - v_get_first_customer_only := COALESCE(a_get_first_customer_only, FALSE); - v_ids_customer := TRIM(COALESCE(a_ids_customer, '')); - v_get_all_customer := COALESCE(a_get_all_customer, CASE WHEN v_ids_customer = '' THEN TRUE ELSE FALSE END); - - - v_code_error_type_bad_data = 'BAD_DATA'; - v_id_error_type_bad_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_type_bad_data LIMIT 1); - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW'); - - v_has_filter_customer = CASE WHEN a_ids_customer = '' THEN FALSE ELSE TRUE END; - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Customer; - - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - active BOOLEAN NOT NULL, - rank_customer INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_customer = TRUE OR a_get_all_customer = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) THEN - RAISE EXCEPTION 'Invalid customer IDs: %', ( - SELECT STRING_AGG(Customer_Id, ', ') - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Customer ( - id_customer, - active, - rank_customer - ) - SELECT - C.id_customer, - C.active, - RANK() OVER (ORDER BY C.id_customer ASC) AS rank_customer - FROM Shop_Customer C - LEFT JOIN Split_Temp S_T ON C.id_customer = S_T.substring - WHERE - ( - a_get_all_customer = 1 - OR NOT ISNULL(S_T.substring) - ) - AND ( - a_get_inactive_customer = 1 - OR C.active = TRUE - ) - ; - END IF; - - IF a_get_first_customer_only = TRUE THEN - DELETE FROM tmp_Shop_Customer t_C - WHERE t_C.rank_customer > ( - SELECT MIN(t_C.rank_customer) - FROM tmp_Shop_Customer t_C - ) - ; - END IF; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_id_permission_customer := (SELECT id_permission FROM Shop_Permission WHERE code = 'STORE_CUSTOMER' LIMIT 1); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_id_permission_customer, v_id_access_level_view, ''); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT COALESCE(STRING_AGG(name, ', '), 'NULL') - FROM Shop_Permission - WHERE id_permission = v_id_permission_customer - ) - USING ERRCODE = '42501' - ; - END IF; - - - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- customers - OPEN result_customers FOR - SELECT - t_C.id_customer, - C.name_company, - C.name_contact, - C.department_contact, - C.id_address, - C.phone_number, - C.email, - C.id_currency, - C.active - FROM tmp_Shop_Customer t_C - INNER JOIN Shop_Customer C ON t_C.id_customer = C.id_customer - ; - RETURN NEXT result_customers; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Customer; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_customer ( - '', -- a_id_user - 1, -- a_get_all_customer - 0, -- a_get_inactive_customer - 0, -- a_get_first_customer_only - '' -- a_ids_customer - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/710_p_shop_get_many_customer_sales_order.sql b/static/PostgreSQL/710_p_shop_get_many_customer_sales_order.sql deleted file mode 100644 index e880a2a0..00000000 --- a/static/PostgreSQL/710_p_shop_get_many_customer_sales_order.sql +++ /dev/null @@ -1,718 +0,0 @@ - - -CREATE OR REPLACE FUNCTION p_shop_get_many_customer_sales_order ( - IN a_id_user INTEGER, - IN a_get_all_customer BOOLEAN, - IN a_get_inactive_customer BOOLEAN, - IN a_get_first_customer_only BOOLEAN, - IN a_ids_customer INTEGER[], - IN a_get_all_order BOOLEAN, - IN a_get_inactive_order BOOLEAN, - IN a_get_first_order_only BOOLEAN, - IN a_ids_order INTEGER[], - IN a_get_inactive_category BOOLEAN, - IN a_ids_category INTEGER[], - IN a_get_inactive_product BOOLEAN, - IN a_ids_product INTEGER[], - IN a_get_inactive_permutation BOOLEAN, - IN a_ids_permutation INTEGER[], - IN a_date_from TIMESTAMP, - IN a_date_to TIMESTAMP -) -RETURNS SETOF REFCURSOR -AS $$ -DECLARE - v_id_user INTEGER; - v_get_all_customer BOOLEAN; - v_get_inactive_customer BOOLEAN; - v_get_first_customer_only BOOLEAN; - v_ids_customer INTEGER[]; - v_get_all_order BOOLEAN; - v_get_inactive_order BOOLEAN; - v_get_first_order_only BOOLEAN; - v_ids_order INTEGER[]; - v_get_inactive_category BOOLEAN; - v_ids_category INTEGER[]; - v_get_inactive_product BOOLEAN; - v_ids_product INTEGER[]; - v_get_inactive_permutation BOOLEAN; - v_ids_permutation INTEGER[]; - v_date_from TIMESTAMP; - v_date_to TIMESTAMP; - -- Argument redeclaration - -- Variable declaration - v_has_filter_customer BOOLEAN; - v_has_filter_order BOOLEAN; - v_has_filter_category BOOLEAN; - v_has_filter_product BOOLEAN; - v_has_filter_permutation BOOLEAN; - v_has_filter_date_from BOOLEAN; - v_has_filter_date_to BOOLEAN; - v_guid UUID; - -- v_id_user VARCHAR(100); - -- v_ids_permutation_unavailable VARCHAR(4000); - v_ids_permission_customer_purchase_order VARCHAR(4000); - v_ids_product_permission VARCHAR(4000); - -- v_ids_permutation_permission VARCHAR(4000); - v_id_access_level_view INTEGER; - -- v_now TIMESTAMP; - -- v_id_minimum INTEGER; - v_code_error_data VARCHAR(50); - v_id_type_error_data INTEGER; - result_customers REFCURSOR; - result_orders REFCURSOR; - result_order_product_links REFCURSOR; - -- result_errors REFCURSOR; -BEGIN - v_id_user := TRIM(COALESCE(a_id_user, '')); - v_get_inactive_customer := COALESCE(a_get_inactive_customer, FALSE); - v_get_first_customer_only := COALESCE(a_get_first_customer_only, FALSE); - v_ids_customer := TRIM(COALESCE(a_ids_customer, '')); - v_get_all_customer := COALESCE(a_get_all_customer, CASE WHEN v_ids_customer = '' THEN TRUE ELSE FALSE END); - v_get_inactive_order := COALESCE(a_get_inactive_order, FALSE); - v_get_first_order_only := COALESCE(a_get_first_order_only, FALSE); - v_ids_order := TRIM(COALESCE(a_ids_order, '')); - v_get_all_order := COALESCE(a_get_all_order, CASE WHEN v_ids_order = '' THEN TRUE ELSE FALSE END); - v_get_inactive_category := COALESCE(a_get_inactive_category, FALSE); - v_ids_category := TRIM(COALESCE(a_ids_category, '')); - v_get_inactive_product := COALESCE(a_get_inactive_product, FALSE); - v_ids_product := TRIM(COALESCE(a_ids_product, '')); - v_get_inactive_permutation := COALESCE(a_get_inactive_permutation, FALSE); - v_ids_permutation := TRIM(COALESCE(a_ids_permutation, '')); - v_date_from := a_date_from; - v_date_to := a_date_to; - - v_guid := gen_random_uuid(); - v_id_access_level_view := (SELECT id_access_level FROM Shop_Access_Level WHERE code = 'VIEW' LIMIT 1); - -- v_ids_permission_customer_purchase_order := (SELECT id_permission FROM Shop_Permission WHERE code = 'Shop_Customer_Sales_ORDER' LIMIT 1); - v_code_error_data := 'BAD_DATA'; - v_id_type_error_data := (SELECT id_type FROM Shop_Msg_Error_Type WHERE code = v_code_error_data); - - v_has_filter_category := CASE WHEN a_ids_category = '' THEN FALSE ELSE TRUE END; - v_has_filter_product := CASE WHEN a_ids_product = '' THEN FALSE ELSE TRUE END; - v_has_filter_permutation := CASE WHEN a_ids_permutation = '' THEN FALSE ELSE TRUE END; - v_has_filter_date_from := CASE WHEN ISNULL(a_date_from) THEN FALSE ELSE TRUE END; - v_has_filter_date_to := CASE WHEN ISNULL(a_date_to) THEN FALSE ELSE TRUE END; - - - -- Temporary tables - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order; - DROP TABLE IF EXISTS tmp_Shop_Customer; - DROP TABLE IF EXISTS tmp_Shop_Product; - - CREATE TABLE tmp_Shop_Customer ( - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - active BOOLEAN NOT NULL, - rank_customer INTEGER NULL, - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - CREATE TABLE tmp_Shop_Customer_Sales_Order ( - id_order INTEGER NOT NULL PRIMARY KEY, - /* - id_customer INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Customer_Sales_Order_id_customer - FOREIGN KEY (id_customer) - REFERENCES Shop_Customer(id_customer), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - */ - active BOOLEAN NOT NULL, - rank_order INTEGER NOT NULL - ); - - /* - CREATE TABLE tmp_Shop_Customer_Sales_Order_Product_Link ( - id_link INTEGER NOT NULL PRIMARY KEY, - id_order INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_order - FOREIGN KEY (id_order) - REFERENCES Shop_Customer_Sales_Order(id_order), - id_permutation INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - price_total_local REAL NOT NULL, - id_currency_price INTEGER NOT NULL, - quantity_ordered REAL NOT NULL, - id_unit_quantity INTEGER NOT NULL, - CONSTRAINT FK_tmp_customer_Purchase_Order_Product_Link_id_unit_quantity - FOREIGN KEY (id_unit_quantity) - REFERENCES Shop_Unit_Measurement(id_unit_measurement), - quantity_received REAL NULL, - latency_delivery_days INTEGER NOT NULL, - display_order INTEGER NOT NULL - ); - */ - - CREATE TABLE tmp_Shop_Product ( - id_category INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_category - FOREIGN KEY (id_category) - REFERENCES Shop_Product_Category(id_category), - id_product INTEGER NOT NULL, - CONSTRAINT FK_tmp_Shop_Product_id_product - FOREIGN KEY (id_product) - REFERENCES Shop_Product(id_product), - -- product_has_variations BOOLEAN NOT NULL, - id_permutation INTEGER NULL, - CONSTRAINT FK_tmp_Shop_Product_id_permutation - FOREIGN KEY (id_permutation) - REFERENCES Shop_Product_Permutation(id_permutation), - active_category BOOLEAN NOT NULL, - active_product BOOLEAN NOT NULL, - active_permutation BOOLEAN NULL, - display_order_category INTEGER NOT NULL, - display_order_product INTEGER NOT NULL, - display_order_permutation INTEGER NULL, - rank_permutation INTEGER NOT NULL, -- _in_category - -- name VARCHAR(255) NOT NULL, - -- description VARCHAR(4000) NOT NULL, - /* - price_GBP_full REAL NOT NULL, - price_GBP_min REAL NOT NULL, - */ - /* - latency_manufacture INTEGER NOT NULL, - quantity_min REAL NOT NULL, - quantity_max REAL NOT NULL, - quantity_step REAL NOT NULL, - quantity_stock REAL NOT NULL, - is_subscription BOOLEAN NOT NULL, - id_unit_measurement_interval_recurrence INTEGER, - CONSTRAINT FK_tmp_Shop_Product_id_unit_measurement_interval_recurrence - FOREIGN KEY (id_unit_measurement_interval_recurrence) - REFERENCES Shop_Interval_Recurrence(id_interval), - count_interval_recurrence INTEGER, - id_stripe_product VARCHAR(100), - product_has_variations INTEGER NOT NULL, - */ - can_view BOOLEAN, - can_edit BOOLEAN, - can_admin BIT - ); - - /* - CREATE TABLE IF NOT EXISTS tmp_Msg_Error ( - display_order INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - guid UUID NOT NULL, - id_type INTEGER NOT NULL, - CONSTRAINT FK_tmp_Msg_Error_id_type - FOREIGN KEY (id_type) - REFERENCES Shop_Msg_Error_Type (id_type), - code VARCHAR(50) NOT NULL, - msg VARCHAR(4000) NOT NULL - ); - */ - - -- select v_has_filter_product, v_has_filter_permutation; - - IF v_has_filter_customer = TRUE OR a_get_all_customer = TRUE THEN - IF EXISTS ( - SELECT * - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid customer IDs: %', ( - SELECT STRING_AGG(Customer_Id, ', ') - FROM UNNEST(v_ids_customer) AS Customer_Id - LEFT JOIN Shop_Customer C ON Customer_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) - USING ERRCODE = '22000' - ; - ELSE - INSERT INTO tmp_Shop_Customer ( - id_customer, - active, - rank_customer - ) - SELECT - C.id_customer, - C.active, - RANK() OVER (ORDER BY id_customer ASC) AS rank_customer - FROM Shop_Customer C - -- LEFT JOIN Split_Temp S_T ON C.id_customer = S_T.substring - WHERE - ( - a_get_all_customer = TRUE - -- OR NOT ISNULL(S_T.substring) - OR C.id_customer = ANY(v_ids_customer) - ) - AND ( - a_get_inactive_customer - OR C.active = TRUE - ) - ; - END IF; - - IF a_get_first_customer_only THEN - DELETE FROM tmp_Shop_Customer t_C - WHERE t_C.rank_customer > ( - SELECT MIN(t_C.rank_customer) - FROM tmp_Shop_Customer t_C - ) - ; - END IF; - END IF; - - IF v_has_filter_category = TRUE AND EXISTS ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_customer - WHERE ISNULL(C.id_customer) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid category IDs: %', ( - SELECT STRING_AGG(Category_Id, ', ') - FROM UNNEST(v_ids_category) AS Category_Id - LEFT JOIN Shop_Product_Category C ON Category_Id = C.id_customer - WHERE ISNULL(C.id_customer) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_product = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid product IDs: %', ( - SELECT COALESCE(STRING_AGG(Product_Id, ', ') ,'NULL') - FROM UNNEST(v_ids_product) AS Product_Id - LEFT JOIN Shop_Product P ON Product_Id = P.id_product - WHERE ISNULL(P.id_product) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_permutation = TRUE AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid permutation IDs: %', ( - SELECT STRING_AGG(Permutation_Id, ', ') - FROM UNNEST(v_ids_permutation) AS Permutation_Id - LEFT JOIN Shop_Product_Permutation PP ON Permutation_Id = PP.id_permutation - WHERE ISNULL(PP.id_permutation) - ) - USING ERRCODE = '22000' - ; - END IF; - - IF v_has_filter_category = TRUE OR v_has_filter_product = TRUE OR v_has_filter_permutation = TRUE THEN - INSERT INTO tmp_Shop_Product ( - id_category, - id_product, - id_permutation, - active_category, - active_product, - active_permutation, - display_order_category, - display_order_product, - display_order_permutation - -- rank_permutation, - /* - name, - description, - /* - price_GBP_VAT_incl, - price_GBP_VAT_excl, - price_GBP_min, - */ - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - id_stripe_product, - product_has_variations - */ - ) - SELECT - P.id_category, - P.id_product, - -- P.has_variations AS product_has_variations, - PP.id_permutation, - C.active AS active_category, - P.active AS active_product, - PP.active AS active_permutation, - C.display_order AS display_order_category, - P.display_order AS display_order_product, - PP.display_order AS display_order_permutation - -- RANK() OVER (ORDER BY C.display_order, P.display_order, PP.display_order) AS rank_permutation, #PARTITION BY P.id_category -- _in_category - /* - P.name, - PP.description, - /* - PP.price_GBP_VAT_incl, - PP.price_GBP_VAT_excl, - PP.price_GBP_min, - */ - PP.latency_manufacture, - PP.quantity_min, - PP.quantity_max, - PP.quantity_step, - PP.quantity_stock, - PP.is_subscription, - PP.id_unit_measurement_interval_recurrence, - PP.count_interval_recurrence, - PP.id_stripe_product, - P.has_variations - */ - FROM Shop_Product P - INNER JOIN Shop_Product_Permutation PP - ON P.id_product = PP.id_product - INNER JOIN Shop_Product_Category C - ON P.id_category = C.id_category - WHERE - -- permutations - ( - ( - NOT v_has_filter_permutation - OR FIND_IN_SET(PP.id_permutation, a_ids_permutation) > 0 - ) - AND ( - a_get_inactive_permutation - OR PP.active = TRUE - ) - ) - -- categories - AND ( - ( - NOT v_has_filter_category - OR FIND_IN_SET(P.id_category, a_ids_category) > 0 - ) - AND ( - a_get_inactive_category - OR C.active = TRUE - ) - ) - -- products - AND ( - ( - NOT v_has_filter_product - OR FIND_IN_SET(P.id_product, a_ids_product) > 0 - ) - AND ( - a_get_inactive_product - OR P.active = TRUE - ) - ) - ; - END IF; - - -- Get orders - IF v_has_filter_order AND EXISTS ( - SELECT * - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Customer_Sales_Order CSO ON Order_Id = CSO.id_order - WHERE ISNULL(CSO.id_order) - LIMIT 1 - ) THEN - RAISE EXCEPTION 'Invalid order IDs: %', ( - SELECT STRING_AGG(Order_Id, ', ') - FROM UNNEST(v_ids_order) AS Order_Id - LEFT JOIN Shop_Customer_Sales_Order CSO ON Order_Id = CSO.id_order - WHERE ISNULL(CSO.id_order) - ) - USING ERRCODE = '22000' - ; - END IF; - - INSERT INTO tmp_Shop_Customer_Sales_Order ( -- _Product_Link - id_order, - active, - rank_order - ) - SELECT - CSO.id_order, - CSO.active, - RANK() OVER (ORDER BY CSO.id_order ASC) AS rank_order - FROM Shop_Customer_Sales_Order CSO - -- LEFT JOIN Split_Temp S_T ON CSO.id_order = S_T.substring - INNER JOIN Shop_Customer_Sales_Order_Product_Link CSOPL ON CSO.id_order = CSOPL.id_order - INNER JOIN Shop_Customer S ON CSO.id_customer = S.id_customer - INNER JOIN Shop_Product_Permutation PP ON CSOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - LEFT JOIN tmp_Shop_Product t_P ON CSOPL.id_permutation = t_P.id_permutation - LEFT JOIN tmp_Shop_Customer t_S ON CSO.id_customer = t_S.id_customer - WHERE - -- customer - /* - ( - a_get_all_customer = 1 - OR NOT ISNULL(t_S.id_customer) -- CSO.id_customer IN (SELECT DISTINCT id_customer FROM tmp_Shop_Customer) - ) - */ - NOT ISNULL(t_S.id_customer) - -- order - AND ( - ( - a_get_all_order = 1 - OR ( - -- ID - -- FIND_IN_SET(CSO.id_order, a_ids_order) > 0 - -- NOT ISNULL(S_T.substring) - CSO.id_order = ANY(v_ids_order) - -- date - AND ( - ( - v_has_filter_date_from = 0 - OR CSO.created_on > a_date_from - ) - AND ( - v_has_filter_date_to = 0 - OR CSO.created_on < a_date_to - ) - ) - ) - ) - -- active - AND ( - a_get_inactive_order - OR CSO.active = TRUE - ) - ) - -- permutations - AND ( - ( - v_has_filter_category = FALSE - AND v_has_filter_product = FALSE - AND v_has_filter_permutation = 0 - ) - OR NOT ISNULL(t_P.id_permutation) -- CSO.id_permutation IN (SELECT DISTINCT id_permutation FROM tmp_Shop_Product) - ) - ; - - IF a_get_first_order_only THEN - DELETE FROM tmp_Shop_Customer_Sales_Order t_CSO - WHERE t_CSO.rank_order > ( - SELECT MIN(t_CSO.rank_order) - FROM tmp_Shop_Customer_Sales_Order t_CSO - ) - ; - END IF; - - -- Permissions - -- v_id_user := (SELECT id_user FROM Shop_User WHERE name = CURRENT_USER); - v_ids_permission_customer_purchase_order := (SELECT STRING_AGG(id_permission, ',') FROM Shop_Permission WHERE code IN ('STORE_customer', 'STORE_customer_PURCHASE_ORDER')); - -- v_ids_permutation_permission := (SELECT STRING_AGG(id_permutation, ',') FROM tmp_Shop_Product WHERE NOT ISNULL(id_permutation)); - v_ids_product_permission := (SELECT STRING_AGG(P.id_product, ',') FROM (SELECT DISTINCT id_product FROM tmp_Shop_Product WHERE NOT ISNULL(id_product)) P); - - -- SELECT v_guid, a_id_user, false, v_id_permission_product, v_id_access_level_view, v_ids_permutation_permission; - -- select * from Shop_Calc_User_Temp; - - CALL p_shop_calc_user(v_guid, a_id_user, FALSE, v_ids_permission_customer_purchase_order, v_id_access_level_view, v_ids_product_permission); - - -- select * from Shop_Calc_User_Temp; - - IF NOT EXISTS (SELECT can_view FROM Shop_Calc_User_Temp UE_T WHERE UE_T.GUID = v_guid) THEN - RAISE EXCEPTION 'You do not have view permissions for %', ( - SELECT COALESCE(STRING_AGG(name, ', '), 'NULL') - FROM Shop_Permission - WHERE id_permission = v_ids_permission_customer_purchase_order - ) - USING ERRCODE = '42501' - ; - END IF; - - - UPDATE tmp_Shop_Product t_P - SET t_P.can_view = UE_T.can_view, - t_P.can_edit = UE_T.can_edit, - t_P.can_admin = UE_T.can_admin - FROM tmp_Shop_Product t_P - INNER JOIN Shop_Calc_User_Temp UE_T - ON t_P.id_product = UE_T.id_product -- t_P.id_permutation = UE_T.id_permutation - AND UE_T.GUID = v_guid - ; - - -- CALL p_shop_clear_calc_user(v_guid); - -- DROP TABLE IF EXISTS Shop_Calc_User_Temp; - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; - - - -- select * from tmp_Shop_Customer; - -- select * from tmp_Shop_Product; - - -- Returns - -- v_now := CURRENT_TIMESTAMP; - - -- customers - OPEN result_customers FOR - SELECT - t_S.id_customer, - S.name_company, - S.name_contact, - S.department_contact, - S.id_address, - S.phone_number, - S.email, - S.id_currency, - t_S.active - FROM tmp_Shop_Customer t_S - INNER JOIN Shop_Customer S - ON t_S.id_customer = S.id_customer - ; - RETURN NEXT result_customers; - - -- Customer Sales Order - OPEN result_orders FOR - SELECT -- * - t_CSO.id_order, - CSO.id_customer, - CSO.price_total_local, - CSO.id_currency_price, - t_CSO.active - FROM Shop_Customer_Sales_Order CSO - INNER JOIN tmp_Shop_Customer_Sales_Order t_CSO ON CSO.id_order = t_CSO.id_order - ; - RETURN NEXT result_orders; - - -- Customer Sales Order Product Link - OPEN result_order_product_links FOR - SELECT - CSOPL.id_link, - CSOPL.id_order, - CSOPL.id_permutation, - P.name as name_product, - CSOPL.price_total_local, - CSOPL.id_currency_price, - CSOPL.quantity_ordered, - CSOPL.id_unit_quantity, - CSOPL.quantity_delivered, - CSOPL.latency_delivery_days, - CSOPL.display_order - FROM Shop_Customer_Sales_Order_Product_Link CSOPL - -- INNER JOIN tmp_Shop_Customer_Sales_Order_Product_Link t_CSOPL ON CSOPL.id_link = t_CSOPL.id_link - INNER JOIN tmp_Shop_Customer_Sales_Order t_CSO ON CSOPL.id_order = t_CSO.id_order - INNER JOIN Shop_Product_Permutation PP ON CSOPL.id_permutation = PP.id_permutation - INNER JOIN Shop_Product P ON PP.id_product = P.id_product - INNER JOIN Shop_Product_Category C ON P.id_category = C.id_category - ORDER BY CSOPL.id_order, C.display_order, P.display_order, PP.display_order - ; - RETURN NEXT result_order_product_links; - - -- Errors - /* - SELECT - /* - t_ME.display_order, - t_ME.guid, - t_ME.id_type, - t_ME.msg, - MET.code, - MET.name, - MET.description - */ - * - FROM tmp_Msg_Error t_ME - INNER JOIN Shop_Msg_Error_Type MET - ON t_ME.id_type = MET.id_type - WHERE guid = v_guid - ; - OPEN result_errors FOR - SELECT * - FROM tmp_Msg_Error - ; - -- RETURN NEXT result_errors; - */ - - /* - -- Return arguments for test - SELECT - a_ids_category, - a_get_inactive_category, - a_ids_product, - a_get_inactive_product, - a_get_first_product_only, - a_get_all_product, - a_ids_image, - a_get_inactive_image, - a_get_first_image_only, - a_get_all_image - ; - */ - - -- select 'other outputs'; - -- select * from tmp_Shop_Product; - - -- Clean up - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order_Product_Link; - DROP TABLE IF EXISTS tmp_Shop_Customer_Sales_Order; - DROP TABLE IF EXISTS tmp_Shop_Customer; - DROP TABLE IF EXISTS tmp_Shop_Product; - - DELETE FROM Shop_Calc_User_Temp - WHERE GUID = v_guid - ; -END; -$$ LANGUAGE plpgsql; - - -/* - - -DROP FUNCTION IF EXISTS fetch_results; - -CREATE OR REPLACE FUNCTION fetch_results() -RETURNS VOID AS $$ -DECLARE - curs refcursor; - rec record; -BEGIN - FOR curs IN SELECT p_shop_get_many_customer_sales_order ( - '', -- a_id_user - 1, -- a_get_all_customer - 0, -- a_get_inactive_customer - 0, -- a_get_first_customer_only - '', -- a_ids_customer - 1, -- a_get_all_order - 0, -- a_get_inactive_order - 0, -- a_get_first_order_only - '', -- a_ids_order - 0, -- a_get_inactive_category - '', -- a_ids_category - 0, -- a_get_inactive_product - '', -- a_ids_product - 0, -- a_get_inactive_permutation - '', -- a_ids_permutation - NULL, -- a_date_from - NULL -- a_date_to - ) LOOP - RAISE NOTICE 'Fetching from cursor: %', curs; - LOOP - FETCH curs INTO rec; - EXIT WHEN NOT FOUND; - RAISE NOTICE 'Record: %', rec; - END LOOP; - END LOOP; -END; -$$ LANGUAGE plpgsql; - -SELECT fetch_results(); - -*/ - diff --git a/static/PostgreSQL/900_populate.sql b/static/PostgreSQL/900_populate.sql deleted file mode 100644 index ec74f1c5..00000000 --- a/static/PostgreSQL/900_populate.sql +++ /dev/null @@ -1,656 +0,0 @@ - - -DO $$ -BEGIN - RAISE NOTICE 'PROCEDURE CREATION COMPLETE'; -END $$; - -/* - -CALL p_populate_database () - -*/ - -/* --- Remove previous proc -DROP PROCEDURE IF EXISTS p_populate_database; - - -DELIMITER // -CREATE OR REPLACE PROCEDURE p_populate_database () -BEGIN -*/ - - --- Access Levels -INSERT INTO Shop_Access_Level ( - display_order, code, name, priority -) -VALUES - (1, 'VIEW', 'View', 3), - (2, 'EDIT', 'Edit', 2), - (3, 'ADMIN', 'Admin', 1) -; - --- Error Message Types -INSERT INTO Shop_Msg_Error_Type ( - code, name, description -) -VALUES - ('BAD_DATA', 'Invalid data', 'Rubbish data'), - ('NO_PERMISSION', 'No permission', 'Not authorised'), - ('PRODUCT_AVAILABILITY', 'Product not available', 'Product not available') -; - --- File Types -INSERT INTO File_Type ( - code, name, extension -) -VALUES - ('JPEG', 'Joint Photographic Export Group', 'jpg'), - ('PNG', 'Portable Network Graphic', 'png'), - ('GIF', 'GIF', 'gif'), - ('MPEG-4', 'Multimedia Photographic Export Group 4', 'mp4') -; - --- Generic / shared properties -INSERT INTO Shop_General ( - quantity_max -) -VALUES ( - 10 -); - --- Categories -INSERT INTO Shop_Product_Category ( - display_order, - code, - name, - description -) -VALUES - (1, 'ASS', 'Assistive Devices', 'Braille product line and other assistive devices'), - (99, 'MISC', 'Miscellaneous', 'Not category allocated products'), - (2, 'TECH', 'Technology', 'Technological devices') -; - --- Recurrence Interval -INSERT INTO Shop_Interval_Recurrence ( - code, name, name_plural -) -VALUES - ('WEEK', 'Week', 'Weeks'), - ('MONTH', 'Month', 'Months'), - ('YEAR', 'Year', 'Years') -; - -INSERT INTO Shop_Region ( - display_order, code, name -) -VALUES - (1, 'UK', 'United Kingdom') -; - -/* -INSERT INTO Shop_Region_Branch ( - display_order, id_region_parent, id_region_child -) -VALUES - (1, 1, 2) -; -*/ - --- Currency -INSERT INTO Shop_Currency ( - display_order, code, name, symbol, factor_from_GBP -) -VALUES - (1, 'GBP', 'Great British Pound', '£', 1), - (2, 'EUR', 'Euro', '€', 1.17) -; - --- Taxes and Surcharges -INSERT INTO Shop_Tax_Or_Surcharge ( - display_order, - code, - name, - id_region_buyer, - id_region_seller, - fixed_fee, - multiplier, - apply_fixed_fee_before_multiplier, - quantity_min, - quantity_max -) -VALUES - (1, 'VAT', 'Value Added Tax', 1, 1, 0, 0.2, TRUE, 0, 1) -; - --- Products -INSERT INTO Shop_Product ( - display_order, - id_category, - name, - has_variations, - id_access_level_required -) -VALUES - ( - 1, - 1, - 'Braille Keyboard Translator', - TRUE, - 3 - ), - ( - 2, - 2, - 'Test product 1', - FALSE, - 3 - ), - ( - 3, - 3, - 'Phone', - FALSE, - 1 - ), - ( - 4, - 3, - 'Laptop', - FALSE, - 1 - ), - ( - 5, - 3, - 'Smart Watch', - FALSE, - 1 - ) -; - --- Variation Types -INSERT INTO Shop_Variation_Type ( - display_order, code, name, name_plural -) -VALUES - (1, 'COLOUR', 'Colour', 'Colours') -; - --- Variations -INSERT INTO Shop_Variation ( - display_order, id_type, code, name -) -VALUES - (1, 1, 'RED', 'Red'), - (2, 1, 'BLUE', 'Blue') -; - --- Product Permutations -INSERT INTO Shop_Product_Permutation ( - display_order, - id_product, - description, - cost_local, - id_currency_cost, - profit_local_min, - -- id_currency_profit_min, - latency_manufacture, - quantity_min, - quantity_max, - quantity_step, - quantity_stock, - is_subscription, - id_unit_measurement_interval_recurrence, - count_interval_recurrence, - -- id_access_level_required, - id_stripe_product -) -VALUES - ( - 1, - 1, - 'Good Red', - 5, - 1, - 3, - -- 1, - 14, - 1, - 3, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 2, - 1, - 'Good Blue', - 6, - 1, - 4, - -- 1, - 14, - 1, - 3, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 3, - 2, - 'Test product describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 4, - 3, - 'Phone describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 5, - 4, - 'Laptop describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ), - ( - 6, - 5, - 'Smart watch describes good', - 10, - 1, - 5, - -- 1, - 14, - 1, - 2, - 1, - 99, - FALSE, - NULL, - NULL, - -- 1, - NULL - ) -; - --- Product Permutation Variation Links -INSERT INTO Shop_Product_Permutation_Variation_Link ( - display_order, id_permutation, id_variation -) -VALUES - (1, 1, 1), - (2, 2, 2) -; - --- Product Currency Link -INSERT INTO Shop_Product_Currency_Region_Link ( - id_product, id_permutation, id_currency, id_region_purchase, price_local_VAT_incl, price_local_VAT_excl -) -VALUES - (1, 1, 1, 1, 24, 20), - (1, 1, 2, 1, 48, 40), - (1, 2, 1, 1, 96, 80), - (2, 3, 1, 1, 144, 120), - (3, 4, 1, 1, 600, 500), - (4, 5, 1, 1, 1500, 1200), - (5, 6, 1, 1, 180, 150) -; - -INSERT INTO Shop_Image_Type ( - display_order, code, name, name_plural -) -VALUES - (1, 'FULL', 'Full Quality Image', 'Full Quality Images'), - (2, 'LOW', 'Low Quality Image', 'Low Quality Images'), - (3, 'THUMBNAIL', 'Thumbnail Image', 'Thumbnail Images') -; - -INSERT INTO Shop_Image ( - display_order, id_product, id_permutation, id_type_image, id_type_file, url -) -VALUES - (1, 1, 1, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - -- (1, NULL, 1, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - (2, 1, 2, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - -- (1, NULL, 2, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg') - (3, 2, 3, 1, 1, '/static/images/prod_PB0NUOSEs06ymG.jpg'), - (4, 3, 4, 1, 1, '/static/images/prod_.jpg'), - (5, 4, 5, 1, 1, '/static/images/prod_1.jpg'), - (6, 5, 6, 1, 1, '/static/images/prod_2.jpg') -; - -INSERT INTO Shop_Delivery_Option ( - display_order, code, name, latency_delivery_min, latency_delivery_max, quantity_min, quantity_max -) -VALUES - (1, 'COLLECT', 'Collection', 0, 0, 0, 1), - (2, 'SIGNED_1', 'First Class Signed-For', 2, 4, 0, 1) -; - -INSERT INTO Shop_Product_Permutation_Delivery_Option_Link ( - display_order, id_product, id_permutation, id_delivery_option, id_region, id_currency, price_local -) -VALUES - (1, 1, 1, 1, 1, 1, 5), - (2, 1, 2, 1, 1, 1, 9), - (3, 2, NULL, 1, 1, 1, 10), - (4, 3, 4, 1, 1, 1, 10), - (5, 4, 5, 1, 1, 1, 10), - (6, 5, 6, 1, 1, 1, 10) -; - --- Discounts -INSERT INTO Shop_Discount ( - id_product, - id_permutation, - code, - name, - multiplier, - quantity_min, - quantity_max, - date_start, - date_end, - display_order -) -VALUES - (1, 1, 'CRIMBO50', 'Christmas 50% off sale!', 0.5, 3, 9, CURRENT_TIMESTAMP, '2023-12-31 23:59:59', 1), - (1, 2, 'CRIMBO50', 'Christmas 50% off sale!', 0.5, 3, 9, CURRENT_TIMESTAMP, '2023-12-31 23:59:59', 1) -; - --- Discount Delivery Region Links -INSERT INTO Shop_Discount_Region_Currency_Link ( - id_discount, - id_region, - id_currency -) -VALUES - (1, 1, 1), - (2, 1, 1), - (1, 1, 2), - (2, 1, 2) -; - --- Permission Groups -INSERT INTO Shop_Permission_Group ( - display_order, code, name -) -VALUES - (0, 'ADMIN', 'Website Admin'), - (1, 'HOME', 'Home, Contact Us, and other public information'), - (2, 'PRODUCT', 'Store Products'), - (3, 'USER', 'Store User'), - (4, 'SALES_AND_PURCHASING', 'Sales and Purchasing'), - (5, 'MANUFACTURING', 'Manufacturing') -; - --- Permissions -INSERT INTO Shop_Permission ( - display_order, code, name, id_permission_group, id_access_level_required -) -VALUES - (1, 'HOME', 'Home Page', 2, 1), - (2, 'STORE_PRODUCT', 'Store Product Page', 3, 1), - (3, 'STORE_USER', 'Store User Account Page', 4, 2), - (4, 'STORE_ADMIN', 'Store Admin Page', 1, 3), - (5, 'STORE_SUPPLIER', 'Store Supplier Page', 4, 2), - (6, 'STORE_SUPPLIER_PURCHASE_ORDER', 'Store Supplier Purchase Order Page', 4, 2), - (7, 'STORE_MANUFACTURING_PURCHASE_ORDER', 'Store Manufacturing Purchase Order Page', 5, 2), - (8, 'STORE_CUSTOMER', 'Store Customer Page', 4, 2), - (9, 'STORE_CUSTOMER_SALES_ORDER', 'Store Customer Sales Order Page', 4, 2), - (99, 'CONTACT_US', 'Contact Us Page', 2, 1) -; - --- Roles -INSERT INTO Shop_Role ( - display_order, - code, - name -) -VALUES - (1, 'DIRECTOR', 'Director'), - (2, 'USER', 'User') -; - --- Role Permission link -INSERT INTO Shop_Role_Permission_Link ( - id_role, id_permission, id_access_level -) -VALUES - (1, 1, 3), - (1, 2, 3), - (1, 3, 3), - (1, 4, 3), - (1, 5, 3), - (2, 1, 1), - (2, 2, 1), - (2, 3, 1), - (2, 4, 1), - (2, 5, 1) -; - --- Users -INSERT INTO Shop_User ( - id_user_oauth, - name, - email, - -- is_email_verified, - is_super_user -) -VALUES - ('auth0|6582b95c895d09a70ba10fef', 'Teddy', 'edward.middletonsmith@gmail.com', TRUE), - ('parts_guest', 'Guest', '', FALSE) -; - --- User Role link -INSERT INTO Shop_User_Role_Link ( - id_user, id_role -) -VALUES - (1, 1) -; - --- Addresses -INSERT INTO Shop_Address ( - -- id_user, - id_region, name_full, phone_number, postcode, address_line_1, address_line_2, city, county -) -VALUES (1, 'Edward M-S', '07375 571430', 'CV22 6DN', '53 Alfred Green Close', '', 'Rugby', 'Warwickshire') -/* -SELECT U.id_user, 1, U.name, '07375 571430', 'CV22 6DN', '53 Alfred Green Close', '', 'Rugby', 'Warwickshire' - FROM Shop_User U -*/ -; - --- User Basket -INSERT INTO Shop_User_Basket ( - id_user, - id_product, - id_permutation, - quantity -) -VALUES - (1, 1, 1, 69) -; - --- User Order Status -INSERT INTO Shop_User_Order_Status ( - display_order, code, name, name_plural -) -VALUES - (1, 'SUCCESS', 'Success', 'Successes'), - (2, 'FAIL', 'Failure', 'Failures') -; - -/* --- User Order -INSERT INTO Shop_User_Order ( - id_user, value_total, id_order_status, id_checkout_session, id_currency -) -VALUES - (1, 25, 1, 'noods', 1), - (1, 25, 1, 'noods', 1) -; - --- User Order Product Link -INSERT INTO Shop_User_Order_Product_Link ( - id_order, id_product, id_permutation, quantity -) -VALUES - (1, 1, 1, 69), - (1, 2, NULL, 69), - (1, 1, 2, 69) -; -*/ - --- Supplier -INSERT INTO Shop_Supplier ( - name_company, name_contact, department_contact, id_address, phone_number, fax, email, website, id_currency -) -VALUES - ('Precision And Research Technology Systems Limited', 'Teddy Middleton-Smith', 'Executive Management', 1, '07375571430', '', 'teddy@partsltd.co.uk', 'www.partsltd.co.uk', 1) -; - --- Unit of Measurement -INSERT INTO Shop_Unit_Measurement ( - name_singular, name_plural, symbol, is_base_unit -) -VALUES - ('metre', 'metres', 'm', TRUE), - ('kilogram', 'kilograms', 'kg', TRUE), - ('item', 'items', 'x', FALSE) -; - -/* --- Unit of Measurement Conversion -INSERT INTO Shop_Unit_Measurement_Conversion ( - id_unit_derived, id_unit_base, power_unit_base, multiplier_unit_base, increment_unit_base -) -VALUES - -; -*/ - -/* --- Supplier Purchase Order -INSERT INTO Shop_Supplier_Purchase_Order ( - id_supplier, value_total, id_order_status, id_checkout_session, id_currency -) -VALUES -; - --- Supplier Purchase Order Product Link -INSERT INTO Shop_Supplier_Purchase_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_ordered, id_unit_quantity, quantity_received, latency_delivery, display_order -) -VALUES -; -*/ - -/* --- Manufacturing Purchase Order -INSERT INTO Shop_Manufacturing_Purchase_Order ( - cost_total_local, id_currency_cost -) -VALUES -; - --- Manufacturing Purchase Order Product Link -INSERT INTO Shop_Manufacturing_Purchase_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_used, id_unit_quantity, quantity_produced, latency_manufacturing_days, display_order -) -VALUES -; -*/ - -/* --- Customer -INSERT INTO Shop_Customer ( - name_company, name_contact, department_contact, id_address, phone_number, email, id_currency -) -VALUES - -; -*/ - -/* --- Customer Sales Order -INSERT INTO Shop_Customer_Sales_Order ( - cost_total_local, id_currency_cost -) -VALUES -; - --- Customer Sales Order Product Link -INSERT INTO Shop_Customer_Sales_Order_Product_Link ( - id_order, id_permutation, cost_total_local, id_currency_cost, quantity_ordered, id_unit_quantity, quantity_delivered, latency_delivery_days, display_order -) -VALUES -; -*/ - - -/* - -- Clean up -END // -DELIMITER ; - - --- Call -CALL p_populate_database(); - --- Remove proc -DROP PROCEDURE IF EXISTS p_populate_database; -*/ \ No newline at end of file diff --git a/static/PostgreSQL/901_view.sql b/static/PostgreSQL/901_view.sql deleted file mode 100644 index 9e139d9a..00000000 --- a/static/PostgreSQL/901_view.sql +++ /dev/null @@ -1,188 +0,0 @@ - - -DO $$ -BEGIN - RAISE NOTICE 'TABLE POPULATION COMPLETE'; -END $$; - --- Product Change Sets -SELECT * FROM Shop_Product_Change_Set; - --- User Change Sets -SELECT * FROM Shop_User_Change_Set; - --- Access Levels -SELECT * FROM Shop_Access_Level; -SELECT * FROM Shop_Access_Level_Audit; - --- Error Message type -SELECT * FROM Shop_Msg_Error_Type; - --- File Types -SELECT * FROM File_Type; -SELECT * FROM File_Type_Audit; - --- Generic / shared properties -SELECT * FROM Shop_General; -SELECT * FROM Shop_General_Audit; - --- Categories -SELECT * FROM Shop_Product_Category; -SELECT * FROM Shop_Product_Category_Audit; - --- Recurrence Interval -SELECT * FROM Shop_Interval_Recurrence; -SELECT * FROM Shop_Interval_Recurrence_Audit; - --- Region -SELECT * FROM Shop_Region; -SELECT * FROM Shop_Region_Audit; - --- Region Branch -SELECT * FROM Shop_Region_Branch; -SELECT * FROM Shop_Region_Branch_Audit; - --- Currency -SELECT * FROM Shop_Currency; -SELECT * FROM Shop_Currency_Audit; - --- Taxes and Surcharges -SELECT * FROM Shop_Tax_Or_Surcharge; -SELECT * FROM Shop_Tax_Or_Surcharge_Audit; - --- Products -SELECT * FROM Shop_Product; -SELECT * FROM Shop_Product_Audit; - --- Variation Types -SELECT * FROM Shop_Variation_Type; -SELECT * FROM Shop_Variation_Type_Audit; - --- Variations -SELECT * FROM Shop_Variation; -SELECT * FROM Shop_Variation_Audit; - --- Permutations -SELECT * FROM Shop_Product_Permutation; -SELECT * FROM Shop_Product_Permutation_Audit; - --- Permutation Variation Links -SELECT * FROM Shop_Product_Permutation_Variation_Link; -SELECT * FROM Shop_Product_Permutation_Variation_Link_Audit; - --- Product Currency Links -SELECT * FROM Shop_Product_Currency_Region_Link; -SELECT * FROM Shop_Product_Currency_Region_Link_Audit; - --- Image Types -SELECT * FROM Shop_Image_Type; -SELECT * FROM Shop_Image_Type_Audit; - --- Images -SELECT * FROM Shop_Image; -SELECT * FROM Shop_Image_Audit; - --- Delivery Option Types -SELECT * FROM Shop_Delivery_Option; -SELECT * FROM Shop_Delivery_Option_Audit; - --- Delivery Options -SELECT * FROM Shop_Product_Permutation_Delivery_Option_Link; -SELECT * FROM Shop_Product_Permutation_Delivery_Option_Link_Audit; - --- Discounts -SELECT * FROM Shop_Discount; -SELECT * FROM Shop_Discount_Audit; - --- Discount Delivery Region Links -SELECT * FROM Shop_Discount_Region_Currency_Link; -SELECT * FROM Shop_Discount_Region_Currency_Link_Audit; - - --- Permission Groups -SELECT * FROM Shop_Permission_Group; -SELECT * FROM Shop_Permission_Group_Audit; - --- Permissions -SELECT * FROM Shop_Permission; -SELECT * FROM Shop_Permission_Audit; - --- Roles -SELECT * FROM Shop_Role; -SELECT * FROM Shop_Role_Audit; - --- Role Permission link -SELECT * FROM Shop_Role_Permission_Link; -SELECT * FROM Shop_Role_Permission_Link_Audit; - --- Users -SELECT * FROM Shop_User; -SELECT * FROM Shop_User_Audit; - --- User Role link -SELECT * FROM Shop_User_Role_Link; -SELECT * FROM Shop_User_Role_Link_Audit; - - --- Addresses -SELECT * FROM Shop_Address; -SELECT * FROM Shop_Address_Audit; - --- Basket -SELECT * FROM Shop_User_Basket; -SELECT * FROM Shop_User_Basket_Audit; - --- Order Statuses -SELECT * FROM Shop_User_Order_Status; -SELECT * FROM Shop_User_Order_Status_Audit; - -/* --- Orders -SELECT * FROM Shop_User_Order; -SELECT * FROM Shop_User_Order_Audit; - --- Order Products -SELECT * FROM Shop_User_Order_Product_Link; -SELECT * FROM Shop_User_Order_Product_Link_Audit; -*/ - --- Supplier -SELECT * FROM Shop_Supplier; -SELECT * FROM Shop_Supplier_Audit; - --- Unit Of Measurement -SELECT * FROM Shop_Unit_Measurement; -SELECT * FROM Shop_Unit_Measurement_Audit; - --- Unit of Measurement Conversion -SELECT * FROM Shop_Unit_Measurement_Conversion; -SELECT * FROM Shop_Unit_Measurement_Conversion_Audit; - --- Supplier Purchase Order -SELECT * FROM Shop_Supplier_Purchase_Order; -SELECT * FROM Shop_Supplier_Purchase_Order_Audit; - --- Supplier Purchase Order Product Link -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link; -SELECT * FROM Shop_Supplier_Purchase_Order_Product_Link_Audit; - --- Manufacturing Purchase Order -SELECT * FROM Shop_Manufacturing_Purchase_Order; -SELECT * FROM Shop_Manufacturing_Purchase_Order_Audit; - --- Manufacturing Purchase Order Product Link -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link; -SELECT * FROM Shop_Manufacturing_Purchase_Order_Product_Link_Audit; - --- Customers -SELECT * FROM Shop_Customer; -SELECT * FROM Shop_Customer_Audit; - --- Customer Sales Order -SELECT * FROM Shop_Customer_Sales_Order; -SELECT * FROM Shop_Customer_Sales_Order_Audit; - --- Customer Sales Order Product Link -SELECT * FROM Shop_Customer_Sales_Order_Product_Link; -SELECT * FROM Shop_Customer_Sales_Order_Product_Link_Audit; - diff --git a/static/PostgreSQL/910_anal.sql b/static/PostgreSQL/910_anal.sql deleted file mode 100644 index d83f6962..00000000 --- a/static/PostgreSQL/910_anal.sql +++ /dev/null @@ -1,26 +0,0 @@ - - - - -SELECT TABLE_NAME -FROM INFORMATION_SCHEMA.TABLES -WHERE TABLE_NAME LIKE '%SHOP%' - OR TABLE_NAME LIKE '%FILE_TYPE%'; - - -SELECT FOUND_ROWS(); - - - -SELECT - CONSTRAINT_NAME, - CONSTRAINT_TYPE, - TABLE_NAME, - COLUMN_NAME, - REFERENCED_TABLE_NAME, - REFERENCED_COLUMN_NAME -FROM - INFORMATION_SCHEMA.TABLES -WHERE - TABLE_SCHEMA = 'PARTS' - -- AND TABLE_NAME = 'your_table_name'; diff --git a/static/PostgreSQL/920_edit_permissions.sql b/static/PostgreSQL/920_edit_permissions.sql deleted file mode 100644 index 93a911d9..00000000 --- a/static/PostgreSQL/920_edit_permissions.sql +++ /dev/null @@ -1,83 +0,0 @@ -SELECT URL.id_link, - URL.id_user, - U.name AS name, - URL.id_role, - R.name AS role -FROM Shop_User_Role_Link URL -INNER JOIN Shop_User U - ON URL.id_user = U.id_user -INNER JOIN Shop_Role R - ON URL.id_role = R.id_role -; -SELECT * -FROM Shop_Role_Permission_Link -; -SELECT * -FROM Shop_Access_Level -; -SELECT * -FROM Shop_Permission -; -SELECT * -FROM Shop_Access_Level -; - - -select * from shop_user; -select * from shop_user_audit; -select * from Shop_User_Change_Set; -/* -INSERT INTO Shop_User_Change_Set ( comment ) -VALUES ('Demotion'); -*/ -UPDATE Shop_User -SET is_super_user = 0, - id_change_set = (SELECT id_change_set FROM Shop_User_Change_Set LIMIT 1) -WHERE id_user = 1 -; -select * from shop_user; -select * from shop_user_audit; - - -drop procedure if exists p_test; -delimiter // -create procedure p_test () -begin - declare b0 int; - declare b1 int; - SET b0 = FALSE; - SET b1 = TRUE; - select b0, b1; - select cast(b0 as char), cast(b1 as char); - select cast(b0 as char character set utf8), cast(b1 as char character set utf8); - select convert(b0, char), convert(b1, char); - select convert(b0, char character set utf8), convert(b1, char character set utf8); - select convert(convert(b0, signed), char), convert(convert(b1, signed), char); - select convert(convert(b0, signed), char character set utf8), convert(convert(b1, signed), char character set utf8); -end // -delimiter ; -call p_test(); -drop procedure if exists p_test; - -INSERT INTO Shop_User_Audit ( - id_user, - name_field, - value_prev, - value_new, - id_change_set -) -SELECT id_user, name_field, value_prev, value_new, id_change_set -FROM Shop_User_Audit -WHERE id_audit = 1 -UNION -SELECT id_user, name_field, value_prev, value_new, id_change_set -FROM Shop_User_Audit -WHERE id_audit = 1 -; - -select * from shop_user_audit; - - -SELECT * FROM Shop_Access_Level; - -SELECT * FROM Shop_Product; \ No newline at end of file diff --git a/static/PostgreSQL/temp.txt b/static/PostgreSQL/temp.txt deleted file mode 100644 index b87bbca4..00000000 --- a/static/PostgreSQL/temp.txt +++ /dev/null @@ -1,153 +0,0 @@ -001_destroy.sql -100.0_tbl_Shop_Product_Change_Set.sql -100.1_tbl_Shop_User_Change_Set.sql -100.2_tbl_Shop_Access_Level.sql -100.2_tbl_Shop_Sales_And_Purchasing_Change_Set.sql -100.3_tbl_Shop_Access_Level_Audit.sql -100_tbl_Msg_Error_Type.sql -102_tbl_File_Type.sql -103_tbl_File_Type_Audit.sql -104_tbl_Shop_General.sql -105_tbl_Shop_General_Audit.sql -106_tbl_Shop_Product_Category.sql -107_tbl_Shop_Product_Category_Audit.sql -108_tbl_Shop_Interval_Recurrence.sql -109_tbl_Shop_Interval_Recurrence_Audit.sql -110.0_tbl_Shop_Region.sql -110.1_tbl_Shop_Region_Audit.sql -110.2_tbl_Shop_Region_Branch.sql -110.3_tbl_Shop_Region_Branch_Audit.sql -110.4_tbl_Shop_Currency.sql -110.5_tbl_Shop_Currency_Audit.sql -110.6_tbl_Shop_Tax_Or_Surcharge.sql -110.7_tbl_Shop_Tax_Or_Surcharge_Audit.sql -110.8_tbl_Shop_Product.sql -110.9_tbl_Shop_Product_Audit.sql -112_tbl_Shop_Variation_Type.sql -113.0_tbl_Shop_Variation_Type_Audit.sql -114_tbl_Shop_Variation.sql -115_tbl_Shop_Variation_Audit.sql -117.1_tbl_Shop_Product_Permutation.sql -117.2_tbl_Shop_Product_Permutation_Audit.sql -117.3_tbl_Shop_Product_Permutation_Variation_Link.sql -117.4_tbl_Shop_Product_Permutation_Variation_Link_Audit.sql -117.5_tbl_Shop_Product_Currency_Region_Link.sql -117.6_tbl_Shop_Product_Currency_Region_Link_Audit.sql -118_tbl_Shop_Image_Type.sql -119_tbl_Shop_Image_Type_Audit.sql -120_tbl_Shop_Image.sql -121_tbl_Shop_Image_Audit.sql -122_tbl_Shop_Delivery_Option.sql -123_tbl_Shop_Delivery_Option_Audit.sql -124_tbl_Shop_Product_Permutation_Delivery_Option_Link.sql -125_tbl_Shop_Product_Permutation_Delivery_Option_Link_Audit.sql -130.4_tbl_Shop_Discount.sql -131_tbl_Shop_Discount_Audit.sql -132_tbl_Shop_Discount_Region_Currency_Link.sql -133_tbl_Shop_Discount_Region_Currency_Link_Audit.sql -153_tbl_Shop_Permission_Group.sql -154_tbl_Shop_Permission_Group_Audit.sql -155_tbl_Shop_Permission.sql -156_tbl_Shop_Permission_Audit.sql -157_tbl_Shop_Role.sql -158_tbl_Shop_Role_Audit.sql -159_tbl_Shop_Role_Permission_Link.sql -160_tbl_Shop_Role_Permission_Link_Audit.sql -161_tbl_Shop_User.sql -162_tbl_Shop_User_Audit.sql -163_tbl_Shop_User_Role_Link.sql -164_tbl_Shop_User_Role_Link_Audit.sql -165_tbl_Shop_Address.sql -166_tbl_Shop_Address_Audit.sql -167_tbl_Shop_User_Basket.sql -168_tbl_Shop_User_Basket_Audit.sql -169_tbl_Shop_User_Order_Status.sql -170_tbl_Shop_User_Order_Status_Audit.sql -181.0_tbl_Shop_Supplier.sql -181.1_tbl_Shop_Supplier_Audit.sql -181.2_tbl_Shop_Unit_Measurement.sql -181.3_tbl_Shop_Unit_Measurement_Audit.sql -181.4_tbl_Shop_Unit_Measurement_Conversion.sql -181.5_tbl_Shop_Unit_Measurement_Conversion_Audit.sql -181.6_tbl_Shop_Supplier_Purchase_Order.sql -181.7_tbl_Shop_Supplier_Purchase_Order_Audit.sql -181.8_tbl_Shop_Supplier_Purchase_Order_Product_Link.sql -181.9_tbl_Shop_Supplier_Purchase_Order_Product_Link_Audit.sql -182.0_tbl_Shop_Supplier_Purchase_Order_Product_Link_Temp.sql -183_tbl_Shop_Manufacturing_Purchase_Order.sql -184_tbl_Shop_Manufacturing_Purchase_Order_Audit.sql -185_tbl_Shop_Manufacturing_Purchase_Order_Product_Link.sql -186.1_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Temp.sql -186_tbl_Shop_Manufacturing_Purchase_Order_Product_Link_Audit.sql -187.0_tbl_Shop_Customer.sql -187.1_tbl_Shop_Customer_Audit.sql -187.2_tbl_Shop_Customer_Sales_Order.sql -188_tbl_Shop_Customer_Sales_Order_Audit.sql -189_tbl_Shop_Customer_Sales_Order_Product_Link.sql -190_tbl_Shop_Customer_Sales_Order_Product_Link_Audit.sql -191_tbl_Shop_Customer_Sales_Order_Product_Link_Temp.sql -300.2_tri_Shop_Sales_And_Purchasing_Change_Set.sql -301.1_tri_Shop_User_Change_Set.sql -301.2_tri_Shop_Access_Level.sql -301_tri_Shop_Product_Change_Set.sql -302_tri_File_Type.sql -303_tri_File_Type_Audit.sql -304_tri_Shop_General.sql -306_tri_Shop_Product_Category.sql -308_tri_Shop_Interval_Recurrence.sql -310.0_tri_Shop_Region.sql -310.2_tri_Shop_Region_Branch.sql -310.4_tri_Shop_Currency.sql -310.6_tri_Shop_Tax_Or_Surcharge.sql -310.8_tri_Shop_Product.sql -312_tri_Shop_Variation_Type.sql -314_tri_Shop_Variation.sql -317.1_tri_Shop_Product_Permutation.sql -317.3_tri_Shop_Product_Permutation_Variation_Link.sql -317.5_tri_Shop_Product_Currency_Region_Link.sql -318_tri_Shop_Image_Type.sql -320_tri_Shop_Image.sql -322_tri_Shop_Delivery_Option.sql -324_tri_Shop_Product_Permutation_Delivery_Option_Link.sql -330_tri_Shop_Discount.sql -332_tri_Shop_Discount_Region_Currency_Link.sql -353_tri_Shop_Permission_Group.sql -355_tri_Shop_Permission.sql -357_tri_Shop_Role.sql -359_tri_Shop_Role_Permission_Link.sql -361_tri_Shop_User.sql -363_tri_Shop_User_Role_Link.sql -365_tri_Shop_Address.sql -367_tri_Shop_User_Basket.sql -369_tri_Shop_User_Order_Status.sql -381.0_tri_Shop_Supplier.sql -381.2_tri_Shop_Unit_Measurement.sql -381.4_tri_Shop_Unit_Of_Measurement_Conversion.sql -381.6_tri_Shop_Supplier_Purchase_Order.sql -381.8_tri_Shop_Supplier_Purchase_Order_Product_Link.sql -383_tri_Shop_Manufacturing_Purchase_Order.sql -385_tri_Shop_Manufacturing_Purchase_Order_Product_Link.sql -387.0_tri_Shop_Customer.sql -387.2_tri_Shop_Customer_Sales_Order.sql -389_tri_Shop_Customer_Sales_Order_Product_Link.sql -600_p_shop_calc_user.sql -602_p_save_supplier_purchase_order.sql -602_p_shop_save_supplier.sql -604_p_shop_save_manufacturing_purchase_order.sql -605_p_shop_save_customer.sql -606_p_shop_save_customer_sales_order.sql -610_p_shop_save_user.sql -611_p_shop_save_user_basket.sql -700_p_shop_get_many_product.sql -702.1_p_shop_get_many_currency.sql -702.2_p_shop_get_many_region.sql -703_p_shop_get_many_user_order.sql -704_p_shop_get_many_stripe_product_new.sql -705_p_shop_get_many_stripe_price_new.sql -706_p_shop_get_many_supplier.sql -706_p_shop_get_many_supplier_purchase_order.sql -708_p_shop_get_many_manufacturing_purchase_order.sql -709_p_shop_get_many_customer.sql -710_p_shop_get_many_customer_sales_order.sql -900_populate.sql -901_view.sql diff --git a/static/js/pages/base.js b/static/js/pages/base.js index c8b6097f..ac6b83b2 100644 --- a/static/js/pages/base.js +++ b/static/js/pages/base.js @@ -49,14 +49,6 @@ export default class BasePage { } hookupNavigation() { - /* Can be removed: */ - let overlayHamburger = document.querySelector(idOverlayHamburger); - let hamburgerOptions = overlayHamburger.querySelectorAll('div.' + flagRow); - let countOptions = hamburgerOptions.length; - // console.log('count nav options: ', countOptions); - // overlayHamburger.css('height', (countOptions * 27) + 'px'); - /* end of can be removed */ - this.hookupEventHandler("click", idButtonHamburger, (event, element) => { let overlayHamburger = document.querySelector(idOverlayHamburger); if (overlayHamburger.classList.contains(flagCollapsed)) { @@ -66,7 +58,6 @@ export default class BasePage { overlayHamburger.classList.remove(flagExpanded); overlayHamburger.classList.add(flagCollapsed); } - // overlayHamburger.classList.add(flagInitialised); }); this.hookupButtonsNavHome(); @@ -118,7 +109,6 @@ export default class BasePage { API.loginUser() .then((response) => { if (response.Success) { - // this.router.navigateToUrl(response[flagCallback], null, false); // window.app. window.location.href = response[flagCallback]; } else { DOM.alertError("Error", response.Message); @@ -186,7 +176,6 @@ export default class BasePage { if (_verbose) { console.log('saving page: ', this.title); } OverlayConfirm.show(); }); - // button.classList.add(flagCollapsed); }); } @@ -210,7 +199,7 @@ export default class BasePage { return LocalStorage.getLocalStorage(this.hash); } - toggleShowButtonsSaveCancel(show) { // , buttonSave = null, buttonCancel = null + toggleShowButtonsSaveCancel(show) { let buttonSave = document.querySelector('form.' + flagFilter + ' button.' + flagSave); let buttonCancel = document.querySelector('form.' + flagFilter + ' button.' + flagCancel); if (show) {