from trytond.model import ModelSQL, ModelView, fields
from sql import Table
from sql.functions import CurrentTimestamp
from sql import Column, Literal
import logging
logger = logging.getLogger(__name__)

class FreightBookingInfo(ModelSQL, ModelView):
    "Freight Booking"
    __name__ = 'freight.booking.info'

    booking_number = fields.Char("Booking Number")
    agent = fields.Char("Agent")
    controller = fields.Char("Customer")
    origin = fields.Char("Origin")
    destination = fields.Char("Destination")
    etd = fields.Date("ETD")
    bl_date = fields.Date("BL date")
    bl_number = fields.Char("BL Nb")
    carrier = fields.Char("Carrier")
    vessel = fields.Char("Vessel")
    container_count = fields.Float("Containers")
    quantity = fields.Float("Gross Weight")

    @classmethod
    def table_query(cls):
        t = Table('freight_booking_info')
         
        query = t.select(
            Literal(None).as_('create_uid'),
            CurrentTimestamp().as_('create_date'),
            Literal(None).as_('write_uid'),
            Literal(None).as_('write_date'),
            Column(t, 'FintradeBookingKey').as_('id'),
            Column(t, 'ShippingInstructionNumber').as_('booking_number'),
            Column(t, 'BookingAgent').as_('agent'),
            Column(t, 'ExpectedController').as_('controller'),
            Column(t, 'Loading').as_('origin'),
            Column(t, 'Destination').as_('destination'),
            Column(t, 'ETD_Date').as_('etd'),
            Column(t, 'BL_Date').as_('bl_date'),
            Column(t, 'BL_Number').as_('bl_number'),
            Column(t, 'Carrier').as_('carrier'),
            Column(t, 'Vessel').as_('vessel'),
            Column(t, 'NumberOfContainers').as_('container_count'),
            Column(t, 'ShippingInstructionQuantity').as_('quantity'),
        )
        #logger.info("*****QUERY*****:%s",query)
        return query

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls._order = [
            ('etd', 'DESC'),
        ]
