Simply looking to create an area/block in the admin area of a product page that displays what the current gross profit is on that product. I.e. calculating the difference between the cost price and selling price of the item. So for example we have a cost attribute, and either a price or special_price attribute for the selling price. Some products have a special (sale) price active, some don't, so this probably requires an if statement. Initially I explored creating an attribute to do this, but that didn't really work out, so looking to hard code something into the admin page. Ideally creating small module to do this. Any ideas on the best way to go about or detail some of the code involved to get me started?
I just completed a module to calculate the profit and profit ratio on a product. The calculation is done when the product is saved and stores the values in 2 attributes (product_profit and product_profit_ratio). The module also adds 2 columns to the productgrid to display both the profit and ratio.
I published the source on BitBucket, see https://bitbucket.org/genmato/productprofit/.
I tested it on both 22.214.171.124 and 1.8, and might work on 1.6 also. Not compatibel with 1.5 and lower.
Since I do not have the privilege to comment I'll try to answer how I would approach this. First, I will use the native product attribute
cost to calculate the gross profit. Now, I need to add a
gross_profit column to the table
sales_flat_order_grid. The later table is for display the
gross_profit data in admin. Now, depending if the site is in production or under development, I'll need to create the necessary database setup. Let's take the more interesting production setup, which will requires data upgrade, meaning I will create the data upgrade script to calculate the
gross_profit of all the previous orders and put that values in the 2 tables. The script can be reused in the
model class to calculate the
gross_profit for new orders. Basically, I'll list all the ordered items and accumulate the profit, minus the discount. Table
sales_flat_order_item is involved in the data upgrade script but will not be directly involved in the
model class. I think this makes a nice project for an experienced Magento developer, otherwise, I suggest buying an extension.
I apologize for not reading your requirements correctly. Here's what I would do to calculate GP at the product level. First, I set up a product attribute called
accumulated_profit in the backend. Then, I set up an event listener for
sales_order_invoice_pay which fires when payment is received for an order. From the invoice object that is passed from the event, I will check what items were being invoiced and the qty invoiced. For each item, I will then calculate the profit and add it to the
accumulated_profit. Third, I will provide a custom column in the admin to display
accumulated_profit in the product grid. Last, I will also create the data upgrade script, which will retrieved passed invoices and update the
accumulated_profit. The above ignores any refund. But this can easily be accommodated with another listener for event
I think a complete approach would be create a new table to record all the invoiced items and refunded items and the GP for each along with the timestamp. This will provide a complete statistic on GP for each product. The table can then be either pulled for display some simple trends in the backend or be exported for analysis by third-party application.
EDIT AGAIN: I do not believe you want to calculate accumulated GP. So the above is incorrect. If you just need a simple GP on each product, then it can be easily accomplished by adding a custom column to the catalog grid in the admin. To do this, I'd would overwrite the class
Mage_Adminhtml_Block_Sales_Order_Grid and add a new column
gross_profit, and I'd write a custom renderer for it. If you need to export products that are below a certain threshold, then I'd write a script to calculate each product and export them to a csv file. To be a little bit sophisticated, I'd add an action in the dropdown
admin > Catalog > Manage Products so that the script can be executed when the
Submit button is clicked.