Fixes edge cases when a payment is deleted multiple times

This commit is contained in:
David Bomba 2023-10-09 17:49:26 +11:00
parent 1ad3480f31
commit 60e5bd6e63
10 changed files with 25 additions and 25 deletions

View File

@ -158,9 +158,7 @@ class ActivityExport extends BaseExport
$clean_row = []; $clean_row = [];
foreach (array_values($this->input['report_keys']) as $key => $value) { foreach (array_values($this->input['report_keys']) as $key => $value) {
nlog("key: {$key}, value: {$value}");
nlog($row);
$clean_row[$key]['entity'] = 'activity'; $clean_row[$key]['entity'] = 'activity';
$clean_row[$key]['id'] = $key; $clean_row[$key]['id'] = $key;
$clean_row[$key]['hashed_id'] = null; $clean_row[$key]['hashed_id'] = null;

View File

@ -115,7 +115,7 @@ class TaskExport extends BaseExport
$this->storage_array = []; $this->storage_array = [];
}); });
nlog($this->storage_item_array); // nlog($this->storage_item_array);
return array_merge(['columns' => $header], $this->storage_item_array); return array_merge(['columns' => $header], $this->storage_item_array);
} }

View File

@ -25,7 +25,8 @@ class GroupSettingFilters extends QueryFilters
* @return Builder * @return Builder
*/ */
public function name(string $name = ''): Builder public function name(string $name = ''): Builder
{nlog("filter"); {
if (strlen($name) == 0) { if (strlen($name) == 0) {
return $this->builder; return $this->builder;
} }

View File

@ -458,14 +458,15 @@ class ProductController extends BaseController
*/ */
public function bulk(BulkProductRequest $request) public function bulk(BulkProductRequest $request)
{ {
/** @var \App\Models\User $user */
$user = auth()->user();
$action = $request->input('action'); $action = $request->input('action');
$ids = $request->input('ids'); $ids = $request->input('ids');
$products = Product::withTrashed()->whereIn('id', $ids); $products = Product::withTrashed()->whereIn('id', $ids);
nlog($products->count());
if($action == 'set_tax_id'){ if($action == 'set_tax_id'){
$tax_id = $request->input('tax_id'); $tax_id = $request->input('tax_id');
@ -475,8 +476,8 @@ class ProductController extends BaseController
return $this->listResponse(Product::withTrashed()->whereIn('id', $ids)); return $this->listResponse(Product::withTrashed()->whereIn('id', $ids));
} }
$products->cursor()->each(function ($product, $key) use ($action) { $products->cursor()->each(function ($product, $key) use ($action, $user) {
if (auth()->user()->can('edit', $product)) { if ($user->can('edit', $product)) {
$this->product_repo->{$action}($product); $this->product_repo->{$action}($product);
} }
}); });

View File

@ -182,7 +182,8 @@ class SetupController extends Controller
* @return Application|ResponseFactory|JsonResponse|Response * @return Application|ResponseFactory|JsonResponse|Response
*/ */
public function checkDB(CheckDatabaseRequest $request) public function checkDB(CheckDatabaseRequest $request)
{nlog("trying"); {
try { try {
$status = SystemHealth::dbCheck($request); $status = SystemHealth::dbCheck($request);

View File

@ -319,6 +319,14 @@ class Invoice extends BaseModel
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps(); return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps();
} }
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<Payment>
*/
public function net_payments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->where('is_deleted',0)->withPivot('amount', 'refunded', 'deleted_at')->withTimestamps();
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<CompanyLedger> * @return \Illuminate\Database\Eloquent\Relations\MorphMany<CompanyLedger>
*/ */

View File

@ -135,7 +135,6 @@ class ActivityRepository extends BaseRepository
$design = Design::withTrashed()->find($entity_design_id); $design = Design::withTrashed()->find($entity_design_id);
if (! $entity->invitations()->exists() || ! $design) { if (! $entity->invitations()->exists() || ! $design) {
nlog("No invitations for entity {$entity->id} - {$entity->number}");
return ''; return '';
} }
@ -204,8 +203,6 @@ class ActivityRepository extends BaseRepository
$design = Design::withTrashed()->find($entity_design_id); $design = Design::withTrashed()->find($entity_design_id);
if (! $entity->invitations()->exists() || ! $design) { if (! $entity->invitations()->exists() || ! $design) {
nlog("No invitations for entity {$entity->id} - {$entity->number}");
return ''; return '';
} }

View File

@ -138,7 +138,7 @@ class ZugferdEInvoice extends AbstractService
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3); $this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3);
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate3); $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate3);
} else { } else {
nlog("Can't add correct tax position"); // nlog("Can't add correct tax position");
} }
} else { } else {
if (!empty($this->invoice->tax_name1)) { if (!empty($this->invoice->tax_name1)) {
@ -157,7 +157,7 @@ class ZugferdEInvoice extends AbstractService
$taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS; $taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', 0); $this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', 0);
$this->addtoTaxMap($taxtype, $linenetamount, 0); $this->addtoTaxMap($taxtype, $linenetamount, 0);
nlog("Can't add correct tax position"); // nlog("Can't add correct tax position");
} }
} }
} }

