Skip to content

Commit

Permalink
LIMS-753: Allow LN2 topups as part of dewar history
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Williams committed Nov 4, 2024
1 parent f7780e9 commit 73baa7a
Showing 1 changed file with 88 additions and 90 deletions.
178 changes: 88 additions & 90 deletions api/src/Page/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ function _add_history()

if (!$this->has_arg('BARCODE'))
$this->_error('No barcode specified');
if (!$this->has_arg('LOCATION'))
$this->_error('No location specified');
if (!$this->has_arg('LOCATION') && !$this->has_arg('STATUS'))
$this->_error('No location or status specified');

$dew = $this->db->pq("SELECT CONCAT(pe.givenname, ' ', pe.familyname) as lcout, pe.emailaddress as lcoutemail, CONCAT(CONCAT(pe2.givenname, ' '), pe2.familyname) as lcret, pe2.emailaddress as lcretemail, CONCAT(p.proposalcode, p.proposalnumber, '-', e.visit_number) as firstexp, TO_CHAR(e.startdate, 'DD-MM-YYYY HH24:MI') as firstexpst, e.beamlinename, e.beamlineoperator, d.dewarid, d.trackingnumberfromsynchrotron, s.shippingid, s.shippingname, p.proposalcode, CONCAT(p.proposalcode, p.proposalnumber) as prop, d.barcode, d.facilitycode, d.firstexperimentid, d.dewarstatus
$dew = $this->db->pq("SELECT CONCAT(pe.givenname, ' ', pe.familyname) as lcout, pe.emailaddress as lcoutemail, CONCAT(CONCAT(pe2.givenname, ' '), pe2.familyname) as lcret, pe2.emailaddress as lcretemail, CONCAT(p.proposalcode, p.proposalnumber, '-', e.visit_number) as firstexp, TO_CHAR(e.startdate, 'DD-MM-YYYY HH24:MI') as firstexpst, e.beamlinename, e.beamlineoperator, d.dewarid, d.trackingnumberfromsynchrotron, s.shippingid, s.shippingname, p.proposalcode, CONCAT(p.proposalcode, p.proposalnumber) as prop, d.barcode, d.facilitycode, d.firstexperimentid, d.dewarstatus, d.storagelocation
FROM dewar d
INNER JOIN shipping s ON s.shippingid = d.shippingid
LEFT OUTER JOIN labcontact c ON s.sendinglabcontactid = c.labcontactid
Expand All @@ -505,115 +505,113 @@ function _add_history()

$track = $this->has_arg('TRACKINGNUMBERFROMSYNCHROTRON') ? $this->arg('TRACKINGNUMBERFROMSYNCHROTRON') : $dew['TRACKINGNUMBERFROMSYNCHROTRON'];

// What was the last history entry for this dewar?
// If it's come from a beamline, register flag so we can e-mail further down...
$last_history_results = $this->db->pq("SELECT storageLocation FROM dewartransporthistory WHERE dewarId = :1 ORDER BY DewarTransportHistoryId DESC LIMIT 1", array($dew['DEWARID']));

if (sizeof($last_history_results)) {
$last_history = $last_history_results[0];
// We only add data to dewar history in lower case from this method.
// If that ever changes, update this to become case insensitive search
$last_location = $last_history['STORAGELOCATION'];
if ($this->has_arg('LOCATION')) {
// If it's come from a beamline, register flag so we can e-mail further down...
$last_location = $dew['STORAGELOCATION'];
if (!isset($dewar_complete_email_locations) || !is_array($dewar_complete_email_locations)) {
$bls = $this->_get_beamlines_from_type('all');
$send_return_email = in_array($last_location, $bls);
} else if (array_key_exists($last_location, $dewar_complete_email_locations)) {
$email_location = $dewar_complete_email_locations[$last_location];
$send_return_email = preg_match($email_location, strtolower($this->arg('LOCATION')));
}
// If dewar status is dispatch-requested - don't change it.
// This way the dewar can be moved between storage locations and keep the record that a user requested the dispatch
// Default status is 'at-facility'
$dewarstatus = strtolower($dew['DEWARSTATUS']) == 'dispatch-requested' ? 'dispatch-requested' : 'at facility';
$dewarlocation = $this->arg('LOCATION');

} else {
// No history - could be a new dewar, so not necessarily an error...
if ($this->debug)
error_log("No previous dewar transport history for DewarId " . $dew['DEWARID']);
// just a status given
$dewarstatus = $this->arg('STATUS');
$dewarlocation = $dew['STORAGELOCATION'];
}
// If dewar status is dispatch-requested - don't change it.
// This way the dewar can be moved between storage locations and keep the record that a user requested the dispatch
// Default status is 'at-facility'
$dewarstatus = strtolower($dew['DEWARSTATUS']) == 'dispatch-requested' ? 'dispatch-requested' : 'at facility';

$this->db->pq("INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate) VALUES (s_dewartransporthistory.nextval,:1,lower(:2),lower(:3),CURRENT_TIMESTAMP) RETURNING dewartransporthistoryid INTO :id", array($dew['DEWARID'], $dewarstatus, $this->arg('LOCATION')));
$this->db->pq("INSERT INTO dewartransporthistory (dewartransporthistoryid,dewarid,dewarstatus,storagelocation,arrivaldate) VALUES (s_dewartransporthistory.nextval,:1,lower(:2),lower(:3),CURRENT_TIMESTAMP) RETURNING dewartransporthistoryid INTO :id", array($dew['DEWARID'], $dewarstatus, $dewarlocation));
$dhid = $this->db->id();

$this->db->pq("UPDATE dewar set dewarstatus=lower(:4), storagelocation=lower(:2), trackingnumberfromsynchrotron=:3 WHERE dewarid=:1", array($dew['DEWARID'], $this->arg('LOCATION'), $track, $dewarstatus));
$this->db->pq("UPDATE shipping set shippingstatus=lower(:2) WHERE shippingid=:1", array($dew['SHIPPINGID'], $dewarstatus));

$containers = $this->db->pq("SELECT containerid
FROM container
WHERE dewarid=:1", array($dew['DEWARID']));
foreach ($containers as $c) {
$this->db->pq("INSERT INTO containerhistory (containerid,status) VALUES (:1,:2)", array($c['CONTAINERID'], 'at facility'));
}

// Email
// EHCs, local contact(s), labcontact, dh, pa
$dew['NOW'] = strftime('%d-%m-%Y %H:%M');
$dew['INCONTACTS'] = $in_contacts;
$dew['TRACKINGNUMBERFROMSYNCHROTRON'] = $track;

if (strtolower($this->arg('LOCATION')) == 'stores-in' && $dew['LCOUTEMAIL']) {
$lcs = $this->db->pq("SELECT p.login
FROM person p
INNER JOIN session_has_person shp ON shp.personid = p.personid
WHERE shp.sessionid=:1 AND (shp.role = 'Local Contact' OR shp.role = 'Local Contact 2')", array($dew['FIRSTEXPERIMENTID']));
$emails = array($dew['LCOUTEMAIL'], $arrival_email);
foreach ($lcs as $lc) {
array_push($emails, $this->_get_email($lc['LOGIN']));
}
if ($this->has_arg('LOCATION')) {
$this->db->pq("UPDATE dewar set dewarstatus=lower(:4), storagelocation=lower(:2), trackingnumberfromsynchrotron=:3 WHERE dewarid=:1", array($dew['DEWARID'], $this->arg('LOCATION'), $track, $dewarstatus));
$this->db->pq("UPDATE shipping set shippingstatus=lower(:2) WHERE shippingid=:1", array($dew['SHIPPINGID'], $dewarstatus));

$email = new Email($dew['PROPOSALCODE'] == 'in' ? 'dewar-stores-in-in' : 'dewar-stores-in', '*** Dewar Received for ' . $dew['PROP'] . ' at ' . $dew['NOW'] . ' ***');
$email->data = $dew;
$email->send(implode(', ', $emails));
}
$containers = $this->db->pq("SELECT containerid
FROM container
WHERE dewarid=:1", array($dew['DEWARID']));
foreach ($containers as $c) {
$this->db->pq("INSERT INTO containerhistory (containerid,status) VALUES (:1,:2)", array($c['CONTAINERID'], 'at facility'));
}

if (strtolower($this->arg('LOCATION')) == 'stores-out' && $dew['LCRETEMAIL']) {
$email = new Email('dewar-stores-out', '*** Dewar ready to leave Synchrotron ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL']);
}
// Email
// EHCs, local contact(s), labcontact, dh, pa
$dew['NOW'] = strftime('%d-%m-%Y %H:%M');
$dew['INCONTACTS'] = $in_contacts;
$dew['TRACKINGNUMBERFROMSYNCHROTRON'] = $track;

if (strtolower($this->arg('LOCATION')) == 'stores-in' && $dew['LCOUTEMAIL']) {
$lcs = $this->db->pq("SELECT p.login
FROM person p
INNER JOIN session_has_person shp ON shp.personid = p.personid
WHERE shp.sessionid=:1 AND (shp.role = 'Local Contact' OR shp.role = 'Local Contact 2')", array($dew['FIRSTEXPERIMENTID']));
$emails = array($dew['LCOUTEMAIL'], $arrival_email);
foreach ($lcs as $lc) {
array_push($emails, $this->_get_email($lc['LOGIN']));
}

if (strpos(strtolower($this->arg('LOCATION')), '-rack') !== false && $dew['LCRETEMAIL']) {
$dew['LOCATION'] = $this->arg('LOCATION');
$email = new Email($dew['PROPOSALCODE'] == 'in' ? 'dewar-stores-in-in' : 'dewar-stores-in', '*** Dewar Received for ' . $dew['PROP'] . ' at ' . $dew['NOW'] . ' ***');
$email->data = $dew;
$email->send(implode(', ', $emails));
}

$email = new Email('dewar-rack', '*** Dewar now outside Beamline ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL']);
}
if (strtolower($this->arg('LOCATION')) == 'stores-out' && $dew['LCRETEMAIL']) {
$email = new Email('dewar-stores-out', '*** Dewar ready to leave Synchrotron ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL']);
}

if ($dew['LCRETEMAIL'] && $send_return_email) {
// Any data collections for this dewar's containers?
// Note this counts data collection ids for containers and uses the DataCollection.SESSIONID to determine the session/visit
// Should work for UDC (where container.sessionid is set) as well as any normal scheduled session (where container.sessionid is not set)
$rows = $this->db->pq("SELECT CONCAT(p.proposalcode, p.proposalnumber, '-', ses.visit_number) as visit, dc.sessionid, count(dc.datacollectionid) as dccount
FROM Dewar d
INNER JOIN Container c on c.dewarid = d.dewarid
INNER JOIN BLSample bls ON bls.containerid = c.containerid
INNER JOIN DataCollection dc ON dc.blsampleid = bls.blsampleid
INNER JOIN BLSession ses ON dc.sessionid = ses.sessionid
INNER JOIN Proposal p ON p.proposalid = ses.proposalid
WHERE d.dewarid = :1
GROUP BY dc.sessionid", array($dew['DEWARID']));
if (strpos(strtolower($this->arg('LOCATION')), '-rack') !== false && $dew['LCRETEMAIL']) {
$dew['LOCATION'] = $this->arg('LOCATION');

if (sizeof($rows))
$dew['DC'] = $rows;
$email = new Email('dewar-rack', '*** Dewar now outside Beamline ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL']);
}

$cc = array($dewar_complete_email ? $dewar_complete_email : null);
if ($dew['LCRETEMAIL'] && $send_return_email) {
// Any data collections for this dewar's containers?
// Note this counts data collection ids for containers and uses the DataCollection.SESSIONID to determine the session/visit
// Should work for UDC (where container.sessionid is set) as well as any normal scheduled session (where container.sessionid is not set)
$rows = $this->db->pq("SELECT CONCAT(p.proposalcode, p.proposalnumber, '-', ses.visit_number) as visit, dc.sessionid, count(dc.datacollectionid) as dccount
FROM Dewar d
INNER JOIN Container c on c.dewarid = d.dewarid
INNER JOIN BLSample bls ON bls.containerid = c.containerid
INNER JOIN DataCollection dc ON dc.blsampleid = bls.blsampleid
INNER JOIN BLSession ses ON dc.sessionid = ses.sessionid
INNER JOIN Proposal p ON p.proposalid = ses.proposalid
WHERE d.dewarid = :1
GROUP BY dc.sessionid", array($dew['DEWARID']));

if (sizeof($rows))
$dew['DC'] = $rows;

$cc = array($dewar_complete_email ? $dewar_complete_email : null);

$owners = $this->db->pq("SELECT p.emailaddress
FROM Container c
INNER JOIN Person p ON c.ownerId = p.personId
WHERE c.dewarId = :1
GROUP BY p.emailaddress", array($dew['DEWARID']));

foreach ($owners as $owner) {
if ($owner['EMAILADDRESS'] != '') array_push($cc, $owner['EMAILADDRESS']);
}

$owners = $this->db->pq("SELECT p.emailaddress
FROM Container c
INNER JOIN Person p ON c.ownerId = p.personId
WHERE c.dewarId = :1
GROUP BY p.emailaddress", array($dew['DEWARID']));
// Log the event if debugging
if ($this->debug) error_log("Dewar " . $dew['DEWARID'] . " back from beamline...");

foreach ($owners as $owner) {
if ($owner['EMAILADDRESS'] != '') array_push($cc, $owner['EMAILADDRESS']);
$email = new Email('storage-rack', '*** Visit finished, dewar awaiting instructions ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL'], implode(', ', $cc));
}

// Log the event if debugging
if ($this->debug) error_log("Dewar " . $dew['DEWARID'] . " back from beamline...");

$email = new Email('storage-rack', '*** Visit finished, dewar awaiting instructions ***');
$email->data = $dew;
$email->send($dew['LCRETEMAIL'], implode(', ', $cc));
}

$this->_output(array('DEWARHISTORYID' => $dhid));
Expand Down

0 comments on commit 73baa7a

Please sign in to comment.