83 lines
3.0 KiB
Python
Executable File
83 lines
3.0 KiB
Python
Executable File
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api
|
|
from datetime import date
|
|
import base64
|
|
class SOS_Shelflife_Material_Register(models.Model):
|
|
_name = 'sos_shelflife_register'
|
|
_description = 'Shelf Life Register'
|
|
|
|
|
|
name = fields.Many2one('sos_material',string="Material Name", required=True)
|
|
quantity = fields.Float(string="Quantity")
|
|
expiry_date = fields.Date(string="Expiry Date")
|
|
ir_ref_no = fields.Many2one('sos_ir',string="Inward Ref No")
|
|
remaining_days = fields.Integer(string="Remaining days to expire", compute="_compute_remaining_days", store=True)
|
|
|
|
@api.depends('expiry_date')
|
|
def _compute_remaining_days(self):
|
|
today = date.today()
|
|
for record in self:
|
|
if record.expiry_date:
|
|
delta = (record.expiry_date - today).days
|
|
record.remaining_days = max(0, delta) # don't show negative days
|
|
else:
|
|
record.remaining_days = 0
|
|
def action_generate_expiry_report(self):
|
|
expiring_materials = self.search([
|
|
('remaining_days', '<', 7),
|
|
('remaining_days', '>', 0)
|
|
])
|
|
if not expiring_materials:
|
|
return
|
|
|
|
# Build HTML table
|
|
table_rows = ''.join([
|
|
f"""
|
|
<tr>
|
|
<td>{material.name.part_no or ''}</td>
|
|
<td>{material.expiry_date or ''}</td>
|
|
<td>{material.remaining_days or ''}</td>
|
|
</tr>
|
|
"""
|
|
for material in expiring_materials
|
|
])
|
|
|
|
html_body = f"""
|
|
<p>Dear Team,</p>
|
|
<p>Below is the weekly report of materials expiring within 7 days:</p>
|
|
<table class="table" border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse;">
|
|
<thead>
|
|
<tr>
|
|
<th>Material Name</th>
|
|
<th>Expiry Date</th>
|
|
<th>Remaining Days to Expire</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{table_rows}
|
|
</tbody>
|
|
</table><br></br>
|
|
<p>Regards,<br/>Slink Admin</p>
|
|
"""
|
|
|
|
# Get users in the group
|
|
group = self.env.ref('sos_inventory.sos_scg_group_user')
|
|
users = self.env['res.users'].search([('groups_id', 'in', group.id)])
|
|
recipient_emails = ','.join(filter(None, users.mapped('email')))
|
|
|
|
# Create and send the email directly
|
|
mail_template = self.env['mail.template'].create({
|
|
'name': 'HTML Shelf Life Expiry Report',
|
|
'model_id': self.env['ir.model']._get(self._name).id,
|
|
'subject': f'Shelf Life Expiry Report - {date.today().strftime("%Y-%m-%d")}',
|
|
'email_from': self.env.user.email,
|
|
'email_to': recipient_emails,
|
|
'body_html': html_body,
|
|
})
|
|
|
|
mail_template.send_mail(
|
|
res_id=expiring_materials[0].id, # Just link to one of the records
|
|
force_send=True
|
|
)
|
|
|