# -*- encoding: utf-8 -*-
##############################################################################
#
-# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
+# OpenERP, Open Source Management Solution
+# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+# $Id$
#
-# $Id$
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
#
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
res[order.id] += oline.price_unit * oline.product_qty
return res
- def _amount_untaxed(self, cr, uid, ids, field_name, arg, context):
- res = {}
- cur_obj=self.pool.get('res.currency')
- for purchase in self.browse(cr, uid, ids):
- res[purchase.id] = 0.0
- for line in purchase.order_line:
- res[purchase.id] += line.price_subtotal
- cur = purchase.pricelist_id.currency_id
- res[purchase.id] = cur_obj.round(cr, uid, cur, res[purchase.id])
-
- return res
-
- def _amount_tax(self, cr, uid, ids, field_name, arg, context):
+ def _amount_all(self, cr, uid, ids, field_name, arg, context):
res = {}
cur_obj=self.pool.get('res.currency')
for order in self.browse(cr, uid, ids):
- val = 0.0
+ res[order.id] = {
+ 'amount_untaxed': 0.0,
+ 'amount_tax': 0.0,
+ 'amount_total': 0.0,
+ }
+ val = val1 = 0.0
cur=order.pricelist_id.currency_id
for line in order.order_line:
for c in self.pool.get('account.tax').compute(cr, uid, line.taxes_id, line.price_unit, line.product_qty, order.partner_address_id.id, line.product_id, order.partner_id):
- val+= cur_obj.round(cr, uid, cur, c['amount'])
- res[order.id]=cur_obj.round(cr, uid, cur, val)
- return res
-
- def _amount_total(self, cr, uid, ids, field_name, arg, context):
- res = {}
- untax = self._amount_untaxed(cr, uid, ids, field_name, arg, context)
- tax = self._amount_tax(cr, uid, ids, field_name, arg, context)
- cur_obj=self.pool.get('res.currency')
- for id in ids:
- order=self.browse(cr, uid, [id])[0]
- cur=order.pricelist_id.currency_id
- res[id] = cur_obj.round(cr, uid, cur, untax.get(id, 0.0) + tax.get(id, 0.0))
+ val+= c['amount']
+ val1 += line.price_subtotal
+ res[order.id]['amount_tax']=cur_obj.round(cr, uid, cur, val)
+ res[order.id]['amount_untaxed']=cur_obj.round(cr, uid, cur, val1)
+ res[order.id]['amount_total']=res[order.id]['amount_untaxed'] + res[order.id]['amount_tax']
return res
def _set_minimum_planned_date(self, cr, uid, ids, name, value, arg, context):
cr.execute("""update purchase_order_line set
date_planned=%s
where
- order_id=%d and
+ order_id=%s and
(date_planned=%s or date_planned<%s)""", (value,po.id,po.minimum_planned_date,value))
return True
res[r] = 100.0 * res[r][0] / res[r][1]
return res
+ def _get_order(self, cr, uid, ids, context={}):
+ result = {}
+ for line in self.pool.get('purchase.order.line').browse(cr, uid, ids, context=context):
+ result[line.order_id.id] = True
+ return result.keys()
+
_columns = {
'name': fields.char('Order Reference', size=64, required=True, select=True),
- 'origin': fields.char('Origin', size=64,
+ 'origin': fields.char('Origin', size=64,
help="Reference of the document that generated this purchase order request."
),
'partner_ref': fields.char('Partner Ref.', size=64),
'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}, help="The pricelist sets the currency used for this purchase order. It also computes the supplier price for the selected products/quantities."),
'state': fields.selection([('draft', 'Request for Quotation'), ('wait', 'Waiting'), ('confirmed', 'Confirmed'), ('approved', 'Approved'),('except_picking', 'Shipping Exception'), ('except_invoice', 'Invoice Exception'), ('done', 'Done'), ('cancel', 'Cancelled')], 'Order Status', readonly=True, help="The state of the purchase order or the quotation request. A quotation is a purchase order in a 'Draft' state. Then the order has to be confirmed by the user, the state switch to 'Confirmed'. Then the supplier must confirm the order to change the state to 'Approved'. When the purchase order is paid and received, the state becomes 'Done'. If a cancel action occurs in the invoice or in the reception of goods, the state becomes in exception.", select=True),
- 'order_line': fields.one2many('purchase.order.line', 'order_id', 'Order Lines', states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
+ 'order_line': fields.one2many('purchase.order.line', 'order_id', 'Order Lines', states={'approved':[('readonly',True)]}),
'validator' : fields.many2one('res.users', 'Validated by', readonly=True),
'notes': fields.text('Notes'),
'invoice_id': fields.many2one('account.invoice', 'Invoice', readonly=True),
"Manual: no invoice will be pre-generated. The accountant will have to encode manually."
),
'minimum_planned_date':fields.function(_minimum_planned_date, fnct_inv=_set_minimum_planned_date, method=True,store=True, string='Planned Date', type='datetime', help="This is computed as the minimum scheduled date of all purchase order lines' products."),
- 'amount_untaxed': fields.function(_amount_untaxed, method=True, string='Untaxed Amount'),
- 'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
- 'amount_total': fields.function(_amount_total, method=True, string='Total'),
+ 'amount_untaxed': fields.function(_amount_all, method=True, string='Untaxed Amount',
+ store={
+ 'purchase.order.line': (_get_order, None, 10),
+ }, multi="sums"),
+ 'amount_tax': fields.function(_amount_all, method=True, string='Taxes',
+ store={
+ 'purchase.order.line': (_get_order, None, 10),
+ }, multi="sums"),
+ 'amount_total': fields.function(_amount_all, method=True, string='Total',
+ store={
+ 'purchase.order.line': (_get_order, None, 10),
+ }, multi="sums"),
+ 'fiscal_position': fields.many2one('account.fiscal.position', 'Fiscal Position')
}
_defaults = {
'date_order': lambda *a: time.strftime('%Y-%m-%d'),
_description = "Purchase order"
_order = "name desc"
+ def unlink(self, cr, uid, ids, context=None):
+ purchase_orders = self.read(cr, uid, ids, ['state'])
+ unlink_ids = []
+ for s in purchase_orders:
+ if s['state'] in ['draft','cancel']:
+ unlink_ids.append(s['id'])
+ else:
+ raise osv.except_osv(_('Invalid action !'), _('Cannot delete Purchase Order(s) which are in %s State!' % s['state']))
+ return super(purchase_order, self).unlink(cr, uid, unlink_ids, context=context)
+
def button_dummy(self, cr, uid, ids, context={}):
return True
def onchange_partner_id(self, cr, uid, ids, part):
if not part:
- return {'value':{'partner_address_id': False}}
+ return {'value':{'partner_address_id': False, 'fiscal_position': False}}
addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['default'])
- pricelist = self.pool.get('res.partner').property_get(cr, uid,
- part,property_pref=['property_product_pricelist_purchase']).get('property_product_pricelist_purchase',False)
- return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist}}
+ part = self.pool.get('res.partner').browse(cr, uid, part)
+ pricelist = part.property_product_pricelist_purchase.id
+ fiscal_position = part.property_account_position and part.property_account_position.id or False
+ return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist, 'fiscal_position': fiscal_position}}
- def wkf_approve_order(self, cr, uid, ids):
+ def wkf_approve_order(self, cr, uid, ids, context={}):
self.write(cr, uid, ids, {'state': 'approved', 'date_approve': time.strftime('%Y-%m-%d')})
return True
})
def inv_line_create(self,a,ol):
return (0, False, {
- 'name': ol.name,
- 'account_id': a,
- 'price_unit': ol.price_unit or 0.0,
- 'quantity': ol.product_qty,
- 'product_id': ol.product_id.id or False,
- 'uos_id': ol.product_uom.id or False,
- 'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
- 'account_analytic_id': ol.account_analytic_id.id,
- })
+ 'name': ol.name,
+ 'account_id': a,
+ 'price_unit': ol.price_unit or 0.0,
+ 'quantity': ol.product_qty,
+ 'product_id': ol.product_id.id or False,
+ 'uos_id': ol.product_uom.id or False,
+ 'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
+ 'account_analytic_id': ol.account_analytic_id.id,
+ })
+
+ def action_cancel_draft(self, cr, uid, ids, *args):
+ if not len(ids):
+ return False
+ self.write(cr, uid, ids, {'state':'draft','shipped':0})
+ wf_service = netsvc.LocalService("workflow")
+ for p_id in ids:
+ wf_service.trg_create(uid, 'purchase.order', p_id, cr)
+ return True
def action_invoice_create(self, cr, uid, ids, *args):
res = False
+ journal_obj = self.pool.get('account.journal')
for o in self.browse(cr, uid, ids):
il = []
for ol in o.order_line:
raise osv.except_osv(_('Error !'), _('There is no expense account defined for this product: "%s" (id:%d)') % (ol.product_id.name, ol.product_id.id,))
else:
a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category')
+ fpos = o.fiscal_position or False
+ a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
il.append(self.inv_line_create(a,ol))
-# il.append((0, False, {
-# 'name': ol.name,
-# 'account_id': a,
-# 'price_unit': ol.price_unit or 0.0,
-# 'quantity': ol.product_qty,
-# 'product_id': ol.product_id.id or False,
-# 'uos_id': ol.product_uom.id or False,
-# 'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
-# 'account_analytic_id': ol.account_analytic_id.id,
-# }))
-
- a = o.partner_id.property_account_payable.id
+
+ a = o.partner_id.property_account_payable.id
+ journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase')], limit=1)
inv = {
'name': o.partner_ref or o.name,
'reference': "P%dPO%d" % (o.partner_id.id, o.id),
'currency_id': o.pricelist_id.currency_id.id,
'address_invoice_id': o.partner_address_id.id,
'address_contact_id': o.partner_address_id.id,
+ 'journal_id': len(journal_ids) and journal_ids[0] or False,
'origin': o.name,
'invoice_line': il,
}
return True
return False
+ def action_cancel(self, cr, uid, ids, context={}):
+ ok = True
+ purchase_order_line_obj = self.pool.get('purchase.order.line')
+ for purchase in self.browse(cr, uid, ids):
+ for pick in purchase.picking_ids:
+ if pick.state not in ('draft','cancel'):
+ raise osv.except_osv(
+ _('Could not cancel purchase order !'),
+ _('You must first cancel all packing attached to this purchase order.'))
+ for pick in purchase.picking_ids:
+ wf_service = netsvc.LocalService("workflow")
+ wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_cancel', cr)
+ inv = purchase.invoice_id
+ if inv and inv.state not in ('cancel','draft'):
+ raise osv.except_osv(
+ _('Could not cancel this purchase order !'),
+ _('You must first cancel all invoices attached to this purchase order.'))
+ if inv:
+ wf_service = netsvc.LocalService("workflow")
+ wf_service.trg_validate(uid, 'account.invoice', inv.id, 'invoice_cancel', cr)
+ self.write(cr,uid,ids,{'state':'cancel'})
+ return True
+
def action_picking_create(self,cr, uid, ids, *args):
picking_id = False
for order in self.browse(cr, uid, ids):
_table = 'purchase_order_line'
_name = 'purchase.order.line'
_description = 'Purchase Order lines'
- def copy(self, cr, uid, id, default=None,context={}):
+ def copy_data(self, cr, uid, id, default=None,context={}):
if not default:
default = {}
default.update({'state':'draft', 'move_id':False})
- return super(purchase_order_line, self).copy(cr, uid, id, default, context)
+ return super(purchase_order_line, self).copy_data(cr, uid, id, default, context)
def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
- partner_id, date_order=False):
- if not product:
- return {'value':{}}
- prod= self.pool.get('product.product').browse(cr, uid,product)
+ partner_id, date_order=False, fiscal_position=False):
if not pricelist:
raise osv.except_osv(_('No Pricelist !'), _('You have to select a pricelist in the purchase form !\nPlease set one before choosing a product.'))
+ if not partner_id:
+ raise osv.except_osv(_('No Partner!'), _('You have to select a partner in the purchase form !\nPlease set one partner before choosing a product.'))
if not product:
return {'value': {'price_unit': 0.0, 'name':'','notes':'', 'product_uom' : False}, 'domain':{'product_uom':[]}}
+ prod= self.pool.get('product.product').browse(cr, uid,product)
lang=False
if partner_id:
lang=self.pool.get('res.partner').read(cr, uid, partner_id)['lang']
context={'lang':lang}
+ context['partner_id'] = partner_id
- prod = self.pool.get('product.product').read(cr, uid, product, ['supplier_taxes_id','name','seller_delay','uom_po_id','description_purchase'],context=context)
- prod_uom_po = prod['uom_po_id'][0]
+ prod = self.pool.get('product.product').browse(cr, uid, product, context=context)
+ prod_uom_po = prod.uom_po_id.id
if not uom:
uom = prod_uom_po
if not date_order:
'uom': uom,
'date': date_order,
})[pricelist]
- dt = (DateTime.now() + DateTime.RelativeDateTime(days=prod['seller_delay'] or 0.0)).strftime('%Y-%m-%d %H:%M:%S')
- prod_name = self.pool.get('product.product').name_get(cr, uid, [product], context=context)[0][1]
- res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':prod['supplier_taxes_id'], 'date_planned': dt,'notes':prod['description_purchase'], 'product_uom': uom}}
+ qty = 1
+ seller_delay = 0
+ for s in prod.seller_ids:
+ seller_delay = s.delay
+ if s.name.id == partner_id:
+ seller_delay = s.delay
+ qty = s.qty
+ dt = (DateTime.now() + DateTime.RelativeDateTime(days=seller_delay or 0.0)).strftime('%Y-%m-%d %H:%M:%S')
+ prod_name = prod.partner_ref
+
+
+ res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':map(lambda x: x.id, prod.supplier_taxes_id),
+ 'date_planned': dt,'notes':prod.description_purchase,
+ 'product_qty': qty,
+ 'product_uom': uom}}
domain = {}
-
- taxes = self.pool.get('account.tax').browse(cr, uid,prod['supplier_taxes_id'])
- taxep = None
- if partner_id:
- taxep_id = self.pool.get('res.partner').property_get(cr, uid,partner_id,property_pref=['property_account_supplier_tax']).get('property_account_supplier_tax',False)
- if taxep_id:
- taxep=self.pool.get('account.tax').browse(cr, uid,taxep_id)
- if not taxep or not taxep.id:
- res['value']['taxes_id'] = [x.id for x in taxes]
- else:
- res5 = [taxep.id]
- for t in taxes:
- if not t.tax_group==taxep.tax_group:
- res5.append(t.id)
- res['value']['taxes_id'] = res5
+ partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
+ taxes = self.pool.get('account.tax').browse(cr, uid,map(lambda x: x.id, prod.supplier_taxes_id))
+ fpos = fiscal_position and self.pool.get('account.fiscal.position').browse(cr, uid, fiscal_position) or False
+ res['value']['taxes_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, taxes)
res2 = self.pool.get('product.uom').read(cr, uid, [uom], ['category_id'])
- res3 = self.pool.get('product.uom').read(cr, uid, [prod_uom_po], ['category_id'])
+ res3 = prod.uom_id.category_id.id
domain = {'product_uom':[('category_id','=',res2[0]['category_id'][0])]}
- if res2[0]['category_id'] != res3[0]['category_id']:
+ if res2[0]['category_id'][0] != res3:
raise osv.except_osv(_('Wrong Product UOM !'), _('You have to select a product UOM in the same category than the purchase UOM of the product'))
res['domain'] = domain