Version en CoffeeScript

 
prologue = ( titre ) ->
    console.time "Exécution"
    console.log "#{titre} : début de l'exécution à #{formatTime( new Date() )} du #{formatDate( new Date() )}\n"
    process.on "exit", ->
        console.log "\n#{titre} : fin de l'exécution"
        console.timeEnd "Exécution"
 
 
formatTime = ( date, separation = ":" ) ->
    hours = ( "0" + date.getHours() )[ -2..-1 ]
    minutes = ( "0" + date.getMinutes() )[ -2..-1 ]
    seconds = ( "0" + date.getSeconds() )[ -2..-1 ]
    "#{hours}#{separation}#{minutes}#{separation}#{seconds}"
 
 
formatDate = ( date, separation = "-" ) ->
    year = date.getFullYear()
    month = ( "0" + ( date.getMonth() + 1 ) )[ -2..-1 ]
    day = ( "0" + date.getDate() )[ -2..-1 ]
    "#{year}#{separation}#{month}#{separation}#{day}"
 
 
format = ( value, padLeft = 0, decimalSymbol = ",", groupSymbol = "\u00A0", spaceSymbol = "\u00A0", currencySymbol = "$" ) ->
    parts = Math.abs( value ).toFixed( 2 ).split "."
    units = splitUnits parts[ 0 ], groupSymbol
    cents = parts[ 1 ]
    result = if value < 0
        "(#{units}#{decimalSymbol}#{cents}#{currencySymbol})"
    else
        "#{units}#{decimalSymbol}#{cents}#{spaceSymbol}#{currencySymbol}"
    result = " " + result while result.length < padLeft
    result
 
 
splitUnits = ( units, groupSymbol ) ->
    postfix = ""
    while units.length > 3
        postfix = groupSymbol + units[-3..-1] + postfix
        units = units[0..-4]
    units += postfix
 
 
prologue "SIMULATEUR COMPTABLE"
 
# actif et dépenses = débit
# passif et revenus = crédit
 
ligneHorizontale = "________________________________________\n"
 
Type = {
    ACTIF: { debit: true, nom: "ACTIF      " }
    PASSIF: { debit: false, nom: "PASSIF     " }
    ACTIFS_NETS: { debit: false, nom: "ACTIFS NETS" }
    PRODUITS: { debit: false, nom: "PRODUITS   " }
    CHARGES: { debit: true, nom: "CHARGES    " }
}
 
comptes = {
    "11000": { type: Type.ACTIF, description: "Encaisse", solde: 0.00 }
    "12000": { type: Type.ACTIF, description: "Comptes clients", solde: 0.00 }
 
    "21000": { type: Type.PASSIF, description: "Taxes à payer", solde: 0.00 }
    "29000": { type: Type.PASSIF, description: "Primes perçues d'avance", solde: 0.00 }
 
    "41000": { type: Type.PRODUITS, description: "Primes", solde: 0.00 }
    "41500": { type: Type.PRODUITS, description: "Primes perçues d'avance", solde: 0.00 }
    "42000": { type: Type.PRODUITS, description: "Charges d'intérêts", solde: 0.00 }
 
    "51000": { type: Type.CHARGES, description: "Mauvaises créances", solde: 0.00 }
}
 
transactions = {
    "01":
        description: "Émission d'une police"
        imputations: { "41000": -2000.00, "21000": -180.00, "12000": 2180.00, }
    "02":
        description: "Annulation d'une police"
        imputations: { "41000": 2000.00, "21000": 180.00, "12000": -2180.00, }
    "03":
        description: "Émission d'une police"
        imputations: { "41000": -2000.00, "21000": -180.00, "12000": 2180.00, }
    "04":
        description: "Émission d'un avenant"
        imputations: { "41000": -1000.00, "21000": -90.00, "12000": 1090.00, }
    "05":
        description: "Paiement"
        imputations: { "11000": 1090.00, "12000": -1090.00, }
    "06":
        description: "Écriture de fin de période pour les primes non gagnées"
        imputations: { "41500": 1500.00, "29000": -1500.00, }
    "07":
        description: "Écriture de début de période pour les primes non gagnées"
        imputations: { "41500": -1500.00, "29000": 1500.00, }
    "08":
        description: "Radiation d'un compte à recevoir"
        imputations: { "12000": -1090.00, "51000": 1000.00, "21000": 90.00,}
}
 
formater = ( montant ) ->
    format montant, 14
 
 
imprimerBalanceVerification = ->
    console.log ligneHorizontale
    console.log "BALANCE DE VÉRIFICATION\n"
    total = 0.0
    for numero, compte of comptes
        total += compte.solde
        console.log "#{numero} #{formater(compte.solde)}  #{compte.type.nom}  #{compte.description}"
    console.log "\nTOTAL #{formater( total )}\n"
 
 
imprimerTotaliser = ( type ) ->
    console.log "#{type.nom} \n"
    total = 0.00
    for numero, compte of comptes when compte.type == type
        montant = if type.debit then compte.solde else - compte.solde
        total += montant
        console.log "#{numero} #{formater(montant)}  #{compte.description}"
    console.log "\nTOTAL #{formater( total )}\n"
    total
 
 
