Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to get the value from that record order by date descending?

By
philip
on 11/16/14, 11:17 AM 2,077 views

Hi all,

How to get the value from that record order by date descending to get the last value of my field "nextweek"?

in my table test_prod I have the following data http://i60.tinypic.com/w2nd3d.png

what I want is when I create new record I want to get the 70.00 value which is the last create_date of my prod_code and store to my begvalue field. 

The idea is to get that value which is the totalending of specific product and use as the beginning value of same product

could someone have a simple working code please post. 

something like this:

In create method, check the class if already any record exists order by id or not

2) if records exists, then browse the value from searched record and update it to your newly creating record.

3) if record does not exist, then you can give your own values to the initialbeginning and initialending  fields.

 

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from osv import osv, fields
from openerp import tools
import time
class test_product(osv.Model):
    _name = "test.product"

      _columns = {

        'prodcode_id': fields.many2one("abs.prod.desc", 'Product Code'),
        'description_id': fields.many2one('abs.desc', 'Description'),
        'description': fields.related('description_id', type='many2one', relation='abs.desc', string='Description',),
        'price_id': fields.many2one('abs.price', 'Price', ondelete='cascade'),
        'price': fields.related('price_id','price_value', type='float', string='Price', ondelete='cascade'),
        'abs_weeklyperiod_id': fields.many2one('abs.weeklyperiod', 'Periods Covered', ondelete='cascade'),
        'newval': fields.float('New Val', digits=(12,2)),
    }
    
    def onchange_prodcode(self, cr, uid, ids, prodcode_id, context=None):
        if prodcode_id:
            description_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).description_id.id
            price_id = self.pool.get('abs.prod.desc').browse(cr, uid, prodcode_id, context).price_id.id
            return {'value':{'description_id':description_id, 'price_id':price_id}}
        return {}

    def onchange_prodcode1(self,cr,uid,ids,prodcode,context=None):
        if prodcode:
            test_prod_ids = self.search(cr,uid,[('prodcode','=',prodcode)],order="id desc",limit=1,context=context)
            if test_prod_ids:
                rd_test_prod = self.read(cr,uid,test_prod_uids[0],['nextweek'],context=conetxt)
                return {'value':{'newval':rd_test_prod[0].get('nextweek')}}
            return {'value':{}}

class descriptionproduct(osv.osv):
    _description="Description Product"
    _name = 'abs.prod.desc'
    _columns = {
        'name': fields.char('Code', size=64, required=True,),
        'description_id': fields.many2one('abs.desc', 'Description', required=True),
        'price_id': fields.many2one('abs.price', 'Price', required=True),
    }
    
class description(osv.osv):
    _name = 'abs.desc'
    _description = 'Description'
    _columns = {
        'name': fields.char('Description Name', size=64, required=True,),
    }

class productprice(osv.osv):
    _description="Product Price"
    _name = 'abs.prod.price'
    _columns = {
        'price_id': fields.many2one('abs.price', 'Price', required=True),
        'price_value': fields.float('Price', digits=(12,2)),
    }

class price(osv.osv):
    _name = 'abs.price'
    _description = 'Price'
    _columns = {
        'name': fields.float('Price', digits=(12,2)),
    }

class abs_weeklyperiod(osv.Model):
    _name = "abs.weeklyperiod"

    _columns = {
        'start_date': fields.date('Start Date'),
        'end_date': fields.date('End Date'),
        'name': fields.char('Periods', size=32, required=True),
        'initialbeginning': fields.integer('Initial Beginning', size=32, ondelete='cascade'),
        'initialending': fields.integer('Initial Ending', size=32, ondelete='cascade'),
    }

    _defaults = {
        'start_date': lambda *a: time.strftime('%Y-%m-%d'),
        'end_date': lambda *a: (datetime.today() + relativedelta(days=6)).strftime('%Y-%m-%d'),
    }

Very Thanks

 

1

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 11/16/14, 11:54 AM

Hello philip

You can write an onchange_method for prodcode (which I assume is the many2one of product.product).

I am not sure in which model/screen you want to use this field but for now i am assuming it for the same table that you have specified. You can just modify the below code as per your requirement.

def onchange_prodcode(self,cr,uid,ids,prodcode,context=None)

    if prodcode:

        test_prod_ids = self.search(cr,uid,[('prodcode','=',prodcode)],order="id desc",limit=1,context=context)

        if test_prod_ids:

            rd_test_prod = self.read(cr,uid,test_prod_uids[0],['next_week'],context=conetxt)

            return {'value':{'yourfield':rd_test_prod[0].get('next_week')}}

    return {'value':{}}

I have used order by "id", you can use "create_date", if you want.

You can use same code by overriding the create method.

Hope this helps !!

 

Thanks I tried the code when I upgrade my module nothings happened, is there any I to my code, already update above code please

philip
on 11/17/14, 3:46 AM

any update here

philip
on 11/19/14, 12:26 PM

UPDATE PLEASE, NOT working with the current code Thanks

philip
on 11/24/14, 10:50 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/16/14, 11:17 AM
Seen: 2077 times
Last updated: 3/16/15, 8:10 AM