# -*- coding: utf-8 -*-
"""
    sale.py

    :copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
    :license: BSD, see LICENSE for more details.
"""
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.transaction import Transaction
from trytond.pyson import Eval, Or

__metaclass__ = PoolMeta
__all__ = ['SaleLine']


class SaleLine:
    __name__ = 'sale.line'

    available_stock_qty = fields.Function(
        fields.Float(
            'Available Quantity',
            digits=(16, Eval('unit_digits', 2)),
            states={
                'invisible': Or(
                    Eval('type') != 'line',
                    Eval('sale_state') == 'done'
                ),
            },
            depends=['type', 'unit_digits', 'sale_state']
        ),
        'on_change_with_available_stock_qty'
    )

    sale_state = fields.Function(
        fields.Char('Sale State'), 'get_sale_state'
    )

    @fields.depends('_parent_sale.warehouse', 'product', 'type')
    def on_change_with_available_stock_qty(self, name=None):
        """
        Returns the available stock to process a Sale
        """
        Location = Pool().get('stock.location')
        Date = Pool().get('ir.date')

        try:
            self.warehouse
        except AttributeError:
            # On change will not have the warehouse set since its constructed
            # from a dictionary of changes rather than the database active
            # record. The warehouse on line is never displayed on view and
            # won't be there.
            self.warehouse = Location(self.get_warehouse(None))

        if self.type == 'line' and self.product and self.warehouse:
            with Transaction().set_context(
                locations=[self.warehouse.id],  # warehouse of the line
                stock_skip_warehouse=True,
                stock_date_end=Date.today(),
                stock_assign=True
            ):
                return self.product.quantity

    def get_sale_state(self, name):
        """
        Returns the state of the Sale
        """
        return self.sale and self.sale.state

@@ -972,16 +972,25 @@
 depends(
+%0A        
 '_parent
@@ -1006,16 +1006,50 @@
 ehouse',
+ '_parent_sale.sale_date',%0A       
  'produc
@@ -1679,16 +1679,373 @@
 None))%0A%0A
+        # If a date is specified on sale, use that. If not, use the%0A        # current date.%0A        date = self.sale.sale_date or Date.today()%0A%0A        # If the sales person is taking an order for a date in the past%0A        # (which tryton allows), the stock cannot be of the past, but of%0A        # the current date.%0A        date = max(date, Date.today())%0A%0A
         
@@ -2164,16 +2164,20 @@
         
+    
 location
@@ -2236,24 +2236,28 @@
             
+    
 stock_skip_w
@@ -2270,17 +2270,53 @@
 se=True,
-%0A
+      # quantity of storage only%0A    
         
@@ -2342,22 +2342,53 @@
 end=
-Date.today(),%0A
+date,            # Stock as of sale date%0A    
     
@@ -2416,17 +2416,19 @@
 ign=True
-%0A
+): 
         
@@ -2431,18 +2431,34 @@
         
-):
+# Exclude Assigned
 %0A       

Consider sale date to compute quantity