imprimerEtatResultats = ->
    console.log ligneHorizontale
    console.log "ÉTAT DES RÉSULTATS\n"
    total = imprimerTotaliser( Type.PRODUITS ) - imprimerTotaliser( Type.CHARGES )
    console.log "EXCÉDENT"
    console.log "\nTOTAL #{formater( total )}\n"
 
 
imprimerBilan = ->
    console.log ligneHorizontale
    console.log "BILAN\n"
    total = imprimerTotaliser( Type.ACTIF ) - imprimerTotaliser( Type.PASSIF )
    console.log "ACTIFS NETS"
    console.log "\nTOTAL #{formater( total )}\n"
 
 
imprimerEtatsFinanciers = ->
    imprimerEtatResultats()
    imprimerBilan()
 
 
imprimerTransaction = ( id ) ->
    transaction = transactions[ id ]
    console.log ligneHorizontale
    console.log "TRANSACTION : #{id} #{transaction.description}\n"
    total = 0.00
    for compte, montant of transaction.imputations
        total += montant
        console.log "#{compte} #{formater( montant )}  #{comptes[ compte ].description}"
    console.log "\nTOTAL #{formater( total )}\n"
 
 
traiterTransaction = ( id ) ->
    for compte, montant of transactions[ id ].imputations
        comptes[ compte ].solde += montant
 
 
traiterLot = ->
    for id of transactions
        imprimerTransaction id
        traiterTransaction id
        imprimerEtatsFinanciers()
 
 
imprimerBalanceVerification()
 
imprimerEtatsFinanciers()
 
traiterLot()
 
imprimerBalanceVerification()

Version en JavaScript

// Generated by CoffeeScript 1.4.0
var Type, comptes, format, formatDate, formatTime, formater,
    imprimerBalanceVerification, imprimerBilan, imprimerEtatResultats,
    imprimerEtatsFinanciers, imprimerTotaliser, imprimerTransaction,
    ligneHorizontale, prologue, splitUnits, traiterLot, traiterTransaction, transactions;
 
prologue = function(titre) {
  console.time("Exécution");
  console.log("" + titre + " : début de l'exécution à " + (formatTime(new Date())) + " du " + (formatDate(new Date())) + "\n");
  return process.on("exit", function() {
    console.log("\n" + titre + " : fin de l'exécution");
    return console.timeEnd("Exécution");
  });
};
 
formatTime = function(date, separation) {
  var hours, minutes, seconds;
  if (separation == null) {
    separation = ":";
  }
  hours = ("0" + date.getHours()).slice(-2);
  minutes = ("0" + date.getMinutes()).slice(-2);
  seconds = ("0" + date.getSeconds()).slice(-2);
  return "" + hours + separation + minutes + separation + seconds;
};
 
formatDate = function(date, separation) {
  var day, month, year;
  if (separation == null) {
    separation = "-";
  }
  year = date.getFullYear();
  month = ("0" + (date.getMonth() + 1)).slice(-2);
  day = ("0" + date.getDate()).slice(-2);
  return "" + year + separation + month + separation + day;
};
 
format = function(value, padLeft, decimalSymbol, groupSymbol, spaceSymbol, currencySymbol) {
  var cents, parts, result, units;
  if (padLeft == null) {
    padLeft = 0;
  }
  if (decimalSymbol == null) {
    decimalSymbol = ",";
  }
  if (groupSymbol == null) {
    groupSymbol = "\u00A0";
  }
  if (spaceSymbol == null) {
    spaceSymbol = "\u00A0";
  }
  if (currencySymbol == null) {
    currencySymbol = "$";
  }
  parts = Math.abs(value).toFixed(2).split(".");
  units = splitUnits(parts[0], groupSymbol);
  cents = parts[1];
  result = value < 0 ? "(" + units + decimalSymbol + cents + currencySymbol + ")" : "" + units + decimalSymbol + cents + spaceSymbol + currencySymbol;
  while (result.length < padLeft) {
    result = " " + result;
  }
  return result;
};
 
splitUnits = function(units, groupSymbol) {
  var postfix;
  postfix = "";
  while (units.length > 3) {
    postfix = groupSymbol + units.slice(-3) + postfix;
    units = units.slice(0, -3);
  }
  return units += postfix;
};
 
prologue("SIMULATEUR COMPTABLE");
 
ligneHorizontale = "________________________________________\n";
 
Type = {
  ACTIF: {
    debit: true,
    nom: "ACTIF      "
  },
  PASSIF: {
    debit: false,
    nom: "PASSIF     "
  },
  ACTIFS_NETS: {
    debit: false,
    nom: "ACTIFS NETS"
  },
  PRODUITS: {
    debit: false,
    nom: "PRODUITS   "
  },
  CHARGES: {
    debit: true,
    nom: "CHARGES    "
  }
};
 
