/** * Record Tithe * * @return bool * @throws \Exception */ private function recordTithe() { $user = User::find($this->requestor_id); $tithe = $user->tithe; if ($tithe) { $userProperty = $user->userProperties()->where('key', UserProperty::KEY_TITHE_PERCENTAGE)->first(); $userProperty->value = strval(floatval($userProperty->value) + UserProperty::KEY_TITHE_PERCENTAGE_INCREMENT_VALUE); if (!$userProperty->save()) { throw new \Exception('Error updating'); } } else { $tithe = new Tithe(); $tithe->status = Tithe::STATUS_STORED; } $tithe->user_id = $user->id; $tithe->setPoolAmount($this->amount); $tithe->setAmount(); if (!$tithe->save()) { throw new \Exception('Error saving/updating tithe!'); } return true; }
/** * Testing 1 2 3... * * @return void */ public function testAll() { $user = factory(User::class)->create(); $up = new UserProperty(); $up->user_id = $user->id; $up->key = UserProperty::KEY_TITHE_PERCENTAGE; $up->value = '10'; $this->assertTrue($up->save()); $pay = new Pay(); $pay->user_id = $user->id; $pay->amount = 5000; $this->assertTrue($pay->save()); $tithe = new Tithe(); $tithe->user_id = $user->id; $tithe->setPoolAmount($pay->amount); $tithe->setAmount(); $tithe->status = Tithe::STATUS_STORED; $this->assertTrue($tithe->save()); $tithe = Tithe::find($tithe->id); $this->assertNotNull($tithe); $this->assertEquals(5000, $tithe->pool_amount); $this->assertEquals(500, $tithe->amount); }