from __future__ import unicode_literals
from __future__ import division

from django.db.models import Sum

from ...utils import money_to_default
from ..models import Stock


def get_stock_quantity(product, location_type=None, **kwargs):
    qs = Stock.objects.filter(product=product, **kwargs)

    if location_type:
        qs = qs.filter(location__type=location_type)

    result = qs.aggregate(Sum("quantity"))

    return result["quantity__sum"] or 0


def get_stock_price(product, location_type=None, **kwargs):
    qs = Stock.objects.filter(product=product, **kwargs)

    if location_type:
        qs = qs.filter(location__type=location_type)

    stocks = qs.values("unit_price", "quantity")

    if len(stocks) > 0:
        # compute a weighted arithmetic mean or standard mean if weights sum is 0
        weights = sum(map(lambda s: s["quantity"], stocks))
        if weights != 0:
            value = sum(map(lambda s: s["unit_price"] * s["quantity"], stocks)) / weights
        else:
            value = sum(map(lambda s: s["unit_price"], stocks)) / len(stocks)
    else:
        value = 0

    return money_to_default(value)

@@ -66,16 +66,36 @@
 vision%0A%0A
+import collections%0A%0A
 from dja
@@ -184,16 +184,336 @@
 rt Stock
+, Location%0A%0A%0A__all__ = %5B%0A    %22get_stock_quantity%22, %22get_stock_price%22, %22get_storage_quantity%22, %22get_storage_price%22,%0A    %22get_customer_price%22, %22get_customer_quantity%22, %22get_output_price%22, %22get_output_quantity%22,%0A    %22get_lostandfound_price%22, %22get_lostandfound_quantity%22, %22get_supplier_price%22,%0A    %22get_supplier_quantity%22,%0A%5D
 %0A%0A%0Adef g
@@ -642,32 +642,171 @@
  location_type:%0A
+        if isinstance(location_type, collections.Sequence):%0A            qs = qs.filter(location__type__in=location_type)%0A        else:%0A    
         qs = qs.
@@ -1061,32 +1061,171 @@
  location_type:%0A
+        if isinstance(location_type, collections.Sequence):%0A            qs = qs.filter(location__type__in=location_type)%0A        else:%0A    
         qs = qs.
@@ -1749,8 +1749,1236 @@
 (value)%0A
+%0A%0Adef get_supplier_quantity(product, **kwargs):%0A    return get_stock_quantity(product, Location.LOCATION_SUPPLIER, **kwargs)%0A%0A%0Adef get_supplier_price(product, **kwargs):%0A    return get_stock_price(product, Location.LOCATION_SUPPLIER, **kwargs)%0A%0A%0Adef get_storage_quantity(product, **kwargs):%0A    return get_stock_quantity(product, Location.LOCATION_STORAGE, **kwargs)%0A%0A%0Adef get_storage_price(product, **kwargs):%0A    return get_stock_price(product, Location.LOCATION_STORAGE, **kwargs)%0A%0A%0Adef get_output_quantity(product, **kwargs):%0A    return get_stock_quantity(product, Location.LOCATION_OUTPUT, **kwargs)%0A%0A%0Adef get_output_price(product, **kwargs):%0A    return get_stock_price(product, Location.LOCATION_OUTPUT, **kwargs)%0A%0A%0Adef get_customer_quantity(product, **kwargs):%0A    return get_stock_quantity(product, Location.LOCATION_CUSTOMER, **kwargs)%0A%0A%0Adef get_customer_price(product, **kwargs):%0A    return get_stock_price(product, Location.LOCATION_CUSTOMER, **kwargs)%0A%0A%0Adef get_lostandfound_quantity(product, **kwargs):%0A    return get_stock_quantity(product, Location.LOCATION_LOST_AND_FOUND, **kwargs)%0A%0A%0Adef get_lostandfound_price(product, **kwargs):%0A    return get_stock_price(product, Location.LOCATION_LOST_AND_FOUND, **kwargs)%0A

Add api to get quantity and price for each location type