Browse Source

[IMP] - Improve code

pull/414/head
sbejaoui 5 years ago
parent
commit
6432d48ab8
  1. 48
      contract/wizards/contract_manually_create_invoice.py

48
contract/wizards/contract_manually_create_invoice.py

@ -11,56 +11,44 @@ class ContractManuallyCreateInvoice(models.TransientModel):
invoice_date = fields.Date(string="Invoice Date", required=True) invoice_date = fields.Date(string="Invoice Date", required=True)
contract_to_invoice_count = fields.Integer( contract_to_invoice_count = fields.Integer(
compute="_compute_contract_to_invoice_count"
compute="_compute_contract_to_invoice_ids"
)
contract_to_invoice_ids = fields.Many2many(
comodel_name="contract.contract",
compute="_compute_contract_to_invoice_ids",
) )
@api.multi
def action_show_contract_to_invoice(self):
self.ensure_one()
@api.depends('invoice_date')
def _compute_contract_to_invoice_ids(self):
if not self.invoice_date: if not self.invoice_date:
contract_to_invoice_domain = [('id', '=', False)] contract_to_invoice_domain = [('id', '=', False)]
else: else:
contract_to_invoice_domain = self.env[ contract_to_invoice_domain = self.env[
'contract.contract' 'contract.contract'
]._get_contracts_to_invoice_domain(self.invoice_date) ]._get_contracts_to_invoice_domain(self.invoice_date)
self.contract_to_invoice_ids = self.env['contract.contract'].search(
contract_to_invoice_domain
)
self.contract_to_invoice_count = len(self.contract_to_invoice_ids)
@api.multi
def action_show_contract_to_invoice(self):
self.ensure_one()
return { return {
"type": "ir.actions.act_window", "type": "ir.actions.act_window",
"name": _("Contracts to invoice"), "name": _("Contracts to invoice"),
"res_model": "contract.contract", "res_model": "contract.contract",
"domain": contract_to_invoice_domain,
"domain": [('id', 'in', self.contract_to_invoice_ids.ids)],
"view_mode": "tree,form", "view_mode": "tree,form",
"context": self.env.context, "context": self.env.context,
} }
@api.depends('invoice_date')
def _compute_contract_to_invoice_count(self):
self.ensure_one()
if not self.invoice_date:
self.contract_to_invoice_count = 0
else:
contract_model = self.env['contract.contract']
self.contract_to_invoice_count = contract_model.search_count(
contract_model._get_contracts_to_invoice_domain(
self.invoice_date
)
)
@api.multi @api.multi
def create_invoice(self): def create_invoice(self):
self.ensure_one() self.ensure_one()
contract_model = self.env['contract.contract']
contracts = contract_model.search(
contract_model._get_contracts_to_invoice_domain(self.invoice_date)
)
invoices = contract_model.cron_recurring_create_invoice(
self.invoice_date
)
for contract in contracts:
contract.message_post(
body=_("Manually invoiced. Invoice date: %s")
% self.invoice_date
)
invoices = self.env['account.invoice']
for contract in self.contract_to_invoice_ids:
invoices |= contract.recurring_create_invoice()
return { return {
"type": "ir.actions.act_window", "type": "ir.actions.act_window",
"name": _("Invoices"), "name": _("Invoices"),

Loading…
Cancel
Save