diff --git a/app/Http/Controllers/Support/Messages/SendingController.php b/app/Http/Controllers/Support/Messages/SendingController.php index b5031d667e0c..a1afc2e5b6ae 100644 --- a/app/Http/Controllers/Support/Messages/SendingController.php +++ b/app/Http/Controllers/Support/Messages/SendingController.php @@ -69,8 +69,13 @@ class SendingController extends Controller 'message' => ['required'], ]); + $send_logs = false; + + if($request->has('send_logs')); + $send_logs = $request->input('send_logs'); + Mail::to(config('ninja.contact.ninja_official_contact')) - ->send(new SupportMessageSent($request->message)); + ->send(new SupportMessageSent($request->message, $send_logs)); return response()->json([ 'success' => true diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 3cfeac9aee40..98b3d4de5632 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -37,15 +37,21 @@ class StorePaymentRequest extends Request { $input = $this->all(); + $invoices_total = 0; + $credits_total = 0; + if (isset($input['client_id'])) { $input['client_id'] = $this->decodePrimaryKey($input['client_id']); } - if (isset($input['invoices']) && is_array($input['invoices']) !== false) { + foreach ($input['invoices'] as $key => $value) { $input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']); + $invoices_total += $value['amount']; } + + //if(!isset($input['amount']) || ) } if (isset($input['invoices']) && is_array($input['invoices']) === false) { @@ -55,6 +61,7 @@ class StorePaymentRequest extends Request if (isset($input['credits']) && is_array($input['credits']) !== false) { foreach ($input['credits'] as $key => $value) { $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']); + $credits_total += $value['amount']; } } @@ -62,6 +69,10 @@ class StorePaymentRequest extends Request $input['credits'] = null; } + if(!isset($input['amount'])){ + $input['amount'] = $invoices_total - $credits_total; + } + $this->replace($input); } @@ -72,6 +83,10 @@ class StorePaymentRequest extends Request 'amount' => [new PaymentAmountsBalanceRule(),new ValidCreditsPresentRule()], 'date' => 'required', 'client_id' => 'required', + 'invoices.*.invoice_id' => 'required', + 'invoices.*.amount' => 'required', + 'credits.*.credit_id' => 'required', + 'credits.*.amount' => 'required', 'invoices' => new ValidPayableInvoicesRule(), 'number' => 'nullable', ]; diff --git a/app/Mail/SupportMessageSent.php b/app/Mail/SupportMessageSent.php index 34a17f8c1166..85a299c20025 100644 --- a/app/Mail/SupportMessageSent.php +++ b/app/Mail/SupportMessageSent.php @@ -14,9 +14,12 @@ class SupportMessageSent extends Mailable public $message; - public function __construct($message) + public $send_logs; + + public function __construct($message, $send_logs) { $this->message = $message; + $this->send_logs = $send_logs; } /** @@ -34,7 +37,7 @@ class SupportMessageSent extends Mailable * we are going to bundle system-level info * and last 10 lines of laravel.log file. */ - if (Ninja::isSelfHost()) { + if (Ninja::isSelfHost() && $this->send_logs !== false) { $system_info = Ninja::getDebugInfo(); $log_file = new \SplFileObject(sprintf('%s/laravel.log', base_path('storage/logs'))); diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 9653f7f872bc..fcb218948671 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -946,4 +946,124 @@ class PaymentTest extends TestCase $response->assertStatus(200); } + + + public function testStorePaymentWithNoAmountField() + { + $client = ClientFactory::create($this->company->id, $this->user->id); + $client->save(); + + $this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id + $this->invoice->client_id = $client->id; + $this->invoice->status_id = Invoice::STATUS_SENT; + + $this->invoice->line_items = $this->buildLineItems(); + $this->invoice->uses_inclusive_Taxes = false; + + $this->invoice->save(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + $this->invoice_calc->build(); + + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); + + $data = [ + 'client_id' => $client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'amount' => $this->invoice->amount + ], + ], + 'date' => '2020/12/12', + + ]; + + $response = null; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments?include=invoices', $data); + + } + catch(ValidationException $e) { + // \Log::error('in the validator'); + $message = json_decode($e->validator->getMessageBag(),1); + // \Log::error($message); + $this->assertNotNull($message); + + } + + if($response){ + $arr = $response->json(); + $response->assertStatus(200); + + $payment_id = $arr['data']['id']; + + $this->assertEquals($this->invoice->amount, $arr['data']['amount']); + + $payment = Payment::find($this->decodePrimaryKey($payment_id))->first(); + + $this->assertNotNull($payment); + $this->assertNotNull($payment->invoices()); + $this->assertEquals(1, $payment->invoices()->count()); + } + + } + + + public function testStorePaymentWithZeroAmountField() + { + $client = ClientFactory::create($this->company->id, $this->user->id); + $client->save(); + + $this->invoice = InvoiceFactory::create($this->company->id,$this->user->id);//stub the company and user_id + $this->invoice->client_id = $client->id; + $this->invoice->status_id = Invoice::STATUS_SENT; + + $this->invoice->line_items = $this->buildLineItems(); + $this->invoice->uses_inclusive_Taxes = false; + + $this->invoice->save(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + $this->invoice_calc->build(); + + $this->invoice = $this->invoice_calc->getInvoice(); + $this->invoice->save(); + + $data = [ + 'amount' => 0, + 'client_id' => $client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'amount' => $this->invoice->amount + ], + ], + 'date' => '2020/12/12', + + ]; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments?include=invoices', $data); + + } + catch(ValidationException $e) { + // \Log::error('in the validator'); + $message = json_decode($e->validator->getMessageBag(),1); + // \Log::error($message); + $this->assertNotNull($message); + + } + + + } + }