import requests from decimal import getcontext, Decimal, ROUND_HALF_UP from datetime import datetime from trytond.model import fields from trytond.model import (ModelSQL, ModelView) from trytond.pool import Pool, PoolMeta from trytond.transaction import Transaction import logging from sql import Table logger = logging.getLogger(__name__) class Cron(metaclass=PoolMeta): __name__ = 'ir.cron' @classmethod def __setup__(cls): super().__setup__() cls.method.selection.append( ('automation.cron|update_shipment', "Update Shipment from freight booking info")) class AutomationCron(ModelSQL, ModelView): "Automation Cron" __name__ = 'automation.cron' frequency = fields.Selection([ ('daily', "Daily"), ('weekly', "Weekly"), ('monthly', "Monthly"), ], "Frequency", required=True, help="How frequently rates must be updated.") last_update = fields.Date("Last Update", required=True) @classmethod def __setup__(cls): super().__setup__() cls._buttons.update({ 'run': {}, }) @classmethod def default_frequency(cls): return 'daily' @classmethod def default_last_update(cls): pool = Pool() Date = pool.get('ir.date') return Date.today() @classmethod @ModelView.button def run(cls, crons): cls.update_shipment(crons) @classmethod def update_shipment(cls): t = Table('freight_booking_info') cursor = Transaction().connection.cursor() cursor.execute(*t.select( t.ShippingInstructionNumber, t.ShippingInstructionDate, t.ShippingInstructionQuantity, t.ShippingInstructionQuantityUnit, t.NumberOfContainers, t.ContainerType, t.Loading, t.Destination, t.BookingAgent, t.Carrier, t.Vessel, t.BL_Number, t.ETD_Date, t.BL_Date, t.ExpectedController, t.Comments, t.FintradeBookingKey, )) for row in cursor.fetchall(): ( si_number, si_date, si_quantity, si_unit, container_number, container_type, loading, destination, agent, carrier, vessel, bl_number, etd_date, bl_date, controller, comments, fintrade_booking_key, ) = row logger.info("ROW_FROM_CRON:%s",row) Location = Pool().get('stock.location') Vessel = Pool().get('trade.vessel') Party = Pool().get('party.party') ShipmentIn = Pool().get('stock.shipment.in') shipment = ShipmentIn.search(['reference','=',si_number]) if not shipment: sh = ShipmentIn() sh.reference = si_number sh.from_location = Location.getLocationByName(loading,'supplier') sh.to_location = Location.getLocationByName(destination,'customer') sh.carrier = Party.getPartyByName(carrier) sh.supplier = Party.getPartyByName(agent) sh.vessel = Vessel.getVesselByName(vessel) sh.cargo_mode = 'bulk' sh.bl_number = bl_number sh.bl_date = bl_date sh.etd = etd_date ShipmentIn.save([sh]) logger.info("SHIPMENT_CREATED:%s",sh)