diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 3fe5e1b071bc..427e235b67df 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -31,6 +31,7 @@ class Expense extends EntityModel 'client_id', 'vendor_id', 'expense_currency_id', + 'expense_date', 'invoice_currency_id', 'amount', 'foreign_amount', @@ -46,6 +47,29 @@ class Expense extends EntityModel 'tax_name2', ]; + public static function getImportColumns() + { + return [ + 'client', + 'vendor', + 'amount', + 'public_notes', + 'category', + 'expense_date', + ]; + } + + public static function getImportMap() + { + return [ + 'amount|total' => 'amount', + 'category' => 'category', + 'client' => 'client', + 'vendor' => 'vendor', + 'notes|details' => 'public_notes', + 'date' => 'expense_date', + ]; + } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Ninja/Import/CSV/ExpenseTransformer.php b/app/Ninja/Import/CSV/ExpenseTransformer.php new file mode 100644 index 000000000000..62b55c4189ff --- /dev/null +++ b/app/Ninja/Import/CSV/ExpenseTransformer.php @@ -0,0 +1,49 @@ + isset($data->amount) ? (float) $data->amount : null, + 'vendor_id' => isset($data->vendor) ? $this->getVendorId($data->vendor) : null, + 'expense_date' => isset($data->expense_date) ? date('Y-m-d', strtotime($data->expense_date)) : null, + 'public_notes' => $this->getString($data, 'public_notes'), + ]; + }); + + /* + return new Item($data, function ($data) { + return [ + 'client_id' => $this->getClientId($data->name), + 'invoice_number' => isset($data->invoice_number) ? $this->getInvoiceNumber($data->invoice_number) : null, + 'paid' => isset($data->paid) ? (float) $data->paid : null, + 'po_number' => $this->getString($data, 'po_number'), + 'terms' => $this->getString($data, 'terms'), + 'public_notes' => $this->getString($data, 'public_notes'), + 'invoice_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null, + 'invoice_items' => [ + [ + 'product_key' => '', + 'notes' => $this->getString($data, 'notes'), + 'cost' => isset($data->amount) ? (float) $data->amount : null, + 'qty' => 1, + ] + ], + ]; + }); + */ + + } +} diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 67ec500d6620..b62a209541cb 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -15,9 +15,12 @@ use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\ProductRepository; +use App\Ninja\Repositories\ExpenseRepository; +use App\Ninja\Repositories\VendorRepository; use App\Ninja\Serializers\ArraySerializer; use App\Models\Client; use App\Models\Invoice; +use App\Models\Expense; use App\Models\EntityModel; /** @@ -110,7 +113,9 @@ class ImportService InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ContactRepository $contactRepo, - ProductRepository $productRepo + ProductRepository $productRepo, + ExpenseRepository $expenseRepo, + VendorRepository $vendorRepo ) { $this->fractal = $manager; @@ -121,6 +126,8 @@ class ImportService $this->paymentRepo = $paymentRepo; $this->contactRepo = $contactRepo; $this->productRepo = $productRepo; + $this->expenseRepo = $expenseRepo; + $this->vendorRepo = $vendorRepo; } /** @@ -462,12 +469,11 @@ class ImportService $title = strtolower($headers[$i]); $mapped[$i] = ''; - if ($hasHeaders) { - foreach ($map as $search => $column) { - if ($this->checkForMatch($title, $search)) { - $mapped[$i] = $column; - break; - } + foreach ($map as $search => $column) { + if ($this->checkForMatch($title, $search)) { + $hasHeaders = true; + $mapped[$i] = $column; + break; } } } @@ -668,6 +674,8 @@ class ImportService 'currencies' => [], 'client_ids' => [], 'invoice_ids' => [], + 'vendors' => [], + 'expense_categories' => [], ]; $clients = $this->clientRepo->all(); @@ -695,6 +703,11 @@ class ImportService foreach ($currencies as $currency) { $this->maps['currencies'][strtolower($currency->code)] = $currency->id; } + + $vendors = $this->vendorRepo->all(); + foreach ($vendors as $vendor) { + $this->maps['vendor'][strtolower($vendor->name)] = $vendor->id; + } } /** @@ -729,4 +742,9 @@ class ImportService $this->maps['product'][$key] = $product->id; } } + + private function addExpenseToMaps(Expense $expense) + { + // do nothing + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 538f2dd993f8..06254428f8c4 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2142,7 +2142,10 @@ $LANG = array( 'enable_recurring' => 'Enable Recurring', 'disable_recurring' => 'Disable Recurring', 'text' => 'Text', - + 'expense_will_create' => 'expense will be created', + 'expenses_will_create' => 'expenses will be created', + 'created_expenses' => 'Successfully created :count expense(s)', + ); return $LANG;