From c3c868b90da520fc5dc3cea6e299747f89963066 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 19 Dec 2020 12:45:12 +1100 Subject: [PATCH 1/5] Working on Invoice + Payment csv import --- app/Import/Definitions/PaymentMap.php | 58 +++++++++++++++++ app/Import/Definitions/ProductMap.php | 2 - app/Import/Transformers/BaseTransformer.php | 9 +++ app/Import/Transformers/ClientTransformer.php | 9 +++ .../Transformers/InvoiceTransformer.php | 65 +++++++++++++++++++ .../Transformers/PaymentTransformer.php | 47 ++++++++++++++ .../Transformers/ProductTransformer.php | 9 +++ app/Jobs/Import/CSVImport.php | 2 +- 8 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 app/Import/Definitions/PaymentMap.php create mode 100644 app/Import/Transformers/InvoiceTransformer.php create mode 100644 app/Import/Transformers/PaymentTransformer.php diff --git a/app/Import/Definitions/PaymentMap.php b/app/Import/Definitions/PaymentMap.php new file mode 100644 index 0000000000..c9567e50e0 --- /dev/null +++ b/app/Import/Definitions/PaymentMap.php @@ -0,0 +1,58 @@ + 'payment.number', + 1 => 'payment.user_id', + 2 => 'payment.amount', + 3 => 'payment.refunded', + 4 => 'payment.applied', + 5 => 'payment.transaction_reference', + 6 => 'payment.private_notes', + 7 => 'payment.custom_value1', + 8 => 'payment.custom_value2', + 9 => 'payment.custom_value3', + 10 => 'payment.custom_value4', + 11 => 'payment.client_id', + 12 => 'payment.invoice_number', + 13 => 'payment.date', + 14 => 'payment.method', + ]; + } + + public static function import_keys() + { + return [ + 0 => 'texts.number', + 1 => 'texts.user', + 2 => 'texts.amount', + 3 => 'texts.refunded', + 4 => 'texts.applied', + 5 => 'texts.transaction_reference', + 6 => 'texts.private_notes', + 7 => 'texts.custom_value', + 8 => 'texts.custom_value', + 9 => 'texts.custom_value', + 10 => 'texts.custom_value', + 11 => 'texts.client', + 12 => 'texts.invoice_number', + 13 => 'texts.date', + 14 => 'texts.method' + ]; + } +} + diff --git a/app/Import/Definitions/ProductMap.php b/app/Import/Definitions/ProductMap.php index 91506fe9aa..d795828528 100644 --- a/app/Import/Definitions/ProductMap.php +++ b/app/Import/Definitions/ProductMap.php @@ -31,7 +31,6 @@ class ProductMap 12 => 'product.custom_value2', 13 => 'product.custom_value3', 14 => 'product.custom_value4', - 15 => 'product.user_id', ]; } @@ -53,7 +52,6 @@ class ProductMap 12 => 'texts.custom_value', 13 => 'texts.custom_value', 14 => 'texts.custom_value', - 15 => 'texts.user', ]; } } diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index 743c127084..dc50579a77 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -1,4 +1,13 @@ $this->maps['company']->id, + 'number' => $this->getString($data, 'invoice.number'), + 'user_id' => $this->getString($data, 'invoice.user_id'), + 'amount' => $this->getString($data, 'invoice.amount'), + 'balance' => $this->getString($data, 'invoice.balance'), + 'client_id' => $this->getString($data, 'invoice.client_id'), + 'discount' => $this->getString($data, 'invoice.discount'), + 'po_number' => $this->getString($data, 'invoice.po_number'), + 'date' => $this->getString($data, 'invoice.date'), + 'due_date' => $this->getString($data, 'invoice.due_date'), + 'terms' => $this->getString($data, 'invoice.terms'), + 'public_notes' => $this->getString($data, 'invoice.public_notes'), + 'is_sent' => $this->getString($data, 'invoice.is_sent'), + 'private_notes' => $this->getString($data, 'invoice.private_notes'), + 'uses_inclusive_taxes' => $this->getString($data, 'invoice.uses_inclusive_taxes'), + 'tax_name1' => $this->getString($data, 'invoice.tax_name1'), + 'tax_rate1' => $this->getString($data, 'invoice.tax_rate1'), + 'tax_name2' => $this->getString($data, 'invoice.tax_name2'), + 'tax_rate2' => $this->getString($data, 'invoice.tax_rate2'), + 'tax_name3' => $this->getString($data, 'invoice.tax_name3'), + 'tax_rate3' => $this->getString($data, 'invoice.tax_rate3'), + 'custom_value1' => $this->getString($data, 'invoice.custom_value1'), + 'custom_value2' => $this->getString($data, 'invoice.custom_value2'), + 'custom_value3' => $this->getString($data, 'invoice.custom_value3'), + 'custom_value4' => $this->getString($data, 'invoice.custom_value4'), + 'is_amount_discount' => $this->getString($data, 'invoice.is_amount_discount'), + 'footer' => $this->getString($data, 'invoice.footer'), + 'partial' => $this->getString($data, 'invoice.partial'), + 'partial_due_date' => $this->getString($data, 'invoice.partial_due_date'), + 'custom_surcharge1' => $this->getString($data, 'invoice.custom_surcharge1'), + 'custom_surcharge2' => $this->getString($data, 'invoice.custom_surcharge2'), + 'custom_surcharge3' => $this->getString($data, 'invoice.custom_surcharge3'), + 'custom_surcharge4' => $this->getString($data, 'invoice.custom_surcharge4'), + 'exchange_rate' => $this->getString($data, 'invoice.exchange_rate'), + ]; + } +} \ No newline at end of file diff --git a/app/Import/Transformers/PaymentTransformer.php b/app/Import/Transformers/PaymentTransformer.php new file mode 100644 index 0000000000..ec076f184e --- /dev/null +++ b/app/Import/Transformers/PaymentTransformer.php @@ -0,0 +1,47 @@ + $this->maps['company']->id, + 'number' => $this->getString($data, 'payment.number'), + 'user_id' => $this->getString($data, 'payment.user_id'), + 'amount' => $this->getString($data, 'payment.amount'), + 'refunded' => $this->getString($data, 'payment.refunded'), + 'applied' => $this->getString($data, 'payment.applied'), + 'transaction_reference' => $this->getString($data, 'payment.transaction_reference '), + 'date' => $this->getString($data, 'payment.date'), + 'private_notes' => $this->getString($data, 'payment.private_notes'), + 'number' => $this->getString($data, 'number'), + 'custom_value1' => $this->getString($data, 'custom_value1'), + 'custom_value2' => $this->getString($data, 'custom_value2'), + 'custom_value3' => $this->getString($data, 'custom_value3'), + 'custom_value4' => $this->getString($data, 'custom_value4'), + 'client_id' => $this->getString($data, 'client_id'), + 'invoice_number' => $this->getString($data, 'payment.invoice_number'), + ]; + } +} \ No newline at end of file diff --git a/app/Import/Transformers/ProductTransformer.php b/app/Import/Transformers/ProductTransformer.php index 6069bb51b0..84ff28f3b3 100644 --- a/app/Import/Transformers/ProductTransformer.php +++ b/app/Import/Transformers/ProductTransformer.php @@ -1,4 +1,13 @@ column_map); + info("import".ucfirst($this->entity_type)); $this->{"import".ucfirst($this->entity_type)}(); - info(print_r($this->maps,1)); } public function failed($exception) From 298deac06265fd84cf12225b339bd67fce41e93f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 19 Dec 2020 14:49:15 +1100 Subject: [PATCH 2/5] Working on Invoice CSV Import --- app/Import/Transformers/BaseTransformer.php | 28 +++- .../Transformers/InvoiceTransformer.php | 4 +- .../Transformers/PaymentTransformer.php | 1 + app/Jobs/Import/CSVImport.php | 132 ++++++++++++++++++ 4 files changed, 161 insertions(+), 4 deletions(-) diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index dc50579a77..8ac37b29b3 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -51,12 +51,38 @@ class BaseTransformer $code = array_key_exists('client.currency_id', $data) ? $data['client.currency_id'] : false; if ($code) { - return $this->maps['currencies']->where('code', $code)->first()->id; + $currency = $this->maps['currencies']->where('code', $code)->first(); + + if($currency_id) + return $currency->id; } return $this->maps['company']->settings->currency_id; } + public function getClient($client_key) + { + $clients = $this->maps['company']->clients; + + $clients = $clients->where('name', $client_key); + + if($clients->count() >= 1) + return $clients->first()->id; + + $contacts = ClientContact::where('company_id', $this->maps['company']->id) + ->where('email', $client_key); + + if($contacts->count() >=1) + return $contact->first()->client_id; + + + return NULL; + + } + + + +/////////////////////////////////////////////////////////////////////////////////// /** * @param $name * diff --git a/app/Import/Transformers/InvoiceTransformer.php b/app/Import/Transformers/InvoiceTransformer.php index a77acd7d34..2aab08c552 100644 --- a/app/Import/Transformers/InvoiceTransformer.php +++ b/app/Import/Transformers/InvoiceTransformer.php @@ -31,7 +31,7 @@ class InvoiceTransformer extends BaseTransformer 'user_id' => $this->getString($data, 'invoice.user_id'), 'amount' => $this->getString($data, 'invoice.amount'), 'balance' => $this->getString($data, 'invoice.balance'), - 'client_id' => $this->getString($data, 'invoice.client_id'), + 'client_id' => $this->getClient($this->getString($data, 'invoice.client_id')), 'discount' => $this->getString($data, 'invoice.discount'), 'po_number' => $this->getString($data, 'invoice.po_number'), 'date' => $this->getString($data, 'invoice.date'), @@ -40,7 +40,6 @@ class InvoiceTransformer extends BaseTransformer 'public_notes' => $this->getString($data, 'invoice.public_notes'), 'is_sent' => $this->getString($data, 'invoice.is_sent'), 'private_notes' => $this->getString($data, 'invoice.private_notes'), - 'uses_inclusive_taxes' => $this->getString($data, 'invoice.uses_inclusive_taxes'), 'tax_name1' => $this->getString($data, 'invoice.tax_name1'), 'tax_rate1' => $this->getString($data, 'invoice.tax_rate1'), 'tax_name2' => $this->getString($data, 'invoice.tax_name2'), @@ -51,7 +50,6 @@ class InvoiceTransformer extends BaseTransformer 'custom_value2' => $this->getString($data, 'invoice.custom_value2'), 'custom_value3' => $this->getString($data, 'invoice.custom_value3'), 'custom_value4' => $this->getString($data, 'invoice.custom_value4'), - 'is_amount_discount' => $this->getString($data, 'invoice.is_amount_discount'), 'footer' => $this->getString($data, 'invoice.footer'), 'partial' => $this->getString($data, 'invoice.partial'), 'partial_due_date' => $this->getString($data, 'invoice.partial_due_date'), diff --git a/app/Import/Transformers/PaymentTransformer.php b/app/Import/Transformers/PaymentTransformer.php index ec076f184e..4d03456425 100644 --- a/app/Import/Transformers/PaymentTransformer.php +++ b/app/Import/Transformers/PaymentTransformer.php @@ -42,6 +42,7 @@ class PaymentTransformer extends BaseTransformer 'custom_value4' => $this->getString($data, 'custom_value4'), 'client_id' => $this->getString($data, 'client_id'), 'invoice_number' => $this->getString($data, 'payment.invoice_number'), + 'method' => $this ]; } } \ No newline at end of file diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index 2067247175..17f6e820fe 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -12,18 +12,23 @@ namespace App\Jobs\Import; use App\Factory\ClientFactory; +use App\Factory\InvoiceFactory; use App\Factory\ProductFactory; use App\Http\Requests\Client\StoreClientRequest; +use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Product\StoreProductRequest; use App\Import\Transformers\ClientTransformer; +use App\Import\Transformers\InvoiceTransformer; use App\Import\Transformers\ProductTransformer; use App\Libraries\MultiDB; use App\Models\Client; use App\Models\Company; use App\Models\Currency; +use App\Models\Invoice; use App\Models\User; use App\Repositories\ClientContactRepository; use App\Repositories\ClientRepository; +use App\Repositories\InvoiceRepository; use App\Repositories\ProductRepository; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -134,6 +139,133 @@ class CSVImport implements ShouldQueue } } + private function importInvoice() + { + info("import invoices"); + + $records = $this->getCsvData(); + + $invoice_number_key = array_search('Invoice Number', reset($records)); + + info("number key = {$invoice_number_key}"); + + $unique_array_filter = array_unique(array_column($records, 'Invoice Number')); + +info('unique array_filter'); +info(print_r($unique_array_filter,1)); + + $unique_invoices = array_intersect_key( $records, $unique_array_filter ); + +info("unique invoices"); + +info(print_r($unique_invoices,1)); + + $invoice = $invoice_transformer->transform(reset($invoices)); + + foreach($unique_invoices as $val) { + + $invoices = array_filter($arr, function($item) use ($val){ + return $item['Invoice Number'] == $val['Invoice Number']; + }); + + $this->processInvoice($invoices); + } + + } + + private function processInvoices($invoices) + { + + $invoice_repository = new InvoiceRepository(); + $invoice_transformer = new InvoiceTransformer($this->maps); + + $items = []; + +info("invoice = "); +info(print_r($invoice,1)); + + foreach($invoices as $record) + { + $keys = $this->column_map; + + $item_keys = [ + 36 => 'item.quantity', + 37 => 'item.cost', + 38 => 'item.product_key', + 39 => 'item.notes', + 40 => 'item.discount', + 41 => 'item.is_amount_discount', + 42 => 'item.tax_name1', + 43 => 'item.tax_rate1', + 44 => 'item.tax_name2', + 45 => 'item.tax_rate2', + 46 => 'item.tax_name3', + 47 => 'item.tax_rate3', + 48 => 'item.custom_value1', + 49 => 'item.custom_value2', + 50 => 'item.custom_value3', + 51 => 'item.custom_value4', + 52 => 'item.type_id', + ]; + + $values = array_intersect_key($record, $item_keys); + + $items[] = array_combine($keys, $values); + + } + +info("items"); +info(print_r($items,1)); + + $invoice->line_items = $items; + + $validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules()); + + if ($validator->fails()) { + $this->error_array[] = ['product' => $invoice, 'error' => json_encode($validator->errors())]; + } else { + $invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record))); + + $invoice->save(); + + $this->maps['invoices'][] = $invoice->id; + + $this->performInvoiceActions($invoice, $record, $invoice_repository); + } + } + + private function performInvoiceActions($invoice, $record, $invoice_repository) + { + + $invoice = $this->actionInvoiceStatus($invoice, $record, $invoice_repository); + } + + private function actionInvoiceStatus($invoice, $status, $invoice_repository) + { + switch ($status) { + case 'Archived': + $invoice_repository->archive($invoice); + $invoice->fresh(); + break; + case 'Sent': + $invoice = $invoice->service()->markSent()->save(); + break; + case 'Viewed'; + $invoice = $invoice->service()->markSent()->save(); + break; + default: + # code... + break; + } + + if($invoice->balance < $invoice->amount && $invoice->status_id <= Invoice::STATUS_SENT){ + $invoice->status_id = Invoice::STATUS_PARTIAL; + $invoice->save(); + } + + return $invoice; + } + //todo limit client imports for hosted version private function importClient() { From 4877d3c2f5e880de7cb44c6e08a021e587dd047e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 19 Dec 2020 15:11:15 +1100 Subject: [PATCH 3/5] Working on importing invoices --- app/Import/Transformers/BaseTransformer.php | 1 + app/Jobs/Import/CSVImport.php | 28 +++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index 8ac37b29b3..63f33713f5 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -11,6 +11,7 @@ namespace App\Import\Transformers; +use App\Models\ClientContact; use Carbon; use Exception; diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index 17f6e820fe..ffb0f92ae9 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -97,6 +97,10 @@ class CSVImport implements ShouldQueue info("import".ucfirst($this->entity_type)); $this->{"import".ucfirst($this->entity_type)}(); + + info("errors"); + info(print_r($this->$this->error_array,1)); + } public function failed($exception) @@ -141,6 +145,10 @@ class CSVImport implements ShouldQueue private function importInvoice() { + + $invoice_repository = new InvoiceRepository(); + $invoice_transformer = new InvoiceTransformer($this->maps); + info("import invoices"); $records = $this->getCsvData(); @@ -148,8 +156,11 @@ class CSVImport implements ShouldQueue $invoice_number_key = array_search('Invoice Number', reset($records)); info("number key = {$invoice_number_key}"); - - $unique_array_filter = array_unique(array_column($records, 'Invoice Number')); + + if ($this->skip_header) + array_shift($records); + + $unique_array_filter = array_unique($records[$invoice_number_key]); info('unique array_filter'); info(print_r($unique_array_filter,1)); @@ -160,25 +171,22 @@ info("unique invoices"); info(print_r($unique_invoices,1)); - $invoice = $invoice_transformer->transform(reset($invoices)); + $invoice = $invoice_transformer->transform(reset($records)); foreach($unique_invoices as $val) { - $invoices = array_filter($arr, function($item) use ($val){ - return $item['Invoice Number'] == $val['Invoice Number']; + $invoices = array_filter($records, function($item) use ($val, $invoice_number_key){ + return $item[$invoice_number_key] == $val[$invoice_number_key]; }); - $this->processInvoice($invoices); + $this->processInvoice($invoices, $invoice); } } - private function processInvoices($invoices) + private function processInvoice($invoices, $invoice) { - $invoice_repository = new InvoiceRepository(); - $invoice_transformer = new InvoiceTransformer($this->maps); - $items = []; info("invoice = "); From 6b02d5108078e9d3bf585a4eb8199dac719cc326 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 19 Dec 2020 18:28:58 +1100 Subject: [PATCH 4/5] Invoice Import --- app/Helpers/Invoice/InvoiceItemSum.php | 3 + .../Requests/Invoice/StoreInvoiceRequest.php | 9 +- app/Import/Transformers/BaseTransformer.php | 5 ++ app/Import/Transformers/ClientTransformer.php | 4 +- .../Transformers/InvoiceItemTransformer.php | 48 +++++++++++ .../Transformers/InvoiceTransformer.php | 14 ++-- .../Transformers/PaymentTransformer.php | 6 +- .../Transformers/ProductTransformer.php | 12 +-- app/Jobs/Import/CSVImport.php | 83 +++++++++---------- 9 files changed, 118 insertions(+), 66 deletions(-) create mode 100644 app/Import/Transformers/InvoiceItemTransformer.php diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 22b4008b15..2f555981dd 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -112,6 +112,9 @@ class InvoiceItemSum { $item_tax = 0; +info(print_r($this->item,1)); +info(print_r($this->invoice,1)); + $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100)); $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount); diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index 5f0464d762..b7e7734615 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -51,10 +51,13 @@ class StoreInvoiceRequest extends Request $rules['invitations.*.client_contact_id'] = 'distinct'; - if ($this->input('number')) { - $rules['number'] = 'unique:invoices,number,'.$this->id.',id,company_id,'.auth()->user()->company()->id; + // if ($this->input('number')) { + // $rules['number'] = 'unique:invoices,number,'.$this->id.',id,company_id,'.auth()->user()->company()->id; + // } + if (isset($this->number)) { + $rules['number'] = Rule::unique('invoices')->where('company_id', auth()->user()->company()->id); } -// $rules['number'] = new UniqueInvoiceNumberRule($this->all()); + $rules['project_id'] = ['bail', 'sometimes', new ValidProjectForClient($this->all())]; diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index 63f33713f5..2491e52d60 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -47,6 +47,11 @@ class BaseTransformer return (isset($data[$field]) && $data[$field]) ? $data[$field] : ''; } + public function getInvoiceTypeId($data, $field) + { + return (isset($data[$field]) && $data[$field]) ? $data[$field] : '1'; + } + public function getCurrencyByCode($data) { $code = array_key_exists('client.currency_id', $data) ? $data['client.currency_id'] : false; diff --git a/app/Import/Transformers/ClientTransformer.php b/app/Import/Transformers/ClientTransformer.php index df30759b16..b23e9fcde7 100644 --- a/app/Import/Transformers/ClientTransformer.php +++ b/app/Import/Transformers/ClientTransformer.php @@ -54,8 +54,8 @@ class ClientTransformer extends BaseTransformer 'custom_value2' => $this->getString($data, 'client.custom2'), 'custom_value3' => $this->getString($data, 'client.custom3'), 'custom_value4' => $this->getString($data, 'client.custom4'), - 'balance' => $this->getString($data, 'client.balance'), - 'paid_to_date' => $this->getString($data, 'client.paid_to_date'), + 'balance' => $this->getFloat($data, 'client.balance'), + 'paid_to_date' => $this->getFloat($data, 'client.paid_to_date'), 'credit_balance' => 0, 'settings' => $settings, 'client_hash' => Str::random(40), diff --git a/app/Import/Transformers/InvoiceItemTransformer.php b/app/Import/Transformers/InvoiceItemTransformer.php new file mode 100644 index 0000000000..5ddcca8df3 --- /dev/null +++ b/app/Import/Transformers/InvoiceItemTransformer.php @@ -0,0 +1,48 @@ + $this->getFloat($data, 'item.quantity'), + 'cost' => $this->getFloat($data, 'item.cost'), + 'product_key' => $this->getString($data, 'item.product_key'), + 'notes' => $this->getString($data, 'item.notes'), + 'discount' => $this->getFloat($data, 'item.discount'), + 'is_amount_discount' => $this->getString($data, 'item.is_amount_discount'), + 'tax_name1' => $this->getString($data, 'item.tax_name1'), + 'tax_rate1' => $this->getFloat($data, 'item.tax_rate1'), + 'tax_name2' => $this->getString($data, 'item.tax_name2'), + 'tax_rate2' => $this->getFloat($data, 'item.tax_rate2'), + 'tax_name3' => $this->getString($data, 'item.tax_name3'), + 'tax_rate3' => $this->getFloat($data, 'item.tax_rate3'), + 'custom_value1' => $this->getString($data, 'item.custom_value1'), + 'custom_value2' => $this->getString($data, 'item.custom_value2'), + 'custom_value3' => $this->getString($data, 'item.custom_value3'), + 'custom_value4' => $this->getString($data, 'item.custom_value4'), + 'type_id' => $this->getInvoiceTypeId($data, 'item.type_id'), + ]; + } +} \ No newline at end of file diff --git a/app/Import/Transformers/InvoiceTransformer.php b/app/Import/Transformers/InvoiceTransformer.php index 2aab08c552..a2410d3fd0 100644 --- a/app/Import/Transformers/InvoiceTransformer.php +++ b/app/Import/Transformers/InvoiceTransformer.php @@ -29,10 +29,10 @@ class InvoiceTransformer extends BaseTransformer 'company_id' => $this->maps['company']->id, 'number' => $this->getString($data, 'invoice.number'), 'user_id' => $this->getString($data, 'invoice.user_id'), - 'amount' => $this->getString($data, 'invoice.amount'), - 'balance' => $this->getString($data, 'invoice.balance'), + 'amount' => $this->getFloat($data, 'invoice.amount'), + 'balance' => $this->getFloat($data, 'invoice.balance'), 'client_id' => $this->getClient($this->getString($data, 'invoice.client_id')), - 'discount' => $this->getString($data, 'invoice.discount'), + 'discount' => $this->getFloat($data, 'invoice.discount'), 'po_number' => $this->getString($data, 'invoice.po_number'), 'date' => $this->getString($data, 'invoice.date'), 'due_date' => $this->getString($data, 'invoice.due_date'), @@ -41,17 +41,17 @@ class InvoiceTransformer extends BaseTransformer 'is_sent' => $this->getString($data, 'invoice.is_sent'), 'private_notes' => $this->getString($data, 'invoice.private_notes'), 'tax_name1' => $this->getString($data, 'invoice.tax_name1'), - 'tax_rate1' => $this->getString($data, 'invoice.tax_rate1'), + 'tax_rate1' => $this->getFloat($data, 'invoice.tax_rate1'), 'tax_name2' => $this->getString($data, 'invoice.tax_name2'), - 'tax_rate2' => $this->getString($data, 'invoice.tax_rate2'), + 'tax_rate2' => $this->getFloat($data, 'invoice.tax_rate2'), 'tax_name3' => $this->getString($data, 'invoice.tax_name3'), - 'tax_rate3' => $this->getString($data, 'invoice.tax_rate3'), + 'tax_rate3' => $this->getFloat($data, 'invoice.tax_rate3'), 'custom_value1' => $this->getString($data, 'invoice.custom_value1'), 'custom_value2' => $this->getString($data, 'invoice.custom_value2'), 'custom_value3' => $this->getString($data, 'invoice.custom_value3'), 'custom_value4' => $this->getString($data, 'invoice.custom_value4'), 'footer' => $this->getString($data, 'invoice.footer'), - 'partial' => $this->getString($data, 'invoice.partial'), + 'partial' => $this->getFloat($data, 'invoice.partial'), 'partial_due_date' => $this->getString($data, 'invoice.partial_due_date'), 'custom_surcharge1' => $this->getString($data, 'invoice.custom_surcharge1'), 'custom_surcharge2' => $this->getString($data, 'invoice.custom_surcharge2'), diff --git a/app/Import/Transformers/PaymentTransformer.php b/app/Import/Transformers/PaymentTransformer.php index 4d03456425..5ce5042f5b 100644 --- a/app/Import/Transformers/PaymentTransformer.php +++ b/app/Import/Transformers/PaymentTransformer.php @@ -29,9 +29,9 @@ class PaymentTransformer extends BaseTransformer 'company_id' => $this->maps['company']->id, 'number' => $this->getString($data, 'payment.number'), 'user_id' => $this->getString($data, 'payment.user_id'), - 'amount' => $this->getString($data, 'payment.amount'), - 'refunded' => $this->getString($data, 'payment.refunded'), - 'applied' => $this->getString($data, 'payment.applied'), + 'amount' => $this->getFloat($data, 'payment.amount'), + 'refunded' => $this->getFloat($data, 'payment.refunded'), + 'applied' => $this->getFloat($data, 'payment.applied'), 'transaction_reference' => $this->getString($data, 'payment.transaction_reference '), 'date' => $this->getString($data, 'payment.date'), 'private_notes' => $this->getString($data, 'payment.private_notes'), diff --git a/app/Import/Transformers/ProductTransformer.php b/app/Import/Transformers/ProductTransformer.php index 84ff28f3b3..c932823d66 100644 --- a/app/Import/Transformers/ProductTransformer.php +++ b/app/Import/Transformers/ProductTransformer.php @@ -29,15 +29,15 @@ class ProductTransformer extends BaseTransformer 'company_id' => $this->maps['company']->id, 'product_key' => $this->getString($data, 'product.product_key'), 'notes' => $this->getString($data, 'product.notes'), - 'cost' => $this->getString($data, 'product.cost'), - 'price' => $this->getString($data, 'product.price'), - 'quantity' => $this->getString($data, 'product.quantity'), + 'cost' => $this->getFloat($data, 'product.cost'), + 'price' => $this->getFloat($data, 'product.price'), + 'quantity' => $this->getFloat($data, 'product.quantity'), 'tax_name1' => $this->getString($data, 'product.tax_name1'), - 'tax_rate1' => $this->getString($data, 'product.tax_rate1'), + 'tax_rate1' => $this->getFloat($data, 'product.tax_rate1'), 'tax_name2' => $this->getString($data, 'product.tax_name2'), - 'tax_rate2' => $this->getString($data, 'product.tax_rate2'), + 'tax_rate2' => $this->getFloat($data, 'product.tax_rate2'), 'tax_name3' => $this->getString($data, 'product.tax_name3'), - 'tax_rate3' => $this->getString($data, 'product.tax_rate3'), + 'tax_rate3' => $this->getFloat($data, 'product.tax_rate3'), 'custom_value1' => $this->getString($data, 'product.custom_value1'), 'custom_value2' => $this->getString($data, 'product.custom_value2'), 'custom_value3' => $this->getString($data, 'product.custom_value3'), diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index ffb0f92ae9..a98d2bc1ef 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -18,6 +18,7 @@ use App\Http\Requests\Client\StoreClientRequest; use App\Http\Requests\Invoice\StoreInvoiceRequest; use App\Http\Requests\Product\StoreProductRequest; use App\Import\Transformers\ClientTransformer; +use App\Import\Transformers\InvoiceItemTransformer; use App\Import\Transformers\InvoiceTransformer; use App\Import\Transformers\ProductTransformer; use App\Libraries\MultiDB; @@ -99,7 +100,8 @@ class CSVImport implements ShouldQueue info("errors"); - info(print_r($this->$this->error_array,1)); + + info(print_r($this->error_array,1)); } @@ -146,11 +148,14 @@ class CSVImport implements ShouldQueue private function importInvoice() { - $invoice_repository = new InvoiceRepository(); $invoice_transformer = new InvoiceTransformer($this->maps); info("import invoices"); + info("column_map"); + + info(print_r($this->column_map,1)); + $records = $this->getCsvData(); $invoice_number_key = array_search('Invoice Number', reset($records)); @@ -160,77 +165,63 @@ class CSVImport implements ShouldQueue if ($this->skip_header) array_shift($records); + if(!$invoice_number_key){ + info("no invoice number to use as key - returning"); + return; + } + $unique_array_filter = array_unique($records[$invoice_number_key]); - -info('unique array_filter'); -info(print_r($unique_array_filter,1)); - $unique_invoices = array_intersect_key( $records, $unique_array_filter ); -info("unique invoices"); + foreach($unique_invoices as $unique) + { -info(print_r($unique_invoices,1)); + $keys = $this->column_map; + $values = array_intersect_key($unique, $this->column_map); + $invoice_data = array_combine($keys, $values); - $invoice = $invoice_transformer->transform(reset($records)); + $invoice = $invoice_transformer->transform($invoice_data); - foreach($unique_invoices as $val) { + foreach($unique_invoices as $val) { - $invoices = array_filter($records, function($item) use ($val, $invoice_number_key){ - return $item[$invoice_number_key] == $val[$invoice_number_key]; - }); + $invoices = array_filter($records, function($item) use ($val, $invoice_number_key){ + return $item[$invoice_number_key] == $val[$invoice_number_key]; + }); + + } $this->processInvoice($invoices, $invoice); + } + } private function processInvoice($invoices, $invoice) { - + $invoice_repository = new InvoiceRepository(); + $item_transformer = new InvoiceItemTransformer($this->maps); $items = []; -info("invoice = "); -info(print_r($invoice,1)); - foreach($invoices as $record) { - $keys = $this->column_map; - - $item_keys = [ - 36 => 'item.quantity', - 37 => 'item.cost', - 38 => 'item.product_key', - 39 => 'item.notes', - 40 => 'item.discount', - 41 => 'item.is_amount_discount', - 42 => 'item.tax_name1', - 43 => 'item.tax_rate1', - 44 => 'item.tax_name2', - 45 => 'item.tax_rate2', - 46 => 'item.tax_name3', - 47 => 'item.tax_rate3', - 48 => 'item.custom_value1', - 49 => 'item.custom_value2', - 50 => 'item.custom_value3', - 51 => 'item.custom_value4', - 52 => 'item.type_id', - ]; - - $values = array_intersect_key($record, $item_keys); - $items[] = array_combine($keys, $values); + $keys = $this->column_map; + $values = array_intersect_key($record, $this->column_map); + $invoice_data = array_combine($keys, $values); + + $items[] = $item_transformer->transform($invoice_data); } -info("items"); -info(print_r($items,1)); + $invoice['line_items'] = $items; - $invoice->line_items = $items; +info(print_r($invoice->toArray(),1)); $validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules()); if ($validator->fails()) { - $this->error_array[] = ['product' => $invoice, 'error' => json_encode($validator->errors())]; + $this->error_array[] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())]; } else { $invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record))); @@ -280,6 +271,8 @@ info(print_r($items,1)); //clients $records = $this->getCsvData(); +info(print_r($this->column_map,1)); + $contact_repository = new ClientContactRepository(); $client_repository = new ClientRepository($contact_repository); $client_transformer = new ClientTransformer($this->maps); From d8fab379ac607d4441280d8c1558f7d7648f03ed Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 20 Dec 2020 08:00:02 +1100 Subject: [PATCH 5/5] Fixes for release --- .github/workflows/release.yml | 9 +++++++++ app/Helpers/Invoice/InvoiceItemSum.php | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b4748d9635..ea01c19760 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,10 +5,19 @@ on: name: Upload Release Asset jobs: + run: + runs-on: ubuntu-18.04 + build: name: Upload Release Asset runs-on: ubuntu-latest steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + extensions: mysql, mysqlnd, sqlite3, bcmath, gd, curl, zip, openssl, mbstring, xml + - name: Checkout code uses: actions/checkout@v1 with: diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 2f555981dd..3aa1974741 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -112,8 +112,8 @@ class InvoiceItemSum { $item_tax = 0; -info(print_r($this->item,1)); -info(print_r($this->invoice,1)); +// info(print_r($this->item,1)); +// info(print_r($this->invoice,1)); $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100)); $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);