Include contacts in sample migration files. (#3260)

* add types to transformers

* minor fixes for test data creator

* Working on refunds

* Update migration files to include client contacts

* Working on refunds

* Working on refunds

* Working on refunds

* Refund Tests

* Working on refund tests
This commit is contained in:
David Bomba 2020-01-29 13:03:47 +11:00 committed by GitHub
parent 401d859f7a
commit 45cc67075d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 775 additions and 218 deletions

View File

@ -674,9 +674,10 @@ class PaymentController extends BaseController
*/
public function refund(RefundPaymentRequest $request)
{
\Log::error("Payment id = ".$request->input('id'));
$payment = Payment::whereId($request->input('id'))->first();
$payment = $request->payment();
$payment->refund($request->all());
return $this->itemResponse($payment);
}

View File

@ -12,6 +12,7 @@
namespace App\Http\Requests\Payment;
use App\Http\Requests\Request;
use App\Http\ValidationRules\Payment\ValidRefundableRequest;
use App\Http\ValidationRules\ValidRefundableInvoices;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
@ -33,13 +34,31 @@ class RefundPaymentRequest extends Request
protected function prepareForValidation()
{
$input = $this->all();
if(!isset($input['gateway_refund']))
$input['gateway_refund'] = false;
if(!isset($input['send_email']))
$input['send_email'] = false;
if(isset($input['id']))
$input['id'] = $this->decodePrimaryKey($input['id']);
if(isset($input['invoices']))
{
foreach($input['invoices'] as $key => $invoice)
$input['invoices'][$key]['invoice_id'] = $this->decodePrimaryKey($invoice['invoice_id']);
}
if(isset($input['credits']))
{
foreach($input['credits'] as $key => $credit)
$input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($credit['credit_id']);
}
$this->replace($input);
}
@ -47,6 +66,7 @@ class RefundPaymentRequest extends Request
{
$rules = [
'id' => 'required',
'id' => new ValidRefundableRequest(),
'refunded' => 'numeric',
'date' => 'required',
'invoices.*.invoice_id' => 'required',
@ -56,4 +76,9 @@ class RefundPaymentRequest extends Request
return $rules;
}
public function payment() :?Payment
{
return Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
}
}

View File

@ -0,0 +1,201 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\ValidationRules\Payment;
use App\Libraries\MultiDB;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Validation\Rule;
/**
* Class ValidRefundableRequest
* @package App\Http\ValidationRules\Payment
*/
class ValidRefundableRequest implements Rule
{
use MakesHash;
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private $error_msg;
public function passes($attribute, $value)
{
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
if(!$payment)
{
$this->error_msg = "Unable to retrieve specified payment";
return false;
}
$request_invoices = request()->has('invoices') ? request()->input('invoices') : [];
$request_credits = request()->has('credits') ? request()->input('credits') : [];
foreach($request_invoices as $key => $value)
$request_invoices[$key]['invoice_id'] = $this->decodePrimaryKey($value['invoice_id']);
foreach($request_credits as $key => $value)
$request_credits[$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
if($payment->invoices()->exists())
{
foreach($payment->invoices as $paymentable_invoice)
$this->checkInvoice($paymentable_invoice, $request_invoices);
}
if($payment->credits()->exists())
{
foreach($payment->credits as $paymentable_credit)
$this->paymentable_type($paymentable_credit, $request_credits);
}
foreach($request_invoices as $request_invoice)
$this->checkInvoiceIsPaymentable($request_invoice, $payment);
foreach($request_credits as $request_credit)
$this->checkCreditIsPaymentable($request_credit, $payment);
return true;
}
private function checkInvoiceIsPaymentable($invoice, $payment)
{
$invoice = Invoice::find($invoice['invoice_id']);
if($payment->invoices()->exists())
{
$paymentable_invoice = $payment->invoices->where('id', $invoice->id)->first();
if(!$paymentable_invoice){
$this->error_msg = "Invoice id ".$invoice->hashed_id." is not related to this payment";
return false;
}
}
else
{
$this->error_msg = "Invoice id ".$invoice->hashed_id." is not related to this payment";
return false;
}
}
private function checkCreditIsPaymentable($credit, $payment)
{
$credit = Credit::find($credit['credit_id']);
if($payment->credits()->exists())
{
$paymentable_credit = $payment->credits->where('id', $credit->id)->first();
if(!$paymentable_invoice){
$this->error_msg = "Credit id ".$credit->hashed_id." is not related to this payment";
return false;
}
}
else
{
$this->error_msg = "Credit id ".$credit->hashed_id." is not related to this payment";
return false;
}
}
private function checkInvoice($paymentable, $request_invoices)
{
$record_found = false;
foreach($request_invoices as $request_invoice)
{
if($request_invoice['invoice_id'] == $paymentable->pivot->paymentable_id)
{
$record_found = true;
$refundable_amount = ($paymentable->pivot->amount - $paymentable->pivot->refunded);
if($request_invoice['amount'] > $refundable_amount){
$invoice = $paymentable->paymentable;
$this->error_msg = "Attempting to refund more than allowed for invoice ".$invoice->number.", maximum refundable amount is ". $refundable_amount;
return false;
}
}
}
if(!$record_found)
{
$this->error_msg = "Attempting to refund a payment with invoices attached, please specify valid invoice/s to be refunded.";
return false;
}
}
private function checkCredit($paymentable, $request_credits)
{
$record_found = null;
foreach($request_credits as $request_credit)
{
if($request_credit['invoice_id'] == $paymentable->pivot->paymentable_id)
{
$record_found = true;
$refundable_amount = ($paymentable->pivot->amount - $paymentable->pivot->refunded);
if($request_invoice['amount'] > $refundable_amount){
$credit = $paymentable->paymentable;
$this->error_msg = "Attempting to refund more than allowed for credit ".$credit->number.", maximum refundable amount is ". $refundable_amount;
return false;
}
}
}
if(!$record_found)
{
$this->error_msg = "Attempting to refund a payment with credits attached, please specify valid credit/s to be refunded.";
return false;
}
}
/**
* @return string
*/
public function message()
{
return $this->error_msg;
}
}

View File

@ -36,7 +36,7 @@ class ValidRefundableInvoices implements Rule
{
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
if($request->has('refunded') && ($request->input('refunded') > $payment->amount)){
if($request->has('refunded') && ($request->input('refunded') > ($payment->amount - $payment->refunded))){
$this->error_msg = "Attempting to refunded more than payment amount, enter a value equal to or lower than the payment amount of ". $payment->amount;
return false;
}
@ -63,6 +63,7 @@ class ValidRefundableInvoices implements Rule
if($val['refunded'] > ($invoice->amount - $invoice->balance))
$this->error_msg = "Attempting to refund more than is possible for an invoice";
return false;
}
}

View File

@ -69,6 +69,8 @@ class UpdateOrCreateProduct implements ShouldQueue
$product->tax_rate1 = isset($item->tax_rate1) ? $item->tax_rate1 : 0 ;
$product->tax_name2 = isset($item->tax_name2) ? $item->tax_name2 : '';
$product->tax_rate2 = isset($item->tax_rate2) ? $item->tax_rate2 : 0;
$product->tax_name3 = isset($item->tax_name3) ? $item->tax_name3 : '';
$product->tax_rate3 = isset($item->tax_rate3) ? $item->tax_rate3 : 0;
$product->custom_value1 = isset($item->custom_value1) ? $item->custom_value1 : '';
$product->custom_value2 = isset($item->custom_value2) ? $item->custom_value2 : '';
$product->custom_value3 = isset($item->custom_value3) ? $item->custom_value3 : '';

View File

@ -12,12 +12,14 @@
namespace App\Models;
use App\Models\BaseModel;
use App\Models\Credit;
use App\Models\DateFormat;
use App\Models\Filterable;
use App\Models\Paymentable;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Payment\Refundable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@ -27,7 +29,8 @@ class Payment extends BaseModel
use Filterable;
use MakesDates;
use SoftDeletes;
use Refundable;
const STATUS_PENDING = 1;
const STATUS_VOIDED = 2;
const STATUS_FAILED = 3;
@ -167,8 +170,11 @@ class Payment extends BaseModel
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
public function refund()
public function refund(array $data) :Payment
{
return $this->processRefund($data);
}
}

View File

@ -30,4 +30,9 @@ class Paymentable extends Pivot
'deleted_at' => 'timestamp',
'settings' => 'object',
];
public function paymentable()
{
return $this->morphTo();
}
}

View File

@ -138,48 +138,52 @@ class PaymentRepository extends BaseRepository
}
/**
* @deprecated Refundable trait replaces this.
*/
private function refundPayment(array $data, Payment $payment): string
{
//temp variable to sum the total refund/credit amount
$invoice_total_adjustment = 0;
// //temp variable to sum the total refund/credit amount
// $invoice_total_adjustment = 0;
if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
// if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
foreach ($data['invoices'] as $adjusted_invoice) {
// foreach ($data['invoices'] as $adjusted_invoice) {
$invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->first();
// $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->first();
$invoice_total_adjustment += $adjusted_invoice['amount'];
// $invoice_total_adjustment += $adjusted_invoice['amount'];
if (array_key_exists('credits', $adjusted_invoice)) {
// if (array_key_exists('credits', $adjusted_invoice)) {
//process and insert credit notes
foreach ($adjusted_invoice['credits'] as $credit) {
// //process and insert credit notes
// foreach ($adjusted_invoice['credits'] as $credit) {
$credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->id, auth()->user()->id), $invoice);
// $credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->id, auth()->user()->id), $invoice);
}
// }
} else {
//todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
}
// } else {
// //todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
// }
}
// }
if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
return 'Amount must equal the sum of invoice adjustments';
}
// if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
// return 'Amount must equal the sum of invoice adjustments';
// }
//adjust applied amount
$payment->applied += $invoice_total_adjustment;
// //adjust applied amount
// $payment->applied += $invoice_total_adjustment;
//adjust clients paid to date
$client = $payment->client;
$client->paid_to_date += $invoice_total_adjustment;
// //adjust clients paid to date
// $client = $payment->client;
// $client->paid_to_date += $invoice_total_adjustment;
// $payment->save();
// $client->save();
$payment->save();
$client->save();
}

View File

@ -0,0 +1,163 @@
<?php
namespace App\Utils\Traits\Payment;
use App\Factory\CreditFactory;
use App\Factory\InvoiceItemFactory;
use App\Models\Activity;
use App\Models\Credit;
use App\Models\Payment;
use App\Repositories\ActivityRepository;
trait Refundable
{
//public function processRefund(array $data)
//{
// if (array_key_exists('invoices', $data) && is_array($data['invoices'])) {
// foreach ($data['invoices'] as $adjusted_invoice) {
// $invoice = Invoice::whereId($adjusted_invoice['invoice_id'])->first();
// $invoice_total_adjustment += $adjusted_invoice['amount'];
// if (array_key_exists('credits', $adjusted_invoice)) {
// //process and insert credit notes
// foreach ($adjusted_invoice['credits'] as $credit) {
// $credit = $this->credit_repo->save($credit, CreditFactory::create(auth()->user()->id, auth()->user()->id), $invoice);
// }
// } else {
// //todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
// }
// }
// if (array_key_exists('amount', $data) && $data['amount'] != $invoice_total_adjustment)
// return 'Amount must equal the sum of invoice adjustments';
// }
// //adjust applied amount
// $payment->applied += $invoice_total_adjustment;
// //adjust clients paid to date
// $client = $payment->client;
// $client->paid_to_date += $invoice_total_adjustment;
// $payment->save();
// $client->save();
//}
public function processRefund(array $data)
{
if(isset($data['invoices']) && isset($data['credits']))
return $this->refundPaymentWithInvoicesAndCredits($data);
else if(isset($data['invoices']))
return $this->refundPaymentWithInvoices($data);
return $this->refundPaymentWithNoInvoicesOrCredits($data);
}
private function refundPaymentWithNoInvoicesOrCredits(array $data)
{
//adjust payment refunded column amount
$this->refunded = $data['refunded'];
if($data['refunded'] == $this->amount)
$this->status_id = Payment::STATUS_REFUNDED;
else
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
$credit_note = CreditFactory::create($this->company_id, $this->user_id);
$credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null;
$credit_note->date = $data['date'];
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->status_id = Credit::STATUS_DRAFT;
$credit_note->client_id = $this->client->id;
$credit_line_item = InvoiceItemFactory::create();
$credit_line_item->quantity = 1;
$credit_line_item->cost = $data['refunded'];
$credit_line_item->product_key = ctrans('texts.credit');
$credit_line_item->notes = ctrans('texts.credit_created_by', ['transaction_reference', $this->number]);
$credit_line_item->line_total = $data['refunded'];
$credit_line_item->date = $data['date'];
$line_items = [];
$line_items[] = $credit_line_item;
$credit_note->line_items = $line_items;
$credit_note->amount = $data['refunded'];
$credit_note->balance = $data['refunded'];
$credit_note->save();
$this->createActivity($data, $credit_note->id);
//determine if we need to refund via gateway
if($data['gateway_refund'] !== false)
{
//process gateway refund, on success, reduce the credit note balance to 0
}
$this->save();
$this->client->paid_to_date -= $data['refunded'];
$this->client->save();
return $this;
}
private function refundPaymentWithInvoices($data)
{
}
private function refundPaymentWithInvoicesAndCredits($data)
{
}
private function createCreditLineItems()
{
}
private function createActivity(array $data, int $credit_id)
{
$fields = new \stdClass;
$activity_repo = new ActivityRepository();
$fields->payment_id = $this->id;
$fields->user_id = $this->user_id;
$fields->company_id = $this->company_id;
$fields->activity_type_id = Activity::REFUNDED_PAYMENT;
$fields->credit_id = $credit_id;
if(isset($data['invoices']))
{
foreach ($data['invoices'] as $invoice)
{
$fields->invoice_id = $invoice->id;
$activity_repo->save($fields, $this);
}
}
else
$activity_repo->save($fields, $this);
}
}

