~savoirfairelinux-openerp/sale-wkfl/sfl-contrib-45518

« back to all changes in this revision

Viewing changes to sale_dropshipping/purchase.py

  • Committer: nicolas.bessi at camptocamp
  • Date: 2013-11-15 13:33:28 UTC
  • mfrom: (19.2.17 add-sale_dropshipping-jge)
  • Revision ID: nicolas.bessi@camptocamp.com-20131115133328-amlv89gmuo6jg54d
[ADD] module sale_dropshipping makes it better to deal with purchases with known sale schemes, specially the following case:
1) normal
2) direct delivery (also called drop shipping)
3) direct invoice
4) direct delivery and direct invoice

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2010 Akretion LDTA (<http://www.akretion.com>).
 
6
#    @author Raphaël Valyi
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
from openerp.osv import fields, orm
 
23
 
 
24
 
 
25
class purchase_order_line(orm.Model):
 
26
    _inherit = "purchase.order.line"
 
27
 
 
28
    _columns = {
 
29
        'sale_order_line_id': fields.many2one('sale.order.line', 'Sale Order Line'),
 
30
    }
 
31
 
 
32
 
 
33
class purchase_order(orm.Model):
 
34
    _inherit = "purchase.order"
 
35
 
 
36
    _columns = {
 
37
        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
 
38
        'sale_id': fields.many2one('sale.order', 'Related Sale Order'),
 
39
        'sale_flow': fields.selection([('normal', 'Normal'),
 
40
                                       ('direct_delivery', 'Drop Shipping'),
 
41
                                       ('direct_invoice', 'Direct Invoice/Indirect Delivery'),
 
42
                                       ('direct_invoice_and_delivery', 'Direct Invoice')],
 
43
                                      'Sale Flow',
 
44
                                      help="Is this order tied to a sale order?"
 
45
                                           " How will it be delivered and invoiced then?"),
 
46
    }
 
47
 
 
48
    _defaults = {
 
49
        'sale_flow': 'normal',
 
50
    }
 
51
 
 
52
    def sale_flow_change(self, cr, uid, ids, sale_flow, sale_id,
 
53
                         warehouse_id, context=None):
 
54
        if sale_id:
 
55
            sale_obj = self.pool.get('sale.order')
 
56
            partner_obj = self.pool.get('res.partner')
 
57
            warehouse_obj = self.pool.get('stock.warehouse')
 
58
            sale = sale_obj.browse(cr, uid, sale_id, context=context)
 
59
            partner_id = sale.partner_id.id
 
60
            if sale_flow in ('direct_delivery', 'direct_invoice_and_delivery'):
 
61
                partner = partner_obj.browse(cr, uid, partner_id,
 
62
                                             context=context)
 
63
                address = partner.address_get(['delivery'])['delivery']
 
64
                vals = {'location_id': partner.property_stock_customer.id,
 
65
                        'dest_address_id': address}
 
66
                if sale_flow == 'direct_delivery':
 
67
                    vals['invoice_method'] = 'order'
 
68
                else:
 
69
                    vals['invoice_method'] = 'picking'
 
70
                return {'value': vals}
 
71
            else:
 
72
                warehouse = warehouse_obj.browse(cr, uid,
 
73
                                                 warehouse_id, context=context)
 
74
                company_partner = warehouse.company_id.partner_id
 
75
                address = company_partner.address_get(['delivery'])['delivery']
 
76
                vals = {'invoice_method': 'picking',
 
77
                        'location_id': warehouse.lot_input_id.id,
 
78
                        'dest_address_id': address}
 
79
                if sale_flow == 'direct_invoice':
 
80
                    vals['invoice_method'] = 'picking'
 
81
                return {'value': vals}
 
82
        return {}
 
83
 
 
84
    def action_picking_create(self, cr, uid, ids, context=None):
 
85
        res = super(purchase_order, self).action_picking_create(cr, uid, ids, context=context)
 
86
        picking_obj = self.pool.get('stock.picking')
 
87
        for purchase in self.browse(cr, uid, ids, context=context):
 
88
            # TODO bad code inherited from OpenERP, see bug https://bugs.launchpad.net/openobject-addons/+bug/788789
 
89
            if res:
 
90
                if purchase.sale_flow == 'direct_delivery':
 
91
                    if purchase.sale_id and purchase.sale_id.order_policy == 'picking':
 
92
                        invoice_control = '2binvoiced'
 
93
                    else:
 
94
                        invoice_control = 'none'
 
95
                    picking_obj.write(
 
96
                        cr, uid, res,
 
97
                        {'type': 'out',
 
98
                         'invoice_state': invoice_control,
 
99
                         'sale_id': purchase.sale_id and purchase.sale_id.id},
 
100
                        context=context)
 
101
                elif purchase.sale_flow == 'direct_invoice':
 
102
                    picking_obj.write(cr, uid, res,
 
103
                                      {'invoice_state': 'none'},
 
104
                                      context=context)
 
105
                elif purchase.sale_flow == 'direct_invoice_and_delivery':
 
106
                    picking_obj.write(
 
107
                        cr, uid, res,
 
108
                        {'type': 'out',
 
109
                         'invoice_state': 'none',
 
110
                         'sale_id': purchase.sale_id and purchase.sale_id.id},
 
111
                        context=context)
 
112
        return res