diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index 3b675d14f762..6638a21eec81 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -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); diff --git a/app/Export/CSV/ProductExport.php b/app/Export/CSV/ProductExport.php new file mode 100644 index 000000000000..bf4c6b5cf1cd --- /dev/null +++ b/app/Export/CSV/ProductExport.php @@ -0,0 +1,126 @@ + '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; + } + +} diff --git a/app/Http/Controllers/Reports/ProductReportController.php b/app/Http/Controllers/Reports/ProductReportController.php new file mode 100644 index 000000000000..1de907c127e9 --- /dev/null +++ b/app/Http/Controllers/Reports/ProductReportController.php @@ -0,0 +1,85 @@ +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); + + } + + + +} diff --git a/app/Models/Project.php b/app/Models/Project.php index 694a44c8dc89..107ef476a14b 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -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'); diff --git a/app/Notifications/Ninja/WePayFailureNotification.php b/app/Notifications/Ninja/WePayFailureNotification.php new file mode 100644 index 000000000000..08e96cb856c0 --- /dev/null +++ b/app/Notifications/Ninja/WePayFailureNotification.php @@ -0,0 +1,82 @@ +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); + } +} diff --git a/app/PaymentDrivers/WePay/ACH.php b/app/PaymentDrivers/WePay/ACH.php index 2052562db28c..9dfe578fb25c 100644 --- a/app/PaymentDrivers/WePay/ACH.php +++ b/app/PaymentDrivers/WePay/ACH.php @@ -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); } diff --git a/routes/api.php b/routes/api.php index 5681dae0a930..c21c3e9ece27 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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');