Working on Import / Export

This commit is contained in:
David Bomba 2020-12-14 21:43:07 +11:00
parent c0aa30ab58
commit 64b9f94299
5 changed files with 178 additions and 85 deletions

View File

@ -9,9 +9,10 @@
* @license https://opensource.org/licenses/AAL
*/
namespace App\Export\CSV
namespace App\Export\CSV;
use App\Models\Company;
use Excel;
class InvoiceExport
{
@ -24,15 +25,15 @@ class InvoiceExport
public function export()
{
$fileName = 'test.csv';
// $fileName = 'test.csv';
$data = $this->company->invoices->get();
// $data = $this->company->invoices->get();
return Excel::create($fileName, function ($excel) use ($data) {
$excel->sheet('', function ($sheet) use ($data) {
$sheet->loadView('export', $data);
});
})->download('csv');
// return Excel::create($fileName, function ($excel) use ($data) {
// $excel->sheet('', function ($sheet) use ($data) {
// $sheet->loadView('export', $data);
// });
// })->download('csv');
}
}

View File

@ -13,51 +13,7 @@ namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Import\PreImportRequest;
use App\Import\Definitions
class InvoiceMap
{
public static function importable()
{
return [
0 => 'number',
1 => 'user_id',
2 => 'amount',
3 => 'balance',
4 => 'client_id',
5 => 'status_id',
6 => 'is_deleted',
7 => 'number',
8 => 'discount',
9 => 'po_number',
10 => 'date',
11 => 'due_date',
12 => 'terms',
13 => 'public_notes',
14 => 'private_notes',
15 => 'uses_inclusive_taxes',
16 => 'tax_name1',
17 => 'tax_rate1',
18 => 'tax_name2',
19 => 'tax_rate2',
20 => 'tax_name3',
21 => 'tax_rate3',
22 => 'is_amount_discount',
23 => 'footer',
24 => 'partial',
25 => 'partial_due_date',
26 => 'custom_value1',
27 => 'custom_value2',
28 => 'custom_value3',
29 => 'custom_value4',
30 => 'custom_surcharge1',
31 => 'custom_surcharge2',
32 => 'custom_surcharge3',
33 => 'custom_surcharge4',
34 => 'exchange_rate',
35 => 'line_items',
]\InvoiceMap;
use App\Import\Definitions\Import\ImportMap;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
@ -125,7 +81,7 @@ class ImportController extends Controller
//parse CSV
$csv_array = $this->getCsvData(file_get_contents($request->file('file')->getPathname()));
$data['data'] = [
$data = [
'hash' => $hash,
'available' => InvoiceMap::importable(),
'headers' => array_slice($csv_array, 0, 2)

View File

@ -9,7 +9,7 @@
* @license https://opensource.org/licenses/AAL
*/
namespace App\Import\Definitions
namespace App\Import\Definitions;
class InvoiceMap
{
@ -24,35 +24,75 @@ class InvoiceMap
4 => 'client_id',
5 => 'status_id',
6 => 'is_deleted',
7 => 'number',
8 => 'discount',
9 => 'po_number',
10 => 'date',
11 => 'due_date',
12 => 'terms',
13 => 'public_notes',
14 => 'private_notes',
15 => 'uses_inclusive_taxes',
16 => 'tax_name1',
17 => 'tax_rate1',
18 => 'tax_name2',
19 => 'tax_rate2',
20 => 'tax_name3',
21 => 'tax_rate3',
22 => 'is_amount_discount',
23 => 'footer',
24 => 'partial',
25 => 'partial_due_date',
26 => 'custom_value1',
27 => 'custom_value2',
28 => 'custom_value3',
29 => 'custom_value4',
30 => 'custom_surcharge1',
31 => 'custom_surcharge2',
32 => 'custom_surcharge3',
33 => 'custom_surcharge4',
34 => 'exchange_rate',
35 => 'line_items',
7 => 'discount',
8 => 'po_number',
9 => 'date',
10 => 'due_date',
11 => 'terms',
12 => 'public_notes',
13 => 'private_notes',
14 => 'uses_inclusive_taxes',
15 => 'tax_name1',
16 => 'tax_rate1',
17 => 'tax_name2',
18 => 'tax_rate2',
19 => 'tax_name3',
20 => 'tax_rate3',
21 => 'is_amount_discount',
22 => 'footer',
23 => 'partial',
24 => 'partial_due_date',
25 => 'custom_value1',
26 => 'custom_value2',
27 => 'custom_value3',
28 => 'custom_value4',
29 => 'custom_surcharge1',
30 => 'custom_surcharge2',
31 => 'custom_surcharge3',
32 => 'custom_surcharge4',
33 => 'exchange_rate',
34 => 'line_items',
];
}
public static function import_keys()
{
return [
0 => 'texts.invoice_number',
1 => 'texts.user',
2 => 'texts.amount',
3 => 'texts.balance',
4 => 'texts.client',
5 => 'texts.status',
6 => 'texts.deleted',
7 => 'texts.discount',
8 => 'texts.po_number',
9 => 'texts.date',
10 => 'texts.due_date',
11 => 'texts.terms',
12 => 'texts.public_notes',
13 => 'texts.private_notes',
14 => 'texts.uses_inclusive_taxes',
15 => 'texts.tax_name1',
16 => 'texts.tax_rate',
17 => 'texts.tax_name',
18 => 'texts.tax_rate',
19 => 'texts.tax_name',
20 => 'texts.tax_rate',
21 => 'texts.is_amount_discount',
22 => 'texts.footer',
23 => 'texts.partial',
24 => 'texts.partial_due_date',
25 => 'texts.custom_value1',
26 => 'texts.custom_value2',
27 => 'texts.custom_value3',
28 => 'texts.custom_value4',
29 => 'texts.surcharge',
30 => 'texts.surcharge',
31 => 'texts.surcharge',
32 => 'texts.surcharge',
33 => 'texts.exchange_rate',
34 => 'texts.items',
];
}
}

View File

@ -3318,6 +3318,9 @@ return [
'notification_invoice_reminder2_sent_subject' => 'Reminder 2 for Invoice :invoice was sent to :client',
'notification_invoice_reminder3_sent_subject' => 'Reminder 3 for Invoice :invoice was sent to :client',
'notification_invoice_reminder_endless_sent_subject' => 'Endless reminder for Invoice :invoice was sent to :client',
'assigned_user' => 'Assigned User',
'custom_value3' => 'Custom Value',
'custom_value4' => 'Custom Value',
'inclusive_taxes' => 'Include taxes',
];

View File

@ -0,0 +1,93 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\Export;
use App\Models\Invoice;
use App\Models\Product;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use League\Csv\Reader;
use League\Csv\Statement;
use League\Csv\Writer;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class ExportCsvTest extends TestCase
{
use MakesHash;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
// $this->faker = \Faker\Factory::create();
$this->makeTestData();
$this->withoutExceptionHandling();
}
public function testExportCsv()
{
$csv = Writer::createFromFileObject(new \SplTempFileObject());
$header_invoice = Invoice::take(10)->get()->toArray();
$header_item = $header_invoice[0]['line_items'][0];
unset($header_invoice[0]['line_items']);
$header_invoice_keys = array_keys($header_invoice[0]);
$header_item_keys = array_keys((array)$header_item);
$header_invoice_values = array_values($header_invoice[0]);
$header_item_values = array_values((array)$header_item);
$merged_values = array_merge($header_invoice_values, (array)$header_item_values);
$merged_keys = array_merge($header_invoice_keys, (array)$header_item_keys);
info(print_r( $merged_keys,1));
info(print_r( $merged_values,1));
foreach($merged_keys as &$key) {
$key = ctrans('texts.'.$key);
}
$csv->insertOne($merged_keys);
foreach(Invoice::take(10)->get() as $invoice){
foreach($invoice->line_items as $item) {
unset($invoice->line_items);
$csv->insertOne(array_merge($invoice->toArray(), (array)$item));
}
}
Storage::put(base_path('invy.csv'), $csv->getContent());
}
}