Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created November 27, 2017 17:51
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 sharoonthomas/fbd366df1452d0f2d2567e0eb1de4d3c to your computer and use it in GitHub Desktop.
Save sharoonthomas/fbd366df1452d0f2d2567e0eb1de4d3c to your computer and use it in GitHub Desktop.
This example shows how to fetch data for inventory-planner.com from Fulfil. The code is written to be compatible with any kitchensinks.
import unicodecsv as csv
from kitchensink.extensions import fulfil
Product = fulfil.model('product.product')
SaleLine = fulfil.model('sale.line')
PurchaseLine = fulfil.model('purchase.line')
def format_product(product):
return {
'product_id': product['id'],
'title': product['variant_name'],
'price': product['list_price'],
'SKU': product['code'],
'created_at': product['create_date'].date(),
'stock_quantity': product['quantity_on_hand'],
}
def get_products():
products = Product.search_read_all(
[],
None,
[
'id',
'variant_name',
'list_price',
'code',
'create_date',
'quantity_on_hand',
],
context={'locations': [4]}
)
with open('./files/products.tsv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=[
'product_id', 'title', 'price', 'SKU',
'created_at',
'stock_quantity'
], dialect='excel-tab')
writer.writeheader()
writer.writerows(map(format_product, products))
def format_order_line(line):
return {
'order_number': line['sale.reference'],
'product_id': line['product'],
'SKU': line['product.code'],
'date': line['shipping_date'],
'price': line['unit_price'],
'quantity': line['quantity'] - (line['quantity_canceled'] or 0.0) - (line['quantity_shipped'] or 0.0),
}
def get_orders():
lines = SaleLine.search_read_all(
[
('sale.state', '=', 'processing'),
('quantity', '>', 0.0),
],
None,
[
'sale.reference',
'product',
'product.code',
'shipping_date',
'quantity',
'unit_price',
'quantity_canceled',
'quantity_shipped',
]
)
with open('./files/orders.tsv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=[
'order_number',
'product_id',
'SKU',
'date',
'price',
'quantity',
'quantity_canceled',
'quantity_shipped',
], dialect='excel-tab')
writer.writeheader()
writer.writerows(map(format_order_line, lines))
def format_po_line(line):
return {
'po_id': line['purchase'],
'po_name': line['purchase.number'],
'vendor': line['purchase.party.name'],
'date': line['purchase.purchase_date'],
'expected_date': line['delivery_date'],
'product_id': line['product'],
'SKU': line['product.code'],
'cost_price': line['unit_price'],
'status': 'partial' if line['quantity_received'] else 'open',
'ordered': line['quantity'],
'received': line['quantity_received'],
}
def get_purchases():
lines = PurchaseLine.search_read_all(
[
('purchase.state', '=', 'processing'),
('quantity', '>', 0.0),
],
None,
[
'purchase',
'purchase.number',
'purchase.party.name',
'purchase.purchase_date',
'quantity',
'quantity_received',
'product',
'product.code',
'unit_price',
'purchase.state',
'delivery_date',
]
)
with open('./files/purchase.tsv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=[
'po_id',
'po_name',
'vendor',
'date',
'expected_date',
'product_id',
'SKU',
'cost_price',
'status',
'ordered',
'received',
], dialect='excel-tab')
writer.writeheader()
writer.writerows(map(format_po_line, lines))
if __name__ == '__main__':
get_products()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment