Overview
Comment:Suppression de la possibilité d'importer depuis Citizen
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA1: 620df5a2e5a76c4512b46ed93735d287ab8f55d2
User & Date: bohwaz on 2020-04-07 00:41:16
Other Links: manifest | tags
Context
2020-04-07
00:50
Import dans la bonne catégorie recette/dépense s'il y a une catégorie qui a le même nom dans l'autre type check-in: 34f324b0a2 user: bohwaz tags: trunk, stable
00:41
Suppression de la possibilité d'importer depuis Citizen check-in: 620df5a2e5 user: bohwaz tags: trunk, stable
00:36
Fix import comptable avec le nom du moyen de paiement à la place du code check-in: 7e67be9f3d user: bohwaz tags: trunk, stable
Changes

Modified src/include/lib/Garradin/Compta/Import.php from [936ab834d8] to [99fd308df7].

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
			}
			catch (UserException $e)
			{
				throw new UserException(sprintf('Ligne %s: %s', $line, $e->getMessage()));
			}
		}

		$db->commit();

		fclose($fp);
		return true;
	}

	public function fromCitizen($path)
	{
		if (!file_exists($path) || !is_readable($path))
		{
			throw new \RuntimeException('Fichier inconnu : '.$path);
		}

		$fp = fopen($path, 'r');

		if (!$fp)
		{
			return false;
		}

		$db = DB::getInstance();
		$db->begin();
		$comptes = new Comptes;
		$banques = new Comptes_Bancaires;
		$cats = new Categories;
		$journal = new Journal;

		$columns = [];
		$liste_comptes = $db->getAssoc('SELECT id, id FROM compta_comptes;');
		$liste_cats = $db->getAssoc('SELECT intitule, id FROM compta_categories;');
		$liste_moyens = $cats->listMoyensPaiement();

		$get_compte = function ($compte, $intitule) use (&$liste_comptes, &$comptes, &$banques)
		{
			if (substr($compte, 0, 2) == '51')
			{
				$compte = '512' . substr($compte, -1);
			}

			// Création comptes
			if (!array_key_exists($compte, $liste_comptes))
			{
				if (substr($compte, 0, 3) == '512')
				{
					$liste_comptes[$compte] = $banques->add([
						'libelle'	=>	$intitule,
						'banque'	=>	'Inconnue',
					]);
				}
				else
				{
					$liste_comptes[$compte] = $comptes->add([
						'id'		=>	$compte,
						'libelle'	=>	$intitule,
						'parent'	=>	substr($compte, 0, -1)
					]);
				}
			}

			return $compte;
		};

		$col = function($column) use (&$row, &$columns)
		{
			if (!isset($columns[$column]))
				return null;

			if (!isset($row[$columns[$column]]))
				return null;

			return $row[$columns[$column]];
		};

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
		Utils::skip_bom($fp);

		while (!feof($fp))
		{
			$row = fgetcsv($fp, 4096, $delim);
			$line++;

			if (empty($row))
			{
				continue;
			}

			if (empty($columns))
			{
				if (empty($row[0]))
				{
					throw new UserException(sprintf('Erreur sur la ligne %d : la ligne est vide ?', $line));
				}

				$columns = $row;
				$columns = array_flip($columns);
				continue;
			}

			$date = $col('Date');
			$date = \DateTime::createFromFormat('d/m/Y', $date);

			if (!$date)
			{
				$db->rollback();
				throw new UserException(sprintf('Erreur sur la ligne %d : la date "%s" n\'est pas au format jj/mm/aaaa.', $line, $col('Date')));
			}

			$date = $date->format('Y-m-d');

			if ($db->test('compta_exercices', '(? < debut OR ? > fin) AND cloture = 0', $date, $date))
			{
				continue;
			}

			$debit = $get_compte($col('Compte débité - Numéro'), $col('Compte débité - Intitulé'));
			$credit = $get_compte($col('Compte crédité - Numéro'), $col('Compte crédité - Intitulé'));

			$cat = $col('Rubrique');
			$moyen = strtoupper(substr($col('Moyen de paiement'), 0, 2));

			if (!$moyen || !array_key_exists($moyen, $liste_moyens))
			{
				$moyen = false;
				$cat = false;
			}

			if ($cat && !array_key_exists($cat, $liste_cats))
			{
				if ($col('Nature') == 'Recette')
				{
					$type = $cats::RECETTES;
					$compte = $credit;
				}
				elseif ($col('Nature') == 'Dépense')
				{
					$type = $cats::DEPENSES;
					$compte = $debit;
				}
				else
				{
					$type = $cats::AUTRES;
					$cat = false;
				}

				if ($type != $cats::AUTRES)
				{
					$liste_cats[$cat] = $cats->add([
						'intitule'	=>	$cat,
						'type'		=>	$type,
						'compte'	=>	$compte
					]);
				}
			}

			$data = [
				'libelle'       =>  $col('Libellé'),
				'montant'       =>  $col('Montant'),
				'date'          =>  $date,
				'compte_credit' =>  $credit,
				'compte_debit'  =>  $debit,
				'numero_piece'  =>  $col('Numéro de pièce'),
				'remarques'     =>  $col('Remarques'),
			];

			if ($cat)
			{
				$data['moyen_paiement']	=	$moyen;
				$data['numero_cheque']	=	$col('Numéro de chèque');
				$data['id_categorie']	=	$liste_cats[$cat];
			}

			$journal->add($data);
		}

		$db->commit();

		fclose($fp);
		return true;
	}
}












