Why invoice line values not passed in tax line in odoo10? - odoo

I create a custom field in account.invoice.line .
I added the field in account.invoice.tax
I override the prepare invoice function like this
cost_center_id is Many2one field
def _prepare_tax_line_vals(self, line, tax):
res = super(AccountInvoice, self)._prepare_tax_line_vals(line, tax)
if not res.get('cost_center_id') and line.cost_center_id:
res['cost_center_id'] = line.cost_center_id.id
return res
I print the cost_center values here the value is printed here.
But in account.invoice.tax the values is not there.
How to solve this issue??

Related

How to add another discount to subtotal in Quotation Order?

There is a subtotal (price_subtotal) field in Quotation Order Line.
I have added a new field extra_discount.
I have tried this code but to no avail.
#api.depends('product_uom_qty', 'price_unit', 'extra_discount')
def compute_all(self):
for record in self:
record.price_subtotal = (record.product_uom_qty * record.price_unit) - record.extra_discount
It does nothing to the subtotal.
So, how do I make this happen?
I would try to override the computation method behind the field sale.order.line.price_subtotal.
But i'm not sure if api.depends() extensions (adding one more recomputation trigger field) are working as expected.
But it should look something like that:
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
extra_discount = fields.Float()
#api.depends('product_uom_qty', 'discount', 'price_unit',
'tax_id', 'extra_discount')
def _compute_amount(self):
"""
Compute the amounts of the SO line.
Fully overridden to add field extra_discount to the
formula and triggers.
"""
for line in self:
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
# new: substract extra_discount
price -= line.extra_discount
taxes = line.tax_id.compute_all(price, line.order_id.currency_id, line.product_uom_qty, product=line.product_id, partner=line.order_id.partner_shipping_id)
line.update({
'price_tax': sum(t.get('amount', 0.0) for t in taxes.get('taxes', [])),
'price_total': taxes['total_included'],
'price_subtotal': taxes['total_excluded'],
})
if self.env.context.get('import_file', False) and not self.env.user.user_has_groups('account.group_account_manager'):
line.tax_id.invalidate_cache(['invoice_repartition_line_ids'], [line.tax_id.id])

How to print records number in form view in odoo?

I have in my python file this function to count number of records in (Order Lines):
class Class_sale_order(models.Model):
_inherit = 'sale.order'
caseacocher_sale_order = fields.Boolean(string='Print customized')
new_field2 = fields.Integer(compute='function_count_saleorderlines')
#api.depends('order_line')
def function_count_saleorderlines(self):
for rec in self:
rec.new_field2 = len('order_line')
But when i see the form view i find that the value of filed is 10, the problem that i have only 5 records.
len('order_line') returns the size of the string 'order_line' which is 10 that's why you are getting the value 10. Set like following:
rec.new_field2 = len(rec.order_line)

Why Custom Field could not saved in create mode, it saved on write mode in odoo?

I added the part number field in product template model
partnumber = fields.Char(
'Part Number', compute='_compute_partnumber',
inverse='_set_partnumber', store=True)
I write the function like internal reference code
#api.depends('product_variant_ids', 'product_variant_ids.partnumber')
def _compute_partnumber(self):
unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1)
for template in unique_variants:
template.partnumber = template.product_variant_ids.partnumber
for template in (self - unique_variants):
template.partnumber = ''
#api.one
def _set_partnumber(self):
if len(self.product_variant_ids) == 1:
self.product_variant_ids.partnumber = self.partnumber
I successfully added part number in product form.I used above methods for name get(to get part number in product description)
My problem is the part number could not saved in create method.
the field only saved in edit mode.
As "dccdany" already said in his comment, the creation order is a bit tricky on templates. You can see it in the code. First the template will be created. The partnumber won't be set, because there is no variant existing at that point.
After template creation, the variants will be created (one line later) without partnumber, so there will be no partnumber after template creation.
What can you do? Just overwrite product.template create(), like:
#api.model
def create(self, vals):
template = super(ProductTemplate, self).create(vals)
if 'partnumber' in vals and len(template.product_varient_ids) == 1:
template.partnumber = vals.get('partnumber')
return template

Increment integer fileds Odoo

I have added this fields under account.invoice in order to get an autoincrement number but it doesn't work.
Help me please to figure out my error
Example Code
class invoice(osv.osv):
_inherit = 'account.invoice'
def _get_increment(self, cr, uid, ids, fields, arg, context=None):
if context is None: context = {}
res = {}
if type == 'out_invoice':
ids = self.search(cr,uid,[('id','!=',False),('type','in',('out_invoice','out_refund'))])
if ids:
last_id = ids and max(ids)
print 'last_id',last_id
for invoice in self.browse(cr, uid, last_id, context):
print 'invoice', invoice
if invoice.name1:
res[invoice.id] = invoice.name1
else :
res[invoice.id] = invoice.name1 + 1
return res
_columns={
'name1':fields.function(_get_increment, type='integer', string='Name1'),
}
First of all. Your function never returns a value since type is never set.
Which means the if condition is never triggered.
At second. I'd suggest that you'd use the new Odoo API.
function fields are replaced by the compute attribute on fields and the declaration no longer takes place in the _columns dictionary.
New API
instead of importing from openerp.osv you should import the following:
from openerp import fields, models, api
The code would look like this:
from openerp import fields, models, api
class invoice(models.Model):
_inherit = 'account.invoice'
name1 = fields.Integer('Name1', compute='_get_increment')
#api.one
def _get_increment(self):
self.name1 = 1 + 1 #This value should be the value that you've calculated
the only thing you need to do in the method _get_increment is set self.name1.
In the new API self is a record. So self.id would get you the id of the record and so on.

OpenERP domain : char compare

Here's is what i've tried to do:
<field name="of_num" domain="[('etat','=','Terminé')]"/>
where 'of_num' is a many2one field and 'etat' is a function field of char type.
But it seems not working.I still get all records in my dropdown list.
I have also tried with some other text with no unicode chars but still the same.
I tried also to use 'ilike' operator and tried to put domain in python code with the field definition but with no chance.
EDITED
I've figured out the source of my problem :
the field 'etat' is computed but not stored since I'am using 'store=false'.
it's working with store=True.
Still, I don't wan't to store it because my value needs to be computed every time a view is loaded.
Could anyone please help me to do that without having to store my value ? thank you
The only solution I've found to get around my problem is to use a Boolean field that is stored and updated every time my function is computed (of the function field 'etat').
Use fnct_search. For functional fields there is an argument called 'fnct_search' which returns a search domain condition.
For example
_columns = {
'a':fields.float('A'),
'b':fields.float('B'),
'total_fn': fields.function(_total, fnct_search=_total_search, string='Total'),
}
def _total(self, cr, uid, ids, name, arg, context=None):
res = {}
for obj in self.browse(cr, uid, ids, context):
res[obj.id] = obj.a + obj.b
return res
def _total_search(self, cursor, user, obj, name, args, domain=None, context=None):
ids = set()
for cond in args:
amount = cond[2]
cr.execute("select id from your_table having sum(a,b) %s %%s" % (cond[1]),(amount,))
res_ids = set(id[0] for id in cr.fetchall())
ids = ids and (ids & res_ids) or res_ids
if ids:
return [('id', 'in', tuple(ids))]
return [('id', '=', '0')]
Here _total returns the value to display for the field total_fn, fnct_search returns the list of tuple need for searching. So whenever we are giving the argument [('total_fn','=',1500)]