Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function doReturn(Request $request, Response $response, int $id): Respons
$object = $service->getObject($id);

try {
$service->giveBack($object, (int)($post['status'] ?? 0));
$service->giveBack($object, (int)($post['status'] ?? 0), trim($post['comments'] ?? ''));
} catch (LendException $e) {
$this->flash->addMessage('error_detected', $e->getMessage());
return $response
Expand Down
15 changes: 14 additions & 1 deletion lib/GaletteObjectsLend/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function preferences(Request $request, Response $response): Response

$params = [
'page_title' => _T('ObjectsLend preferences', 'objectslend'),
'type_cotis_options' => $ctypes->getList(),
//a rent is not a membership fee
'type_cotis_options' => $ctypes->getList(false),
'lendsprefs' => (new LendPreferences($this->preferences))->toArray(),
'sample_data' => !(new SampleData($this->zdb))->hasObjects()
];
Expand Down Expand Up @@ -75,6 +76,18 @@ public function storePreferences(Request $request, Response $response): Response

$stored = true;
$errors = [];

//a membership fee would extend the membership, and needs an end date
$type_id = $post[LendPreferences::GENERATED_CONTRIBUTION_TYPE_ID] ?? null;
if (
isset($post[LendPreferences::AUTO_GENERATE_CONTRIBUTION])
&& !isset((new ContributionsTypes($this->zdb))->getList(false)[(int)$type_id])
) {
$stored = false;
$errors[] = _T("Generated contributions must be of a donation type.", "objectslend");
unset($post[LendPreferences::GENERATED_CONTRIBUTION_TYPE_ID]);
}

foreach (array_keys(LendPreferences::getSchema()) as $name) {
if (isset($booleans[$name])) {
$value = (int)isset($post[$name]);
Expand Down
4 changes: 2 additions & 2 deletions lib/GaletteObjectsLend/Entity/LendRent.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ public function getComments(): string
}

/**
* Set comments
* Set comments, cut to the column size
*
* @param string $comments Comments
*/
public function setComments(string $comments): self
{
$this->comments = $comments;
$this->comments = mb_substr($comments, 0, 200);
return $this;
}

Expand Down
21 changes: 18 additions & 3 deletions lib/GaletteObjectsLend/LendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ public function take(
*
* @param LendObject $object Object, loaded with getObject()
* @param int $status_id In stock status
* @param string $comments Comment on the closed rent
*
* @throws LendException
*/
public function giveBack(LendObject $object, int $status_id): LendRent
public function giveBack(LendObject $object, int $status_id, string $comments = ''): LendRent
{
if (!$this->canGiveBack($object)) {
$this->refuse(
Expand All @@ -214,7 +215,7 @@ public function giveBack(LendObject $object, int $status_id): LendRent
}

return $this->inTransaction(
fn() => $this->openRent($object, $status_id, null, '')
fn() => $this->openRent($object, $status_id, null, $comments)
);
}

Expand Down Expand Up @@ -378,6 +379,20 @@ private function storeContribution(
float $amount,
?int $payment_type
): Contribution {
//a membership fee would extend the membership, and needs an end date
$type_id = $this->lendsprefs->getContributionTypeId();
$ctype = new ContributionsTypes($this->zdb);
if (!$ctype->load($type_id) || $ctype->isExtension()) {
Analog::log(
'Unable to generate contribution for object #' . $object->getId()
. ': contribution type #' . $type_id . ' is not a donation one.',
Analog::ERROR
);
throw new LendException(
_T("Generated contributions must be of a donation type, check plugin preferences.", "objectslend")
);
}

$info = str_replace(
[
'{NAME}',
Expand All @@ -402,7 +417,7 @@ private function storeContribution(

$values = [
'montant_cotis' => $amount,
ContributionsTypes::PK => $this->lendsprefs->getContributionTypeId(),
ContributionsTypes::PK => $type_id,
'date_enreg' => date("Y-m-d"),
'date_debut_cotis' => date("Y-m-d"),
'type_paiement_cotis' => $payment_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,37 @@ public function testStoreInvalidPreference(): void
$this->flash_data = [];
}

/**
* Generated contributions cannot be membership fees
*/
public function testStoreMembershipFeeContributionType(): void
{
$this->logSuperAdmin();
$this->assertTrue($this->preferences->setValue(LendPreferences::GENERATED_CONTRIBUTION_TYPE_ID, 5, $this->login));

//only donation types are offered
$test_response = $this->app->handle($this->createRequest(route_name: 'objectslend_preferences'));
$body = (string)$test_response->getBody();
$this->assertStringContainsString('data-value="5"', $body);
$this->assertStringNotContainsString('data-value="1"', $body);

$request = $this->createRequest(route_name: 'store_objectlend_preferences', method: 'POST')
->withParsedBody([
LendPreferences::AUTO_GENERATE_CONTRIBUTION => '1',
LendPreferences::GENERATED_CONTRIBUTION_TYPE_ID => '1',
LendPreferences::THUMB_MAX_WIDTH => '210',
]);
$test_response = $this->app->handle($request);
$this->assertSame(302, $test_response->getStatusCode());
$this->expectFlashData(['error_detected' => ['Generated contributions must be of a donation type.']]);

$this->preferences->load();
$lendsprefs = new LendPreferences($this->preferences);
$this->assertSame(5, $lendsprefs->getContributionTypeId());
//other values are stored
$this->assertSame(210, $lendsprefs->getThumbWidth());
}

/**
* Preferences are for admins only
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,27 @@ public function testMemberReturnOwnObject(): void
$this->assertCount(2, $this->getRents());
}

/**
* Comment of the return form goes on the closed rent, cut to its column size
*/
public function testReturnComments(): void
{
$this->setPrefs(true);
$this->logSuperAdmin();
$this->lendObject($this->getMemberOne()->id);

$test_response = $this->app->handle($this->returnRequest(['comments' => ' Back, all good ']));
$this->assertSame(301, $test_response->getStatusCode());
$closed = array_values(array_filter($this->getRents(), fn($rent) => $rent->getDateEnd() !== ''));
$this->assertCount(1, $closed);
$this->assertSame('Back, all good', $closed[0]->getComments());

$this->lendObject($this->getMemberOne()->id);
$this->app->handle($this->returnRequest(['comments' => str_repeat('é', 250)]));
$comments = array_map(fn($rent) => $rent->getComments(), $this->getRents());
$this->assertContains(str_repeat('é', 200), $comments);
}

/**
* Giving back requires an "in stock" status, and a lent object
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/GaletteObjectsLend/tests/units/LendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,39 @@ public function testTakeRollsBackOnContributionError(): void
$this->assertTrue($service->isAvailable($object));
}

/**
* Test nothing is stored when generated contribution would be a membership fee
*/
public function testTakeRefusesMembershipFeeContributionType(): void
{
$this->assertTrue($this->preferences->setValue(LendPreferences::GENERATED_CONTRIBUTION_TYPE_ID, 1, $this->login));
$member = $this->getMemberOne();
$this->logSuperAdmin();
$service = $this->getService();

try {
$service->take(
$service->getObject($this->object_id),
$this->lent_status,
member_id: $member->id
);
$this->fail('Take should have failed');
} catch (LendException $e) {
$this->assertSame(
'Generated contributions must be of a donation type, check plugin preferences.',
$e->getMessage()
);
}
$this->expectLogEntry(
Analog::ERROR,
'Unable to generate contribution for object #' . $this->object_id . ': contribution type #1 is not a donation one.'
);

$this->assertSame(0, $this->countContributions());
$this->assertCount(0, (new \GaletteObjectsLend\Repository\Rents($this->zdb))->getForObject($this->object_id));
$this->assertTrue($service->isAvailable($service->getObject($this->object_id)));
}

/**
* Test status change refuses unknown and inactive statuses
*/
Expand Down
Loading