[MERGE] delivery: cleanup yml
[odoo/odoo.git] / addons / delivery / delivery.py
index aae6f56..214d2a6 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
@@ -15,7 +15,7 @@
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
@@ -36,10 +36,11 @@ class delivery_carrier(osv.osv):
         if not order_id:
             res = super(delivery_carrier, self).name_get(cr, uid, ids, context=context)
         else:
-            order = self.pool.get('sale.order').browse(cr, uid, [order_id], context=context)[0]
+            order = self.pool.get('sale.order').browse(cr, uid, order_id, context=context)
             currency = order.pricelist_id.currency_id.name or ''
             res = [(r['id'], r['name']+' ('+(str(r['price']))+' '+currency+')') for r in self.read(cr, uid, ids, ['name', 'price'], context)]
         return res
+
     def get_price(self, cr, uid, ids, field_name, arg=None, context=None):
         res={}
         if context is None:
@@ -50,7 +51,7 @@ class delivery_carrier(osv.osv):
             order_id=context.get('order_id',False)
             price=False
             if order_id:
-              order = sale_obj.browse(cr, uid, [order_id], context=context)[0]
+              order = sale_obj.browse(cr, uid, order_id, context=context)
               carrier_grid=self.grid_get(cr,uid,[carrier.id],order.partner_shipping_id.id,context)
               if carrier_grid:
                   price=grid_obj.get_price(cr, uid, carrier_grid, order, time.strftime('%Y-%m-%d'), context)
@@ -58,19 +59,28 @@ class delivery_carrier(osv.osv):
                   price = 0.0
             res[carrier.id]=price
         return res
+
     _columns = {
-        'name': fields.char('Carrier', size=64, required=True),
-        'partner_id': fields.many2one('res.partner', 'Carrier Partner', required=True),
+        'name': fields.char('Delivery Method', size=64, required=True),
+        'partner_id': fields.many2one('res.partner', 'Transport Company', required=True, help="The partner that is doing the delivery service."),
         'product_id': fields.many2one('product.product', 'Delivery Product', required=True),
         'grids_id': fields.one2many('delivery.grid', 'carrier_id', 'Delivery Grids'),
-        'price' : fields.function(get_price, method=True,string='Price'),
-        'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the delivery carrier without removing it.")
+        'price' : fields.function(get_price, string='Price'),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the delivery carrier without removing it."),
+        'normal_price': fields.float('Normal Price', help="Keep empty if the pricing depends on the advanced pricing per destination"),
+        'free_if_more_than': fields.boolean('Free If More Than', help="If the order is more expensive than a certain amount, the customer can benefit from a free shipping"),
+        'amount': fields.float('Amount', help="Amount of the order to benefit from a free shipping, expressed in the company currency"),
+        'use_detailed_pricelist': fields.boolean('Advanced Pricing per Destination', help="Check this box if you want to manage delivery prices that depends on the destination, the weight, the total of the order, etc."),
+        'pricelist_ids': fields.one2many('delivery.grid', 'carrier_id', 'Advanced Pricing'),
     }
+
     _defaults = {
-        'active': lambda *args:1
+        'active': 1,
+        'free_if_more_than': False,
     }
+
     def grid_get(self, cr, uid, ids, contact_id, context=None):
-        contact = self.pool.get('res.partner.address').browse(cr, uid, [contact_id], context=context)[0]
+        contact = self.pool.get('res.partner.address').browse(cr, uid, contact_id, context=context)
         for carrier in self.browse(cr, uid, ids, context=context):
             for grid in carrier.grids_id:
                 get_id = lambda x: x.id
@@ -86,6 +96,70 @@ class delivery_carrier(osv.osv):
                     continue
                 return grid.id
         return False
