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); if(is_null($invoice)){ $result = $this->createPerfexCRMInvoiceProcessor->execute($transaction); if ($result) { $invoiceId = $result->payload['id']; } else { $log['message'] = 'FetchPerfexCRMInvoiceProcessor failed for transaction > bill_no: '.$number; Log::channel('perfex_crm')->info($log); } } else{ $invoiceId = $invoice->id; } return $invoiceId; } }