diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index dc50579a77d7..8ac37b29b3f0 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 a77acd7d344f..2aab08c5525a 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 ec076f184eea..4d03456425d5 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 20672471751a..17f6e820fe9c 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() {