+
+    def create_grid_lines(self, cr, uid, ids, vals, context=None):
+        if context == None:
+            context = {}
+        grid_line_pool = self.pool.get('delivery.grid.line')
+        grid_pool = self.pool.get('delivery.grid')
+        for record in self.browse(cr, uid, ids, context=context):
+            grid_id = grid_pool.search(cr, uid, [('carrier_id', '=', record.id)], context=context)
+
+            if grid_id and not (record.normal_price or record.free_if_more_than):
+                grid_pool.unlink(cr, uid, grid_id, context=context)
+
+            if not (record.normal_price or record.free_if_more_than):
+                continue
+
+            if not grid_id:
+                record_data = {
+                    'name': record.name,
+                    'carrier_id': record.id,
+                    'sequence': 10,
+                }
+                new_grid_id = grid_pool.create(cr, uid, record_data, context=context)
+                grid_id = [new_grid_id]
+
+            lines = grid_line_pool.search(cr, uid, [('grid_id','in',grid_id)], context=context)
+            if lines:
+                grid_line_pool.unlink(cr, uid, lines, context=context)
+
+            #create the grid lines
+            default_data = None
+            if record.free_if_more_than:
+                default_data = {
+                    'grid_id': grid_id and grid_id[0],
+                    'name': _('Free if more than %.2f') % record.amount,
+                    'type': 'price',
+                    'operator': '>=',
+                    'max_value': record.amount,
+                    'standard_price': 0.0,
+                    'list_price': 0.0,
+                }
+            if record.normal_price:
+                default_data = {
+                    'grid_id': grid_id and grid_id[0],
+                    'name': _('Default price'),
+                    'type': 'price',
+                    'operator': '>=',
+                    'max_value': 0.0,
+                    'standard_price': record.normal_price,
+                    'list_price': record.normal_price,
+                }
+            if default_data:
+                grid_line_pool.create(cr, uid, default_data, context=context)
+        return True
+
+    def write(self, cr, uid, ids, vals, context=None):
+        res_id = super(delivery_carrier, self).write(cr, uid, ids, vals, context=context)
+        self.create_grid_lines(cr, uid, ids, vals, context=context)
+        return res_id
+
+    def create(self, cr, uid, vals, context=None):
+        res_id = super(delivery_carrier, self).create(cr, uid, vals, context=context)
+        self.create_grid_lines(cr, uid, [res_id], vals, context=context)
+        return res_id
+
 delivery_carrier()
 
 class delivery_grid(osv.osv):
@@ -100,7 +174,7 @@ class delivery_grid(osv.osv):
         'zip_from': fields.char('Start Zip', size=12),
         'zip_to': fields.char('To Zip', size=12),
         'line_ids': fields.one2many('delivery.grid.line', 'grid_id', 'Grid Line'),
-        'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the delivery grid without removing it."),
+        'active': fields.boolean('Active', help="If the active field is set to False, it will allow you to hide the delivery grid without removing it."),
     }
     _defaults = {
         'active': lambda *a: 1,
@@ -138,7 +212,7 @@ class delivery_grid(osv.osv):
                 ok = True
                 break
         if not ok:
-            raise osv.except_osv(_('No price available !'), _('No line matched this order in the choosed delivery grids !'))
+            raise osv.except_osv(_('No price available!'), _('No line matched this product or order in the choosed delivery grid.'))
 
         return price
 
@@ -150,8 +224,10 @@ class delivery_grid_line(osv.osv):
     _description = "Delivery Grid Line"
     _columns = {
         'name': fields.char('Name', size=32, required=True),
-        'grid_id': fields.many2one('delivery.grid', 'Grid',required=True),
-        'type': fields.selection([('weight','Weight'),('volume','Volume'),('wv','Weight * Volume'), ('price','Price')], 'Variable', required=True),
+        'grid_id': fields.many2one('delivery.grid', 'Grid',required=True, ondelete='cascade'),
+        'type': fields.selection([('weight','Weight'),('volume','Volume'),\
+                                  ('wv','Weight * Volume'), ('price','Price')],\
+                                  'Variable', required=True),
         'operator': fields.selection([('==','='),('<=','<='),('>=','>=')], 'Operator', required=True),
         'max_value': fields.float('Maximum Value', required=True),
         'price_type': fields.selection([('fixed','Fixed'),('variable','Variable')], 'Price Type', required=True),
@@ -167,9 +243,25 @@ class delivery_grid_line(osv.osv):
     }
     _order = 'list_price'
 
-
 delivery_grid_line()
 
+class define_delivery_steps(osv.osv_memory):
+    _name = 'delivery.define.delivery.steps.wizard'
 
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+    _columns = {
+        'picking_policy' : fields.selection([('direct', 'Deliver each product when available'), ('one', 'Deliver all products at once')], 'Picking Policy'),
+    }
+    _defaults = {
+        'picking_policy': lambda s,c,u,ctx: s.pool.get('sale.order').default_get(c,u,['picking_policy'],context=ctx)['picking_policy']
+    }
+
+    def apply_cb(self, cr, uid, ids, context=None):
+        ir_values_obj = self.pool.get('ir.values')
+        wizard = self.browse(cr, uid, ids, context=context)[0]
+        ir_values_obj.set(cr, uid, 'default', False, 'picking_policy', ['sale.order'], wizard.picking_policy)
+        return {'type' : 'ir.actions.act_window_close'}
 
+define_delivery_steps()
+
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: