diff --git a/modules/purchase_trade/valuation.py b/modules/purchase_trade/valuation.py index a7c8c01..1ebf30c 100644 --- a/modules/purchase_trade/valuation.py +++ b/modules/purchase_trade/valuation.py @@ -50,6 +50,8 @@ class ValuationBase(ModelSQL): amount = fields.Numeric("Amount",digits='unit') mtm = fields.Numeric("Mtm",digits='unit') lot = fields.Many2One('lot.lot',"Lot") + base_amount = fields.Numeric("Base Amount",digits='unit') + rate = fields.Numeric("Rate", digits=(16,6)) @classmethod def _base_pnl(cls, *, line, lot, pnl_type, sale=None): @@ -70,6 +72,8 @@ class ValuationBase(ModelSQL): @classmethod def _build_basis_pnl(cls, *, line, lot, sale_line, pc, sign): + Currency = Pool().get('currency.currency') + Date = Pool().get('ir.date') values = cls._base_pnl( line=line, lot=lot, @@ -97,22 +101,32 @@ class ValuationBase(ModelSQL): if pc.price and pc.ratio: amount = round(pc.price * qty * Decimal(sign) * pc.ratio / 100, 4) - + base_amount = amount + currency = sale_line.sale.currency.id if sale_line else line.purchase.currency.id + rate = Decimal(1) + if line.purchase.company.currency != currency: + with Transaction().set_context(date=Date.today()): + base_amount = Currency.compute(currency,amount, line.purchase.company.currency) + rate = round(amount / base_amount,6) last_price = pc.get_last_price() mtm = round(Decimal(last_price) * qty * Decimal(sign), 4) if last_price else Decimal(0) values.update({ 'quantity': round(qty, 5), 'amount': amount, + 'base_amount': base_amount, + 'rate': rate, 'mtm': round(amount - (mtm * pc.ratio / 100), 4), 'unit': sale_line.unit.id if sale_line else line.unit.id, - 'currency': sale_line.sale.currency.id if sale_line else line.purchase.currency.id, + 'currency': currency, }) return values @classmethod def _build_simple_pnl(cls, *, line, lot, sale_line, price, state, sign, pnl_type): + Currency = Pool().get('currency.currency') + Date = Pool().get('ir.date') values = cls._base_pnl( line=line, lot=lot, @@ -121,15 +135,25 @@ class ValuationBase(ModelSQL): ) qty = lot.get_current_quantity_converted() + amount = round(price * qty * Decimal(sign), 4) + base_amount = amount + currency = sale_line.sale.currency.id if sale_line else line.purchase.currency.id + rate = Decimal(1) + if line.purchase.company.currency != currency: + with Transaction().set_context(date=Date.today()): + base_amount = Currency.compute(currency,amount, line.purchase.company.currency) + rate = round(amount / base_amount,6) values.update({ 'price': round(price, 4), 'quantity': round(qty, 5), - 'amount': round(price * qty * Decimal(sign), 4), + 'amount': amount, + 'base_amount': base_amount, + 'rate': rate, 'mtm': Decimal(0), 'state': state, 'unit': sale_line.unit.id if sale_line else line.unit.id, - 'currency': sale_line.sale.currency.id if sale_line else line.purchase.currency.id, + 'currency': currency, 'counterparty': sale_line.sale.party.id if sale_line else line.purchase.party.id, 'product': sale_line.product.id if sale_line else line.product.id, 'reference': ( @@ -348,6 +372,8 @@ class ValuationDyn(ModelSQL,ModelView): r_quantity = fields.Numeric("Quantity",digits='r_unit') r_unit = fields.Many2One('product.uom',"Unit") r_amount = fields.Numeric("Amount",digits='r_unit') + r_base_amount = fields.Numeric("Base Amount",digits='r_unit') + r_rate = fields.Numeric("Rate",digits=(16,6)) r_mtm = fields.Numeric("Mtm",digits='r_unit') r_lot = fields.Many2One('lot.lot',"Lot") @@ -378,9 +404,77 @@ class ValuationDyn(ModelSQL,ModelView): Sum(val.quantity).as_('r_quantity'), Max(val.unit).as_('r_unit'), Sum(val.amount).as_('r_amount'), + Sum(val.base_amount).as_('r_base_amount'), + Sum(val.rate).as_('r_rate'), Sum(val.mtm).as_('r_mtm'), Max(val.lot).as_('r_lot'), where=wh, group_by=[val.type,val.counterparty,val.state]) return query + +class ValuationReport(ValuationBase, ModelView): + "Valuation Report" + __name__ = 'valuation.report' + + @classmethod + def table_query(cls): + Valuation = Pool().get('valuation.valuation') + val = Valuation.__table__() + context = Transaction().context + valuation_date = context.get('valuation_date') + wh = (val.date == valuation_date) + + query = val.select( + Literal(0).as_('create_uid'), + CurrentTimestamp().as_('create_date'), + Literal(None).as_('write_uid'), + Literal(None).as_('write_date'), + val.id.as_('id'), + val.purchase.as_('purchase'), + val.line.as_('line'), + val.date.as_('date'), + val.type.as_('type'), + val.reference.as_('reference'), + val.counterparty.as_('counterparty'), + val.product.as_('product'), + val.state.as_('state'), + val.price.as_('price'), + val.currency.as_('currency'), + val.quantity.as_('quantity'), + val.unit.as_('unit'), + val.amount.as_('amount'), + val.base_amount.as_('base_amount'), + val.rate.as_('rate'), + val.mtm.as_('mtm'), + val.lot.as_('lot'), + where=wh) + + return query + +class ValuationReportContext(ModelView): + "Valuation Report Context" + __name__ = 'valuation.report.context' + + valuation_date = fields.Date("Valuation date") + supplier = fields.Many2One('party.party',"Supplier") + client = fields.Many2One('party.party',"Client") + product = fields.Many2One('product.product',"Product") + purchase = fields.Many2One('purchase.purchase', "Purchase") + sale = fields.Many2One('sale.sale',"Sale") + state = fields.Selection([ + ('all', 'All'), + ('open', 'Open'), + ('fixed', 'Fixed'), + ('hedged', 'Hedged') + ], 'State') + + @classmethod + def default_valuation_date(cls): + pool = Pool() + Date = pool.get('ir.date') + return Date.today() + + @classmethod + def default_state(cls): + return 'all' diff --git a/modules/purchase_trade/valuation.xml b/modules/purchase_trade/valuation.xml index 63ba7dc..1b44bf2 100644 --- a/modules/purchase_trade/valuation.xml +++ b/modules/purchase_trade/valuation.xml @@ -20,5 +20,32 @@ tree valuation_tree_sequence4 + + + valuation.report.context + form + valuation_context_form + + + valuation.report + tree + valuation_list + + + Valuation + valuation.report + valuation.report.context + + + + + + + + \ No newline at end of file diff --git a/modules/purchase_trade/view/valuation_context_form.xml b/modules/purchase_trade/view/valuation_context_form.xml new file mode 100644 index 0000000..8ec7022 --- /dev/null +++ b/modules/purchase_trade/view/valuation_context_form.xml @@ -0,0 +1,16 @@ +
+