import openerp.addons.decimal_precision as dp
from openerp.osv.orm import browse_record, browse_null
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP
+from openerp.tools.float_utils import float_compare
class purchase_order(osv.osv):
def _invoiced(self, cursor, user, ids, name, arg, context=None):
res = {}
for purchase in self.browse(cursor, user, ids, context=context):
- invoiced = False
- if purchase.invoiced_rate == 100.00:
- invoiced = True
- res[purchase.id] = invoiced
+ res[purchase.id] = all(line.invoiced for line in purchase.order_line)
return res
def _get_journal(self, cr, uid, context=None):
'picking_ids': fields.one2many('stock.picking.in', 'purchase_id', 'Picking List', readonly=True, help="This is the list of incoming shipments that have been generated for this purchase order."),
'shipped':fields.boolean('Received', readonly=True, select=True, help="It indicates that a picking has been done"),
'shipped_rate': fields.function(_shipped_rate, string='Received Ratio', type='float'),
- 'invoiced': fields.function(_invoiced, string='Invoice Received', type='boolean', help="It indicates that an invoice has been paid"),
+ 'invoiced': fields.function(_invoiced, string='Invoice Received', type='boolean', help="It indicates that an invoice has been validated"),
'invoiced_rate': fields.function(_invoiced_rate, string='Invoiced', type='float'),
'invoice_method': fields.selection([('manual','Based on Purchase Order lines'),('order','Based on generated draft invoice'),('picking','Based on incoming shipments')], 'Invoicing Control', required=True,
readonly=True, states={'draft':[('readonly',False)], 'sent':[('readonly',False)]},
_name = "purchase.order"
_inherit = ['mail.thread', 'ir.needaction_mixin']
_description = "Purchase Order"
- _order = "name desc"
+ _order = 'date_order desc, id desc'
def create(self, cr, uid, vals, context=None):
if vals.get('name','/')=='/':
if not acc_id:
acc_id = po_line.product_id.categ_id.property_account_expense_categ.id
if not acc_id:
- raise osv.except_osv(_('Error!'), _('Define expense account for this company: "%s" (id:%d).') % (po_line.product_id.name, po_line.product_id.id,))
+ raise osv.except_osv(_('Error!'), _('Define expense account for this product: "%s" (id:%d).') % (po_line.product_id.name, po_line.product_id.id,))
else:
- acc_id = property_obj.get(cr, uid, 'property_account_expense_categ', 'product.category').id
+ acc_id = property_obj.get(cr, uid, 'property_account_expense_categ', 'product.category', context=context).id
fpos = po_line.order_id.fiscal_position or False
return fiscal_obj.map_account(cr, uid, fpos, acc_id)
if not len(ids):
return False
self.write(cr, uid, ids, {'state':'draft','shipped':0})
+ for purchase in self.browse(cr, uid, ids, context=context):
+ self.pool['purchase.order.line'].write(cr, uid, [l.id for l in purchase.order_line], {'state': 'draft'})
wf_service = netsvc.LocalService("workflow")
for p_id in ids:
# Deleting the existing instance of workflow for PO
:return: ID of created invoice.
:rtype: int
"""
- res = False
-
+ if context is None:
+ context = {}
journal_obj = self.pool.get('account.journal')
inv_obj = self.pool.get('account.invoice')
inv_line_obj = self.pool.get('account.invoice.line')
+ res = False
+ uid_company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
for order in self.browse(cr, uid, ids, context=context):
+ context.pop('force_company', None)
+ if order.company_id.id != uid_company_id:
+ #if the company of the document is different than the current user company, force the company in the context
+ #then re-do a browse to read the property fields for the good company.
+ context['force_company'] = order.company_id.id
+ order = self.browse(cr, uid, order.id, context=context)
pay_acc_id = order.partner_id.property_account_payable.id
- journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', order.company_id.id)], limit=1)
+ journal_ids = journal_obj.search(cr, uid, [('type', '=', 'purchase'), ('company_id', '=', order.company_id.id)], limit=1)
if not journal_ids:
raise osv.except_osv(_('Error!'),
_('Define purchase journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
inv_line_id = inv_line_obj.create(cr, uid, inv_line_data, context=context)
inv_lines.append(inv_line_id)
- po_line.write({'invoiced':True, 'invoice_lines': [(4, inv_line_id)]}, context=context)
+ po_line.write({'invoice_lines': [(4, inv_line_id)]}, context=context)
# get invoice data and create invoice
inv_data = {
_('You must first cancel all receptions related to this purchase order.'))
if inv:
wf_service.trg_validate(uid, 'account.invoice', inv.id, 'invoice_cancel', cr)
+ self.pool['purchase.order.line'].write(cr, uid, [l.id for l in purchase.order_line],
+ {'state': 'cancel'})
self.write(cr,uid,ids,{'state':'cancel'})
for (id, name) in self.name_get(cr, uid, ids):
'name': self.pool.get('ir.sequence').get(cr, uid, 'stock.picking.in'),
'origin': order.name + ((order.origin and (':' + order.origin)) or ''),
'date': self.date_to_datetime(cr, uid, order.date_order, context),
- 'partner_id': order.dest_address_id.id or order.partner_id.id,
+ 'partner_id': order.partner_id.id,
'invoice_state': '2binvoiced' if order.invoice_method == 'picking' else 'none',
'type': 'in',
- 'partner_id': order.dest_address_id.id or order.partner_id.id,
'purchase_id': order.id,
'company_id': order.company_id.id,
'move_lines' : [],
}
def _prepare_order_line_move(self, cr, uid, order, order_line, picking_id, context=None):
+ price_unit = order_line.price_unit
+ if order.currency_id.id != order.company_id.currency_id.id:
+ #we don't round the price_unit, as we may want to store the standard price with more digits than allowed by the currency
+ price_unit = self.pool.get('res.currency').compute(cr, uid, order.currency_id.id, order.company_id.currency_id.id, price_unit, round=False, context=context)
return {
'name': order_line.name or '',
'product_id': order_line.product_id.id,
'type':'in',
'purchase_line_id': order_line.id,
'company_id': order.company_id.id,
- 'price_unit': order_line.price_unit
+ 'price_unit': price_unit
}
def _create_pickings(self, cr, uid, order, order_lines, picking_id=False, context=None):
continue
if order_line.product_id.type in ('product', 'consu'):
move = stock_move.create(cr, uid, self._prepare_order_line_move(cr, uid, order, order_line, picking_id, context=context))
- if order_line.move_dest_id:
+ if order_line.move_dest_id and order_line.move_dest_id.state != 'done':
order_line.move_dest_id.write({'location_id': order.location_id.id})
todo_moves.append(move)
stock_move.action_confirm(cr, uid, todo_moves)
'invoiced':False,
'invoice_ids': [],
'picking_ids': [],
+ 'partner_ref': '',
'name': self.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
})
return super(purchase_order, self).copy(cr, uid, id, default, context)
if porder.notes:
order_infos['notes'] = (order_infos['notes'] or '') + ('\n%s' % (porder.notes,))
if porder.origin:
- order_infos['origin'] = (order_infos['origin'] or '') + ' ' + porder.origin
+ if not porder.origin in order_infos['origin'] and not order_infos['origin'] in porder.origin:
+ order_infos['origin'] = (order_infos['origin'] or '') + ' ' + porder.origin
for order_line in porder.order_line:
line_key = make_key(order_line, ('name', 'date_planned', 'taxes_id', 'price_unit', 'product_id', 'move_dest_id', 'account_analytic_id'))
res[line.id] = cur_obj.round(cr, uid, cur, taxes['total'])
return res
+ def _get_uom_id(self, cr, uid, context=None):
+ try:
+ proxy = self.pool.get('ir.model.data')
+ result = proxy.get_object_reference(cr, uid, 'product', 'product_uom_unit')
+ return result[1]
+ except Exception, ex:
+ return False
+
_columns = {
'name': fields.text('Description', required=True),
'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True),
}
_defaults = {
+ 'product_uom' : _get_uom_id,
'product_qty': lambda *a: 1.0,
'state': lambda *args: 'draft',
'invoiced': lambda *a: 0,
default.update({'state':'draft', 'move_ids':[],'invoiced':0,'invoice_lines':[]})
return super(purchase_order_line, self).copy_data(cr, uid, id, default, context)
+ def unlink(self, cr, uid, ids, context=None):
+ procurement_ids_to_cancel = []
+ for line in self.browse(cr, uid, ids, context=context):
+ if line.state not in ['draft', 'cancel']:
+ raise osv.except_osv(_('Invalid Action!'), _('Cannot delete a purchase order line which is in state \'%s\'.') %(line.state,))
+ if line.move_dest_id:
+ procurement_ids_to_cancel.extend(procurement.id for procurement in line.move_dest_id.procurements)
+ if procurement_ids_to_cancel:
+ self.pool['procurement.order'].action_cancel(cr, uid, procurement_ids_to_cancel)
+ return super(purchase_order_line, self).unlink(cr, uid, ids, context=context)
+
def onchange_product_uom(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, context=None):
"""
onchange handler of product_uom.
"""
+ if context is None:
+ context = {}
if not uom_id:
return {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'product_uom' : uom_id or False}}
+ context = dict(context, purchase_uom_check=True)
return self.onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned,
name=name, price_unit=price_unit, context=context)
uom_id = product_uom_po_id
if product.uom_id.category_id.id != product_uom.browse(cr, uid, uom_id, context=context).category_id.id:
- if self._check_product_uom_group(cr, uid, context=context):
+ if context.get('purchase_uom_check') and self._check_product_uom_group(cr, uid, context=context):
res['warning'] = {'title': _('Warning!'), 'message': _('Selected Unit of Measure does not belong to the same category as the product Unit of Measure.')}
uom_id = product_uom_po_id
supplierinfo = False
+ precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Product Unit of Measure')
for supplier in product.seller_ids:
if partner_id and (supplier.name.id == partner_id):
supplierinfo = supplier
if supplierinfo.product_uom.id != uom_id:
res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier only sells this product by %s') % supplierinfo.product_uom.name }
min_qty = product_uom._compute_qty(cr, uid, supplierinfo.product_uom.id, supplierinfo.min_qty, to_uom_id=uom_id)
- if (qty or 0.0) < min_qty: # If the supplier quantity is greater than entered from user, set minimal.
+ if float_compare(min_qty , qty, precision_digits=precision) == 1: # If the supplier quantity is greater than entered from user, set minimal.
if qty:
res['warning'] = {'title': _('Warning!'), 'message': _('The selected supplier has a minimal quantity set to %s %s, you should not purchase less.') % (supplierinfo.min_qty, supplierinfo.product_uom.name)}
qty = min_qty
If none match, returns then first warehouse of the company
"""
# TODO refactor the domain once we implement the "parent_of" domain operator
+ # NOTE This method has been copied in the `purchase_requisition` module to ensure
+ # retro-compatibility. This code duplication will be deleted in next stable version.
+ # Do not forget to update both version in case of modification.
company_id = (procurement.company_id or user_company).id
domains = [
[
wf_service.trg_validate(uid, 'purchase.order', context['default_res_id'], 'send_rfq', cr)
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
+class account_invoice(osv.Model):
+ _inherit = 'account.invoice'
+
+ def invoice_validate(self, cr, uid, ids, context=None):
+ res = super(account_invoice, self).invoice_validate(cr, uid, ids, context=context)
+ purchase_order_obj = self.pool.get('purchase.order')
+ # read access on purchase.order object is not required
+ if not purchase_order_obj.check_access_rights(cr, uid, 'read', raise_exception=False):
+ user_id = SUPERUSER_ID
+ else:
+ user_id = uid
+ po_ids = purchase_order_obj.search(cr, user_id, [('invoice_ids', 'in', ids)], context=context)
+ wf_service = netsvc.LocalService("workflow")
+ for order in purchase_order_obj.browse(cr, uid, po_ids, context=context):
+ # Signal purchase order workflow that an invoice has been validated.
+ invoiced = []
+ for po_line in order.order_line:
+ if any(line.invoice_id.state not in ['draft', 'cancel'] for line in po_line.invoice_lines):
+ invoiced.append(po_line.id)
+ if invoiced:
+ self.pool['purchase.order.line'].write(cr, uid, invoiced, {'invoiced': True})
+ wf_service.trg_write(uid, 'purchase.order', order.id, cr)
+ return res
+
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: