Esempio n. 1
0
def calculate_total_net_profit():
    """ Calculate total profit.
    """

    logging.debug('Listing sales')
    # get sales stats
    saleStats = salesModel.get_stats_by_products()

    logging.debug('Listing purchases')
    # get purchases stats
    purchaseStats = purchasesModel.get_stats_by_products()

    # The profit margin is the weighted avg
    # from sales, where quantity as weight and
    # (weighted avg net profit - weighted avg unit cost)
    # as the number to calculate avg
    #
    # For instance, if you have 80% sales wich
    # specific product, it will be considered more than
    # the other 20%.

    logging.debug('Calculating net profit')
    sum_net_profit = 0.0
    for sale in saleStats:

        # Search purchase by product
        purchases = [x for x in purchaseStats if x['product'].key.id()
                     == sale['product'].key.id()]

        if len(purchases) == 1:
            purchase = purchases[0]

            # Calculate the diference between net (sale value minus tax)
            # minus product cost
            sum_net_profit = sum_net_profit + (sale['weighted_avg_net_profit'] -
                                               purchase['weighted_avg_cost']
                                               ) * sale['sum_quantity']

        else:
            logging.warning("Product %s didn't find!", sale['product'].name)

    # Return
    return sum_net_profit
Esempio n. 2
0
    def test_sales_statistics_by_products(self):
        """ Unit test to get sales statistics by products.
        """

        # Sales list
        salesList = []
        
        # Mock sale model and products models
        salesMock = [{
            'id': 1,
            'product_id': 1,
            'net_value': 5,
            'quantity': 7
        }, {
            'id': 2,
            'product_id': 2,
            'net_value': 3,
            'quantity': 20
        }, {
            'id': 3,
            'product_id': 1,
            'net_value': 15,
            'quantity': 8
        }, {
            'id': 4,
            'product_id': 3,
            'net_value': 1,
            'quantity': 1
        }, {
            'id': 5,
            'product_id': 2,
            'net_value': 9,
            'quantity': 40
        }]

        # Iterate sales mock
        for x in salesMock:
            
            # Create sale mock
            sale = Mock(spec_set=saleModel.SaleModel())
            sale.key = Mock(spec_set=ndb.Key('SaleModel', x['id']))

            # Create product mock
            sale.product = Mock(spec_set=ndb.Key('ProductModel', x['product_id']))
            sale.product.id = Mock(return_value=x['product_id'])
            sale.product.get = Mock(return_value=sale.product)

            # Net total value
            sale.net_total = x['net_value']
            sale.quantity = x ['quantity']

            # Append to list
            salesList.append(sale)
        
        # Mock list method
        saleModel.list = MagicMock(return_value=salesList)    
        
        # Call report_customers_by_product method
        result = saleModel.get_stats_by_products()

        # Must have lenght == 3
        self.assertEqual(len(result), 3)

        # Verify quantity
        self.assertEqual(15, result[0]['sum_quantity'])
        self.assertEqual(60, result[1]['sum_quantity'])
        self.assertEqual(1, result[2]['sum_quantity'])

        # Verify sum net profit
        self.assertEqual(20, result[0]['sum_net_profit'])
        self.assertEqual(12, result[1]['sum_net_profit'])
        self.assertEqual(1, result[2]['sum_net_profit'])        

        # Verify sum net profit
        self.assertEqual(1.29, round(result[0]['avg_net_profit'], 2))
        self.assertEqual(0.19, round(result[1]['avg_net_profit'], 2))
        self.assertEqual(1, result[2]['avg_net_profit'])  

        # Verify sum net profit
        self.assertEqual(1.33, round(result[0]['weighted_avg_net_profit'], 2))
        self.assertEqual(0.20, round(result[1]['weighted_avg_net_profit'], 2))
        self.assertEqual(1, result[2]['weighted_avg_net_profit'])