dhrnameのブログ

プログラミング関連を語るよ

SVGを拡張した言語を使った勘定科目の集計について

仕訳を集計できるSVGの拡張言語

 SVGXML文書なので、独自にタグを定義して、仕訳の簡単な集計ができるSVGベースの言語を作ってみた。とはいっても、貸借対照表は作ることができず、借方と貸方それぞれの勘定科目をJavaScrpitで合計して、テキストで表示できるだけである

 つまり、

  • SVGを拡張した言語
  • 仕訳の勘定科目を貸方と借方それぞれで集計

といったように、会計処理には使えず、簡易に科目ごとに加算していくだけの言語である

サンプル

 次のようなSVG文書(拡張子は.svg)となる

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink">
<title>2022年度 帳簿</title>
    <ac d="現金" c="売上">10000</ac>
    <ac d="水道光熱費" c="普通預金">16000</ac>
    <ac d="現金" c="売上">30000</ac>
    <ac d="仕入" c="現金">3000</ac>
    <ac d="水道光熱費" c="普通預金">9000<desc>ガス代</desc></ac>
<text id="debit" x="0" y="30">借方:</text>
<text id="credit" x="0" y="230">貸方:</text>
<script type="application/ecmascript"><![CDATA[
    /*勘定科目ごとに合計を算出するプログラム*/
    const eles = document.getElementsByTagNameNS("http://www.w3.org/2000/svg", "ac");
    const debit = {};
    const credit = {};
    /*使用されている全ての勘定科目をオブジェクトに記録*/
    const accountTitleDebit = {};
    const accountTitleCredit = {};
    for (let i=0;i<eles.length;++i) {
        let debitValue = eles[i].getAttributeNS(null, "d");
        let creditValue = eles[i].getAttributeNS(null, "c");
        let priceValue = eles[i].firstChild ? Number.parseInt(eles[i].firstChild.data) : 0;
        debit[debitValue] ?? (debit[debitValue] = 0);
        credit[creditValue] ?? (credit[creditValue] = 0);
        debit[debitValue] += priceValue;
        credit[creditValue] += priceValue;
        accountTitleDebit[debitValue] = accountTitleCredit[creditValue] = true;
    }
    
    const debtext = document.getElementById("debit");
    const cretext = document.getElementById("credit");
    
    /*借方の全ての勘定科目を数え上げる*/
    console.log("借方");
    Object.keys(accountTitleDebit).forEach(function(title) {
        debtext.firstChild.appendData(title+debit[title]);
    });
    /*貸方の全ての勘定科目を数え上げる*/
    console.log("貸方");
    Object.keys(accountTitleCredit).forEach(function(title) {
        cretext.firstChild.appendData(title+credit[title]);
    });
]]></script>
</svg>

使い方

 サンプルで示した文書をテキストエディタ―(Windowsだとメモ帳などでよい)で編集して、適当にhoge.svgというファイル名を名付け、ブラウザで読み込むとよい

 集計結果がブラウザに表示されるはずである。私の環境では(Win11, Firefox 118.0.1)、次のような文字が表示された。

借方:現金40000水道光熱費25000仕入3000

貸方:売上40000普通預金25000現金3000

 会計処理には使えないが、家計簿なら使えるだろう