~openerp-community/openobject-addons/trunk-addons-community

« back to all changes in this revision

Viewing changes to product_loan/sale.py

  • Committer: Alexis de Lattre
  • Date: 2014-01-20 16:28:33 UTC
  • Revision ID: alexis@via.ecp.fr-20140120162833-wozad66s7ln25ec2
Enhance the code ; the generation of the return picking is now inheritable. The return stock move is now linked to the out stock move (move_dest_id)
Small update to coding style.
Loan IN and OUT are now regular M2O fields and not properties (warning : it requires data migration).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
##############################################################################
23
23
 
24
24
 
25
 
from osv import fields, osv
26
 
import netsvc
 
25
from openerp.osv import fields, osv
 
26
from openerp import netsvc
27
27
import time
28
28
from datetime import datetime
29
29
 
41
41
        'product_type': fields.char('Product type', size=32),
42
42
    }
43
43
 
44
 
 
45
44
    def product_id_change(self, cr, uid, ids, pricelist, product_id, qty=0,
46
45
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
47
46
            lang=False, update_tax=True, date_order=False, packaging=False,
119
118
        'loan_end_date': fields.date('Loan End Date', required=False),
120
119
    }
121
120
 
122
 
 
123
121
    # Set the name with the loan sequence instead of the
124
122
    # sale order sequence
125
 
    def copy(self, cr, uid, id, default=None, context={}):
126
 
        order_id = super(sale_order, self).copy(cr, uid, id, default, context)
127
 
 
128
 
        if context.get('is_loan', False):
129
 
            self.pool.get('sale.order').write(cr, uid, [order_id], {
130
 
                'name': self.pool.get('ir.sequence').get(cr, uid, 'loan')
131
 
            })
 
123
    def copy(self, cr, uid, id, default=None, context=None):
 
124
        order_id = super(sale_order, self).copy(
 
125
            cr, uid, id, default=default, context=context)
 
126
        if context.get('is_loan'):
 
127
            self.pool['sale.order'].write(cr, uid, order_id, {
 
128
                'name': self.pool['ir.sequence'].get(cr, uid, 'loan')
 
129
            }, context=context)
132
130
 
133
131
        return order_id
134
132
 
164
162
 
165
163
        return new_ids
166
164
 
167
 
 
168
165
    def _prepare_order_picking(self, cr, uid, order, context=None):
169
166
        picking_vals = super(sale_order, self)._prepare_order_picking(cr, uid, order, context=context)
170
167
        if order.is_loan:
174
171
            })
175
172
        return picking_vals
176
173
 
177
 
 
178
 
    def _prepare_order_line_move(self, cr, uid, order, line, picking_id, date_planned, context=None):
179
 
        move_vals = super(sale_order, self)._prepare_order_line_move(cr, uid, order, line, picking_id, date_planned, context=context)
 
174
    def _prepare_order_line_move(
 
175
            self, cr, uid, order, line, picking_id, date_planned,
 
176
            context=None):
 
177
        if context is None:
 
178
            context = {}
 
179
        move_vals = super(sale_order, self)._prepare_order_line_move(
 
180
            cr, uid, order, line, picking_id, date_planned, context=context)
180
181
        if order.is_loan:
 
182
            if context.get('loan_in'):
 
183
                location_id = order.shop_id.warehouse_id.loan_out_location_id.id
 
184
                location_dest_id = order.shop_id.warehouse_id.loan_in_location_id.id
 
185
                date = order.loan_end_date
 
186
                move_dest_id = False
 
187
            else:
 
188
                location_id = order.shop_id.warehouse_id.loan_in_location_id.id
 
189
                location_dest_id = order.shop_id.warehouse_id.loan_out_location_id.id
 
190
                date = order.loan_start_date
 
191
                line_in_move = context.get('line_in_move')
 
192
                if line_in_move:
 
193
                    move_dest_id = line_in_move.get(line.id, False)
181
194
            move_vals.update({
182
 
                'date': order.loan_start_date,
183
 
                'date_expected': order.loan_start_date,
184
 
                'location_id': order.shop_id.warehouse_id.property_stock_loan_inside_location.id,
185
 
                'location_dest_id': order.shop_id.warehouse_id.property_stock_loan_outside_location.id,
 
195
                'date': date,
 
196
                'date_expected': date,
 
197
                'location_id': location_id,
 
198
                'location_dest_id': location_dest_id,
186
199
                'prodlot_id': line.prodlot_id.id,
 
200
                'move_dest_id': move_dest_id,
187
201
                })
188
202
        return move_vals
189
203
 
 
204
    def _prepare_loan_picking_in(self, cr, uid, order, context=None):
 
