Overview
Comment:Avoid duplicates when copying a service
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA3-256: a9cf5c62d0098de0fba9fc1a0f5bc3531a1ac95185f2cbf523722e2155f299c9
User & Date: bohwaz on 2021-12-22 19:57:36
Other Links: manifest | tags
Context
2021-12-23
00:34
Upgrade process: fix link to changelog, do an async check for latest version to not delay the loading of the config page check-in: 70b5f30cad user: bohwaz tags: trunk
2021-12-22
22:04
Merge with trunk check-in: 6a4f6f6abc user: bohwaz tags: templates
19:57
Avoid duplicates when copying a service check-in: a9cf5c62d0 user: bohwaz tags: trunk, stable
19:39
Fix link to subscribe service from user context check-in: c5cd56ee67 user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/Entities/Services/Service.php from [93f0e77858] to [bac627a89f].

130
131
132
133
134
135
136
137
138

139
140
141
142
	{
		$list = $this->paidUsersList();
		$conditions = sprintf('su.id_service = %d AND su.expiry_date < date() AND m.id_category NOT IN (SELECT id FROM users_categories WHERE hidden = 1)', $this->id());
		$list->setConditions($conditions);
		return $list;
	}

	public function getUsersIds(bool $paid_only = false) {
		$where = $paid_only ? 'AND paid = 1' : '';

		$sql = sprintf('SELECT id_user, id_user FROM services_users WHERE id_service = ? %s;', $where);
		return DB::getInstance()->getAssoc($sql, $this->id());
	}
}







|

>
|



130
131
132
133
134
135
136
137
138
139
140
141
142
143
	{
		$list = $this->paidUsersList();
		$conditions = sprintf('su.id_service = %d AND su.expiry_date < date() AND m.id_category NOT IN (SELECT id FROM users_categories WHERE hidden = 1)', $this->id());
		$list->setConditions($conditions);
		return $list;
	}

	public function getUsers(bool $paid_only = false) {
		$where = $paid_only ? 'AND paid = 1' : '';
		$id_field = Config::getInstance()->champ_identite;
		$sql = sprintf('SELECT su.id_user, u.%s FROM services_users su INNER JOIN membres u ON u.id = su.id_user WHERE su.id_service = ? %s;', $id_field, $where);
		return DB::getInstance()->getAssoc($sql, $this->id());
	}
}

Modified src/include/lib/Garradin/Entities/Services/Service_User.php from [976537b27f] to [9c410c5349].

41
42
43
44
45
46
47
48

49



















