fixed bug:308803
[odoo/odoo.git] / addons / purchase / purchase.py
index 6c1c814..514dbba 100644 (file)
@@ -1,30 +1,22 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
 #
-# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
+#    OpenERP, Open Source Management Solution  
+#    Copyright (C) 2004-2008 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/>.
 #
 ##############################################################################
 
@@ -71,7 +63,7 @@ class purchase_order(osv.osv):
             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'])
+                    val+= c['amount']
             res[order.id]=cur_obj.round(cr, uid, cur, val)
         return res
 
@@ -94,7 +86,7 @@ class purchase_order(osv.osv):
             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
 
@@ -154,21 +146,26 @@ class purchase_order(osv.osv):
 
     _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),
         'date_order':fields.date('Date Ordered', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
-        'date_approve':fields.date('Date Approved'),
+        'date_approve':fields.date('Date Approved', readonly=1),
         'partner_id':fields.many2one('res.partner', 'Supplier', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}, change_default=True),
         'partner_address_id':fields.many2one('res.partner.address', 'Address', required=True, states={'posted':[('readonly',True)]}),
 
-        'dest_address_id':fields.many2one('res.partner.address', 'Destination Address', states={'posted':[('readonly',True)]}),
+        'dest_address_id':fields.many2one('res.partner.address', 'Destination Address', states={'posted':[('readonly',True)]},
+            help="Put an address if you want to deliver directly from the supplier to the customer." \
+                "In this case, it will remove the warehouse link and set the customer location."
+        ),
         'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', states={'posted':[('readonly',True)]}),
         'location_id': fields.many2one('stock.location', 'Destination', required=True),
 
         '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),
@@ -177,7 +174,12 @@ class purchase_order(osv.osv):
         'shipped_rate': fields.function(_shipped_rate, method=True, string='Received', type='float'),
         'invoiced':fields.boolean('Invoiced & Paid', readonly=True, select=True),
         'invoiced_rate': fields.function(_invoiced_rate, method=True, string='Invoiced', type='float'),
-        'invoice_method': fields.selection([('manual','Manual'),('order','From order'),('picking','From picking')], 'Invoicing Control', required=True),
+        'invoice_method': fields.selection([('manual','Manual'),('order','From Order'),('picking','From Picking')], 'Invoicing Control', required=True,
+            help="From Order: a draft invoice will be pre-generated based on the purchase order. The accountant " \
+                "will just have to validate this invoice for control.\n" \
+                "From Picking: a draft invoice will be pre-genearted based on validated receptions.\n" \
+                "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'),
@@ -196,7 +198,17 @@ class purchase_order(osv.osv):
     _name = "purchase.order"
     _description = "Purchase order"
     _order = "name desc"
-
+    
+    def unlink(self, cr, uid, ids):
+        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 osv.osv.unlink(self, cr, uid, unlink_ids)        
+    
     def button_dummy(self, cr, uid, ids, context={}):
         return True
 
@@ -217,10 +229,11 @@ class purchase_order(osv.osv):
         if not part:
             return {'value':{'partner_address_id': False}}
         addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['default'])
-        pricelist = self.pool.get('res.partner').browse(cr, uid, part).property_product_pricelist_purchase.id
+        part = self.pool.get('res.partner').browse(cr, uid, part)
+        pricelist = part.property_product_pricelist_purchase.id
         return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist}}
 
-    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
 
@@ -253,15 +266,24 @@ class purchase_order(osv.osv):
                        })
     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
@@ -277,17 +299,8 @@ class purchase_order(osv.osv):
                         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')
+                a = self.pool.get('account.fiscal.position').map_account(cr, uid, o.partner_id, 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
             inv = {
@@ -316,6 +329,29 @@ class purchase_order(osv.osv):
                     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 packings 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):
@@ -411,14 +447,16 @@ class purchase_order_line(osv.osv):
         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 product:
-            return {'value': {'price_unit': 0.0, 'name':'','notes':''}, 'domain':{'product_uom':[]}}
+            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])[0]['lang']
+            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'])[0]
-        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:
@@ -428,32 +466,32 @@ class purchase_order_line(osv.osv):
                     '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 = {}
 
-        if res['value']['taxes_id']:
-            taxes = self.pool.get('account.tax').browse(cr, uid,
-                    [x.id for x in product.supplier_taxes_id])
-            taxep = None
-            if partner_id:
-                taxep = self.pool.get('res.partner').browse(cr, uid,
-                        partner_id).property_account_supplier_tax
-            if not taxep or not taxep.id:
-                res['value']['taxes_id'] = [x.id for x in product.taxes_id]
-            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))
+        res['value']['taxes_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, partner, 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