2010-01-18 10:43:33 +00:00
# -*- encoding: utf-8 -*-
##############################################################################
#
2010-04-09 00:32:46 +00:00
# OpenERP, Open Source Management Solution
2010-01-18 10:43:33 +00:00
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $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.
#
# 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, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields , osv
from osv import orm
import netsvc
import time
2010-04-09 07:18:17 +00:00
class purchase_requisition ( osv . osv ) :
_name = " purchase.requisition "
2010-05-19 18:32:32 +00:00
_description = " Purchase Requisition "
2010-01-18 10:43:33 +00:00
_columns = {
2010-04-09 07:18:17 +00:00
' name ' : fields . char ( ' Requisition Reference ' , size = 32 , required = True ) ,
2010-04-09 07:47:07 +00:00
' origin ' : fields . char ( ' Origin ' , size = 32 ) ,
' date_start ' : fields . datetime ( ' Requisition Date ' ) ,
' date_end ' : fields . datetime ( ' Requisition Deadline ' ) ,
2010-01-18 10:43:33 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
2010-04-09 07:47:07 +00:00
' exclusive ' : fields . selection ( [ ( ' exclusive ' , ' Purchase Tender (exclusive) ' ) , ( ' multiple ' , ' Multiple Requisitions ' ) ] , ' Requisition Type ' , help = " If the requisition is exclusive, it will cancel all purchase orders when you confirm one of them " , required = True ) ,
2010-01-18 10:43:33 +00:00
' description ' : fields . text ( ' Description ' ) ,
2010-04-23 07:24:58 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True ) ,
2010-04-09 07:18:17 +00:00
' purchase_ids ' : fields . one2many ( ' purchase.order ' , ' requisition_id ' , ' Purchase Orders ' ) ,
' line_ids ' : fields . one2many ( ' purchase.requisition.line ' , ' requisition_id ' , ' Products to Purchase ' ) ,
2010-04-21 10:10:09 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' in_progress ' , ' In Progress ' ) , ( ' cancel ' , ' Cancelled ' ) , ( ' done ' , ' Done ' ) ] , ' State ' , required = True )
2010-01-18 10:43:33 +00:00
}
_defaults = {
' date_start ' : lambda * args : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
2010-04-21 09:19:35 +00:00
' state ' : lambda * args : ' draft ' ,
2010-04-09 07:47:07 +00:00
' exclusive ' : lambda * args : ' multiple ' ,
2010-04-16 06:17:50 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , c ) . company_id . id ,
2010-04-23 07:24:58 +00:00
' user_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , c ) . id ,
2010-04-09 07:18:17 +00:00
' name ' : lambda obj , cr , uid , context : obj . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' purchase.order.requisition ' ) ,
2010-01-18 10:43:33 +00:00
}
2010-04-16 04:42:36 +00:00
2010-04-09 13:57:28 +00:00
def tender_cancel ( self , cr , uid , ids , context = { } ) :
2010-04-12 06:58:25 +00:00
purchase_order_obj = self . pool . get ( ' purchase.order ' )
for purchase in self . browse ( cr , uid , ids ) :
for purchase_id in purchase . purchase_ids :
if str ( purchase_id . state ) in ( ' draft ' , ' wait ' ) :
2010-04-23 07:24:58 +00:00
purchase_order_obj . action_cancel ( cr , uid , [ purchase_id . id ] )
2010-04-09 13:57:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' cancel ' } )
2010-04-23 07:24:58 +00:00
return True
2010-04-21 09:19:35 +00:00
def tender_in_progress ( self , cr , uid , ids , context = { } ) :
self . write ( cr , uid , ids , { ' state ' : ' in_progress ' } , context = context )
2010-04-23 07:24:58 +00:00
return True
2010-04-09 13:57:28 +00:00
def tender_reset ( self , cr , uid , ids , context = { } ) :
self . write ( cr , uid , ids , { ' state ' : ' draft ' } )
2010-04-23 07:24:58 +00:00
return True
2010-04-09 13:57:28 +00:00
def tender_done ( self , cr , uid , ids , context = { } ) :
2010-04-21 09:19:35 +00:00
self . write ( cr , uid , ids , { ' state ' : ' done ' , ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } , context = context )
2010-04-23 07:24:58 +00:00
return True
2010-01-18 10:43:33 +00:00
2010-04-09 07:18:17 +00:00
purchase_requisition ( )
2010-01-18 10:43:33 +00:00
2010-04-09 07:18:17 +00:00
class purchase_requisition_line ( osv . osv ) :
_name = " purchase.requisition.line "
_description = " Purchase Requisition Line "
2010-04-09 00:32:46 +00:00
_rec_name = ' product_id '
_columns = {
2010-05-04 12:54:03 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' ) ,
2010-04-09 00:32:46 +00:00
' product_uom_id ' : fields . many2one ( ' product.uom ' , ' Product UoM ' ) ,
2010-04-12 06:12:17 +00:00
' product_qty ' : fields . float ( ' Quantity ' , digits = ( 16 , 2 ) ) ,
2010-04-16 06:17:50 +00:00
' requisition_id ' : fields . many2one ( ' purchase.requisition ' , ' Purchase Requisition ' , ondelete = ' cascade ' ) ,
2010-04-12 05:15:29 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True ) ,
2010-04-09 07:18:17 +00:00
' requisition_id ' : fields . many2one ( ' purchase.requisition ' , ' Purchase Requisition ' , ondelete = ' cascade ' )
2010-04-09 00:32:46 +00:00
}
2010-04-12 10:23:28 +00:00
2010-05-05 12:48:59 +00:00
def onchange_product_id ( self , cr , uid , ids , product_id , product_uom_id , context = { } ) :
2010-04-23 07:24:58 +00:00
2010-04-09 13:57:28 +00:00
""" Changes UoM and name if product_id changes.
@param name : Name of the field
@param product_id : Changed product_id
@return : Dictionary of changed values
"""
2010-05-20 11:10:36 +00:00
value = { ' product_uom_id ' : ' ' }
2010-04-09 13:57:28 +00:00
if product_id :
prod = self . pool . get ( ' product.product ' ) . browse ( cr , uid , [ product_id ] ) [ 0 ]
value = { ' product_uom_id ' : prod . uom_id . id }
return { ' value ' : value }
2010-05-05 12:48:59 +00:00
2010-04-23 07:24:58 +00:00
_defaults = {
2010-04-12 05:15:29 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , c ) . company_id . id ,
}
2010-04-09 07:18:17 +00:00
purchase_requisition_line ( )
2010-04-09 00:32:46 +00:00
2010-01-18 10:43:33 +00:00
class purchase_order ( osv . osv ) :
_inherit = " purchase.order "
_columns = {
2010-04-09 07:18:17 +00:00
' requisition_id ' : fields . many2one ( ' purchase.requisition ' , ' Purchase Requisition ' )
2010-01-18 10:43:33 +00:00
}
def wkf_confirm_order ( self , cr , uid , ids , context = { } ) :
res = super ( purchase_order , self ) . wkf_confirm_order ( cr , uid , ids , context )
for po in self . browse ( cr , uid , ids , context ) :
2010-04-09 07:47:07 +00:00
if po . requisition_id and ( po . requisition_id . exclusive == ' exclusive ' ) :
2010-04-09 07:18:17 +00:00
for order in po . requisition_id . purchase_ids :
2010-01-18 10:43:33 +00:00
if order . id < > po . id :
wf_service = netsvc . LocalService ( " workflow " )
wf_service . trg_validate ( uid , ' purchase.order ' , order . id , ' purchase_cancel ' , cr )
2010-04-28 07:28:52 +00:00
self . pool . get ( ' purchase.requisition ' ) . write ( cr , uid , [ po . requisition_id . id ] , { ' state ' : ' done ' , ' date_end ' : time . strftime ( ' % Y- % m- %d % H: % M: % S ' ) } )
2010-04-16 04:42:36 +00:00
2010-01-18 10:43:33 +00:00
return res
2010-04-09 00:32:46 +00:00
2010-04-23 07:24:58 +00:00
purchase_order ( )
2010-04-09 00:32:46 +00:00
class product_product ( osv . osv ) :
_inherit = ' product.product '
_columns = {
2010-04-09 07:18:17 +00:00
' purchase_requisition ' : fields . boolean ( ' Purchase Requisition ' , help = " Check this box so that requisitions generates purchase requisitions instead of directly requests for quotations. " )
2010-04-09 00:32:46 +00:00
}
_defaults = {
2010-04-09 07:18:17 +00:00
' purchase_requisition ' : lambda * args : False
2010-04-09 00:32:46 +00:00
}
product_product ( )
class mrp_procurement ( osv . osv ) :
_inherit = ' mrp.procurement '
def make_po ( self , cr , uid , ids , context = { } ) :
2010-04-09 13:57:28 +00:00
sequence_obj = self . pool . get ( ' ir.sequence ' )
2010-04-09 00:32:46 +00:00
res = super ( mrp_procurement , self ) . make_po ( cr , uid , ids , context )
for proc_id , po_id in res . items ( ) :
procurement = self . browse ( cr , uid , proc_id )
2010-04-09 07:18:17 +00:00
if procurement . product_id . purchase_requisition :
self . pool . get ( ' purchase.requisition ' ) . create ( cr , uid , {
2010-04-21 09:19:35 +00:00
' name ' : sequence_obj . get ( cr , uid , ' purchase.order.requisition ' ) ,
2010-04-09 07:47:07 +00:00
' origin ' : procurement . name ,
' date_end ' : procurement . date_planned ,
2010-04-21 07:56:40 +00:00
' line_ids ' : [ ( 0 , 0 , {
2010-04-09 00:32:46 +00:00
' product_id ' : procurement . product_id . id ,
' product_uom_id ' : procurement . product_uom . id ,
' product_qty ' : procurement . product_qty
} ) ] ,
' purchase_ids ' : [ ( 6 , 0 , [ po_id ] ) ]
} )
2010-04-09 07:47:07 +00:00
return res
2010-04-09 00:32:46 +00:00
mrp_procurement ( )