Fixes for Quote Item Exports

This commit is contained in:
David Bomba 2023-08-29 00:04:32 +10:00
parent 659b6b8c8f
commit e168f5725e
5 changed files with 80 additions and 156 deletions

View File

@ -200,10 +200,6 @@ class BaseExport
'amount' => 'purchase_order.amount', 'amount' => 'purchase_order.amount',
'balance' => 'purchase_order.balance', 'balance' => 'purchase_order.balance',
'vendor' => 'purchase_order.vendor_id', 'vendor' => 'purchase_order.vendor_id',
// 'custom_surcharge1' => 'purchase_order.custom_surcharge1',
// 'custom_surcharge2' => 'purchase_order.custom_surcharge2',
// 'custom_surcharge3' => 'purchase_order.custom_surcharge3',
// 'custom_surcharge4' => 'purchase_order.custom_surcharge4',
'custom_value1' => 'purchase_order.custom_value1', 'custom_value1' => 'purchase_order.custom_value1',
'custom_value2' => 'purchase_order.custom_value2', 'custom_value2' => 'purchase_order.custom_value2',
'custom_value3' => 'purchase_order.custom_value3', 'custom_value3' => 'purchase_order.custom_value3',

View File

@ -12,7 +12,6 @@
namespace App\Export\CSV; namespace App\Export\CSV;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\Invoice; use App\Models\Invoice;
use App\Transformers\InvoiceTransformer; use App\Transformers\InvoiceTransformer;
@ -126,10 +125,6 @@ class InvoiceItemExport extends BaseExport
$key = str_replace("item.", "", $key); $key = str_replace("item.", "", $key);
// $keyval = $key;
// $key = str_replace("custom_value", "invoice", $key);
if($key == 'type_id') if($key == 'type_id')
$key = 'type'; $key = 'type';
@ -144,26 +139,12 @@ class InvoiceItemExport extends BaseExport
} }
} }
} }
nlog($item_array);
// $entity = [];
// foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only
// // $keyval = array_search($key, $this->entity_keys);
// $key = str_replace("item.", "", $key);
// if (array_key_exists($key, $transformed_items)) {
// $entity[$key] = $transformed_items[$key];
// } else {
// $entity[$key] = "";
// }
// }
$transformed_items = array_merge($transformed_invoice, $item_array); $transformed_items = array_merge($transformed_invoice, $item_array);
$entity = $this->decorateAdvancedFields($invoice, $transformed_items); $entity = $this->decorateAdvancedFields($invoice, $transformed_items);
$this->storage_array[] = $entity; $this->storage_array[] = $entity;
// $this->csv->insertOne($entity);
} }
} }
@ -174,15 +155,7 @@ nlog($item_array);
$entity = []; $entity = [];
foreach (array_values($this->input['report_keys']) as $key) { foreach (array_values($this->input['report_keys']) as $key) {
// $keyval = array_search($key, $this->entity_keys);
// if(!$keyval) {
// $keyval = array_search(str_replace("invoice.", "", $key), $this->entity_keys) ?? $key;
// }
// if(!$keyval) {
// $keyval = $key;
// }
$parts = explode('.', $key); $parts = explode('.', $key);
if(is_array($parts) && $parts[0] == 'item') if(is_array($parts) && $parts[0] == 'item')
@ -193,9 +166,6 @@ nlog($item_array);
}else if (array_key_exists($key, $transformed_invoice)) { }else if (array_key_exists($key, $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$key]; $entity[$key] = $transformed_invoice[$key];
} }
// elseif (array_key_exists($keyval, $transformed_invoice)) {
// $entity[$keyval] = $transformed_invoice[$keyval];
// }
else { else {
$entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer); $entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
} }

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\Quote; use App\Models\Quote;
use App\Transformers\QuoteTransformer; use App\Transformers\QuoteTransformer;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use League\Csv\Writer; use League\Csv\Writer;
@ -28,63 +29,7 @@ class QuoteItemExport extends BaseExport
public Writer $csv; public Writer $csv;
public array $entity_keys = [ private array $storage_array;
'amount' => 'amount',
'balance' => 'balance',
'client' => 'client_id',
'custom_surcharge1' => 'custom_surcharge1',
'custom_surcharge2' => 'custom_surcharge2',
'custom_surcharge3' => 'custom_surcharge3',
'custom_surcharge4' => 'custom_surcharge4',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'date' => 'date',
'discount' => 'discount',
'due_date' => 'due_date',
'exchange_rate' => 'exchange_rate',
'footer' => 'footer',
'number' => 'number',
'paid_to_date' => 'paid_to_date',
'partial' => 'partial',
'partial_due_date' => 'partial_due_date',
'po_number' => 'po_number',
'private_notes' => 'private_notes',
'public_notes' => 'public_notes',
'status' => 'status_id',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'terms' => 'terms',
'total_taxes' => 'total_taxes',
'currency' => 'currency_id',
'quantity' => 'item.quantity',
'cost' => 'item.cost',
'product_key' => 'item.product_key',
'buy_price' => 'item.product_cost',
'cost' => 'item.cost',
'notes' => 'item.notes',
'discount' => 'item.discount',
'is_amount_discount' => 'item.is_amount_discount',
'tax_rate1' => 'item.tax_rate1',
'tax_rate2' => 'item.tax_rate2',
'tax_rate3' => 'item.tax_rate3',
'tax_name1' => 'item.tax_name1',
'tax_name2' => 'item.tax_name2',
'tax_name3' => 'item.tax_name3',
'line_total' => 'item.line_total',
'gross_line_total' => 'item.gross_line_total',
'quote1' => 'item.custom_value1',
'quote2' => 'item.custom_value2',
'quote3' => 'item.custom_value3',
'quote4' => 'item.custom_value4',
'tax_category' => 'item.tax_id',
'type' => 'item.type_id',
];
private array $decorate_keys = [ private array $decorate_keys = [
'client', 'client',
@ -98,24 +43,19 @@ class QuoteItemExport extends BaseExport
$this->quote_transformer = new QuoteTransformer(); $this->quote_transformer = new QuoteTransformer();
} }
public function run() public function init(): Builder
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
App::forgetInstance('translator'); App::forgetInstance('translator');
App::setLocale($this->company->locale()); App::setLocale($this->company->locale());
$t = app('translator'); $t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings)); $t->replace(Ninja::transformTranslations($this->company->settings));
//load the CSV document from a string
$this->csv = Writer::createFromString();
if (count($this->input['report_keys']) == 0) { if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->entity_keys); $this->input['report_keys'] = array_values($this->mergeItemsKeys('quote_report_keys'));
} }
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Quote::query() $query = Quote::query()
->withTrashed() ->withTrashed()
->with('client')->where('company_id', $this->company->id) ->with('client')->where('company_id', $this->company->id)
@ -123,12 +63,50 @@ class QuoteItemExport extends BaseExport
$query = $this->addDateRange($query); $query = $this->addDateRange($query);
return $query;
}
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
})->toArray();
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
});
return array_merge(['columns' => $header], $this->storage_array);
}
public function run()
{
//load the CSV document from a string
$this->csv = Writer::createFromString();
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query->cursor() $query->cursor()
->each(function ($quote) { ->each(function ($quote) {
$this->iterateItems($quote); $this->iterateItems($quote);
}); });
$this->csv->insertAll($this->storage_array);
return $this->csv->toString(); return $this->csv->toString();
} }
private function iterateItems(Quote $quote) private function iterateItems(Quote $quote)
@ -137,51 +115,34 @@ class QuoteItemExport extends BaseExport
$transformed_items = []; $transformed_items = [];
$transformed_items = [];
foreach ($quote->line_items as $item) { foreach ($quote->line_items as $item) {
$item_array = []; $item_array = [];
foreach (array_values($this->input['report_keys']) as $key) { //items iterator produces item array foreach (array_values(array_intersect($this->input['report_keys'], $this->item_report_keys)) as $key) { //items iterator produces item array
if (str_contains($key, "item.")) { if (str_contains($key, "item.")) {
$key = str_replace("item.", "", $key); $key = str_replace("item.", "", $key);
$keyval = $key;
$keyval = str_replace("custom_value", "quote", $key);
if($key == 'type_id') if($key == 'type_id')
$keyval = 'type'; $key = 'type';
if($key == 'tax_id') if($key == 'tax_id')
$keyval = 'tax_category'; $key = 'tax_category';
if (property_exists($item, $key)) { if (property_exists($item, $key)) {
$item_array[$keyval] = $item->{$key}; $item_array[$key] = $item->{$key};
} else {
$item_array[$keyval] = '';
} }
else {
$item_array[$key] = '';
} }
} }
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_items)) {
$entity[$keyval] = $transformed_items[$key];
} else {
$entity[$keyval] = "";
}
} }
$transformed_items = array_merge($transformed_quote, $item_array); $transformed_items = array_merge($transformed_quote, $item_array);
$entity = $this->decorateAdvancedFields($quote, $transformed_items); $entity = $this->decorateAdvancedFields($quote, $transformed_items);
$this->csv->insertOne($entity); $this->storage_array[] = $entity;
} }
} }
@ -192,25 +153,22 @@ class QuoteItemExport extends BaseExport
$entity = []; $entity = [];
foreach (array_values($this->input['report_keys']) as $key) { foreach (array_values($this->input['report_keys']) as $key) {
$keyval = array_search($key, $this->entity_keys);
if(!$keyval) { $parts = explode('.', $key);
$keyval = array_search(str_replace("quote.", "", $key), $this->entity_keys) ?? $key;
if(is_array($parts) && $parts[0] == 'item') {
continue;
} }
if(!$keyval) { if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_quote)) {
$keyval = $key; $entity[$key] = $transformed_quote[$parts[1]];
} elseif (array_key_exists($key, $transformed_quote)) {
$entity[$key] = $transformed_quote[$key];
} else {
$entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer);
}
} }
if (array_key_exists($key, $transformed_quote)) {
$entity[$keyval] = $transformed_quote[$key];
} elseif (array_key_exists($keyval, $transformed_quote)) {
$entity[$keyval] = $transformed_quote[$keyval];
}
else {
$entity[$keyval] = $this->resolveKey($keyval, $quote, $this->quote_transformer);
}
}
return $this->decorateAdvancedFields($quote, $entity); return $this->decorateAdvancedFields($quote, $entity);
} }

View File

@ -1082,15 +1082,15 @@ nlog($csv);
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name')); $this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote Number')); $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote Number'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity')); $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost')); $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Item Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total')); $this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Item Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount')); $this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Item Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes')); $this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Item Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product')); $this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Item Product'));
$this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1')); $this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1')); $this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Item Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1')); $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Tax Rate 1'));
$data = [ $data = [

View File

@ -82,7 +82,7 @@ class ReportPreviewTest extends TestCase
$this->assertNotNull($r); $this->assertNotNull($r);
nlog($r); //nlog($r);
} }
@ -143,7 +143,7 @@ class ReportPreviewTest extends TestCase
$r = Cache::pull('123'); $r = Cache::pull('123');
$this->assertNotNull($r); $this->assertNotNull($r);
nlog($r); //nlog($r);
} }
public function testDocumentJsonExport() public function testDocumentJsonExport()
@ -174,7 +174,7 @@ nlog($r);
$r = Cache::pull('123'); $r = Cache::pull('123');
$this->assertNotNull($r); $this->assertNotNull($r);
nlog($r); //nlog($r);
} }
public function testClientExportJson() public function testClientExportJson()