mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
Assign contract obligations at Unity
This commit is contained in:
@@ -41,7 +41,7 @@ class GenerateOrderStepsProcessor
|
||||
|
||||
public function execute(Order $order, array $contract_obligation_list=[]){
|
||||
|
||||
$obligations = $contract_obligation_list;//$this->getUnityContractObligationsProcessor->execute();
|
||||
$obligations = $contract_obligation_list;
|
||||
|
||||
//TODO: Collect hash_id & search company module table for appointee id
|
||||
|
||||
@@ -51,7 +51,7 @@ class GenerateOrderStepsProcessor
|
||||
1,
|
||||
$step->name,
|
||||
true,
|
||||
$step->sequence,
|
||||
$step->sequence??0,
|
||||
0,
|
||||
0,
|
||||
null
|
||||
|
||||
@@ -21,7 +21,11 @@ class OrderObject implements DataTransferObject
|
||||
|
||||
private $current_step;
|
||||
|
||||
public function __construct(int $company_module_id, ?string $reference, string $reference_contract, int $type, int $status, string $current_step, int $id=0)
|
||||
private $address_id;
|
||||
|
||||
private $warehouse_id;
|
||||
|
||||
public function __construct(int $company_module_id, ?string $reference, string $reference_contract, int $type, int $status, string $current_step, int $id=0, int $address_id, int $warehouse_id)
|
||||
{
|
||||
$this->company_module_id = $company_module_id;
|
||||
$this->reference = $reference;
|
||||
@@ -30,6 +34,8 @@ class OrderObject implements DataTransferObject
|
||||
$this->status = $status;
|
||||
$this->current_step = $current_step;
|
||||
$this->id = $id;
|
||||
$this->address_id = $address_id;
|
||||
$this->warehouse_id = $warehouse_id;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
@@ -67,6 +73,16 @@ class OrderObject implements DataTransferObject
|
||||
return $this->current_step;
|
||||
}
|
||||
|
||||
public function getWarehouseId(): int
|
||||
{
|
||||
return $this->warehouse_id;
|
||||
}
|
||||
|
||||
public function getAddressId(): int
|
||||
{
|
||||
return $this->address_id;
|
||||
}
|
||||
|
||||
public function setCurrentStep(string $step)
|
||||
{
|
||||
$this->current_step = $step;
|
||||
|
||||
@@ -11,6 +11,7 @@ use App\Classes\ValueObjects\Constants\OrderStatus;
|
||||
use App\Classes\ValueObjects\Constants\OrderSteps;
|
||||
use App\Classes\ValueObjects\Constants\OrderType;
|
||||
use App\Classes\Modules\Unity\Processors\CreateContractProcessor;
|
||||
use App\Classes\Modules\Unity\Processors\AssignContractEntityProcessor;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CreateOrderProcessor
|
||||
@@ -25,26 +26,35 @@ class CreateOrderProcessor
|
||||
|
||||
private $unityCreateContract;
|
||||
|
||||
public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, CreateOrderStepsProcessor $createOrderStepsProcessor, ConfirmOrderProcessor $confirmOrderProcessor, CreateContractProcessor $unityCreateContract)
|
||||
private $unityAssignContractEntity;
|
||||
|
||||
public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, CreateOrderStepsProcessor $createOrderStepsProcessor, ConfirmOrderProcessor $confirmOrderProcessor, CreateContractProcessor $unityCreateContract, AssignContractEntityProcessor $unityAssignContractEntity)
|
||||
{
|
||||
$this->canCreateOrder = $canCreateOrder;
|
||||
$this->createsOrder = $createsOrder;
|
||||
$this->confirmOrderProcessor = $confirmOrderProcessor;
|
||||
$this->createOrderStepsProcessor = $createOrderStepsProcessor;
|
||||
$this->unityCreateContract = $unityCreateContract;
|
||||
$this->unityAssignContractEntity = $unityAssignContractEntity;
|
||||
}
|
||||
|
||||
public function execute(Request $request){
|
||||
|
||||
$reference_no = rand(1000000, 9999999);
|
||||
|
||||
//TODO: Call Unity to create contract and get contract reference no (and all the obligations)
|
||||
//Call Unity to create contract and get contract reference no (and all the obligations)
|
||||
$contract = $this->unityCreateContract->execute();
|
||||
|
||||
$contract_reference_no = $contract->hash_id;
|
||||
$contract_obligation_list = $contract->contract_obligation_list;
|
||||
|
||||
$order_object = new OrderObject($request->get('company_module_id'), $contract_reference_no, '', OrderType::SHARED_CONTAINER, OrderStatus::PROCESSING, OrderSteps::PROCESSING);
|
||||
//Call Unity to assign contract entity to obligation
|
||||
$r = $this->unityAssignContractEntity->execute($request, $contract_obligation_list);
|
||||
|
||||
echo('??');
|
||||
dd($r);
|
||||
|
||||
$order_object = new OrderObject($request->get('company_module_id'), $contract_reference_no, '', OrderType::SHARED_CONTAINER, OrderStatus::PROCESSING, OrderSteps::PROCESSING, 0, $request->get('address_id'), $request->get('warehouse_id'));
|
||||
|
||||
$this->canCreateOrder->passes($order_object);
|
||||
|
||||
@@ -55,7 +65,7 @@ class CreateOrderProcessor
|
||||
$this->createOrderStepsProcessor->execute($order, OrderSteps::PROCESSING, 1);
|
||||
|
||||
//Pre-approved order, no need Admin to process it
|
||||
$this->confirmOrderProcessor->execute($order->id);
|
||||
$this->confirmOrderProcessor->execute($order->id, $contract_obligation_list);
|
||||
|
||||
//TODO: Add order roles - Allow multiple entity to process this order
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class OrderValidation extends AbstractValidation
|
||||
'company_module_id' => $object->getCompanyModuleId(),
|
||||
'reference' => $object->getReference(),
|
||||
'reference_contract' => $object->getReferenceContract(),
|
||||
'warehouse_id' => $object->getWarehouseId(),
|
||||
'address_id' => $object->getAddressId(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -29,6 +31,8 @@ class OrderValidation extends AbstractValidation
|
||||
if ($type == 'POST') {
|
||||
return [
|
||||
'company_module_id' => 'required',
|
||||
'warehouse_id'=>'required',
|
||||
'address_id'=>'required',
|
||||
'reference' => '',
|
||||
'reference_contract' => ''
|
||||
];
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Unity\Processors;
|
||||
|
||||
use Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Classes\Modules\Unity\Services\UpdatesContractEntity;
|
||||
use App\Classes\Modules\Companies\Services\FetchesCompany;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AssignContractEntityProcessor
|
||||
{
|
||||
private $updatesContractEntity;
|
||||
|
||||
private $fetchesCompany;
|
||||
|
||||
public function __construct( UpdatesContractEntity $updatesContractEntity, FetchesCompany $fetchesCompany)
|
||||
{
|
||||
$this->updatesContractEntity = $updatesContractEntity;
|
||||
$this->fetchesCompany = $fetchesCompany;
|
||||
}
|
||||
|
||||
public function execute(Request $request, array $obligations){
|
||||
|
||||
$responses = [];
|
||||
|
||||
$companies = $this->fetchesCompany->getRepository()->whereIn('id',[1, $request->get('warehouse_id')])->get();
|
||||
|
||||
$cief_company = $companies[0]->id == 1 ? $companies[0] : $companies[1];
|
||||
|
||||
$warehouse_company = $companies[0]->id != 1 ? $companies[0] : $companies[1];
|
||||
|
||||
$cief_unity_hash_id = $cief_company->companyModule()->first()->unity_hash_id;
|
||||
|
||||
$warehouse_unity_hash_id = $warehouse_company->companyModule()->first()->unity_hash_id;
|
||||
|
||||
foreach($obligations as $obligation){
|
||||
$entities_hash_id = [];
|
||||
$entities_hash_id[] = $cief_unity_hash_id;
|
||||
|
||||
if(in_array($obligation->name,['WAREHOUSE_RECEIVING','WAREHOUSE_LOAD_CONTAINER'])){
|
||||
$entities_hash_id[] = $warehouse_unity_hash_id ;
|
||||
}
|
||||
|
||||
$responses[] = $this->updatesContractEntity->execute($obligation->hash_id, $entities_hash_id);
|
||||
}
|
||||
|
||||
return $responses;
|
||||
}
|
||||
}
|
||||
@@ -23,9 +23,9 @@ class CreatesContract
|
||||
|
||||
public function execute(){
|
||||
|
||||
$accesToken=$this->getAccessToken->execute();
|
||||
$accessToken=$this->getAccessToken->execute();
|
||||
|
||||
$response = Http::withToken($accesToken)->post($this->createEntityEndpoint,['contract_template_id'=>[$this->contract_hash_id]])->object();
|
||||
$response = Http::withToken($accessToken)->post($this->createEntityEndpoint,['contract_template_id'=>[$this->contract_hash_id]])->object();
|
||||
|
||||
return (isset($response->data)) ? $response->data : null;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class CreatesEntity
|
||||
|
||||
public function __construct(GetAccessToken $getAccessToken)
|
||||
{
|
||||
$this->createEntityEndpoint = Config::get('unity.url').Config::get('unity.create_entity_endpoint');
|
||||
$this->createEntityEndpoint = Config::get('unity.url').Config::get('unity.endpoint.create_entity');
|
||||
$this->getAccessToken = $getAccessToken;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,31 +7,39 @@ use Illuminate\Support\Facades\Http;
|
||||
|
||||
class GetAccessToken
|
||||
{
|
||||
private $unityConfig;
|
||||
private $getAccessTokenEndpoint;
|
||||
|
||||
private $username;
|
||||
|
||||
private $password;
|
||||
|
||||
private $apiKey;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->unityConfig = Config::get('unity');
|
||||
$this->getAccessTokenEndpoint = Config::get('unity.url').Config::get('unity.endpoint.login');
|
||||
$this->username = Config::get('unity.auth.login.username');
|
||||
$this->password = Config::get('unity.auth.login.password');
|
||||
$this->apiKey = Config::get('unity.auth.api_key.key');
|
||||
}
|
||||
|
||||
public function execute(){
|
||||
|
||||
//TODO: Cache access token base on expiry for login version
|
||||
//Note: Cache access token base on expiry from login not ideal as token may gets overwritten if user login Unity dashboard
|
||||
|
||||
$accesToken='';
|
||||
if($this->unityConfig['default_auth']=='login'){
|
||||
$loginDetails = $this->unityConfig['auth']['login'];
|
||||
$response = Http::post($this->unityConfig['url'].$this->unityConfig['endpoint']['login'], [
|
||||
'email' => $loginDetails['username'],
|
||||
'password' => $loginDetails['password'],
|
||||
$accessToken='';
|
||||
if(empty($this->apiKey)){
|
||||
$response = Http::post($this->getAccessTokenEndpoint, [
|
||||
'email' => $this->username,
|
||||
'password' => $this->password ,
|
||||
])->object();
|
||||
$accesToken = $response->access_token;
|
||||
$accessToken = $response->access_token;
|
||||
}else{
|
||||
$accesToken = $this->unityConfig['auth']['api_key']['key'];
|
||||
$accessToken = $this->apiKey;
|
||||
}
|
||||
|
||||
//TODO: Throw error if access token not available
|
||||
|
||||
return $accesToken;
|
||||
return $accessToken;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Unity\Services;
|
||||
|
||||
use Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Classes\Modules\Unity\Services\GetAccessToken;
|
||||
|
||||
class UpdatesContractEntity
|
||||
{
|
||||
private $getAccessToken;
|
||||
|
||||
private $assignEntityEndpoint;
|
||||
|
||||
public function __construct(GetAccessToken $getAccessToken)
|
||||
{
|
||||
$this->assignEntityEndpoint = Config::get('unity.url').Config::get('unity.endpoint.assign_entity');
|
||||
$this->getAccessToken = $getAccessToken;
|
||||
}
|
||||
|
||||
public function execute(string $obligation_hash_id, array $entity_hash_id){
|
||||
|
||||
$accessToken=$this->getAccessToken->execute();
|
||||
|
||||
$this->assignEntityEndpoint = str_replace('{hash}',$obligation_hash_id,$this->assignEntityEndpoint);
|
||||
|
||||
$response = Http::withToken($accessToken)->put( $this->assignEntityEndpoint,['contract_entity_id'=>$entity_hash_id])->object();
|
||||
|
||||
dd($response);
|
||||
return (isset($response->data)) ? $response->data : null;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Unity\Services;
|
||||
|
||||
use Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Classes\Modules\Unity\Services\GetAccessToken;
|
||||
|
||||
class UpdatesContractObligation
|
||||
{
|
||||
private $getAccessToken;
|
||||
|
||||
private $createEntityEndpoint;
|
||||
|
||||
public function __construct(GetAccessToken $getAccessToken)
|
||||
{
|
||||
$this->createEntityEndpoint = Config::get('unity.url').Config::get('unity.create_entity_endpoint');
|
||||
$this->getAccessToken = $getAccessToken;
|
||||
}
|
||||
|
||||
public function execute($entity_name){
|
||||
|
||||
$accesToken=$this->getAccessToken->execute();
|
||||
|
||||
$response = Http::withToken($accesToken)->post($this->createEntityEndpoint,['name'=>$entity_name])->object();
|
||||
|
||||
return (isset($response->data)) ? $response->data : null;
|
||||
|
||||
}
|
||||
}
|
||||
+7
-4
@@ -10,13 +10,16 @@ return [
|
||||
'endpoint' => [
|
||||
'login' => env('UNITY_LOGIN_ENDPOINT','login'),
|
||||
'create_entity' => env('UNITY_CREATE_ENTITY_ENDPOINT','entity/store'),
|
||||
'create_contract_entity' => env('UNITY_CREATE_ENTITY_ENDPOINT','contract-entity/store'),
|
||||
'get_contract_obligations' => env('UNITY_OB_LIST_ENDPOINT','contract-template-obligation/list'),
|
||||
'create_contract' => env('UNITY_OB_LIST_ENDPOINT','contract/generate'),
|
||||
'assign_entity' => env('UNITY_OB_LIST_ENDPOINT','contract-obligation/assign-contract-entity/{hash}'),
|
||||
'update_obligation_status' => env('UNITY_OB_LIST_ENDPOINT','contract-template-obligation/list'),
|
||||
],
|
||||
|
||||
'default_auth' => env('UNITY_AUTH','login'),
|
||||
#'default_auth' => env('UNITY_AUTH','login'),
|
||||
|
||||
//Set API key empty if using login as auth
|
||||
'auth' => [
|
||||
|
||||
'api_key' => [
|
||||
@@ -30,15 +33,15 @@ return [
|
||||
|
||||
],
|
||||
|
||||
'get_ob_endpoint' => env('UNITY_OB_LIST_ENDPOINT','contract-template-obligation/list'),
|
||||
#'get_ob_endpoint' => env('UNITY_OB_LIST_ENDPOINT','contract-template-obligation/list'),
|
||||
|
||||
'create_entity_endpoint' => env('UNITY_CREATE_ENTITY_ENDPOINT','entity/store'),
|
||||
#'create_entity_endpoint' => env('UNITY_CREATE_ENTITY_ENDPOINT','entity/store'),
|
||||
|
||||
'on_error_email' => env('UNITY_ON_ERROR_EMAIL','dev@shipping.com,dev@unity.com'),
|
||||
|
||||
'contract_template_id' => env('UNITY_CONTRACT_TEMPLATE_ID', 102),
|
||||
|
||||
'contract_template_hash_id' => env('UNITY_CONTRACT_TEMPLATE_HASH_ID', '5lpn70mA2VmgxoekgYrJ'),
|
||||
'contract_template_hash_id' => env('UNITY_CONTRACT_TEMPLATE_HASH_ID', 'Kp3qzMV82mqA2PZg6eX0'),
|
||||
|
||||
'contract_template_cache' => env('UNITY_CONTRACT_TEMPLATE_CACHE','60'),
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Database\Seeders;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\CompanyType;
|
||||
use App\Classes\ValueObjects\Constants\BusinessType;
|
||||
use App\Classes\Modules\Unity\Processors\CreateEntityProcessor;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
@@ -13,6 +14,14 @@ use App\Models\CompanyModule;
|
||||
|
||||
class CompaniesTableSeeder extends Seeder
|
||||
{
|
||||
|
||||
private $createEntityProcessor;
|
||||
|
||||
public function __construct(CreateEntityProcessor $createEntityProcessor)
|
||||
{
|
||||
$this->createEntityProcessor = $createEntityProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
@@ -20,7 +29,6 @@ class CompaniesTableSeeder extends Seeder
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
|
||||
$companies = [
|
||||
[
|
||||
'id' => 1, //Reserve ID
|
||||
@@ -47,12 +55,15 @@ class CompaniesTableSeeder extends Seeder
|
||||
if (!empty($companies)) {
|
||||
foreach ($companies as $company) {
|
||||
|
||||
$unityEntity = $this->createEntityProcessor->execute($company['name']);
|
||||
|
||||
$company = Company::create($company);
|
||||
|
||||
$companyModule = [
|
||||
'company_id' => $company->id,
|
||||
'type' => BusinessType::WAREHOUSE,
|
||||
'status' => ApprovalStatus::APPROVED,
|
||||
'unity_hash_id' => $unityEntity->hash_id,
|
||||
];
|
||||
|
||||
if($company['reference']=='CIEF'){
|
||||
|
||||
@@ -2299,6 +2299,20 @@
|
||||
"description": "Company Module ID",
|
||||
"required": true,
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"name": "warehouse_id",
|
||||
"in": "query",
|
||||
"description": "Warehouse ID",
|
||||
"required": true,
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"name": "address_id",
|
||||
"in": "query",
|
||||
"description": "Address ID",
|
||||
"required": true,
|
||||
"type": "integer"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
||||
Reference in New Issue
Block a user