Skip to content

Commit

Permalink
Create all recurring orders at confirmation
Browse files Browse the repository at this point in the history
like the cron.

Fix #312
  • Loading branch information
hparfr authored and brian10048 committed Jul 18, 2020
1 parent 0c38444 commit 1795f8d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions fieldservice_recurring/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Contributors

* Brian McMaster <brian@mcmpest.com>
* Kitti Upariphutthiphone <kittiu@ecosoft.co.th>
* Rapha??l Reverdy <raphael.reverdy@akretion.com>

Other credits
~~~~~~~~~~~~~
Expand Down
33 changes: 21 additions & 12 deletions fieldservice_recurring/models/fsm_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def action_start(self):
for rec in self:
if not rec.start_date:
rec.start_date = datetime.now()
rec._create_order(date=rec.start_date)
rec.write({'state': 'progress'})
rec._generate_orders()

@api.multi
def action_renew(self):
Expand All @@ -119,7 +119,7 @@ def _get_rruleset(self):
if self.state != 'progress':
return ruleset
# set next_date which is used as the rrule 'dtstart' parameter
next_date = datetime.now()
next_date = self.start_date
last_order = self.env['fsm.order'].search([
('fsm_recurring_id', '=', self.id),
('stage_id', '!=', self.env.ref(
Expand Down Expand Up @@ -164,18 +164,15 @@ def _create_order(self, date):
vals = self._prepare_order_values(date)
return self.env['fsm.order'].create(vals)

@api.model
def _cron_generate_orders(self):
@api.multi
def _generate_orders(self):
"""
Executed by Cron task to create field service orders from any
recurring orders which are in progress, or to renew, and up to
the max orders allowed by the recurring order
create field service orders from self
up to the max orders allowed by the recurring order
@return {recordset} orders: all the order objects created
"""
orders = self.env['fsm.order']
for rec in self.env['fsm.recurring'].search([
('state', 'in', ('progress', 'pending'))
]):
for rec in self:
schedule_dates = rec._get_rruleset()
order_dates = []
for order in rec.fsm_order_ids:
Expand All @@ -186,11 +183,23 @@ def _cron_generate_orders(self):
for date in schedule_dates:
if date.date() in order_dates:
continue
if max_orders >= order_count or not max_orders:
orders += rec._create_order(date=date)
if max_orders > order_count or not max_orders:
orders |= rec._create_order(date=date)
order_count += 1
return orders

@api.model
def _cron_generate_orders(self):
"""
Executed by Cron task to create field service orders from any
recurring orders which are in progress, or to renew, and up to
the max orders allowed by the recurring order
@return {recordset} orders: all the order objects created
"""
return self.env['fsm.recurring'].search([
('state', 'in', ('progress', 'pending'))
])._generate_orders()

@api.model
def _cron_manage_expiration(self):
"""
Expand Down
1 change: 1 addition & 0 deletions fieldservice_recurring/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ <h2><a class="toc-backref" href="#id8">Contributors</a></h2>
<ul class="simple">
<li>Brian McMaster &lt;<a class="reference external" href="mailto:brian&#64;mcmpest.com">brian&#64;mcmpest.com</a>&gt;</li>
<li>Kitti Upariphutthiphone &lt;<a class="reference external" href="mailto:kittiu&#64;ecosoft.co.th">kittiu&#64;ecosoft.co.th</a>&gt;</li>
<li>Rapha??l Reverdy &lt;<a class="reference external" href="mailto:raphael.reverdy&#64;akretion.com">raphael.reverdy&#64;akretion.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand Down

0 comments on commit 1795f8d

Please sign in to comment.