Fixes for credits

This commit is contained in:
David Bomba 2022-05-10 14:25:16 +10:00
parent 9c359bc3b4
commit e0e53af87f
4 changed files with 119 additions and 50 deletions

View File

@ -81,6 +81,8 @@ class BaseExport
$header[] = ctrans("texts.{$key}");
}
nlog($header);
return $header;
}
}

View File

@ -68,45 +68,6 @@ class CreditExport extends BaseExport
'currency' => 'currency'
];
protected array $all_keys = [
'amount',
'balance',
'client_id',
'custom_surcharge1',
'custom_surcharge2',
'custom_surcharge3',
'custom_surcharge4',
'country_id',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'date',
'discount',
'due_date',
'exchange_rate',
'footer',
'invoice_id',
'number',
'paid_to_date',
'partial',
'partial_due_date',
'po_number',
'private_notes',
'public_notes',
'status_id',
'tax_name1',
'tax_name2',
'tax_name3',
'tax_rate1',
'tax_rate2',
'tax_rate3',
'terms',
'total_taxes',
'currency'
];
private array $decorate_keys = [
'country',
'client',
@ -133,12 +94,12 @@ class CreditExport extends BaseExport
//load the CSV document from a string
$this->csv = Writer::createFromString();
if(count($this->input['report_keys']) == 0)
$this->input['report_keys'] = array_values($this->entity_keys);
//insert the header
$this->csv->insertOne($this->buildHeader());
if(count($this->input['report_keys']) == 0)
$this->input['report_keys'] = $this->all_keys;
$query = Credit::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
@ -166,7 +127,13 @@ class CreditExport extends BaseExport
foreach(array_values($this->input['report_keys']) as $key){
$entity[$key] = $transformed_credit[$key];
$keyval = array_search($key, $this->entity_keys);
if(array_key_exists($key, $transformed_credit))
$entity[$keyval] = $transformed_credit[$key];
else
$entity[$keyval] = '';
}
return $this->decorateAdvancedFields($credit, $entity);
@ -176,17 +143,20 @@ class CreditExport extends BaseExport
private function decorateAdvancedFields(Credit $credit, array $entity) :array
{
if(array_key_exists('country_id', $entity))
$entity['country_id'] = $credit->client->country ? ctrans("texts.country_{$credit->client->country->name}") : "";
if(in_array('country_id', $this->input['report_keys']))
$entity['country'] = $credit->client->country ? ctrans("texts.country_{$credit->client->country->name}") : "";
if(array_key_exists('currency', $entity))
if(in_array('currency', $this->input['report_keys']))
$entity['currency'] = $credit->client->currency()->code;
if(array_key_exists('invoice_id', $entity))
$entity['invoice_id'] = $credit->invoice ? $credit->invoice->number : "";
if(in_array('invoice_id', $this->input['report_keys']))
$entity['invoice'] = $credit->invoice ? $credit->invoice->number : "";
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $credit->client->present()->name();
if(in_array('client_id', $this->input['report_keys']))
$entity['client'] = $credit->client->present()->name();
if(in_array('status_id',$this->input['report_keys']))
$entity['status'] = $credit->stringStatus($credit->status_id);
return $entity;
}

View File

@ -319,4 +319,25 @@ class Credit extends BaseModel
{
return ctrans('texts.credit');
}
public static function stringStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
return ctrans('texts.draft');
break;
case self::STATUS_SENT:
return ctrans('texts.sent');
break;
case self::STATUS_PARTIAL:
return ctrans('texts.partial');
break;
case self::STATUS_APPLIED:
return ctrans('texts.applied');
break;
default:
return "";
break;
}
}
}

View File

@ -0,0 +1,76 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\Export;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Storage;
use League\Csv\Writer;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class ClientCsvTest extends TestCase
{
use MakesHash;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
$this->makeTestData();
$this->withoutExceptionHandling();
}
public function testClientExportCsv()
{
$data = [
"date_range" => "this_year",
"report_keys" => [],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/clients' , $data);
$response->assertStatus(200);
}
public function testContactExportCsv()
{
$data = [
"date_range" => "this_year",
"report_keys" => [],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/contacts' , $data);
$response->assertStatus(200);
}
}