Skip to content

Instantly share code, notes, and snippets.

@krabello
Last active April 26, 2021 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krabello/c7131cfd9a49983d8c94f67ec7e5e335 to your computer and use it in GitHub Desktop.
Save krabello/c7131cfd9a49983d8c94f67ec7e5e335 to your computer and use it in GitHub Desktop.
Generates SQL for Prestashop Special Pricing import
###
Usage: python3 ./price_conversion.py <DATA-FILE> > <EXPORT>.sql
###
import csv, math, re, sys
id_field_name = "ID"
regular_price_field_name = "Price tax excluded or Price tax included"
approved_customer_price_field_name = "Specific Price"
def generate_values(id_product, discount_price):
return "(0,0,"+str(id_product)+",1,0,0,0,5,0,0,"+str(discount_price)+",1,0.000000,1,'amount','2020-01-01 00:00:00','2050-01-01 00:00:00'),"
def generate_discount_price(regular_price, approved_customer_price):
return format(regular_price - approved_customer_price, '.1f')
def check_for_duplicates(reader):
id_numbers = []
duplicate_list = []
seen = {}
for row in reader:
for (field_name,field_value) in row.items():
if field_name == id_field_name:
id_numbers.append(field_value)
for id in id_numbers:
if id not in seen:
seen[id] = 1
else:
if seen[id] == 1:
duplicate_list.append(id)
seen[id] += 1
if len(duplicate_list) > 0:
errors.append("Duplicate items Found: "+",".join(duplicate_list))
def field_validated(field_name, field_value, line_number):
"""Checks to make sure value is not empty"""
if str(field_value) == "":
errors.append("Error: Could not find an '"+str(field_name)+"' at line "+ str(line_number))
return False
else:
return True
def generateSQL(data):
"""Generates the sql"""
table_name = 'ps_specific_price'
insert_columns = 'INSERT INTO `'+table_name+'` (`id_specific_price_rule`,`id_cart`,`id_product`,`id_shop`,`id_shop_group`,`id_currency`,`id_country`,`id_group`,`id_customer`,`id_product_attribute`,`price`,`from_quantity`,`reduction`,`reduction_tax`,`reduction_type`,`from`,`to`) VALUES'
# Replace the trailing comma and append the ending semicolon
data = str(data[:-1]+";")
return insert_columns+data
file_name = sys.argv[1]
try:
data = ''
errors = []
line_number = 1
with open(file_name) as f:
reader = csv.DictReader(f)
check_for_duplicates(reader)
for row in reader:
for (field_name,field_value) in row.items():
if (field_name == id_field_name):
if field_validated(field_name,field_value,line_number):
id_product = field_value
if (field_name == approved_customer_price_field_name):
if field_validated(field_name,field_value,line_number):
approved_customer_price = float(field_value)
if (field_name == regular_price_field_name):
if field_validated(field_name,field_value,line_number):
regular_price = float(field_value)
line_number += 1
discount_price = generate_discount_price(regular_price, approved_customer_price)
data += generate_values(id_product, discount_price)
except:
errors.append('File not found')
if (len(errors)):
for error in errors:
print(error)
exit()
print(generateSQL(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment