Merge branch 'dillon/39-crm-changes' into dillon/development

This commit is contained in:
Dillon
2023-06-19 06:02:01 +08:00
12 changed files with 146 additions and 11 deletions
+17
View File
@@ -3,6 +3,7 @@
namespace App\Classes\Jobs;
use App\Classes\Modules\PerfexCRM\Processors\UpdatePerfexCRMProcessor;
use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMInvoiceV2Processor;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -41,6 +42,22 @@ class UpdatePerfexCRM implements ShouldQueue
public function handle()
{
$invoiceId = 0;
//To use invoice as a reference to decide whether more tasks should be created
if($this->transaction != null){
$updatePerfexCRMInvoiceObject = new UpdatePerfexCRMInvoiceObject(
$this->updatePerfexCRMObject->getContactEmail(),
$this->updatePerfexCRMObject->getProjectName(),
'',
$this->transaction,
true
);
// UpdatePerfexCRMInvoice::dispatch($updatePerfexCRMInvoiceObject);
$invoiceId = (App()->make(CreatePerfexCRMInvoiceV2Processor::class))->execute($updatePerfexCRMInvoiceObject);
}
$this->updatePerfexCRMObject->setInvoiceId($invoiceId);
$result = (App()->make(UpdatePerfexCRMProcessor::class))->execute($this->updatePerfexCRMObject);
if($this->transaction != null && $this->shouldCreateInvoice){
$updatePerfexCRMInvoiceObject = new UpdatePerfexCRMInvoiceObject(
@@ -42,7 +42,10 @@ class CreateTaskPerfexCRMObject implements DataTransferObject
/** @var string */
private $duedate;
public function __construct(string $email, string $name, string $description, string $leadId, string $projectId, string $milestoneId, string $reference, string $onTaskCompletion, string $status, string $department, string $priority, string $duedate)
/** @var int */
private $invoiceId;
public function __construct(string $email, string $name, string $description, string $leadId, string $projectId, string $milestoneId, string $reference, string $onTaskCompletion, string $status, string $department, string $priority, string $duedate, int $invoiceId)
{
$this->email = $email;
$this->name = $name;
@@ -56,6 +59,7 @@ class CreateTaskPerfexCRMObject implements DataTransferObject
$this->department = $department;
$this->priority = $priority;
$this->duedate = $duedate;
$this->invoiceId = $invoiceId;
}
/**
@@ -158,4 +162,12 @@ class CreateTaskPerfexCRMObject implements DataTransferObject
{
return $this->duedate;
}
/**
* @return int
*/
public function getInvoiceId(): int
{
return $this->invoiceId;
}
}
@@ -34,8 +34,11 @@ class UpdatePerfexCRMObject implements DataTransferObject
/** @var array */
private $tasks;
/** @var int */
private $invoiceId;
public function __construct(string $companyName, string $companyReference, string $contactName, string $contactEmail, string $bookingMarking, string $projectName, int $projectStatus, array $milestoneNames, array $tasks)
public function __construct(string $companyName, string $companyReference, string $contactName, string $contactEmail, string $bookingMarking, string $projectName, int $projectStatus, int $invoiceId, array $milestoneNames, array $tasks)
{
$this->companyName = $companyName;
@@ -45,6 +48,7 @@ class UpdatePerfexCRMObject implements DataTransferObject
$this->bookingMarking = $bookingMarking;
$this->projectName = $projectName;
$this->projectStatus = $projectStatus;
$this->$invoiceId = $invoiceId;
$this->milestoneNames = $milestoneNames;
$this->tasks = $tasks;
}
@@ -105,6 +109,14 @@ class UpdatePerfexCRMObject implements DataTransferObject
return $this->projectStatus;
}
/**
* @return int
*/
public function getInvoiceId(): int
{
return $this->invoiceId;
}
/**
* @return array
*/
@@ -126,4 +138,9 @@ class UpdatePerfexCRMObject implements DataTransferObject
$this->tasks = $tasks;
}
public function setInvoiceId($invoiceId)
{
$this->invoiceId = $invoiceId;
}
}
@@ -35,6 +35,7 @@ class BookingToPerfexCRMProcessor
$bookingMarking,
$projectName,
PerfexCRMProjectStatus::NOT_STARTED,
0,
[],
[]
);
@@ -0,0 +1,72 @@
<?php
namespace App\Classes\Modules\PerfexCRM\Processors;
use App\Classes\Modules\PerfexCRM\Processors\CreatePerfexCRMInvoiceProcessor;
use App\Classes\Modules\PerfexCRM\Services\FetchesPerfexCRMCustomer;
use App\Classes\Modules\PerfexCRM\Services\FetchesPerfexCRMInvoice;
use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMInvoiceObject;
use Illuminate\Support\Facades\Log;
use App\Classes\General\Helper;
class CreatePerfexCRMInvoiceV2Processor
{
private $fetchesPerfexCRMCustomer;
/** @var FetchesPerfexCRMCustomer */
/** @var FetchesPerfexCRMInvoice */
private $fetchesPerfexCRMInvoice;
/** @var CreatePerfexCRMInvoiceProcessor */
private $createPerfexCRMInvoiceProcessor;
/**
*
* CreatePerfexCRMInvoiceV2Processor constructor.
* @param FetchesPerfexCRMCustomer $fetchesPerfexCRMCustomer
* @param FetchesPerfexCRMInvoice $fetchesPerfexCRMInvoice
* @param CreatePerfexCRMInvoiceProcessor $createPerfexCRMInvoiceProcessor
*/
public function __construct(FetchesPerfexCRMCustomer $fetchesPerfexCRMCustomer,
FetchesPerfexCRMInvoice $fetchesPerfexCRMInvoice,
CreatePerfexCRMInvoiceProcessor $createPerfexCRMInvoiceProcessor)
{
$this->fetchesPerfexCRMCustomer = $fetchesPerfexCRMCustomer;
$this->fetchesPerfexCRMInvoice = $fetchesPerfexCRMInvoice;
$this->createPerfexCRMInvoiceProcessor = $createPerfexCRMInvoiceProcessor;
}
public function execute(UpdatePerfexCRMInvoiceObject $updatePerfexCRMInvoiceObject)
{
//get the client id
$customer = $this->fetchesPerfexCRMCustomer->execute($updatePerfexCRMInvoiceObject->getEmail());
$transaction = $updatePerfexCRMInvoiceObject->getTransaction();
//get the invoice id
$invoiceId = 0;
$number = $transaction->bill_no;
$prefix = "INV-";
//This will remove the prefix if prefix already exist in the string
if (substr($number, 0, strlen($prefix)) == $prefix) {
$number = substr($number, strlen($prefix));
}
$number = 'EXC-'.$number;
$invoice = $this->fetchesPerfexCRMInvoice->execute($customer->userid,"INV-", $number);
Log::error(json_encode('CreatePerfexCRMInvoiceV2Processor debug $number: '.$number));
if(is_null($invoice)){
$result = $this->createPerfexCRMInvoiceProcessor->execute($transaction, null, null, $updatePerfexCRMInvoiceObject->getIsPaid());
if ($result) {
$invoiceId = $result->payload['id'];
} else {
$log['message'] = 'CreatePerfexCRMInvoiceV2Processor CreatePerfexCRMInvoiceProcessor failed';
Helper::debugLoggerForPerfexCRM($log);
}
}
return $invoiceId;
}
}
@@ -37,6 +37,7 @@ class CreatePerfexCRMTaskProcessor
$createTaskPerfexCRMObject->getStatus(),
$createTaskPerfexCRMObject->getPriority(),
$createTaskPerfexCRMObject->getDuedate(),
$createTaskPerfexCRMObject->getInvoiceId(),
);
return true;
}
@@ -32,7 +32,8 @@ class NewLeadTaskToPerfexCRMProcessor
PerfexCRMTasks::TASK_IDENTIFICATION_1['status'],
PerfexCRMTasks::TASK_IDENTIFICATION_1['department'],
PerfexCRMTaskPriority::DEFAULT,
"0"
"0",
0
);
CreatePerfexCRMSingleTask::dispatch($createTaskPerfexCRMObject);
@@ -114,6 +114,7 @@ class TransactionToPerfexCRMProcessor
$bookingMarking,
$projectName,
PerfexCRMProjectStatus::IN_PROGRESS,
0,
[],
$tasks
);
@@ -140,6 +141,7 @@ class TransactionToPerfexCRMProcessor
$bookingMarking,
$projectName,
PerfexCRMProjectStatus::IN_PROGRESS,
0,
[],
$tasks
);
@@ -8,8 +8,6 @@ use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PerfexCRMProjectStatus;
use App\Classes\ValueObjects\Constants\PerfexCRMTaskStatus;
use App\Classes\Jobs\UpdatePerfexCRMInvoice;
use App\Classes\Jobs\UpdatePerfexCRM;
use App\Classes\Jobs\UpdatePerfexCRMPrelude;
use App\Models\Booking;
use App\Models\Transaction;
@@ -35,6 +33,7 @@ class TransactionToPerfexCRMProcessorV2
$bookingInfo['bookingMarking'],
$projectName,
PerfexCRMProjectStatus::IN_PROGRESS,
0,
[],
$tasks
);
@@ -203,18 +203,19 @@ class UpdatePerfexCRMProcessor
}
// Get existing or create task
$result = $this->fetchesPerfexCRMTask->execute($tasks[$count]['name'], $milestoneId, 'project', $projectId);
$result = $this->fetchesPerfexCRMTask->execute($tasks[$count]['name'], $milestoneId, 'project', $projectId, $updatePerfexCRMObject->getInvoiceId());
Log::info("UpdatePerfexCRMProcessor task: ".json_encode($taskStatus)." , ".json_encode($result));
if(isset($result->payload)){
//&& $result->payload[0]['status'] == PerfexCRMTaskStatus::NOT_STARTED
Log::info(json_encode([$taskStatus]));
if($taskStatus != PerfexCRMTaskStatus::NOT_STARTED )
if($taskStatus != PerfexCRMTaskStatus::NOT_STARTED)
{
$task = $result->payload[0];
$result = $this->updatesPerfexCRMTask->execute($task['id'], $task['name'], $task['milestone'], $task['rel_id'], $taskStatus, $task['startdate'], is_null($task['duedate']) ? '': $task['duedate']);
}
}
else{
$result = $this->createsPerfexCRMTask->execute($tasks[$count]['name'], $tasks[$count]['description'], '', $milestoneId, $projectId, $tasks[$count]['reference'], $tasks[$count]['on_task_completion'], $tasks[$count]['department'], $taskStatus, $tasks[$count]['priority'], $tasks[$count]['duedate']);
$result = $this->createsPerfexCRMTask->execute($tasks[$count]['name'], $tasks[$count]['description'], '', $milestoneId, $projectId, $tasks[$count]['reference'], $tasks[$count]['on_task_completion'], $tasks[$count]['department'], $taskStatus, $tasks[$count]['priority'], $tasks[$count]['duedate'], $updatePerfexCRMObject->getInvoiceId());
}
//cief todo: to evaluate if this is still needed
@@ -21,10 +21,11 @@ class CreatesPerfexCRMTask
* @param string $status
* @param string $priority
* @param string $duedate
* @param int $invoiceId
* @return null|object
* @throws MalformedRequestException
*/
public function execute(string $taskName, string $taskDescription, string $leadId, string $milestoneId, string $projectId, string $reference, string $on_task_completion, string $department, string $status, string $priority, string $duedate) {
public function execute(string $taskName, string $taskDescription, string $leadId, string $milestoneId, string $projectId, string $reference, string $on_task_completion, string $department, string $status, string $priority, string $duedate, int $invoiceId) {
try{
$custom_fields = [];
if($department != ""){
@@ -35,6 +36,10 @@ class CreatesPerfexCRMTask
];
}
if($invoiceId != 0){
$taskName = $taskName." (".$invoiceId.")";
}
$data = [
'name' => $taskName,
'description' => $taskDescription,
@@ -49,6 +54,7 @@ class CreatesPerfexCRMTask
'custom_fields' => $custom_fields,
'priority' => $priority,
'duedate' => date('Y-m-d', strtotime('+' . $duedate . ' days')),
'invoice_id' => $invoiceId,
];
if($leadId != '') {
@@ -66,6 +72,7 @@ class CreatesPerfexCRMTask
'custom_fields' => $custom_fields,
'priority' => $priority,
'duedate' => date('Y-m-d', strtotime('+' . $duedate . ' days')),
'invoice_id' => 0
];
}
@@ -17,7 +17,7 @@ class FetchesPerfexCRMTask
* @return null|object
* @throws MalformedRequestException
*/
public function execute(string $taskName, string $milestoneId, string $relType, string $relId) {
public function execute(string $taskName, string $milestoneId, string $relType, string $relId, string $invoiceId) {
try{
$data = [
'name' => $taskName,
@@ -30,6 +30,11 @@ class FetchesPerfexCRMTask
$data = array_merge($data, $newItem);
}
if($invoiceId != 0) {
$newItem = ['invoice_id' => $invoiceId ];
$data = array_merge($data, $newItem);
}
$response = Http::asForm()->withHeaders([
'authtoken' => config('perfexcrm.api_key')])
->post(config('perfexcrm.base_url').'/api/tasks/customsearch', $data);