Raimon Esteve (Zikzakmedia) avatar Raimon Esteve (Zikzakmedia) committed 8c3dc1b

Active Record

Comments (0)

Files changed (8)

+Version 2.6.0 - 2012-10-16
+* Active Record
+* Simplify module information with python configuration
 * Only available Price fields by Stock Price Group in move view 
 * Add on_change_with_amount method on change price or quantity
 
 ====================
 
 The stock_valued module of the Tryton application platform.
-See __tryton__.py
 
 Installing
 ----------
 #This file is part stock_valued 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 .shipment import *
+
+
+def register():
+    Pool.register(
+        Move,
+        ShipmentOut,
+        module='stock_valued', type_='model')

__tryton__.py

-#This file is part stock_valued module for Tryton.
-#The COPYRIGHT file at the top level of this repository contains 
-#the full copyright notices and license terms.
-{
-    'name': 'Stock Valued',
-    'name_ca_ES': 'Albarans valorats',
-    'name_es_ES': 'Albaranes valorados',
-    'version': '2.4.0',
-    'author': 'Zikzakmedia',
-    'email': 'zikzak@zikzakmedia.com',
-    'website': 'http://www.zikzakmedia.com/',
-    'description': '''Add amount untaxed, tax and total in Shipment Out.
-If you have installed sale_discount, remember install stock_valued_discount
-    ''',
-    'description_ca_ES': '''Afegeix base, taxes i total en els albarans.
-Si heu instal·lat el mòdul sale_discount, recordeu d'instal·lar també stock_valued_discount
-    ''',
-    'description_es_ES': '''Añade base, impuestos y total en los albaranes
-Si habeis instalado el módulo sale_discount, recuerden también de instalar stock_valued_discount
-    ''',
-    'depends': [
-        'ir',
-        'res',
-        'account_invoice',
-        'stock',
-    ],
-    'xml': [
-        'stock.xml',
-        'move.xml',
-        'shipment.xml',
-    ],
-    'translation': [
-        'locale/ca_ES.po',
-        'locale/es_ES.po',
-    ]
-}
 #The COPYRIGHT file at the top level of this repository contains 
 #the full copyright notices and license terms.
 
-from trytond.model import ModelView, ModelSQL, fields
+from trytond.model import fields
 from trytond.pyson import Not, Equal, Eval, Bool
-from trytond.transaction import Transaction
-from trytond.pool import Pool
+from trytond.pool import Pool, PoolMeta
 from decimal import Decimal
 
+__all__ = ['Move']
+__metaclass__ = PoolMeta
+
 _ZERO = Decimal('0.0')
 
-class Move(ModelSQL, ModelView):
-    _name = 'stock.move'
+class Move:
+    "Stock Move"
+    __name__ = 'stock.move'
 
     amount = fields.Function(fields.Numeric('Amount',
             digits=(16, 4),
                 }, on_change_with=['quantity', 'unit_price'],
             ),'get_amount')
 
-    def on_change_with_amount(self, vals):
-        currency_obj = Pool().get('currency.currency')
-        if vals.get('quantity') and vals.get('unit_price'):
+    def on_change_with_amount(self, name=None):
+        Currency = Pool().get('currency.currency')
+        if self.quantity and self.unit_price:
             currency = (vals.get('_parent_invoice.currency')
-                or vals.get('currency'))
+                or self.currency)
             if isinstance(currency, (int, long)) and currency:
-                currency = currency_obj.browse(currency)
-            amount = Decimal(str(vals.get('quantity') or '0.0')) * \
-                    (vals.get('unit_price') or Decimal('0.0'))
+                currency = Currency.browse(currency)
+            amount = Decimal(str(self.quantity or '0.0')) * \
+                    (self.unit_price or Decimal('0.0'))
             if currency:
-                return currency_obj.round(currency, amount)
+                return Currency.round(currency, amount)
             return amount
         return Decimal('0.0')
 
