diff --git a/modules/automation/__init__.py b/modules/automation/__init__.py index 308bd5e..a40894b 100644 --- a/modules/automation/__init__.py +++ b/modules/automation/__init__.py @@ -7,6 +7,10 @@ def register(): rules.AutomationRuleSet, freight_booking.FreightBookingInfo, booking_report.FreightBookingShipmentReport, + booking_report.FreightBookingShipmentReportLinkStart, cron.Cron, cron.AutomationCron, module='automation', type_='model') + Pool.register( + booking_report.FreightBookingShipmentReportLink, + module='automation', type_='wizard') diff --git a/modules/automation/booking_report.py b/modules/automation/booking_report.py index 2475ef2..4fc021f 100644 --- a/modules/automation/booking_report.py +++ b/modules/automation/booking_report.py @@ -5,6 +5,9 @@ from sql.functions import CurrentTimestamp from trytond.model import ModelSQL, ModelView, fields from trytond.pool import Pool +from trytond.transaction import Transaction +from trytond.wizard import Button, StateTransition, StateView, Wizard +from trytond.exceptions import UserError class FreightBookingShipmentReport(ModelSQL, ModelView): @@ -126,3 +129,65 @@ class FreightBookingShipmentReport(ModelSQL, ModelView): else 'error') for report in reports } + + +class FreightBookingShipmentReportLinkStart(ModelView): + "Link Freight Booking Shipment Report" + __name__ = 'freight.booking.shipment.report.link.start' + + bl_number = fields.Char("BL Number", readonly=True) + shipment = fields.Many2One( + 'stock.shipment.in', "Shipment", required=True) + + +class FreightBookingShipmentReportLink(Wizard): + "Link Freight Booking Shipment Report" + __name__ = 'freight.booking.shipment.report.link' + + start = StateView( + 'freight.booking.shipment.report.link.start', + 'automation.freight_booking_shipment_report_link_start_form', + [ + Button('Cancel', 'end', 'tryton-cancel'), + Button('Link', 'link', 'tryton-ok', default=True), + ]) + link = StateTransition() + + def default_start(self, fields): + Report = Pool().get('freight.booking.shipment.report') + active_ids = Transaction().context.get('active_ids') or [] + reports = Report.browse(active_ids) + error_reports = [ + report for report in reports + if report.status == 'error' and report.bl_number] + if len(error_reports) > 1: + raise UserError( + "Link one error line at a time to avoid overwriting the " + "shipment BL number.") + return { + 'bl_number': error_reports[0].bl_number if error_reports else None, + } + + def transition_link(self): + Report = Pool().get('freight.booking.shipment.report') + ShipmentIn = Pool().get('stock.shipment.in') + active_ids = Transaction().context.get('active_ids') or [] + reports = Report.browse(active_ids) + error_reports = [ + report for report in reports + if report.status == 'error' and report.bl_number] + if not error_reports: + raise UserError( + "Select at least one error line with a BL number.") + if len(error_reports) > 1: + raise UserError( + "Link one error line at a time to avoid overwriting the " + "shipment BL number.") + + shipment = self.start.shipment + shipment.bl_number = error_reports[0].bl_number + ShipmentIn.save([shipment]) + return 'end' + + def end(self): + return 'reload' diff --git a/modules/automation/booking_report.xml b/modules/automation/booking_report.xml index df857de..2014799 100644 --- a/modules/automation/booking_report.xml +++ b/modules/automation/booking_report.xml @@ -17,6 +17,21 @@ + + freight.booking.shipment.report.link.start + form + freight_booking_shipment_report_link_start_form + + + Link to Shipment + freight.booking.shipment.report.link + + + form_action + freight.booking.shipment.report,-1 + + + freight.booking.shipment.report @@ -24,6 +39,21 @@ + + freight.booking.shipment.report.link.start + + + + + + + freight.booking.shipment.report.link.start + + + + + + freight.booking.shipment.report diff --git a/modules/automation/view/freight_booking_shipment_report_link_start_form.xml b/modules/automation/view/freight_booking_shipment_report_link_start_form.xml new file mode 100644 index 0000000..1f641a5 --- /dev/null +++ b/modules/automation/view/freight_booking_shipment_report_link_start_form.xml @@ -0,0 +1,6 @@ +
+