1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

working on paid_to_date fields and calculations

This commit is contained in:
David Bomba 2021-01-24 17:44:14 +11:00
parent a274fdc353
commit 24dfbe48dc
33 changed files with 179 additions and 50 deletions

View File

@ -44,6 +44,7 @@ class InvoiceFactory
$invoice->custom_value4 = 0;
$invoice->amount = 0;
$invoice->balance = 0;
$invoice->paid_to_date = 0;
$invoice->partial = 0;
$invoice->user_id = $user_id;
$invoice->company_id = $company_id;

View File

@ -50,6 +50,7 @@ class InvoiceToRecurringInvoiceFactory
$recurring_invoice->last_sent_date = null;
$recurring_invoice->next_send_date = null;
$recurring_invoice->remaining_cycles = 0;
$recurring_invoice->paid_to_date = 0;
return $recurring_invoice;
}

View File

@ -45,6 +45,7 @@ class QuoteFactory
$quote->partial = 0;
$quote->user_id = $user_id;
$quote->company_id = $company_id;
$quote->paid_to_date = 0;
return $quote;
}

View File

@ -49,6 +49,7 @@ class RecurringInvoiceFactory
$invoice->last_sent_date = null;
$invoice->next_send_date = null;
$invoice->remaining_cycles = 0;
$invoice->paid_to_date = 0;
return $invoice;
}

View File

@ -50,6 +50,7 @@ class RecurringInvoiceToInvoiceFactory
$invoice->recurring_id = $recurring_invoice->id;
$invoice->client_id = $client->id;
$invoice->auto_bill_enabled = $recurring_invoice->auto_bill_enabled;
$invoice->paid_to_date = 0;
return $invoice;
}

View File

@ -30,6 +30,7 @@
* @OA\Property(property="line_items", type="object", example="", description="_________"),
* @OA\Property(property="amount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="balance", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="paid_to_date", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="discount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="partial", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="is_amount_discount", type="boolean", example=true, description="_________"),

View File

@ -29,6 +29,7 @@
* @OA\Property(property="line_items", type="object", example="", description="_________"),
* @OA\Property(property="amount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="balance", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="paid_to_date", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="discount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="partial", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="is_amount_discount", type="boolean", example=true, description="_________"),

View File

@ -29,6 +29,7 @@
* @OA\Property(property="line_items", type="object", example="", description="_________"),
* @OA\Property(property="amount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="balance", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="paid_to_date", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="discount", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="partial", type="number", format="float", example="10.00", description="_________"),
* @OA\Property(property="is_amount_discount", type="boolean", example=true, description="_________"),

View File

@ -435,11 +435,6 @@ class PaymentController extends BaseController
*/
public function destroy(DestroyPaymentRequest $request, Payment $payment)
{
// $payment->service()->deletePayment();
// $payment->is_deleted = true;
// $payment->save();
// $payment->delete();
$this->payment_repo->delete($payment);

View File

@ -57,17 +57,31 @@ class ApplyCreditPayment implements ShouldQueue
if ($cred->id == $this->credit->id) {
$cred->pivot->amount = $this->amount;
$cred->pivot->save();
$cred->paid_to_date += $this->amount;
$cred->save();
}
});
$credit_balance = $this->credit->balance;
if ($this->amount == $credit_balance) { //total credit applied.
$this->credit->setStatus(Credit::STATUS_APPLIED);
$this->credit->updateBalance($this->amount * -1);
$this->credit
->service()
->setStatus(Credit::STATUS_APPLIED)
->updateBalance($this->amount * -1)
->updatePaidToDate($this->amount)
->save();
} elseif ($this->amount < $credit_balance) { //compare number appropriately
$this->credit->setStatus(Credit::STATUS_PARTIAL);
$this->credit->updateBalance($this->amount * -1);
$this->credit
->service()
->setStatus(Credit::STATUS_PARTIAL)
->updateBalance($this->amount * -1)
->updatePaidToDate($this->amount)
->save();
}
/* Update Payment Applied Amount*/

View File

@ -896,14 +896,6 @@ class Import implements ShouldQueue
],
];
//depending on the status, we do a final action.
//s$payment = $this->updatePaymentForStatus($payment, $modified['status_id']);
// if($modified['is_deleted'])
// $payment->service()->deletePayment();
// if(isset($modified['deleted_at']))
// $payment->delete();
}
Payment::reguard();

View File

@ -49,6 +49,6 @@ class CreditArchivedActivity implements ShouldQueue
$fields->company_id = $event->credit->company_id;
$fields->activity_type_id = Activity::ARCHIVE_CREDIT;
$this->activity_repo->save($fields, $$event->credit, $event->event_vars);
$this->activity_repo->save($fields, $event->credit, $event->event_vars);
}
}

View File

@ -261,4 +261,5 @@ class Credit extends BaseModel
}
});
}
}

View File

@ -117,6 +117,10 @@ class PaymentMigrationRepository extends BaseRepository
$inv->pivot->amount = $invoice_totals;
$inv->pivot->refunded = $refund_totals;
$inv->pivot->save();
$inv->paid_to_date += $invoice_totals;
$inv->save();
});
}
@ -130,6 +134,9 @@ class PaymentMigrationRepository extends BaseRepository
$payment->credits->each(function ($cre) use ($credit_totals) {
$cre->pivot->amount = $credit_totals;
$cre->pivot->save();
$cre->paid_to_date += $invoice_totals;
$cre->save();
});
}

View File

@ -118,8 +118,6 @@ class PaymentRepository extends BaseRepository
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
nlog("invoice totals = {$invoice_totals}");
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
$payment->invoices()->saveMany($invoices);

View File

@ -209,7 +209,7 @@ class PaymentMethod
$payment_urls = [];
}
$payment_urls[] = [
$this->payment_urls[] = [
'label' => ctrans('texts.apply_credit'),
'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT,
'gateway_type_id' => GatewayType::CREDIT,
@ -238,6 +238,5 @@ class PaymentMethod
return true;
}
}

View File

@ -70,6 +70,7 @@ class ApplyPayment
}
$this->credit->balance -= $this->amount_applied;
$this->credit->paid_to_date += $this->amount_applied;
if ((int)$this->credit->balance == 0) {
$this->credit->status_id = Credit::STATUS_APPLIED;
@ -110,6 +111,8 @@ class ApplyPayment
$this->payment->currency_id = $this->credit->client->getSetting('currency_id');
$this->payment->save();
$this->payment->service()->applyNumber()->save();
$this->payment
->invoices()
->attach($this->invoice->id, ['amount' => $this->amount_applied]);

View File

@ -97,6 +97,12 @@ class CreditService
return $this;
}
public function updatePaidToDate($adjustment)
{
$this->credit->paid_to_date += $adjustment;
return $this;
}
public function fillDefaults()
{

View File

@ -104,14 +104,24 @@ class ApplyPayment extends AbstractService
->ledger()
->updatePaymentBalance($amount_paid);
$this->invoice->client->service()->updateBalance($amount_paid)->save();
$this->invoice
->client
->service()
->updateBalance($amount_paid)
->save();
/* Update Pivot Record amount */
$this->payment->invoices->each(function ($inv) use($amount_paid){
if ($inv->id == $this->invoice->id) {
$inv->pivot->amount = ($amount_paid*-1);
$inv->pivot->save();
$inv->paid_to_date += floatval($amount_paid*-1);
$inv->save();
}
});
$this->invoice->service()->applyNumber()->save();

View File

@ -59,9 +59,6 @@ class AutoBillInvoice extends AbstractService
$this->applyCreditPayment();
}
// info("partial = {$this->invoice->partial}");
// info("balance = {$this->invoice->balance}");
/* Determine $amount */
if ($this->invoice->partial > 0) {
$amount = $this->invoice->partial;
@ -122,18 +119,24 @@ class AutoBillInvoice extends AbstractService
$payment->invoices()->attach($this->invoice->id, ['amount' => $amount]);
$this->invoice->service()->setStatus(Invoice::STATUS_PAID)->save();
$this->invoice
->service()
->setStatus(Invoice::STATUS_PAID)
->save();
foreach ($this->used_credit as $credit) {
$current_credit = Credit::find($credit['credit_id']);
$payment->credits()->attach($current_credit->id, ['amount' => $credit['amount']]);
$payment->credits()
->attach($current_credit->id, ['amount' => $credit['amount']]);
info("adjusting credit balance {$current_credit->balance} by this amount ". $credit['amount']);
$current_credit->balance -= $credit['amount'];
$current_credit->service()
->updateBalance($credit['amount']*-1)
->updatePaidToDate($credit['amount'])
->setCalculatedStatus()
->save();
$current_credit->service()->setCalculatedStatus()->save();
// $this->applyPaymentToCredit($current_credit, $credit['amount']);
}
$payment->ledger()
@ -153,7 +156,10 @@ class AutoBillInvoice extends AbstractService
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
return $this->invoice->service()->setCalculatedStatus()->save();
return $this->invoice
->service()
->setCalculatedStatus()
->save();
}
/**
@ -194,6 +200,7 @@ class AutoBillInvoice extends AbstractService
$this->used_credit[$key]['credit_id'] = $credit->id;
$this->used_credit[$key]['amount'] = $this->invoice->partial;
$this->invoice->balance -= $this->invoice->partial;
$this->invoice->paid_to_date += $this->invoice->partial;
$this->invoice->partial = 0;
break;
} else {
@ -201,6 +208,7 @@ class AutoBillInvoice extends AbstractService
$this->used_credit[$key]['amount'] = $credit->balance;
$this->invoice->partial -= $credit->balance;
$this->invoice->balance -= $credit->balance;
$this->invoice->paid_to_date += $credit->balance;
}
} else {
@ -208,12 +216,15 @@ class AutoBillInvoice extends AbstractService
if ($credit->balance >= $this->invoice->balance) {
$this->used_credit[$key]['credit_id'] = $credit->id;
$this->used_credit[$key]['amount'] = $this->invoice->balance;
$this->invoice->paid_to_date += $this->invoice->balance;
$this->invoice->balance = 0;
break;
} else {
$this->used_credit[$key]['credit_id'] = $credit->id;
$this->used_credit[$key]['amount'] = $credit->balance;
$this->invoice->balance -= $credit->balance;
$this->invoice->paid_to_date += $credit->balance;
}
}
}

View File

@ -105,6 +105,9 @@ class HandleReversal extends AbstractService
//harvest the credit record and add in the amount for the credit.
$paymentable_credit->pivot->amount = $total_paid;
$paymentable_credit->pivot->save();
$paymentable_credit->paid_to_date += $total_paid;
$paymentable_credit->save();
}
/* Set invoice balance to 0 */

View File

@ -112,6 +112,13 @@ class InvoiceService
return $this;
}
public function updatePaidToDate($adjustment)
{
$this->invoice->paid_to_date += $adjustment;
return $this;
}
public function createInvitations()
{
$this->invoice = (new CreateInvitations($this->invoice))->run();

View File

@ -70,6 +70,7 @@ class MarkPaid extends AbstractService
$this->invoice->service()
->updateBalance($payment->amount * -1)
->updatePaidToDate($payment->amount)
->setStatus(Invoice::STATUS_PAID)
->applyNumber()
->save();

View File

@ -55,7 +55,6 @@ class DeletePayment
private function cleanupPayment()
{
$this->payment->is_deleted = true;
// $entity->save();
$this->payment->delete();
return $this;
@ -78,10 +77,22 @@ class DeletePayment
private function adjustInvoices()
{
if ($this->payment->invoices()->exists()) {
$this->payment->invoices()->each(function ($paymentable_invoice) {
$paymentable_invoice->service()->updateBalance($paymentable_invoice->pivot->amount)->save();
$paymentable_invoice->ledger()->updateInvoiceBalance($paymentable_invoice->pivot->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")->save();
$paymentable_invoice->client->service()->updateBalance($paymentable_invoice->pivot->amount)->save();
$paymentable_invoice->service()
->updateBalance($paymentable_invoice->pivot->amount)
->updatePaidToDate($paymentable_invoice->pivot->amount * -1)
->save();
$paymentable_invoice->ledger()
->updateInvoiceBalance($paymentable_invoice->pivot->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save();
$paymentable_invoice->client
->service()
->updateBalance($paymentable_invoice->pivot->amount)
->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
@ -101,10 +112,12 @@ class DeletePayment
{
if ($this->payment->credits()->exists()) {
$this->payment->credits()->each(function ($paymentable_credit) {
$paymentable_credit->balance += $paymentable_credit->pivot->amount;
$paymentable_credit->setStatus(Credit::STATUS_SENT);
//fire event for this credit
//
$paymentable_credit->service()
->updateBalance($paymentable_credit->pivot->amount)
->updatePaidToDate($paymentable_credit->pivot->amount*-1)
->setStatus(Credit::STATUS_SENT)
->save();
});
}

View File

@ -59,11 +59,16 @@ class PaymentService
$client = $this->payment->client;
$invoices->each(function ($invoice) {
if ($invoice->pivot->amount > 0) {
$invoice->status_id = Invoice::STATUS_SENT;
$invoice->balance = $invoice->pivot->amount;
$invoice->save();
$invoice->service()
->updateBalance($invoice->pivot->amount)
->updatePaidToDate($invoice->pivot->amount*-1)
->setStatus(Invoice::STATUS_SENT)
->save();
}
});
$this->payment

View File

@ -188,18 +188,26 @@ class RefundPayment
if ($available_credit > $this->total_refund) {
$paymentable_credit->pivot->refunded += $this->total_refund;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $this->total_refund;
$paymentable_credit->service()->setStatus(Credit::STATUS_SENT)->save();
//$paymentable_credit->save();
$paymentable_credit->service()
->setStatus(Credit::STATUS_SENT)
->updateBalance($this->total_refund)
->updatePaidToDate($this->total_refund*-1)
->save();
$this->total_refund = 0;
} else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $available_credit;
$paymentable_credit->service()->setStatus(Credit::STATUS_SENT)->save();
// $paymentable_credit->save();
$paymentable_credit->service()
->setStatus(Credit::STATUS_SENT)
->updateBalance($available_credit)
->updatePaidToDate($available_credit*-1)
->save();
$this->total_refund -= $available_credit;
}

View File

@ -79,6 +79,7 @@ class UpdateInvoicePayment
$invoice->service() //caution what if we amount paid was less than partial - we wipe it!
->clearPartial()
->updateBalance($paid_amount * -1)
->updatePaidToDate($paid_amount)
->updateStatus()
->save();

View File

@ -136,6 +136,7 @@ class CreditTransformer extends EntityTransformer
'line_items' => $credit->line_items ?: (array) [],
'entity_type' => 'credit',
'exchange_rate' => (float) $credit->exchange_rate,
'paid_to_date' => (float) $credit->paid_to_date,
];
}
}

View File

@ -140,6 +140,8 @@ class InvoiceTransformer extends EntityTransformer
'reminder2_sent' => $invoice->reminder2_sent ?: '',
'reminder3_sent' => $invoice->reminder3_sent ?: '',
'reminder_last_sent' => $invoice->reminder_last_sent ?: '',
'paid_to_date' => (float) $invoice->paid_to_date,
];
}
}

View File

@ -137,6 +137,7 @@ class QuoteTransformer extends EntityTransformer
'line_items' => $quote->line_items ?: (array) [],
'entity_type' => 'quote',
'exchange_rate' => (float) $quote->exchange_rate,
'paid_to_date' => (float) $quote->paid_to_date,
'project_id' => $this->encodePrimaryKey($quote->project_id),
];
}

View File

@ -138,6 +138,7 @@ class RecurringInvoiceTransformer extends EntityTransformer
'auto_bill' => (string) $invoice->auto_bill,
'auto_bill_enabled' => (bool) $invoice->auto_bill_enabled,
'due_date_days' => (string) $invoice->due_date_days ?: '',
'paid_to_date' => (float) $invoice->paid_to_date,
];
}
}

View File

@ -0,0 +1,42 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPaidToDateColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('invoices', function (Blueprint $table) {
$table->decimal('paid_to_date', 20, 6)->default(0);
});
Schema::table('quotes', function (Blueprint $table) {
$table->decimal('paid_to_date', 20, 6)->default(0);
});
Schema::table('credits', function (Blueprint $table) {
$table->decimal('paid_to_date', 20, 6)->default(0);
});
Schema::table('recurring_invoices', function (Blueprint $table) {
$table->decimal('paid_to_date', 20, 6)->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}