50
51
52
53
54
55
56
	protected $_service, $_fee;

	public function selfCheck(): void
	{
		$this->paid = (bool) $this->paid;
		$this->assert($this->id_service, 'Aucune activité spécifiée');
		$this->assert($this->id_user, 'Aucun membre spécifié');
		$this->assert($this->exists() || !DB::getInstance()->test(self::TABLE, 'id_user = ? AND id_service = ? AND date = ?', $this->id_user, $this->id_service, $this->date->format('Y-m-d')), 'Cette activité a déjà été enregistrée pour ce membre et cette date');

		$this->assert(!$this->exists() || !DB::getInstance()->test(self::TABLE, 'id_user = ? AND id_service = ? AND date = ? AND id != ?', $this->id_user, $this->id_service, $this->date->format('Y-m-d'), $this->id()), 'Cette activité a déjà été enregistrée pour ce membre et cette date');



















	}

	public function importForm(?array $source = null)
	{
		if (null === $source) {
			$source = $_POST;
		}







|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	protected $_service, $_fee;

	public function selfCheck(): void
	{
		$this->paid = (bool) $this->paid;
		$this->assert($this->id_service, 'Aucune activité spécifiée');
		$this->assert($this->id_user, 'Aucun membre spécifié');
		$this->assert(!$this->isDuplicate(), 'Cette activité a déjà été enregistrée pour ce membre et cette date');
	}

	public function isDuplicate(bool $using_date = true): bool
	{
		$params = [
			'id_user' => $this->id_user,
			'id_service' => $this->id_service,
		];

		if ($using_date) {
			$params['date'] = $this->date->format('Y-m-d');
		}

		if ($this->exists()) {
			$params['id'] = $this->id();
		}

		$where = array_map(fn($k) => sprintf('%s = ?', $k), array_keys($params));
		$where = implode(' AND ', $where);

		return DB::getInstance()->test(self::TABLE, $where, array_values($params));
	}

	public function importForm(?array $source = null)
	{
		if (null === $source) {
			$source = $_POST;
		}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161









162
163
164
165
166
167
168
		$transaction->importFromNewForm($source);
		$transaction->save();
		$transaction->linkToUser($this->id_user, $this->id());

		return $transaction;
	}

	static public function createFromForm(array $users, int $creator_id, ?array $source = null)
	{
		if (null === $source) {
			$source = $_POST;
		}

		$db = DB::getInstance();
		$db->begin();

		foreach ($users as $id) {
			$su = new self;
			$su->date = new \DateTime;
			$su->importForm($source);
			$su->id_user = (int) $id;

			if ($su->id_fee && $su->fee()->id_account && $su->id_user) {
				$su->expected_amount = $su->fee()->getAmountForUser($su->id_user);
			}










			$su->save();

			if ($su->id_fee && $su->fee()->id_account
				&& !empty($source['amount'])
				&& !empty($source['create_payment'])) {
				try {







|








|








>
>
>
>
>
>
>
>
>







157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		$transaction->importFromNewForm($source);
		$transaction->save();
		$transaction->linkToUser($this->id_user, $this->id());

		return $transaction;
	}

	static public function createFromForm(array $users, int $creator_id, bool $from_copy = false, ?array $source = null)
	{
		if (null === $source) {
			$source = $_POST;
		}

		$db = DB::getInstance();
		$db->begin();

		foreach ($users as $id => $name) {
			$su = new self;
			$su->date = new \DateTime;
			$su->importForm($source);
			$su->id_user = (int) $id;

			if ($su->id_fee && $su->fee()->id_account && $su->id_user) {
				$su->expected_amount = $su->fee()->getAmountForUser($su->id_user);
			}

			if ($su->isDuplicate($from_copy ? false : true)) {
				if ($from_copy) {
					continue;
				}
				else {
					throw new ValidationException(sprintf('%s : Cette activité a déjà été enregistrée pour ce membre et cette date', $name));
				}
			}

			$su->save();

			if ($su->id_fee && $su->fee()->id_account
				&& !empty($source['amount'])
				&& !empty($source['create_payment'])) {
				try {

Modified src/www/admin/services/user/subscribe.php from [a6566f023b] to [72bcfd51bb].

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$create = true;

// Only load the form if a user has been selected
require __DIR__ . '/_form.php';

$form->runIf(f('save') || f('save_and_add_payment'), function () use ($session, $users, $copy_service, $copy_service_only_paid) {
	if ($copy_service) {
		$users = $copy_service->getUsersIds($copy_service_only_paid);
	}
	else {
		$users = array_keys($users);
	}

	$su = Service_User::createFromForm($users, $session->getUser()->id);

	if (count($users) > 1) {
		$url = ADMIN_URL . 'services/details.php?id=' . $su->id_service;
	}
	elseif (f('save_and_add_payment')) {
		$url = ADMIN_URL . 'services/user/payment.php?id=' . $su->id;
	}







|

<
<
|
<
|







44
45
46
47
48
49
50
51
52


53

54
55
56
57
58
59
60
61
$create = true;

// Only load the form if a user has been selected
require __DIR__ . '/_form.php';

$form->runIf(f('save') || f('save_and_add_payment'), function () use ($session, $users, $copy_service, $copy_service_only_paid) {
	if ($copy_service) {
		$users = $copy_service->getUsers($copy_service_only_paid);
	}




	$su = Service_User::createFromForm($users, $session->getUser()->id, $copy_service ? true : false);

	if (count($users) > 1) {
		$url = ADMIN_URL . 'services/details.php?id=' . $su->id_service;
	}
	elseif (f('save_and_add_payment')) {
		$url = ADMIN_URL . 'services/user/payment.php?id=' . $su->id;
	}