public static function apiQuery() { $query = self::query(); if (Auth::user()->isPublisherAdmin() || Auth::user()->isPublisher()) { $query->where(Advance::table() . '.company_id', '=', Auth::user()["company_id"]); } if (Auth::user()->isPayee()) { $query->where(Advance::table() . ".payee_code", '=', Auth::user()["code"]); } return $query; }
private function distributePayeePaymentToAdvancePayments() { $currentAdvance = Advance::where('payee_code', '=', $this->payee_code)->where('company_id', '=', $this->company_id)->where('status', '=', 'incomplete')->orderBy('start_date')->first(); if ($currentAdvance != null) { $amountLeft = $currentAdvance->amountLeftToPay(); $this->addAdvancePayment($currentAdvance, $this); if ($this->amount - $amountLeft <= 0) { $this->amount_paid = 0; } else { $this->amount_paid = $this->amount - $amountLeft; } $this->save(); } }
public function testMarkAsPaidWithMultipleAdvances2CompleteUnfixedAmount() { $this->resetEvents(); $user = User::create(self::userArray()); $advance1 = Advance::create(self::advanceArray()); $advance2 = Advance::create(self::advanceArray()); $advance3 = Advance::create(self::advanceArray()); $paymentDate = date("Y-m-d", strtotime("+ 5 days")); $payeePayment = PayeePayment::create(self::payeePaymentArray()); $royaltyPayment = self::royaltyPaymentsArray($payeePayment); $royaltyPayment["amount_received"] = 800; RoyaltyPayment::create($royaltyPayment); RoyaltyPayment::create($royaltyPayment); RoyaltyPayment::create($royaltyPayment); $payeePayment->markAsPaid($paymentDate); $this->assertTrue(count(AdvancePayment::all()) == 5); $this->assertTrue(count(AdvancePayment::where("advance_id", "=", $advance1->id)->get()) == 2); $this->assertTrue(count(AdvancePayment::where("advance_id", "=", $advance2->id)->get()) == 2); $this->assertTrue(count(AdvancePayment::where("advance_id", "=", $advance3->id)->get()) == 1); $advanceFound1 = Advance::find($advance1->id); $this->assertTrue($advanceFound1->status == "complete"); $advanceFound2 = Advance::find($advance2->id); $this->assertTrue($advanceFound2->status == "complete"); $advanceFound3 = Advance::find($advance3->id); $this->assertTrue($advanceFound3->status == "incomplete"); $this->assertTrue($advanceFound3->amountLeftToPay() == 600); $this->assertTrue(AdvancePayment::find(1)->amount == 800); $this->assertTrue(AdvancePayment::find(2)->amount == 200); $this->assertTrue(AdvancePayment::find(3)->amount == 600); $this->assertTrue(AdvancePayment::find(4)->amount == 400); $this->assertTrue(AdvancePayment::find(5)->amount == 400); $payeePayment1 = PayeePayment::find($payeePayment->id); $this->assertTrue($payeePayment1->status == "paid"); $this->resetEvents(); }