Ejemplo n.º 1
0
def generate_report(hsa, date=datetime.utcnow()):
    choice = randint(1,9)
    if choice < 5:
        if not hsa or not hsa.name: return
        print "Got soh for " + hsa.name
        print ProductStock.objects.filter(supply_point=hsa)
        # SOH
        for p in ProductStock.objects.filter(supply_point=hsa):
            amc = (p.product.average_monthly_consumption if p.product.average_monthly_consumption else 100)
            q = p.quantity if p.quantity else amc
            q = max(0, q - normalvariate(q/3, q/6)) if randint(0,6) else 0
            print "Generating SOH report for %s : %s : %d" % (hsa.name, p.product.sms_code, q)
            r = ProductReport(product=p.product, report_type = ProductReportType.objects.get(code='soh'),
                          quantity=q, message=None, supply_point=hsa, report_date=date)
            r.save()
    elif choice < 10:
        # REC
        for p in ProductStock.objects.filter(supply_point=hsa):
            amc = (p.product.average_monthly_consumption if p.product.average_monthly_consumption else 100)
            q = normalvariate(amc, amc / 4)
            print "Generating REC report for %s : %s : %d" % (hsa.name, p.product.sms_code, q)
            r = ProductReport(product=p.product, report_type = ProductReportType.objects.get(code='rec'),
                          quantity=q, message=None, supply_point=hsa, report_date=date)
            r.save()
    else:
        pass
Ejemplo n.º 2
0
    def testConsumption(self):
        self.sp.report_stock(self.pr, 200)

        # Not enough data.
        self.assertEquals(None, self.ps.daily_consumption)
        self.assertEquals(self.ps.product.average_monthly_consumption,
                          self.ps.monthly_consumption)  #fallback

        self.ps.monthly_consumption = 999

        st = StockTransaction.objects.get(supply_point=self.sp,
                                          product=self.pr)
        st.date = st.date - timedelta(days=5)
        st.save()
        self.sp.report_stock(self.pr, 150)

        # 5 days still aren't enough to compute.
        self.ps = ProductStock.objects.get(supply_point=self.sp,
                                           product=self.pr)
        self.assertEquals(None, self.ps.daily_consumption)
        self.assertEquals(self.ps.monthly_consumption,
                          self.ps.monthly_consumption)

        sta = StockTransaction.objects.filter(supply_point=self.sp,
                                              product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=5)
            st.save()
        self.sp.report_stock(self.pr, 100)

        # 10 days is enough.
        self.ps = ProductStock.objects.get(supply_point=self.sp,
                                           product=self.pr)
        # 200 in stock 10 days ago, 150 in stock 5 days ago, 100 now
        self.assertEquals(10, round(self.ps.daily_consumption))
        self.assertEquals(300, round(self.ps.monthly_consumption))

        sta = StockTransaction.objects.filter(supply_point=self.sp,
                                              product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=10)
            st.save()
        # 200 in stock 20 days ago, 150 in stock 15 days ago, 100 10 days ago, 50 now
        self.sp.report_stock(self.pr, 50)

        # Another data point.
        self.ps = ProductStock.objects.get(supply_point=self.sp,
                                           product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))
        # Make sure the rounding is correct
        self.assertEquals(225, round(self.ps.monthly_consumption))

        sta = StockTransaction.objects.filter(supply_point=self.sp,
                                              product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=10)
            st.save()
        self.sp.report_stock(self.pr, 100)

        # Reporting higher stock shouldn't change the daily consumption
        # since we have no way of knowing how much was received vs dispensed.
        self.ps = ProductStock.objects.get(supply_point=self.sp,
                                           product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))

        npr = ProductReport(
            product=self.pr,
            report_type=ProductReportType.objects.get(code=Reports.REC),
            quantity=10000,
            message=None,
            supply_point=self.sp)
        npr.save()

        # Make sure receipts after a soh report don't change the daily consumption
        # since we don't know what the new balance is. Only change consumption
        # when we get the soh report after the receipt.
        self.ps = ProductStock.objects.get(supply_point=self.sp,
                                           product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))
Ejemplo n.º 3
0
    def testConsumption(self):
        self.sp.report_stock(self.pr, 200) 

        # Not enough data.
        self.assertEquals(None, self.ps.daily_consumption)
        self.assertEquals(self.ps.product.average_monthly_consumption, self.ps.monthly_consumption) #fallback

        self.ps.monthly_consumption = 999

        st = StockTransaction.objects.get(supply_point=self.sp,product=self.pr)
        st.date = st.date - timedelta(days=5)
        st.save()
        self.sp.report_stock(self.pr, 150)
        
        # 5 days still aren't enough to compute.
        self.ps = ProductStock.objects.get(supply_point=self.sp, product=self.pr)
        self.assertEquals(None, self.ps.daily_consumption)
        self.assertEquals(self.ps.monthly_consumption, self.ps.monthly_consumption)

        sta = StockTransaction.objects.filter(supply_point=self.sp,product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=5)
            st.save()
        self.sp.report_stock(self.pr, 100)

        # 10 days is enough.
        self.ps = ProductStock.objects.get(supply_point=self.sp, product=self.pr)
        # 200 in stock 10 days ago, 150 in stock 5 days ago, 100 now
        self.assertEquals(10, round(self.ps.daily_consumption)) 
        self.assertEquals(300, round(self.ps.monthly_consumption))

        sta = StockTransaction.objects.filter(supply_point=self.sp,product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=10)
            st.save()
        # 200 in stock 20 days ago, 150 in stock 15 days ago, 100 10 days ago, 50 now
        self.sp.report_stock(self.pr, 50) 

        # Another data point.
        self.ps = ProductStock.objects.get(supply_point=self.sp, product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))
        # Make sure the rounding is correct
        self.assertEquals(225, round(self.ps.monthly_consumption))

        sta = StockTransaction.objects.filter(supply_point=self.sp,product=self.pr)
        for st in sta:
            st.date = st.date - timedelta(days=10)
            st.save()
        self.sp.report_stock(self.pr, 100)

        # Reporting higher stock shouldn't change the daily consumption
        # since we have no way of knowing how much was received vs dispensed.
        self.ps = ProductStock.objects.get(supply_point=self.sp, product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))

        npr = ProductReport(product=self.pr, report_type=ProductReportType.objects.get(code=Reports.REC),
                            quantity=10000, message=None, supply_point=self.sp)
        npr.save()
        
        # Make sure receipts after a soh report don't change the daily consumption
        # since we don't know what the new balance is. Only change consumption 
        # when we get the soh report after the receipt.
        self.ps = ProductStock.objects.get(supply_point=self.sp, product=self.pr)
        self.assertEquals(7.5, round(self.ps.daily_consumption, 1))