Am Dienstag, 22. März 2016 13:05:04 UTC+1 schrieb TPH:
>
> Hello,
>
> I just wrote this question in the German group, but I think here are much
> more people. Ill will keep both articles updated.
>
> What is the best way to get product stock directly from database?
>
> For a webshop / ebay sync I need the stock for every product of the
> warehouse. I have tried this with proteus, but this is not fast enough.
>
> So I am searching for an way, to get the actually stock of product on the
> warehouse.
>
> I think I have to use "stock_location" and "stock_move". Maybe someone
> have already a snipped or can give me some hints.
>
> Thank you very much.
>
>
This is now my first draft to get the stock directly from database, first
only for an specific product. Optimization tips are welcome. I don't have
sub-locations so I don't have to search for other locations then parent =
warehouse.
product_id = 7
warehouse_id = 22
stock = 0
# get all locations with parent warehouse_id
cursor.execute("SELECT * FROM stock_location WHERE parent = '%s'",
[warehouse_id])
records = cursor.fetchall()
location_ids = [r["id"] for r in records]
# get all moves from or to warehouse
cursor.execute("SELECT * FROM stock_move WHERE product = %s AND state !=
'draft' AND (from_location = ANY(%s) OR to_location = ANY(%s))", (
product_id,
location_ids,
location_ids
))
records = cursor.fetchall()
# calculate stock
for r in records:
if r["to_location"] in location_ids:
stock + r["internal_quantity"]
if r["from_location"] in location_ids:
stock - r["internal_quantity"]
print "stock:", stock
regards
--
You received this message because you are subscribed to the Google Groups
"tryton" group.
To view this discussion on the web visit
https://groups.google.com/d/msgid/tryton/2c797be7-2ae6-47e2-8e4f-6765d213af0b%40googlegroups.com.