You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
7.9 KiB

# © 2019 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api
# import dateutil.rrule as rrule
# from datetime import date
from dateutil.relativedelta import *
from dateutil.easter import *
from dateutil.rrule import *
from dateutil.parser import *
from datetime import *
class VracoopPointRetrait(models.Model):
_name = "vracoop.point.retrait"
_description = "Point de retrait"
_inherit = ['website.published.multi.mixin']
@api.model
def default_get(self, fields):
res = super(VracoopPointRetrait, self).default_get(fields)
vracoop_time_ids = self.env['vracoop.time'].search([])
vracoop_retrait_time_ids = []
for vracoop_time_id in vracoop_time_ids:
if vracoop_time_id.name == 'samedi' or vracoop_time_id.name == 'dimanche':
active_day = False
else:
active_day = True
vals = {
'vracoop_time_id': vracoop_time_id.id,
'name': vracoop_time_id.name,
'first_morning_heure': vracoop_time_id.first_morning_heure,
'last_morning_heure': vracoop_time_id.last_morning_heure,
'first_noon_heure': vracoop_time_id.first_noon_heure,
'last_noon_heure': vracoop_time_id.last_noon_heure,
'preparation_time': vracoop_time_id.preparation_time,
'availability_time': vracoop_time_id.availability_time,
'active_day': active_day
}
vracoop_retrait_time_ids.append((0, 0, vals))
res.update({'vracoop_retrait_time_ids': vracoop_retrait_time_ids})
return res
name = fields.Char("Nom du point relais")
active = fields.Boolean(default=True)
street = fields.Char()
street2 = fields.Char()
zip = fields.Char(change_default=True)
city = fields.Char()
state_id = fields.Many2one(
"res.country.state",
string='State',
ondelete='restrict',
domain="[('country_id', '=?', country_id)]")
country_id = fields.Many2one(
'res.country', string='Country', ondelete='restrict')
image = fields.Binary("Image", attachment=True,)
image_medium = fields.Binary("Medium-sized image", attachment=True)
image_small = fields.Binary("Small-sized image", attachment=True)
vracoop_retrait_time_ids = fields.One2many(
comodel_name='vracoop.retrait.time',
inverse_name='vracoop_point_retrait_id',
string="Configuration des horaires")
nb_max_retrait = fields.Integer(
"Nombre de retrait max par tranche horaire")
nb_day_available = fields.Integer("Nombre de jours pour commande")
@api.multi
def slot_calculate(self):
self.ensure_one()
LIST_WEEK_DAY = [
('lundi',0),
('mardi',1),
('mercredi',2),
('jeudi',3),
('vendredi',4),
('samedi',5),
('dimanche',6),
]
for rec in self:
my_datetime = datetime.today()
return_slot_list = []
vals = []
list_week = list(rrule(DAILY, count=rec.nb_day_available, dtstart=datetime.today()))
for week in list_week:
corresponding_line = rec.vracoop_retrait_time_ids.search([
('vracoop_point_retrait_id', '=', rec.id), ('name', '=', week.strftime("%A"))])
for week_day in LIST_WEEK_DAY:
if week_day[0] == week.strftime("%A"):
byweekday = week_day[1]
time_available_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.availability_time)
hour = time_available_week.strftime("%H")
minute = time_available_week.strftime("%M")
interval = int(hour)*60 + int(minute)
if week.day == my_datetime.day:
first_morning_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.first_morning_heure)
last_morning_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.last_morning_heure)
first_noon_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.first_noon_heure)
last_noon_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.last_noon_heure)
today_hour_available = my_datetime + timedelta(hours=corresponding_line.preparation_time)
if (today_hour_available > first_morning_hour_week) and (today_hour_available < last_morning_hour_week):
dtstart_morning = today_hour_available
dtstart_noon = first_noon_hour_week
elif (today_hour_available > first_noon_hour_week) and (today_hour_available < last_noon_hour_week):
dtstart_morning = today_hour_available
dtstart_noon = today_hour_available
elif (today_hour_available > last_morning_hour_week) and (today_hour_available < first_noon_hour_week):
dtstart_morning = today_hour_available
dtstart_noon = first_noon_hour_week
else:
dtstart_morning = today_hour_available
dtstart_noon = today_hour_available
list_slot_per_day_morning = list(rrule(MINUTELY, interval=interval,
byweekday=byweekday,
dtstart=dtstart_morning,
until=last_morning_hour_week))
list_slot_per_day_noon = list(rrule(MINUTELY, interval=interval,
byweekday=byweekday,
dtstart=dtstart_noon,
until=last_noon_hour_week))
else:
first_morning_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.first_morning_heure)
last_morning_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.last_morning_heure)
first_noon_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.first_noon_heure)
last_noon_hour_week = datetime(week.year, week.month, week.day) + timedelta(hours=corresponding_line.last_noon_heure)
list_slot_per_day_morning = list(rrule(MINUTELY, interval=interval,
byweekday=byweekday,
dtstart=first_morning_hour_week,
until=last_morning_hour_week))
list_slot_per_day_noon = list(rrule(MINUTELY, interval=interval,
byweekday=byweekday,
dtstart=first_noon_hour_week,
until=last_noon_hour_week))
slots = []
for slot_per_day_morning in list_slot_per_day_morning:
first_slot = slot_per_day_morning.strftime("%H:%M")
slot_per_day_morning = slot_per_day_morning + timedelta(hours=corresponding_line.availability_time)
last_slot = slot_per_day_morning.strftime("%H:%M")
slots.append((first_slot,last_slot))
for slot_per_day_noon in list_slot_per_day_noon:
first_slot = slot_per_day_noon.strftime("%H:%M")
slot_per_day_noon = slot_per_day_noon + timedelta(hours=corresponding_line.availability_time)
last_slot = slot_per_day_noon.strftime("%H:%M")
slots.append((first_slot,last_slot))
return_slot_list = slots
vals.append((week.strftime("%a"), week, week.strftime("%b"), return_slot_list))
return vals