0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

原価計算をPythonで自動化するプログラム

Posted at

組別総合原価計算で、月末仕掛品原価と完成品原価を求める計算をPythonで自動化してみました。先入先出法で計算しております。

本プロジェクトでは以下の参考書を使用しました。
『いちばんわかる 日商簿記2級 工業簿記の教科書』(サンクチュアリ出版)

計算のプロセス

1.加工費の配賦

ワルサーPPKの間接費(加工費)の振り分け

.32ACP弾仕様:1,500,000円×700時間/1600時間=656,250円
.380ACP弾仕様:1,500,000円×900時間/1600時間=843,750円

2.ワルサーPPKの月末仕掛品及び完成品原価の計算

.32ACP弾仕様の場合

月末仕掛品直接材料費:当月投入組直接費(直接材料費)2,000,000円×月末仕掛品数量600個/当月投入数量2500個=480,000円

月末仕掛品加工費:当月投入組間接費(加工費)656,250×月末仕掛品加工換算量540個(月末仕掛品数量600個×進捗率90%)/当月投入加工換算量2860個=123,907円

月末仕掛品原価:月末仕掛品直接材料費480,000円+月末仕掛品加工費123,907円=603,907円

当月製造費用合計:当月投入組直接費(直接材料費)2,000,000円+当月投入組間接費(加工費)656,250円=2,656,250円

完成品原価:(月初仕掛品原価600,000円+当月製造費用合計2,656,250円)−月末仕掛品原価603,907円=2,652,343円

.380ACP弾仕様の場合

月末仕掛品直接材料費:当月投入組直接費(直接材料費)2,200,000円×月末仕掛品数量500個/当月投入数量2300個=478,261円(四捨五入してあります)

月末仕掛品加工費:当月投入組間接費(加工費)843,750円×月末仕掛品加工換算量150個(月末仕掛品数量600個×進捗率30%)/当月投入加工換算量2250個=56,250円

月末仕掛品原価:月末仕掛品直接材料費478,261円+月末仕掛品加工費56,250円=534,511円

当月製造費用合計:当月投入組直接費(直接材料費)2,200,000円+当月投入組間接費(加工費)843,750円=3,043,750円

完成品原価:(月初仕掛品750,000円+当月製造費用合計3,043,750円)−月末仕掛品原価534,511円=3,259,239円

ソースコードの中にExcelシートの番地がございます。こちらの画像を参考にしてください。
新規メモ.jpeg

ソースコードを.32ACP弾と.380ACP弾に分けて載せます。

.32ACP弾の場合

#.32ACP弾仕様

import pandas as pd
from openpyxl import load_workbook

def get_cell_value(sheet, cell, default=0):
    """
    Excelのセルから値を取得する。Noneの場合はデフォルト値を返す。
    例外処理を行い、数値以外の値が入っている場合はエラーを回避する。
    """
    try:
        value = sheet[cell].value
        if value is None:
            return default  # デフォルト値を返す
        if isinstance(value, (int, float)):  
            return value  # 数値ならそのまま返す
        return float(value)  # 文字列の場合は数値に変換
    except ValueError:
        print(f"セル {cell} の値が不正です: {sheet[cell].value}")
        return default  # 不正な値ならデフォルト値を返す

# Excelファイルの情報
excel_file = "原価計算ポートフォリオ.xlsx"
sheet_name = "組別総合原価計算"

# Excelファイルを読み込む(エラーハンドリング付き)
try:
    wb = load_workbook(excel_file, data_only=True)
    ws = wb[sheet_name]
except FileNotFoundError:
    print(f"エラー: ファイル '{excel_file}' が見つかりません。")
    exit(1)
except KeyError:
    print(f"エラー: シート '{sheet_name}' が見つかりません。")
    exit(1)


# Excelからデータを取得(セル番地を指定)
beginning_work_in_process = ws["B2"].value # 月初仕掛品数量
total_units = ws["B3"].value # 当月投入数量
ending_wip_units = ws["B4"].value # 月末仕掛品数量
finished_product_quantity = ws["B5"].value #完成品数量

beginning_total_indirect_cost = ws["B7"].value  #月初仕掛品加工換算量
total_equivalent_units = ws["B8"].value  # 当月投入加工換算量
ending_wip_equivalent_units = ws["B9"].value  # 月末仕掛品加工換算量

beginning_material_cost = ws["B12"].value  # 月初仕掛品直接材料費
beginning_processing_cost = ws["B13"].value # 月初仕掛品直接加工費
total_material_cost = ws["B14"].value  # 当月投入組直接費(直接材料費)
total_processing_cost = ws["B15"].value  # 当月投入組間接費(加工費)

allocation_hours = ws["B16"].value  # 配賦時間
total_hours = ws["D16"].value  # 全体の時間

#計算

# 1.加工費の配賦

#ワルサーPPKの間接費(加工費)の振り分け

#当月投入組直接費(直接材料費)* 配賦時間/全体の時間
wip_processing_cost = total_processing_cost * allocation_hours / total_hours  

# 2.ワルサーPPKの月末仕掛品及び完成品原価の計算

#月末仕掛品直接材料費 = 当月投入組直接費(直接材料費)×月末仕掛品数量/当月投入数量
ending_material_cost = total_material_cost * ending_wip_units / total_units 

#月末仕掛品加工費 = 当月投入組間接費(加工費)×月末仕掛品加工換算量(月末仕掛品数量×進捗率)/当月投入加工換算量
ending_processing_cost = wip_processing_cost * ending_wip_equivalent_units / total_equivalent_units

