如何计算销售商品的成本

How to calculate the cost of goods sold

通过下面的代码,我的输出告诉我该批次中有多少产品尚未发货

但是,我想知道在先出规则下售出的每件产品的成本。

import pandas as pd
import numpy as np

df=pd.DataFrame({'code': [2,2,2,2,2,2,2,3,3,3,5,5,6],
        'date': ['2015-11-15','2015-11-17','2015-11-20','2016-04-01','2016-04-02','2016-04-03','2016-11-01','2015-02-01','2015-05-01','2016-03-01','2015-11-20','2016-06-01','2015-02-01'],
        'num' : [10,-10, 20, 10, -30,50, -40, 25, 35, -15, 50, -50, 35],
        'price': [3.8,3.7,3.5,3.2, 3.6,3.4, 3.5, 7, 7.5,  8, 5,  5.5, 11.5],
        'money': [-38,37,-70,-32, 108,-170, 140,-175,-262.5,120,-250, 275,-402.5]
        })
print(df)

def FiFo(dfg):
    if dfg[dfg['CS'] < 0]['num'].count():
        subT = dfg[dfg['CS'] < 0]['CS'].iloc[-1]
        dfg['num1'] = np.where((dfg['CS'] + subT) <= 0, 0, dfg['CS'] + subT)
        dfg['num1'] = np.where(dfg['num']<dfg['num1'],dfg['num'],dfg['num1'])
        dfg['num'] = np.where(dfg['num1']>0,dfg['num1'],0)
    return dfg

df['PN'] = np.where(df['num'] > 0, 'P', 'N')
df['CS'] = df.groupby(['code', 'PN'])['num'].cumsum()
dfR = df.groupby(['code'], as_index=False)\
    .apply(FiFo) \
    .drop(['price','PN'], axis=1) \
    .reset_index(drop=True)

print(dfR[dfR['num'] > 0])
2015-11-17: The cost of sold product 2 is 3.8
2016-04-02: The cost of sold product 2 is (70+32)/(20+10)=3.4

试试这个,如果你要处理剩菜,你必须添加更多条件。

import pandas as pd
import numpy as np

df=pd.DataFrame({'code': [2,2,2,2,2,2,2,3,3,3,5,5,6],
        'date': ['2015-11-15','2015-11-17','2015-11-20','2016-04-01','2016-04-02','2016-04-03','2016-11-01','2015-02-01','2015-05-01','2016-03-01','2015-11-20','2016-06-01','2015-02-01'],
        'num' : [10,-10, 20, 10, -30,50, -40, 25, 35, -15, 50, -50, 35],
        'price': [3.8,3.7,3.5,3.2, 3.6,3.4, 3.5, 7, 7.5,  8, 5,  5.5, 11.5],
        'money': [-38,37,-70,-32, 108,-170, 140,-175,-262.5,120,-250, 275,-402.5]
        })
print(df)

def FiFo(dfg):
    if dfg[dfg['CS'] < 0]['num'].count():
        subT = dfg[dfg['CS'] < 0]['CS'].iloc[-1]
        dfg['num1'] = np.where((dfg['CS'] + subT) <= 0, 0, dfg['CS'] + subT)
        dfg['num1'] = np.where(dfg['num']<dfg['num1'],dfg['num'],dfg['num1'])
        dfg['num'] = np.where(dfg['num1']>0,dfg['num1'],0)
    return dfg

df['PN'] = np.where(df['num'] > 0, 'P', 'N')
df['CS'] = df.groupby(['code', 'PN'])['num'].cumsum()
dfR = df.groupby(['code'], as_index=False)\
    .apply(FiFo) \
    .drop(['price','PN'], axis=1) \
    .reset_index(drop=True)
print(dfR[dfR['num'] > 0])
newdf=df.loc[df['PN'] == 'N']
print(df)
arr=[]
for i in range(len(df)):
    if (df['money'][i])<0:
        arr.append(i)
def findpurchase(code):
    for i in arr:
        # print(arr,i)
        if df['code'][i]==code:
            arr.pop(arr.index(i))
            return i
newdf_top = newdf.head()

