diff --git a/account_invoice_subcontractor/__manifest__.py b/account_invoice_subcontractor/__manifest__.py index 84751b1..d21626e 100644 --- a/account_invoice_subcontractor/__manifest__.py +++ b/account_invoice_subcontractor/__manifest__.py @@ -16,6 +16,7 @@ ], "data": [ "data/cron_data.xml", + "data/mail_data.xml", "security/security.xml", "security/ir.model.access.csv", "views/hr_employee_view.xml", diff --git a/account_invoice_subcontractor/data/mail_data.xml b/account_invoice_subcontractor/data/mail_data.xml new file mode 100644 index 0000000..5b20e94 --- /dev/null +++ b/account_invoice_subcontractor/data/mail_data.xml @@ -0,0 +1,25 @@ + + + + + External subcontractor invoice template + {{ (object.user_id.email or '') }} + Nouvelle facture sous traitance Akretion + {{ object.partner_id.id }} + + + {{ object.partner_id.lang }} + + + + + diff --git a/account_invoice_subcontractor/models/hr_employee.py b/account_invoice_subcontractor/models/hr_employee.py index b7cfece..2ac3e41 100644 --- a/account_invoice_subcontractor/models/hr_employee.py +++ b/account_invoice_subcontractor/models/hr_employee.py @@ -24,6 +24,7 @@ def _get_subcontractor_type(self): required=True, default="internal", ) + auto_generate_invoice = fields.Boolean(string="Création facture automatique") def _get_employee_invoice_partner(self): self.ensure_one() diff --git a/account_invoice_subcontractor/models/subcontractor_work.py b/account_invoice_subcontractor/models/subcontractor_work.py index 0ae81b4..5dd6584 100644 --- a/account_invoice_subcontractor/models/subcontractor_work.py +++ b/account_invoice_subcontractor/models/subcontractor_work.py @@ -385,9 +385,15 @@ def _scheduler_action_subcontractor_invoice_create(self, days=7): date_filter = date.today() - timedelta(days=days) subcontractors = self.env["hr.employee"].search( [ + "|", + "&", ("subcontractor_type", "=", "internal"), ("subcontractor_company_id", "!=", False), - ] + "&", + ("subcontractor_type", "=", "external"), + ("auto_generate_invoice", "=", True), + ], + order="subcontractor_type", ) # Need to search on all subcontractor work # because of the filter on date invoice @@ -395,29 +401,37 @@ def _scheduler_action_subcontractor_invoice_create(self, days=7): [ ("invoice_id.invoice_date", "<=", date_filter), ("subcontractor_invoice_line_id", "=", False), + "|", ("subcontractor_type", "=", "internal"), + "&", + ("subcontractor_type", "=", "external"), + ("auto_generate_invoice", "=", True), ("state", "in", ["posted", "paid"]), ], ) for subcontractor in subcontractors: - dest_company = subcontractor.subcontractor_company_id - user = subcontractor.user_id - subcontractor_works = ( - self.with_user(user) - .with_company(dest_company) - .search( - [ - ("id", "in", all_works.ids), - ("employee_id", "=", subcontractor.id), - ], - ) + if subcontractor.subcontractor_type == "internal": + dest_company = subcontractor.subcontractor_company_id + user = subcontractor.user_id + self = self.with_user(user).with_company(dest_company) + subcontractor_works = self.search( + [ + ("id", "in", all_works.ids), + ("employee_id", "=", subcontractor.id), + ], ) sub_ids = subcontractor_works.ids sub_name = subcontractor.name _logger.info(f"{sub_ids} lines found for subcontractor {sub_name}") invoices = subcontractor_works.invoice_from_work() for invoice in invoices: - invoice.action_post() + if subcontractor.subcontractor_type == "internal": + invoice.action_post() + else: + template = self.env.ref( + "account_invoice_subcontractor.external_subcontractor_invoice_email_template" + ) + template.send_mail(invoice.id) # if old_company: # user.company_id = old_company.id return True diff --git a/account_invoice_subcontractor/views/hr_employee_view.xml b/account_invoice_subcontractor/views/hr_employee_view.xml index b2c7153..54b5501 100644 --- a/account_invoice_subcontractor/views/hr_employee_view.xml +++ b/account_invoice_subcontractor/views/hr_employee_view.xml @@ -14,6 +14,10 @@ name="subcontractor_company_id" attrs="{'required': [('subcontractor_type', '=', 'internal'), ('active', '=', True)], 'invisible': [('subcontractor_type','!=', 'internal')]}" /> +