# -*- encoding: utf-8 -*-


from openerp import models, fields, api, _
from openerp.osv import fields as ofields
from openerp.exceptions import Warning
from datetime import datetime as time
import openerp.addons.decimal_precision as dp    

class ez_sale_info(models.Model):
    _name = 'ez.sale.info'
    
    @api.model
    def get_sale_line(self):
        if self._context['sale_line']:
            return self.env['sale.order.line'].browse(self._context['sale_line'])
        else:
            return 0
    
    @api.one
    def confirm(self):
        return 1
    
    name = fields.Many2one('sale.order.line', 'Order Line', default=get_sale_line)
    child_ids = fields.One2many('ez.sale.order.line.wizard', 'parent_id', 'Children')

class ez_sale_order_line_wizard(models.Model):
    _name = 'ez.sale.order.line.wizard'
    
    @api.model
    def get_delivery_man(self):
        if self._context['sale_line']:
            return self.env['sale.order.line'].browse(self._context['sale_line']).order_id.ez_delivery_man
        else:
            return 0
    
    @api.model
    def get_qty(self):
        if self._context['sale_line']:
            return self.env['sale.order.line'].browse(self._context['sale_line']).product_uom_qty
        else:
            return 0
        
    @api.model
    def ez_esign_on_stock_move(self):
        if self._context['sale_line']:
            return 'Minh'
        else:
            return 0

    @api.onchange('ez_qty')
    def onchange_ez_qty(self):
        warning = {}
        result = {}
        if self.parent_id:
            if self.parent_id.name.product_uom_qty < self.ez_qty:
                warning = {
                    'title': 'Warning',
                    'message': 'Quantity is over. Please check again!'}
                result = {'ez_qty': 0}
        else:
                result = {}
        return {'values': result, 'warning': warning}


    ez_shipping_name = fields.Char('Name')
    ez_delivery_to = fields.Text('Delivery to')
    ez_qty = fields.Integer('Quantity', default=get_qty)
    ez_delivery_address = fields.Text('Address')
    parent_id = fields.Many2one('ez.sale.info', 'Parent')
    ez_delivery_man_wz = fields.Many2one('res.users', 'Delivery Man', default=get_delivery_man)
    ez_shipping_datetime = fields.Char('Datetime')
