Active Record

This commit is contained in:
resteve 2012-10-16 17:09:39 +02:00
parent 77bfe5646d
commit 6d5caf2e7a
8 changed files with 80 additions and 159 deletions

1
README
View File

@ -2,7 +2,6 @@ trytond_sale_discount
=====================
Adds a discount field on sale order of Tryton.
See __tryton__.py
Installing
----------

View File

@ -1,6 +1,13 @@
#This file is part sale_discount module for Tryton.
#The COPYRIGHT file at the top level of this repository contains
#the full copyright notices and license terms.
from trytond.pool import Pool
from move import *
from sale import *
def register():
Pool.register(
SaleLine,
Move,
module='sale_discount', type_='model')

View File

@ -1,42 +0,0 @@
#This file is part sale_discount module for Tryton.
#The COPYRIGHT file at the top level of this repository contains
#the full copyright notices and license terms.
{
'name': 'Sale Discount',
'name_ca_ES': 'Descomptes comandes de venda',
'name_de_DE': 'Verkauf Rabatt',
'name_es_ES': 'Descuentos pedidos de venta',
'version': '2.4.0',
'author': 'virtual things',
'email': 'info@virtual-things.biz',
'website': 'http://www.virtual-things.biz/',
'description': '''Discounts for Sales
- Define discounts for sale lines
- Adds field discount in report sale
''',
'description_ca_ES': '''Descomptes per a vendes
- Defineix descomptes per a línies de venda
- Afegeix camp descompte al tiquet de venda
''',
'description_de_DE': '''Rabatt für Verkäufe
- Ermöglicht die Eingabe von Rabatten pro Verkaufsposition
- Fügt Rabattfeld im Bericht Verkauf hinzu
''',
'description_es_ES': '''Descuentos para ventas
- Define descuentos para líneas de ventas
- Añade campo descuento al tiquet de venta
''',
'depends': [
'sale',
'account_invoice_discount',
],
'xml': [
'move.xml',
'sale.xml',
],
'translation': [
'locale/ca_ES.po',
'locale/de_DE.po',
'locale/es_ES.po',
],
}

16
move.py
View File

@ -1,15 +1,16 @@
#This file is part sale_discount module for 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.model import fields
from trytond.pyson import Not, Equal, Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pool import PoolMeta
class Move(ModelSQL, ModelView):
_name = 'stock.move'
__all__ = ['Move']
__metaclass__ = PoolMeta
class Move:
"Stock Move"
__name__ = 'stock.move'
discount = fields.Numeric('Discount %', digits=(16, 4),
states={
@ -17,4 +18,3 @@ class Move(ModelSQL, ModelView):
},
depends=['state'])
Move()

143
sale.py
View File

