Product CSV Export

This commit is contained in:
David Bomba 2022-04-28 13:09:17 +10:00
parent b5c0e678cb
commit f7a3afdafa
7 changed files with 308 additions and 7 deletions

View File

@ -86,7 +86,7 @@ class PaymentExport extends BaseExport
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Payment::query()->where('company_id', $this->company->id);
$query = Payment::query()->where('company_id', $this->company->id)->where('is_deleted', 0);
$query = $this->addDateRange($query);

View File

@ -0,0 +1,126 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Document;
use App\Models\Product;
use App\Transformers\ProductTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class ProductExport extends BaseExport
{
private Company $company;
protected array $input;
private $entity_transformer;
protected $date_key = 'created_at';
protected array $entity_keys = [
'project' => 'project_id',
'vendor' => 'vendor_id',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'product_key' => 'product_key',
'notes' => 'notes',
'cost' => 'cost',
'price' => 'price',
'quantity' => 'quantity',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
];
private array $decorate_keys = [
'vendor',
'project',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->entity_transformer = new ProductTransformer();
}
public function run()
{
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
App::setLocale($this->company->locale());
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
//load the CSV document from a string
$this->csv = Writer::createFromString();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Product::query()->where('company_id', $this->company->id)->where('is_deleted', 0);
$query = $this->addDateRange($query);
$query->cursor()
->each(function ($entity){
$this->csv->insertOne($this->buildRow($entity));
});
return $this->csv->toString();
}
private function buildRow(Product $product) :array
{
$transformed_entity = $this->entity_transformer->transform($product);
$entity = [];
foreach(array_values($this->input['report_keys']) as $key){
$entity[$key] = $transformed_entity[$key];
}
return $this->decorateAdvancedFields($product, $entity);
}
private function decorateAdvancedFields(Product $product, array $entity) :array
{
if(array_key_exists('vendor_id', $entity))
$entity['vendor_id'] = $product->vendor()->exists() ? $product->vendor->name : '';
if(array_key_exists('project_id', $entity))
$entity['project_id'] = $product->project()->exists() ? $product->project->name : '';
return $entity;
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\Reports;
use App\Export\CSV\ProductExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Models\Client;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class ProductReportController extends BaseController
{
use MakesHash;
private string $filename = 'products.csv';
public function __construct()
{
parent::__construct();
}
/**
* @OA\Post(
* path="/api/v1/reports/products",
* operationId="getProductReport",
* tags={"reports"},
* summary="Product reports",
* description="Export product reports",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(ref="#/components/schemas/GenericReportSchema")
* ),
* @OA\Response(
* response=200,
* description="success",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function __invoke(GenericReportRequest $request)
{
// expect a list of visible fields, or use the default
$export = new ProductExport(auth()->user()->company(), $request->all());
$csv = $export->run();
$headers = array(
'Content-Disposition' => 'attachment',
'Content-Type' => 'text/csv',
);
return response()->streamDownload(function () use ($csv) {
echo $csv;
}, $this->filename, $headers);
}
}

View File

@ -63,6 +63,16 @@ class Project extends BaseModel
return $this->belongsTo(Client::class)->withTrashed();
}
public function vendor()
{
return $this->belongsTo(Vendor::class)->withTrashed();
}
public function project()
{
return $this->belongsTo(Project::class)->withTrashed();
}
public function documents()
{
return $this->morphMany(Document::class, 'documentable');

View File

@ -0,0 +1,82 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Notifications\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class WePayFailureNotification extends Notification
{
/**
* Create a new notification instance.
*
* @return void
*/
protected $company_id;
public function __construct($company_id)
{
$this->company_id = $company_id;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return MailMessage
*/
public function toMail($notifiable)
{
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function toSlack($notifiable)
{
(new SlackMessage)
->success()
->from(ctrans('texts.notification_bot'))
->image('https://app.invoiceninja.com/favicon.png')
->content("New WePay ACH Failure from Company ID: ". $this->company_id);
}
}

View File

@ -18,6 +18,7 @@ use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Notifications\Ninja\WePayFailureNotification;
use App\PaymentDrivers\WePayPaymentDriver;
use App\PaymentDrivers\WePay\WePayCommon;
use App\Utils\Traits\MakesHash;
@ -87,11 +88,8 @@ class ACH
$this->wepay_payment_driver->client->company,
);
(new SlackMessage)
->success()
->from(ctrans('texts.notification_bot'))
->image('https://app.invoiceninja.com/favicon.png')
->content("New WePay ACH Failure from Company ID: ". $this->wepay_payment_driver->company_gateway->company->id);
if(config('ninja.notification.slack'))
$this->wepay_payment_driver->company_gateway->company->notification(new WePayFailureNotification($this->wepay_payment_driver->company_gateway->company))->ninja();
throw new PaymentFailed($e->getMessage(), 400);
}

View File

@ -164,7 +164,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
Route::post('reports/quotes', 'Reports\QuoteReportController');
Route::post('reports/recurring_invoices', 'Reports\RecurringInvoiceReportController');
Route::post('reports/payments', 'Reports\PaymentReportController');
Route::post('reports/products', 'Reports\ProductReportController');
Route::get('scheduler', 'SchedulerController@index');
Route::post('support/messages/send', 'Support\Messages\SendingController');