Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prices not only from interface #39

Open
wants to merge 5 commits into
base: 10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions account_invoice_line_pricelist/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ def _onchange_partner_id(self):
if self.partner_id:
self.pricelist_id = self.partner_id.property_product_pricelist
return res

@api.multi
def write(self, vals):
"""
Update pricelist_id whenever missing, before real write()
Remember: do *not* update rec.pricelist_id directly! that triggers write()
"""
for rec in self:
if ('type' in vals and vals['type'] in ['out_invoice', 'out_refund']) or (rec.type in ['out_invoice', 'out_refund']):
if 'partner_id' in vals:
#INSERT/UPDATE partner
if not 'pricelist_id' in vals:
vals['pricelist_id'] = vals['partner_id'].property_product_pricelist
else:
#UPDATE record
if ((not rec.pricelist_id) or(not rec.pricelist_id.id)) and (rec.partner_id) and (rec.partner_id.property_product_pricelist) and (rec.partner_id.property_product_pricelist.id):
vals['pricelist_id'] = rec.partner_id.property_product_pricelist.id
return super(AccountInvoice, self).write(vals)
52 changes: 48 additions & 4 deletions account_invoice_line_pricelist/models/account_invoice_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ class AccountInvoiceLine(models.Model):

@api.onchange('product_id')
def _onchange_product_id(self):

res = super(AccountInvoiceLine, self)._onchange_product_id()

price_unit = self._common_recalc_price()
if price_unit is not None and price_unit != 0:
self.price_unit = price_unit

return res

def _common_recalc_price(self):
"""
Re/calculate and return given price
"""
if self.invoice_id.type not in ['out_invoice', 'out_refund']:
return res
return None

partner = self.invoice_id.partner_id
pricelist = self.invoice_id.pricelist_id
product = self.product_id

if not partner or not product or not pricelist:
return res
return None

inv_date = self.invoice_id.date_invoice or date.today().strftime(DT)
product = product.with_context(
Expand All @@ -34,9 +45,42 @@ def _onchange_product_id(self):
uom=self.uom_id.id
)

self.price_unit = self.env['account.tax']._fix_tax_included_price(
return self.env['account.tax']._fix_tax_included_price(
product.price,
product.taxes_id,
self.invoice_line_tax_ids
)
return res

def create(self, vals):
"""
Update price if missing, after real create()
"""
line = super(AccountInvoiceLine, self).create(vals)
if line.invoice_id:
invoice = line.invoice_id
if ((not line.price_unit) or line.price_unit == 0) and (invoice.pricelist_id and invoice.pricelist_id.id):
price_unit = line._common_recalc_price()
if price_unit is not None and price_unit != 0:
line.price_unit = price_unit #triggers write()
return line

@api.multi
def write(self, vals):
"""
Update price whenever missing, before real write()
Remember: do *not* update rec.price_unit directly! that triggers write()
"""
for rec in self:
if (rec.invoice_id.pricelist_id and rec.invoice_id.pricelist_id.id):
if 'price_unit' in vals and (vals['price_unit'] is None or vals['price_unit'] == 0):
#INSERT/UPDATE price_unit
price_unit = rec._common_recalc_price()
if price_unit is not None:
vals['price_unit'] = price_unit
else:
#UPDATE record
if rec.price_unit is None or rec.price_unit == 0:
price_unit = rec._common_recalc_price()
if price_unit is not None and price_unit != 0:
vals['price_unit'] = price_unit
return super(AccountInvoiceLine, self).write(vals)