205
        return {
 
206
            'name': self.pool['ir.sequence'].get(cr, uid, 'stock.picking.in'),
 
207
            'origin': order.name,
 
208
            'date': order.loan_end_date,
 
209
            'type': 'in',
 
210
            'state': 'auto',
 
211
            'move_type': order.picking_policy,
 
212
            'sale_id': order.id,
 
213
            'address_id': order.partner_shipping_id.id,
 
214
            'note': order.note,
 
215
            'invoice_state': 'loan',
 
216
            'company_id': order.company_id.id,
 
217
        }
190
218
 
191
219
    def _create_pickings_and_procurements(self, cr, uid, order, order_lines, picking_id=False, context=None):
192
 
        res = super(sale_order, self)._create_pickings_and_procurements(cr, uid, order, order_lines, picking_id, context=context)
193
 
        if order.is_loan and order.picking_ids:
194
 
            picking_out_id = order.picking_ids[0].id
195
 
        else:
196
 
            return res
197
 
 
198
 
        # Create an incoming picking is we have at least one "stockable product"
199
 
        if any([order_line.product_id.type == 'product' for order_line in order_lines]):
200
 
            picking_obj = self.pool.get('stock.picking')
201
 
            move_obj = self.pool.get('stock.move')
202
 
 
203
 
            return_picking_default = {
204
 
                'type': 'in',
205
 
                'name': self.pool.get('ir.sequence').get(cr, uid, 'stock.picking.in'),
206
 
                'min_date': order.loan_end_date,
207
 
               }
208
 
 
209
 
            return_picking_id = picking_obj.copy(cr, uid, picking_out_id, return_picking_default, context=context)
210
 
            return_picking = picking_obj.browse(cr, uid, return_picking_id, context=context)
211
 
            picking_out = picking_obj.browse(cr, uid, picking_out_id, context=context)
212
 
            i = 0
213
 
            for move in return_picking.move_lines:
214
 
                move_vals = {
215
 
                    'date': order.loan_end_date,
216
 
                    'date_expected': order.loan_end_date,
217
 
                    'location_id': order.shop_id.warehouse_id.property_stock_loan_outside_location.id,
218
 
                    'location_dest_id': order.shop_id.warehouse_id.property_stock_loan_inside_location.id,
219
 
                    'sale_line_id': picking_out.move_lines[i].sale_line_id.id,
220
 
                    'prodlot_id': picking_out.move_lines[i].sale_line_id.prodlot_id.id,
221
 
                }
222
 
 
223
 
                if move.product_id.type == 'consu':
224
 
                    move_vals['state'] = 'cancel'
225
 
 
226
 
                move_obj.write(cr, uid, move.id, move_vals, context=context)
227
 
                i += 1
228
 
 
229
 
            wf_service = netsvc.LocalService("workflow")
230
 
            wf_service.trg_validate(uid, 'stock.picking', return_picking_id, 'button_confirm', cr)
231
 
 
 
220
        if context is None:
 
221
            context = {}
 
222
        line_in_move = {}  # key = line ID, value = in_move_id
 
223
        if order.is_loan:
 
224
            # Create an incoming picking is we have at least one "stockable product"
 
225
            if any([order_line.product_id.type == 'product' for order_line in order_lines]):
 
226
                picking_obj = self.pool['stock.picking']
 
227
                move_obj = self.pool['stock.move']
 
228
 
 
229
                return_picking_id = picking_obj.create(
 
230
                    cr, uid, self._prepare_loan_picking_in(
 
231
                        cr, uid, order, context=context),
 
232
                    context=context)
 
233
 
 
234
                move_in_ctx = context.copy()
 
235
                move_in_ctx['loan_in'] = True
 
236
 
 
237
                for line in order_lines:
 
238
                    if line.product_id.type == 'product':
 
239
                        # consu should not be return
 
240
                        in_move_id = move_obj.create(
 
241
                            cr, uid, self._prepare_order_line_move(
 
242
                                cr, uid, order, line, return_picking_id, False,
 
243
                                context=move_in_ctx),
 
244
                            context=context)
 
245
                        line_in_move[line.id] = in_move_id
 
246
 
 
247
                wf_service = netsvc.LocalService("workflow")
 
248
                wf_service.trg_validate(
 
249
                    uid, 'stock.picking', return_picking_id, 'button_confirm', cr)
 
250
 
 
251
        out_ctx = context.copy()
 
252
        out_ctx['line_in_move'] = line_in_move
 
253
        res = super(sale_order, self)._create_pickings_and_procurements(
 
254
            cr, uid, order, order_lines, picking_id, context=out_ctx)
232
255
        return res
233
256
 
234
 
 
235
257
    def _check_loan_date_order(self, cr, uid, ids):
236
258
        for order in self.browse(cr, uid, ids):
237
259
            if order.is_loan: