mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-04 02:34:35 -04:00
Fixes for missing contact_key in seeder (#3190)
* Stub refund payment * Fixes for missing contact_key in seeder
This commit is contained in:
parent
c47bf76f48
commit
08d8d11dea
@ -29,4 +29,9 @@ class Credit extends BaseModel
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(User::class, 'assigned_user_id', 'id');
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function invoices()
|
||||||
|
{
|
||||||
|
return $this->morphToMany(Invoice::class, 'creditable');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,12 @@ class Invoice extends BaseModel
|
|||||||
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function credits()
|
||||||
|
{
|
||||||
|
return $this->morphedByMany(Credit::class, 'creditable')->withPivot('amount');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ---------------- */
|
/* ---------------- */
|
||||||
/* Settings getters */
|
/* Settings getters */
|
||||||
/* ---------------- */
|
/* ---------------- */
|
||||||
|
@ -31,7 +31,9 @@ class PaymentRepository extends BaseRepository
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves
|
* Saves a payment.
|
||||||
|
*
|
||||||
|
*
|
||||||
* @param Request $request the request object
|
* @param Request $request the request object
|
||||||
* @param Payment $payment The Payment object
|
* @param Payment $payment The Payment object
|
||||||
* @return Object Payment $payment
|
* @return Object Payment $payment
|
||||||
@ -41,9 +43,10 @@ class PaymentRepository extends BaseRepository
|
|||||||
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here.
|
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here.
|
||||||
$payment->fill($request->input());
|
$payment->fill($request->input());
|
||||||
|
|
||||||
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
$payment->save();
|
$payment->save();
|
||||||
|
|
||||||
if ($request->input('invoices')) {
|
if ($request->has('invoices')) {
|
||||||
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get();
|
$invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get();
|
||||||
|
|
||||||
$payment->invoices()->saveMany($invoices);
|
$payment->invoices()->saveMany($invoices);
|
||||||
@ -56,7 +59,7 @@ class PaymentRepository extends BaseRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//paid is made, but not to any invoice, therefore we are applying the payment to the clients credit
|
//payment is made, but not to any invoice, therefore we are applying the payment to the clients credit
|
||||||
ApplyClientPayment::dispatchNow($payment, $payment->company);
|
ApplyClientPayment::dispatchNow($payment, $payment->company);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,4 +69,63 @@ class PaymentRepository extends BaseRepository
|
|||||||
|
|
||||||
return $payment->fresh();
|
return $payment->fresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates
|
||||||
|
*
|
||||||
|
* The update code path is different to the save path
|
||||||
|
* additional considerations come into play when 'updating'
|
||||||
|
* a payment.
|
||||||
|
*
|
||||||
|
* @param Request $request the request object
|
||||||
|
* @param Payment $payment The Payment object
|
||||||
|
* @return Object Payment $payment
|
||||||
|
*/
|
||||||
|
public function update(Request $request, Payment $payment) :?Payment
|
||||||
|
{
|
||||||
|
|
||||||
|
if($payment->amount >= 0)
|
||||||
|
return $this->applyPayment($request, $payment);
|
||||||
|
|
||||||
|
return $this->refundPayment($request, $payment);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function applyPayment(Request $request, Payment $payment) :?Payment
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function refundPayment(Request $request, Payment $payment) :?Payment
|
||||||
|
{
|
||||||
|
//temp variable to sum the total refund/credit amount
|
||||||
|
$invoice_total_adjustment = 0;
|
||||||
|
|
||||||
|
if($request->has('invoices')){
|
||||||
|
|
||||||
|
foreach($request->input('invoices') as $adjusted_invoice) {
|
||||||
|
$invoice = Invoice::whereId($adjusted_invoice['id'])->company()->first();
|
||||||
|
$invoice_total_adjustment += $adjusted_invoice['amount'];
|
||||||
|
|
||||||
|
if(!array_key_exists('credit', $adjusted_invoice)){
|
||||||
|
//todo - generate Credit Note for $amount on $invoice - the assumption here is that it is a FULL refund
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if($request->input('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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class ClientContactTransformer extends EntityTransformer
|
|||||||
'custom_value2' => $contact->custom_value2 ?: '',
|
'custom_value2' => $contact->custom_value2 ?: '',
|
||||||
'custom_value3' => $contact->custom_value3 ?: '',
|
'custom_value3' => $contact->custom_value3 ?: '',
|
||||||
'custom_value4' => $contact->custom_value4 ?: '',
|
'custom_value4' => $contact->custom_value4 ?: '',
|
||||||
'contact_key' => $contact->contact_key,
|
'contact_key' => $contact->contact_key ?: '',
|
||||||
'send_invoice' => (bool) $contact->send_invoice,
|
'send_invoice' => (bool) $contact->send_invoice,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -959,6 +959,18 @@ class CreateUsersTable extends Migration
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Schema::create('creditables', function ($table) { //allows multiple credits to one invoice
|
||||||
|
$table->increments('id');
|
||||||
|
$table->unsignedInteger('credit_id');
|
||||||
|
$table->unsignedInteger('creditable_id');
|
||||||
|
$table->decimal('amount', 16, 4)->default(0);
|
||||||
|
$table->string('creditable_type');
|
||||||
|
|
||||||
|
$table->foreign('credit_id')->references('id')->on('credits')->onDelete('cascade');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
Schema::create('payment_libraries', function ($t) {
|
Schema::create('payment_libraries', function ($t) {
|
||||||
$t->increments('id');
|
$t->increments('id');
|
||||||
$t->timestamps(6);
|
$t->timestamps(6);
|
||||||
|
@ -116,6 +116,7 @@ class RandomDataSeeder extends Seeder
|
|||||||
'password' => Hash::make(config('ninja.testvars.password')),
|
'password' => Hash::make(config('ninja.testvars.password')),
|
||||||
'email_verified_at' => now(),
|
'email_verified_at' => now(),
|
||||||
'client_id' =>$client->id,
|
'client_id' =>$client->id,
|
||||||
|
'contact_key' => \Illuminate\Support\Str::random(40),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user