Commits

Guillem Barba (NaN·tic) committed 0245c11

Add Shop's address and allow to set this as sale's shipment address

Comments (0)

Files changed (5)

 from .shop import *
 from .sale import *
 from .user import *
+from .stock import *
 
 
 def register():
         SaleShopResUser,
         Sale,
         User,
+        ShipmentOut,
+        ShipmentOutReturn,
         module='sale_shop', type_='model')
             ], states={
             'readonly': Bool(Eval('reference')),
             }, depends=['reference'])
+    shop_address = fields.Function(fields.Many2One('party.address',
+            'Shop Address', on_change_with=['shop']),
+        'on_change_with_shop_address')
 
     @classmethod
     def __setup__(cls):
         super(Sale, cls).__setup__()
+
+        shipment_addr_domain = cls.shipment_address.domain[:]
+        if shipment_addr_domain:
+            cls.shipment_address.domain = [
+                'OR',
+                shipment_addr_domain,
+                [('id', '=', Eval('shop_address', 0))],
+                ]
+        else:
+            cls.shipment_address.domain = [('id', '=', Eval('shop_address'))]
+        cls.shipment_address.depends.append('shop_address')
+
         cls._error_messages.update({
                 'not_sale_shop': (
                     'Go to user preferences and select a shop ("%s")'),
         user = User(Transaction().user)
         return user.shop.payment_term.id if user.shop else None
 
+    @staticmethod
+    def default_shop_address():
+        User = Pool().get('res.user')
+        user = User(Transaction().user)
+        return (user.shop and user.shop.address and
+            user.shop.address.id or None)
+
+    def on_change_with_shop_address(self, name=None):
+        return (self.shop and self.shop.warehouse.address and
+            self.shop.warehouse.address.id or None)
+
     def on_change_party(self):
         User = Pool().get('res.user')
         Shop = Pool().get('sale.shop')
 
     name = fields.Char('Shop Name', required=True, select=True)
     users = fields.Many2Many('sale.shop-res.user', 'shop', 'user', 'Users')
+    address = fields.Many2One('party.address', 'Address', domain=[
+            ('party', '=', Eval('company_party')),
+            ], depends=['company_party'])
     warehouse = fields.Many2One('stock.location', "Warehouse", required=True,
         domain=[('type', '=', 'warehouse')])
     price_list = fields.Many2One('product.price_list', 'Pricelist',
+# This file is part sale_shop module for Tryton.
+# The COPYRIGHT file at the top level of this repository contains the full
+# copyright notices and license terms.
+from trytond.model import fields
+from trytond.pool import Pool, PoolMeta
+from trytond.pyson import Eval
+
+__all__ = ['ShipmentOut', 'ShipmentOutReturn']
+__metaclass__ = PoolMeta
+
+
+class ShipmentOut:
+    __name__ = 'stock.shipment.out'
+
+    shop_addresses = fields.Function(fields.Many2Many('party.address', None,
+            None, 'Shop Addresses', on_change_with=['warehouse']),
+        'on_change_with_shop_addresses')
+
+    @classmethod
+    def __setup__(cls):
+        super(ShipmentOut, cls).__setup__()
+        delivery_addr_domain = cls.delivery_address.domain[:]
+        if delivery_addr_domain:
+            cls.delivery_address.domain = [
+                'OR',
+                delivery_addr_domain,
+                [('id', 'in', Eval('shop_addresses'))],
+                ]
+        else:
+            cls.delivery_address.domain = [
+                ('id', 'in', Eval('shop_addresses')),
+                ]
+        if 'shop_addresses' not in cls.delivery_address.depends:
+            cls.delivery_address.depends.append('shop_addresses')
+
+    def on_change_with_shop_addresses(self, name=None):
+        Shop = Pool().get('sale.shop')
+        if not self.warehouse:
+            return []
+        warehouse_shops = Shop.search([
+                ('warehouse', '=', self.warehouse.id),
+                ])
+        return [s.address.id for s in warehouse_shops if s.address]
+
+
+class ShipmentOutReturn:
+    __name__ = 'stock.shipment.out.return'
+
+    shop_addresses = fields.Function(fields.Many2Many('party.address', None,
+            None, 'Shop Addresses', on_change_with=['warehouse']),
+        'on_change_with_shop_addresses')
+
+    @classmethod
+    def __setup__(cls):
+        super(ShipmentOutReturn, cls).__setup__()
+        delivery_addr_domain = cls.delivery_address.domain[:]
+        if delivery_addr_domain:
+            cls.delivery_address.domain = [
+                'OR',
+                delivery_addr_domain,
+                [('id', 'in', Eval('shop_addresses'))],
+                ]
+        else:
+            cls.delivery_address.domain = [
+                ('id', 'in', Eval('shop_addresses')),
+                ]
+        if 'shop_addresses' not in cls.delivery_address.depends:
+            cls.delivery_address.depends.append('shop_addresses')
+
+    def on_change_with_shop_addresses(self, name=None):
+        Shop = Pool().get('sale.shop')
+        if not self.warehouse:
+            return []
+        warehouse_shops = Shop.search([
+                ('warehouse', '=', self.warehouse.id),
+                ])
+        return [s.address.id for s in warehouse_shops if s.address]

view/sale_shop_form.xml

     <field name="name"/>
     <label name="company"/>
     <field name="company"/>
+    <label name="address"/>
+    <field name="address"/>
     <notebook colspan="4">
         <page string="General" id="general">
             <label name="sale_sequence"/>