diff --git a/modules/automation/__init__.py b/modules/automation/__init__.py index 5944d8f..a934ce9 100644 --- a/modules/automation/__init__.py +++ b/modules/automation/__init__.py @@ -1,9 +1,10 @@ from trytond.pool import Pool -from . import automation,rules,freight_booking #, document +from . import automation,rules,freight_booking,cron #, document def register(): Pool.register( automation.AutomationDocument, rules.AutomationRuleSet, freight_booking.FreightBookingInfo, + cron.Cron, module='automation', type_='model') \ No newline at end of file diff --git a/modules/automation/cron.py b/modules/automation/cron.py new file mode 100644 index 0000000..9031582 --- /dev/null +++ b/modules/automation/cron.py @@ -0,0 +1,118 @@ +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 PriceCron(ModelSQL, ModelView): + "Price 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_forex(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('stock.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.vessel = Vessel.getVesselByName(vessel) + sh.bl_number = bl_number + sh.bl_date = bl_date + sh.etd = etd_date + ShipmentIn.save([sh]) + logger.info("SHIPMENT_CREATED:%s",sh) diff --git a/modules/automation/cron.xml b/modules/automation/cron.xml new file mode 100644 index 0000000..ef872b9 --- /dev/null +++ b/modules/automation/cron.xml @@ -0,0 +1,37 @@ + + + + + automation.cron + tree + cron_list + + + + automation.cron + form + cron_form + + + + Update shipment from freight booking + automation.cron + + + + + + + + + + + + + + automation.cron|update_shipment + + days + + + diff --git a/modules/automation/tryton.cfg b/modules/automation/tryton.cfg index 0ddc3cc..c8027fb 100644 --- a/modules/automation/tryton.cfg +++ b/modules/automation/tryton.cfg @@ -6,4 +6,5 @@ depends: document_incoming xml: automation.xml - freight_booking.xml \ No newline at end of file + freight_booking.xml + cron.xml \ No newline at end of file diff --git a/modules/purchase_trade/party.py b/modules/purchase_trade/party.py index dcebfda..331c78a 100755 --- a/modules/purchase_trade/party.py +++ b/modules/purchase_trade/party.py @@ -22,3 +22,16 @@ class Party(metaclass=PoolMeta): wb = fields.Many2One('purchase.weight.basis',"Weight basis") association = fields.Many2One('purchase.association',"Association") execution = fields.One2Many('party.execution','party',"") + + @classmethod + def getPartyByName(cls, party, category=None): + party = party.upper() + p = cls.search([('name', '=', party)], limit=1) + if p: + return p[0].id + else: + p = cls() + p.name = party + cls.save([p]) + return p.id + \ No newline at end of file diff --git a/modules/purchase_trade/stock.py b/modules/purchase_trade/stock.py index 6183c3a..c519abe 100755 --- a/modules/purchase_trade/stock.py +++ b/modules/purchase_trade/stock.py @@ -24,16 +24,21 @@ class Location(metaclass=PoolMeta): __name__ = 'stock.location' @classmethod - def get_transit_id(cls): - transit = cls.search([('name', '=', 'Transit')], limit=1) - if transit: - return transit[0].id + def getLocationByName(cls, location, type): + location = location.upper() + loc = cls.search([('name', '=', location),('type', '=', type)], limit=1) + if loc: + return loc[0].id else: - transit = cls() - transit.name = 'Transit' - transit.type = 'storage' - cls.save([transit]) - return transit.id + loc = cls() + loc.name = location + loc.type = type + cls.save([loc]) + return loc.id + + @classmethod + def get_transit_id(cls): + return cls.getLocationByName('TRANSIT','storage') def is_transit(self): if self.name == 'Transit': diff --git a/modules/stock/vessel.py b/modules/stock/vessel.py index 3f0cdd2..07e595e 100755 --- a/modules/stock/vessel.py +++ b/modules/stock/vessel.py @@ -93,6 +93,19 @@ class Vessel( #logger.info("GET_INFO:%s",aux) return inf + @classmethod + def getVesselByName(cls, vessel, imo=None): + vessel = vessel.upper() + v = cls.search([('vessel_name', '=', vessel)], limit=1) + if v: + return v[0].id + else: + v = cls() + v.vessel_name = vessel + v.vessel_imo = imo + cls.save([v]) + return v.id + @classmethod def remover(cls,tr = ""): char_to_keep = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")