2023-01-29 22:28:27 +08:00
|
|
|
# The Mia! Accounting Flask Project.
|
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/25
|
|
|
|
|
|
|
|
# Copyright (c) 2023 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""The console commands for the base account management.
|
|
|
|
|
|
|
|
"""
|
2023-02-08 10:25:29 +08:00
|
|
|
import csv
|
|
|
|
|
2023-01-29 22:28:27 +08:00
|
|
|
import click
|
|
|
|
from flask.cli import with_appcontext
|
|
|
|
|
2023-02-08 10:25:29 +08:00
|
|
|
from accounting import data_dir
|
2023-02-08 11:13:09 +08:00
|
|
|
from accounting import db
|
2023-02-01 15:44:58 +08:00
|
|
|
from accounting.models import BaseAccount, BaseAccountL10n
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
@click.command("accounting-init-base")
|
|
|
|
@with_appcontext
|
|
|
|
def init_base_accounts_command() -> None:
|
|
|
|
"""Initializes the base accounts."""
|
|
|
|
if BaseAccount.query.first() is not None:
|
|
|
|
click.echo("Base accounts already exist.")
|
|
|
|
raise click.Abort
|
|
|
|
|
2023-02-09 00:02:14 +08:00
|
|
|
with open(data_dir / "base_accounts.csv") as fp:
|
|
|
|
data: list[dict[str, str]] = [x for x in csv.DictReader(fp)]
|
2023-02-08 10:25:29 +08:00
|
|
|
account_data: list[dict[str, str]] = [{"code": x["code"],
|
|
|
|
"title_l10n": x["title"]}
|
|
|
|
for x in data]
|
|
|
|
locales: list[str] = [x[5:] for x in data[0] if x.startswith("l10n-")]
|
|
|
|
l10n_data: list[dict[str, str]] = [{"account_code": x["code"],
|
|
|
|
"locale": y,
|
|
|
|
"title": x[f"l10n-{y}"]}
|
|
|
|
for x in data for y in locales]
|
|
|
|
db.session.bulk_insert_mappings(BaseAccount, account_data)
|
|
|
|
db.session.bulk_insert_mappings(BaseAccountL10n, l10n_data)
|
2023-01-29 22:28:27 +08:00
|
|
|
db.session.commit()
|
|
|
|
click.echo("Base accounts initialized.")
|