Add average cost and amount cost
This commit is contained in:
parent
5bf13d44d0
commit
71cf4d225e
|
@ -13,6 +13,7 @@ def register():
|
|||
Pool.register(
|
||||
product.Product,
|
||||
product.Template,
|
||||
product.AverageCost,
|
||||
position.ProductPosition,
|
||||
position.ProductTemplatePosition,
|
||||
stock.Move,
|
||||
|
|
50
product.py
50
product.py
|
@ -1,7 +1,8 @@
|
|||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
from datetime import date, timedelta
|
||||
from trytond.model import fields
|
||||
|
||||
from trytond.model import fields, ModelSQL, ModelView
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
from trytond.pyson import Eval
|
||||
from trytond.transaction import Transaction
|
||||
|
@ -13,7 +14,7 @@ TIME_HISTORY = 90 # Days
|
|||
|
||||
class Product(metaclass=PoolMeta):
|
||||
__name__ = 'product.product'
|
||||
|
||||
|
||||
stock_duration = fields.Function(fields.Integer('Stock Duration',
|
||||
states=STATES), 'get_stock_duration')
|
||||
stock_level = fields.Function(fields.Selection([
|
||||
|
@ -22,11 +23,37 @@ class Product(metaclass=PoolMeta):
|
|||
('exhausted', 'Exhausted'),
|
||||
('normal', 'Normal'),
|
||||
], 'Stock Level', states=STATES), 'get_stock_level')
|
||||
avg_cost_price = fields.Function(fields.Numeric("Average Cost Price",
|
||||
required=True, digits=(16, 4)), 'get_avg_cost_price')
|
||||
amount_cost = fields.Function(fields.Numeric("Amiunt Cost",
|
||||
required=True, digits=(16, 4)), 'get_amount_cost')
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(Product, cls).__setup__()
|
||||
|
||||
def get_amount_cost(self, name=None):
|
||||
if self.avg_cost_price and self.quantity:
|
||||
return float(self.avg_cost_price) * self.quantity
|
||||
return
|
||||
|
||||
def get_avg_cost_price(self, name=None):
|
||||
target_date = date.today()
|
||||
stock_date_end = Transaction().context.get('stock_date_end')
|
||||
if stock_date_end:
|
||||
target_date = stock_date_end
|
||||
|
||||
AverageCost = Pool().get('product.average_cost')
|
||||
avg_product = AverageCost.search([
|
||||
('product', '=', self.id),
|
||||
('effective_date', '<=', target_date),
|
||||
], order=[('effective_date', 'DESC')], limit=1)
|
||||
print(' product >>>> ', avg_product)
|
||||
if avg_product:
|
||||
return avg_product[0].cost_price
|
||||
else:
|
||||
return self.cost_price
|
||||
|
||||
def get_stock_duration(self, name):
|
||||
res = None
|
||||
if Transaction().context.get('stock_date_end'):
|
||||
|
@ -72,7 +99,6 @@ class Product(metaclass=PoolMeta):
|
|||
|
||||
class Template(metaclass=PoolMeta):
|
||||
__name__ = 'product.template'
|
||||
|
||||
positions = fields.One2Many('product_template.position','template', 'Positions')
|
||||
|
||||
@classmethod
|
||||
|
@ -82,4 +108,20 @@ class Template(metaclass=PoolMeta):
|
|||
else:
|
||||
default = default.copy()
|
||||
default.setdefault('positions', None)
|
||||
return super(Template, cls).copy(records, default=default)
|
||||
return super(Template, cls).copy(records, default=default)
|
||||
|
||||
|
||||
class AverageCost(ModelSQL, ModelView):
|
||||
"Average Cost"
|
||||
__name__ = "product.average_cost"
|
||||
product = fields.Many2One('product.product', 'Product',
|
||||
required=True, domain=[('template.type', '=', 'goods')]
|
||||
)
|
||||
effective_date = fields.Date('Effective Date', required=True)
|
||||
cost_price = fields.Numeric("Cost Price", required=True, digits=(16, 4))
|
||||
stock_move = fields.Many2One('stock.move', 'Move')
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(AverageCost, cls).__setup__()
|
||||
cls._order.insert(0, ('effective_date', 'DESC'))
|
||||
|
|
34
stock.py
34
stock.py
|
@ -79,6 +79,23 @@ class Move(metaclass=PoolMeta):
|
|||
res = description.rstrip('\n')
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def do(cls, moves):
|
||||
super(Move, cls).do(moves)
|
||||
for move in moves:
|
||||
if move.origin.__name__ in ('purchase.line', 'stock.inventory.line'):
|
||||
move.set_average_cost()
|
||||
|
||||
def set_average_cost(self):
|
||||
AverageCost = Pool().get('product.average_cost')
|
||||
data = {
|
||||
"stock_move": self.id,
|
||||
"product": self.product.id,
|
||||
"effective_date": self.effective_date,
|
||||
"cost_price": self.product.cost_price,
|
||||
}
|
||||
AverageCost.create([data])
|
||||
|
||||
|
||||
class MoveByProductStart(ModelView):
|
||||
'Move By Product Start'
|
||||
|
@ -359,7 +376,6 @@ class WarehouseReport(Report):
|
|||
Product = pool.get('product.product')
|
||||
OrderPoint = pool.get('stock.order_point')
|
||||
Location = pool.get('stock.location')
|
||||
# ProductsSupplier = pool.get('purchase.product_supplier')
|
||||
ids_location = data['locations']
|
||||
locations = Location.browse(data['locations'])
|
||||
dom_products = [
|
||||
|
@ -381,7 +397,6 @@ class WarehouseReport(Report):
|
|||
('categories', 'in', [data['category']]),
|
||||
],
|
||||
]])
|
||||
# dom_products.append(('account_category', '=', data['category']))
|
||||
|
||||
if not data['zero_quantity']:
|
||||
dom_products.append([('quantity', '!=', 0)])
|
||||
|
@ -406,26 +421,22 @@ class WarehouseReport(Report):
|
|||
stock_context['locations'] = [l.id]
|
||||
with Transaction().set_context(stock_context):
|
||||
prdts = Product.search(dom_products, order=[('code', 'ASC')])
|
||||
# p = [p.template.id for p in prdts]
|
||||
# products_supplier = ProductsSupplier.search([
|
||||
# ('template', 'in', p)])
|
||||
suppliers = {}
|
||||
if data['group_by_supplier']:
|
||||
for p in prdts:
|
||||
if not p.template.product_suppliers:
|
||||
continue
|
||||
for prod_sup in p.template.product_suppliers:
|
||||
# sup = p.template.product_suppliers[0].party
|
||||
sup_id = prod_sup.party.id
|
||||
try:
|
||||
suppliers[sup_id]['products'].append(p)
|
||||
suppliers[sup_id]['total_amount'].append(p.cost_value)
|
||||
suppliers[sup_id]['total_amount'].append(p.amount_cost)
|
||||
except:
|
||||
suppliers[sup_id] = {}
|
||||
suppliers[sup_id]['products'] = [p]
|
||||
suppliers[sup_id]['party'] = prod_sup.party
|
||||
suppliers[sup_id]['total_amount'] = [p.cost_value]
|
||||
total_amount = sum([p.cost_value for p in prdts if p.cost_value])
|
||||
total_amount = sum([p.amount_cost for p in prdts if p.amount_cost])
|
||||
values[l.id] = {
|
||||
'name': l.name,
|
||||
'products': prdts,
|
||||
|
@ -439,23 +450,22 @@ class WarehouseReport(Report):
|
|||
|
||||
if data['only_minimal_level']:
|
||||
products = [p for p in products if p.quantity <= min_quantities[p.id]]
|
||||
total_amount = sum([p.cost_value for p in products if p.cost_value])
|
||||
total_amount = sum([p.amount_cost for p in products if p.amount_cost])
|
||||
suppliers = {}
|
||||
if data['group_by_supplier']:
|
||||
for p in products:
|
||||
if not p.template.product_suppliers:
|
||||
continue
|
||||
for prod_sup in p.template.product_suppliers:
|
||||
# sup = p.template.product_suppliers[0].party
|
||||
sup_id = prod_sup.party.id
|
||||
try:
|
||||
suppliers[sup_id]['products'].append(p)
|
||||
suppliers[sup_id]['total_amount'].append(p.cost_value)
|
||||
suppliers[sup_id]['total_amount'].append(p.amount_cost)
|
||||
except:
|
||||
suppliers[sup_id] = {}
|
||||
suppliers[sup_id]['products'] = [p]
|
||||
suppliers[sup_id]['party'] = prod_sup.party
|
||||
suppliers[sup_id]['total_amount'] = [p.cost_value]
|
||||
suppliers[sup_id]['total_amount'] = [p.amount_cost]
|
||||
products = suppliers.values()
|
||||
|
||||
cursor = Transaction().connection.cursor()
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[tryton]
|
||||
version=6.0.7
|
||||
version=6.0.8
|
||||
depends:
|
||||
product
|
||||
stock
|
||||
|
|
BIN
warehouse.fods
BIN
warehouse.fods
Binary file not shown.
Loading…
Reference in New Issue