diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index 321f0aafc428..6063a84d84ad 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -126,13 +126,12 @@ class MatchBankTransactions implements ShouldQueue { $collection = collect(); + /** @array $invoices */ $invoices = explode(",", $invoice_hashed_ids); - if (count($invoices) >= 1) { - foreach ($invoices as $invoice) { - if (is_string($invoice) && strlen($invoice) > 1) { - $collection->push($this->decodePrimaryKey($invoice)); - } + foreach ($invoices as $invoice) { + if (is_string($invoice) && strlen($invoice) > 1) { + $collection->push($this->decodePrimaryKey($invoice)); } } @@ -189,7 +188,7 @@ class MatchBankTransactions implements ShouldQueue private function coalesceExpenses($expense): string { - if (!$this->bt->expense_id || strlen($this->bt->expense_id) < 1) { + if (!$this->bt->expense_id || strlen($this->bt->expense_id ?? '') < 2) { return $expense; } @@ -233,11 +232,12 @@ class MatchBankTransactions implements ShouldQueue $_invoices = Invoice::query() ->withTrashed() ->where('company_id', $this->bt->company_id) - ->whereIn('id', $this->getInvoices($input['invoice_ids'])); + ->whereIn('id', $this->getInvoices($input['invoice_ids'])) + ->get(); $amount = $this->bt->amount; - if ($_invoices && $this->checkPayable($_invoices)) { + if ($_invoices->count() >0 && $this->checkPayable($_invoices)) { $this->createPayment($_invoices, $amount); $this->bts->push($this->bt->id); @@ -323,6 +323,7 @@ class MatchBankTransactions implements ShouldQueue }); }, 2); + // @phpstan-ignore-next-line if (!$this->invoice) { return; } @@ -355,7 +356,7 @@ class MatchBankTransactions implements ShouldQueue $this->setExchangeRate($payment); /* Create a payment relationship to the invoice entity */ - foreach ($this->attachable_invoices as $attachable_invoice) { + foreach ($this->attachable_invoices as $attachable_invoice) { // @phpstan-ignore-line $payment->invoices()->attach($attachable_invoice['id'], [ 'amount' => $attachable_invoice['amount'], ]); diff --git a/tests/Feature/PurchaseOrderTest.php b/tests/Feature/PurchaseOrderTest.php index 893969026645..4f1a4b6bf202 100644 --- a/tests/Feature/PurchaseOrderTest.php +++ b/tests/Feature/PurchaseOrderTest.php @@ -11,19 +11,20 @@ namespace Tests\Feature; +use Tests\TestCase; +use App\Utils\Ninja; +use App\Models\Activity; +use Tests\MockAccountData; +use Illuminate\Support\Str; +use App\Models\PurchaseOrder; +use App\Utils\Traits\MakesHash; +use App\Models\PurchaseOrderInvitation; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Session; +use App\Repositories\ActivityRepository; use App\Events\PurchaseOrder\PurchaseOrderWasCreated; use App\Events\PurchaseOrder\PurchaseOrderWasUpdated; -use App\Models\Activity; -use App\Models\PurchaseOrder; -use App\Repositories\ActivityRepository; -use App\Utils\Ninja; -use App\Utils\Traits\MakesHash; -use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Support\Facades\Session; -use Illuminate\Support\Str; -use Tests\MockAccountData; -use Tests\TestCase; class PurchaseOrderTest extends TestCase { @@ -97,24 +98,39 @@ class PurchaseOrderTest extends TestCase public function testPurchaseOrderBulkActions() { - $this->purchase_order->service()->createInvitations()->save(); - $i = $this->purchase_order->invitations->first(); + $po = PurchaseOrder::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'vendor_id' => $this->vendor->id, + ]); - // $data = [ - // 'ids' => [$this->purchase_order->hashed_id], - // 'action' => 'download', - // ]; + // PurchaseOrderInvitation::factory()->create([ + // 'user_id' => $this->user->id, + // 'company_id' => $this->company->id, + // 'vendor_contact_id' => $this->vendor->contacts()->first()->id, + // 'purchase_order_id' => $po->id, + // ]); - // $response = $this->withHeaders([ - // 'X-API-SECRET' => config('ninja.api_secret'), - // 'X-API-TOKEN' => $this->token, - // ])->post("/api/v1/purchase_orders/bulk", $data) - // ->assertStatus(200); + + $po->service()->createInvitations()->save(); + + $i = $po->invitations->first(); + + $data = [ + 'ids' => [$po->hashed_id], + 'action' => 'download', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post("/api/v1/purchase_orders/bulk", $data) + ->assertStatus(200); $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => 'archive', ]; @@ -125,7 +141,7 @@ class PurchaseOrderTest extends TestCase ->assertStatus(200); $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => 'restore', ]; @@ -136,7 +152,7 @@ class PurchaseOrderTest extends TestCase ->assertStatus(200); $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => 'delete', ]; @@ -148,7 +164,7 @@ class PurchaseOrderTest extends TestCase $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => 'restore', ]; @@ -171,7 +187,7 @@ class PurchaseOrderTest extends TestCase ->assertStatus(302); $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => '', ]; @@ -183,7 +199,7 @@ class PurchaseOrderTest extends TestCase $data = [ - 'ids' =>[$this->purchase_order->hashed_id], + 'ids' =>[$po->hashed_id], 'action' => 'molly', ];