1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2010 Akretion LDTA (<http://www.akretion.com>).
6
# @author Raphaël Valyi
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.
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.
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/>.
21
##############################################################################
22
from openerp.osv import fields, orm
25
class purchase_order_line(orm.Model):
26
_inherit = "purchase.order.line"
29
'sale_order_line_id': fields.many2one('sale.order.line', 'Sale Order Line'),
33
class purchase_order(orm.Model):
34
_inherit = "purchase.order"
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')],
44
help="Is this order tied to a sale order?"
45
" How will it be delivered and invoiced then?"),
49
'sale_flow': 'normal',
52
def sale_flow_change(self, cr, uid, ids, sale_flow, sale_id,
53
warehouse_id, context=None):
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,
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'
69
vals['invoice_method'] = 'picking'
70
return {'value': vals}
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}
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
90
if purchase.sale_flow == 'direct_delivery':
91
if purchase.sale_id and purchase.sale_id.order_policy == 'picking':
92
invoice_control = '2binvoiced'
94
invoice_control = 'none'
98
'invoice_state': invoice_control,
99
'sale_id': purchase.sale_id and purchase.sale_id.id},
101
elif purchase.sale_flow == 'direct_invoice':
102
picking_obj.write(cr, uid, res,
103
{'invoice_state': 'none'},
105
elif purchase.sale_flow == 'direct_invoice_and_delivery':
109
'invoice_state': 'none',
110
'sale_id': purchase.sale_id and purchase.sale_id.id},