261 lines
14 KiB
Python
261 lines
14 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from openerp import SUPERUSER_ID
|
|
from openerp.osv import fields, osv
|
|
from openerp.tools.translate import _
|
|
from openerp.exceptions import UserError
|
|
|
|
class stock_move(osv.osv):
|
|
_inherit = 'stock.move'
|
|
_columns = {
|
|
'purchase_line_id': fields.many2one('purchase.order.line',
|
|
'Purchase Order Line', ondelete='set null', select=True,
|
|
readonly=True),
|
|
}
|
|
|
|
def get_price_unit(self, cr, uid, move, context=None):
|
|
""" Returns the unit price to store on the quant """
|
|
if move.purchase_line_id:
|
|
return move.price_unit
|
|
|
|
return super(stock_move, self).get_price_unit(cr, uid, move, context=context)
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
if isinstance(ids, (int, long)):
|
|
ids = [ids]
|
|
res = super(stock_move, self).write(cr, uid, ids, vals, context=context)
|
|
from openerp import workflow
|
|
if vals.get('state') in ['done', 'cancel']:
|
|
po_to_check = []
|
|
for move in self.browse(cr, uid, ids, context=context):
|
|
if move.purchase_line_id and move.purchase_line_id.order_id:
|
|
order = move.purchase_line_id.order_id
|
|
order_id = order.id
|
|
# update linked purchase order as superuser as the warehouse
|
|
# user may not have rights to access purchase.order
|
|
if self.pool.get('purchase.order').test_moves_done(cr, uid, [order_id], context=context):
|
|
workflow.trg_validate(SUPERUSER_ID, 'purchase.order', order_id, 'picking_done', cr)
|
|
if self.pool.get('purchase.order').test_moves_except(cr, uid, [order_id], context=context):
|
|
workflow.trg_validate(SUPERUSER_ID, 'purchase.order', order_id, 'picking_cancel', cr)
|
|
if order_id not in po_to_check and vals['state'] == 'cancel' and order.invoice_method == 'picking':
|
|
po_to_check.append(order_id)
|
|
# Some moves which are cancelled might be part of a PO line which is partially
|
|
# invoiced, so we check if some PO line can be set on "invoiced = True".
|
|
if po_to_check:
|
|
self.pool.get('purchase.order')._set_po_lines_invoiced(cr, uid, po_to_check, context=context)
|
|
return res
|
|
|
|
def copy(self, cr, uid, id, default=None, context=None):
|
|
default = default or {}
|
|
context = context or {}
|
|
if not default.get('split_from'):
|
|
#we don't want to propagate the link to the purchase order line except in case of move split
|
|
default['purchase_line_id'] = False
|
|
return super(stock_move, self).copy(cr, uid, id, default, context)
|
|
|
|
def _get_master_data(self, cr, uid, move, inv_type, context=None):
|
|
if inv_type == 'in_invoice' and move.purchase_line_id:
|
|
purchase_order = move.purchase_line_id.order_id
|
|
return purchase_order.partner_id, purchase_order.create_uid.id, purchase_order.currency_id.id
|
|
if inv_type == 'in_refund' and move.origin_returned_move_id.purchase_line_id:
|
|
purchase_order = move.origin_returned_move_id.purchase_line_id.order_id
|
|
return purchase_order.partner_id, purchase_order.create_uid.id, purchase_order.currency_id.id
|
|
elif inv_type in ('in_invoice', 'in_refund') and move.picking_id:
|
|
# In case of an extra move, it is better to use the data from the original moves
|
|
for purchase_move in move.picking_id.move_lines:
|
|
if purchase_move.purchase_line_id:
|
|
purchase_order = purchase_move.purchase_line_id.order_id
|
|
return purchase_order.partner_id, purchase_order.create_uid.id, purchase_order.currency_id.id
|
|
|
|
partner = move.picking_id and move.picking_id.partner_id or False
|
|
code = self.get_code_from_locs(cr, uid, move, context=context)
|
|
if partner and code == 'incoming':
|
|
currency = purchase_order.currency_id.id
|
|
return partner, uid, currency
|
|
return super(stock_move, self)._get_master_data(cr, uid, move, inv_type, context=context)
|
|
|
|
def _get_invoice_line_vals(self, cr, uid, move, partner, inv_type, context=None):
|
|
res = super(stock_move, self)._get_invoice_line_vals(cr, uid, move, partner, inv_type, context=context)
|
|
purchase_line = False
|
|
if inv_type == 'in_invoice' and move.purchase_line_id:
|
|
purchase_line = move.purchase_line_id
|
|
elif inv_type == 'in_refund' and move.origin_returned_move_id.purchase_line_id:
|
|
purchase_line = move.origin_returned_move_id.purchase_line_id
|
|
if purchase_line:
|
|
res['invoice_line_tax_ids'] = [(6, 0, [x.id for x in purchase_line.taxes_id])]
|
|
res['price_unit'] = purchase_line.price_unit
|
|
res['account_analytic_id'] = purchase_line.account_analytic_id.id
|
|
res['purchase_line_ids'] = [(4, purchase_line.id)]
|
|
return res
|
|
|
|
def _get_moves_taxes(self, cr, uid, moves, inv_type, context=None):
|
|
is_extra_move, extra_move_tax = super(stock_move, self)._get_moves_taxes(cr, uid, moves, inv_type, context=context)
|
|
if inv_type == 'in_invoice':
|
|
for move in moves:
|
|
if move.purchase_line_id:
|
|
is_extra_move[move.id] = False
|
|
extra_move_tax[move.picking_id, move.product_id] = [(6, 0, [x.id for x in move.purchase_line_id.taxes_id])]
|
|
elif move.product_id.product_tmpl_id.supplier_taxes_id:
|
|
mov_id = self.search(cr, uid, [('purchase_line_id', '!=', False), ('picking_id', '=', move.picking_id.id)], limit=1, context=context)
|
|
if mov_id:
|
|
mov = self.browse(cr, uid, mov_id[0], context=context)
|
|
fp = mov.purchase_line_id.order_id.fiscal_position
|
|
res = self.pool.get("account.invoice.line").product_id_change(cr, uid, [], move.product_id.id, None, partner_id=move.picking_id.partner_id.id, fposition_id=(fp and fp.id), type='in_invoice', context=context)
|
|
extra_move_tax[0, move.product_id] = [(6, 0, res['value']['invoice_line_tax_ids'])]
|
|
return (is_extra_move, extra_move_tax)
|
|
|
|
def _get_taxes(self, cr, uid, move, context=None):
|
|
if move.origin_returned_move_id.purchase_line_id.taxes_id:
|
|
return [tax.id for tax in move.origin_returned_move_id.purchase_line_id.taxes_id]
|
|
return super(stock_move, self)._get_taxes(cr, uid, move, context=context)
|
|
|
|
class stock_picking(osv.osv):
|
|
_inherit = 'stock.picking'
|
|
|
|
def _get_to_invoice(self, cr, uid, ids, name, args, context=None):
|
|
res = {}
|
|
for picking in self.browse(cr, uid, ids, context=context):
|
|
res[picking.id] = False
|
|
for move in picking.move_lines:
|
|
if move.purchase_line_id and move.purchase_line_id.order_id.invoice_method == 'picking':
|
|
if not move.move_orig_ids:
|
|
res[picking.id] = True
|
|
return res
|
|
|
|
def _get_picking_to_recompute(self, cr, uid, ids, context=None):
|
|
picking_ids = set()
|
|
for move in self.pool.get('stock.move').browse(cr, uid, ids, context=context):
|
|
if move.picking_id and move.purchase_line_id:
|
|
picking_ids.add(move.picking_id.id)
|
|
return list(picking_ids)
|
|
|
|
_columns = {
|
|
'reception_to_invoice': fields.function(_get_to_invoice, type='boolean', string='Invoiceable on incoming shipment',
|
|
help='Does the picking contain some moves related to a purchase order invoiceable on the receipt?',
|
|
store={
|
|
'stock.move': (_get_picking_to_recompute, ['purchase_line_id', 'picking_id'], 10),
|
|
}),
|
|
}
|
|
|
|
def _get_invoice_vals(self, cr, uid, key, inv_type, journal_id, moves, context=None):
|
|
inv_vals = super(stock_picking, self)._get_invoice_vals(cr, uid, key, inv_type, journal_id, moves, context=context)
|
|
purchases = []
|
|
for move in moves:
|
|
if move.purchase_line_id or move.origin_returned_move_id.purchase_line_id:
|
|
purchase = move.purchase_line_id.order_id or move.origin_returned_move_id.purchase_line_id.order_id
|
|
inv_vals.update({
|
|
'fiscal_position_id': purchase.fiscal_position_id.id,
|
|
'payment_term_id': purchase.payment_term_id.id,
|
|
})
|
|
purchases.append(purchase.id)
|
|
if purchases:
|
|
inv_vals['purchase_ids'] = [(6, 0, list(set(purchases)))]
|
|
return inv_vals
|
|
|
|
def get_service_line_vals(self, cr, uid, moves, partner, inv_type, context=None):
|
|
purchase_obj = self.pool['purchase.order']
|
|
purchase_line_obj = self.pool['purchase.order.line']
|
|
res = super(stock_picking, self).get_service_line_vals(cr, uid, moves, partner, inv_type, context=context)
|
|
# Add service lines for purchases
|
|
if inv_type == 'in_invoice':
|
|
purchase_ids = list(set([x.purchase_line_id.order_id.id for x in moves if x.purchase_line_id]))
|
|
purchase_line_ids = purchase_line_obj.search(cr, uid, [('order_id', 'in', purchase_ids), ('invoice_lines', '=', False), '|', ('product_id', '=', False),
|
|
('product_id.type', '=', 'service')], context=context)
|
|
for po_line in purchase_line_obj.browse(cr, uid, purchase_line_ids, context=context):
|
|
acc_id = purchase_obj._choose_account_from_po_line(cr, uid, po_line, context=context)
|
|
inv_line_data = purchase_obj._prepare_inv_line(cr, uid, acc_id, po_line, context=context)
|
|
inv_line_data['purchase_line_ids'] = [(4, po_line.id)]
|
|
res += [(0, 0, inv_line_data)]
|
|
return res
|
|
|
|
|
|
class stock_warehouse(osv.osv):
|
|
_inherit = 'stock.warehouse'
|
|
_columns = {
|
|
'buy_to_resupply': fields.boolean('Purchase to resupply this warehouse',
|
|
help="When products are bought, they can be delivered to this warehouse"),
|
|
'buy_pull_id': fields.many2one('procurement.rule', 'Buy rule'),
|
|
}
|
|
_defaults = {
|
|
'buy_to_resupply': True,
|
|
}
|
|
|
|
def _get_buy_pull_rule(self, cr, uid, warehouse, context=None):
|
|
route_obj = self.pool.get('stock.location.route')
|
|
data_obj = self.pool.get('ir.model.data')
|
|
try:
|
|
buy_route_id = data_obj.get_object_reference(cr, uid, 'purchase', 'route_warehouse0_buy')[1]
|
|
except:
|
|
buy_route_id = route_obj.search(cr, uid, [('name', 'like', _('Buy'))], context=context)
|
|
buy_route_id = buy_route_id and buy_route_id[0] or False
|
|
if not buy_route_id:
|
|
raise UserError(_('Can\'t find any generic Buy route.'))
|
|
|
|
return {
|
|
'name': self._format_routename(cr, uid, warehouse, _(' Buy'), context=context),
|
|
'location_id': warehouse.in_type_id.default_location_dest_id.id,
|
|
'route_id': buy_route_id,
|
|
'action': 'buy',
|
|
'picking_type_id': warehouse.in_type_id.id,
|
|
'warehouse_id': warehouse.id,
|
|
'group_propagation_option': 'none',
|
|
}
|
|
|
|
def create_routes(self, cr, uid, ids, warehouse, context=None):
|
|
pull_obj = self.pool.get('procurement.rule')
|
|
res = super(stock_warehouse, self).create_routes(cr, uid, ids, warehouse, context=context)
|
|
if warehouse.buy_to_resupply:
|
|
buy_pull_vals = self._get_buy_pull_rule(cr, uid, warehouse, context=context)
|
|
buy_pull_id = pull_obj.create(cr, uid, buy_pull_vals, context=context)
|
|
res['buy_pull_id'] = buy_pull_id
|
|
return res
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
pull_obj = self.pool.get('procurement.rule')
|
|
if isinstance(ids, (int, long)):
|
|
ids = [ids]
|
|
|
|
if 'buy_to_resupply' in vals:
|
|
if vals.get("buy_to_resupply"):
|
|
for warehouse in self.browse(cr, uid, ids, context=context):
|
|
if not warehouse.buy_pull_id:
|
|
buy_pull_vals = self._get_buy_pull_rule(cr, uid, warehouse, context=context)
|
|
buy_pull_id = pull_obj.create(cr, uid, buy_pull_vals, context=context)
|
|
vals['buy_pull_id'] = buy_pull_id
|
|
else:
|
|
for warehouse in self.browse(cr, uid, ids, context=context):
|
|
if warehouse.buy_pull_id:
|
|
buy_pull_id = pull_obj.unlink(cr, uid, warehouse.buy_pull_id.id, context=context)
|
|
return super(stock_warehouse, self).write(cr, uid, ids, vals, context=None)
|
|
|
|
def get_all_routes_for_wh(self, cr, uid, warehouse, context=None):
|
|
all_routes = super(stock_warehouse, self).get_all_routes_for_wh(cr, uid, warehouse, context=context)
|
|
if warehouse.buy_to_resupply and warehouse.buy_pull_id and warehouse.buy_pull_id.route_id:
|
|
all_routes += [warehouse.buy_pull_id.route_id.id]
|
|
return all_routes
|
|
|
|
def _get_all_products_to_resupply(self, cr, uid, warehouse, context=None):
|
|
res = super(stock_warehouse, self)._get_all_products_to_resupply(cr, uid, warehouse, context=context)
|
|
if warehouse.buy_pull_id and warehouse.buy_pull_id.route_id:
|
|
for product_id in res:
|
|
for route in self.pool.get('product.product').browse(cr, uid, product_id, context=context).route_ids:
|
|
if route.id == warehouse.buy_pull_id.route_id.id:
|
|
res.remove(product_id)
|
|
break
|
|
return res
|
|
|
|
def _handle_renaming(self, cr, uid, warehouse, name, code, context=None):
|
|
res = super(stock_warehouse, self)._handle_renaming(cr, uid, warehouse, name, code, context=context)
|
|
pull_obj = self.pool.get('procurement.rule')
|
|
#change the buy procurement rule name
|
|
if warehouse.buy_pull_id:
|
|
pull_obj.write(cr, uid, warehouse.buy_pull_id.id, {'name': warehouse.buy_pull_id.name.replace(warehouse.name, name, 1)}, context=context)
|
|
return res
|
|
|
|
def change_route(self, cr, uid, ids, warehouse, new_reception_step=False, new_delivery_step=False, context=None):
|
|
res = super(stock_warehouse, self).change_route(cr, uid, ids, warehouse, new_reception_step=new_reception_step, new_delivery_step=new_delivery_step, context=context)
|
|
if warehouse.in_type_id.default_location_dest_id != warehouse.buy_pull_id.location_id:
|
|
self.pool.get('procurement.rule').write(cr, uid, warehouse.buy_pull_id.id, {'location_id': warehouse.in_type_id.default_location_dest_id.id}, context=context)
|
|
return res
|