Python Tutorial 33: Create an eCommerce Profit & Loss Calculator or P&L Using OOP, Flask

Business is essentially a calculation. Based on the calculations that integrate with external and internal factors, people can stand on a giant shoulder and evaluate the current status and predict a more precise future. It’s not 100% correct, as the change is beyond the plan. However, it ensures you are indeed on the way to the goal instead of going the wrong way. The goal here is profitable whatever strategies you are implementing. And in the eCommerce space, SKU profit margin calculation is applicable as well.

In this Python tutorial, I will walk you through what and why OOP for building a P&L calculator. By the end of this Python Tutorial, you can master how to write the Python codings and Flask scripts to build the calculator that fits your business model.

Python Modules: OOP, flask, requests

Python Tutorial: What’s OOP – Object Oriented Programming

​​Object Oriented Programming or OOP is a programming paradigm that relies on the concept of classes and objects. You can use it to structure a software program into simple, reusable pieces of code blueprints, which we usually call classes. And in a class, you can create individual instances of objects.

OOP basically consists of parent class (can have child class), class attributes and methods (It’s the function, but OOP calls them methods).

Take the eCommerce P&L calculator for an example. eCommerce has different types of business models, or is selling different product categories . It has dometic, cross border, dropshipping, B2B wholesale etc. Each type of eCommerce business can be a specific instance of an object. It should need a different calculation algorithm and attribute value. It’s because the variable cost item would be slightly different. However, no matter which type of eCommerce, it should have some general attributes to impact the profit margin.

The profit margin attributes are:

  • Landed cost (Including product per unit cost, plus international shipping and tarrifs if it’s a cross border eCommerce)
  • Fulfillment cost
  • Payment processing cost
  • Refund cost
  • Cost per sale
  • Discount cost
  • The SKU price

In OOP, the concept of class is fundamental. Each class consists of attributes and methods. Then, it can have different objects using this class. Each object in the ecommerce P&L calculator can represent your business model. In other words, all objects are using the same parent class, however the local attribute value and methods are different.

For example, domestic eCommerce P&L might not have international shipping costs. So the landed cost value would be different with cross border eCommerce.

If you like to learn more details regarding eCommerce variable cost, please check out this article

Selling Well But Be Losing Money? 7 Variable Costs Are Impacting Your Online Store Profit Margin

Methods: Algorithm of Cross border eCommerce Profit Margin Calculator

The calculation algorithm is the critical component of the calculator. It’s because of two reasons. The first reason is you absolutely like to have a right SKU profit margin calculation for reference. Then, the 2nd reason is you like to ensure users are clear what data and data format she or he should input on your calculator. Sometimes it can be very complex if your calculator aims to help users to find out a number with more complex factors.

For example, generally the profit margin formula is (SKU price – selling cost) / SKU price. And you can try to break down the selling cost into landed cost, fulfillment cost, refund cost, payment processing cost, CPS, discount cost. Depending on the object of the calculator, you can further break down if necessary, for example it requests users to input weight, size and destination for the fulfillment cost. Or here I will take refund rate input for an example to show the algorithm

The object algorithm that connects with the attribute value is:

(SKU price - landed cost - fulfillment cost - (refund rate * fulfillment cost * 2) - payment processing cost - discount rate * SKU price - CPS) / SKU price

Python OOP codings of a SKU Profit Margin calculator

I take the cross border eCommerce P&L calculator as the parent class in this Python OOP. Now the scope of attributes and calculation algorithm are ready. Below are the codings of the core engine of the calculator.

python tutorial

In Python OOP, the first thing is to create a parent class. In this class, there are three fundamental components. They are the _init__ attribute, attribute association and a method definition.

_init__ attribute

This is the self default object and method of the class. We need to def the default method by adding all attributes here in the positional arguments in the __init__( ). As we are building a cross border eCommerce P&L calculator, the attribute items are the price and variable costs related to this model.

def __init__(
self,
landedCost,
shippingCost,
paymentCost,
refundCost,
CPA,
discount,
price
):

Assign to self object

Self object is ready, so you can create the variables that can be associated with the attributes in the positional arguments. So you can start using the variables for all methods in this object.

self.landedCost = landedCost
self.shippingCost = shippingCost
self.paymentCost = paymentCost
self.refundCost = refundCost
self.CPA = CPA
self.discount = discount
self.price = price
Define the P&L calculator algorithm method

The OOP object is to calculate the profit margin after inputting the SKU attribute values. So we need an algorithm embedded in this class. For this, we can create a method by using def, and the positional argument can be self.

def calculate_profitmargin(self):

return (self.price - self.landedCost - self.shippingCost - self.price * self.paymentCost - self.shippingCost * 2 * self.refundCost - self.CPA - self.discount * self.price) / self.price

As mentioned, the calculation algorithm varies depending on the actual needs. Thus, you can define more algorithm methods in this class, for example you can set the other one that replace SKU price with the AOV.

Flask scripts

Flask is a friend and powerful web application builder that can integrate with your programming scripts, such as Python, HTML, css, php, etc. If you like to learn more details about how to install, set up and use it to build and test Python web application, please check out this article

Python Tutorial 26: Create a Shopify Bot Web Application Using Flask and Heroku

In the Flask script of the calculator application, there are two primary things you need to be taking care of. They are the Python script and the html script.

Python Script in the Flask Script

python tutorial

The Python script here needs to connect the front-end html table which is for collecting the attribute value. Meanwhile, it needs to connect with the OOP calculator algorithm by feeding the collected data from the html page. One of the most important thing is the positional argument, please match well the input data variable and the positional argument, for avoiding wrong result in the calculation

Self object:

profitMargin1 = item(landedCost1, shippingCost1, paymentCost1, refundCost1, CPA1, discount1, price1)

Assign the calculation algorithm

result = "{:.2%}".format(round(profitMargin1.calculate_profitmargin(), 2))

You might notice, we convert the calculation into a percentage number. That’s because the outcome from calculate_profitmargin is a floating number. That would create a better user experience

Keep the input value in the result page

Users would like to double check what number they input to the box generating the result. Thus, keeping the input value is important if you like to create a better user experience. Thus, in the return, we need to add these parameters apart from selecting the template of html page.

return render_template("result.html", price1=price1, landedCost1=landedCost1, shippingCost1=shippingCost1, paymentCost1=paymentCost1, refundCost1=refundCost1, CPA1=CPA1, discount1=discount1, result=result)

HTML template pages

Html pages are the front-end where you show your calculator layout and design, and interaction blocks to the users. I’ll release another article regarding how to build and decorate a web page using html and CSS. But in this application, the key thing is you need to make sure all collected data can be related to the right positional argument. Otherwise, the calculation result might be wrong

Thus, as you can see, each input box has a related name element that is connected with the same name of the variable you use to calculate the profit margin.

Full Python and Flask Script of eCommerce Profit & Loss Calculator

If you are interested in the full script of eCommerce Profit & Loss Calculator, please subscribe to our newsletter by adding the message “Profit & Loss Calculator script”. We would send you the script immediately to your mailbox.

Contact us

I hope you enjoy reading Python Tutorial 33: Create an eCommerce Profit & Loss Calculator Using OOP, Flask. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support my channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)

Leave a Reply

Your email address will not be published.