Ajout du support des écritures ventilées.
[garradin.git] / www / admin / compta / operations / modifier.php
index 660b879..42f5f0b 100644 (file)
@@ -19,6 +19,8 @@ if (!$operation)
     throw new UserException("L'opération demandée n'existe pas.");
 }
 
+$debug = "_POST=".print_r($_POST, true);
+
 if ($operation['id_categorie'])
 {
     $categorie = $cats->get($operation['id_categorie']);
@@ -37,6 +39,13 @@ else
     $type = null;
 }
 
+foreach ($operation['fluxs'] as $key => &$flux)
+{
+    $compte = $comptes->get($flux['compte']);
+    $flux['compte_libelle'] = $compte['libelle'];
+}
+$tpl->assign('fluxs', $operation['fluxs']);
+
 $error = false;
 
 if (!empty($_POST['save']))
@@ -51,12 +60,33 @@ if (!empty($_POST['save']))
         {
             if (is_null($type))
             {
+                $fluxs = [];
+                $solde = 0.0;
+                for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++)
+                {
+                  $compte = $_POST['compte_'.$n];
+                  if (array_key_exists('montant_'.$n, $_POST)) {
+                      $montant = $_POST['montant_'.$n];
+                      if ($montant == 0.0)
+                      {
+                          continue;
+                      }
+                      else
+                      {
+                          $solde += $montant;
+                          $fluxs[] = ['compte' => $compte, 'montant' => $montant];
+                      }
+                  } else {
+                      if ($solde != 0.0 || $n < 1) {
+                          $fluxs[] = ['compte' => $compte, 'montant' => - $solde];
+                      }
+                      break;
+                  }
+                }
                 $journal->edit($operation['id'], [
                     'libelle'       =>  utils::post('libelle'),
-                    'montant'       =>  utils::post('montant'),
+                    'fluxs'         =>  $fluxs,
                     'date'          =>  utils::post('date'),
-                    'compte_credit' =>  utils::post('compte_credit'),
-                    'compte_debit'  =>  utils::post('compte_debit'),
                     'numero_piece'  =>  utils::post('numero_piece'),
                     'remarques'     =>  utils::post('remarques'),
                 ]);
@@ -109,12 +139,12 @@ if (!empty($_POST['save']))
 
                 $journal->edit($operation['id'], [
                     'libelle'       =>  utils::post('libelle'),
-                    'montant'       =>  utils::post('montant'),
                     'date'          =>  utils::post('date'),
+                    'fluxs'      =>
+                      [ ['compte'=>$credit, 'montant' => - utils::post('montant')]
+                      , ['compte'=>$debit,  'montant' =>   utils::post('montant')] ],
                     'moyen_paiement'=>  utils::post('moyen_paiement'),
                     'numero_cheque' =>  utils::post('numero_cheque'),
-                    'compte_credit' =>  $credit,
-                    'compte_debit'  =>  $debit,
                     'numero_piece'  =>  utils::post('numero_piece'),
                     'remarques'     =>  utils::post('remarques'),
                     'id_categorie'  =>  (int)$cat['id'],
@@ -145,8 +175,9 @@ else
     $tpl->assign('comptes_bancaires', $banques->getList());
 }
 
+$tpl->assign('debug', $debug);
 $tpl->assign('operation', $operation);
 
 $tpl->display('admin/compta/operations/modifier.tpl');
 
-?>
\ No newline at end of file
+?>