Hi guys,
I've been extending the contracts from the sales module so that you can create contracts for cars (linking to fleet.vehicle). I've added the following fields to the model account.analytic.invoice.line:
class account_analytic_invoice_line(osv.osv): _inherit = 'account.analytic.invoice.line' _columns = { 'car_id': fields.many2one('fleet.vehicle', 'Car'), 'analytic_car_account_id': fields.many2one('account.analytic.account', 'Analytic Account 2'), 'car_license_plate': fields.text('Nummerplaat'), 'car_mavirent_nummer': fields.text('MaviRent nummer'), 'car_chassis': fields.text('Chassis'), 'car_rent_price': fields.float('Prijs'), 'car_number_of_units': fields.integer('Aantal'), 'car_line_tax_id': fields.many2many('account.tax', 'account_car_line_tax', 'car_line_id', 'tax_id', string='BTW', required=True), 'car_account_id': fields.many2one('account.account', 'Grootboekrekening', required=True), }
Then in the contract view I've added a new one2many (the field recurring_invoice_car_line_ids) like this:
Now when a record is added on this one2many a Python function (on change event) named 'car_id_change' will go off which has the following code:<openerp> <data> <record id="view_contract_form_inherit_be_trans" model="ir.ui.view"> <field name="name">account.analytic.account.inherit</field> <field name="model">account.analytic.account</field> <field name="inherit_id" ref="account_analytic_analysis.account_analytic_account_form_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='recurring_invoice_line_ids']" position="after"> <label for="recurring_invoice_car_line_ids" attrs="{'invisible': [('recurring_invoices','=',False)]}"/> <div attrs="{'invisible': [('recurring_invoices','=',False)]}"> <field name="recurring_invoice_car_line_ids"> <tree string="Account Analytic Lines" editable="bottom"> <field name="car_id" on_change="car_id_change(car_id, car_license_plate, car_mavirent_nummer, car_chassis, car_rent_price)" attrs="{'required':[('car_id', '!=', '')]}"/> <!--These fields are only required when there is a value in the many2one field product_id' --> <field name="car_mavirent_nummer" attrs="{'required':[('car_id', '!=', '')]}"/> <field name="car_license_plate" attrs="{'required':[('car_id', '!=', '')]}"/> <field name="car_chassis" attrs="{'required':[('car_id', '!=', '')]}"/>
<field name="car_rent_price" attrs="{'required':[('car_id', '!=', '')]}"/> <field name="car_number_of_units" attrs="{'required':[('car_id', '!=', '')]}"/> <field name="car_line_tax_id" widget="many2many_tags"/> <field name="car_account_id"/> </tree> </field> </div> </xpath> </field> </record> </data> </openerp>
Which works fine. I can choose cars and all the data is filled in automatically (atleast, which I can).def car_id_change(self, cr, uid, ids, car, car_id=None,car_name='', car_license_plate='', car_mavirent_nummer='', car_chassis='', car_rent_price=0.0,car_number_of_units=1.0, context=None): context = context or {} local_context = dict(context, car_id=car_id) result = {} if not car: return {'value': {'car_name': '', 'car_license_plate': '', car_mavirent_nummer: '', car_chassis: '','car_rent_price': 0.0, 'car_number_of_units': 1.0}} else: res = self.pool.get('fleet.vehicle').browse(cr, uid, car, context=local_context) if res['license_plate']: car_license_plate = res['license_plate'] if res['name']: car_name = res['name'] if res['MaviVoertuigNummer']: car_mavirent_nummer = res['MaviVoertuigNummer'] if res['Chassis']: car_chassis = res['Chassis'] #Get a tax to automatically add to the contract. btw_categ_pool = self.pool.get('account.tax') goede_btw_id = btw_categ_pool.search(cr, uid, [('description','=','VAT-OUT-21-S')]) result.update({'car_name': car_name,'car_license_plate': car_license_plate, 'car_mavirent_nummer': car_mavirent_nummer, 'car_chassis': car_chassis, 'car_rent_price': car_rent_price, 'car_number_of_units': car_number_of_units, 'car_line_tax_id': [(6, 0, goede_btw_id)]}) res_final = {'value':result} return res_final
When I now click on save I get an error though:
When I look in the terminal I will see the following:Integrity Error
The operation cannot be completed, probably due to the following: - deletion: you may be trying to delete a record while other records still reference it - creation/update: a mandatory field is not correctly set [object with reference: name - name]
openerp.sql_db: bad query: INSERT INTO "account_analytic_invoice_line" ("id", "car_mavirent_nummer", "car_license_plate", "car_account_id", "car_number_of_units", "car_chassis", "analytic_car_account_id", "car_rent_price", "car_id", "quantity", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('account_analytic_invoice_line_id_seq'), 'MAV09', 'FFF-111', 819, 1, 'OPO06', 13, 0.0, 2, 1.0, 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
Traceback (most recent call last): File "/odoo/odoo-server/openerp/sql_db.py", line 234, in execute res = self._obj.execute(query, params) IntegrityError: null value in column "name" violates not-null constraint DETAIL: Failing row contains (27, null, 1, 2016-01-13 08:48:12.818483, null, null, 1, null, 2016-01-13 08:48:12.818483, 1, null, MAV09, 13, FFF-111, 1, OPO06, 819, 0, 2).
I do see there is a null value in the SQL statement and I know that is not allowed but what I don't know is where it is coming from or what I am missing that it is giving null. I'm passing along all the values if I'm not mistaking? So, how do I fix this?
Update: The error was coming from the fact that I didn't inherit the module 'account', I did inherit 'account_analytic_analysis' but not 'account'. I now have a new issue:
Integrity Error The operation cannot be completed, probably due to the following: - deletion: you may be trying to delete a record while other records still reference it - creation/update: a mandatory field is not correctly set [object with reference: price_unit - price.unit]
You can view and test the whole module, which is available on Github: https://github.com/Yenthe666/Dev/tree/master/aa_maatwerk_be_trans
Thanks,
Yenthe
what are steps to reproduce for an updated issue?
@Temur go to sales > contracts, create a new contract and add one car and no products. Then click on save, you will get a notification saying that a field is missing. When you click on save again you will get the error.
not solved yet?
@Axel sadly no! I can't find the problem in the code.
Ok. I reproduced it
@Yenthe, I traced down the error and applied quick fix to be able to save contract according of steps to reproduce above. Please refer to updated answer and adapt solution further to meet your requirements.
@Temur perfect! That was exactly what I was looking for. I'll need some minor changes to have everything perfect but my problem is fixed now. Thanks a lot! Accepted & upvoted! :)