comptes = {
  "11000": {
    type: Type.ACTIF,
    description: "Encaisse",
    solde: 0.00
  },
  "12000": {
    type: Type.ACTIF,
    description: "Comptes clients",
    solde: 0.00
  },
  "21000": {
    type: Type.PASSIF,
    description: "Taxes à payer",
    solde: 0.00
  },
  "29000": {
    type: Type.PASSIF,
    description: "Primes perçues d'avance",
    solde: 0.00
  },
  "41000": {
    type: Type.PRODUITS,
    description: "Primes",
    solde: 0.00
  },
  "41500": {
    type: Type.PRODUITS,
    description: "Primes perçues d'avance",
    solde: 0.00
  },
  "42000": {
    type: Type.PRODUITS,
    description: "Charges d'intérêts",
    solde: 0.00
  },
  "51000": {
    type: Type.CHARGES,
    description: "Mauvaises créances",
    solde: 0.00
  }
};
 
transactions = {
  "01": {
    description: "Émission d'une police",
    imputations: {
      "41000": -2000.00,
      "21000": -180.00,
      "12000": 2180.00
    }
  },
  "02": {
    description: "Annulation d'une police",
    imputations: {
      "41000": 2000.00,
      "21000": 180.00,
      "12000": -2180.00
    }
  },
  "03": {
    description: "Émission d'une police",
    imputations: {
      "41000": -2000.00,
      "21000": -180.00,
      "12000": 2180.00
    }
  },
  "04": {
    description: "Émission d'un avenant",
    imputations: {
      "41000": -1000.00,
      "21000": -90.00,
      "12000": 1090.00
    }
  },
  "05": {
    description: "Paiement",
    imputations: {
      "11000": 1090.00,
      "12000": -1090.00
    }
  },
  "06": {
    description: "Écriture de fin de période pour les primes non gagnées",
    imputations: {
      "41500": 1500.00,
      "29000": -1500.00
    }
  },
  "07": {
    description: "Écriture de début de période pour les primes non gagnées",
    imputations: {
      "41500": -1500.00,
      "29000": 1500.00
    }
  },
  "08": {
    description: "Radiation d'un compte à recevoir",
    imputations: {
      "12000": -1090.00,
      "51000": 1000.00,
      "21000": 90.00
    }
  }
};
 
formater = function(montant) {
  return format(montant, 14);
};
 
imprimerBalanceVerification = function() {
  var compte, numero, total;
  console.log(ligneHorizontale);
  console.log("BALANCE DE VÉRIFICATION\n");
  total = 0.0;
  for (numero in comptes) {
    compte = comptes[numero];
    total += compte.solde;
    console.log("" + numero + " " + (formater(compte.solde)) + "  " + compte.type.nom + "  " + compte.description);
  }
  return console.log("\nTOTAL " + (formater(total)) + "\n");
};
 
imprimerTotaliser = function(type) {
  var compte, montant, numero, total;
  console.log("" + type.nom + " \n");
  total = 0.00;
  for (numero in comptes) {
    compte = comptes[numero];
    if (!(compte.type === type)) {
      continue;
    }
    montant = type.debit ? compte.solde : -compte.solde;
    total += montant;
    console.log("" + numero + " " + (formater(montant)) + "  " + compte.description);
  }
  console.log("\nTOTAL " + (formater(total)) + "\n");
  return total;
};
 
imprimerEtatResultats = function() {
  var total;
  console.log(ligneHorizontale);
  console.log("ÉTAT DES RÉSULTATS\n");
  total = imprimerTotaliser(Type.PRODUITS) - imprimerTotaliser(Type.CHARGES);
  console.log("EXCÉDENT");
  return console.log("\nTOTAL " + (formater(total)) + "\n");
};
 
imprimerBilan = function() {
  var total;
  console.log(ligneHorizontale);
  console.log("BILAN\n");
  total = imprimerTotaliser(Type.ACTIF) - imprimerTotaliser(Type.PASSIF);
  console.log("ACTIFS NETS");
  return console.log("\nTOTAL " + (formater(total)) + "\n");
};
 
imprimerEtatsFinanciers = function() {
  imprimerEtatResultats();
  return imprimerBilan();
};
 
imprimerTransaction = function(id) {
  var compte, montant, total, transaction, _ref;
  transaction = transactions[id];
  console.log(ligneHorizontale);
  console.log("TRANSACTION : " + id + " " + transaction.description + "\n");
  total = 0.00;
  _ref = transaction.imputations;
  for (compte in _ref) {
    montant = _ref[compte];
    total += montant;
    console.log("" + compte + " " + (formater(montant)) + "  " + comptes[compte].description);
  }
  return console.log("\nTOTAL " + (formater(total)) + "\n");
};
 
traiterTransaction = function(id) {
  var compte, montant, _ref, _results;
  _ref = transactions[id].imputations;
  _results = [];
  for (compte in _ref) {
    montant = _ref[compte];
    _results.push(comptes[compte].solde += montant);
  }
  return _results;
};
 
traiterLot = function() {
  var id, _results;
  _results = [];
  for (id in transactions) {
    imprimerTransaction(id);
    traiterTransaction(id);
    _results.push(imprimerEtatsFinanciers());
  }
  return _results;
};
 
imprimerBalanceVerification();
 
imprimerEtatsFinanciers();
 
traiterLot();
 
imprimerBalanceVerification();