Skip to content

Commit

Permalink
Merge pull request #53 from akretion/16.0-auto-invoice-external
Browse files Browse the repository at this point in the history
auto invoice external subcontractor
  • Loading branch information
florian-dacosta authored Oct 7, 2024
2 parents 968a1a0 + 921c1bb commit f984dbc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
1 change: 1 addition & 0 deletions account_invoice_subcontractor/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions account_invoice_subcontractor/data/mail_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record id="external_subcontractor_invoice_email_template" model="mail.template">
<field name="name">External subcontractor invoice template</field>
<field name="email_from">{{ (object.user_id.email or '') }}</field>
<field name="subject">Nouvelle facture sous traitance Akretion</field>
<field name="partner_to">{{ object.partner_id.id }}</field>
<field name="model_id" ref="account.model_account_move" />
<field name="auto_delete" eval="False" />
<field name="lang">{{ object.partner_id.lang }}</field>
<field name="report_template" ref="account.account_invoices" />
<field
name="body_html"
><![CDATA[
Une facture de sous traitance a été créée pour Akretion.
Vous trouverez ci-joint le montant.
Veuillez transmettre votre facture correspondante à l'adresse '[email protected]' afin que nous puissions la valider et la payer une fois le paiement client effectué.
Merci.
Cordialement,
]]></field>
</record>

</odoo>
1 change: 1 addition & 0 deletions account_invoice_subcontractor/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AccountMove(models.Model):
readonly=True,
prefetch=False,
)
subcontractor_sent = fields.Boolean()

@api.depends(
"invoice_line_ids",
Expand Down
1 change: 1 addition & 0 deletions account_invoice_subcontractor/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 48 additions & 14 deletions account_invoice_subcontractor/models/subcontractor_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ def _prepare_invoice_line(self, invoice):
def _get_subcontractor_invoicing_group(self):
groups = OrderedDict()
for sub in self.sorted("invoice_date"):
if sub.subcontractor_type == "internal":
if sub.subcontractor_type == "internal" or self.env.context.get(
"invoice_create_cron"
):
key = (sub.employee_id.id, sub.invoice_id.id)
elif sub.subcontractor_type == "external":
key = (sub.employee_id.id, False)
Expand Down Expand Up @@ -383,41 +385,73 @@ def invoice_from_work(self):

def _scheduler_action_subcontractor_invoice_create(self, days=7):
date_filter = date.today() - timedelta(days=days)
# The order is used to start cron by external subcontractor because the user
# and company changes for internal subcontractors
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
all_works = self.search(
[
("invoice_id.invoice_date", "<=", date_filter),
("subcontractor_invoice_line_id", "=", False),
"|",
("subcontractor_type", "=", "internal"),
"&",
("subcontractor_type", "=", "external"),
("employee_id.auto_generate_invoice", "=", True),
("state", "in", ["posted", "paid"]),
],
)
template = self.env.ref(
"account_invoice_subcontractor.external_subcontractor_invoice_email_template"
)
# Send validation email to old draft external subcontractor invoices
invoices = self.env["account.move"].search(
[
("subcontractor_sent", "=", False),
("partner_id", "in", subcontractors.user_id.partner_id.ids),
("state", "=", "draft"),
("create_date", "<=", date_filter),
]
)
for draft_invoice in invoices:
template.send_mail(draft_invoice.id)
draft_invoice.subcontractor_sent = True
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)
else:
# Used to group by invoice also for external in case of the cron
self = self.with_context(invoice_create_cron=True)
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.send_mail(invoice.id)
invoice.subcontractor_sent = True
# if old_company:
# user.company_id = old_company.id
return True
Expand Down
4 changes: 4 additions & 0 deletions account_invoice_subcontractor/views/hr_employee_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
name="subcontractor_company_id"
attrs="{'required': [('subcontractor_type', '=', 'internal'), ('active', '=', True)], 'invisible': [('subcontractor_type','!=', 'internal')]}"
/>
<field
name="auto_generate_invoice"
attrs="{'invisible': [('subcontractor_type','!=', 'external')]}"
/>
</group>
</xpath>
</field>
Expand Down

0 comments on commit f984dbc

Please sign in to comment.