summaryrefslogtreecommitdiff
path: root/addons/product_margin
diff options
context:
space:
mode:
authorLaurent Smet <las@odoo.com>2019-05-29 13:35:09 +0000
committerqdp-odoo <qdp@odoo.com>2019-06-28 11:52:55 +0000
commitbeaa30a3d1843de43a45f419bfbc1bfa7613a920 (patch)
tree00e62f6861eb15b3735fe032b849fd3e6150868c /addons/product_margin
parent0bd61b5245e475825fdeb33fb7b9ddb4f378c657 (diff)
[IMP/REF] accounting-pocalypse yeaaahh
This commit merges the following models * account.invoice and account.move * account.invoice.line and account.move.line * account.voucher and account.move * account.voucher.line and account.move.line It was the opportunity for a big cleanup of the code, so it also restructures the whole account module, its different models/fields, the tests etc. for a better world and a better code readability. ==== Rationale ==== The rationale of this huge change is that we want journal entries / invoices to be easily edited, and changes reflected in the other model. It's a HUGE feature and very strategic for the fiduciary companies. For example, changing the account of a journal entry needs to be automatically reflected on the related invoice. The same reasoning applies to sale/purchase vouchers. ==== Changes made in features ===== When creating an invoice, you are now creating a journal entry directly. --> The object account.invoice no longer exists. In the same fashion when creating an invoice line, you're now adding journal items directly in the journal entry representing the invoice. If this invoice line has some tax, it may create additional journal items as well. --> The models account.invoice.line & account.invoice.tax no longer exist Identically, when creating a sale/purchase receipt with its lines, you are now creating a journal entry directly and there's no more usability difference between encoding a receipt or an invoice. --> The object account.voucher no longer exists. --> The object account.voucher.line no longer exists. --> The whole account_voucher module no longer exists. Positive side-effects coming from these changes are * draft invoices/bills/sale or purchase receipts now create a draft accounting entry. Validate these objects now simply post its journal entry. That means that draft invoices/bills/sale or purchase receipt can straightforwardly be included in reporting or budgets. * opening a journal entry in form view will now always open the correct view: if it's a sale/purchase journal entry we will have a customer invoice/vendor bill view or a sale/purchase receipt view, whatever the menu we're coming from. * code & business logic simplification. It is also condensed in a single place instead of being partially duplicated on invoices, vouchers and journal entries. There should be no feature loss, except the one allowing to group multiple journal items together based on the same product during the invoice validation. ==== Changes made in models ===== * account.invoice: model removed. Instead, now use account.move with following mapping field (account.invoice) field (account.move) ----------------------- -------------------- name invoice_payment_ref number name reference ref comment narration user_id invoice_user_id amount_ total_company_signed amount_total_signed residual amount_residual state state + invoice_payment_state /!\ selection changed date_invoice invoice_date date_due invoice_date_due sent invoice_sent origin invoice_origin payment_term_id invoice_payment_term_id partner_bank_id invoice_partner_bank_id incoterm_id invoice_incoterm_id vendor_bill_id invoice_vendor_bill_id source_email invoice_source_email vendor_display_name invoice_vendor_display_name invoice_icon invoice_vendor_icon cash_rounding_id invoice_cash_rounding_id sequence_number_next invoice_sequence_number_next sequence_number_next_prefix invoice_sequence_number_next_prefix 'invoices' subset of account.move can be accessed by using the selection field 'type' or one of the many helpers like is_invoice() * account.move: now has a valid state 'cancel' that has to be excluded from all business logic * account.move: field 'amount' renamed into 'amount_total' * account.move: field 'reverse_entry_id' renamed into 'reversed_entry_id' * account.move.line: now has a field 'display_type' that has to be excluded from all business logic, in order to support invoice layouting * account.invoice.line: model removed. Instead, now use account.move.line with following mapping field (account.invoice.line) field (account.move.line) ---------------------------- ------------------------- invoice_id move_id uom_id product_uom_id invoice_line_tax_ids tax_ids account_analytic_id analytic_account_id 'invoice lines' subset of all account.move.line from a journal entry can be accessed by using the boolean field 'exclude_from_invoice_tab' * account.invoice.tax: model removed. Instead, now use account.move.line with following mapping field (account.invoice.tax) field (account.move.line) --------------------------- ------------------------- invoice_id move_id account_analytic_id analytic_account_id amount price_unit base tax_base_amount 'tax lines' subset of all account.move.line from a journal entry can be accessed by using the relational field 'tax_line_id' * account.invoice.confirm: model removed. Instead, now use the 'post()' function of account.move * account.invoice.refund: model removed. Instead, now use account.move.reversal to reverse the entries with the same options as we had for invoices * account.voucher: model removed. Instead, now use account.move of type in ['out_receipt', 'in_receipt] * account.voucher.line: model removed. Instead, now use account.move.line ==== Changes made in functions ==== * on account.move, method _run_post_draft_to_post() renamed into _autopost_draft_entries() * on account.move, method action_account_invoice_payment() renamed into action_invoice_register_payment() * on account.move, method action_invoice_reconcile_to_check() renamed into action_open_matching_suspense_moves() * on account.move, method _get_domain_edition_mode_available() renamed into _get_domain_matching_supsense_moves() * on account.move, method _get_intrastat_country_id() renamed into _get_invoice_intrastat_country_id() * on account.move.line, method _get_domain_for_edition_mode() renamed into _get_suspense_moves_domain() * in account.bank.statement, contextual key 'edition_mode' renamed into 'suspense_moves_mode' Was task 1917430
Diffstat (limited to 'addons/product_margin')
-rw-r--r--addons/product_margin/models/product_product.py43
-rw-r--r--addons/product_margin/tests/test_product_margin.py4
2 files changed, 28 insertions, 19 deletions
diff --git a/addons/product_margin/models/product_product.py b/addons/product_margin/models/product_product.py
index 5e4d5565b59..362fc1fbccb 100644
--- a/addons/product_margin/models/product_product.py
+++ b/addons/product_margin/models/product_product.py
@@ -89,14 +89,17 @@ class ProductProduct(models.Model):
res[val.id]['date_from'] = date_from
res[val.id]['date_to'] = date_to
res[val.id]['invoice_state'] = invoice_state
- invoice_types = ()
states = ()
+ invoice_payment_states = ()
if invoice_state == 'paid':
- states = ('paid',)
+ states = ('posted',)
+ invoice_payment_states = ('paid',)
elif invoice_state == 'open_paid':
- states = ('open', 'paid')
+ states = ('posted',)
+ invoice_payment_states = ('not_paid', 'paid')
elif invoice_state == 'draft_open_paid':
- states = ('draft', 'open', 'paid')
+ states = ('posted', 'draft')
+ invoice_payment_states = ('not_paid', 'paid')
if "force_company" in self.env.context:
company_id = self.env.context['force_company']
else:
@@ -104,29 +107,35 @@ class ProductProduct(models.Model):
#Cost price is calculated afterwards as it is a property
sqlstr = """
- select
- sum(l.price_unit * l.quantity)/nullif(sum(l.quantity),0) as avg_unit_price,
- sum(l.quantity) as num_qty,
- sum(l.quantity * (l.price_subtotal_signed/(nullif(l.quantity,0)))) as total,
- sum(l.quantity * pt.list_price) as sale_expected
- from account_invoice_line l
- left join account_invoice i on (l.invoice_id = i.id)
- left join product_product product on (product.id=l.product_id)
- left join product_template pt on (pt.id = product.product_tmpl_id)
- where l.product_id = %s and i.state in %s and i.type IN %s and (i.date_invoice IS NULL or (i.date_invoice>=%s and i.date_invoice<=%s and i.company_id=%s))
+ SELECT
+ SUM(l.price_unit * l.quantity)/NULLIF(SUM(l.quantity),0) AS avg_unit_price,
+ SUM(l.quantity) AS num_qty,
+ SUM(l.balance) AS total,
+ SUM(l.quantity * pt.list_price) AS sale_expected
+ FROM account_move_line l
+ LEFT JOIN account_move i ON (l.move_id = i.id)
+ LEFT JOIN product_product product ON (product.id=l.product_id)
+ LEFT JOIN product_template pt ON (pt.id = product.product_tmpl_id)
+ WHERE l.product_id = %s
+ AND i.state IN %s
+ AND i.invoice_payment_state IN %s
+ AND i.type IN %s
+ AND i.invoice_date BETWEEN %s AND %s
+ AND i.company_id = %s
+ AND l.display_type IS NULL
"""
invoice_types = ('out_invoice', 'in_refund')
- self.env.cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
+ self.env.cr.execute(sqlstr, (val.id, states, invoice_payment_states, invoice_types, date_from, date_to, company_id))
result = self.env.cr.fetchall()[0]
res[val.id]['sale_avg_price'] = result[0] and result[0] or 0.0
res[val.id]['sale_num_invoiced'] = result[1] and result[1] or 0.0
- res[val.id]['turnover'] = result[2] and result[2] or 0.0
+ res[val.id]['turnover'] = result[2] and -result[2] or 0.0
res[val.id]['sale_expected'] = result[3] and result[3] or 0.0
res[val.id]['sales_gap'] = res[val.id]['sale_expected'] - res[val.id]['turnover']
ctx = self.env.context.copy()
ctx['force_company'] = company_id
invoice_types = ('in_invoice', 'out_refund')
- self.env.cr.execute(sqlstr, (val.id, states, invoice_types, date_from, date_to, company_id))
+ self.env.cr.execute(sqlstr, (val.id, states, invoice_payment_states, invoice_types, date_from, date_to, company_id))
result = self.env.cr.fetchall()[0]
res[val.id]['purchase_avg_price'] = result[0] and result[0] or 0.0
res[val.id]['purchase_num_invoiced'] = result[1] and result[1] or 0.0
diff --git a/addons/product_margin/tests/test_product_margin.py b/addons/product_margin/tests/test_product_margin.py
index 9cc292dd82d..9626b47bb6d 100644
--- a/addons/product_margin/tests/test_product_margin.py
+++ b/addons/product_margin/tests/test_product_margin.py
@@ -10,7 +10,7 @@ class TestProductMargin(common.TransactionCase):
def create_account_invoice(self, invoice_type, partner, product, quantity=0.0, price_unit=0.0):
""" Create an invoice as in a view by triggering its onchange methods"""
- invoice_form = Form(self.env['account.invoice'].with_context(type=invoice_type))
+ invoice_form = Form(self.env['account.move'].with_context(default_type=invoice_type))
invoice_form.partner_id = partner
with invoice_form.invoice_line_ids.new() as line:
line.product_id = product
@@ -18,7 +18,7 @@ class TestProductMargin(common.TransactionCase):
line.price_unit = price_unit
invoice = invoice_form.save()
- invoice.action_invoice_open()
+ invoice.post()
return invoice
def test_product_margin(self):