diff --git a/app/Classes/Jobs/UpdatePerfexCRM.php b/app/Classes/Jobs/UpdatePerfexCRM.php index 833fb365..93f536c5 100644 --- a/app/Classes/Jobs/UpdatePerfexCRM.php +++ b/app/Classes/Jobs/UpdatePerfexCRM.php @@ -3,6 +3,7 @@ namespace App\Classes\Jobs; use App\Classes\Modules\PerfexCRM\Processors\UpdatePerfexCRMProcessor; +use App\Classes\Modules\PerfexCRM\Processors\FetchPerfexCRMInvoiceProcessor; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -10,6 +11,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMObject; use App\Classes\Modules\PerfexCRM\DataTransferObjects\UpdatePerfexCRMInvoiceObject; +use App\Classes\Modules\PerfexCRM\DataTransferObjects\FetchPerfexCRMInvoiceObject; use Illuminate\Support\Facades\Log; use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean; @@ -41,6 +43,9 @@ class UpdatePerfexCRM implements ShouldQueue public function handle() { + //To use invoice as a reference to decide whether more tasks should be created + $this->updatePerfexCRMObject->setInvoiceId($this->getInvoiceId()); + $result = (App()->make(UpdatePerfexCRMProcessor::class))->execute($this->updatePerfexCRMObject); if($this->transaction != null && $this->shouldCreateInvoice){ $updatePerfexCRMInvoiceObject = new UpdatePerfexCRMInvoiceObject( @@ -50,9 +55,20 @@ class UpdatePerfexCRM implements ShouldQueue $this->transaction, true ); - UpdatePerfexCRMInvoice::dispatch($updatePerfexCRMInvoiceObject); - } } + + private function getInvoiceId(){ + if($this->transaction != null){ + $fetchPerfexCRMInvoiceObject = new FetchPerfexCRMInvoiceObject( + $this->updatePerfexCRMObject->getContactEmail(), + $this->transaction, + true + ); + $invoiceId = (App()->make(FetchPerfexCRMInvoiceProcessor::class))->execute($fetchPerfexCRMInvoiceObject); + return $invoiceId; + } + return 0; + } } diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/CreateTaskPerfexCRMObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/CreateTaskPerfexCRMObject.php index 6c6354f1..2475ebbf 100644 --- a/app/Classes/Modules/PerfexCRM/DataTransferObjects/CreateTaskPerfexCRMObject.php +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/CreateTaskPerfexCRMObject.php @@ -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; + } } diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/FetchPerfexCRMInvoiceObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/FetchPerfexCRMInvoiceObject.php new file mode 100644 index 00000000..cbb85a3e --- /dev/null +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/FetchPerfexCRMInvoiceObject.php @@ -0,0 +1,50 @@ +email = $email; + $this->transaction = $transaction; + $this->isPaid = $isPaid; + } + + /** + * @return string + */ + public function getEmail(): string + { + return $this->email; + } + + /** + * @return Transaction + */ + public function getTransaction(): Transaction + { + return $this->transaction; + } + + /** + * @return bool + */ + public function getIsPaid(): bool + { + return $this->isPaid; + } +} diff --git a/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMObject.php b/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMObject.php index 023bd786..96bbc8bd 100644 --- a/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMObject.php +++ b/app/Classes/Modules/PerfexCRM/DataTransferObjects/UpdatePerfexCRMObject.php @@ -34,8 +34,11 @@ class UpdatePerfexCRMObject implements DataTransferObject /** @var array */ private $tasks; + /** @var int */ + private $invoiceId = 0; - 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; + } + } diff --git a/app/Classes/Modules/PerfexCRM/Processors/BookingToPerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/BookingToPerfexCRMProcessor.php index 930d3765..379caae7 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/BookingToPerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/BookingToPerfexCRMProcessor.php @@ -35,6 +35,7 @@ class BookingToPerfexCRMProcessor $bookingMarking, $projectName, PerfexCRMProjectStatus::NOT_STARTED, + 0, [], [] ); diff --git a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php index 4dbaa2d5..2bd95920 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMInvoiceProcessor.php @@ -199,7 +199,7 @@ class CreatePerfexCRMInvoiceProcessor //When this transaction is of TransactionType::PAYMENT, the amount is actually in the currecy user choose to pay (RM) //So there is no need to convert it if ($transaction->type == TransactionType::PAYMENT){ - $total = $transaction->amount; + $total = number_format($transaction->amount, 2,'.','') * 1; } else{ if($booking->first()->fix_currency_id !== 1){ diff --git a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMTaskProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMTaskProcessor.php index e9822471..4aa5a089 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMTaskProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/CreatePerfexCRMTaskProcessor.php @@ -37,6 +37,7 @@ class CreatePerfexCRMTaskProcessor $createTaskPerfexCRMObject->getStatus(), $createTaskPerfexCRMObject->getPriority(), $createTaskPerfexCRMObject->getDuedate(), + $createTaskPerfexCRMObject->getInvoiceId(), ); return true; } diff --git a/app/Classes/Modules/PerfexCRM/Processors/FetchPerfexCRMInvoiceProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/FetchPerfexCRMInvoiceProcessor.php new file mode 100644 index 00000000..ddf0ee55 --- /dev/null +++ b/app/Classes/Modules/PerfexCRM/Processors/FetchPerfexCRMInvoiceProcessor.php @@ -0,0 +1,75 @@ +fetchesPerfexCRMCustomer = $fetchesPerfexCRMCustomer; + $this->fetchesPerfexCRMInvoice = $fetchesPerfexCRMInvoice; + $this->createPerfexCRMInvoiceProcessor = $createPerfexCRMInvoiceProcessor; + } + + public function execute(FetchPerfexCRMInvoiceObject $fetchPerfexCRMInvoiceObject) + { + //invoiceId to be returned - fetch or create + $invoiceId = 0; + + //get the client id + $customer = $this->fetchesPerfexCRMCustomer->execute($fetchPerfexCRMInvoiceObject->getEmail()); + $transaction = $fetchPerfexCRMInvoiceObject->getTransaction(); + + $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('FetchPerfexCRMInvoiceProcessor debug $number: '.$number)); + if(is_null($invoice)){ + $result = $this->createPerfexCRMInvoiceProcessor->execute($transaction, null, null, $fetchPerfexCRMInvoiceObject->getIsPaid()); + if ($result) { + $invoiceId = $result->payload['id']; + } else { + $log['message'] = 'FetchPerfexCRMInvoiceProcessor failed'; + Helper::debugLogger($log); + } + } + else{ + $invoiceId = $invoice->id; + } + + return $invoiceId; + } +} diff --git a/app/Classes/Modules/PerfexCRM/Processors/NewLeadTaskToPerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/NewLeadTaskToPerfexCRMProcessor.php index bd03c894..2195fc46 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/NewLeadTaskToPerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/NewLeadTaskToPerfexCRMProcessor.php @@ -32,7 +32,8 @@ class NewLeadTaskToPerfexCRMProcessor PerfexCRMTasks::TASK_IDENTIFICATION_1['status'], PerfexCRMTasks::TASK_IDENTIFICATION_1['department'], PerfexCRMTaskPriority::DEFAULT, - "0" + "0", + 0 ); CreatePerfexCRMSingleTask::dispatch($createTaskPerfexCRMObject); diff --git a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php index 903c640e..b996e592 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessor.php @@ -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 ); diff --git a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php index cd5990f1..7ec712c6 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php +++ b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php @@ -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 ); @@ -42,7 +41,6 @@ class TransactionToPerfexCRMProcessorV2 } } } catch (\Exception $exception) { - Log::error('TransactionToPerfexCRMProcessor debug:'); Log::error($exception); } return true; @@ -233,7 +231,7 @@ class TransactionToPerfexCRMProcessorV2 private function dispatchUpdateJob(Transaction $model, int $status, UpdatePerfexCRMObject $updatePerfexCRMObject) { - $withInvoice = ($model->type === TransactionType::PAYMENT && $model->owner === Booking::class && $status === ApprovalStatus::APPROVED); + $withInvoice = ($model->type === TransactionType::PAYMENT && $model->owner instanceof Booking && $status === ApprovalStatus::APPROVED); UpdatePerfexCRMPrelude::dispatch($model, $updatePerfexCRMObject, $withInvoice); } diff --git a/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php index 7527760f..2971e3c8 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php @@ -203,18 +203,23 @@ class UpdatePerfexCRMProcessor } // Get existing or create task - $result = $this->fetchesPerfexCRMTask->execute($tasks[$count]['name'], $milestoneId, 'project', $projectId); + $taskName = $tasks[$count]['name'];; + if($updatePerfexCRMObject->getInvoiceId() != 0){ + $taskName = $taskName." (".$updatePerfexCRMObject->getInvoiceId().")"; + } + $result = $this->fetchesPerfexCRMTask->execute($taskName, $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($taskName, $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 diff --git a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMTask.php b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMTask.php index f2f461a4..ace2aa00 100644 --- a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMTask.php +++ b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMTask.php @@ -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 != ""){ @@ -49,6 +50,7 @@ class CreatesPerfexCRMTask 'custom_fields' => $custom_fields, 'priority' => $priority, 'duedate' => date('Y-m-d', strtotime('+' . $duedate . ' days')), + 'invoice_id' => $invoiceId, ]; if($leadId != '') { @@ -66,6 +68,7 @@ class CreatesPerfexCRMTask 'custom_fields' => $custom_fields, 'priority' => $priority, 'duedate' => date('Y-m-d', strtotime('+' . $duedate . ' days')), + 'invoice_id' => 0 ]; } diff --git a/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMTask.php b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMTask.php index 78b61bd6..6cd4f052 100644 --- a/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMTask.php +++ b/app/Classes/Modules/PerfexCRM/Services/FetchesPerfexCRMTask.php @@ -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);