-    def get_amount(self, ids, name):
-        currency_obj = Pool().get('currency.currency')
-        res = {}
-        for move in self.browse(ids):
-            res[move.id] = _ZERO
-            if move.quantity:
-                res[move.id] += currency_obj.round(
-                            move.company.currency,
-                            Decimal(str(move.quantity)) * move.unit_price)
+    def get_amount(self, name):
+        Currency = Pool().get('currency.currency')
+        res = _ZERO
+        if self.quantity:
+            res = Currency.round(
+                    self.company.currency,
+                    Decimal(str(self.quantity)) * self.unit_price)
         return res
-
-Move()
 
 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)
 
 setup(name='trytond_stock_valued',
     version=info.get('version', '0.0.1'),
-    description=info.get('description', ''),
-    author=info.get('author', 'Zikzakmedia'),
-    author_email=info.get('email', 'zikzak@zikzakmedia.com'),
-    url=info.get('website', 'http://www.zikzakmedia.com'),
+    description='Tryton module for stock valued',
+    author='Zikzakmedia SL',
+    author_email='zikzak@zikzakmedia.com',
+    url='http://www.zikzakmedia.com',
     download_url="https://bitbucket.org/zikzakmedia/trytond-stock_valued",
     package_dir={'trytond.modules.stock_valued': '.'},
     packages=[
     ],
     package_data={
         'trytond.modules.stock_valued': info.get('xml', []) \
-                + info.get('translation', []),
+            + ['tryton.cfg', 'locale/*.po'],
     },
     classifiers=[
         'Development Status :: 5 - Production/Stable',
 #The COPYRIGHT file at the top level of this repository contains 
 #the full copyright notices and license terms.
 
-from trytond.model import ModelView, ModelSQL, fields
+from trytond.model import fields
 from trytond.pyson import Eval
 from trytond.transaction import Transaction
-from trytond.pool import Pool
+from trytond.pool import Pool, PoolMeta
 from decimal import Decimal
 
-class ShipmentOut(ModelSQL, ModelView):
-    _name = 'stock.shipment.out'
+__all__ = ['ShipmentOut']
+__metaclass__ = PoolMeta
+
+class ShipmentOut:
+    __name__ = 'stock.shipment.out'
 
     untaxed_amount = fields.Function(fields.Numeric('Untaxed',
             digits=(16, Eval('currency_digits', 2)),
         readonly=True,
         depends=['currency_digits'])
 
-    def __init__(self):
-        super(ShipmentOut, self).__init__()
-        self._states_cached = ['done', 'assigned', 'packed', 'waiting', 'cancel']
+    @classmethod
+    def __setup__(cls):
+        super(ShipmentOut, cls).__setup__()
+        cls._states_cached = ['done', 'assigned', 'packed', 'waiting', 'cancel']
 
-    def get_tax_context(self, shipment):
-        party_obj = Pool().get('party.party')
-        res = {}
-        if isinstance(shipment, dict):
-            if shipment.get('customer'):
-                party = party_obj.browse(shipment['customer'])
-                if party.lang:
-                    res['language'] = party.lang.code
-        else:
-            if shipment.customer.lang:
-                res['language'] = shipment.customer.lang.code
-        return res
+    def get_tax_context(self):
+        context = {}
+        user = Pool().get('res.user')(Transaction().user)
+        context['language'] = user.language
+        if self.customer and self.customer.lang:
+            context['language'] = self.customer.lang.code
+        return context
 
-    def get_untaxed_amount(self, ids, name):
+    def get_untaxed_amount(self, name):
         '''
         Compute the untaxed amount for each ShipmentOut
         '''
-        currency_obj = Pool().get('currency.currency')
-        amounts = {}
-        for shipment in self.browse(ids):
-            if (shipment.state in self._states_cached
-                    and shipment.untaxed_amount_cache is not None):
-                amounts[shipment.id] = shipment.untaxed_amount_cache
-                continue
-            amount = sum((m.amount for m in shipment.outgoing_moves), Decimal(0))
-            amounts[shipment.id] = currency_obj.round(shipment.company.currency, amount)
-        return amounts
+        Currency = Pool().get('currency.currency')
+        if (self.state in self._states_cached
+                and self.untaxed_amount_cache is not None):
+            return self.untaxed_amount_cache
+        amount = sum((m.amount for m in self.outgoing_moves), Decimal(0))
+        return Currency.round(self.company.currency, amount)
 
-    def get_tax_amount(self, ids, name):
+    def get_tax_amount(self, name):
         '''
         Compute tax amount for each ShipmentOut
         '''
         pool = Pool()
-        currency_obj = pool.get('currency.currency')
-        tax_obj = pool.get('account.tax')
-        invoice_obj = pool.get('account.invoice')
+        Currency = pool.get('currency.currency')
+        Tax = pool.get('account.tax')
+        Invoice = pool.get('account.invoice')
 
-        amounts = {}
-        for shipment in self.browse(ids):
-            if (shipment.state in self._states_cached
-                    and shipment.tax_amount_cache is not None):
-                amounts[shipment.id] = shipment.tax_amount_cache
-                continue
-            context = self.get_tax_context(shipment)
-            taxes = {}
-            for move in shipment.outgoing_moves:
-                with Transaction().set_context(context):
-                    tax_list = tax_obj.compute(
-                            [t.id for t in move.product.customer_taxes], 
-                            move.unit_price, move.quantity)
-                # Don't round on each line to handle rounding error
-                for tax in tax_list:
-                    key, val = invoice_obj._compute_tax(tax, 'out_invoice')
-                    if not key in taxes:
-                        taxes[key] = val['amount']
-                    else:
-                        taxes[key] += val['amount']
-            amount = sum((currency_obj.round(shipment.company.currency, taxes[key])
-                    for key in taxes), Decimal(0))
-            amounts[shipment.id] = currency_obj.round(shipment.company.currency, amount)
-        return amounts
+        if (self.state in self._states_cached
+                and self.tax_amount_cache is not None):
+            return self.tax_amount_cache
+        context = self.get_tax_context()
+        taxes = {}
+        for move in self.outgoing_moves:
+            with Transaction().set_context(context):
+                tax_list = Tax.compute(getattr(move.product, 'customer_taxes', []),
+                    move.unit_price or Decimal('0.0'),
+                    move.quantity or 0.0)
+            for tax in tax_list:
+                key, val = Invoice._compute_tax(tax, 'out_invoice')
+                if not key in taxes:
+                    taxes[key] = val['amount']
+                else:
+                    taxes[key] += val['amount']
+        amount = sum((Currency.round(self.company.currency, taxes[key])
+                for key in taxes), Decimal(0))
+        return Currency.round(self.company.currency, amount)
 
-    def get_total_amount(self, ids, name):
+    def get_total_amount(self, name):
         '''
         Return the total amount of each ShipmentOut
         '''
-        currency_obj = Pool().get('currency.currency')
-        amounts = {}
-        for shipment in self.browse(ids):
-            if (shipment.state in self._states_cached
-                    and shipment.total_amount_cache is not None):
-                amounts[shipment.id] = shipment.total_amount_cache
-                continue
-            amounts[shipment.id] = currency_obj.round(shipment.company.currency,
-                shipment.untaxed_amount + shipment.tax_amount)
-        return amounts
-
-ShipmentOut()
+        Currency = Pool().get('currency.currency')
+        if (self.state in self._states_cached
+                and self.total_amount_cache is not None):
+            return self.total_amount_cache
+        if not (self.untaxed_amount and self.tax_amount):
+            return Decimal('0.0')
+        return Currency.round(self.company.currency,
+            self.untaxed_amount + self.tax_amount)
+[tryton]
+version=2.6.0
+depends:
+    ir
+    res
+    account_invoice
+    stock
+xml:
+    stock.xml
+    move.xml
+    shipment.xml
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.