mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Working on reversing an invoice cancellation
This commit is contained in:
parent
1dd73e3a06
commit
4846c9bccc
@ -70,6 +70,7 @@ class Credit extends BaseModel
|
||||
|
||||
protected $casts = [
|
||||
'line_items' => 'object',
|
||||
'backup' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
'deleted_at' => 'timestamp',
|
||||
|
@ -25,5 +25,6 @@ class Currency extends StaticModel
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
'deleted_at' => 'timestamp',
|
||||
'precision' => 'integer',
|
||||
];
|
||||
}
|
||||
|
@ -103,6 +103,7 @@ class Invoice extends BaseModel
|
||||
|
||||
protected $casts = [
|
||||
'line_items' => 'object',
|
||||
'backup' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
'deleted_at' => 'timestamp',
|
||||
|
@ -76,6 +76,7 @@ class Quote extends BaseModel
|
||||
'due_date' => 'date:Y-m-d',
|
||||
'partial_due_date' => 'date:Y-m-d',
|
||||
'line_items' => 'object',
|
||||
'backup' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
'deleted_at' => 'timestamp',
|
||||
|
@ -101,6 +101,7 @@ class RecurringInvoice extends BaseModel
|
||||
protected $casts = [
|
||||
'settings' => 'object',
|
||||
'line_items' => 'object',
|
||||
'backup' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
'deleted_at' => 'timestamp',
|
||||
|
@ -83,6 +83,7 @@ class RecurringQuote extends BaseModel
|
||||
|
||||
protected $casts = [
|
||||
'line_items' => 'object',
|
||||
'backup' => 'object',
|
||||
'settings' => 'object',
|
||||
'updated_at' => 'timestamp',
|
||||
'created_at' => 'timestamp',
|
||||
|
@ -45,6 +45,9 @@ class HandleCancellation extends AbstractService
|
||||
}
|
||||
|
||||
$adjustment = $this->invoice->balance*-1;
|
||||
|
||||
$this->backupCancellation($adjustment);
|
||||
|
||||
//set invoice balance to 0
|
||||
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice cancellation");
|
||||
|
||||
@ -56,6 +59,58 @@ class HandleCancellation extends AbstractService
|
||||
|
||||
event(new InvoiceWasCancelled($this->invoice));
|
||||
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
|
||||
public function reverse()
|
||||
{
|
||||
|
||||
$cancellation = $this->backup->cancellation;
|
||||
|
||||
$adjustment = $cancellation->adjustment*-1;
|
||||
|
||||
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice cancellation REVERSAL");
|
||||
|
||||
/* Reverse the invoice status and balance */
|
||||
$this->invoice->balance += $adjustment;
|
||||
$this->invoice->status_id = $cancellation->status_id;
|
||||
|
||||
$this->invoice->client->service()->updateBalance($adjustment)->save();
|
||||
|
||||
/* Pop the cancellation out of the backup*/
|
||||
$backup = $this->invoice->backup;
|
||||
unset($backup->cancellation);
|
||||
$this->invoice->backup = $backup;
|
||||
$this->invoice->save();
|
||||
|
||||
return $this->invoice;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the cancellation in case we ever need to reverse it.
|
||||
*
|
||||
* @param float $adjustment The amount the balance has been reduced by to cancel the invoice
|
||||
* @return void
|
||||
*/
|
||||
private function backupCancellation($adjustment)
|
||||
{
|
||||
|
||||
if(!is_object($this->invoice->backup)){
|
||||
$backup = new \stdClass;
|
||||
$this->invoice->backup = $backup;
|
||||
}
|
||||
|
||||
$cancellation = new \stdClass;
|
||||
$cancellation->adjustment = $adjustment;
|
||||
$cancellation->status_id = $this->invoice->status_id;
|
||||
|
||||
$invoice_backup = $this->invoice->backup;
|
||||
$invoice_backup->cancellation = $cancellation;
|
||||
|
||||
$this->invoice->backup = $invoice_backup;
|
||||
$this->invoice->save();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,10 @@ class HandleReversal extends AbstractService
|
||||
return $this->invoice;
|
||||
}
|
||||
|
||||
if($this->invoice->status_id == Invoice::STATUS_CANCELLED)
|
||||
$this->invoice->service()->reverseCancellation();
|
||||
|
||||
/*Consider if we have just cancelled the invoice here... this is broken!*/
|
||||
$balance_remaining = $this->invoice->balance;
|
||||
|
||||
$total_paid = $this->invoice->amount - $this->invoice->balance;
|
||||
@ -88,10 +92,12 @@ class HandleReversal extends AbstractService
|
||||
|
||||
$credit->service()->markSent()->save();
|
||||
}
|
||||
/* Set invoice balance to 0 */
|
||||
$this->invoice->ledger()->updateInvoiceBalance($balance_remaining*-1, $notes)->save();
|
||||
|
||||
$this->invoice->balance= 0;
|
||||
/* Set invoice balance to 0 */
|
||||
if($this->invoice->balance != 0)
|
||||
$this->invoice->ledger()->updateInvoiceBalance($balance_remaining*-1, $notes)->save();
|
||||
|
||||
$this->invoice->balance=0;
|
||||
|
||||
/* Set invoice status to reversed... somehow*/
|
||||
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();
|
||||
|
@ -129,6 +129,13 @@ class InvoiceService
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reverseCancellation()
|
||||
{
|
||||
$this->invoice = (new HandleCancellation($this->invoice))->reverse();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function markViewed()
|
||||
{
|
||||
$this->invoice->last_viewed = Carbon::now()->format('Y-m-d H:i');
|
||||
|
@ -17,7 +17,10 @@ trait ActionsInvoice
|
||||
{
|
||||
public function invoiceDeletable($invoice) :bool
|
||||
{
|
||||
if ($invoice->status_id <= Invoice::STATUS_SENT && $invoice->is_deleted == false && $invoice->deleted_at == null && $invoice->balance == 0) {
|
||||
if ($invoice->status_id <= Invoice::STATUS_SENT &&
|
||||
$invoice->is_deleted == false &&
|
||||
$invoice->deleted_at == null &&
|
||||
$invoice->balance == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -26,7 +29,10 @@ trait ActionsInvoice
|
||||
|
||||
public function invoiceCancellable($invoice) :bool
|
||||
{
|
||||
if (($invoice->status_id == Invoice::STATUS_SENT || $invoice->status_id == Invoice::STATUS_PARTIAL) && $invoice->is_deleted == false && $invoice->deleted_at == null) {
|
||||
if (($invoice->status_id == Invoice::STATUS_SENT ||
|
||||
$invoice->status_id == Invoice::STATUS_PARTIAL) &&
|
||||
$invoice->is_deleted == false &&
|
||||
$invoice->deleted_at == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -35,7 +41,12 @@ trait ActionsInvoice
|
||||
|
||||
public function invoiceReversable($invoice) :bool
|
||||
{
|
||||
if (($invoice->status_id == Invoice::STATUS_SENT || $invoice->status_id == Invoice::STATUS_PARTIAL || $invoice->status_id == Invoice::STATUS_PAID) && $invoice->is_deleted == false && $invoice->deleted_at == null) {
|
||||
if (($invoice->status_id == Invoice::STATUS_SENT ||
|
||||
$invoice->status_id == Invoice::STATUS_PARTIAL ||
|
||||
$invoice->status_id == Invoice::STATUS_CANCELLED ||
|
||||
$invoice->status_id == Invoice::STATUS_PAID) &&
|
||||
$invoice->is_deleted == false &&
|
||||
$invoice->deleted_at == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -454,6 +454,7 @@ class CreateUsersTable extends Migration
|
||||
$t->boolean('is_deleted')->default(false);
|
||||
|
||||
$t->mediumText('line_items')->nullable();
|
||||
$t->mediumText('backup')->nullable();
|
||||
$t->text('footer')->nullable();
|
||||
$t->text('public_notes')->nullable();
|
||||
$t->text('private_notes')->nullable();
|
||||
@ -530,6 +531,7 @@ class CreateUsersTable extends Migration
|
||||
|
||||
$t->boolean('is_deleted')->default(false);
|
||||
$t->mediumText('line_items')->nullable();
|
||||
$t->mediumText('backup')->nullable();
|
||||
$t->text('footer')->nullable();
|
||||
$t->text('public_notes')->nullable();
|
||||
$t->text('private_notes')->nullable();
|
||||
@ -633,6 +635,7 @@ class CreateUsersTable extends Migration
|
||||
$t->boolean('is_deleted')->default(false);
|
||||
|
||||
$t->mediumText('line_items')->nullable();
|
||||
$t->mediumText('backup')->nullable();
|
||||
$t->text('footer')->nullable();
|
||||
$t->text('public_notes')->nullable();
|
||||
$t->text('private_notes')->nullable();
|
||||
@ -699,6 +702,7 @@ class CreateUsersTable extends Migration
|
||||
$t->boolean('is_deleted')->default(false);
|
||||
|
||||
$t->mediumText('line_items')->nullable();
|
||||
$t->mediumText('backup')->nullable();
|
||||
|
||||
$t->text('footer')->nullable();
|
||||
$t->text('public_notes')->nullable();
|
||||
@ -770,7 +774,7 @@ class CreateUsersTable extends Migration
|
||||
$t->boolean('is_deleted')->default(false);
|
||||
|
||||
$t->mediumText('line_items')->nullable();
|
||||
|
||||
$t->mediumText('backup')->nullable();
|
||||
|
||||
$t->text('footer')->nullable();
|
||||
$t->text('public_notes')->nullable();
|
||||
|
Loading…
x
Reference in New Issue
Block a user