for i in newdf_top.index:
    num=0
    mon=0
    while num<abs(df['num'][i]):
        index=findpurchase(df['code'][i])
        mon+=abs(df['money'][index])
        num+=abs(df['num'][index])
    cost=mon/num
    print(f'{df["date"][i]}: The cost of sold product {df["code"][i]} is {cost}')

输出

2015-11-17: The cost of sold product 2 is 3.8
2016-04-02: The cost of sold product 2 is 3.4
2016-11-01: The cost of sold product 2 is 3.4
2016-03-01: The cost of sold product 3 is 7.0
2016-06-01: The cost of sold product 5 is 5.0
df=pd.DataFrame({'code': [2,2,2,2,2,2,2,3,3,3,5,5,6],
        'date': ['2015-11-15','2015-11-17','2015-11-20','2016-04-01','2016-04-02','2016-04-03','2016-11-01','2015-02-01','2015-05-01','2016-03-01','2015-11-20','2016-06-01','2015-02-01'],
        'num' : [10,-8, 20, 10, -30,50, -40, 25, 35, -15, 50, -50, 35],
        'price': [3.8,3.7,3.5,3.2, 3.6,3.4, 3.5, 7, 7.5,  8, 5,  5.5, 11.5],
        'money': [-38,29.6,-70,-32, 108,-170, 140,-175,-262.5,120,-250, 275,-402.5]
        })
out:

2015-11-17: 售出产品2的成本为3.8

(The quantity of the first purchase of product 2 after the first sold is still 2)

2016-04-02:售出产品2的成本为3.44

Explanation of calculation result:
mon=(Product 2 Remaining quantity from first purchase)*(Unit price of the first purchase of product 2)+(Product 2 Second Purchase Quantity)*(Unit price of the second purchase of product 2)+(Unit price of the third purchase of the product 2)*8
(Product cost of second sold)=mon/30
The reason for this calculation is
(Remaining quantity of product 2 first purchase) + (quantity of product 2 second purchase) + (quantity of product 2 third purchase)> (quantity of product 2 second sold)

(The quantity of the third purchase of product 2 after the second sold is still 2)

2016-11-01:售出产品2的成本为3.39

mon=(Product 2 Remaining quantity from third purchase)*(Unit price of the third purchase of product 2)+38*(Unit price of the fourth purchase of product 2)
(Product cost of third sold)=mon/40

The reason for this calculation is
(Remaining quantity of product 2 third purchase) + (quantity of product 2 fourth purchase)> (quantity of product 2 third sold)

2016-03-01:售出产品3的成本为7.0

2016-06-01: 售出产品5的成本为5.0

Adhun Thalekkara 谢谢,我试着修改你的代码来达到我想要的效果。修改后的代码如下:

def findpurchase(code):
    for i in arr:
        # print(arr,i)
        if df['code'][i]==code:
            #if abs(df['num'][i])<a:
             # arr.pop(arr.index(i))
            return i
newdf_top = newdf.head()

for i in newdf_top.index:
    num=0
    mon=0
    while num<abs(df['num'][i]):
        index=findpurchase(df['code'][i])
        if abs(df['num'][index])>(abs(df['num'][i])-num):
          a=(abs(df['num'][i])-num)
          mon+=abs(df['price'][index])*a
          num+=a
          df['money'][index]=(abs(df['num'][index])-a)*abs(df['price'][index])
          df['num'][index]=abs(df['num'][index])-a
        else:
          mon+=abs(df['money'][index])
          num+=abs(df['num'][index])
          arr.pop(arr.index(index))
          df['num'][index]=0
          df['money'][index]=0
        #print(df,"1-----",num)
    cost=mon/num
    #print(mon,"---",num)
    print(f'{df["date"][i]}: The cost of sold product {df["code"][i]} is {cost}')

输出:

2015-11-17: The cost of sold product 2 is 3.8
2016-04-02: The cost of sold product 2 is 3.4399999999999995
2016-11-01: The cost of sold product 2 is 3.3899999999999997
2016-03-01: The cost of sold product 3 is 7.0
2016-06-01: The cost of sold product 5 is 5.0