createsPerfexCRMInvoice = $createsPerfexCRMInvoice; $this->createsPerfexCRMInvoicePayment = $createsPerfexCRMInvoicePayment; $this->convertsPerfexCRMLeadToCustomer = $convertsPerfexCRMLeadToCustomer; $this->fetchesPerfexCRMProject = $fetchesPerfexCRMProject; $this->createsPerfexCRMCustomerProject = $createsPerfexCRMCustomerProject; } /** * @param $transaction * @param $purchaseOrder * @param $supplier * @return null|object * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute($transaction, $purchaseOrder, $supplier) { $clientId = ""; $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; $date = Carbon::parse($transaction->booking->created_at)->format('Y-m-d'); $dueDate = Carbon::parse($transaction->booking->created_at)->format('Y-m-d'); $currency = 1; //cief TODO: To look into Malaysia and Chinese currency $subTotal = 0.00; $total = 0.00; $billingStreet = ""; $addresses = $supplier->addresses()->where('billing', '=', true)->first(); $billingStreet = $billingStreet.$addresses->street_one; $billingStreet = $billingStreet.$addresses->street_two.','; $billingStreet = $billingStreet.$addresses->district()->first()->name.','; $billingStreet = $billingStreet.$addresses->postcode; $billingStreet = $billingStreet.$addresses->state()->first()->name.','; $billingStreet = $billingStreet.$addresses->country()->first()->name; $allowedPaymentModes = []; $invoiceItems = []; $email = $supplier->employees()->first()->email; $result = $this->convertsPerfexCRMLeadToCustomer->execute($email); if(isset($result->payload)){ $clientId = $result->payload['client_id']; } //get project $projectId = ""; $bookingMarking = $transaction->owner->marking; $serviceTypeName = $transaction->owner->company->services()->where('id', $transaction->owner->service_id)->first()->name; $projectName = 'Exchange | '.$serviceTypeName.' | '.$bookingMarking; $project = $this->fetchesPerfexCRMProject->execute($projectName, $clientId); if(!is_null($project)){ $projectId = $project->id; } else{ $result = $this->createsPerfexCRMCustomerProject->execute($projectName, PerfexCRMProjectStatus::NOT_STARTED, $clientId); if(isset($result->payload)){ //Here means project creation successful $projectId = $result->payload['project_id']; } } //newitems foreach ($purchaseOrder->transactionDetails as $key => $transaction_detail){ $order = $key + 1; $stockCode = $transaction_detail->product_code; $description = $transaction_detail->product_name; $quantity = $transaction_detail->quantity; $unitPrice = 0.00; if($transaction->booking()->first()->fix_currency_id !== 1) $unitPrice = (1/$transaction->currency_rate) * $transaction_detail->price; else $unitPrice = $transaction_detail->price; //$totalAmount = 0.00; if($transaction->booking()->first()->fix_currency_id !== 1){ //$totalAmount = (float)number_format( (1/$transaction->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity; $subTotal += number_format((1/$transaction->currency_rate) * $transaction_detail->price, 2,'.','') * $transaction_detail->quantity; } else { //$totalAmount = (float)number_format($transaction_detail->price, 2,'.','')*$transaction_detail->quantity; $subTotal += number_format($transaction_detail->price, 2,'.','') * $transaction_detail->quantity; } //string $description, string $longDescription, int $qty, int $rate, int $order, string $unit $invoiceSingleItem = new InvoiceSingleItemPerfexCRMObject( $description, "", $quantity, $unitPrice, $order, "" ); array_push($invoiceItems, $invoiceSingleItem); } if($transaction->booking()->first()->fix_currency_id !== 1){ $total = ((1/$transaction->currency_rate) * $transaction->amount) + $transaction->service_charge + $transaction->tax; } else{ $total = $transaction->amount + $transaction->service_charge + $transaction->tax; } //This setting is similar to Setup > Leads > Sources, Setup > Leads > Statuses //Can be found at Finance > Payment Modes array_push($allowedPaymentModes, 1, 2); $invoicePerfexCRMObject = new InvoicePerfexCRMObject( $clientId, $number, $date, $dueDate, $currency, $subTotal, $total, $billingStreet, $projectId, $allowedPaymentModes, $invoiceItems ); $result = $this->createsPerfexCRMInvoice->execute($invoicePerfexCRMObject); if(!is_null($result)) { if($result->payload['id']){ $invoicePaymentPerfexCRMObject = new InvoicePaymentPerfexCRMObject( $result->payload['id'], $total, $date, 1, "", "" ); $this->createsPerfexCRMInvoicePayment->execute($invoicePaymentPerfexCRMObject); } } //else: logs will record the following: //"status":false,"error":{"number":"The Invoice number is already in use"},"message":"

The Invoice number is already in use<\/p>"} return true; } }