|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
235
236
237
238
239
240
241
242
243
244
245
246
247















































































































































































			}
			catch (UserException $e)
			{
				throw new UserException(sprintf('Ligne %s: %s', $line, $e->getMessage()));
			}
		}

		$db->commit();

		fclose($fp);
		return true;
	}
}















































































































































































Modified src/templates/admin/compta/import.tpl from [d8c04dcc34] to [5bf6707f89].

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                <label for="f_type">Export CSV de Garradin</label>
            </dd>
            <dd class="help">
                Export du journal comptable au format CSV provenant de Garradin.
                Les lignes comportant un numéro d'opération mettront à jour les opérations existantes,
                les lignes sans numéro créeront de nouvelles opérations.
            </dd>
            <dd>
                <input type="radio" name="type" id="f_type_citizen" value="citizen" {form_field name=type checked="citizen"} />
                <label for="f_type_citizen">Export CSV de Citizen Comptabilité</label>
            </dd>
            <dd class="help">
                Export des données au format CSV provenant du logiciel de comptabilité de
                <a href="http://www.citizenplace.com/">Citizen Place</a>.
            </dd>
            <dd class="help">
                Toutes les opérations du fichier seront créées dans l'exercice en cours.  Les catégories et comptes associés aux opérations seront automatiquement créés s'ils n'existent pas déjà.
            </dd>
        </dl>
    </fieldset>

    <p class="alert">
        Si le fichier comporte des opérations dont la date est en dehors de l'exercice courant,
        elles seront ignorées.
    </p>







<
<
<
<
<
<
<
<
<
<
<







27
28
29
30
31
32
33











34
35
36
37
38
39
40
                <label for="f_type">Export CSV de Garradin</label>
            </dd>
            <dd class="help">
                Export du journal comptable au format CSV provenant de Garradin.
                Les lignes comportant un numéro d'opération mettront à jour les opérations existantes,
                les lignes sans numéro créeront de nouvelles opérations.
            </dd>











        </dl>
    </fieldset>

    <p class="alert">
        Si le fichier comporte des opérations dont la date est en dehors de l'exercice courant,
        elles seront ignorées.
    </p>

Modified src/www/admin/compta/import.php from [d14bd1f9b9] to [2c5e1fbc3f].

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    exit;
}

if (f('import'))
{
    $form->check('compta_import', [
        'upload' => 'file|required',
        'type'   => 'required|in:citizen,garradin',
    ]);

    if (!$form->hasErrors())
    {
        try
        {
            if (f('type') == 'citizen')
            {
                $import->fromCitizen($_FILES['upload']['tmp_name']);
            }
            elseif (f('type') == 'garradin')
            {
                $import->fromCSV($_FILES['upload']['tmp_name']);
            }
            else
            {
                throw new UserException('Import inconnu.');
            }







|






<
<
<
<
|







19
20
21
22
23
24
25
26
27
28
29
30
31
32




33
34
35
36
37
38
39
40
    exit;
}

if (f('import'))
{
    $form->check('compta_import', [
        'upload' => 'file|required',
        'type'   => 'required|in:garradin',
    ]);

    if (!$form->hasErrors())
    {
        try
        {




            if (f('type') == 'garradin')
            {
                $import->fromCSV($_FILES['upload']['tmp_name']);
            }
            else
            {
                throw new UserException('Import inconnu.');
            }