View File

@ -81,8 +81,6 @@ class HandleRestore extends AbstractService
Paymentable::query() Paymentable::query()
->withTrashed() ->withTrashed()
->where('payment_id', $payment->id) ->where('payment_id', $payment->id)
// ->where('paymentable_type', '=', 'invoices')
// ->where('paymentable_id', $this->invoice->id)
->update(['deleted_at' => null]); ->update(['deleted_at' => null]);
}); });
@ -93,11 +91,6 @@ class HandleRestore extends AbstractService
private function setAdjustmentAmount() private function setAdjustmentAmount()
{ {
foreach ($this->invoice->payments as $payment) { foreach ($this->invoice->payments as $payment) {
$this->adjustment_amount += $payment->paymentables
->where('paymentable_type', '=', 'invoices')
->where('paymentable_id', $this->invoice->id)
->sum('amount');
$this->adjustment_amount += $payment->paymentables $this->adjustment_amount += $payment->paymentables
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
->where('paymentable_id', $this->invoice->id) ->where('paymentable_id', $this->invoice->id)
@ -108,6 +101,7 @@ class HandleRestore extends AbstractService
->where('paymentable_type', '=', 'App\Models\Credit') ->where('paymentable_type', '=', 'App\Models\Credit')
->sum('amount'); ->sum('amount');
nlog("Adjustment amount: {$this->adjustment_amount}");
} }
$this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded'); $this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded');
@ -122,10 +116,12 @@ class HandleRestore extends AbstractService
if ($this->adjustment_amount == $this->total_payments) { if ($this->adjustment_amount == $this->total_payments) {
$this->invoice->payments()->update(['payments.deleted_at' => null, 'payments.is_deleted' => false]); $this->invoice->payments()->update(['payments.deleted_at' => null, 'payments.is_deleted' => false]);
} }
else
$this->invoice->net_payments()->update(['payments.deleted_at' => null, 'payments.is_deleted' => false]);
//adjust payments down by the amount applied to the invoice payment. //adjust payments down by the amount applied to the invoice payment.
$this->invoice->payments->fresh()->each(function ($payment) { $this->invoice->net_payments()->each(function ($payment) {
$payment_adjustment = $payment->paymentables $payment_adjustment = $payment->paymentables
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
->where('paymentable_id', $this->invoice->id) ->where('paymentable_id', $this->invoice->id)

View File

@ -76,14 +76,12 @@ class MarkInvoiceDeleted extends AbstractService
private function adjustPayments() private function adjustPayments()
{ {
//if total payments = adjustment amount - that means we need to delete the payments as well. //if total payments = adjustment amount - that means we need to delete the payments as well.
if ($this->adjustment_amount == $this->total_payments) { if ($this->adjustment_amount == $this->total_payments) {
$this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]); $this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]);
} }
//adjust payments down by the amount applied to the invoice payment. //adjust payments down by the amount applied to the invoice payment.
$this->invoice->payments->each(function ($payment) { $this->invoice->payments->each(function ($payment) {
$payment_adjustment = $payment->paymentables $payment_adjustment = $payment->paymentables
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')