2009-10-14 11:15:34 +00:00
# -*- coding: utf-8 -*-
2008-09-09 09:23:59 +00:00
##############################################################################
2009-11-13 05:41:16 +00:00
#
2009-07-27 09:07:43 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-09-09 09:23:59 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2008-09-09 09:23:59 +00:00
#
2008-11-03 19:18:56 +00:00
# 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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2008-09-09 09:23:59 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-13 05:41:16 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-09-09 09:23:59 +00:00
#
##############################################################################
2010-06-24 08:18:20 +00:00
import time
from mx import DateTime
2008-09-09 09:23:59 +00:00
from osv import osv
import netsvc
import pooler
2009-07-27 09:07:43 +00:00
2010-05-27 12:47:06 +00:00
class procurement_order ( osv . osv ) :
_inherit = ' procurement.order '
2009-07-27 09:07:43 +00:00
2008-09-09 09:23:59 +00:00
def _procure_confirm ( self , cr , uid , ids = None , use_new_cursor = False , context = None ) :
'''
use_new_cursor : False or the dbname
'''
if not context :
2009-07-27 09:07:43 +00:00
context = { }
2008-09-09 09:23:59 +00:00
if use_new_cursor :
2010-06-24 08:18:20 +00:00
cr = pooler . get_db ( use_new_cursor ) . cursor ( )
2008-09-09 09:23:59 +00:00
wf_service = netsvc . LocalService ( " workflow " )
2010-05-27 12:47:06 +00:00
procurement_obj = self . pool . get ( ' procurement.order ' )
2008-09-09 09:23:59 +00:00
if not ids :
2009-07-27 09:07:43 +00:00
ids = procurement_obj . search ( cr , uid , [ ] , order = " date_planned " )
2008-09-09 09:23:59 +00:00
for id in ids :
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , id , ' button_restart ' , cr )
2008-09-09 09:23:59 +00:00
if use_new_cursor :
cr . commit ( )
company = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id
maxdate = DateTime . now ( ) + DateTime . RelativeDateTime ( days = company . schedule_range )
start_date = time . strftime ( ' % Y- % m- %d , % Hh % Mm % Ss ' )
offset = 0
report = [ ]
report_total = 0
report_except = 0
report_later = 0
while True :
2010-05-27 12:47:06 +00:00
cr . execute ( ' select id from procurement_order where state= %s and procure_method= %s order by priority,date_planned limit 500 offset %s ' , ( ' confirmed ' , ' make_to_order ' , offset ) )
2009-07-27 09:07:43 +00:00
ids = map ( lambda x : x [ 0 ] , cr . fetchall ( ) )
2008-09-09 09:23:59 +00:00
for proc in procurement_obj . browse ( cr , uid , ids ) :
2010-04-06 08:57:06 +00:00
if ( maxdate . strftime ( ' % Y- % m- %d ' ) > = proc . date_planned ) :
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc . id , ' button_check ' , cr )
2008-09-09 09:23:59 +00:00
else :
2009-07-27 09:07:43 +00:00
offset + = 1
2008-09-09 09:23:59 +00:00
report_later + = 1
for proc in procurement_obj . browse ( cr , uid , ids ) :
if proc . state == ' exception ' :
report . append ( ' PROC %d : on order - %3.2f %-5s - %s ' % \
( proc . id , proc . product_qty , proc . product_uom . name ,
proc . product_id . name ) )
report_except + = 1
report_total + = 1
if use_new_cursor :
cr . commit ( )
if not ids :
break
offset = 0
ids = [ ]
while True :
report_ids = [ ]
2010-04-06 08:57:06 +00:00
ids = procurement_obj . search ( cr , uid , [ ( ' state ' , ' = ' , ' confirmed ' ) , ( ' procure_method ' , ' = ' , ' make_to_stock ' ) ] , offset = offset )
2008-09-09 09:23:59 +00:00
for proc in procurement_obj . browse ( cr , uid , ids ) :
2010-01-18 08:37:37 +00:00
if ( ( maxdate ) . strftime ( ' % Y- % m- %d ' ) > = proc . date_planned ) :
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc . id , ' button_check ' , cr )
2008-09-09 09:23:59 +00:00
report_ids . append ( proc . id )
else :
2009-07-27 09:07:43 +00:00
report_later + = 1
report_total + = 1
2008-09-09 09:23:59 +00:00
for proc in procurement_obj . browse ( cr , uid , report_ids ) :
if proc . state == ' exception ' :
report . append ( ' PROC %d : from stock - %3.2f %-5s - %s ' % \
( proc . id , proc . product_qty , proc . product_uom . name ,
proc . product_id . name , ) )
2009-07-27 09:07:43 +00:00
report_except + = 1
2008-09-09 09:23:59 +00:00
if use_new_cursor :
cr . commit ( )
offset + = len ( ids )
if not ids : break
end_date = time . strftime ( ' % Y- % m- %d , % Hh % Mm % Ss ' )
if uid :
request = self . pool . get ( ' res.request ' )
summary = ''' Here is the procurement scheduling report.
Computation Started ; % s
2009-01-27 11:15:46 +00:00
Computation Finished ; % s
2008-09-09 09:23:59 +00:00
Total procurement : % d
Exception procurement : % d
Not run now procurement : % d
Exceptions ;
2009-07-27 09:07:43 +00:00
''' % (start_date, end_date, report_total, report_except, report_later)
2008-09-09 09:23:59 +00:00
summary + = ' \n ' . join ( report )
request . create ( cr , uid ,
2010-04-09 07:14:08 +00:00
{ ' name ' : " Procurement calculation report. " ,
2009-07-27 09:07:43 +00:00
' act_from ' : uid ,
' act_to ' : uid ,
2008-09-09 09:23:59 +00:00
' body ' : summary ,
} )
if use_new_cursor :
cr . commit ( )
cr . close ( )
return { }
2009-07-27 09:07:43 +00:00
def create_automatic_op ( self , cr , uid , context = None ) :
2008-09-09 09:23:59 +00:00
if not context :
2009-07-27 09:07:43 +00:00
context = { }
2008-09-09 09:23:59 +00:00
product_obj = self . pool . get ( ' product.product ' )
2010-05-27 12:47:06 +00:00
proc_obj = self . pool . get ( ' procurement.order ' )
2008-09-09 09:23:59 +00:00
warehouse_obj = self . pool . get ( ' stock.warehouse ' )
wf_service = netsvc . LocalService ( " workflow " )
2008-12-07 13:06:32 +00:00
warehouse_ids = warehouse_obj . search ( cr , uid , [ ] , context = context )
2008-09-09 09:23:59 +00:00
cr . execute ( ' select id from product_product ' )
products_id = [ x for x , in cr . fetchall ( ) ]
2008-12-07 13:06:32 +00:00
for warehouse in warehouse_obj . browse ( cr , uid , warehouse_ids , context = context ) :
context [ ' warehouse ' ] = warehouse
2010-04-06 08:57:06 +00:00
for product in product_obj . browse ( cr , uid , products_id , context = context ) :
2009-07-27 09:07:43 +00:00
if product . virtual_available > = 0.0 :
2008-09-09 09:23:59 +00:00
continue
2008-12-07 13:06:32 +00:00
newdate = DateTime . now ( )
if product . supply_method == ' buy ' :
location_id = warehouse . lot_input_id . id
elif product . supply_method == ' produce ' :
location_id = warehouse . lot_stock_id . id
2008-09-09 09:23:59 +00:00
else :
continue
proc_id = proc_obj . create ( cr , uid , {
2009-04-07 06:26:45 +00:00
' name ' : ' Automatic OP: %s ' % product . name ,
2008-09-09 09:23:59 +00:00
' origin ' : ' SCHEDULER ' ,
2008-12-07 13:06:32 +00:00
' date_planned ' : newdate . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
' product_id ' : product . id ,
' product_qty ' : - product . virtual_available ,
' product_uom ' : product . uom_id . id ,
2008-09-09 09:23:59 +00:00
' location_id ' : location_id ,
' procure_method ' : ' make_to_order ' ,
} )
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id , ' button_confirm ' , cr )
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id , ' button_check ' , cr )
2008-09-09 09:23:59 +00:00
def _procure_orderpoint_confirm ( self , cr , uid , automatic = False , \
use_new_cursor = False , context = None , user_id = False ) :
'''
use_new_cursor : False or the dbname
'''
if not context :
2009-07-27 09:07:43 +00:00
context = { }
2008-09-09 09:23:59 +00:00
if use_new_cursor :
2010-06-24 08:18:20 +00:00
cr = pooler . get_db ( use_new_cursor ) . cursor ( )
2008-09-09 09:23:59 +00:00
orderpoint_obj = self . pool . get ( ' stock.warehouse.orderpoint ' )
location_obj = self . pool . get ( ' stock.location ' )
2010-05-27 12:47:06 +00:00
procurement_obj = self . pool . get ( ' procurement.order ' )
2008-09-09 09:23:59 +00:00
request_obj = self . pool . get ( ' res.request ' )
wf_service = netsvc . LocalService ( " workflow " )
report = [ ]
offset = 0
ids = [ 1 ]
if automatic :
2008-09-26 10:25:10 +00:00
self . create_automatic_op ( cr , uid , context = context )
2008-09-09 09:23:59 +00:00
while ids :
2009-07-27 09:07:43 +00:00
ids = orderpoint_obj . search ( cr , uid , [ ] , offset = offset , limit = 100 )
2008-09-09 09:23:59 +00:00
for op in orderpoint_obj . browse ( cr , uid , ids ) :
2010-05-31 12:49:26 +00:00
if op . procurement_id . state != ' exception ' :
if op . procurement_id and op . procurement_id . purchase_id and op . procurement_id . purchase_id . state in ( ' draft ' , ' confirmed ' ) :
continue
2008-11-25 20:35:31 +00:00
prods = location_obj . _product_virtual_get ( cr , uid ,
op . location_id . id , [ op . product_id . id ] ,
{ ' uom ' : op . product_uom . id } ) [ op . product_id . id ]
if prods < op . product_min_qty :
qty = max ( op . product_min_qty , op . product_max_qty ) - prods
reste = qty % op . qty_multiple
2009-07-27 09:07:43 +00:00
if reste > 0 :
2010-06-24 08:18:20 +00:00
qty + = op . qty_multiple - reste
2008-11-25 20:35:31 +00:00
newdate = DateTime . now ( ) + DateTime . RelativeDateTime (
2010-05-19 05:43:42 +00:00
days = int ( op . product_id . seller_delay ) )
2008-11-25 20:35:31 +00:00
if op . product_id . supply_method == ' buy ' :
location_id = op . warehouse_id . lot_input_id
elif op . product_id . supply_method == ' produce ' :
location_id = op . warehouse_id . lot_stock_id
else :
continue
2009-07-27 09:07:43 +00:00
if qty < = 0 :
2008-12-22 15:56:31 +00:00
continue
2009-01-22 11:53:34 +00:00
if op . product_id . type not in ( ' consu ' ) :
proc_id = procurement_obj . create ( cr , uid , {
2009-07-27 09:07:43 +00:00
' name ' : ' OP: ' + str ( op . id ) ,
2009-01-22 11:53:34 +00:00
' date_planned ' : newdate . strftime ( ' % Y- % m- %d ' ) ,
' product_id ' : op . product_id . id ,
' product_qty ' : qty ,
' product_uom ' : op . product_uom . id ,
' location_id ' : op . warehouse_id . lot_input_id . id ,
' procure_method ' : ' make_to_order ' ,
' origin ' : op . name
} )
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id ,
2009-01-22 11:53:34 +00:00
' button_confirm ' , cr )
2010-05-27 12:47:06 +00:00
wf_service . trg_validate ( uid , ' procurement.order ' , proc_id ,
2009-01-22 11:53:34 +00:00
' button_check ' , cr )
orderpoint_obj . write ( cr , uid , [ op . id ] ,
{ ' procurement_id ' : proc_id } )
2008-09-09 09:23:59 +00:00
offset + = len ( ids )
if use_new_cursor :
cr . commit ( )
if user_id and report :
request_obj . create ( cr , uid , {
' name ' : ' Orderpoint report. ' ,
' act_from ' : user_id ,
' act_to ' : user_id ,
' body ' : ' \n ' . join ( report )
} )
if use_new_cursor :
cr . commit ( )
cr . close ( )
return { }
2010-06-24 08:18:20 +00:00
2010-05-27 12:47:06 +00:00
procurement_order ( )
2010-04-29 13:30:07 +00:00
2009-03-06 22:18:24 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: