diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index a52b657b1f72..b9c25da20741 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -200,10 +200,6 @@ class BaseExport 'amount' => 'purchase_order.amount', 'balance' => 'purchase_order.balance', '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_value2' => 'purchase_order.custom_value2', 'custom_value3' => 'purchase_order.custom_value3', diff --git a/app/Export/CSV/InvoiceItemExport.php b/app/Export/CSV/InvoiceItemExport.php index 33fb942c846e..d7db46889269 100644 --- a/app/Export/CSV/InvoiceItemExport.php +++ b/app/Export/CSV/InvoiceItemExport.php @@ -12,7 +12,6 @@ namespace App\Export\CSV; use App\Libraries\MultiDB; -use App\Models\Client; use App\Models\Company; use App\Models\Invoice; use App\Transformers\InvoiceTransformer; @@ -126,10 +125,6 @@ class InvoiceItemExport extends BaseExport $key = str_replace("item.", "", $key); - // $keyval = $key; - - // $key = str_replace("custom_value", "invoice", $key); - if($key == 'type_id') $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); $entity = $this->decorateAdvancedFields($invoice, $transformed_items); $this->storage_array[] = $entity; - // $this->csv->insertOne($entity); } } @@ -174,15 +155,7 @@ nlog($item_array); $entity = []; 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); if(is_array($parts) && $parts[0] == 'item') @@ -193,10 +166,7 @@ nlog($item_array); }else if (array_key_exists($key, $transformed_invoice)) { $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); } } diff --git a/app/Export/CSV/QuoteItemExport.php b/app/Export/CSV/QuoteItemExport.php index 9af20df7b5ca..a2f78d16ce41 100644 --- a/app/Export/CSV/QuoteItemExport.php +++ b/app/Export/CSV/QuoteItemExport.php @@ -16,6 +16,7 @@ use App\Models\Company; use App\Models\Quote; use App\Transformers\QuoteTransformer; use App\Utils\Ninja; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Support\Facades\App; use League\Csv\Writer; @@ -28,63 +29,7 @@ class QuoteItemExport extends BaseExport public Writer $csv; - public array $entity_keys = [ - '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 $storage_array; private array $decorate_keys = [ 'client', @@ -98,37 +43,70 @@ class QuoteItemExport extends BaseExport $this->quote_transformer = new QuoteTransformer(); } - public function run() + public function init(): Builder { + MultiDB::setDb($this->company->db); App::forgetInstance('translator'); App::setLocale($this->company->locale()); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->company->settings)); + + if (count($this->input['report_keys']) == 0) { + $this->input['report_keys'] = array_values($this->mergeItemsKeys('quote_report_keys')); + } + $query = Quote::query() + ->withTrashed() + ->with('client')->where('company_id', $this->company->id) + ->where('is_deleted', 0); + + $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(); - if (count($this->input['report_keys']) == 0) { - $this->input['report_keys'] = array_values($this->entity_keys); - } + $query = $this->init(); //insert the header $this->csv->insertOne($this->buildHeader()); - $query = Quote::query() - ->withTrashed() - ->with('client')->where('company_id', $this->company->id) - ->where('is_deleted', 0); - - $query = $this->addDateRange($query); - + $query->cursor() ->each(function ($quote) { $this->iterateItems($quote); }); + $this->csv->insertAll($this->storage_array); + return $this->csv->toString(); + } private function iterateItems(Quote $quote) @@ -137,51 +115,34 @@ class QuoteItemExport extends BaseExport $transformed_items = []; - $transformed_items = []; - foreach ($quote->line_items as $item) { $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.")) { $key = str_replace("item.", "", $key); - $keyval = $key; - - $keyval = str_replace("custom_value", "quote", $key); - if($key == 'type_id') - $keyval = 'type'; + $key = 'type'; if($key == 'tax_id') - $keyval = 'tax_category'; + $key = 'tax_category'; if (property_exists($item, $key)) { - $item_array[$keyval] = $item->{$key}; - } else { - $item_array[$keyval] = ''; + $item_array[$key] = $item->{$key}; + } + 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); $entity = $this->decorateAdvancedFields($quote, $transformed_items); - $this->csv->insertOne($entity); + $this->storage_array[] = $entity; } } @@ -192,26 +153,23 @@ class QuoteItemExport extends BaseExport $entity = []; foreach (array_values($this->input['report_keys']) as $key) { - $keyval = array_search($key, $this->entity_keys); + + $parts = explode('.', $key); - if(!$keyval) { - $keyval = array_search(str_replace("quote.", "", $key), $this->entity_keys) ?? $key; + if(is_array($parts) && $parts[0] == 'item') { + continue; } - if(!$keyval) { - $keyval = $key; - } - - 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); + if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_quote)) { + $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); } } + return $this->decorateAdvancedFields($quote, $entity); } private function decorateAdvancedFields(Quote $quote, array $entity) :array diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index 27b4f48041d3..a6868262d10f 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -1082,15 +1082,15 @@ nlog($csv); $this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name')); $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote Number')); - $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity')); - $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost')); - $this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total')); - $this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount')); - $this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes')); - $this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Quantity')); + $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Item Cost')); + $this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Item Line Total')); + $this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Item Discount')); + $this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Item Notes')); + $this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Item Product')); $this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1')); - $this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1')); - $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1')); + $this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Item Tax Name 1')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Tax Rate 1')); $data = [ diff --git a/tests/Feature/Export/ReportPreviewTest.php b/tests/Feature/Export/ReportPreviewTest.php index 33c65b477770..50e7b3e2eb7c 100644 --- a/tests/Feature/Export/ReportPreviewTest.php +++ b/tests/Feature/Export/ReportPreviewTest.php @@ -82,7 +82,7 @@ class ReportPreviewTest extends TestCase $this->assertNotNull($r); - nlog($r); + //nlog($r); } @@ -143,7 +143,7 @@ class ReportPreviewTest extends TestCase $r = Cache::pull('123'); $this->assertNotNull($r); -nlog($r); +//nlog($r); } public function testDocumentJsonExport() @@ -174,7 +174,7 @@ nlog($r); $r = Cache::pull('123'); $this->assertNotNull($r); -nlog($r); +//nlog($r); } public function testClientExportJson()