Skip to content

Commit

Permalink
[ADD] purchase_requisition_filter_date: Add fields filter_date_from a…
Browse files Browse the repository at this point in the history
…nd filter_date_to
  • Loading branch information
unaiberis committed Jan 28, 2025
1 parent d5a03e4 commit 550d9f2
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 0 deletions.
50 changes: 50 additions & 0 deletions purchase_requisition_filter_date/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg
:target: https://opensource.org/licenses/LGPL-3.0
:alt: License: LGPL-3

===================================
Purchase Requisition Filter by Date
===================================

Overview
========

The **Purchase Requisition Filter by Date** module allows users to filter purchase requisition lines by a date range. This is particularly helpful in managing large purchase requisitions by focusing on specific scheduled dates.

Features
========

- **Date Range Filters**:

- Add fields for "From Date" and "To Date" on the purchase requisition form.

- Filter purchase requisition lines based on the provided date range.

- **Enhanced Workflow**:

- Update the wizard for creating purchase orders to respect the filtered date range.

- **Automatic Grouping**:

- Group requisition lines by scheduled date and create purchase orders with the appropriate planned dates.

Bug Tracker
===========

If you encounter any issues, please report them on the GitHub repository at `GitHub Issues <https://github.com/avanzosc/odoo-addons/issues>`_.

Credits
=======

Contributors
------------

* Ana Juaristi <[email protected]>
* Unai Beristain <[email protected]>

For specific questions or support, please contact the contributors.

License
=======

This project is licensed under the LGPL-3 License. For more details, refer to the LICENSE file or visit <https://opensource.org/licenses/LGPL-3.0>.
3 changes: 3 additions & 0 deletions purchase_requisition_filter_date/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import models

# from . import wizard
15 changes: 15 additions & 0 deletions purchase_requisition_filter_date/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Purchase Requisition Filter by Date",
"version": "16.0.1.0.0",
"category": "Purchases",
"author": "Avanzosc",
"license": "LGPL-3",
"depends": ["purchase_requisition"],
"data": [
# "views/purchase_requisition_views.xml",
# "wizard/purchase_requisition_wizard_views.xml",
],
"installable": True,
"application": False,
"website": "https://github.com/avanzosc/odoo-addons",
}
2 changes: 2 additions & 0 deletions purchase_requisition_filter_date/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import purchase_order_line
from . import purchase_requisition
15 changes: 15 additions & 0 deletions purchase_requisition_filter_date/models/purchase_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import api, models


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

@api.model_create_multi
def create(self, vals_list):
if "purchase_requisition_date" in self.env.context:
for i in range(len(vals_list) - 1, -1, -1):
if "date_planned" not in vals_list[i]:
del vals_list[i]
if "purchase_requisition_date" in self.env.context and not vals_list:
return self.env["purchase.order.line"]
return super().create(vals_list)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models


class PurchaseRequisition(models.Model):
_inherit = "purchase.requisition"

date_from = fields.Date(string="From")
date_to = fields.Date(string="To")
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<odoo>
<record id="view_purchase_requisition_form_inherit" model="ir.ui.view">
<field name="name">purchase.requisition.form.inherit.filter.date</field>
<field name="model">purchase.requisition</field>
<field
name="inherit_id"
ref="purchase_requisition.view_purchase_requisition_form"
/>
<field name="arch" type="xml">
<xpath expr="//group/group" position="inside">
<field name="date_from" />
<field name="date_to" />
</xpath>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions purchase_requisition_filter_date/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_requisition_wizard
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from odoo import _, api, fields, models


class PurchaseRequisitionAlternativeWarning(models.TransientModel):
_inherit = "purchase.requisition.alternative.warning"

@api.model
def _default_alternatives_filter_date(self):
po_obj = self.env["purchase.order"]
alternatives = self._default_alternative_po_ids()
filtered_alternatives = []
for alternative in alternatives:
po_info = alternative[2]
po = po_obj.browse(po_info.get("po_id"))
valid = True
if (
po.date_order and po.date_planned and po.date_planned < po.date_order
) or (
po.date_deadline
and po.date_planned
and po.date_planned > po.date_deadline
):
valid = False
if valid:
filtered_alternatives.append(alternative)
return filtered_alternatives

alternative_po_ids = fields.Many2many(
default=_default_alternatives_filter_date,
)

def action_group_by_date(self):
dates = set(self.alternative_po_ids.mapped("date_planned"))
grouped_alternatives = []
for date in dates:
result = self.with_context(
purchase_requisition_date=date
)._group_alternatives()
if "domain" in result:
domain = result.get("domain")
grouped_alternatives += domain[0][2]
return {
"domain": [("id", "in", grouped_alternatives)],
"name": _("Grouped Alternatives"),
"view_type": "form",
"view_mode": "tree,form",
"res_model": "purchase.order",
"context": {"from_grouped_alternatives": True},
"type": "ir.actions.act_window",
}

def _group_alternatives(self):
# Implementa la lógica para agrupar alternativas por fecha o cualquier otro criterio necesario.
# Retorna un resultado con el dominio de las alternativas agrupadas.
return {
"domain": [("id", "in", self.alternative_po_ids.ids)],
}

def _prepare_po_vals(self, supplier, currency_id, alternative_lines_by_supplier):
vals = super()._prepare_po_vals(
supplier, currency_id, alternative_lines_by_supplier
)
if "purchase_requisition_date" in self.env.context:
vals["date_planned"] = self.env.context.get("purchase_requisition_date")
return vals

def _prepare_po_line_vals(self, line):
vals = super()._prepare_po_line_vals(line)
if (
"purchase_requisition_date" in self.env.context
and line.date_planned == self.env.context.get("purchase_requisition_date")
):
vals["date_planned"] = self.env.context.get("purchase_requisition_date")
return vals
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="purchase_requisition_alternative_warning_form_inherit" model="ir.ui.view">
<field name="name">Alternative Warning Inherited</field>
<field name="model">purchase.requisition.alternative.warning</field>
<field name="inherit_id" ref="purchase_requisition.purchase_requisition_alternative_warning_form" />
<field name="arch" type="xml">
<xpath expr="//tree" position="inside">
<field name="filter_date_from" />
<field name="filter_date_to" />
</xpath>
</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions setup/purchase_requisition_filter_date/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 550d9f2

Please sign in to comment.