diff --git a/config/services.yaml b/config/services.yaml index a5d7d05f..825398eb 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -328,3 +328,15 @@ services: App\Service\WarrantySerialLoadLogger: arguments: $em: "@doctrine.orm.entity_manager" + + # entity listener for the third party JO status + App\EntityListener\JobOrderStatusListener: + arguments: + $jo_manager: "@App\\Service\\JobOrderManager" + tags: + - name: doctrine.orm.entity_listener + event: 'preUpdate' + entity: 'App\Entity\JobOrder' + - name: doctrine.orm.entity_listener + event: 'postPersist' + entity: 'App\Entity\JobOrder' diff --git a/src/EntityListener/JobOrderStatusListener.php b/src/EntityListener/JobOrderStatusListener.php new file mode 100644 index 00000000..bb08b7bb --- /dev/null +++ b/src/EntityListener/JobOrderStatusListener.php @@ -0,0 +1,59 @@ +jo_manager = $jo_manager; + } + + public function preUpdate(JobOrder $jo, PreUpdateEventArgs $args) + { + // error_log('PRE UPDATE'); + + // check if status of third party JO has changed + // check if JO is third party + $source = $jo->getJOSource(); + if ($source != null) + { + if ($args->hasChangedField('status')) + { + // error_log('status has changed'); + // send callback + $this->jo_manager->sendJOStatusCallback($jo); + } + } + } + + public function postUpdate(JobOrder $jo, LifecycleEventArgs $args) + { + // this is for updating of status of third party JOs + } + + public function postPersist(JobOrder $jo, LifecycleEventArgs $args) + { + // error_log('POST PERSIST'); + + // check if JO is third party + $source = $jo->getJOSource(); + if ($source != null) + { + // need to send a callback for a new third party JO + $this->jo_manager->sendJOStatusCallback($jo); + } + } +} diff --git a/src/Service/JobOrderManager.php b/src/Service/JobOrderManager.php index c54557d6..fea58b53 100644 --- a/src/Service/JobOrderManager.php +++ b/src/Service/JobOrderManager.php @@ -5,6 +5,7 @@ namespace App\Service; use App\Entity\JobOrder; use App\Ramcar\ServiceType; +use App\Ramcar\JOStatus; use Doctrine\ORM\EntityManagerInterface; @@ -51,4 +52,56 @@ class JobOrderManager $this->em->flush(); } } + + public function sendJOStatusCallback(JobOrder $jo) + { + // get the job order source + $source = $jo->getJOSource(); + if ($source != null) + { + // check if source has a callback url + $callback_url = $source->getCallbackURL(); + if ($callback_url != null) + { + // form the body for the callback + // jo id and jo status + $jo_data = [ + 'id' => $jo->getID(), + 'status' => JOStatus::getName($jo->getStatus()), + ]; + + // send status + $this->sendJOInfo($jo_data, $callback_url); + } + } + } + + protected function sendJOInfo($jo_info, $callback_url) + { + $body = json_encode($jo_info); + + // error_log(print_r($body, true)); + + // error_log('Sending json output to ' . $callback_url); + + $curl = curl_init(); + + $options = [ + CURLOPT_URL => $callback_url, + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => $body, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + ], + ]; + + curl_setopt_array($curl, $options); + $res = curl_exec($curl); + + curl_close($curl); + + // check result + error_log('Result ' . $res); + } }