# -*- coding: utf-8 -*-

from odoo import models, fields, api
from datetime import datetime


class SaleOrderForecast(models.Model):
    _inherit = 'sale.order'

    dayofweek = fields.Integer(
        string='День недели',
        compute='_compute_dayofweek',
        store=True,
    )

    @api.depends('date_order')
    def _compute_dayofweek(self):
        for s in self:
            if s.date_order:
                order_dt = s.date_order if isinstance(s.date_order, datetime) else datetime.combine(s.date_order, datetime.min.time())
                s.dayofweek = order_dt.weekday()
            else:
                s.dayofweek = 0

    @api.model
    def conf_mass(self, date_start=False, date_end=False):
        if date_start and date_end:
            orders = self.env['sale.order'].search([
                ('date_order', '<=', date_end),
                ('date_order', '>=', date_start),
                ('state', '=', 'draft'),
            ], limit=15)
            for order in orders:
                order.action_confirm()
