Computes percentage margin with the real cost price in order to have a coherent result

This commit is contained in:
jmartin 2015-05-21 11:07:02 +02:00
parent f03cdac228
commit 20149a69bc
1 changed files with 9 additions and 5 deletions

14
sale.py
View File

@ -2,6 +2,7 @@
#The COPYRIGHT file at the top level of this repository contains #The COPYRIGHT file at the top level of this repository contains
#the full copyright notices and license terms. #the full copyright notices and license terms.
from decimal import Decimal from decimal import Decimal
from math import fabs
from trytond.model import fields from trytond.model import fields
from trytond.pyson import Eval from trytond.pyson import Eval
from trytond.pool import Pool, PoolMeta from trytond.pool import Pool, PoolMeta
@ -47,7 +48,8 @@ class Sale:
and self.margin_percent_cache is not None): and self.margin_percent_cache is not None):
return self.margin_percent_cache return self.margin_percent_cache
cost = sum(Decimal(str(l.quantity)) * (l.cost_price or Decimal(0)) cost = sum(
Decimal(str(fabs(l.quantity))) * (l.cost_price or Decimal(0))
for l in self.lines if l.type == 'line') for l in self.lines if l.type == 'line')
if cost: if cost:
return (self.margin / cost).quantize(Decimal('0.0001')) return (self.margin / cost).quantize(Decimal('0.0001'))
@ -144,8 +146,7 @@ class SaleLine:
return return
if not self.cost_price: if not self.cost_price:
return Decimal('1.0') return Decimal('1.0')
cost = Decimal(str(self.quantity)) * (self.cost_price or cost = self.get_cost_price()
Decimal(0))
return (self.margin / cost).quantize(Decimal('0.0001')) return (self.margin / cost).quantize(Decimal('0.0001'))
else: else:
cost = Decimal(0) cost = Decimal(0)
@ -156,7 +157,10 @@ class SaleLine:
else: else:
return (self.margin / cost).quantize(Decimal('0.0001')) return (self.margin / cost).quantize(Decimal('0.0001'))
if line2.type == 'line': if line2.type == 'line':
cost += (Decimal(str(line2.quantity)) cost += line2.get_cost_price()
* (line2.cost_price or Decimal(0)))
elif line2.type == 'subtotal': elif line2.type == 'subtotal':
cost = Decimal(0) cost = Decimal(0)
def get_cost_price(self):
return Decimal(str(fabs(self.quantity))) * (self.cost_price or
Decimal(0))