@@ -146,6 +146,7 @@ class Shipment extends Page
146146 'TOKEN ' => '\w+ ' ,
147147 'tracking_number ' => '\w+ ' ,
148148 'AWBURL ' => '[\w\:\/\.\-]+ ' ,
149+ 'pickup_confirmation_code ' => '\w+ ' ,
149150
150151 'manifest ' => '\d ' ,
151152 'currentuser ' => '\d ' ,
@@ -200,6 +201,7 @@ class Shipment extends Page
200201 array ('/dewars/transfer ' , 'post ' , '_transfer_dewar ' ),
201202 array ('/dewars/dispatch ' , 'post ' , '_dispatch_dewar ' ),
202203 array ('/dewars/confirmdispatch/did/:did/token/:TOKEN ' , 'post ' , '_dispatch_dewar_confirmation ' ),
204+ array ('/dewars/confirmpickup/sid/:sid/token/:TOKEN ' , 'post ' , '_pickup_dewar_confirmation ' ),
203205
204206 array ('/dewars/tracking(/:DEWARID) ' , 'get ' , '_get_dewar_tracking ' ),
205207
@@ -1444,7 +1446,70 @@ function _dispatch_dewar_confirmation()
14441446 $ this ->_output (1 );
14451447 }
14461448
1449+ function _pickup_dewar_confirmation ()
1450+ {
1451+ if (!$ this ->has_arg ('sid ' ))
1452+ $ this ->_error ('No shipment specified ' );
1453+ if (!$ this ->has_arg ('TOKEN ' ))
1454+ $ this ->_error ('No token specified ' );
1455+ if (!$ this ->has_arg ('tracking_number ' ))
1456+ $ this ->_error ('No tracking number specified ' );
1457+
1458+ // Check token against each dewar
1459+ $ dewars = $ this ->db ->pq (
1460+ "SELECT d.dewarid,
1461+ json_unquote(json_extract(d.extra, '$.token')) as token
1462+ FROM dewar d
1463+ WHERE d.shippingid=:1 " ,
1464+ array ($ this ->arg ('sid ' ))
1465+ );
1466+
1467+ foreach ($ dewars as $ dew ) {
1468+ if ($ this ->arg ('TOKEN ' ) !== $ dew ['TOKEN ' ]) {
1469+ $ this ->_error ('Incorrect token ' );
1470+ }
1471+ }
14471472
1473+ $ this ->db ->pq ("UPDATE shipping set shippingstatus='awb created' WHERE shippingid=:1 " , array ($ this ->arg ('sid ' )));
1474+
1475+ foreach ($ dewars as $ dew ) {
1476+ // Update the dewar status and storage location
1477+ $ this ->db ->pq (
1478+ "UPDATE dewar
1479+ set dewarstatus='awb created', storagelocation='off-site', trackingnumbertosynchrotron=:2
1480+ WHERE dewarid=:1 " ,
1481+ array ($ dew ['DEWARID ' ], $ this ->arg ('tracking_number ' ))
1482+ );
1483+
1484+ // Update dewar transport history
1485+ $ this ->db ->pq (
1486+ "INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate)
1487+ VALUES (s_dewartransporthistory.nextval,:1,'awb created','off-site',CURRENT_TIMESTAMP)
1488+ RETURNING dewartransporthistoryid INTO :id " ,
1489+ array ($ dew ['DEWARID ' ])
1490+ );
1491+ }
1492+
1493+ if ($ this ->has_arg ('pickup_confirmation_code ' )) {
1494+
1495+ $ this ->db ->pq ("UPDATE shipping set shippingstatus='pickup booked' WHERE shippingid=:1 " , array ($ this ->arg ('sid ' )));
1496+
1497+ foreach ($ dewars as $ dew ) {
1498+ // Update the dewar status
1499+ $ this ->db ->pq ("UPDATE dewar set dewarstatus='pickup booked' WHERE dewarid=:1 " , array ($ dew ['DEWARID ' ]));
1500+
1501+ // Update dewar transport history (plus 1s so history appears in order)
1502+ $ this ->db ->pq (
1503+ "INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate)
1504+ VALUES (s_dewartransporthistory.nextval,:1,'pickup booked','off-site',CURRENT_TIMESTAMP+1)
1505+ RETURNING dewartransporthistoryid INTO :id " ,
1506+ array ($ dew ['DEWARID ' ])
1507+ );
1508+ }
1509+ }
1510+
1511+ $ this ->_output (1 );
1512+ }
14481513
14491514 function _get_dewar_tracking ()
14501515 {
@@ -3086,20 +3151,27 @@ function($package, $index) {return array("piecenumber" => $index+1, "licenseplat
30863151 function _create_shipment_shipment_request ($ shipment , array $ dewars ): int
30873152 {
30883153
3089- // if (!is_null($shipment['EXTERNALSHIPPINGIDTOSYNCHROTRON'])) {
3090- // return $shipment['EXTERNALSHIPPINGIDTOSYNCHROTRON'];
3091- // }
3154+ $ shipping_id = (int ) $ shipment ['SHIPPINGID ' ];
3155+
3156+ $ token = md5 (openssl_random_pseudo_bytes (7 ));
3157+ $ this ->db ->pq (
3158+ "UPDATE dewar SET extra = JSON_SET(IFNULL(extra, '{}'), '$.token', :1 ) WHERE shippingid=:2 " ,
3159+ array ($ token , $ shipping_id )
3160+ );
3161+
3162+ $ callback_url = "/api/shipment/dewars/confirmpickup/sid/ {$ shipping_id }/token/ {$ token }" ;
30923163
30933164 $ external_shipping_id = $ this ->_create_dewars_shipment_request (
30943165 $ dewars ,
30953166 $ shipment ['PROP ' ],
3096- (int ) $ shipment ['SHIPPINGID ' ],
3097- (int ) $ shipment ['SHIPPINGID ' ]
3167+ $ shipping_id ,
3168+ $ shipping_id ,
3169+ $ callback_url
30983170 );
30993171
31003172 $ this ->db ->pq (
31013173 "UPDATE shipping SET externalShippingIdToSynchrotron=:1 WHERE shippingId=:2 " ,
3102- array ($ external_shipping_id , $ shipment [ ' SHIPPINGID ' ] )
3174+ array ($ external_shipping_id , $ shipping_id )
31033175 );
31043176 return $ external_shipping_id ;
31053177 }
0 commit comments