制造企业产品成本核算是成长控制、成本决策、销售决策的基础。如何快速、准确地完成成本核算,是企业应用中的高级部分,是在存货管理已经正常进行,已经做到账实一致、及时反映、信息完善的前提下才能进行的,还要有及时的会计总账核算结果(期间费用等),在充分 满足一些基础条件后,就可以开展成长核算的业务处理了。本例基于存货总线概念,是整套ERP系统中的一个组成部分,核算结果可直接用于成本控制和成本决策。本文讲述业务逻辑及后台编程。
一、确定成本核算对象:成本核算单
1、成本核算单需要填制、审核、登记。成本核算单是成本核算目标对象,可以直接填制所有成本核算单,不必等到前面的核算完成再填制后面的。
2、成本核算单主要核算对象包括:外协物料、半成品物料、产成品物料。
3、本例分为四个阶段:外协品、初级半成品(机箱)、二级半成品(祼机)、产成品。
4、其中,外协品只分配发出的材料(直接材料)。不分配人工及制造费用。
5、初级半成品分配直接材料、直接人工、制造费用(钣金车间)。
6、二级半成品分配直接材料、直接人工、制造费用(组装车间)。
7、产成品分配直接材料、直接人工、制造费用(检测车间)、管理费用。
8、本例共填制四张成本核算单,来自于委外入库数据和三个车间的完工入库数据。
9、产品入库时,已经按计划价格入库,因此在查询时是可以看到价格的,成本核算时将替换原有一价格,核算不正确时,可以重复进行,直到正确为止。
10、下面内容是要边归集边分配边核算。即必需是前面的分配并核算完成后,再分配核算后面的,因为后面的归集数据来自于前面的分配核算结果。成本核算单可一次全部填制完成,归集单要等到上一步核算完成后才能填制。
11、会计核算要将制造费用内容核算完毕。
二、成本归集与核算:成本归集单
成本归集、分配、核算要按产品层次依次进行,实际应用中,根据真实的层次确定核算的层次,就是说要前一层次核算产生结果后,用于后面层次的计算。
(一)、外协品成本核算
1、成本归集,只有一张单据,来源是委外出库单数据。填制、审核、登记。
2、成本分配(业务处理)。成本分配的结果被存储在djcbgjs表中,并在单据头中加上是否分配的标记。
3、成本核算。将分配结果更新到成本核算单,再由成本核算单更新到入库单。成本核算时自动存货核算,不需要单独操作。
4、成本分配与成本核算可以重复进行。
5、成本归集单允许反登记。
6、成本核算单允许反登记。反登记将清除原有分配的结果。但更新到入库单上的入库金额将无法恢复,直到重新计算出正确的结果后再更新。当然可以在后台用语句或建立业务处理功能,将其他恢复为计划成本价格。
7、使用成本账项查询功能,查询并核对核算结果的正确性。
8、查询核算后的入库单。
9、查询存货账项。
(二)、初级半成品成本核算
1、核算钣金车间生产的机箱的入库成本。
2、成本归集:直接材料、直接人工、制造费用。审核登记。
3、需要先行在会计核算中核算制造费用,因此我们回到会计核算。
4、成本分配。
5、成本核算。
6、单据查询:成本归集单、成本核算单。
7、账项查询:成本类账项。核实分配结果的正确性。
(三)、次级半成品成本核算
1、核算组装车间生产的祼机的入库成本。
2、成本归集:直接材料、直接人工、制造费用。审核登记。
3、成本分配。
4、成本核算。
5、单据查询:成本归集单、成本核算单。
6、账项查询:成本类账项。核实分配结果的正确性。
(四)产成品成本核算
1、核算检测车间生产的产成品的入库成本。
2、成本归集:直接材料、直接人工、制造费用、管理费用。审核登记。
3、成本分配。
4、成本核算。
5、单据查询:成本归集单、成本核算单。
三、成本核算原则
1、决策导向。分配标准以不影响销售决策为准。即售价较高的产品可以多摊成本,反之少摊成本。
2、订单决策。以订单决策为目标,而不是单一产品决策。即只要某一订单满足毛利指标要求即可,不求其中每一种产品都是满足要求的。
3、及时完成。在没有实际成本时,以标准成本或计划成本为依据完成计算。
4、本期完工的成本核算对象承担间接成本和期间费用。
5、完全成本。期间费用分配到产成品成本,不分配给半成品。
四、主要数据表结构
-- 成本核算单单据头 djcbhso字段列表: oid varchar 30 单据号 textread djmc varchar 60 单据名称 textread ny bpchar 6 年月 textread dqbz varchar 40 当前步骤 textread rq date 0 日期 dateedit yyyy-MM-dd scbm varchar 100 生产部门 textedit hslb varchar 40 核算类别 comboedit edit bz varchar 300 备注 textedit tzr varchar 20 填制人 textedit shr varchar 20 审核人 textedit djr varchar 20 登记人 textedit sfhs bool 0 是否核算 checkedit -- 成本核算单单据体,存储成本核算对象和核算结果 djcbhsm字段列表: mid int4 32 序号 textread oid varchar 30 单据号 textread djmc varchar 60 单据名称 textread ny bpchar 6 年月 textread dqbz varchar 40 当前步骤 textread chxh varchar 20 货号 buttonedit chdh varchar 30 存货代号 notenter chmc varchar 100 存货名称 notenter xh1 varchar 100 型号 textread xh2 varchar 100 规格 textread xh3 varchar 400 参数 textedit gg1 varchar 200 颜色 textedit gg2 varchar 200 材质 textread gg3 varchar 200 图号 textread jldw varchar 20 单位 notenter scbm varchar 100 生产部门 textedit wldw varchar 100 往来单位 textedit hssl numeric 16 核算数量 textedit n2 xsje numeric 16 销售金额 textedit n2 dwcb numeric 16 单位成本 textedit n2 cpcb numeric 16 产品成本 textedit n2 dwml numeric 16 单位毛利 textedit n2 cpml numeric 16 产品毛利 textedit n2 bz varchar 100 备注 textedit mhh int4 32 行号 textedit zjcl numeric 16 直接材料 textedit n2 zjrg numeric 16 直接人工 textedit n2 jjcl numeric 16 间接材料 textedit n2 jjrg numeric 16 间接人工 textedit n2 zzfy numeric 16 制造费用 textedit n2 glfy numeric 16 管理费用 textedit n2 sfsh bool 0 是否审核 checkedit sfdj bool 0 是否登记 checkedit -- 成本归集单单据头 djcbgjo字段列表: oid varchar 30 单据号 textread djmc varchar 60 单据名称 textread ny bpchar 8 年月 textread dqbz varchar 40 当前步骤 textread rq date 0 日期 dateedit yyyy-MM-dd scbm varchar 100 生产部门 textedit xmdh varchar 12 项目代号 buttonedit xmmc varchar 100 项目名称 textread xmlx varchar 60 项目类型 comboedit fpfw varchar 500 分配范围 buttontextedit bz varchar 300 备注 textedit tzr varchar 20 填制人 textedit shr varchar 20 审核人 textedit djr varchar 20 登记人 textedit sffp bool 0 是否分配 checkedit -- 成本归集单单据体 djcbgjm字段列表: mid int4 32 序号 textread oid varchar 30 单据号 textread djmc varchar 60 单据名称 textread ny bpchar 6 年月 textread dqbz varchar 40 当前步骤 textread cbmc varchar 150 成本名称 buttonedit jldw varchar 12 单位 textread select gjsl numeric 16 归集数量 textedit n2 cbdj numeric 16 成本单价 textedit n2 cbje numeric 16 成本金额 textedit n2 fpje numeric 16 分配金额 textedit n2 cbyt varchar 20 成本用途 textedit 00000 scbm varchar 100 生产部门 listedit wldw varchar 200 往来单位 textedit lyxh varchar 20 来源货号 textedit bz varchar 100 备注 textedit mhh int4 32 行号 textedit sfsh bool 0 是否审核 checkedit sfdj bool 0 是否登记 checkedit sffp bool 0 是否分配 checkedit -- 成本归集单分配结果 djcbgjs字段列表: id int4 32 键号 textedit ny bpchar 6 年月 textread djmc varchar 60 单据名称 textread oid varchar 30 单据号 textread mid int4 32 序号 textread cbmc varchar 100 成本名称 textedit jldw varchar 12 单位 buttonedit gjsl numeric 16 归集数量 textedit n2 cbdj numeric 16 成本单价 textedit n2 cbje numeric 16 成本金额 textedit n2 cbyt varchar 20 成本用途 textedit hsoid varchar 30 textedit bz varchar 100 备注 textedit xmdh varchar 12 项目代号 buttonedit xmmc varchar 100 项目名称 textread xmlx varchar 20 项目类型 comboedit scbm varchar 100 生产部门 listedit wldw varchar 200 往来单位 buttonedit sfdj bool 0 是否登记 checkread
五、关键代码
本例使用plpgsql语言编写。
(一)、成本分配
将归集好的成本,分配到具体的成本核算对像。
CREATE OR REPLACE FUNCTION public.x9_cbfp_gj( dqyh character varying, djoid character varying) RETURNS boolean LANGUAGE 'plpgsql' COST 100 VOLATILE PARALLEL UNSAFE AS $BODY$ -- Description: 成本分配 登记当前归集单(一张) DECLARE ssmsg text;ssdet text;sstname text;sshint text;ssctt text; --错误处理 rs boolean; ssfpny varchar(6); ssfpfw varchar(500); -- 年月 ssxmdh varchar(12);ssxmmc varchar(50);ssxmlx varchar(30); ssfpblsl decimal(16,6); -- 分配比率数量 ssfpblje decimal(16,6); -- 分配比率金额 sshjfpje decimal(16,2); -- 合计分配金额 sshjsl decimal(16,6); -- 合计产品数量 成本核算单中符合条件的行 ssmaxid INT; -- 数量最大的分配行 ssqdchxh varchar(20); ssqdcpxh varchar(20); rd record; --记录 cs_gj CURSOR FOR SELECT mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,scbm,wldw,lyxh FROM djcbgjm WHERE (oid = $2); BEGIN rs:=false; SELECT ny,fpfw INTO ssfpny,ssfpfw FROM djcbgjo WHERE oid = $2; UPDATE djcbgjo SET sffp = false WHERE oid = $2; DELETE FROM djcbgjs WHERE oid = $2; -- 成本项目 SELECT xmdh,xmmc,xmlx INTO ssxmdh,ssxmmc,ssxmlx FROM djcbgjo WHERE oid = $2; OPEN cs_gj; LOOP FETCH cs_gj INTO rd; EXIT WHEN NOT FOUND; IF ssxmlx = '直接材料' THEN IF rd.cbyt <> '' THEN SELECT sum(m.hssl) INTO sshjsl FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (ssfpfw LIKE '%' || o.oid || '%' OR ssfpfw = '') AND m.chxh = rd.cbyt AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw); -- 本例根据物料清单定额数量进行分配 SELECT chxh,min(cpchxh) INTO ssqdchxh,ssqdcpxh FROM mlqd WHERE chxh = rd.cbyt GROUP BY chxh; WITH cpqd (cpchxh,chxh,jgdh,jc) AS (SELECT cpchxh,chxh,jgdh,jc FROM mlqd WHERE cpchxh = ssqdcpxh AND chxh = ssqdchxh) SELECT round(cl.desl * (1 + (cl.desh / 100)),6) INTO ssfpblsl FROM cpqd AS cp INNER JOIN mlqd cl ON cp.cpchxh = cl.cpchxh AND cp.jc + 1 = cl.jc AND cl.jgdh LIKE cp.jgdh || '%' WHERE cl.cpchxh = ssqdcpxh AND cl.chxh = rd.lyxh; ssfpblje:=round(ssfpblsl * rd.cbdj,6); IF sshjsl > 0 AND (ssfpblsl IS NOT NULL) AND (ssfpblje IS NOT NULL) THEN INSERT INTO djcbgjs(ny,oid,mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,hsoid,xmdh,xmmc,xmlx,scbm,wldw) SELECT o.ny AS ny,$2 AS oid,rd.mid AS id,rd.cbmc AS cbmc,rd.jldw AS jldw, m.hssl * ssfpblsl AS gjsl,rd.cbdj AS cbdj,m.hssl * ssfpblje AS cbje,rd.cbyt AS cbyt,o.oid, ssxmdh AS xmdh, ssxmmc AS xmmc, ssxmlx AS xmlx,rd.scbm,rd.wldw FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (ssfpfw LIKE '%' || o.oid || '%' OR ssfpfw = '') AND m.chxh = rd.cbyt AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw); SELECT sum(cbje) INTO sshjfpje FROM djcbgjs WHERE (mid = rd.mid); UPDATE djcbgjm SET fpje = sshjfpje,sffp = TRUE,bz = '' WHERE mid = rd.mid; IF (sshjfpje - rd.cbje) / rd.cbje < 0.005 THEN SELECT id INTO ssmaxid FROM djcbgjs WHERE (mid = rd.mid) ORDER BY cbje DESC LIMIT 1; UPDATE djcbgjs SET cbje = cbje + (rd.cbje - sshjfpje) WHERE (mid = rd.mid) AND id = ssmaxid; UPDATE djcbgjm SET fpje = rd.cbje,sffp = TRUE,bz = '' WHERE mid = rd.mid; END IF; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,未找到货号' WHERE mid = rd.mid; END IF; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,请指定货号' WHERE mid = rd.mid; END IF; END IF; IF ssxmlx = '直接人工' THEN IF rd.cbyt <> '' THEN SELECT sum(m.hssl) INTO sshjsl FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (ssfpfw LIKE '%' || o.oid || '%' OR ssfpfw = '') AND m.chxh = rd.cbyt AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; IF sshjsl > 0 THEN ssfpblsl:=round(rd.gjsl / sshjsl,6); ssfpblje:=round(rd.cbje / sshjsl,6); INSERT INTO djcbgjs(ny,oid,mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,hsoid,xmdh,xmmc,xmlx,scbm,wldw) SELECT o.ny AS ny, $2 AS oid, rd.mid AS id, rd.cbmc AS cbmc, rd.jldw AS jldw, m.hssl * ssfpblsl AS gjsl, rd.cbdj AS cbdj, m.mid * ssfpblje AS cbje, rd.cbyt AS cbyt,o.oid, ssxmdh AS xmdh, ssxmmc AS xmmc, ssxmlx AS xmlx,rd.scbm,rd.wldw FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (ssfpfw LIKE '%' || o.oid || '%' OR ssfpfw = '') AND m.chxh = rd.cbyt AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; SELECT sum(cbje) INTO sshjfpje FROM djcbgjs WHERE (mid = rd.mid); IF sshjfpje - rd.cbje <> 0 THEN SELECT id INTO ssmaxid FROM djcbgjs WHERE (mid = rd.mid) ORDER BY cbje DESC LIMIT 1; UPDATE djcbgjs SET cbje = cbje + (rd.cbje - sshjfpje) WHERE (mid = rd.mid) AND id = ssmaxid; END IF; UPDATE djcbgjm SET fpje = rd.cbje,sffp = TRUE,bz = '' WHERE mid = rd.mid; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,未找到货号' WHERE mid = rd.mid; END IF; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,请指定货号' WHERE mid = rd.mid; END IF; END IF; IF ssxmlx = '间接材料' OR ssxmlx = '间接人工' THEN SELECT sum(m.hssl) INTO sshjsl FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; IF sshjsl > 0 THEN ssfpblsl:=round(rd.gjsl / sshjsl,6); ssfpblje:=round(rd.cbje / sshjsl,6); INSERT INTO djcbgjs (ny,oid,mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,hsoid,xmdh,xmmc,xmlx,scbm,wldw) SELECT ssfpny AS ny,$2 AS oid,rd.mid AS id,rd.cbmc AS cbmc,rd.jldw AS jldw, m.hssl * ssfpblsl AS gjsl, rd.cbdj AS cbdj, m.hssl * ssfpblje AS cbje, m.chxh AS cbyt,o.oid, ssxmdh AS xmdh, ssxmmc AS xmmc, ssxmlx AS xmlx,rd.scbm,rd.wldw FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; SELECT sum(cbje) INTO sshjfpje FROM djcbgjs WHERE (mid = rd.mid); IF sshjfpje - rd.cbje <> 0 THEN SELECT id INTO ssmaxid FROM djcbgjs WHERE (mid = rd.mid) ORDER BY cbje DESC LIMIT 1; UPDATE djcbgjs SET cbje = cbje + (rd.cbje - sshjfpje) WHERE (mid = rd.mid) AND id = ssmaxid; END IF; UPDATE djcbgjm SET fpje = rd.cbje,sffp = TRUE,bz = '' WHERE mid = rd.mid; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,没有符合条件的产品' WHERE mid = rd.mid; END IF; END IF; IF ssxmlx = '制造费用' THEN SELECT sum(m.hssl) INTO sshjsl FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; IF sshjsl > 0 THEN ssfpblsl := round(rd.gjsl / sshjsl,6); ssfpblje := round(rd.cbje / sshjsl,6); INSERT INTO djcbgjs (ny,oid,mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,hsoid,xmdh,xmmc,xmlx,scbm,wldw) SELECT ssfpny AS ny, $2 AS oid, rd.mid AS id, rd.cbmc AS cbmc, rd.jldw AS jldw, m.hssl * ssfpblsl AS gjsl, rd.cbdj AS dj, m.hssl * ssfpblje AS cbje, m.chxh AS cbyt,o.oid, ssxmdh AS xmdh, ssxmmc AS xmmc, ssxmlx AS xmlx,rd.scbm,rd.wldw FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND (rd.scbm = '' OR m.scbm = rd.scbm) AND (rd.wldw = '' OR m.wldw = rd.wldw) ; SELECT sum(cbje) INTO sshjfpje FROM djcbgjs WHERE (mid = rd.mid); IF sshjfpje - rd.cbje <> 0 THEN SELECT id INTO ssmaxid FROM djcbgjs WHERE (mid = rd.mid) ORDER BY cbje DESC LIMIT 1; UPDATE djcbgjs SET cbje = cbje + (rd.cbje - sshjfpje) WHERE (mid = rd.mid) AND id = ssmaxid; END IF ; UPDATE djcbgjm SET fpje = rd.cbje,sffp = TRUE,bz = '' WHERE mid = rd.mid; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,没有符合条件的产品' WHERE mid = rd.mid; END IF; -- 管理费用直接分配到检测车间入库的产品中 ,直接在代码中加工作中心名称进行限制 END IF; IF ssxmlx = '管理费用' THEN rd.scbm:='检测车间'; SELECT sum(m.hssl) INTO sshjsl FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND m.scbm = rd.scbm; IF sshjsl > 0 THEN ssfpblsl:=round(rd.gjsl / sshjsl,6); ssfpblje:=round(rd.cbje / sshjsl,6); INSERT INTO djcbgjs (ny,oid,mid,cbmc,jldw,gjsl,cbdj,cbje,cbyt,hsoid,xmdh,xmmc,xmlx,scbm,wldw) SELECT ssfpny AS ny, $2 AS oid, rd.mid AS id, rd.cbmc AS cbmc, rd.jldw AS jldw, m.hssl * ssfpblsl AS gjsl, rd.cbdj AS dj, m.hssl * ssfpblje AS cbje, m.chxh AS cbyt, o.oid, ssxmdh AS xmdh, ssxmmc AS xmmc, ssxmlx AS xmlx,rd.scbm as scbm,rd.wldw FROM djcbhso o INNER JOIN djcbhsm m ON o.oid = m.oid WHERE o.ny = ssfpny AND o.djr <> '' AND m.xsje > 0 AND m.scbm = rd.scbm; SELECT sum(cbje) INTO sshjfpje FROM djcbgjs WHERE (mid = rd.mid); IF sshjfpje - rd.cbje <> 0 THEN SELECT id INTO ssmaxid FROM djcbgjs WHERE (mid = rd.mid) ORDER BY cbje DESC LIMIT 1; UPDATE djcbgjs SET cbje = cbje + (rd.cbje - sshjfpje) WHERE (mid = rd.mid) AND id = ssmaxid; END IF; UPDATE djcbgjm SET fpje = rd.cbje,sffp = TRUE,bz = '' WHERE mid = rd.mid; ELSE UPDATE djcbgjm SET sffp = false,bz = '未能分配,没有符合条件的产品' WHERE mid = rd.mid; END IF; END IF; END LOOP; CLOSE cs_gj; -- 归集单标记为已分配 UPDATE djcbgjo SET sffp = TRUE WHERE oid = $2; rs:=true; RETURN rs; EXCEPTION WHEN others THEN GET stacked DIAGNOSTICS ssmsg = MESSAGE_TEXT,ssdet = pg_exception_detail, sstname = TABLE_NAME, sshint = pg_exception_hint, ssctt = pg_exception_context; CALL x9_jlproc(ssmsg, ssdet, sstname, sshint, ssctt); RETURN FALSE; END; $BODY$; ALTER FUNCTION public.x9_cbfp_gj(character varying, character varying) OWNER TO postgres;
(二)、成本核算
将分配好的数据,汇总更新到成本核算单。
成本核算单的结果再更新到入库单中,通过存货核算程序,再将入库成本重新更新的存货总账中。
CREATE OR REPLACE FUNCTION public.x9_cbhs( ssdqyh character varying, sshsny character, ssscbm character varying) RETURNS character varying LANGUAGE 'plpgsql' COST 100 VOLATILE PARALLEL UNSAFE AS $BODY$ -- description: 成本分配之后,更新成本核算单 DECLARE ssmsg text;ssdet text;sstname text;sshint text;ssctt text; --错误处理 rs varchar; rd record; sshjcb decimal(16,2); sscbce decimal(16,2); ssrkid int; cs_cbhs refcursor; BEGIN WITH cbgj (hsoid,cbyt,cpcb,zjcl,jjcl,zjrg,jjrg,zzfy,glfy) AS (SELECT hsoid,cbyt,sum(cbje) AS cpcb, sum(CASE xmlx WHEN '直接材料' THEN cbje ELSE 0 END) AS zjcl, sum(CASE xmlx WHEN '间接材料' THEN cbje ELSE 0 END) AS jjcl, sum(CASE xmlx WHEN '直接人工' THEN cbje ELSE 0 END) AS zjrg, sum(CASE xmlx WHEN '间接人工' THEN cbje ELSE 0 END) AS jjrg, sum(CASE xmlx WHEN '制造费用' THEN cbje ELSE 0 END) AS zzfy, sum(CASE xmlx WHEN '管理费用' THEN cbje ELSE 0 END) AS glfy FROM djcbgjs WHERE ny = $2 AND scbm = $3 GROUP BY hsoid, cbyt) UPDATE djcbhsm m SET cpcb = cbgj.cpcb,zjcl = cbgj.zjcl,zjrg = cbgj.zjrg,jjcl = cbgj.jjcl, jjrg = cbgj.jjrg,zzfy = cbgj.zzfy,glfy = cbgj.glfy FROM cbgj where m.oid = cbgj.hsoid AND m.chxh = cbgj.cbyt and (m.ny = $2) AND m.scbm = $3 AND (m.sfdj = true); UPDATE djcbhsm SET cpml = xsje - cpcb WHERE (ny = $2) AND scbm = $3 AND (sfdj = true); UPDATE djcbhsm SET dwcb = cpcb / hssl,dwml = cpml / hssl WHERE hssl > 0 AND (ny = $2) AND scbm = $3 AND (sfdj = true); --更新入库成本 OPEN cs_cbhs FOR SELECT m.chxh,m.dwcb,m.cpcb,o.hslb FROM djcbhsm m INNER JOIN djcbhso o ON o.oid = m.oid WHERE o.ny = $2 AND o.scbm = $3; LOOP FETCH cs_cbhs INTO rd; EXIT WHEN NOT FOUND; UPDATE djchm SET rkdj = rd.dwcb,rkje = rd.dwcb * rksl WHERE ny = $2 AND chxh = rd.chxh AND djmc = rd.hslb; SELECT sum(rkje) INTO sshjcb FROM djchm WHERE ny = $2 AND chxh = rd.chxh ; sscbce:=rd.cpcb - sshjcb; IF sscbce <> 0 THEN ssrkid = -1; SELECT mid INTO ssrkid FROM djchm WHERE ny = $2 AND chxh = rd.chxh AND djmc = rd.hslb ORDER BY rkje DESC LIMIT 1; IF ssrkid > -1 THEN UPDATE djchm SET rkje = rkje + sscbce WHERE mid = ssrkid; END IF; END IF; -- 更新委外入库单 IF $3 = '' AND rd.hslb = '委外入库单' THEN UPDATE djchm SET rkje = rkje + jgje,rkdj = (rkje + jgje) / rksl WHERE ny = $2 AND chxh = rd.chxh AND djmc = rd.hslb and sfdj=true; END IF; -- into rd.chxh,rd.dwcb,rd.cpcb,rd.hslb ; END LOOP; CLOSE cs_cbhs; -- 存货核算 rs:=x9_hsch($2,$1); IF position('存货核算完成' in rs) > 0 THEN RETURN '成本核算完成!'; ELSE RETURN rs; END IF; EXCEPTION WHEN others THEN GET stacked DIAGNOSTICS ssmsg = MESSAGE_TEXT,ssdet = pg_exception_detail, sstname = TABLE_NAME, sshint = pg_exception_hint, ssctt = pg_exception_context; CALL x9_jlproc(ssmsg, ssdet, sstname, sshint, ssctt); RETURN ssmsg; END; $BODY$; ALTER FUNCTION public.x9_cbhs(character varying, character, character varying) OWNER TO postgres;
六、结果输出
1、成本总账
2、成本明细账
3、成本因素分析
4、成本利润分析
七、总结
综上所述,完成了自制品和外协品的入库成本的核算。
在当今激烈的市场竞争环境中,能够做到这些还是很重要的。在实际业务中,可以将核算结果或预测结果应用到订单编辑中和其他业务环节中,成为重要的参考信息。