View File

@ -1,12 +0,0 @@
<?php
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use Faker\Generator as Faker;
$factory->define(App\Models\Project::class, function (Faker $faker) {
return [
'name' => $faker->name(),
'description' => $faker->text(50),
];
});

View File

@ -180,7 +180,7 @@ class CreateUsersTable extends Migration
//$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('industry_id')->references('id')->on('industries');
$table->foreign('size_id')->references('id')->on('sizes');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade')->onUpdate('cascade');
});
@ -234,7 +234,7 @@ class CreateUsersTable extends Migration
$table->string('documentable_type');
$table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
});
@ -288,9 +288,9 @@ class CreateUsersTable extends Migration
$table->string('token')->nullable();
$table->string('name')->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
Schema::create('clients', function (Blueprint $table) {
@ -342,7 +342,7 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('industry_id')->references('id')->on('industries');
$table->foreign('size_id')->references('id')->on('sizes');
// $table->foreign('currency_id')->references('id')->on('currencies');
@ -384,7 +384,7 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
//$table->unique(['company_id', 'email']);
});
@ -410,9 +410,9 @@ class CreateUsersTable extends Migration
$table->softDeletes('deleted_at', 6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('gateway_key')->references('key')->on('gateways');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
@ -484,9 +484,9 @@ class CreateUsersTable extends Migration
$t->datetime('last_viewed')->nullable();
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -562,9 +562,9 @@ class CreateUsersTable extends Migration
$t->datetime('last_viewed')->nullable();
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -590,10 +590,10 @@ class CreateUsersTable extends Migration
$t->datetime('viewed_date')->nullable();
$t->datetime('opened_date')->nullable();
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade');
$t->foreign('credit_id')->references('id')->on('credits')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('credit_id')->references('id')->on('credits')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -662,9 +662,9 @@ class CreateUsersTable extends Migration
$t->datetime('next_send_date')->nullable();
$t->unsignedInteger('remaining_cycles')->nullable();
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -727,9 +727,9 @@ class CreateUsersTable extends Migration
$t->datetime('next_send_date')->nullable();
$t->unsignedInteger('remaining_cycles')->nullable();
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -804,9 +804,9 @@ class CreateUsersTable extends Migration
$t->datetime('last_viewed')->nullable();
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -831,10 +831,10 @@ class CreateUsersTable extends Migration
$t->datetime('viewed_date')->nullable();
$t->datetime('opened_date')->nullable();
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade');
$t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -863,10 +863,10 @@ class CreateUsersTable extends Migration
$t->datetime('viewed_date')->nullable();
$t->datetime('opened_date')->nullable();
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade');
$t->foreign('quote_id')->references('id')->on('quotes')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('quote_id')->references('id')->on('quotes')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
$t->softDeletes('deleted_at', 6);
@ -887,8 +887,8 @@ class CreateUsersTable extends Migration
$t->string('name',100);
$t->decimal('rate', 13, 3)->default(0);
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
@ -920,8 +920,8 @@ class CreateUsersTable extends Migration
$t->boolean('is_deleted')->default(false);
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->timestamps(6);
@ -954,11 +954,11 @@ class CreateUsersTable extends Migration
$t->boolean('is_deleted')->default(false);
$t->boolean('is_manual')->default(false);
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade');
$t->foreign('company_gateway_id')->references('id')->on('company_gateways')->onDelete('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('client_contact_id')->references('id')->on('client_contacts')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_gateway_id')->references('id')->on('company_gateways')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('type_id')->references('id')->on('payment_types');
@ -973,7 +973,7 @@ class CreateUsersTable extends Migration
$table->string('paymentable_type');
$table->timestamps();
$table->foreign('payment_id')->references('id')->on('payments')->onDelete('cascade');
$table->foreign('payment_id')->references('id')->on('payments')->onDelete('cascade')->onUpdate('cascade');
});
@ -1005,8 +1005,8 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('bank_id')->references('id')->on('banks');
});
@ -1025,9 +1025,9 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('bank_company_id')->references('id')->on('bank_companies')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('bank_company_id')->references('id')->on('bank_companies')->onDelete('cascade')->onUpdate('cascade');
});
@ -1040,8 +1040,8 @@ class CreateUsersTable extends Migration
$table->timestamps(6);
$table->softDeletes('deleted_at', 6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
@ -1056,6 +1056,7 @@ class CreateUsersTable extends Migration
$table->unsignedInteger('vendor_id')->nullable();
$table->unsignedInteger('payment_id')->nullable();
$table->unsignedInteger('invoice_id')->nullable();
$table->unsignedInteger('credit_id')->nullable();
$table->unsignedInteger('invitation_id')->nullable();
$table->unsignedInteger('task_id')->nullable();
$table->unsignedInteger('expense_id')->nullable();
@ -1072,13 +1073,14 @@ class CreateUsersTable extends Migration
$table->index(['client_id', 'company_id']);
$table->index(['payment_id', 'company_id']);
$table->index(['invoice_id', 'company_id']);
$table->index(['credit_id', 'company_id']);
$table->index(['invitation_id', 'company_id']);
$table->index(['task_id', 'company_id']);
$table->index(['expense_id', 'company_id']);
$table->index(['client_contact_id', 'company_id']);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
//$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
@ -1089,7 +1091,7 @@ class CreateUsersTable extends Migration
$table->longText('html_backup')->nullable();
$table->timestamps(6);
$table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade');
$table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade')->onUpdate('cascade');
});
@ -1109,8 +1111,8 @@ class CreateUsersTable extends Migration
$table->string('company_ledgerable_type');
$table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
Schema::create('gateway_types', function ($table) {
@ -1133,8 +1135,8 @@ class CreateUsersTable extends Migration
$table->softDeletes('deleted_at', 6);
$table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
Schema::create('group_settings', function ($table){
@ -1144,7 +1146,7 @@ class CreateUsersTable extends Migration
$table->string('name')->nullable();
$table->mediumText('settings')->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
});
@ -1173,8 +1175,8 @@ class CreateUsersTable extends Migration
$table->mediumText('log');
$table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
@ -1207,8 +1209,8 @@ class CreateUsersTable extends Migration
$table->string('custom_value4')->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('currency_id')->references('id')->on('currencies');
});
@ -1232,9 +1234,9 @@ class CreateUsersTable extends Migration
$table->string('custom_value3')->nullable();
$table->string('custom_value4')->nullable();
$table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
});
@ -1289,8 +1291,8 @@ class CreateUsersTable extends Migration
$table->string('custom_value4')->nullable();
// Relations
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
@ -1314,8 +1316,8 @@ class CreateUsersTable extends Migration
$t->timestamps(6);
$t->softDeletes();
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$t->foreign('company_id')->references('id')->on('companies');
$t->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$t->foreign('company_id')->references('id')->on('companies')->onUpdate('cascade');
$t->unique(['company_id', 'name']);
});
@ -1345,10 +1347,10 @@ class CreateUsersTable extends Migration
$table->boolean('is_running')->default(false);
$table->text('time_log')->nullable();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
});
}

