<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/>.
#
##############################################################################

from openerp import netsvc
import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
from openerp.osv import osv,fields
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP

class pur_req_po_line(osv.osv_memory):
    _name = "pur.req.po.line"
    _rec_name = 'product_id'

    _columns = {
        'wizard_id' : fields.many2one('pur.req.po', string="Wizard"),
        'product_id': fields.many2one('product.product', 'Product' ,required=True),
        'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'product_qty_remain': fields.float('Quantity Remain', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'price_unit': fields.float('Unit Price', digits_compute= dp.get_precision('Product Price')),
        'product_uom_id': fields.many2one('product.uom', 'Product Unit of Measure',required=True),
        'date_required': fields.date('Date Required',required=True),
        'inv_qty': fields.float('Inventory'),
        'req_reason': fields.char('Reason and use',size=64),
        'req_line_id':fields.many2one('pur.req.line', 'Purchase Requisition'),
        'supplier_prod_id': fields.integer(string='Supplier Product ID', required=False),
        'supplier_prod_name': fields.char(string='Supplier Product Name', required=True),
        'supplier_prod_code': fields.char(string='Supplier Product Code', required=False),
        'supplier_delay' : fields.integer(string='Supplier Lead Time', required=False),
        
        'uom_po_qty': fields.float('PO Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'uom_po_qty_remain': fields.float('PO Quantity Remain', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'uom_po_price': fields.float('PO Unit Price', digits_compute=dp.get_precision('Product Price')),
        'uom_po_id': fields.many2one('product.uom', 'PO Unit of Measure', required=True, ),
        'uom_po_factor': fields.float('UOM Ratio',digits=(12,4),) 
    }
    def _check_product_qty(self, cursor, user, ids, context=None):
        for line in self.browse(cursor, user, ids, context=context):
#            if line.product_qty > line.product_qty_remain:
#                raise osv.except_osv(_('Warning!'), _("Product '%s' max po quantity is %s, you can not purchae %s"%(line.product_id.default_code + '-' + line.product_id.name, line.product_qty_remain, line.product_qty)))
            if line.uom_po_qty > line.uom_po_qty_remain:
                raise osv.except_osv(_('Warning!'), _("Product '%s' max po quantity is %s, you can not purchae %s"%(line.product_id.default_code + '-' + line.product_id.name, line.uom_po_qty_remain, line.uom_po_qty)))
        return True    
    _constraints = [(_check_product_qty,'Product quantity exceeds the remaining quantity',['product_qty'])]
        
    def onchange_lead(self, cr, uid, ids, change_type, changes_value, context=None):
        date_order = fields.date.context_today(self,cr,uid,context=context)
        res = {'value':{}}
        if change_type == 'date_required':
            supplier_delay = datetime.strptime(changes_value, DEFAULT_SERVER_DATE_FORMAT) - datetime.strptime(date_order, DEFAULT_SERVER_DATE_FORMAT)
            res['value']={'supplier_delay':supplier_delay.days}
        if change_type == 'supplier_delay':
            date_required = datetime.strptime(date_order, DEFAULT_SERVER_DATE_FORMAT) + relativedelta(days=changes_value)
            date_required = datetime.strftime(date_required,DEFAULT_SERVER_DATE_FORMAT)
            res['value'].update({'date_required':date_required})
        return res    

    #write the product supplier information
    def _update_prod_supplier(self,cr,uid,ids,vals,context=None):
        if vals.has_key('supplier_prod_name') or vals.has_key('supplier_prod_code') or vals.has_key('supplier_delay'):
            prod_supp_obj = self.pool.get('product.supplierinfo')
            new_vals = {'min_qty':0}
            if vals.has_key('supplier_prod_name'):
                new_vals.update({'product_name':vals['supplier_prod_name']})
            if vals.has_key('supplier_prod_code'):
                new_vals.update({'product_code':vals['supplier_prod_code']})
            if vals.has_key('supplier_delay'):
                new_vals.update({'delay':vals['supplier_delay']})
            #for the metro_currency module, set the currency
            user = self.pool.get("res.users").browse(cr,uid,uid,context=context)
            if ids:
                #from order line update
                for line in self.browse(cr,uid,ids,context=context):
                    new_vals.update({'name':line.wizard_id.partner_id.id,'product_id':line.product_id.product_tmpl_id.id,'currency':user.company_id.currency_id.id})
                    if line.supplier_prod_id:
                        #update the prodcut supplier info
                        prod_supp_obj.write(cr,uid,line.supplier_prod_id,new_vals,context=context)
                    else:
                        supplier_prod_id = prod_supp_obj.create(cr,uid,new_vals,context=context)     
            else:
                # from order line create
                po = self.pool.get('pur.req.po').browse(cr,uid,vals['wizard_id'])
                product = self.pool.get('product.product').browse(cr, uid, vals['product_id'], context=context)
                new_vals.update({'name':po.partner_id.id,'product_id':product.product_tmpl_id.id,'currency':user.company_id.currency_id.id})
                prod_supp_ids = prod_supp_obj.search(cr,uid,[('product_id','=',new_vals['product_id']),('name','=',new_vals['name'])])
                if prod_supp_ids and len(prod_supp_ids) > 0:
                    #update the prodcut supplier info
                    prod_supp_obj.write(cr,uid,prod_supp_ids[0],new_vals,context=context)
                else:
                    supplier_prod_id = prod_supp_obj.create(cr,uid,new_vals,context=context)  
                    
    def create(self, cr, user, vals, context=None):
        #check the product supplier info
        if not vals.has_key('supplier_prod_name') or vals['supplier_prod_name'] == '':
            product_name = self.pool.get("product.product").read(cr,user,[vals['product_id']],['name'],context=context)[0]['name']
            raise osv.except_osv(_('Error!'),
                                 _('The product supplier name is required to product .\n %s'%product_name))     

        resu = super(pur_req_po_line,self).create(cr, user, vals, context=context)
        #update product supplier info
        self._update_prod_supplier(cr, user, [], vals, context)
        #if price_unit changed then update it to product_product.standard_price
        if vals.has_key('price_unit'):
            self.pool.get('product.product').write(cr,user,[vals['product_id']],{'standard_price':vals['price_unit']},context=context)        
        return resu 
                        
    def write(self, cr, uid, ids, vals, context=None):
        if not ids:
            return True
        #update product supplier info
        self._update_prod_supplier(cr, uid, ids, vals, context)
        #if price_unit changed then update it to product_product.standard_price
        if vals.has_key('price_unit'):
            self.pool.get('product.product').write(cr,uid,[pur_req_po_line.product_id.id],{'standard_price':vals['price_unit']},context=context)                    
pur_req_po_line()


class pur_req_po(osv.osv_memory):
    _name = 'pur.req.po'
    _description = 'Requisition\'s Purchase Order'
    _columns = {
        'line_ids' : fields.one2many('pur.req.po.line', 'wizard_id', 'Prodcuts'),
        'partner_id': fields.many2one('res.partner', 'Supplier', required=True,domain=[('supplier', '=', True)]),
    }

    def default_get(self, cr, uid, fields, context=None):
        """
         To get default values for the object.
         @param self: The object pointer.
         @param cr: A database cursor
         @param uid: ID of the user currently logged in
         @param fields: List of fields for which we want default values
         @param context: A standard dictionary
         @return: A dictionary with default values for all field in ``fields``
        """
        line_data = []
        if context is None:
            context = {}
        res = super(pur_req_po, self).default_get(cr, uid, fields, context=context)
        record_id = context and context.get('active_id', False) or False
        req_obj = self.pool.get('pur.req')
        req = req_obj.browse(cr, uid, record_id, context=context)
        partner_id = None
        if req:             
            uom_obj = self.pool.get('product.uom')
            for line in req.line_ids:
                if partner_id == None:
                    partner_id = line.product_id.seller_id.id
                if not line.generated_po:
                    uom_po_qty = uom_obj._compute_qty_obj(cr, uid, line.product_uom_id, line.product_qty_remain, line.product_id.uom_po_id, context=context)
                    uom_po_factor = line.product_id.uom_po_factor/line.product_uom_id.factor_display
                    line_data.append({'product_id': line.product_id.id, 
                                      'product_qty_remain': line.product_qty_remain,
                                      'product_qty': line.product_qty_remain, 
                                      'product_uom_id':line.product_uom_id.id, 
                                      'price_unit':line.product_id.standard_price,
                                      
                                      'uom_po_qty_remain': uom_po_qty,
                                      'uom_po_qty': uom_po_qty,
                                      'uom_po_id':line.product_id.uom_po_id.id, 
                                      'uom_po_price':line.product_id.uom_po_price, 
                                      'uom_po_factor':uom_po_factor,
                                      
                                      'inv_qty':line.product_id.qty_available,
                                      'date_required': line.date_required,
                                      'req_line_id':line.id, 
                                      'req_reason':line.req_reason,
                                    })         
                    
            res.update({'line_ids': line_data,'partner_id':partner_id})
        return res

    def view_init(self, cr, uid, fields_list, context=None):<commit_msg>Improve the pur req partner geting logic<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/>.
#
##############################################################################

from openerp import netsvc
import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
from openerp.osv import osv,fields
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, DATETIME_FORMATS_MAP

class pur_req_po_line(osv.osv_memory):
    _name = "pur.req.po.line"
    _rec_name = 'product_id'

    _columns = {
        'wizard_id' : fields.many2one('pur.req.po', string="Wizard"),
        'product_id': fields.many2one('product.product', 'Product' ,required=True),
        'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'product_qty_remain': fields.float('Quantity Remain', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'price_unit': fields.float('Unit Price', digits_compute= dp.get_precision('Product Price')),
        'product_uom_id': fields.many2one('product.uom', 'Product Unit of Measure',required=True),
        'date_required': fields.date('Date Required',required=True),
        'inv_qty': fields.float('Inventory'),
        'req_reason': fields.char('Reason and use',size=64),
        'req_line_id':fields.many2one('pur.req.line', 'Purchase Requisition'),
        'supplier_prod_id': fields.integer(string='Supplier Product ID', required=False),
        'supplier_prod_name': fields.char(string='Supplier Product Name', required=True),
        'supplier_prod_code': fields.char(string='Supplier Product Code', required=False),
        'supplier_delay' : fields.integer(string='Supplier Lead Time', required=False),
        
        'uom_po_qty': fields.float('PO Quantity', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'uom_po_qty_remain': fields.float('PO Quantity Remain', digits_compute=dp.get_precision('Product Unit of Measure'),required=True),
        'uom_po_price': fields.float('PO Unit Price', digits_compute=dp.get_precision('Product Price')),
        'uom_po_id': fields.many2one('product.uom', 'PO Unit of Measure', required=True, ),
        'uom_po_factor': fields.float('UOM Ratio',digits=(12,4),) 
    }
    def _check_product_qty(self, cursor, user, ids, context=None):
        for line in self.browse(cursor, user, ids, context=context):
#            if line.product_qty > line.product_qty_remain:
#                raise osv.except_osv(_('Warning!'), _("Product '%s' max po quantity is %s, you can not purchae %s"%(line.product_id.default_code + '-' + line.product_id.name, line.product_qty_remain, line.product_qty)))
            if line.uom_po_qty > line.uom_po_qty_remain:
                raise osv.except_osv(_('Warning!'), _("Product '%s' max po quantity is %s, you can not purchae %s"%(line.product_id.default_code + '-' + line.product_id.name, line.uom_po_qty_remain, line.uom_po_qty)))
        return True    
    _constraints = [(_check_product_qty,'Product quantity exceeds the remaining quantity',['product_qty'])]
        
    def onchange_lead(self, cr, uid, ids, change_type, changes_value, context=None):
        date_order = fields.date.context_today(self,cr,uid,context=context)
        res = {'value':{}}
        if change_type == 'date_required':
            supplier_delay = datetime.strptime(changes_value, DEFAULT_SERVER_DATE_FORMAT) - datetime.strptime(date_order, DEFAULT_SERVER_DATE_FORMAT)
            res['value']={'supplier_delay':supplier_delay.days}
        if change_type == 'supplier_delay':
            date_required = datetime.strptime(date_order, DEFAULT_SERVER_DATE_FORMAT) + relativedelta(days=changes_value)
            date_required = datetime.strftime(date_required,DEFAULT_SERVER_DATE_FORMAT)
            res['value'].update({'date_required':date_required})
        return res    

    #write the product supplier information
    def _update_prod_supplier(self,cr,uid,ids,vals,context=None):
        if vals.has_key('supplier_prod_name') or vals.has_key('supplier_prod_code') or vals.has_key('supplier_delay'):
            prod_supp_obj = self.pool.get('product.supplierinfo')
            new_vals = {'min_qty':0}
            if vals.has_key('supplier_prod_name'):
                new_vals.update({'product_name':vals['supplier_prod_name']})
            if vals.has_key('supplier_prod_code'):
                new_vals.update({'product_code':vals['supplier_prod_code']})
            if vals.has_key('supplier_delay'):
                new_vals.update({'delay':vals['supplier_delay']})
            #for the metro_currency module, set the currency
            user = self.pool.get("res.users").browse(cr,uid,uid,context=context)
            if ids:
                #from order line update
                for line in self.browse(cr,uid,ids,context=context):
                    new_vals.update({'name':line.wizard_id.partner_id.id,'product_id':line.product_id.product_tmpl_id.id,'currency':user.company_id.currency_id.id})
                    if line.supplier_prod_id:
                        #update the prodcut supplier info
                        prod_supp_obj.write(cr,uid,line.supplier_prod_id,new_vals,context=context)
                    else:
                        supplier_prod_id = prod_supp_obj.create(cr,uid,new_vals,context=context)     
            else:
                # from order line create
                po = self.pool.get('pur.req.po').browse(cr,uid,vals['wizard_id'])
                product = self.pool.get('product.product').browse(cr, uid, vals['product_id'], context=context)
                new_vals.update({'name':po.partner_id.id,'product_id':product.product_tmpl_id.id,'currency':user.company_id.currency_id.id})
                prod_supp_ids = prod_supp_obj.search(cr,uid,[('product_id','=',new_vals['product_id']),('name','=',new_vals['name'])])
                if prod_supp_ids and len(prod_supp_ids) > 0:
                    #update the prodcut supplier info
                    prod_supp_obj.write(cr,uid,prod_supp_ids[0],new_vals,context=context)
                else:
                    supplier_prod_id = prod_supp_obj.create(cr,uid,new_vals,context=context)  
                    
    def create(self, cr, user, vals, context=None):
        #check the product supplier info
        if not vals.has_key('supplier_prod_name') or vals['supplier_prod_name'] == '':
            product_name = self.pool.get("product.product").read(cr,user,[vals['product_id']],['name'],context=context)[0]['name']
            raise osv.except_osv(_('Error!'),
                                 _('The product supplier name is required to product .\n %s'%product_name))     

        resu = super(pur_req_po_line,self).create(cr, user, vals, context=context)
        #update product supplier info
        self._update_prod_supplier(cr, user, [], vals, context)
        #if price_unit changed then update it to product_product.standard_price
        if vals.has_key('price_unit'):
            self.pool.get('product.product').write(cr,user,[vals['product_id']],{'standard_price':vals['price_unit']},context=context)        
        return resu 
                        
    def write(self, cr, uid, ids, vals, context=None):
        if not ids:
            return True
        #update product supplier info
        self._update_prod_supplier(cr, uid, ids, vals, context)
        #if price_unit changed then update it to product_product.standard_price
        if vals.has_key('price_unit'):
            self.pool.get('product.product').write(cr,uid,[pur_req_po_line.product_id.id],{'standard_price':vals['price_unit']},context=context)                    
pur_req_po_line()


class pur_req_po(osv.osv_memory):
    _name = 'pur.req.po'
    _description = 'Requisition\'s Purchase Order'
    _columns = {
        'line_ids' : fields.one2many('pur.req.po.line', 'wizard_id', 'Prodcuts'),
        'partner_id': fields.many2one('res.partner', 'Supplier', required=True,domain=[('supplier', '=', True)]),
    }

    def default_get(self, cr, uid, fields, context=None):
        """
         To get default values for the object.
         @param self: The object pointer.
         @param cr: A database cursor
         @param uid: ID of the user currently logged in
         @param fields: List of fields for which we want default values
         @param context: A standard dictionary
         @return: A dictionary with default values for all field in ``fields``
        """
        line_data = []
        if context is None:
            context = {}
        res = super(pur_req_po, self).default_get(cr, uid, fields, context=context)
        record_id = context and context.get('active_id', False) or False
        req_obj = self.pool.get('pur.req')
        req = req_obj.browse(cr, uid, record_id, context=context)
        partner_id = None
        if req:             
            uom_obj = self.pool.get('product.uom')
            for line in req.line_ids:
                if not line.generated_po:
                    uom_po_qty = uom_obj._compute_qty_obj(cr, uid, line.product_uom_id, line.product_qty_remain, line.product_id.uom_po_id, context=context)
                    uom_po_factor = line.product_id.uom_po_factor/line.product_uom_id.factor_display
                    line_data.append({'product_id': line.product_id.id, 
                                      'product_qty_remain': line.product_qty_remain,
                                      'product_qty': line.product_qty_remain, 
                                      'product_uom_id':line.product_uom_id.id, 
                                      'price_unit':line.product_id.standard_price,
                                      
                                      'uom_po_qty_remain': uom_po_qty,
                                      'uom_po_qty': uom_po_qty,
                                      'uom_po_id':line.product_id.uom_po_id.id, 
                                      'uom_po_price':line.product_id.uom_po_price, 
                                      'uom_po_factor':uom_po_factor,
                                      
                                      'inv_qty':line.product_id.qty_available,
                                      'date_required': line.date_required,
                                      'req_line_id':line.id, 
                                      'req_reason':line.req_reason,
                                    })
                    if partner_id == None:
                        partner_id = line.product_id.seller_id.id    									
                    
            res.update({'line_ids': line_data,'partner_id':partner_id})
        return res

    def view_init(self, cr, uid, fields_list, context=None):
{'fraction_non_alphanumeric': 0.12483467724732283, 'fraction_numerical': 0.0022185246810870773, 'mean_word_length': 2.6223149435944983, 'pattern_counts': {'":': 0, '<': 7, '<?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': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1062851', 'n_tokens_mistral': 7099, 'n_tokens_neox': 6838, 'n_words': 1363}