Added contract entity, address to Orders

This commit is contained in:
Fairuz
2021-08-03 01:17:54 +08:00
parent fa5d063ea2
commit af38004595
17 changed files with 102 additions and 68 deletions
@@ -76,4 +76,4 @@ class CreateAddressLogic extends AbstractControllerLogic
}
}
}
@@ -18,18 +18,22 @@ class CompanyModuleObject implements DataTransferObject
/** @var string */
private $unity_hash_id;
/** @var string */
private $unity_signature;
/**
* CompanyObject constructor.
* @param int $companyId
* @param int|null $type
* @param int|null $status
*/
public function __construct(int $companyId, int $type, int $status, ?string $unity_hash_id)
public function __construct(int $companyId, int $type, int $status, ?string $unity_hash_id, ?string $unity_signature)
{
$this->companyId = $companyId;
$this->type = $type;
$this->status = $status;
$this->unity_hash_id = $unity_hash_id;
$this->unity_signature = $unity_signature;
}
/**
@@ -63,4 +67,12 @@ class CompanyModuleObject implements DataTransferObject
{
return $this->unity_hash_id;
}
/**
* @return string
*/
public function getUnitySignature(): string
{
return $this->unity_signature;
}
}
@@ -3,6 +3,7 @@
namespace App\Classes\Modules\Companies\Processors;
use App\Classes\Modules\Unity\Processors\CreateEntityProcessor;
use App\Classes\Modules\Unity\Processors\CreateEntitySignatureProcessor;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyModuleObject;
use App\Classes\Modules\Companies\Services\CreatesCompanyModule;
use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompanyModule;
@@ -20,15 +21,20 @@ class CreateCompanyModuleProcessor
private $canCreateCompanyModule;
private $createEntityProcessor;
private $createUnityEntitySignatureProcessor;
/**
* CreateCompanyModuleProcessor constructor.
* @param CreatesCompanyModule $createsCompanyModule
*/
public function __construct(CanCreateCompanyModule $canCreateCompanyModule, CreatesCompanyModule $createsCompanyModule, CreateEntityProcessor $createUnityEntityProcessor)
public function __construct(CanCreateCompanyModule $canCreateCompanyModule, CreatesCompanyModule $createsCompanyModule, CreateEntityProcessor $createUnityEntityProcessor, CreateEntitySignatureProcessor $createUnityEntitySignatureProcessor)
{
$this->createsCompanyModule = $createsCompanyModule;
$this->createUnityEntityProcessor = $createUnityEntityProcessor;
$this->canCreateCompanyModule = $canCreateCompanyModule;
$this->createUnityEntitySignatureProcessor = $createUnityEntitySignatureProcessor;
}
/**
@@ -39,11 +45,14 @@ class CreateCompanyModuleProcessor
{
$unityEntity = $this->createUnityEntityProcessor->execute($company->name);
$unityEntitySignature = $this->createUnityEntitySignatureProcessor->execute($unityEntity->hash_id);
$company_module_object = new CompanyModuleObject(
$company->id,
$businessType??BusinessType::IMPORTER,
$company->status,
$unityEntity->hash_id
$unityEntity->hash_id,
$unityEntitySignature->hash_id,
);
$this->canCreateCompanyModule->passes($company_module_object);
@@ -21,6 +21,7 @@ class CreatesCompanyModule extends AbstractUpdateRecord
$model->type = $object->getType();
$model->status = $object->getStatus();
$model->unity_hash_id = $object->getUnityHashId();
$model->unity_signature = $object->getUnitySignature();
return $this->handler($model);
}
@@ -14,7 +14,8 @@ class CompanyModuleValidation extends AbstractValidation
protected function data($object): array
{
return [
'unity_hash_id' => $object->getUnityHashId()
'unity_hash_id' => $object->getUnityHashId(),
'unity_signature' => $object->getUnitySignature(),
];
}
@@ -25,7 +26,8 @@ class CompanyModuleValidation extends AbstractValidation
protected function rules(): array
{
return [
'unity_hash_id' => 'required'
'unity_hash_id' => 'required',
'unity_signature' => 'required'
];
}
@@ -39,7 +39,7 @@ class GenerateOrderStepsProcessor
$this->getUnityContractObligationsProcessor = $getUnityContractObligationsProcessor;
}
public function execute(Order $order, array $contract_obligation_list=[]){
public function execute(Order $order, array $contract_obligation_list=[], array $orderRoles=[]){
$obligations = $contract_obligation_list;
@@ -48,7 +48,7 @@ class GenerateOrderStepsProcessor
foreach($obligations as $step){
$orderSteps[] = new OrderStepsObject(
$order->id,
1,
$orderRoles[0]->company_module_id,
$step->name,
true,
$step->sequence??0,
@@ -26,7 +26,7 @@ class ConfirmOrderProcessor
$this->fetchesOrder = $fetchesOrder;
}
public function execute(int $orderId, array $contract_obligation_list = []){
public function execute(int $orderId, array $contract_obligation_list = [], array $orderRoles=[] ){
$order_object = new OrderObject(0,'','',0,0, '',$orderId,0,0);
@@ -34,7 +34,7 @@ class ConfirmOrderProcessor
$order = $this->fetchesOrder->execute(['id'=>$order_object->getId()]);
$this->generateOrderStepsProcessor->execute($order, $contract_obligation_list);
$this->generateOrderStepsProcessor->execute($order, $contract_obligation_list, $orderRoles);
}
}
@@ -13,6 +13,8 @@ use App\Classes\ValueObjects\Constants\OrderType;
use App\Classes\Modules\Unity\Processors\CreateContractProcessor;
use App\Classes\Modules\Unity\Processors\CreateContractEntityProcessor;
use App\Classes\Modules\Unity\Processors\AssignContractEntityProcessor;
use App\Classes\Modules\Orders\Processors\CreateOrderRoleProcessor;
use App\Classes\Modules\Addresses\Processors\CreateOrderAddressProcessor;
use Illuminate\Http\Request;
class CreateOrderProcessor
@@ -31,12 +33,20 @@ class CreateOrderProcessor
private $unityCreateContractEntity;
public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, CreateOrderStepsProcessor $createOrderStepsProcessor, ConfirmOrderProcessor $confirmOrderProcessor, CreateContractProcessor $unityCreateContract, CreateContractEntityProcessor $unityCreateContractEntity, AssignContractEntityProcessor $unityAssignContractEntity)
private $createOrderRoleProcessor;
private $createOrderAddressProcessor;
public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, CreateOrderStepsProcessor $createOrderStepsProcessor, ConfirmOrderProcessor $confirmOrderProcessor, CreateOrderRoleProcessor $createOrderRoleProcessor, CreateOrderAddressProcessor $createOrderAddressProcessor, CreateContractProcessor $unityCreateContract, CreateContractEntityProcessor $unityCreateContractEntity, AssignContractEntityProcessor $unityAssignContractEntity)
{
$this->canCreateOrder = $canCreateOrder;
$this->createsOrder = $createsOrder;
$this->createOrderRoleProcessor = $createOrderRoleProcessor;
$this->confirmOrderProcessor = $confirmOrderProcessor;
$this->createOrderStepsProcessor = $createOrderStepsProcessor;
$this->createOrderAddressProcessor = $createOrderAddressProcessor;
$this->unityCreateContract = $unityCreateContract;
$this->unityAssignContractEntity = $unityAssignContractEntity;
$this->unityCreateContractEntity = $unityCreateContractEntity;
@@ -46,34 +56,34 @@ class CreateOrderProcessor
$reference_no = rand(1000000, 9999999);
//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;
//$e = $this->unityCreateContractEntity->execute($request, $contract_reference_no, $contract_obligation_list);
//Create Order on Shipping Platform
$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);
$order = $this->createsOrder->execute($order_object);
//Add order roles - Allow entity to process this order
//This processor calls Unity to create Contract Entity ID
$orderRoles = $this->createOrderRoleProcessor->execute($request, $order->id, $contract_reference_no);
//Call Unity to assign contract entity to obligation
//$r = $this->unityAssignContractEntity->execute($request, $contract_obligation_list);
$this->unityAssignContractEntity->execute($request, $orderRoles, $contract_obligation_list);
//echo('??');
//dd($e);
$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);
$order = $this->createsOrder->execute($order_object);
//Add address polymorphic
$this->createOrderAddressProcessor->execute($request->get('address_id'), $order);
//Add processing to order step
//Note: For processing apointee_id is CIEF Freight Forward , id is 1, reserved in company table
$this->createOrderStepsProcessor->execute($order, OrderSteps::PROCESSING, 1);
//Pre-approved order, no need Admin to process it
$this->confirmOrderProcessor->execute($order->id, $contract_obligation_list);
//TODO: Add order roles - Allow multiple entity to process this order
//This processor then calls Generate Order Steps
$this->confirmOrderProcessor->execute($order->id, $contract_obligation_list, $orderRoles);
return $order;
}
@@ -20,29 +20,15 @@ class AssignContractEntityProcessor
$this->fetchesCompany = $fetchesCompany;
}
public function execute(Request $request, array $obligations){
public function execute(Request $request, array $contractEntities, 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;
//CIEF Contract Entity Hash ID
$contract_entity_hash_id = $contractEntities[0]->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);
$responses[] = $this->updatesContractEntity->execute($obligation->hash_id, [$contract_entity_hash_id]);
}
return $responses;
@@ -20,18 +20,7 @@ class CreateContractEntityProcessor
$this->fetchesCompany = $fetchesCompany;
}
public function execute(Request $request, string $new_contract_id, array $obligations){
$entity_hash_ids = [];
$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];
$entity_hash_ids[] = $cief_company->companyModule()->first()->unity_hash_id;
$entity_hash_ids[] = $warehouse_company->companyModule()->first()->unity_hash_id;
public function execute(string $new_contract_id, array $entity_hash_ids){
return $this->createsContractEntity->execute($new_contract_id, $entity_hash_ids);
}
}
@@ -27,10 +27,6 @@ class CreatesContractEntity
$response = Http::withToken($accesToken)->post($this->createContractEntityEndpoint,['contract_id'=>[$new_contract_id],'entity_id'=>$entity_hash_ids])->object();
echo($new_contract_id.'<br/>');
echo($this->createContractEntityEndpoint);
dd($response );
return (isset($response->data)) ? $response->data : null;
}
@@ -3,6 +3,7 @@
namespace App\Classes\Modules\Unity\Services;
use Config;
use Cache;
use Illuminate\Support\Facades\Http;
class GetAccessToken
@@ -15,31 +16,47 @@ class GetAccessToken
private $apiKey;
private $cacheName;
private $cacheTime;
private $accessToken;
public function __construct()
{
$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');
$this->cacheName = 'unity_access_token';
$this->cacheTime = Config::get('unity.access_token_cache');
$this->accessToken = $this->getAccessToken();
}
public function execute(){
//Note: Cache access token base on expiry from login not ideal as token may gets overwritten if user login Unity dashboard
//if(!empty($this->getAccessToken())) return $this->getAccessToken();
$accessToken='';
if(empty($this->apiKey)){
$response = Http::post($this->getAccessTokenEndpoint, [
'email' => $this->username,
'password' => $this->password ,
])->object();
$accessToken = $response->access_token;
$this->accessToken = $response->access_token;
}else{
$accessToken = $this->apiKey;
$this->accessToken = $this->apiKey;
}
//TODO: Throw error if access token not available
return $accessToken;
return $this->accessToken;
}
private function getAccessToken(){
$accessToken = $this->accessToken;
return Cache::remember($this->cacheName, $this->cacheTime, function () use ( $accessToken ) {
return $accessToken??'';
});
}
}
@@ -26,7 +26,6 @@ class UpdatesContractEntity
$response = Http::withToken($accessToken)->put( $this->assignEntityEndpoint,['contract_entity_id'=>$entity_hash_id])->object();
dd($response);
return (isset($response->data)) ? $response->data : null;
}
+4
View File
@@ -27,4 +27,8 @@ class Order extends AbstractModel
return $this->hasMany(OrderStep::class, 'order_id');
}
public function addresses(): morphMany
{
return $this->morphMany(Address::class, 'owner');
}
}
+2 -1
View File
@@ -10,6 +10,7 @@ return [
'endpoint' => [
'login' => env('UNITY_LOGIN_ENDPOINT','login'),
'create_entity' => env('UNITY_CREATE_ENTITY_ENDPOINT','entity/store'),
'create_entity_signature' => env('UNITY_CREATE_ENTITY_SIGNATURE_ENDPOINT','entity-signature/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'),
@@ -17,7 +18,7 @@ return [
'update_obligation_status' => env('UNITY_OB_LIST_ENDPOINT','contract-template-obligation/list'),
],
#'default_auth' => env('UNITY_AUTH','login'),
'access_token_cache' => env('UNITY_TOKEN_CACHE', 86400),
//Set API key empty if using login as auth
'auth' => [
@@ -14,7 +14,8 @@ class ModifyCompanyTable extends Migration
public function up()
{
Schema::table('company_modules', function (Blueprint $table) {
$table->string('unity_hash_id',200)->nullable()->after('status');
$table->string('unity_hash_id',200)->nullable(false)->after('status');
$table->string('unity_signature',200)->nullable(false)->after('unity_hash_id');
});
}
+8 -1
View File
@@ -6,6 +6,7 @@ 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 App\Classes\Modules\Unity\Processors\CreateEntitySignatureProcessor;
use Illuminate\Database\Seeder;
@@ -17,9 +18,12 @@ class CompaniesTableSeeder extends Seeder
private $createEntityProcessor;
public function __construct(CreateEntityProcessor $createEntityProcessor)
private $createEntitySignatureProcessor;
public function __construct(CreateEntityProcessor $createEntityProcessor, CreateEntitySignatureProcessor $createEntitySignatureProcessor)
{
$this->createEntityProcessor = $createEntityProcessor;
$this->createEntitySignatureProcessor = $createEntitySignatureProcessor;
}
/**
@@ -57,6 +61,8 @@ class CompaniesTableSeeder extends Seeder
$unityEntity = $this->createEntityProcessor->execute($company['name']);
$unityEntitySignature = $this->createEntitySignatureProcessor->execute($unityEntity->hash_id);
$company = Company::create($company);
$companyModule = [
@@ -64,6 +70,7 @@ class CompaniesTableSeeder extends Seeder
'type' => BusinessType::WAREHOUSE,
'status' => ApprovalStatus::APPROVED,
'unity_hash_id' => $unityEntity->hash_id,
'unity_signature' => $unityEntitySignature->hash_id,
];
if($company['reference']=='CIEF'){