<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    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.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from mx import DateTime

from osv import osv
import netsvc
import pooler

class procurement_order(osv.osv):
    _inherit = 'procurement.order'

    def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
        '''
        use_new_cursor: False or the dbname
        '''
        if not context:
            context = {}

        if use_new_cursor:
            cr = pooler.get_db(use_new_cursor).cursor()
        wf_service = netsvc.LocalService("workflow")

        procurement_obj = self.pool.get('procurement.order')
        if not ids:
            ids = procurement_obj.search(cr, uid, [], order="date_planned")
        for id in ids:
            wf_service.trg_validate(uid, 'procurement.order', id, 'button_restart', cr)
        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:
            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))
            ids = map(lambda x: x[0], cr.fetchall())
            for proc in procurement_obj.browse(cr, uid, ids):
                if (maxdate.strftime('%Y-%m-%d') >= proc.date_planned):
                    wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
                else:
                    offset += 1
                    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 = []
            ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
            for proc in procurement_obj.browse(cr, uid, ids):
                if ((maxdate).strftime('%Y-%m-%d') >= proc.date_planned) :
                    wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
                    report_ids.append(proc.id)
                else:
                    report_later += 1
                report_total += 1
            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,))
                    report_except += 1
            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
    Computation Finished; %s

    Total procurement: %d
    Exception procurement: %d
    Not run now procurement: %d

    Exceptions;
    '''% (start_date, end_date, report_total, report_except, report_later)
            summary += '\n'.join(report)
            request.create(cr, uid,
                {'name': "Procurement calculation report.",
                    'act_from': uid,
                    'act_to': uid,
                    'body': summary,
                })
        if use_new_cursor:
            cr.commit()
            cr.close()
        return {}

    def create_automatic_op(self, cr, uid, context=None):
        if not context:
            context = {}
        product_obj = self.pool.get('product.product')
        proc_obj = self.pool.get('procurement.order')
        warehouse_obj = self.pool.get('stock.warehouse')
        wf_service = netsvc.LocalService("workflow")

        warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)

        cr.execute('select id from product_product')
        products_id = [x for x, in cr.fetchall()]

        for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
            context['warehouse'] = warehouse
            for product in product_obj.browse(cr, uid, products_id, context=context):
                if product.virtual_available >= 0.0:
                    continue

                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
                else:
                    continue
                proc_id = proc_obj.create(cr, uid, {
                    'name': 'Automatic OP: %s' % product.name,
                    'origin': 'SCHEDULER',
                    '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,
                    'location_id': location_id,
                    'procure_method': 'make_to_order',
                    })
                wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
                wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)

    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:
            context = {}
        if use_new_cursor:
            cr = pooler.get_db(use_new_cursor).cursor()<commit_msg>[IMP] procurement: Add the doc string<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    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.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from mx import DateTime

from osv import osv
import netsvc
import pooler

class procurement_order(osv.osv):
    _inherit = 'procurement.order'

    def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
        '''
        Call the scheduler to check the procurement order
        
        @param self: The object pointer
        @param cr: The current row, from the database cursor,
        @param uid: The current user ID for security checks
        @param ids: List of selected IDs
        @param use_new_cursor: False or the dbname
        @param context: A standard dictionary for contextual values
        @return:  Dictionary of values        
        '''
        if not context:
            context = {}

        if use_new_cursor:
            cr = pooler.get_db(use_new_cursor).cursor()
        wf_service = netsvc.LocalService("workflow")

        procurement_obj = self.pool.get('procurement.order')
        if not ids:
            ids = procurement_obj.search(cr, uid, [], order="date_planned")
        for id in ids:
            wf_service.trg_validate(uid, 'procurement.order', id, 'button_restart', cr)
        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:
            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))
            ids = map(lambda x: x[0], cr.fetchall())
            for proc in procurement_obj.browse(cr, uid, ids):
                if (maxdate.strftime('%Y-%m-%d') >= proc.date_planned):
                    wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
                else:
                    offset += 1
                    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 = []
            ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
            for proc in procurement_obj.browse(cr, uid, ids):
                if ((maxdate).strftime('%Y-%m-%d') >= proc.date_planned) :
                    wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
                    report_ids.append(proc.id)
                else:
                    report_later += 1
                report_total += 1
            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,))
                    report_except += 1
            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
    Computation Finished; %s

    Total procurement: %d
    Exception procurement: %d
    Not run now procurement: %d

    Exceptions;
    '''% (start_date, end_date, report_total, report_except, report_later)
            summary += '\n'.join(report)
            request.create(cr, uid,
                {'name': "Procurement calculation report.",
                    'act_from': uid,
                    'act_to': uid,
                    'body': summary,
                })
        if use_new_cursor:
            cr.commit()
            cr.close()
        return {}

    def create_automatic_op(self, cr, uid, context=None):
        """
        Create procurement of  virtual stock < 0
        
        @param self: The object pointer
        @param cr: The current row, from the database cursor,
        @param uid: The current user ID for security checks
        @param context: A standard dictionary for contextual values
        @return:  Dictionary of values
        """
        if not context:
            context = {}
        product_obj = self.pool.get('product.product')
        proc_obj = self.pool.get('procurement.order')
        warehouse_obj = self.pool.get('stock.warehouse')
        wf_service = netsvc.LocalService("workflow")

        warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)

        cr.execute('select id from product_product')
        products_id = [x for x, in cr.fetchall()]

        for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
            context['warehouse'] = warehouse
            for product in product_obj.browse(cr, uid, products_id, context=context):
                if product.virtual_available >= 0.0:
                    continue

                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
                else:
                    continue
                proc_id = proc_obj.create(cr, uid, {
                    'name': 'Automatic OP: %s' % product.name,
                    'origin': 'SCHEDULER',
                    '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,
                    'location_id': location_id,
                    'procure_method': 'make_to_order',
                    })
                wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
                wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)

    def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
            use_new_cursor=False, context=None, user_id=False):
        '''
        Create procurement based on Orderpoint
        use_new_cursor: False or the dbname
        
        @param self: The object pointer
        @param cr: The current row, from the database cursor,
        @param user_id: The current user ID for security checks
        @param context: A standard dictionary for contextual values
        @param param: False or the dbname
        @return:  Dictionary of values
        """        
        '''
        if not context:
            context = {}
        if use_new_cursor:
            cr = pooler.get_db(use_new_cursor).cursor()
{'fraction_non_alphanumeric': 0.12245156388501963, 'fraction_numerical': 0.0043687476256806385, 'mean_word_length': 1.9120575221238938, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5877717', 'n_tokens_mistral': 4581, 'n_tokens_neox': 4254, 'n_words': 1173}