Jump to content

Recommended Posts

Please help me let me explain with this.....

purchase table

id      purchase_id                   product_id        qty          net_unit_cost             created_at

1                  1                                   1                   10                      10                        2022-10-10

2                  2                                  1                    20                      12                      2022-10-10

 

Sale table

sale_id      product_id                 qty             net_unit_price            created_at

1                      1                            11                      15                           2022-10-10

in this, if i sold '11' units then how can i subtract from the rows to get remaining units? i've to subtract '10' units from first row and '1' unit from second row. Please help how to calculate fifo in php

Link to comment
https://forums.phpfreaks.com/topic/315446-fifo-method-calculation-php/
Share on other sites

Please help me let me explain with this.....

purchase table

id      purchase_id                   product_id        qty          net_unit_cost             created_at

1                  1                                   1                   10                      10                        2022-10-1

2                  2                                  1                    20                      12                      2022-10-10

 

Sale table

sale_id      product_id                 qty             net_unit_price            created_at

1                      1                            11                      15                           2022-10-15

in this, if i sold '11' units then how can i subtract from the rows to get remaining units? i've to subtract '10' units from first row and '1' unit from second row. Please help how to calculate fifo in php

Someone has wanted the net_unit_cost and net_unit_price stored in the records. If, as you said, you subtract the 10 and the 1 from the quantities in the purchase records, how will you know which sales were sold from each purchase batch. When the sales department wants a report showing the profit on each sale, how do you intend producing it?

The answer is to do the processing as part of the sales input process and add a "purchase_id" column to the sales. When you decide 10 are sold from purchase record #1, write a sales record with qty = 10 and prchase_id = 1. Then write another sales record with qty = 1 and purchase_id = 2. Now you know from which batch of purchases each sale was made. And no need to update the purchase records to reduce the quantities.

+---------------------+                +---------------------+
| purchase            |                | sale                |
+---------------------+                +---------------------+
| purchase_id         |-------+        | sale_id             |
| product_id          |       |        | product_id          |
| qty                 |       |        | qty                 |
| net_unit_cost       |       |        | net_unit_price      |
| created_at          |       +-------<| purchase_id         |
+---------------------+                | created_at          |
                                       +---------------------+

To get the available quantities for the next sale (from each purchase, what was sold?)

SELECT p.purchase_id
     , p.qty - coalesce(s.qty, 0) as qty
FROM purchase p
     LEFT JOIN (
        SELECT purchase_id
             , sum(qty) as qty
        FROM sale 
        GROUP BY purchase_id      
        ) s ON p.purchase_id = s.purchase_id
WHERE p.product_id = 1 AND p.qty - coalesce(s.qty, 0) > 0
ORDER BY created_at;

For today's sales (for each sale, what batch was it from?)

SELECT s.product_id
     , s.qty
     , p.net_unit_cost
     , s.net_unit_price
     , s.net_unit_price - p.net_unit_cost as net_unit_profit
     , s.qty * (s.net_unit_price - p.net_unit_cost) as profit
     , s.created_at as date_sold
FROM sale s 
     JOIN purchase p USING (purchase_id)
WHERE s.created_at = CURDATE()
ORDER BY product_id;

The process for determining how many from each purchase batch is simple maths.

foreach purchase record
    how many of the sales qty can I sell from this record?
    write sales record for that qty with purchase id
    reduce sales qty by that amount
end for each

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.