Add scheduled task to close stock period automatically

This commit is contained in:
Cédric Krier committed 2026-08-19 00:14:52 +02:00
1 parent 05474e621d
commit c33f675b28
8 files changed
+61 -4

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Add scheduled task to close period automatically
* Add scheduled task to create period automatically
Version 8.0.0 - 2026-04-20
+16 -1
View File
@@ -113,6 +113,15 @@ class Configuration(
],
help="Leave it empty to prevent periods "
"from being created automatically."))
period_closing_delay = fields.MultiValue(
fields.TimeDelta(
"Period Closing Delay",
domain=['OR',
('period_closing_delay', '=', None),
('period_closing_delay', '>=', TimeDelta()),
],
help="Leave it empty to prevent periods "
"from being closed automatically."))
@classmethod
def multivalue_model(cls, field):
@@ -121,7 +130,7 @@ class Configuration(
return pool.get('stock.configuration.sequence')
if field == 'shipment_internal_transit':
return pool.get('stock.configuration.location')
if field == 'period_creation_interval':
if field in {'period_creation_interval', 'period_closing_delay'}:
return pool.get('stock.configuration.period')
return super().multivalue_model(field)
@@ -217,3 +226,9 @@ class Period(ModelSQL, CompanyValueMixin):
('period_creation_interval', '=', None),
('period_creation_interval', '>=', TimeDelta(days=1)),
])
period_closing_delay = fields.TimeDelta(
"Period Closing Delay",
domain=['OR',
('period_closing_delay', '=', None),
('period_closing_delay', '>=', TimeDelta()),
])
+7
View File
@@ -21,3 +21,10 @@ date, and to improve the performance of Tryton when looking at and using
configuration <model-stock.configuration>` and add the `company
<company:model-company.company>` to the `scheduled task
<trytond:model-ir.cron>` to create periods automatically.
.. tip::
You can set a :guilabel:`Period Close Delay` on the `stock configuration
<model-stock.configuration>` and add the `company
<company:model-company.company>` to the `scheduled task
<trytond:model-ir.cron>` to close periods automatically after the delay.
+2
View File
@@ -27,11 +27,13 @@ class Cron(metaclass=PoolMeta):
"Reschedule Internal Shipments"),
('ir.cron|stock_shipment_assign_try', "Assign Shipments"),
('stock.period|auto_create', "Create Stock Periods"),
('stock.period|auto_close', "Close Stock Periods"),
])
cls.methods_company_needed.update({
'product.product|recompute_cost_price_from_moves',
'ir.cron|stock_shipment_assign_try',
'stock.period|auto_create',
'stock.period|auto_close',
})
@classmethod
+21
View File
@@ -202,6 +202,27 @@ class Period(Workflow, ModelSQL, ModelView):
'company': company,
}])[0]
@classmethod
def auto_close(cls):
pool = Pool()
Date = pool.get('ir.date')
Configuration = pool.get('stock.configuration')
company = Transaction().context.get('company')
if company is None:
return
config = Configuration(1)
delay = config.get_multivalue(
'period_closing_delay', company=company)
if delay is None:
return
today = Date.today()
periods = cls.search([
('company', '=', company),
('date', '<', today - delay),
('state', '=', 'draft'),
])
cls.close(periods)
class Cache(ModelSQL, ModelView):
"It is used to store cached computation of stock quantities"
+5
View File
@@ -123,5 +123,10 @@ this repository contains the full copyright notices and license terms. -->
<field name="interval_number" eval="1"/>
<field name="interval_type">days</field>
</record>
<record model="ir.cron" id="cron_close_period">
<field name="method">stock.period|auto_close</field>
<field name="interval_number" eval="1"/>
<field name="interval_type">days</field>
</record>
</data>
</tryton>
@@ -49,6 +49,7 @@ Setup period configuration::
>>> configuration = Configuration(1)
>>> configuration.period_creation_interval = dt.timedelta(days=1)
>>> configuration.period_closing_delay = dt.timedelta()
>>> configuration.save()
Create periods::
@@ -63,9 +64,12 @@ Create periods::
Close the period::
>>> period.click('close')
>>> period.state
'closed'
>>> cron, = Cron.find([('method', '=', 'stock.period|auto_close')], limit=1)
>>> cron.companies.append(get_company())
>>> cron.click('run_once')
>>> period, = Period.find([])
>>> assertEqual(period.state, 'closed')
Try to create a move::
@@ -21,4 +21,6 @@ this repository contains the full copyright notices and license terms. -->
<separator id="period" colspan="4" string="Period"/>
<label name="period_creation_interval"/>
<field name="period_creation_interval"/>
<label name="period_closing_delay"/>
<field name="period_closing_delay"/>
</form>