flectra/addons/sale_crm/models/crm_lead.py

59 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-16 11:34:37 +01:00
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
from datetime import date
from dateutil.relativedelta import relativedelta
2018-01-16 11:34:37 +01:00
from flectra import api, fields, models
class CrmLead(models.Model):
_inherit = 'crm.lead'
sale_amount_total = fields.Monetary(compute='_compute_sale_amount_total', string="Sum of Orders", help="Untaxed Total of Confirmed Orders", currency_field='company_currency')
sale_number = fields.Integer(compute='_compute_sale_amount_total', string="Number of Quotations")
order_ids = fields.One2many('sale.order', 'opportunity_id', string='Orders')
@api.depends('order_ids')
def _compute_sale_amount_total(self):
for lead in self:
total = 0.0
nbr = 0
company_currency = lead.company_currency or self.env.user.company_id.currency_id
for order in lead.order_ids:
if order.state in ('draft', 'sent', 'sale'):
nbr += 1
if order.state not in ('draft', 'sent', 'cancel'):
total += order.currency_id.compute(order.amount_untaxed, company_currency)
lead.sale_amount_total = total
lead.sale_number = nbr
@api.model
def retrieve_sales_dashboard(self):
res = super(CrmLead, self).retrieve_sales_dashboard()
date_today = date.today()
res['invoiced'] = {
'this_month': 0,
'last_month': 0,
}
account_invoice_domain = [
('state', 'in', ['open', 'paid']),
('user_id', '=', self.env.uid),
('date_invoice', '>=', date_today.replace(day=1) - relativedelta(months=+1)),
('type', 'in', ['out_invoice', 'out_refund'])
]
invoice_data = self.env['account.invoice'].search_read(account_invoice_domain, ['date_invoice', 'amount_untaxed_signed'])
for invoice in invoice_data:
if invoice['date_invoice']:
invoice_date = fields.Date.from_string(invoice['date_invoice'])
if invoice_date <= date_today and invoice_date >= date_today.replace(day=1):
res['invoiced']['this_month'] += invoice['amount_untaxed_signed']
elif invoice_date < date_today.replace(day=1) and invoice_date >= date_today.replace(day=1) - relativedelta(months=+1):
res['invoiced']['last_month'] += invoice['amount_untaxed_signed']
res['invoiced']['target'] = self.env.user.target_sales_invoiced
return res