diff --git a/app/Http/Controllers/ImportController.php b/app/Http/Controllers/ImportController.php index 41102869a90e..89e549c24746 100644 --- a/app/Http/Controllers/ImportController.php +++ b/app/Http/Controllers/ImportController.php @@ -13,7 +13,6 @@ namespace App\Http\Controllers; use App\Http\Requests\Import\ImportRequest; use App\Http\Requests\Import\PreImportRequest; -use App\Jobs\Import\CSVImport; use App\Jobs\Import\CSVIngest; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Cache; diff --git a/app/Import/Transformers/Bank/BankTransformer.php b/app/Import/Transformers/Bank/BankTransformer.php deleted file mode 100644 index c96f9f0e683d..000000000000 --- a/app/Import/Transformers/Bank/BankTransformer.php +++ /dev/null @@ -1,81 +0,0 @@ - $transaction['transaction.bank_integration_id'], - 'transaction_id' => $this->getNumber($transaction,'transaction.transaction_id'), - 'amount' => abs($this->getFloat($transaction, 'transaction.amount')), - 'currency_id' => $this->getCurrencyByCode($transaction, 'transaction.currency'), - 'account_type' => strlen($this->getString($transaction, 'transaction.account_type')) > 1 ? $this->getString($transaction, 'transaction.account_type') : 'bank', - 'category_id' => $this->getNumber($transaction, 'transaction.category_id') > 0 ? $this->getNumber($transaction, 'transaction.category_id') : null, - 'category_type' => $this->getString($transaction, 'transaction.category_type'), - 'date' => array_key_exists('transaction.date', $transaction) ? $this->parseDate($transaction['transaction.date']) - : now()->format('Y-m-d'), - 'bank_account_id' => array_key_exists('transaction.bank_account_id', $transaction) ? $transaction['transaction.bank_account_id'] : 0, - 'description' => array_key_exists('transaction.description', $transaction) ? $transaction['transaction.description'] : '', - 'base_type' => $this->calculateType($transaction), - 'created_at' => $now, - 'updated_at' => $now, - 'company_id' => $this->company->id, - 'user_id' => $this->company->owner()->id, - ]; - - return $transformed; - } - - - private function calculateType($transaction) - { - - if(array_key_exists('transaction.base_type', $transaction) && (($transaction['transaction.base_type'] == 'CREDIT') || strtolower($transaction['transaction.base_type']) == 'deposit')) - return 'CREDIT'; - - if(array_key_exists('transaction.base_type', $transaction) && (($transaction['transaction.base_type'] == 'DEBIT') || strtolower($transaction['transaction.base_type']) == 'withdrawal')) - return 'DEBIT'; - - if(array_key_exists('transaction.category_id', $transaction)) - return 'DEBIT'; - - if(array_key_exists('transaction.category_type', $transaction) && $transaction['transaction.category_type'] == 'Income') - return 'CREDIT'; - - if(array_key_exists('transaction.category_type', $transaction)) - return 'DEBIT'; - - if(array_key_exists('transaction.amount', $transaction) && is_numeric($transaction['transaction.amount']) && $transaction['transaction.amount'] > 0) - return 'CREDIT'; - - return 'DEBIT'; - - } - -} \ No newline at end of file diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php deleted file mode 100644 index 17daf8105b8e..000000000000 --- a/app/Import/Transformers/BaseTransformer.php +++ /dev/null @@ -1,430 +0,0 @@ -maps = $maps; - } - - /** - * @param $data - * @param $field - * - * @return string - */ - public function getString($data, $field) - { - 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, $key = 'client.currency_id') - { - $code = array_key_exists($key, $data) ? $data[$key] : false; - - return $this->maps['currencies'][$code] ?? $this->maps['company']->settings->currency_id; - } - - public function getClient($client_name, $client_email) - { - $clients = $this->maps['company']->clients; - - $client_id_search = $clients->where('id_number', $client_name); - - if ($client_id_search->count() >= 1) { - return $client_id_search->first()->id; - } - - $client_name_search = $clients->where('name', $client_name); - - if ($client_name_search->count() >= 1) { - return $client_name_search->first()->id; - } - - if (! empty($client_email)) { - $contacts = ClientContact::where('company_id', $this->maps['company']->id) - ->where('email', $client_email); - - if ($contacts->count() >= 1) { - return $contacts->first()->client_id; - } - } - - return null; - } - - /////////////////////////////////////////////////////////////////////////////////// - /** - * @param $name - * - * @return bool - */ - public function hasClient($name) - { - $name = trim(strtolower($name)); - - return isset($this->maps['client'][$name]); - } - - /** - * @param $name - * - * @return bool - */ - public function hasVendor($name) - { - $name = trim(strtolower($name)); - - return isset($this->maps['vendor'][$name]); - } - - /** - * @param $key - * - * @return bool - */ - public function hasProduct($key) - { - $key = trim(strtolower($key)); - - return isset($this->maps['product'][$key]); - } - - /** - * @param $data - * @param $field - * - * @return int - */ - public function getNumber($data, $field) - { - return (isset($data->$field) && $data->$field) ? $data->$field : 0; - } - - /** - * @param $data - * @param $field - * - * @return float - */ - public function getFloat($data, $field) - { - if (array_key_exists($field, $data)) { - $number = preg_replace('/[^0-9-.]+/', '', $data[$field]); - } else { - $number = 0; - } - - return Number::parseFloat($number); - } - - public function getFloatWithSamePrecision($data, $field) - { - $precision = (int) strpos(strrev($data[$field]), "."); - - return round($data[$field], $precision); - } - /** - * @param $name - * - * @return null - */ - public function getClientId($name) - { - $name = strtolower(trim($name)); - - return isset($this->maps['client'][$name]) ? $this->maps['client'][$name] : null; - } - - /** - * @param $name - * - * @return null - */ - public function getProduct($data, $key, $field, $default = false) - { - $productKey = trim(strtolower($data->$key)); - - if (! isset($this->maps['product'][$productKey])) { - return $default; - } - - $product = $this->maps['product'][$productKey]; - - return $product->$field ?: $default; - } - - /** - * @param $name - * - * @return null - */ - public function getContact($email) - { - $email = trim(strtolower($email)); - - if (! isset($this->maps['contact'][$email])) { - return false; - } - - return $this->maps['contact'][$email]; - } - - /** - * @param $name - * - * @return null - */ - public function getCustomer($key) - { - $key = trim($key); - - if (! isset($this->maps['customer'][$key])) { - return false; - } - - return $this->maps['customer'][$key]; - } - - /** - * @param $name - * - * @return null - */ - public function getCountryId($name) - { - $name = strtolower(trim($name)); - - if (strlen($name) == 2) { - return $this->getCountryIdBy2($name); - } - - return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null; - } - - /** - * @param $name - * - * @return null - */ - public function getCountryIdBy2($name) - { - $name = strtolower(trim($name)); - - return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null; - } - - /** - * @param $name - * - * @return null - */ - public function getTaxRate($name) - { - $name = strtolower(trim($name)); - - return isset($this->maps['tax_rates'][$name]) ? $this->maps['tax_rates'][$name] : 0; - } - - /** - * @param $name - * - * @return null - */ - public function getTaxName($name) - { - $name = strtolower(trim($name)); - - return isset($this->maps['tax_names'][$name]) ? $this->maps['tax_names'][$name] : ''; - } - - /** - * @param $name - * - * @return mixed - */ - public function getFirstName($name) - { - $name = Utils::splitName($name); - - return $name[0]; - } - - /** - * @param $date - * @param string $format - * @param mixed $data - * @param mixed $field - * - * @return null - */ - public function getDate($data, $field) - { - if ($date = data_get($data, $field)) { - try { - $date = new Carbon($date); - } catch (Exception $e) { - // if we fail to parse return blank - $date = false; - } - } - - return $date ? $date->format('Y-m-d') : null; - } - - /** - * @param $name - * - * @return mixed - */ - public function getLastName($name) - { - $name = Utils::splitName($name); - - return $name[1]; - } - - /** - * @param $number - * - * @return string - */ - public function getInvoiceNumber($number) - { - return $number ? ltrim(trim($number), '0') : null; - } - - /** - * @param $invoiceNumber - * - * @return null - */ - public function getInvoiceId($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - $invoiceNumber = strtolower($invoiceNumber); - - return isset($this->maps['invoice'][$invoiceNumber]) ? $this->maps['invoice'][$invoiceNumber] : null; - } - - /** - * @param $invoiceNumber - * - * @return null - */ - public function getInvoicePublicId($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - $invoiceNumber = strtolower($invoiceNumber); - - return isset($this->maps['invoice'][$invoiceNumber]) ? $this->maps['invoices'][$invoiceNumber]->public_id : null; - } - - /** - * @param $invoiceNumber - * - * @return bool - */ - public function hasInvoice($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - $invoiceNumber = strtolower($invoiceNumber); - - return $this->maps['invoice'][$invoiceNumber] ?? null; - } - - /** - * @param $invoiceNumber - * - * @return null - */ - public function getInvoiceClientId($invoiceNumber) - { - $invoiceNumber = $this->getInvoiceNumber($invoiceNumber); - $invoiceNumber = strtolower($invoiceNumber); - - return $this->maps['invoice_client'][$invoiceNumber] ?? null; - } - - /** - * @param $name - * - * @return null - */ - public function getVendorId($name) - { - $name = strtolower(trim($name)); - - return $this->maps['vendor'][$name] ?? null; - } - - /** - * @param $name - * - * @return null - */ - public function getExpenseCategoryId($name) - { - $name = strtolower(trim($name)); - - return $this->maps['expense_category'][$name] ?? null; - } - - /** - * @param $name - * - * @return null - */ - public function getProjectId($name) - { - $name = strtolower(trim($name)); - - return $this->maps['project'][$name] ?? null; - } - - /** - * @param $name - * - * @return null - */ - public function getPaymentTypeId($name) - { - $name = strtolower(trim($name)); - - return $this->maps['payment_type'][$name] ?? null; - } -} diff --git a/app/Import/Transformers/ClientTransformer.php b/app/Import/Transformers/ClientTransformer.php deleted file mode 100644 index a77af36b5693..000000000000 --- a/app/Import/Transformers/ClientTransformer.php +++ /dev/null @@ -1,78 +0,0 @@ -name) && $this->hasClient($data->name)) { - return false; - } - - $settings = new \stdClass; - $settings->currency_id = (string) $this->getCurrencyByCode($data); - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'client.name'), - 'phone' => $this->getString($data, 'client.phone'), - 'address1' => $this->getString($data, 'client.address1'), - 'address2' => $this->getString($data, 'client.address2'), - 'city' => $this->getString($data, 'client.city'), - 'state' => $this->getString($data, 'client.state'), - 'shipping_address1' => $this->getString($data, 'client.shipping_address1'), - 'shipping_address2' => $this->getString($data, 'client.shipping_address2'), - 'shipping_city' => $this->getString($data, 'client.shipping_city'), - 'shipping_state' => $this->getString($data, 'client.shipping_state'), - 'shipping_postal_code' => $this->getString($data, 'client.shipping_postal_code'), - 'public_notes' => $this->getString($data, 'client.public_notes'), - 'private_notes' => $this->getString($data, 'client.private_notes'), - 'website' => $this->getString($data, 'client.website'), - 'vat_number' => $this->getString($data, 'client.vat_number'), - 'id_number' => $this->getString($data, 'client.id_number'), - 'custom_value1' => $this->getString($data, 'client.custom1'), - 'custom_value2' => $this->getString($data, 'client.custom2'), - 'custom_value3' => $this->getString($data, 'client.custom3'), - 'custom_value4' => $this->getString($data, 'client.custom4'), - '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), - 'contacts' => [ - [ - 'first_name' => $this->getString($data, 'contact.first_name'), - 'last_name' => $this->getString($data, 'contact.last_name'), - 'email' => $this->getString($data, 'contact.email'), - 'phone' => $this->getString($data, 'contact.phone'), - 'custom_value1' => $this->getString($data, 'contact.custom1'), - 'custom_value2' => $this->getString($data, 'contact.custom2'), - 'custom_value3' => $this->getString($data, 'contact.custom3'), - 'custom_value4' => $this->getString($data, 'contact.custom4'), - ], - ], - 'country_id' => isset($data->country_id) ? $this->getCountryId($data->country_id) : null, - 'shipping_country_id' => isset($data->shipping_country_id) ? $this->getCountryId($data->shipping_country_id) : null, - ]; - } -} diff --git a/app/Import/Transformers/Csv/ClientTransformer.php b/app/Import/Transformers/Csv/ClientTransformer.php deleted file mode 100644 index 44f826e1bbb5..000000000000 --- a/app/Import/Transformers/Csv/ClientTransformer.php +++ /dev/null @@ -1,120 +0,0 @@ -name) && $this->hasClient($data->name)) { - throw new ImportException('Client already exists'); - } - - $settings = new \stdClass(); - $settings->currency_id = (string) $this->getCurrencyByCode($data); - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'client.name'), - 'phone' => $this->getString($data, 'client.phone'), - 'address1' => $this->getString($data, 'client.address1'), - 'address2' => $this->getString($data, 'client.address2'), - 'postal_code' => $this->getString($data, 'client.postal_code'), - 'city' => $this->getString($data, 'client.city'), - 'state' => $this->getString($data, 'client.state'), - 'shipping_address1' => $this->getString( - $data, - 'client.shipping_address1' - ), - 'shipping_address2' => $this->getString( - $data, - 'client.shipping_address2' - ), - 'shipping_city' => $this->getString($data, 'client.shipping_city'), - 'shipping_state' => $this->getString( - $data, - 'client.shipping_state' - ), - 'shipping_postal_code' => $this->getString( - $data, - 'client.shipping_postal_code' - ), - 'public_notes' => $this->getString($data, 'client.public_notes'), - 'private_notes' => $this->getString($data, 'client.private_notes'), - 'website' => $this->getString($data, 'client.website'), - 'vat_number' => $this->getString($data, 'client.vat_number'), - 'id_number' => $this->getString($data, 'client.id_number'), - 'custom_value1' => $this->getString($data, 'client.custom_value1'), - 'custom_value2' => $this->getString($data, 'client.custom_value2'), - 'custom_value3' => $this->getString($data, 'client.custom_value3'), - 'custom_value4' => $this->getString($data, 'client.custom_value4'), - 'balance' => preg_replace( - '/[^0-9,.]+/', - '', - $this->getFloat($data, 'client.balance') - ), - 'paid_to_date' => preg_replace( - '/[^0-9,.]+/', - '', - $this->getFloat($data, 'client.paid_to_date') - ), - 'credit_balance' => 0, - 'settings' => $settings, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'first_name' => $this->getString( - $data, - 'contact.first_name' - ), - 'last_name' => $this->getString($data, 'contact.last_name'), - 'email' => $this->getString($data, 'contact.email'), - 'phone' => $this->getString($data, 'contact.phone'), - 'custom_value1' => $this->getString( - $data, - 'contact.custom_value1' - ), - 'custom_value2' => $this->getString( - $data, - 'contact.custom_value2' - ), - 'custom_value3' => $this->getString( - $data, - 'contact.custom_value3' - ), - 'custom_value4' => $this->getString( - $data, - 'contact.custom_value4' - ), - ], - ], - 'country_id' => isset($data['client.country']) - ? $this->getCountryId($data['client.country']) - : null, - 'shipping_country_id' => isset($data['client.shipping_country']) - ? $this->getCountryId($data['client.shipping_country']) - : null, - ]; - } -} diff --git a/app/Import/Transformers/Csv/ExpenseTransformer.php b/app/Import/Transformers/Csv/ExpenseTransformer.php deleted file mode 100644 index d954e1a21d25..000000000000 --- a/app/Import/Transformers/Csv/ExpenseTransformer.php +++ /dev/null @@ -1,42 +0,0 @@ -getClientId($data['expense.client']) : null; - - return [ - 'company_id' => $this->maps['company']->id, - 'amount' => $this->getFloat($data, 'expense.amount'), - 'currency_id' => $this->getCurrencyByCode($data, 'expense.currency_id'), - 'vendor_id' => isset($data['expense.vendor']) ? $this->getVendorId($data['expense.vendor']) : null, - 'client_id' => isset($data['expense.client']) ? $this->getClientId($data['expense.client']) : null, - 'date' => isset($data['expense.date']) ? date('Y-m-d', strtotime($data['expense.date'])) : null, - 'public_notes' => $this->getString($data, 'expense.public_notes'), - 'private_notes' => $this->getString($data, 'expense.private_notes'), - 'category_id' => isset($data['expense.category']) ? $this->getExpenseCategoryId($data['expense.category']) : null, - 'project_id' => isset($data['expense.project']) ? $this->getProjectId($data['expense.project']) : null, - 'payment_type_id' => isset($data['expense.payment_type']) ? $this->getPaymentTypeId($data['expense.payment_type']) : null, - 'payment_date' => isset($data['expense.payment_date']) ? date('Y-m-d', strtotime($data['expense.payment_date'])) : null, - 'custom_value1' => $this->getString($data, 'expense.custom_value1'), - 'custom_value2' => $this->getString($data, 'expense.custom_value2'), - 'custom_value3' => $this->getString($data, 'expense.custom_value3'), - 'custom_value4' => $this->getString($data, 'expense.custom_value4'), - 'transaction_reference' => $this->getString($data, 'expense.transaction_reference'), - 'should_be_invoiced' => $clientId ? true : false, - ]; - } -} diff --git a/app/Import/Transformers/Csv/InvoiceTransformer.php b/app/Import/Transformers/Csv/InvoiceTransformer.php deleted file mode 100644 index b345eccc3b6f..000000000000 --- a/app/Import/Transformers/Csv/InvoiceTransformer.php +++ /dev/null @@ -1,133 +0,0 @@ -hasInvoice($invoice_data['invoice.number'])) { - throw new ImportException('Invoice number already exists'); - } - - $invoiceStatusMap = [ - 'sent' => Invoice::STATUS_SENT, - 'draft' => Invoice::STATUS_DRAFT, - ]; - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'number' => $this->getString($invoice_data, 'invoice.number'), - 'user_id' => $this->getString($invoice_data, 'invoice.user_id'), - 'amount' => $amount = $this->getFloat($invoice_data, 'invoice.amount'), - 'balance' => isset($invoice_data['invoice.balance']) ? $this->getFloat($invoice_data, 'invoice.balance') : $amount, - 'client_id' => $this->getClient($this->getString($invoice_data, 'client.name'), $this->getString($invoice_data, 'client.email')), - 'discount' => $this->getFloat($invoice_data, 'invoice.discount'), - 'po_number' => $this->getString($invoice_data, 'invoice.po_number'), - 'date' => isset($invoice_data['invoice.date']) ? date('Y-m-d', strtotime($invoice_data['invoice.date'])) : now()->format('Y-m-d'), - 'due_date' => isset($invoice_data['invoice.due_date']) ? date('Y-m-d', strtotime($invoice_data['invoice.due_date'])) : null, - 'terms' => $this->getString($invoice_data, 'invoice.terms'), - 'public_notes' => $this->getString($invoice_data, 'invoice.public_notes'), - // 'is_sent' => $this->getString( $invoice_data, 'invoice.is_sent' ), - 'private_notes' => $this->getString($invoice_data, 'invoice.private_notes'), - 'tax_name1' => $this->getString($invoice_data, 'invoice.tax_name1'), - 'tax_rate1' => $this->getFloat($invoice_data, 'invoice.tax_rate1'), - 'tax_name2' => $this->getString($invoice_data, 'invoice.tax_name2'), - 'tax_rate2' => $this->getFloat($invoice_data, 'invoice.tax_rate2'), - 'tax_name3' => $this->getString($invoice_data, 'invoice.tax_name3'), - 'tax_rate3' => $this->getFloat($invoice_data, 'invoice.tax_rate3'), - 'custom_value1' => $this->getString($invoice_data, 'invoice.custom_value1'), - 'custom_value2' => $this->getString($invoice_data, 'invoice.custom_value2'), - 'custom_value3' => $this->getString($invoice_data, 'invoice.custom_value3'), - 'custom_value4' => $this->getString($invoice_data, 'invoice.custom_value4'), - 'footer' => $this->getString($invoice_data, 'invoice.footer'), - 'partial' => $this->getFloat($invoice_data, 'invoice.partial'), - 'partial_due_date' => $this->getString($invoice_data, 'invoice.partial_due_date'), - 'custom_surcharge1' => $this->getString($invoice_data, 'invoice.custom_surcharge1'), - 'custom_surcharge2' => $this->getString($invoice_data, 'invoice.custom_surcharge2'), - 'custom_surcharge3' => $this->getString($invoice_data, 'invoice.custom_surcharge3'), - 'custom_surcharge4' => $this->getString($invoice_data, 'invoice.custom_surcharge4'), - 'exchange_rate' => $this->getString($invoice_data, 'invoice.exchange_rate'), - 'status_id' => $invoiceStatusMap[$status = - strtolower($this->getString($invoice_data, 'invoice.status'))] ?? - Invoice::STATUS_SENT, - // 'viewed' => $status === 'viewed', - 'archived' => $status === 'archived', - ]; - - if (isset($invoice_data['payment.amount'])) { - $transformed['payments'] = [ - [ - 'date' => isset($invoice_data['payment.date']) ? date('Y-m-d', strtotime($invoice_data['payment.date'])) : date('y-m-d'), - 'transaction_reference' => $this->getString($invoice_data, 'payment.transaction_reference'), - 'amount' => $this->getFloat($invoice_data, 'payment.amount'), - ], - ]; - } elseif ($status === 'paid') { - $transformed['payments'] = [ - [ - 'date' => isset($invoice_data['payment.date']) ? date('Y-m-d', strtotime($invoice_data['payment.date'])) : date('y-m-d'), - 'transaction_reference' => $this->getString($invoice_data, 'payment.transaction_reference'), - 'amount' => $this->getFloat($invoice_data, 'invoice.amount'), - ], - ]; - } elseif (isset($transformed['amount']) && isset($transformed['balance']) && ($transformed['amount'] != $transformed['balance'])) { - $transformed['payments'] = [ - [ - 'date' => isset($invoice_data['payment.date']) ? date('Y-m-d', strtotime($invoice_data['payment.date'])) : date('y-m-d'), - 'transaction_reference' => $this->getString($invoice_data, 'payment.transaction_reference'), - 'amount' => $transformed['amount'] - $transformed['balance'], - ], - ]; - } - - $line_items = []; - foreach ($line_items_data as $record) { - $line_items[] = [ - 'quantity' => $this->getFloat($record, 'item.quantity'), - 'cost' => $this->getFloat($record, 'item.cost'), - 'product_key' => $this->getString($record, 'item.product_key'), - 'notes' => $this->getString($record, 'item.notes'), - 'discount' => $this->getFloat($record, 'item.discount'), - 'is_amount_discount' => filter_var($this->getString($record, 'item.is_amount_discount'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE), - 'tax_name1' => $this->getString($record, 'item.tax_name1'), - 'tax_rate1' => $this->getFloat($record, 'item.tax_rate1'), - 'tax_name2' => $this->getString($record, 'item.tax_name2'), - 'tax_rate2' => $this->getFloat($record, 'item.tax_rate2'), - 'tax_name3' => $this->getString($record, 'item.tax_name3'), - 'tax_rate3' => $this->getFloat($record, 'item.tax_rate3'), - 'custom_value1' => $this->getString($record, 'item.custom_value1'), - 'custom_value2' => $this->getString($record, 'item.custom_value2'), - 'custom_value3' => $this->getString($record, 'item.custom_value3'), - 'custom_value4' => $this->getString($record, 'item.custom_value4'), - 'type_id' => $this->getInvoiceTypeId($record, 'item.type_id'), - ]; - } - $transformed['line_items'] = $line_items; - - return $transformed; - } -} diff --git a/app/Import/Transformers/Csv/PaymentTransformer.php b/app/Import/Transformers/Csv/PaymentTransformer.php deleted file mode 100644 index fa0e391d1526..000000000000 --- a/app/Import/Transformers/Csv/PaymentTransformer.php +++ /dev/null @@ -1,65 +0,0 @@ -getClient($this->getString($data, 'payment.client_id'), $this->getString($data, 'payment.client_id')); - - if (empty($client_id)) { - throw new ImportException('Could not find client.'); - } - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'number' => $this->getString($data, 'payment.number'), - 'user_id' => $this->getString($data, 'payment.user_id'), - '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'), - 'custom_value1' => $this->getString($data, 'payment.custom_value1'), - 'custom_value2' => $this->getString($data, 'payment.custom_value2'), - 'custom_value3' => $this->getString($data, 'payment.custom_value3'), - 'custom_value4' => $this->getString($data, 'payment.custom_value4'), - 'client_id' => $client_id, - ]; - - if (isset($data['payment.invoice_number']) && - $invoice_id = $this->getInvoiceId($data['payment.invoice_number'])) { - $transformed['invoices'] = [ - [ - 'invoice_id' => $invoice_id, - 'amount' => $transformed['amount'] ?? null, - ], - ]; - } - - return $transformed; - } -} diff --git a/app/Import/Transformers/Csv/ProductTransformer.php b/app/Import/Transformers/Csv/ProductTransformer.php deleted file mode 100644 index 581259d131bc..000000000000 --- a/app/Import/Transformers/Csv/ProductTransformer.php +++ /dev/null @@ -1,47 +0,0 @@ - $this->maps['company']->id, - 'product_key' => $this->getString($data, 'product.product_key'), - 'notes' => $this->getString($data, 'product.notes'), - '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->getFloat($data, 'product.tax_rate1'), - 'tax_name2' => $this->getString($data, 'product.tax_name2'), - 'tax_rate2' => $this->getFloat($data, 'product.tax_rate2'), - 'tax_name3' => $this->getString($data, 'product.tax_name3'), - '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'), - 'custom_value4' => $this->getString($data, 'product.custom_value4'), - ]; - } -} diff --git a/app/Import/Transformers/Csv/VendorTransformer.php b/app/Import/Transformers/Csv/VendorTransformer.php deleted file mode 100644 index 6d94f3e9daa3..000000000000 --- a/app/Import/Transformers/Csv/VendorTransformer.php +++ /dev/null @@ -1,54 +0,0 @@ -name) && $this->hasVendor($data->name)) { - throw new ImportException('Vendor already exists'); - } - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'vendor.name'), - 'phone' => $this->getString($data, 'vendor.phone'), - 'id_number' => $this->getString($data, 'vendor.id_number'), - 'vat_number' => $this->getString($data, 'vendor.vat_number'), - 'website' => $this->getString($data, 'vendor.website'), - 'currency_id' => $this->getCurrencyByCode($data, 'vendor.currency_id'), - 'public_notes' => $this->getString($data, 'vendor.public_notes'), - 'private_notes' => $this->getString($data, 'vendor.private_notes'), - 'address1' => $this->getString($data, 'vendor.address1'), - 'address2' => $this->getString($data, 'vendor.address2'), - 'city' => $this->getString($data, 'vendor.city'), - 'state' => $this->getString($data, 'vendor.state'), - 'postal_code' => $this->getString($data, 'vendor.postal_code'), - 'custom_value1' => $this->getString($data, 'vendor.custom_value1'), - 'custom_value2' => $this->getString($data, 'vendor.custom_value2'), - 'custom_value3' => $this->getString($data, 'vendor.custom_value3'), - 'custom_value4' => $this->getString($data, 'vendor.custom_value4'), - 'vendor_contacts' => [ - [ - 'first_name' => $this->getString($data, 'vendor.first_name'), - 'last_name' => $this->getString($data, 'vendor.last_name'), - 'email' => $this->getString($data, 'vendor.email'), - 'phone' => $this->getString($data, 'vendor.phone'), - ], - ], - 'country_id' => isset($data['vendor.country_id']) ? $this->getCountryId($data['vendor.country_id']) : null, - ]; - } -} diff --git a/app/Import/Transformers/Freshbooks/ClientTransformer.php b/app/Import/Transformers/Freshbooks/ClientTransformer.php deleted file mode 100644 index 5b82f400c223..000000000000 --- a/app/Import/Transformers/Freshbooks/ClientTransformer.php +++ /dev/null @@ -1,57 +0,0 @@ -hasClient($data['Organization'])) { - throw new ImportException('Client already exists'); - } - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'Organization'), - 'phone' => $this->getString($data, 'Phone'), - 'address1' => $this->getString($data, 'Street'), - 'city' => $this->getString($data, 'City'), - 'state' => $this->getString($data, 'Province/State'), - 'postal_code' => $this->getString($data, 'Postal Code'), - 'country_id' => isset($data['Country']) ? $this->getCountryId($data['Country']) : null, - 'private_notes' => $this->getString($data, 'Notes'), - 'credit_balance' => 0, - 'settings' => new \stdClass, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'first_name' => $this->getString($data, 'First Name'), - 'last_name' => $this->getString($data, 'Last Name'), - 'email' => $this->getString($data, 'Email'), - 'phone' => $this->getString($data, 'Phone'), - ], - ], - ]; - } -} diff --git a/app/Import/Transformers/Freshbooks/InvoiceTransformer.php b/app/Import/Transformers/Freshbooks/InvoiceTransformer.php deleted file mode 100644 index 8a53262b4cf8..000000000000 --- a/app/Import/Transformers/Freshbooks/InvoiceTransformer.php +++ /dev/null @@ -1,87 +0,0 @@ -hasInvoice($invoice_data['Invoice #'])) { - throw new ImportException('Invoice number already exists'); - } - - $invoiceStatusMap = [ - 'sent' => Invoice::STATUS_SENT, - 'draft' => Invoice::STATUS_DRAFT, - ]; - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'client_id' => $this->getClient($this->getString($invoice_data, 'Client Name'), null), - 'number' => $this->getString($invoice_data, 'Invoice #'), - 'date' => isset($invoice_data['Date Issued']) ? date('Y-m-d', strtotime($invoice_data['Date Issued'])) : null, - 'currency_id' => $this->getCurrencyByCode($invoice_data, 'Currency'), - 'amount' => 0, - 'status_id' => $invoiceStatusMap[$status = - strtolower($this->getString($invoice_data, 'Invoice Status'))] ?? Invoice::STATUS_SENT, - // 'viewed' => $status === 'viewed', - ]; - - $line_items = []; - foreach ($line_items_data as $record) { - $line_items[] = [ - 'product_key' => $this->getString($record, 'Item Name'), - 'notes' => $this->getString($record, 'Item Description'), - 'cost' => $this->getFreshbookQuantityFloat($record, 'Rate'), - 'quantity' => $this->getFreshbookQuantityFloat($record, 'Quantity'), - 'discount' => $this->getFreshbookQuantityFloat($record, 'Discount Percentage'), - 'is_amount_discount' => false, - 'tax_name1' => $this->getString($record, 'Tax 1 Type'), - 'tax_rate1' => $this->getFreshbookQuantityFloat($record, 'Tax 1 Amount'), - 'tax_name2' => $this->getString($record, 'Tax 2 Type'), - 'tax_rate2' => $this->getFreshbookQuantityFloat($record, 'Tax 2 Amount'), - ]; - $transformed['amount'] += $this->getFreshbookQuantityFloat($record, 'Line Total'); - } - $transformed['line_items'] = $line_items; - - if (! empty($invoice_data['Date Paid'])) { - $transformed['payments'] = [[ - 'date' => date('Y-m-d', strtotime($invoice_data['Date Paid'])), - 'amount' => $transformed['amount'], - ]]; - } - - return $transformed; - } - - /** @return float */ - public function getFreshbookQuantityFloat($data, $field) - { - return $data[$field]; - } -} diff --git a/app/Import/Transformers/Invoice2Go/InvoiceTransformer.php b/app/Import/Transformers/Invoice2Go/InvoiceTransformer.php deleted file mode 100644 index 1dc59e55170c..000000000000 --- a/app/Import/Transformers/Invoice2Go/InvoiceTransformer.php +++ /dev/null @@ -1,91 +0,0 @@ -hasInvoice($invoice_data['DocumentNumber'])) { - throw new ImportException('Invoice number already exists'); - } - - $invoiceStatusMap = [ - 'unsent' => Invoice::STATUS_DRAFT, - 'sent' => Invoice::STATUS_SENT, - ]; - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'number' => $this->getString($invoice_data, 'DocumentNumber'), - 'notes' => $this->getString($invoice_data, 'Comment'), - 'date' => isset($invoice_data['DocumentDate']) ? date('Y-m-d', strtotime($invoice_data['DocumentDate'])) : null, - 'currency_id' => $this->getCurrencyByCode($invoice_data, 'Currency'), - 'amount' => 0, - 'status_id' => $invoiceStatusMap[$status = - strtolower($this->getString($invoice_data, 'DocumentStatus'))] ?? Invoice::STATUS_SENT, - // 'viewed' => $status === 'viewed', - 'line_items' => [ - [ - 'amount' => $amount = $this->getFloat($invoice_data, 'TotalAmount'), - 'quantity' => 1, - 'discount' => $this->getFloat($invoice_data, 'DiscountValue'), - 'is_amount_discount' => false, - ], - ], - ]; - - $client_id = - $this->getClient($this->getString($invoice_data, 'Name'), $this->getString($invoice_data, 'EmailRecipient')); - - if ($client_id) { - $transformed['client_id'] = $client_id; - } else { - $transformed['client'] = [ - 'name' => $this->getString($invoice_data, 'Name'), - 'address1' => $this->getString($invoice_data, 'DocumentRecipientAddress'), - 'shipping_address1' => $this->getString($invoice_data, 'ShipAddress'), - 'credit_balance' => 0, - 'settings' => new \stdClass, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'email' => $this->getString($invoice_data, 'Email'), - ], - ], - ]; - } - if (! empty($invoice_data['Date Paid'])) { - $transformed['payments'] = [ - [ - 'date' => date('Y-m-d', strtotime($invoice_data['DatePaid'])), - 'amount' => $this->getFloat($invoice_data, 'Payments'), - ], - ]; - } - - return $transformed; - } -} diff --git a/app/Import/Transformers/InvoiceItemTransformer.php b/app/Import/Transformers/InvoiceItemTransformer.php deleted file mode 100644 index acfbb9d9f6a9..000000000000 --- a/app/Import/Transformers/InvoiceItemTransformer.php +++ /dev/null @@ -1,46 +0,0 @@ - $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'), - ]; - } -} diff --git a/app/Import/Transformers/InvoiceTransformer.php b/app/Import/Transformers/InvoiceTransformer.php deleted file mode 100644 index 4142c0e544c8..000000000000 --- a/app/Import/Transformers/InvoiceTransformer.php +++ /dev/null @@ -1,61 +0,0 @@ - $this->maps['company']->id, - 'number' => $this->getString($data, 'invoice.number'), - 'user_id' => $this->getString($data, 'invoice.user_id'), - 'amount' => $this->getFloat($data, 'invoice.amount'), - 'balance' => $this->getFloat($data, 'invoice.balance'), - 'client_id' => $this->getClient($this->getString($data, 'client.name'), $this->getString($data, 'client.email')), - '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'), - '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'), - 'tax_name1' => $this->getString($data, 'invoice.tax_name1'), - 'tax_rate1' => $this->getFloatWithSamePrecision($data, 'invoice.tax_rate1'), - 'tax_name2' => $this->getString($data, 'invoice.tax_name2'), - 'tax_rate2' => $this->getFloatWithSamePrecision($data, 'invoice.tax_rate2'), - 'tax_name3' => $this->getString($data, 'invoice.tax_name3'), - 'tax_rate3' => $this->getFloatWithSamePrecision($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->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'), - 'custom_surcharge3' => $this->getString($data, 'invoice.custom_surcharge3'), - 'custom_surcharge4' => $this->getString($data, 'invoice.custom_surcharge4'), - 'exchange_rate' => $this->getString($data, 'invoice.exchange_rate'), - ]; - } -} diff --git a/app/Import/Transformers/Invoicely/ClientTransformer.php b/app/Import/Transformers/Invoicely/ClientTransformer.php deleted file mode 100644 index 5e2d58ae4a09..000000000000 --- a/app/Import/Transformers/Invoicely/ClientTransformer.php +++ /dev/null @@ -1,50 +0,0 @@ -hasClient($data['Client Name'])) { - throw new ImportException('Client already exists'); - } - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'Client Name'), - 'phone' => $this->getString($data, 'Phone'), - 'country_id' => isset($data['Country']) ? $this->getCountryIdBy2($data['Country']) : null, - 'credit_balance' => 0, - 'settings' => new \stdClass, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'email' => $this->getString($data, 'Email'), - 'phone' => $this->getString($data, 'Phone'), - ], - ], - ]; - } -} diff --git a/app/Import/Transformers/Invoicely/InvoiceTransformer.php b/app/Import/Transformers/Invoicely/InvoiceTransformer.php deleted file mode 100644 index 9e7c457c08d3..000000000000 --- a/app/Import/Transformers/Invoicely/InvoiceTransformer.php +++ /dev/null @@ -1,60 +0,0 @@ -hasInvoice($data['Details'])) { - throw new ImportException('Invoice number already exists'); - } - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'client_id' => $this->getClient($this->getString($data, 'Client'), null), - 'number' => $this->getString($data, 'Details'), - 'date' => isset($data['Date']) ? date('Y-m-d', strtotime($data['Date'])) : null, - 'due_date' => isset($data['Due']) ? date('Y-m-d', strtotime($data['Due'])) : null, - 'status_id' => Invoice::STATUS_SENT, - 'line_items' => [ - [ - 'cost' => $amount = $this->getFloat($data, 'Total'), - 'quantity' => 1, - ], - ], - ]; - - if (strtolower($data['Status']) === 'paid') { - $transformed['payments'] = [ - [ - 'date' => date('Y-m-d'), - 'amount' => $amount, - ], - ]; - } - - return $transformed; - } -} diff --git a/app/Import/Transformers/PaymentTransformer.php b/app/Import/Transformers/PaymentTransformer.php deleted file mode 100644 index 03fffa3d7595..000000000000 --- a/app/Import/Transformers/PaymentTransformer.php +++ /dev/null @@ -1,46 +0,0 @@ - $this->maps['company']->id, - 'number' => $this->getString($data, 'payment.number'), - 'user_id' => $this->getString($data, 'payment.user_id'), - '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'), - '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'), - 'method' => $this, - ]; - } -} diff --git a/app/Import/Transformers/Waveaccounting/ClientTransformer.php b/app/Import/Transformers/Waveaccounting/ClientTransformer.php deleted file mode 100644 index f9b95a42e487..000000000000 --- a/app/Import/Transformers/Waveaccounting/ClientTransformer.php +++ /dev/null @@ -1,75 +0,0 @@ -hasClient($data['customer_name'])) { - throw new ImportException('Client already exists'); - } - - $settings = new \stdClass; - $settings->currency_id = (string) $this->getCurrencyByCode($data, 'customer_currency'); - - if (strval($data['Payment Terms'] ?? '') > 0) { - $settings->payment_terms = $data['Payment Terms']; - } - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'customer_name'), - 'number' => $this->getString($data, 'account_number'), - 'phone' => $this->getString($data, 'phone'), - 'website' => $this->getString($data, 'website'), - 'country_id' => ! empty($data['country']) ? $this->getCountryId($data['country']) : null, - 'state' => $this->getString($data, 'province/state'), - 'address1' => $this->getString($data, 'address_line_1'), - 'address2' => $this->getString($data, 'address_line_2'), - 'city' => $this->getString($data, 'city'), - 'postal_code' => $this->getString($data, 'postal_code/zip_code'), - - 'shipping_country_id' => ! empty($data['ship-to_country']) ? $this->getCountryId($data['country']) : null, - 'shipping_state' => $this->getString($data, 'ship-to_province/state'), - 'shipping_address1' => $this->getString($data, 'ship-to_address_line_1'), - 'shipping_address2' => $this->getString($data, 'ship-to_address_line_2'), - 'shipping_city' => $this->getString($data, 'ship-to_city'), - 'shipping_postal_code' => $this->getString($data, 'ship-to_postal_code/zip_code'), - 'public_notes' => $this->getString($data, 'delivery_instructions'), - - 'credit_balance' => 0, - 'settings' =>$settings, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'first_name' => $this->getString($data, 'contact_first_name'), - 'last_name' => $this->getString($data, 'contact_last_name'), - 'email' => $this->getString($data, 'email'), - 'phone' => $this->getString($data, 'phone'), - ], - ], - ]; - } -} diff --git a/app/Import/Transformers/Waveaccounting/InvoiceTransformer.php b/app/Import/Transformers/Waveaccounting/InvoiceTransformer.php deleted file mode 100644 index 5f3b6d91ceaf..000000000000 --- a/app/Import/Transformers/Waveaccounting/InvoiceTransformer.php +++ /dev/null @@ -1,83 +0,0 @@ -hasInvoice($invoice_data['Invoice Number'])) { - throw new ImportException('Invoice number already exists'); - } - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'client_id' => $this->getClient($customer_name = $this->getString($invoice_data, 'Customer'), 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' => isset( $invoice_data['Invoice Date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['Transaction Date'] ) ) : null, - 'currency_id' => $this->getCurrencyByCode($invoice_data, 'Currency'), - 'status_id' => Invoice::STATUS_SENT, - ]; - - $line_items = []; - $payments = []; - foreach ($line_items_data as $record) { - if ($record['Account Type'] === 'Income') { - $description = $this->getString($record, 'Transaction Line Description'); - - // Remove duplicate data from description - if (substr($description, 0, strlen($customer_name) + 3) === $customer_name.' - ') { - $description = substr($description, strlen($customer_name) + 3); - } - - if (substr($description, 0, strlen($invoice_number) + 3) === $invoice_number.' - ') { - $description = substr($description, strlen($invoice_number) + 3); - } - - $line_items[] = [ - 'notes' => $description, - 'cost' => $this->getFloat($record, 'Amount Before Sales Tax'), - 'tax_name1' => $this->getString($record, 'Sales Tax Name'), - 'tax_rate1' => $this->getFloat($record, 'Sales Tax Amount'), - - 'quantity' => 1, - ]; - } elseif ($record['Account Type'] === 'System Receivable Invoice') { - // This is a payment - $payments[] = [ - 'date' => date('Y-m-d', strtotime($invoice_data['Transaction Date'])), - 'amount' => $this->getFloat($record, 'Amount (One column)'), - ]; - } - } - - $transformed['line_items'] = $line_items; - $transformed['payments'] = $payments; - - return $transformed; - } -} diff --git a/app/Import/Transformers/Zoho/ClientTransformer.php b/app/Import/Transformers/Zoho/ClientTransformer.php deleted file mode 100644 index 6895fabdaeb2..000000000000 --- a/app/Import/Transformers/Zoho/ClientTransformer.php +++ /dev/null @@ -1,74 +0,0 @@ -hasClient($data['Company Name'])) { - throw new ImportException('Client already exists'); - } - - $settings = new \stdClass; - $settings->currency_id = (string) $this->getCurrencyByCode($data, 'Currency'); - - if (strval($data['Payment Terms'] ?? '') > 0) { - $settings->payment_terms = $data['Payment Terms']; - } - - return [ - 'company_id' => $this->maps['company']->id, - 'name' => $this->getString($data, 'Company Name'), - 'phone' => $this->getString($data, 'Phone'), - 'private_notes' => $this->getString($data, 'Notes'), - 'website' => $this->getString($data, 'Website'), - 'id_number' => $this->getString($data, 'Customer ID'), - 'address1' => $this->getString($data, 'Billing Address'), - 'address2' => $this->getString($data, 'Billing Street2'), - 'city' => $this->getString($data, 'Billing City'), - 'state' => $this->getString($data, 'Billing State'), - 'postal_code' => $this->getString($data, 'Billing Code'), - 'country_id' => isset($data['Billing Country']) ? $this->getCountryId($data['Billing Country']) : null, - - 'shipping_address1' => $this->getString($data, 'Shipping Address'), - 'shipping_address2' => $this->getString($data, 'Shipping Street2'), - 'shipping_city' => $this->getString($data, 'Shipping City'), - 'shipping_state' => $this->getString($data, 'Shipping State'), - 'shipping_postal_code' => $this->getString($data, 'Shipping Code'), - 'shipping_country_id' => isset($data['Shipping Country']) ? $this->getCountryId($data['Shipping Country']) : null, - 'credit_balance' => 0, - 'settings' => $settings, - 'client_hash' => Str::random(40), - 'contacts' => [ - [ - 'first_name' => $this->getString($data, 'First Name'), - 'last_name' => $this->getString($data, 'Last Name'), - 'email' => $this->getString($data, 'Email'), - 'phone' => $this->getString($data, 'Phone'), - ], - ], - ]; - } -} diff --git a/app/Import/Transformers/Zoho/InvoiceTransformer.php b/app/Import/Transformers/Zoho/InvoiceTransformer.php deleted file mode 100644 index 44eb3a3bbd09..000000000000 --- a/app/Import/Transformers/Zoho/InvoiceTransformer.php +++ /dev/null @@ -1,79 +0,0 @@ -hasInvoice($invoice_data['Invoice Number'])) { - throw new ImportException('Invoice number already exists'); - } - - $invoiceStatusMap = [ - 'sent' => Invoice::STATUS_SENT, - 'draft' => Invoice::STATUS_DRAFT, - ]; - - $transformed = [ - 'company_id' => $this->maps['company']->id, - 'client_id' => $this->getClient($this->getString($invoice_data, 'Customer ID'), null), - 'number' => $this->getString($invoice_data, 'Invoice Number'), - 'date' => isset($invoice_data['Invoice Date']) ? date('Y-m-d', strtotime($invoice_data['Invoice Date'])) : null, - 'due_date' => isset($invoice_data['Due Date']) ? date('Y-m-d', strtotime($invoice_data['Due Date'])) : null, - 'po_number' => $this->getString($invoice_data, 'PurchaseOrder'), - 'public_notes' => $this->getString($invoice_data, 'Notes'), - // 'currency_id' => $this->getCurrencyByCode($invoice_data, 'Currency'), - 'amount' => $this->getFloat($invoice_data, 'Total'), - 'balance' => $this->getFloat($invoice_data, 'Balance'), - 'status_id' => $invoiceStatusMap[$status = - strtolower($this->getString($invoice_data, 'Invoice Status'))] ?? Invoice::STATUS_SENT, - // 'viewed' => $status === 'viewed', - ]; - - $line_items = []; - foreach ($line_items_data as $record) { - $line_items[] = [ - 'product_key' => $this->getString($record, 'Item Name'), - 'notes' => $this->getString($record, 'Item Description'), - 'cost' => round($this->getFloat($record, 'Item Price'), 2), - 'quantity' => $this->getFloat($record, 'Quantity'), - 'discount' => $this->getString($record, 'Discount Amount'), - 'is_amount_discount' => true, - ]; - } - $transformed['line_items'] = $line_items; - - if ($transformed['balance'] < $transformed['amount']) { - $transformed['payments'] = [[ - 'date' => isset($invoice_data['Last Payment Date']) ? date('Y-m-d', strtotime($invoice_data['Invoice Date'])) : date('Y-m-d'), - 'amount' => $transformed['amount'] - $transformed['balance'], - ]]; - } - - return $transformed; - } -} diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php deleted file mode 100644 index 61b66c4c343e..000000000000 --- a/app/Jobs/Import/CSVImport.php +++ /dev/null @@ -1,627 +0,0 @@ -company = $company; - $this->hash = $request['hash']; - $this->import_type = $request['import_type']; - $this->skip_header = $request['skip_header'] ?? null; - $this->column_map = - ! empty($request['column_map']) ? - array_combine(array_keys($request['column_map']), array_column($request['column_map'], 'mapping')) : null; - } - - /** - * Execute the job. - * - * - * @return void - */ - public function handle() - { - MultiDB::setDb($this->company->db); - - Auth::login($this->company->owner(), true); - - auth()->user()->setCompany($this->company); - - $this->buildMaps(); - - nlog('import '.$this->import_type); - foreach (['client', 'product', 'invoice', 'payment', 'vendor', 'expense'] as $entityType) { - $csvData = $this->getCsvData($entityType); - - if (! empty($csvData)) { - $importFunction = 'import'.Str::plural(Str::title($entityType)); - $preTransformFunction = 'preTransform'.Str::title($this->import_type); - - if (method_exists($this, $preTransformFunction)) { - $csvData = $this->$preTransformFunction($csvData, $entityType); - } - - if (empty($csvData)) { - continue; - } - - if (method_exists($this, $importFunction)) { - // If there's an entity-specific import function, use that. - $this->$importFunction($csvData); - } else { - // Otherwise, use the generic import function. - $this->importEntities($csvData, $entityType); - } - } - } - - $data = [ - 'errors' => $this->error_array, - 'company' => $this->company, - ]; - - $nmo = new NinjaMailerObject; - $nmo->mailable = new ImportCompleted($this->company, $data); - $nmo->company = $this->company; - $nmo->settings = $this->company->settings; - $nmo->to_user = $this->company->owner(); - - NinjaMailerJob::dispatch($nmo); - } - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private function preTransformCsv($csvData, $entityType) - { - if (empty($this->column_map[$entityType])) { - return false; - } - - if ($this->skip_header) { - array_shift($csvData); - } - - //sort the array by key - $keys = $this->column_map[$entityType]; - ksort($keys); - - $csvData = array_map(function ($row) use ($keys) { - return array_combine($keys, array_intersect_key($row, $keys)); - }, $csvData); - - if ($entityType === 'invoice') { - $csvData = $this->groupInvoices($csvData, 'invoice.number'); - } - - return $csvData; - } - - private function preTransformFreshbooks($csvData, $entityType) - { - $csvData = $this->mapCSVHeaderToKeys($csvData); - - if ($entityType === 'invoice') { - $csvData = $this->groupInvoices($csvData, 'Invoice #'); - } - - return $csvData; - } - - private function preTransformInvoicely($csvData, $entityType) - { - $csvData = $this->mapCSVHeaderToKeys($csvData); - - return $csvData; - } - - private function preTransformInvoice2go($csvData, $entityType) - { - $csvData = $this->mapCSVHeaderToKeys($csvData); - - return $csvData; - } - - private function preTransformZoho($csvData, $entityType) - { - $csvData = $this->mapCSVHeaderToKeys($csvData); - - if ($entityType === 'invoice') { - $csvData = $this->groupInvoices($csvData, 'Invoice Number'); - } - - return $csvData; - } - - private function preTransformWaveaccounting($csvData, $entityType) - { - $csvData = $this->mapCSVHeaderToKeys($csvData); - - if ($entityType === 'invoice') { - $csvData = $this->groupInvoices($csvData, 'Invoice Number'); - } - - return $csvData; - } - - private function groupInvoices($csvData, $key) - { - // Group by invoice. - $grouped = []; - - foreach ($csvData as $line_item) { - if (empty($line_item[$key])) { - $this->error_array['invoice'][] = ['invoice' => $line_item, 'error' => 'No invoice number']; - } else { - $grouped[$line_item[$key]][] = $line_item; - } - } - - return $grouped; - } - - private function mapCSVHeaderToKeys($csvData) - { - $keys = array_shift($csvData); - - return array_map(function ($values) use ($keys) { - return array_combine($keys, $values); - }, $csvData); - } - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private function importInvoices($invoices) - { - $invoice_transformer = $this->getTransformer('invoice'); - - /** @var PaymentRepository $payment_repository */ - $payment_repository = app()->make(PaymentRepository::class); - $payment_repository->import_mode = true; - - /** @var ClientRepository $client_repository */ - $client_repository = app()->make(ClientRepository::class); - $client_repository->import_mode = true; - - $invoice_repository = new InvoiceRepository(); - $invoice_repository->import_mode = true; - - foreach ($invoices as $raw_invoice) { - try { - $invoice_data = $invoice_transformer->transform($raw_invoice); - - $invoice_data['line_items'] = $this->cleanItems($invoice_data['line_items'] ?? []); - - // If we don't have a client ID, but we do have client data, go ahead and create the client. - if (empty($invoice_data['client_id']) && ! empty($invoice_data['client'])) { - $client_data = $invoice_data['client']; - $client_data['user_id'] = $this->getUserIDForRecord($invoice_data); - - $client_repository->save( - $client_data, - $client = ClientFactory::create($this->company->id, $client_data['user_id']) - ); - $invoice_data['client_id'] = $client->id; - unset($invoice_data['client']); - } - - $validator = Validator::make($invoice_data, ( new StoreInvoiceRequest() )->rules()); - if ($validator->fails()) { - $this->error_array['invoice'][] = - ['invoice' => $invoice_data, 'error' => $validator->errors()->all()]; - } else { - $invoice = InvoiceFactory::create($this->company->id, $this->getUserIDForRecord($invoice_data)); - if (! empty($invoice_data['status_id'])) { - $invoice->status_id = $invoice_data['status_id']; - } - $invoice_repository->save($invoice_data, $invoice); - $this->addInvoiceToMaps($invoice); - - // If we're doing a generic CSV import, only import payment data if we're not importing a payment CSV. - // If we're doing a platform-specific import, trust the platform to only return payment info if there's not a separate payment CSV. - if ($this->import_type !== 'csv' || empty($this->column_map['payment'])) { - // Check for payment columns - if (! empty($invoice_data['payments'])) { - foreach ($invoice_data['payments'] as $payment_data) { - $payment_data['user_id'] = $invoice->user_id; - $payment_data['client_id'] = $invoice->client_id; - $payment_data['invoices'] = [ - [ - 'invoice_id' => $invoice->id, - 'amount' => $payment_data['amount'] ?? null, - ], - ]; - - /* Make sure we don't apply any payments to invoices with a Zero Amount*/ - if ($invoice->amount > 0) { - $payment_repository->save( - $payment_data, - PaymentFactory::create($this->company->id, $invoice->user_id, $invoice->client_id) - ); - } - } - } - } - - $this->actionInvoiceStatus($invoice, $invoice_data, $invoice_repository); - } - } catch (\Exception $ex) { - if ($ex instanceof ImportException) { - $message = $ex->getMessage(); - } else { - report($ex); - $message = 'Unknown error'; - } - - $this->error_array['invoice'][] = ['invoice' => $raw_invoice, 'error' => $message]; - } - } - } - - private function actionInvoiceStatus($invoice, $invoice_data, $invoice_repository) - { - if (! empty($invoice_data['archived'])) { - $invoice_repository->archive($invoice); - $invoice->fresh(); - } - - if (! empty($invoice_data['viewed'])) { - $invoice = $invoice->service()->markViewed()->save(); - } - - if ($invoice->status_id === Invoice::STATUS_DRAFT) { - } elseif ($invoice->status_id === Invoice::STATUS_SENT) { - $invoice = $invoice->service()->markSent()->save(); - } elseif ($invoice->status_id <= Invoice::STATUS_SENT && $invoice->amount > 0) { - if ($invoice->balance <= 0) { - $invoice->status_id = Invoice::STATUS_PAID; - $invoice->save(); - } elseif ($invoice->balance != $invoice->amount) { - $invoice->status_id = Invoice::STATUS_PARTIAL; - $invoice->save(); - } - } - - return $invoice; - } - - private function importEntities($records, $entity_type) - { - $entity_type = Str::slug($entity_type, '_'); - $formatted_entity_type = Str::title($entity_type); - - $request_name = "\\App\\Http\\Requests\\${formatted_entity_type}\\Store${formatted_entity_type}Request"; - $repository_name = '\\App\\Repositories\\'.$formatted_entity_type.'Repository'; - $factoryName = '\\App\\Factory\\'.$formatted_entity_type.'Factory'; - - /** @var BaseRepository $repository */ - $repository = app()->make($repository_name); - $repository->import_mode = true; - - $transformer = $this->getTransformer($entity_type); - - foreach ($records as $record) { - try { - $entity = $transformer->transform($record); - - /** @var \App\Http\Requests\Request $request */ - // $request = new $request_name(); - // $request->prepareForValidation(); - - // Pass entity data to request so it can be validated - // $request->query = $request->request = new ParameterBag( $entity ); - // $validator = Validator::make( $entity, $request->rules() ); - $validator = $request_name::runFormRequest($entity); - - if ($validator->fails()) { - $this->error_array[$entity_type][] = - [$entity_type => $record, 'error' => $validator->errors()->all()]; - } else { - $entity = - $repository->save( - array_diff_key($entity, ['user_id' => false]), - $factoryName::create($this->company->id, $this->getUserIDForRecord($entity))); - - $entity->save(); - if (method_exists($this, 'add'.$formatted_entity_type.'ToMaps')) { - $this->{'add'.$formatted_entity_type.'ToMaps'}($entity); - } - } - } catch (\Exception $ex) { - if ($ex instanceof ImportException) { - $message = $ex->getMessage(); - } else { - report($ex); - $message = 'Unknown error'; - } - - $this->error_array[$entity_type][] = [$entity_type => $record, 'error' => $message]; - } - } - } - - /** - * @param $entity_type - * - * @return BaseTransformer - */ - private function getTransformer($entity_type) - { - $formatted_entity_type = Str::title($entity_type); - $formatted_import_type = Str::title($this->import_type); - $transformer_name = - '\\App\\Import\\Transformers\\'.$formatted_import_type.'\\'.$formatted_entity_type.'Transformer'; - - return new $transformer_name($this->maps); - } - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private function buildMaps() - { - $this->maps = [ - 'company' => $this->company, - 'client' => [], - 'contact' => [], - 'invoice' => [], - 'invoice_client' => [], - 'product' => [], - 'countries' => [], - 'countries2' => [], - 'currencies' => [], - 'client_ids' => [], - 'invoice_ids' => [], - 'vendors' => [], - 'expense_categories' => [], - 'payment_types' => [], - 'tax_rates' => [], - 'tax_names' => [], - ]; - - Client::where('company_id', $this->company->id)->cursor()->each(function ($client) { - $this->addClientToMaps($client); - }); - - ClientContact::where('company_id', $this->company->id)->cursor()->each(function ($contact) { - $this->addContactToMaps($contact); - }); - - Invoice::where('company_id', $this->company->id)->cursor()->each(function ($invoice) { - $this->addInvoiceToMaps($invoice); - }); - - Product::where('company_id', $this->company->id)->cursor()->each(function ($product) { - $this->addProductToMaps($product); - }); - - Project::where('company_id', $this->company->id)->cursor()->each(function ($project) { - $this->addProjectToMaps($project); - }); - - Country::all()->each(function ($country) { - $this->maps['countries'][strtolower($country->name)] = $country->id; - $this->maps['countries2'][strtolower($country->iso_3166_2)] = $country->id; - }); - - Currency::all()->each(function ($currency) { - $this->maps['currencies'][strtolower($currency->code)] = $currency->id; - }); - - PaymentType::all()->each(function ($payment_type) { - $this->maps['payment_types'][strtolower($payment_type->name)] = $payment_type->id; - }); - - Vendor::where('company_id', $this->company->id)->cursor()->each(function ($vendor) { - $this->addVendorToMaps($vendor); - }); - - ExpenseCategory::where('company_id', $this->company->id)->cursor()->each(function ($category) { - $this->addExpenseCategoryToMaps($category); - }); - - TaxRate::where('company_id', $this->company->id)->cursor()->each(function ($taxRate) { - $name = trim(strtolower($taxRate->name)); - $this->maps['tax_rates'][$name] = $taxRate->rate; - $this->maps['tax_names'][$name] = $taxRate->name; - }); - } - - /** - * @param Invoice $invoice - */ - private function addInvoiceToMaps(Invoice $invoice) - { - if ($number = strtolower(trim($invoice->number))) { - $this->maps['invoices'][$number] = $invoice; - $this->maps['invoice'][$number] = $invoice->id; - $this->maps['invoice_client'][$number] = $invoice->client_id; - $this->maps['invoice_ids'][$invoice->public_id] = $invoice->id; - } - } - - /** - * @param Client $client - */ - private function addClientToMaps(Client $client) - { - if ($name = strtolower(trim($client->name))) { - $this->maps['client'][$name] = $client->id; - $this->maps['client_ids'][$client->public_id] = $client->id; - } - if ($client->contacts->count()) { - $contact = $client->contacts[0]; - if ($email = strtolower(trim($contact->email))) { - $this->maps['client'][$email] = $client->id; - } - if ($name = strtolower(trim($contact->first_name.' '.$contact->last_name))) { - $this->maps['client'][$name] = $client->id; - } - $this->maps['client_ids'][$client->public_id] = $client->id; - } - } - - /** - * @param ClientContact $contact - */ - private function addContactToMaps(ClientContact $contact) - { - if ($key = strtolower(trim($contact->email))) { - $this->maps['contact'][$key] = $contact; - } - } - - /** - * @param Product $product - */ - private function addProductToMaps(Product $product) - { - if ($key = strtolower(trim($product->product_key))) { - $this->maps['product'][$key] = $product; - } - } - - /** - * @param Project $project - */ - private function addProjectToMaps(Project $project) - { - if ($key = strtolower(trim($project->name))) { - $this->maps['project'][$key] = $project; - } - } - - private function addVendorToMaps(Vendor $vendor) - { - $this->maps['vendor'][strtolower($vendor->name)] = $vendor->id; - } - - private function addExpenseCategoryToMaps(ExpenseCategory $category) - { - if ($name = strtolower($category->name)) { - $this->maps['expense_category'][$name] = $category->id; - } - } - - private function getUserIDForRecord($record) - { - if (! empty($record['user_id'])) { - return $this->findUser($record['user_id']); - } else { - return $this->company->owner()->id; - } - } - - private function findUser($user_hash) - { - $user = User::where('account_id', $this->company->account->id) - ->where(\DB::raw('CONCAT_WS(" ", first_name, last_name)'), 'like', '%'.$user_hash.'%') - ->first(); - - if ($user) { - return $user->id; - } else { - return $this->company->owner()->id; - } - } - - private function getCsvData($entityType) - { - $base64_encoded_csv = Cache::pull($this->hash.'-'.$entityType); - if (empty($base64_encoded_csv)) { - return null; - } - - $csv = base64_decode($base64_encoded_csv); - $csv = Reader::createFromString($csv); - - $stmt = new Statement(); - $data = iterator_to_array($stmt->process($csv)); - - if (count($data) > 0) { - $headers = $data[0]; - - // Remove Invoice Ninja headers - if (count($headers) && count($data) > 4 && $this->import_type === 'csv') { - $firstCell = $headers[0]; - if (strstr($firstCell, config('ninja.app_name'))) { - array_shift($data); // Invoice Ninja... - array_shift($data); // - array_shift($data); // Enitty Type Header - } - } - } - - return $data; - } -} diff --git a/tests/Feature/Import/CSV/BaseTransformerTest.php b/tests/Feature/Import/CSV/BaseTransformerTest.php index 39bf1effad6d..812d9b543e46 100644 --- a/tests/Feature/Import/CSV/BaseTransformerTest.php +++ b/tests/Feature/Import/CSV/BaseTransformerTest.php @@ -12,7 +12,6 @@ namespace Tests\Feature\Import\CSV; use App\Import\Transformer\BaseTransformer; -use App\Jobs\Import\CSVImport; use App\Models\Client; use App\Models\ClientContact; use App\Models\Expense; @@ -285,35 +284,4 @@ class BaseTransformerTest extends TestCase $this->assertEquals($vendor->id, $base_transformer->getVendorId('Magic')); $this->assertEquals($vendor->id, $base_transformer->getVendorId('Ma gi c ')); } - - // public function testClientCsvImport() - // { - // $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); - // $hash = Str::random(32); - // $column_map = [ - // 1 => 'client.balance', - // 2 => 'client.paid_to_date', - // 0 => 'client.name', - // 19 => 'client.currency_id', - // 20 => 'client.public_notes', - // 21 => 'client.private_notes', - // 22 => 'contact.first_name', - // 23 => 'contact.last_name', - // ]; - - // $data = [ - // 'hash' => $hash, - // 'column_map' => [ 'client' => [ 'mapping' => $column_map ] ], - // 'skip_header' => true, - // 'import_type' => 'csv', - // ]; - - // $pre_import = Client::count(); - - // Cache::put( $hash . '-client', base64_encode( $csv ), 360 ); - - // CSVImport::dispatchNow( $data, $this->company ); - - // $this->assertGreaterThan( $pre_import, Client::count() ); - // } } diff --git a/tests/Feature/Import/CSV/CsvImportTest.php b/tests/Feature/Import/CSV/CsvImportTest.php index 36197053d47b..06e2716ac43c 100644 --- a/tests/Feature/Import/CSV/CsvImportTest.php +++ b/tests/Feature/Import/CSV/CsvImportTest.php @@ -13,7 +13,6 @@ namespace Tests\Feature\Import\CSV; use App\Import\Providers\Csv; use App\Import\Transformer\BaseTransformer; -use App\Jobs\Import\CSVImport; use App\Models\Client; use App\Models\ClientContact; use App\Models\Expense; @@ -319,34 +318,3 @@ class CsvImportTest extends TestCase $this->assertEquals(51.03, round($invoice->payments()->sum('payments.amount'),2)); } } - -// public function testClientCsvImport() -// { -// $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); -// $hash = Str::random(32); -// $column_map = [ -// 1 => 'client.balance', -// 2 => 'client.paid_to_date', -// 0 => 'client.name', -// 19 => 'client.currency_id', -// 20 => 'client.public_notes', -// 21 => 'client.private_notes', -// 22 => 'contact.first_name', -// 23 => 'contact.last_name', -// ]; - -// $data = [ -// 'hash' => $hash, -// 'column_map' => [ 'client' => [ 'mapping' => $column_map ] ], -// 'skip_header' => true, -// 'import_type' => 'csv', -// ]; - -// $pre_import = Client::count(); - -// Cache::put( $hash . '-client', base64_encode( $csv ), 360 ); - -// CSVImport::dispatchNow( $data, $this->company ); - -// $this->assertGreaterThan( $pre_import, Client::count() ); -// } diff --git a/tests/Feature/Import/ImportCompanyTest.php b/tests/Feature/Import/ImportCompanyTest.php index d7e6d3159bad..7860cd063ca9 100644 --- a/tests/Feature/Import/ImportCompanyTest.php +++ b/tests/Feature/Import/ImportCompanyTest.php @@ -11,7 +11,6 @@ namespace Tests\Feature\Import; -use App\Jobs\Import\CSVImport; use App\Models\Account; use App\Models\Activity; use App\Models\Backup; diff --git a/tests/Feature/Import/ImportCsvTest.php b/tests/Feature/Import/ImportCsvTest.php deleted file mode 100644 index d234c15dd829..000000000000 --- a/tests/Feature/Import/ImportCsvTest.php +++ /dev/null @@ -1,358 +0,0 @@ -withoutMiddleware( - ThrottleRequests::class - ); - config(['database.default' => config('ninja.db.default')]); - - // $this->faker = \Faker\Factory::create(); - - $this->makeTestData(); - - $this->markTestSkipped('Skipping CSV Import to improve test speed'); - - $this->withoutExceptionHandling(); - } - - public function testCsvRead() - { - $csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv'); - - $this->assertTrue(is_array($this->getCsvData($csv))); - } - - public function testClientCsvImport() - { - $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); - $hash = Str::random(32); - $column_map = [ - 1 => 'client.balance', - 2 => 'client.paid_to_date', - 0 => 'client.name', - 19 => 'client.currency_id', - 20 => 'client.public_notes', - 21 => 'client.private_notes', - 22 => 'contact.first_name', - 23 => 'contact.last_name', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['client' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Client::count(); - - Cache::put($hash.'-client', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Client::count()); - } - - public function testInvoiceCsvImport() - { - /*Need to import clients first*/ - $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); - $hash = Str::random(32); - $column_map = [ - 1 => 'client.balance', - 2 => 'client.paid_to_date', - 0 => 'client.name', - 19 => 'client.currency_id', - 20 => 'client.public_notes', - 21 => 'client.private_notes', - 22 => 'contact.first_name', - 23 => 'contact.last_name', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['client' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - Cache::put($hash.'-client', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - /*Now import invoices*/ - $csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv'); - $hash = Str::random(32); - - $column_map = [ - 1 => 'client.email', - 3 => 'payment.amount', - 5 => 'invoice.po_number', - 8 => 'invoice.due_date', - 9 => 'item.discount', - 11 => 'invoice.partial_due_date', - 12 => 'invoice.public_notes', - 13 => 'invoice.private_notes', - 0 => 'client.name', - 2 => 'invoice.number', - 7 => 'invoice.date', - 14 => 'item.product_key', - 15 => 'item.notes', - 16 => 'item.cost', - 17 => 'item.quantity', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['invoice' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Invoice::count(); - - Cache::put($hash.'-invoice', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Invoice::count()); - } - - public function testVendorCsvImport() - { - $csv = file_get_contents(base_path().'/tests/Feature/Import/vendors.csv'); - $hash = Str::random(32); - $column_map = [ - 0 => 'vendor.name', - 19 => 'vendor.currency_id', - 20 => 'vendor.public_notes', - 21 => 'vendor.private_notes', - 22 => 'vendor.first_name', - 23 => 'vendor.last_name', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['vendor' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Vendor::count(); - - Cache::put($hash.'-vendor', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Vendor::count()); - } - - public function testProductCsvImport() - { - $csv = file_get_contents(base_path().'/tests/Feature/Import/products.csv'); - $hash = Str::random(32); - - $column_map = [ - 2 => 'product.notes', - 3 => 'product.cost', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['product' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Product::count(); - - Cache::put($hash.'-product', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Product::count()); - } - - public function testExpenseCsvImport() - { - $csv = file_get_contents(base_path().'/tests/Feature/Import/expenses.csv'); - $hash = Str::random(32); - - $column_map = [ - 2 => 'expense.public_notes', - 3 => 'expense.amount', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['expense' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Expense::count(); - - Cache::put($hash.'-expense', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Expense::count()); - } - - public function testPaymentCsvImport() - { - -/*Need to import clients first*/ - $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv'); - $hash = Str::random(32); - $column_map = [ - 1 => 'client.balance', - 2 => 'client.paid_to_date', - 0 => 'client.name', - 19 => 'client.currency_id', - 20 => 'client.public_notes', - 21 => 'client.private_notes', - 22 => 'contact.first_name', - 23 => 'contact.last_name', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['client' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - Cache::put($hash.'-client', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - /*Now import invoices*/ - $csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv'); - $hash = Str::random(32); - - $column_map = [ - 1 => 'client.email', - 3 => 'payment.amount', - 5 => 'invoice.po_number', - 8 => 'invoice.due_date', - 9 => 'item.discount', - 11 => 'invoice.partial_due_date', - 12 => 'invoice.public_notes', - 13 => 'invoice.private_notes', - 0 => 'client.name', - 2 => 'invoice.number', - 7 => 'invoice.date', - 14 => 'item.product_key', - 15 => 'item.notes', - 16 => 'item.cost', - 17 => 'item.quantity', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['invoice' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Invoice::count(); - - Cache::put($hash.'-invoice', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - /* Test Now import payments*/ - - $csv = file_get_contents(base_path().'/tests/Feature/Import/payments.csv'); - $hash = Str::random(32); - - $column_map = [ - 0 => 'payment.client_id', - 1 => 'payment.invoice_number', - 2 => 'payment.amount', - 3 => 'payment.date', - ]; - - $data = [ - 'hash' => $hash, - 'column_map' => ['payment' => ['mapping' => $column_map]], - 'skip_header' => true, - 'import_type' => 'csv', - ]; - - $pre_import = Payment::count(); - - Cache::put($hash.'-payment', base64_encode($csv), 360); - - CSVImport::dispatchSync($data, $this->company); - - $this->assertGreaterThan($pre_import, Payment::count()); - } - - private function getCsvData($csvfile) - { - if (! ini_get('auto_detect_line_endings')) { - ini_set('auto_detect_line_endings', '1'); - } - - $csv = Reader::createFromString($csvfile); - $stmt = new Statement(); - $data = iterator_to_array($stmt->process($csv)); - - if (count($data) > 0) { - $headers = $data[0]; - - // Remove Invoice Ninja headers - if (count($headers) && count($data) > 4) { - $firstCell = $headers[0]; - if (strstr($firstCell, config('ninja.app_name'))) { - array_shift($data); // Invoice Ninja... - array_shift($data); // - array_shift($data); // Enitty Type Header - } - } - } - - return $data; - } -}