@ -3,114 +3,57 @@
#the full copyright notices and license terms.
import copy
from decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields
from trytond.model import fields
from trytond.pyson import Not, Equal, Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pool import Pool, PoolMeta
__all__ = ['SaleLine']
__metaclass__ = PoolMeta
class SaleLine(ModelSQL, ModelView):
_name = 'sale.line'
class SaleLine:
'Sale Line'
__name__ = 'sale.line'
discount = fields.Numeric('Discount %', digits=(16, 2), states={
'invisible': Not(Equal(Eval('type'), 'line')),
}, depends=['type'])
discount = fields.Numeric('Discount %',
digits=(16, Eval('currency_digits', 2)),
states={
'invisible': Not(Equal(Eval('type'), 'line')),
}, on_change=['discount', 'product', 'quantity', 'type', 'unit_price'],
depends=['type','unit_price', 'quantity', 'amount'])
def __init__(self):
super(SaleLine, self).__init__()
self.amount = copy.copy(self.amount)
if self.amount.on_change_with:
self.amount.on_change_with.extend(['discount'])
self._reset_columns()
def default_discount(self):
@staticmethod
def default_discount():
return 0.0
def on_change_with_amount(self, vals):
if vals.get('type') == 'line' and vals.get('unit_price') and \
vals.get('discount'):
vals = vals.copy()
vals['unit_price'] = (vals.get('unit_price') -
vals.get('unit_price') * vals.get('discount') * Decimal('0.01'))
return super(SaleLine, self).on_change_with_amount(vals)
def get_amount(self, ids, name):
currency_obj = Pool().get('currency.currency')
res = super(SaleLine, self).get_amount(ids, name)
for line in self.browse(ids):
if line.type == 'line':
currency = line.sale and line.sale.currency \
or line.currency
res[line.id] = currency_obj.round(currency,
Decimal(str(line.quantity)) * line.unit_price -
(Decimal(str(line.quantity)) * line.unit_price *
(line.discount * Decimal('0.01'))))
return res
def get_invoice_line(self, line, invoice_type):
res = super(SaleLine, self).get_invoice_line(line, invoice_type)
if not res:
return []
if line.type != 'line':
return [res[0]]
if res[0]['quantity'] <= 0.0:
return None
res[0]['discount'] = line.discount
return [res[0]]
def get_move(self, line, shipment_type):
'''
Add discount value in move out for the sale line
'''
res = super(SaleLine, self).get_move(line, shipment_type)
if line.discount and shipment_type == 'out':
res['discount'] = line.discount
return res
SaleLine()
class Sale(ModelSQL, ModelView):
_name = 'sale.sale'
def on_change_lines(self, vals):
if vals.get('lines'):
vals = vals.copy()
lines = []
for line in vals['lines']:
if line.get('unit_price') and line.get('discount'):
line['unit_price'] = (line.get('unit_price')-
line.get('unit_price') * line.get('discount') *
Decimal('0.01'))
lines.append(line)
vals['lines'] = lines
return super(Sale, self).on_change_lines(vals)
def get_tax_amount(self, ids, name):
'''
Compute tax amount for each sales
:param sales: a BrowseRecordList of sales
:return: a dictionary with sale id as key and
tax amount as value
'''
currency_obj = Pool().get('currency.currency')
tax_obj = Pool().get('account.tax')
def on_change_discount(self):
res = {}
for sale in self.browse(ids):
context = self.get_tax_context(sale)
res.setdefault(sale.id, Decimal('0.0'))
for line in sale.lines:
if line.type != 'line':
continue
price = line.unit_price - line.unit_price * line.discount / Decimal('100')
# Don't round on each line to handle rounding error
with Transaction().set_context(**context):
taxes_compute = tax_obj.compute(
[t.id for t in line.taxes], price, line.quantity)
for tax in taxes_compute:
res[sale.id] += tax['amount']
res[sale.id] = currency_obj.round(sale.currency, res[sale.id])
if self.discount == Decimal(0.0) and self.quantity != None:
res['amount'] = Decimal(self.quantity)*self.unit_price
if self.discount and self.unit_price and self.type == 'line':
res['amount'] = Decimal(self.quantity)*(self.unit_price -
self.unit_price * self.discount * Decimal('0.01'))
return res
Sale()
def on_change_product(self):
res = super(SaleLine, self).on_change_product()
res['discount'] = Decimal(0.0)
return res
def on_change_quantity(self):
res = super(SaleLine, self).on_change_quantity()
res['discount'] = Decimal(0.0)
return res
def get_amount(self, name):
Currency = Pool().get('currency.currency')
res = super(SaleLine, self).get_amount(name)
if self.type == 'line' and self.discount and self.discount != None:
currency = self.sale and self.sale.currency \
or self.currency
res = Currency.round(currency, \
Decimal(str(self.quantity)) * self.unit_price - \
(Decimal(str(self.quantity)) * self.unit_price *\
(self.discount * Decimal('0.01'))))
return res

View File

@ -42,7 +42,6 @@ The COPYRIGHT file at the top level of this repository contains the full copyrig
<xpath
expr="/form/notebook/page/label[@name=&quot;unit&quot;]"
position="before">
<newline/>
<label name="discount"/>
<field name="discount"/>
</xpath>

View File

@ -5,8 +5,15 @@
from setuptools import setup
import re
import os
import ConfigParser
info = eval(open('__tryton__.py').read())
config = ConfigParser.ConfigParser()
config.readfp(open('tryton.cfg'))
info = dict(config.items('tryton'))
for key in ('depends', 'extras_depend', 'xml'):
if key in info:
info[key] = info[key].strip().splitlines()
major_version, minor_version, _ = info.get('version', '0.0.1').split('.', 2)
major_version = int(major_version)
minor_version = int(minor_version)
@ -22,10 +29,10 @@ requires.append('trytond >= %s.%s, < %s.%s' %
setup(name='trytond_sale_discount',
version=info.get('version', '0.0.1'),
description=info.get('description', ''),
author=info.get('author', ''),
author_email=info.get('email', ''),
url=info.get('website', ''),
description='Tryton module for discount in sales',
author='Virtual Things',
author_email='info@virtual-things.biz',
url='http://www.virtual-things.biz',
download_url="http://downloads.tryton.org/" + \
info.get('version', '0.0.1').rsplit('.', 1)[0] + '/',
package_dir={'trytond.modules.sale_discount': '.'},
@ -35,7 +42,7 @@ setup(name='trytond_sale_discount',
],
package_data={
'trytond.modules.sale_discount': info.get('xml', []) \
+ info.get('translation', []),
+ ['tryton.cfg', 'locale/*.po'],
},
classifiers=[
'Development Status :: 5 - Production/Stable',

8
tryton.cfg Normal file
View File

@ -0,0 +1,8 @@
[tryton]
version=2.6.0
depends:
sale
account_invoice_discount
xml:
move.xml
sale.xml