Last active
September 26, 2018 20:54
-
-
Save sharoonthomas/e31acf86ffea10ed2eb6aa8dfe2b5446 to your computer and use it in GitHub Desktop.
Update the supplier prices over API by reading a CSV file with sku, supplier_name and price
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import csv | |
import sys | |
from decimal import Decimal | |
from fulfil_client import Client | |
client = Client( | |
os.environ.get('FULFIL_SUBDOMAIN'), | |
os.environ.get('FULFIL_API_KEY'), | |
) | |
Product = client.model('product.product') | |
Contact = client.model('party.party') | |
Supplier = client.model('purchase.product_supplier') | |
SupplierPrice = client.model('purchase.product_supplier.price') | |
def create_supplier_price(sku, supplier_name, price): | |
"""Adds supplier price for product | |
""" | |
products = Product.search_read( | |
['code', 'ilike', '%%%s%%' % sku], | |
None, None, None, None | |
) | |
if not products: | |
print "ERROR: Couldn't find product with sku %s" % sku | |
return | |
contacts = Contact.search_read( | |
['name', 'ilike', '%%%s%%' % supplier_name], | |
None, None, None, None | |
) | |
if not contacts: | |
print "ERROR: Couldn't find contact with name %s" % supplier_name | |
return | |
product = products[0] | |
contact = contacts[0] | |
suppliers = Supplier.search_read([ | |
('product', '=', product['id']), | |
('party', '=', contact['id']), | |
]) | |
if not suppliers: | |
print "ERROR: Couldn't find supplier for product %s" % sku | |
return | |
supplier = suppliers[0] | |
if SupplierPrice.search(['product_supplier', '=', supplier['id']]): | |
print "INFO: Price already found" | |
return | |
supplier_prices = SupplierPrice.create([{ | |
'product_supplier': supplier['id'], | |
'unit_price': Decimal(str(price)), | |
}]) | |
print "CREATED: Supplier price with id %s" % supplier_prices[0] | |
return supplier_prices[0] | |
if __name__ == '__main__': | |
with open(sys.argv[1]) as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
create_supplier_price( | |
row['sku'], | |
row['supplier_name'], | |
row["price"], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment