mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-05 20:14:35 -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 = [
|
protected $casts = [
|
||||||
'line_items' => 'object',
|
'line_items' => 'object',
|
||||||
|
'backup' => 'object',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
@ -25,5 +25,6 @@ class Currency extends StaticModel
|
|||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
'precision' => 'integer',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -103,6 +103,7 @@ class Invoice extends BaseModel
|
|||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'line_items' => 'object',
|
'line_items' => 'object',
|
||||||
|
'backup' => 'object',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
@ -76,6 +76,7 @@ class Quote extends BaseModel
|
|||||||
'due_date' => 'date:Y-m-d',
|
'due_date' => 'date:Y-m-d',
|
||||||
'partial_due_date' => 'date:Y-m-d',
|
'partial_due_date' => 'date:Y-m-d',
|
||||||
'line_items' => 'object',
|
'line_items' => 'object',
|
||||||
|
'backup' => 'object',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
@ -101,6 +101,7 @@ class RecurringInvoice extends BaseModel
|
|||||||
protected $casts = [
|
protected $casts = [
|
||||||
'settings' => 'object',
|
'settings' => 'object',
|
||||||
'line_items' => 'object',
|
'line_items' => 'object',
|
||||||
|
'backup' => 'object',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
'deleted_at' => 'timestamp',
|
'deleted_at' => 'timestamp',
|
||||||
|
@ -83,6 +83,7 @@ class RecurringQuote extends BaseModel
|
|||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'line_items' => 'object',
|
'line_items' => 'object',
|
||||||
|
'backup' => 'object',
|
||||||
'settings' => 'object',
|
'settings' => 'object',
|
||||||
'updated_at' => 'timestamp',
|
'updated_at' => 'timestamp',
|
||||||
'created_at' => 'timestamp',
|
'created_at' => 'timestamp',
|
||||||
|
@ -45,6 +45,9 @@ class HandleCancellation extends AbstractService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$adjustment = $this->invoice->balance*-1;
|
$adjustment = $this->invoice->balance*-1;
|
||||||
|
|
||||||
|
$this->backupCancellation($adjustment);
|
||||||
|
|
||||||
//set invoice balance to 0
|
//set invoice balance to 0
|
||||||
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice cancellation");
|
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice cancellation");
|
||||||
|
|
||||||
@ -56,6 +59,58 @@ class HandleCancellation extends AbstractService
|
|||||||
|
|
||||||
event(new InvoiceWasCancelled($this->invoice));
|
event(new InvoiceWasCancelled($this->invoice));
|
||||||
|
|
||||||
|
|
||||||
return $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;
|
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;
|
$balance_remaining = $this->invoice->balance;
|
||||||
|
|
||||||
$total_paid = $this->invoice->amount - $this->invoice->balance;
|
$total_paid = $this->invoice->amount - $this->invoice->balance;
|
||||||
@ -88,10 +92,12 @@ class HandleReversal extends AbstractService
|
|||||||
|
|
||||||
$credit->service()->markSent()->save();
|
$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*/
|
/* Set invoice status to reversed... somehow*/
|
||||||
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();
|
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();
|
||||||
|
@ -129,6 +129,13 @@ class InvoiceService
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function reverseCancellation()
|
||||||
|
{
|
||||||
|
$this->invoice = (new HandleCancellation($this->invoice))->reverse();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function markViewed()
|
public function markViewed()
|
||||||
{
|
{
|
||||||
$this->invoice->last_viewed = Carbon::now()->format('Y-m-d H:i');
|
$this->invoice->last_viewed = Carbon::now()->format('Y-m-d H:i');
|
||||||
|
@ -17,7 +17,10 @@ trait ActionsInvoice
|
|||||||
{
|
{
|
||||||
public function invoiceDeletable($invoice) :bool
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +29,10 @@ trait ActionsInvoice
|
|||||||
|
|
||||||
public function invoiceCancellable($invoice) :bool
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +41,12 @@ trait ActionsInvoice
|
|||||||
|
|
||||||
public function invoiceReversable($invoice) :bool
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -454,6 +454,7 @@ class CreateUsersTable extends Migration
|
|||||||
$t->boolean('is_deleted')->default(false);
|
$t->boolean('is_deleted')->default(false);
|
||||||
|
|
||||||
$t->mediumText('line_items')->nullable();
|
$t->mediumText('line_items')->nullable();
|
||||||
|
$t->mediumText('backup')->nullable();
|
||||||
$t->text('footer')->nullable();
|
$t->text('footer')->nullable();
|
||||||
$t->text('public_notes')->nullable();
|
$t->text('public_notes')->nullable();
|
||||||
$t->text('private_notes')->nullable();
|
$t->text('private_notes')->nullable();
|
||||||
@ -530,6 +531,7 @@ class CreateUsersTable extends Migration
|
|||||||
|
|
||||||
$t->boolean('is_deleted')->default(false);
|
$t->boolean('is_deleted')->default(false);
|
||||||
$t->mediumText('line_items')->nullable();
|
$t->mediumText('line_items')->nullable();
|
||||||
|
$t->mediumText('backup')->nullable();
|
||||||
$t->text('footer')->nullable();
|
$t->text('footer')->nullable();
|
||||||
$t->text('public_notes')->nullable();
|
$t->text('public_notes')->nullable();
|
||||||
$t->text('private_notes')->nullable();
|
$t->text('private_notes')->nullable();
|
||||||
@ -633,6 +635,7 @@ class CreateUsersTable extends Migration
|
|||||||
$t->boolean('is_deleted')->default(false);
|
$t->boolean('is_deleted')->default(false);
|
||||||
|
|
||||||
$t->mediumText('line_items')->nullable();
|
$t->mediumText('line_items')->nullable();
|
||||||
|
$t->mediumText('backup')->nullable();
|
||||||
$t->text('footer')->nullable();
|
$t->text('footer')->nullable();
|
||||||
$t->text('public_notes')->nullable();
|
$t->text('public_notes')->nullable();
|
||||||
$t->text('private_notes')->nullable();
|
$t->text('private_notes')->nullable();
|
||||||
@ -699,6 +702,7 @@ class CreateUsersTable extends Migration
|
|||||||
$t->boolean('is_deleted')->default(false);
|
$t->boolean('is_deleted')->default(false);
|
||||||
|
|
||||||
$t->mediumText('line_items')->nullable();
|
$t->mediumText('line_items')->nullable();
|
||||||
|
$t->mediumText('backup')->nullable();
|
||||||
|
|
||||||
$t->text('footer')->nullable();
|
$t->text('footer')->nullable();
|
||||||
$t->text('public_notes')->nullable();
|
$t->text('public_notes')->nullable();
|
||||||
@ -770,7 +774,7 @@ class CreateUsersTable extends Migration
|
|||||||
$t->boolean('is_deleted')->default(false);
|
$t->boolean('is_deleted')->default(false);
|
||||||
|
|
||||||
$t->mediumText('line_items')->nullable();
|
$t->mediumText('line_items')->nullable();
|
||||||
|
$t->mediumText('backup')->nullable();
|
||||||
|
|
||||||
$t->text('footer')->nullable();
|
$t->text('footer')->nullable();
|
||||||
$t->text('public_notes')->nullable();
|
$t->text('public_notes')->nullable();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user