This commit is contained in:
2026-01-20 11:04:53 +01:00
parent c39bb1d6d1
commit a479dc718b
7 changed files with 199 additions and 11 deletions

View File

@@ -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')

118
modules/automation/cron.py Normal file
View File

@@ -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)

View File

@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<tryton>
<data>
<record model="ir.ui.view" id="cron_view_list">
<field name="model">automation.cron</field>
<field name="type">tree</field>
<field name="name">cron_list</field>
</record>
<record model="ir.ui.view" id="cron_view_form">
<field name="model">automation.cron</field>
<field name="type">form</field>
<field name="name">cron_form</field>
</record>
<record model="ir.action.act_window" id="act_cron_form">
<field name="name">Update shipment from freight booking</field>
<field name="res_model">automation.cron</field>
</record>
<record model="ir.action.act_window.view" id="act_cron_form_view1">
<field name="sequence" eval="10"/>
<field name="view" ref="cron_view_list"/>
<field name="act_window" ref="act_cron_form"/>
</record>
<record model="ir.action.act_window.view" id="act_cron_form_view2">
<field name="sequence" eval="20"/>
<field name="view" ref="cron_view_form"/>
<field name="act_window" ref="act_cron_form"/>
</record>
<record model="ir.cron" id="cron_cron">
<field name="method">automation.cron|update_shipment</field>
<field name="interval_number" eval="1"/>
<field name="interval_type">days</field>
</record>
</data>
</tryton>

View File

@@ -6,4 +6,5 @@ depends:
document_incoming
xml:
automation.xml
freight_booking.xml
freight_booking.xml
cron.xml