From bdeb31d4b4ca0b6cb1f47dd5d694dd40456de1a0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Mar 2022 09:22:31 +1100 Subject: [PATCH] Fixes for wave imports --- app/Import/Providers/Wave.php | 4 ++ .../Transformer/Wave/InvoiceTransformer.php | 39 ++++++++++++++++--- app/PaymentDrivers/Stripe/CreditCard.php | 4 +- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/Import/Providers/Wave.php b/app/Import/Providers/Wave.php index bed0e1fdf6..c4f1a106cc 100644 --- a/app/Import/Providers/Wave.php +++ b/app/Import/Providers/Wave.php @@ -140,6 +140,10 @@ class Wave extends BaseImport implements ImportInterface $entity_type = 'vendor'; $data = $this->getCsvData($entity_type); + + if(!is_array($data)) + return; + $data = $this->preTransform($data, $entity_type); if (empty($data)) { diff --git a/app/Import/Transformer/Wave/InvoiceTransformer.php b/app/Import/Transformer/Wave/InvoiceTransformer.php index 7b8e4e8e6d..68e40115b3 100644 --- a/app/Import/Transformer/Wave/InvoiceTransformer.php +++ b/app/Import/Transformer/Wave/InvoiceTransformer.php @@ -31,19 +31,30 @@ class InvoiceTransformer extends BaseTransformer { throw new ImportException( 'Invoice number already exists' ); } + if(array_key_exists('Invoice Date', $invoice_data)) + $date_key = 'Invoice Date'; + else + $date_key = 'Transaction Date'; + + if(array_key_exists('Customer Name', $invoice_data)) + $customer_key = 'Customer Name'; + else + $customer_key = 'Customer'; + $transformed = [ 'company_id' => $this->company->id, - 'client_id' => $this->getClient( $customer_name = $this->getString( $invoice_data, 'Customer' ), null ), + 'client_id' => $this->getClient( $customer_name = $this->getString( $invoice_data, $customer_key ), null ), 'number' => $invoice_number = $this->getString( $invoice_data, 'Invoice Number' ), - 'date' => date( 'Y-m-d', strtotime( $invoice_data['Transaction Date'] ) ) ?: now()->format('Y-m-d'), //27-01-2022 + 'date' => date( 'Y-m-d', strtotime( $invoice_data[$date_key] ) ) ?: now()->format('Y-m-d'), //27-01-2022 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ), 'status_id' => Invoice::STATUS_SENT, + 'due_date' => array_key_exists('Due Date', $invoice_data) ? date( 'Y-m-d', strtotime( $invoice_data['Due Date'] ) ) : null, ]; $line_items = []; $payments = []; foreach ( $line_items_data as $record ) { - if ( $record['Account Type'] === 'Income' ) { + if (array_key_exists('Account Type', $record) && $record['Account Type'] === 'Income' ) { $description = $this->getString( $record, 'Transaction Line Description' ); // Remove duplicate data from description @@ -63,13 +74,31 @@ class InvoiceTransformer extends BaseTransformer { 'quantity' => 1, ]; - } elseif ( $record['Account Type'] === 'System Receivable Invoice' ) { + } elseif (array_key_exists('Account Type', $record) && $record['Account Type'] === 'System Receivable Invoice' ) { // This is a payment $payments[] = [ - 'date' => date( 'Y-m-d', strtotime( $invoice_data['Transaction Date'] ) ), + 'date' => date( 'Y-m-d', strtotime( $invoice_data[$date_key] ) ), 'amount' => $this->getFloat( $record, 'Amount (One column)' ), ]; } + else { + //could be a generate invoices.csv file + $line_items[] = [ + 'notes' => 'Imported Invoice', + 'cost' => $this->getFloat( $record, 'Invoice Total' ), + 'tax_name1' => 'Tax', + 'tax_rate1' => round($this->getFloat( $record, 'Invoice Tax Total' ) / $this->getFloat( $record, 'Invoice Total' ) * 100,2), + 'quantity' => 1, + ]; + + if($record['Invoice Paid'] > 0){ + $payments[] = [ + 'date' => date( 'Y-m-d', strtotime( $record['Last Payment Date'] ) ), + 'amount' => $this->getFloat( $record, 'Invoice Paid' ), + ]; + } + + } } $transformed['line_items'] = $line_items; diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 7f8451224c..6b7766a189 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -60,7 +60,9 @@ class CreditCard { - $description = $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')) . " for client {$this->stripe->client->present()->name()}"; + // $description = $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')) . " for client {$this->stripe->client->present()->name()}"; + $invoice_numbers = collect($data['invoices'])->pluck('invoice_number')->implode(','); + $description = "Invoices: {$invoice_numbers} for {$data['total']['amount_with_fee']} for client {$this->stripe->client->present()->name()}"; $payment_intent_data = [