View File

@ -100,7 +100,7 @@ class ClientApiTest extends TestCase
$arr = $response->json();
$this->assertNull($arr['data']['archived_at']);
$this->assertEquals(0, $arr['data']['archived_at']);
}
public function testClientArchived()
@ -134,7 +134,7 @@ class ClientApiTest extends TestCase
$arr = $response->json();
$this->assertNull($arr['data'][0]['archived_at']);
$this->assertEquals(0, $arr['data'][0]['archived_at']);
}
public function testClientDeleted()

View File

@ -68,7 +68,7 @@ class CompanySettingsTest extends TestCase
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
\Log::error($message);
// \Log::error($message);
}
if($response) {

View File

@ -95,24 +95,25 @@ class MigrationTest extends TestCase
}
public function testMigrationFileUpload()
{
$file = new UploadedFile(base_path('tests/Unit/Migration/migration.zip'), 'migration.zip');
// public function testMigrationFileUpload()
// {
// $file = new UploadedFile(base_path('tests/Unit/Migration/migration.zip'), 'migration.zip');
$data = [
'migration' => $file,
];
// $data = [
// 'migration' => $file,
// ];
$token = $this->company->tokens->first()->token;
// $token = $this->company->tokens->first()->token;
$response = $this->withHeaders([
'X-API-TOKEN' => $token,
'X-API-SECRET' => config('ninja.api_secret'),
'X-Requested-With' => 'XMLHttpRequest',
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->post('/api/v1/migration/start', $data);
// $response = $this->withHeaders([
// 'X-API-TOKEN' => $token,
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-Requested-With' => 'XMLHttpRequest',
// 'X-API-PASSWORD' => 'ALongAndBriliantPassword',
// ])->post('/api/v1/migration/start', $data);
$response->assertStatus(200);
$this->assertTrue(file_exists(base_path('storage/migrations/migration/migration.json')));
}
// $response->assertStatus(200);
// $this->assertTrue(file_exists(base_path('storage/migrations/migration/migration.json')));
// }
}

View File

@ -9,6 +9,7 @@ use App\Factory\InvoiceFactory;
use App\Factory\PaymentFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;
@ -708,7 +709,7 @@ class PaymentTest extends TestCase
'date' => '2019/12/12',
];
$response = false;
$response = false;
try {
$response = $this->withHeaders([
@ -1066,81 +1067,4 @@ class PaymentTest extends TestCase
}
public function testBasicRefundValidation()
{
$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' => 50,
'client_id' => $client->hashed_id,
// 'invoices' => [
// [
// 'invoice_id' => $this->invoice->hashed_id,
// 'amount' => $this->invoice->amount
// ],
// ],
'date' => '2020/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
$arr = $response->json();
$response->assertStatus(200);
$payment_id = $arr['data']['id'];
$this->assertEquals(50, $arr['data']['amount']);
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$this->assertNotNull($payment);
// $this->assertNotNull($payment->invoices());
// $this->assertEquals(1, $payment->invoices()->count());
$data = [
'id' => $this->encodePrimaryKey($payment->id),
'refunded' => 50,
// 'invoices' => [
// [
// 'invoice_id' => $this->invoice->hashed_id,
// 'amount' => $this->invoice->amount
// ],
// ],
'date' => '2020/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/refund', $data);
$arr = $response->json();
$response->assertStatus(200);
}
}

View File

@ -0,0 +1,234 @@
<?php
namespace Tests\Feature;
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\PaymentFactory;
use App\Helpers\Invoice\InvoiceSum;
use App\Models\Account;
use App\Models\Activity;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Utils\Traits\Payment\Refundable
*/
class RefundTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutExceptionHandling();
}
public function testBasicRefundValidation()
{
$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' => 50,
'client_id' => $client->hashed_id,
// 'invoices' => [
// [
// 'invoice_id' => $this->invoice->hashed_id,
// 'amount' => $this->invoice->amount
// ],
// ],
'date' => '2020/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
$arr = $response->json();
$response->assertStatus(200);
$payment_id = $arr['data']['id'];
$this->assertEquals(50, $arr['data']['amount']);
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$this->assertNotNull($payment);
$data = [
'id' => $this->encodePrimaryKey($payment->id),
'refunded' => 50,
// 'invoices' => [
// [
// 'invoice_id' => $this->invoice->hashed_id,
// 'amount' => $this->invoice->amount
// ],
// ],
'date' => '2020/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/refund', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
\Log::error($message);
}
$arr = $response->json();
$response->assertStatus(200);
$this->assertEquals(50, $arr['data']['refunded']);
$this->assertEquals(Payment::STATUS_REFUNDED, $arr['data']['status_id']);
$activity = Activity::wherePaymentId($payment->id)->first();
$this->assertNotNull($activity);
$this->assertEquals(Activity::REFUNDED_PAYMENT, $activity->activity_type_id);
}
public function testRefundValidationNoInvoicesProvided()
{
$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' => 50,
'client_id' => $client->hashed_id,
'invoices' => [
[
'invoice_id' => $this->invoice->hashed_id,
'amount' => $this->invoice->amount
],
],
'date' => '2020/12/12',
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments', $data);
$arr = $response->json();
$response->assertStatus(200);
$payment_id = $arr['data']['id'];
$this->assertEquals(50, $arr['data']['amount']);
$payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first();
$this->assertNotNull($payment);
$this->assertNotNull($payment->invoices());
$this->assertEquals(1, $payment->invoices()->count());
$data = [
'id' => $this->encodePrimaryKey($payment->id),
'refunded' => 50,
// 'invoices' => [
// [
// 'invoice_id' => $this->invoice->hashed_id,
// 'amount' => $this->invoice->amount
// ],
// ],
'date' => '2020/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/refund', $data);
}
catch(ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(),1);
$this->assertNotNull($message);
\Log::error($message);
}
if($response)
$response->assertStatus(302);
}
}

File diff suppressed because one or more lines are too long

Binary file not shown.