在PHP中通过FIFO堆栈购买/销售利润跟踪

时间:2015-11-19 14:54:33

标签: php arrays fifo

我需要实施一个algorythm,它可以跟踪单个项目交易列表的利润并更新“利润”表。我们的想法是获取所购买的商品,并将其与销售的商品交叉引用,纯粹按订单(即不存在单个商品的单个实例,只有商品类型的ID)。所以,假设我们有一个包含这些数据的表格:

items:
item id  item type
1         red box
2         blue box
3         white box

transactions:
item id -- quantity -- transaction_type -- price_unit($) -- time
1          3              buy                 20          2015-10-10 
3          1              buy                 10          2015-10-11
1          1              sell                25          2015-10-12
1          1              sell                20          2015-11-14
3          1              sell                15          2015-11-13

对于此示例,我的利润表应如下所示:

item_id  --  quantity  --   profit  -- timestamp_bought  ---timestamp_sold
1              1           (25-20)=5   2015-10-10            2015-10-12                     
1              1           (20-20)=0   2015-10-10            2015-11-14 
3              1           (15-10)=5   2015-10-11            2015-11-13 

这是我到目前为止所拥有的。它起作用,直到几个不同的买/卖数量发挥作用......

$buy_list = mysqli_query($con, "SELECT * FROM transaction WHERE 
transaction_type = 'Buy'  ORDER BY time DESC");         

$sell_list = mysqli_query($con, "SELECT * FROM transaction WHERE 
transaction_type = 'Sell' ORDER BY time DESC");

 while($row = mysqli_fetch_array($buy_list))
       {
           array_push($buy_stack, array(
            $row['idtrans'], 
            $row['iditem'], 
            $row['quantity'], 
            $row['time'], 
            $row['price_unit']));
       }

       while($row = mysqli_fetch_array($sell_list))
       {
           array_push($sell_stack, array(
            $row['idtrans'], 
            $row['iditem'], 
            $row['quantity'], 
            $row['time'],
            $row['price_unit']));
       }

      $size_buy = sizeof($buy_stack);
      $size_sell = sizeof($sell_stack);


    for($i=0; $i<=$size_buy-1; $i++) //iterate BUY orders
       {
           $idtrans_b = $buy_stack[$i][0];
           $itemid_b = $buy_stack[$i][1];
           $quantity_b = $buy_stack[$i][2];
           $time_b = $buy_stack[$i][3];
           $price_unit_b = $buy_stack[$i][4];

           $quantity_b_calc = $buy_stack[$i][2];

            for($k=0; $k<=$size_sell-1; $k++)
            {
                $idtrans_s = $sell_stack[$k][0];
                $itemid_s = $sell_stack[$k][1];
                $quantity_s = $sell_stack[$k][2];
                $time_s = $sell_stack[$k][3];
                $price_unit_s = $sell_stack[$k][4];

                if($itemid_s == $itemid_b &&
                 $time_s > $time_b && 
                 $quantity_b > 0 
                 && $quantity_s >= $quantity_b) //match
                {
                   $sell_stack[$k][1] = "done_sell";
                   $buy_stack[$i][1] = "done_buy";
                   $profit_unit = $price_unit_s - $price_unit_b;
                   $profit_ quantity = min($quantity_s, $quantity_b);
                   $quantity_b = $quantity_b - $profit_quantity;
                   $time_bought = $buy_stack[$i][3];
                   $time_sold = $sell_stack[$k][3]; 

                   $add_profit = mysqli_query
($con, "INSERT INTO profit (id, transaction_buy, transaction_sell,    profit_unit, quantity, timestamp_buy, timestamp_sell) 
VALUES ( 'null', '$idtrans_b', '$idtrans_s', '$profit_unit', '$profit_quantity', $time_bought, $time_sold));              
                }
             }
        }

我坚持要让它正常工作。我应该废弃这个并用堆栈代替吗?

0 个答案:

没有答案
相关问题