/usr/share/pyshared/trytond/modules/product_cost_fifo/move.py is in tryton-modules-product-cost-fifo 2.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #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 decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pool import Pool
class Move(ModelSQL, ModelView):
_name = 'stock.move'
fifo_quantity = fields.Float('FIFO Quantity',
digits=(16, Eval('unit_digits', 2)),
depends=['unit_digits'])
def __init__(self):
super(Move, self).__init__()
self._sql_constraints += [
('check_fifo_quantity_out',
'CHECK(quantity >= fifo_quantity)',
'FIFO quantity can not be greater than quantity!'),
]
self._error_messages.update({
'del_move_fifo': 'You can not delete move that is used ' \
'for FIFO cost price!',
})
def default_fifo_quantity(self):
return 0.0
def _update_fifo_out_product_cost_price(self, product, quantity, uom, date):
'''
Update the product cost price of the given product. Update
fifo_quantity on the concerned incomming moves. Return the
cost price for outputing the given product and quantity.
:param product: a BrowseRecord of the product
:param quantity: the quantity of the outgoing product
:param uom: the uom id or a BrowseRecord of the uom
:param date: the date for the currency rate calculation
:return: cost_price (of type decimal)
'''
template_obj = Pool().get('product.template')
uom_obj = Pool().get('product.uom')
if isinstance(uom, (int, long)):
uom = uom_obj.browse(uom)
total_qty = uom_obj.compute_qty(uom, quantity, product.default_uom,
round=False)
fifo_moves = template_obj.get_fifo_move(product.template.id, total_qty)
cost_price = Decimal("0.0")
consumed_qty = 0.0
for move, move_qty in fifo_moves:
consumed_qty += move_qty
move_unit_price = uom_obj.compute_price(move.uom, move.unit_price,
move.product.default_uom)
cost_price += move_unit_price * Decimal(str(move_qty))
self._update_product_cost_price(product.id, -move_qty,
product.default_uom, move_unit_price, move.currency,
move.company, date)
move_qty = uom_obj.compute_qty(product.default_uom, move_qty,
move.uom, round=False)
self.write(move.id, {
'fifo_quantity': (move.fifo_quantity or 0.0) + move_qty,
})
if Decimal(str(consumed_qty)) != Decimal("0"):
cost_price = cost_price / Decimal(str(consumed_qty))
if cost_price != Decimal("0"):
digits = self.cost_price.digits
return cost_price.quantize(
Decimal(str(10.0**-digits[1])))
else:
return product.cost_price
def create(self, vals):
pool = Pool()
location_obj = pool.get('stock.location')
product_obj = pool.get('product.product')
uom_obj = pool.get('product.uom')
date_obj = pool.get('ir.date')
today = date_obj.today()
effective_date = vals.get('effective_date') or today
if vals.get('state') == 'done':
from_location = location_obj.browse(vals['from_location'])
to_location = location_obj.browse(vals['to_location'])
product = product_obj.browse(vals['product'])
if from_location.type == 'supplier' \
and product.cost_price_method == 'fifo':
self._update_product_cost_price(vals['product'],
vals['quantity'], vals['uom'], vals['unit_price'],
vals['currency'], effective_date)
if to_location.type == 'supplier' \
and product.cost_price_method == 'fifo':
self._update_product_cost_price(vals['product'],
-vals['quantity'], vals['uom'], vals['unit_price'],
vals['currency'], effective_date)
if to_location.type != 'storage' \
and to_location.type != 'supplier' \
and product.cost_price_method == 'fifo':
cost_price = self._update_fifo_out_product_cost_price(product,
vals['quantity'], vals['uom'], effective_date)
if not vals.get('cost_price'):
vals['cost_price'] = cost_price
return super(Move, self).create(vals)
def write(self, ids, vals):
date_obj = Pool().get('ir.date')
if isinstance(ids, (int, long)):
ids = [ids]
today = date_obj.today()
effective_date = vals.get('effective_date') or today
if 'state' in vals and vals['state'] == 'done':
for move in self.browse(ids):
if vals['state'] == 'cancel':
if move.from_location.type == 'supplier' \
and move.state != 'cancel' \
and move.product.cost_price_method == 'fifo':
self._update_product_cost_price(move.product.id,
-move.quantity, move.uom, move.unit_price,
move.currency, move.company, effective_date)
if move.to_location.type == 'supplier' \
and move.state != 'cancel' \
and move.product.cost_price_method == 'fifo':
self._update_product_cost_price(move.product.id,
move.quantity, move.uom, move.unit_price,
move.currency, move.company, effective_date)
elif vals['state'] == 'done':
if move.from_location.type == 'supplier' \
and move.state != 'done' \
and move.product.cost_price_method == 'fifo':
self._update_product_cost_price(move.product.id,
move.quantity, move.uom, move.unit_price,
move.currency, move.company, effective_date)
if move.to_location.type == 'supplier' \
and move.state != 'done' \
and move.product.cost_price_method == 'fifo':
self._update_product_cost_price(move.product.id,
-move.quantity, move.uom, move.unit_price,
move.currency, move.company, effective_date)
if move.to_location.type != 'storage' \
and move.to_location.type != 'supplier' \
and move.product.cost_price_method == 'fifo':
cost_price = self._update_fifo_out_product_cost_price(
move.product, move.quantity, move.uom,
effective_date)
if not vals.get('cost_price'):
vals['cost_price'] = cost_price
return super(Move, self).write(ids, vals)
def delete(self, ids):
if self.search([
('id', 'in', ids),
('fifo_quantity', '!=', 0.0),
]):
self.raise_user_error('del_move_fifo')
return super(Move, self).delete(ids)
Move()
|