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 a specific data from another class's column in openerp7 modules using fields.function?

By
BEN YAHIA Hamza
on 7/22/16, 3:56 PM 246 views

Hi all,

I am a beginner in developping a module in openerp7  

I am starting to develop my first module in openerp7 (car rental management module) and I need your help because I am struggling for days just to create a simple function that allows me to get data from another class, so here are my classes :

 vehicle class -

class voitures(osv.osv):

_name = "voitures"

_description = "Voitures"

_columns = {

'matricule' : fields.char('Matricule', size=32, required=True),

'marque' : fields.char('Marque', size=32, required=True),

'modele' : fields.char('Modèle', size=32, required=True),

'moteur' : fields.selection([('essence', 'Essence'), ('diesel', 'Diesel'), ('electrique', 'Electrique'), ('hybride', 'Hybride')], 'Type du carburant'),

'nm_chasis' : fields.integer('Numéro de chasis'),

'type' : fields.char('Type', size=32),

'nb_Places' : fields.integer('Numéro de places'),

'nb_Portes' : fields.integer('Numéro de portes'),

'image' : fields.binary('Image'),

'couleur' : fields.char('Couleur', size=32),

'transmission' : fields.selection([('manuelle', 'Manuelle'), ('automatique', 'Automatique')], 'Transmission'),

'nb_Chevaux' : fields.integer('Numéro de chevaux'),

'estDisponible' : fields.selection([('ds', 'Disponible'), ('nDs', 'Non-Disponible')], 'Disponibilité', required=True),

'coutEn24h' : fields.integer('Coût en 24 heures'),

'description' : fields.text('Description'),

 client class -

class clients(osv.osv):

_name = 'clients'

_description = 'Clients'

_columns = {

'nom' : fields.char('Nom', size=32, required=True),

'prenom' : fields.char('Prenom', size=32, required=True),

'cin' : fields.char('CIN', required=True),

'societe' : fields.char('Sociéte', size=32),

'adresse' : fields.char('Adresse', size=32),

'image' : fields.binary('Image'),

'sex' : fields.selection([('male', 'Mâle'), ('femelle', 'Femelle')], 'Sex'),

'numeroTl' : fields.integer('Numéro de téléphone ', required=True),

'courriel' : fields.char('Courriel', size=32),

'description' : fields.text('Description'),

}

contract class -

class contrats(osv.osv):

_name = "contrats"

_description = "Contrats"

_columns = {

'idContrat' : fields.char('Numéro du contrat', required=True),

'cin' : fields.many2one('clients','CIN du contractant', required=True),

'matricule' : fields.many2one('voitures','matricule de voiture à louer',required=True),

'dateContrat' : fields.date('Date de signature',required=True),

'dateDebutContrat' : fields.date('Date du début du contrat',required=True),

'dateExpContrat' : fields.date('Date d''expiration du contrat' ,required=True),

'coutsTotal' : fields.float('Coût tolal'),

'etat' : fields.selection([('enC', 'En cours'), ('exp', 'Expiré')], 'Etat du contrat'),

'estPaye' : fields.selection([('p', 'Payé'), ('n', 'Non_Payé')], 'Paiement'),

'description' : fields.text('Description'),

}

I want to transform the contrats.coutsTotal field (total cost of the contract) to a function field in order to be automatically calculated, to do that I need to get the voitures.coutEn24h field (vehicle cost in 24 hours) because when creating a contract to a client, I select a vehicle that is already stored in the database, so when selecting that specific vehicle and giving the expiration date to the contract I need its total cost to be  automatically generated.Sorry for my bad english and thank you for your time :)

0
ayman mohammed adam
On 7/23/16, 3:30 AM

dear BEN YAHIA Hamza,

     def _compute(self, cr, uid, ids, name, args, context=None):

             res= {}

             for rec in self.browse(cr, uid, ids, context=context):

                 res[rec.id]= rec.matricule.coutEn24h or 0
             return res

                 

     replace the 'coutsTotal' : fields.float('Coût tolal'),
     to                 'coutsTotal' : fields.function(_compute, type='float', method=True, store=True, string='Coût tolal'),

I hoped i helped you...


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: 7/22/16, 3:56 PM
Seen: 246 times
Last updated: 7/23/16, 3:30 AM