#月末仕掛品原価 = 月末仕掛品直接材料費+月末仕掛品加工費
ending_wip_cost = ending_material_cost + ending_processing_cost

#当月製造費用合計 = 当月投入組直接費(直接材料費) + 当月投入組間接費(加工費)
total_manufacturing_cost = total_material_cost + wip_processing_cost

#完成品原価 = (月初仕掛品直接材料費 + 月初仕掛品直接加工費) + 当月製造費用合計 - 月末仕掛品原価
finished_goods_cost = (beginning_material_cost + beginning_processing_cost) + total_manufacturing_cost - ending_wip_cost

# 結果をExcelに書き込む
cell_ending_wip_cost = "B19"
cell_finished_goods_cost = "B20"

try:
    ws[cell_ending_wip_cost] = ending_wip_cost
    ws[cell_finished_goods_cost] = finished_goods_cost
    wb.save(excel_file)
    print(f"計算結果をExcelファイル '{excel_file}' の {cell_ending_wip_cost} と {cell_finished_goods_cost} に保存しました。")
except Exception as e:
    print(f"エラー: Excelファイルへの書き込みに失敗しました: {e}")

# 結果出力
print(f"月末仕掛品原価: {ending_wip_cost:.2f}円")
print(f"完成品原価: {finished_goods_cost:.2f}円")

.380ACP弾の場合


#.380ACP弾

import pandas as pd
from openpyxl import load_workbook

def get_cell_value(sheet, cell, default=0):
    """
    Excelのセルから値を取得する。Noneの場合はデフォルト値を返す。
    例外処理を行い、数値以外の値が入っている場合はエラーを回避する。
    """
    try:
        value = sheet[cell].value
        if value is None:
            return default  # デフォルト値を返す
        if isinstance(value, (int, float)):  
            return value  # 数値ならそのまま返す
        return float(value)  # 文字列の場合は数値に変換
    except ValueError:
        print(f"セル {cell} の値が不正です: {sheet[cell].value}")
        return default  # 不正な値ならデフォルト値を返す

# Excelファイルの情報
excel_file = "原価計算ポートフォリオ.xlsx"
sheet_name = "組別総合原価計算"

# Excelファイルを読み込む(エラーハンドリング付き)
try:
    wb = load_workbook(excel_file, data_only=True)
    ws = wb[sheet_name]
except FileNotFoundError:
    print(f"エラー: ファイル '{excel_file}' が見つかりません。")
    exit(1)
except KeyError:
    print(f"エラー: シート '{sheet_name}' が見つかりません。")
    exit(1)


# Excelからデータを取得(セル番地を指定)
beginning_work_in_process = ws["C2"].value # 月初仕掛品数量
total_units = ws["C3"].value # 当月投入数量
ending_wip_units = ws["C4"].value # 月末仕掛品数量
finished_product_quantity = ws["C5"].value #完成品数量

beginning_total_indirect_cost = ws["C7"].value  #月初仕掛品加工換算量
total_equivalent_units = ws["C8"].value  # 当月投入加工換算量
ending_wip_equivalent_units = ws["C9"].value  # 月末仕掛品加工換算量

beginning_material_cost = ws["C12"].value  # 月初仕掛品直接材料費
beginning_processing_cost = ws["C13"].value # 月初仕掛品加工費
total_material_cost = ws["C14"].value  # 当月投入組直接費(直接材料費)
total_processing_cost = ws["C15"].value  # 当月投入組間接費(加工費)

allocation_hours = ws["C16"].value  # 配賦時間
total_hours = ws["D16"].value  # 全体の時間


#計算

# 1.加工費の配賦

#ワルサーPPKの間接費(加工費)の振り分け

#当月投入組直接費(直接材料費)* 配賦時間/全体の時間
wip_processing_cost = total_processing_cost * allocation_hours / total_hours  

# 2.ワルサーPPKの月末仕掛品及び完成品原価の計算

#月末仕掛品直接材料費 = 当月投入組直接費(直接材料費)×月末仕掛品数量/当月投入数量
ending_material_cost = total_material_cost * ending_wip_units / total_units 

#月末仕掛品加工費 = 当月投入組間接費(加工費)×月末仕掛品加工換算量(月末仕掛品数量×進捗率)/当月投入加工換算量
ending_processing_cost = wip_processing_cost * ending_wip_equivalent_units / total_equivalent_units

#月末仕掛品原価 = 月末仕掛品直接材料費+月末仕掛品加工費
ending_wip_cost = ending_material_cost + ending_processing_cost

#当月製造費用合計 = 当月投入組直接費(直接材料費) + 当月投入組間接費(加工費)
total_manufacturing_cost = total_material_cost + wip_processing_cost

#完成品原価 = (月初仕掛品直接材料費 + 月初仕掛品直接加工費) + 当月製造費用合計 - 月末仕掛品原価
finished_goods_cost = (beginning_material_cost + beginning_processing_cost) + total_manufacturing_cost - ending_wip_cost

# 結果をExcelに書き込む
cell_ending_wip_cost = "B27"
cell_finished_goods_cost = "B28"

try:
    ws[cell_ending_wip_cost] = ending_wip_cost
    ws[cell_finished_goods_cost] = finished_goods_cost
    wb.save(excel_file)
    print(f"計算結果をExcelファイル '{excel_file}' の {cell_ending_wip_cost} と {cell_finished_goods_cost} に保存しました。")
except Exception as e:
    print(f"エラー: Excelファイルへの書き込みに失敗しました: {e}")

# 結果出力
print(f"月末仕掛品原価: {ending_wip_cost:.2f}円")
print(f"完成品原価: {finished_goods_cost:.2f}円")

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?