Esempio n. 1
0
    def redo_totals(self):
        shops = EtsyShops.all().fetch(500)

        #first delete all the totals
        totals = Totals.all().filter("name =", "shop_income").fetch(500)
        totals.extend(Totals.all().filter("name =", "shop_expense").fetch(500))
        db.delete(totals)

        for shop in shops:
            goods = storage.Goods.all().filter("status =", "sold") \
                               .filter("shop =", shop) \
                               .order("sold")
            new_totals = appetsy.totals(goods,
                                           lambda x: x.sold.date().replace(day = 1),
                                           lambda x: x.price)

            for total in new_totals:
                Totals.add_income(shop, total, new_totals[total][0])
                self.write("%d: %s %s <br />" % (shop.id, total.strftime("%Y%m"), new_totals[total]))

            self.write("<br />")
            expenses = Expenses.all().filter("shop =", shop).order("purchase_date")
            new_totals = appetsy.totals(expenses,
                                           lambda x: x.purchase_date.replace(day = 1),
                                           lambda x: x.price)

            for total in new_totals:
                Totals.add_expense(shop, total, new_totals[total][0])
                self.write("%d: %s %s <br />" % (shop.id, total.strftime("%Y%m"), new_totals[total]))

            self.write("<br /><br />")
Esempio n. 2
0
    def balance(self):
        balance_month = self.request.get("month")
        if balance_month:
            this_month = dt.datetime.strptime(balance_month, "%d-%b-%Y").date()
        else:
            this_month = appetsy.today(self.shop).replace(day = 1).date()
            
        # timedelta doesn't do month, so we just add maximum possible days and roll back to first one
        next_month = (this_month + dt.timedelta(days=31)).replace(day=1)
        
        balance_cache = memcache.get("balance", namespace = str(self.shop.id)) or {}
        
        if this_month not in balance_cache:
            expenses = storage.Expenses.all().filter("shop =", self.shop) \
                                     .filter("purchase_date >=", this_month) \
                                     .filter("purchase_date <", next_month) \
                                     .order("purchase_date")        
            for expense in expenses:
                expense.price = -expense.price
                
            goods = storage.Goods.all().filter("status =", "sold") \
                               .filter("shop =", self.shop) \
                               .filter("sold >", this_month) \
                               .filter("sold <", next_month) \
                               .order("sold") \
                               .order("status")
            
            moneys = []
            
            for expense in expenses:
                moneys.append({"date": expense.purchase_date,
                               "price": -(expense.price or 0),
                               "name": expense.name,
                               "creation_date": None,
                               "key": expense.key()
                })
                
            for good in goods:
                moneys.append({"date": good.sold.date(),
                               "price": good.price or 0,
                               "name": good.name,
                               "creation_date": good.created,
                               "key": good.key(),
                               "listing": good.listing
                })
                
            moneys.sort(key = lambda x: x["date"])
            
            
            dates = []
            for group in itertools.groupby(moneys, lambda entry: entry["date"].month):
                dates.append ({"month": group[0],
                               "moneys": [z for z in group[1]]
                               })
            dates.reverse()
            
            prev_totals = storage.Totals.all().filter("shop =", self.shop) \
                                      .filter("name in", ["shop_income"]) \
                                      .fetch(500)
            
            prev_totals = appetsy.totals(prev_totals,
                                            lambda x: x.date.date().replace(day=1),
                                            lambda x: x.total)
            
            
            balance_cache[this_month] = appetsy.get_template("expenses/index.html").render(dates = dates, prev = prev_totals)
            memcache.set("balance", balance_cache, namespace = str(self.shop.id))


        spotlight = self.request.get("spotlight")
        
        balance = balance_cache[this_month]
        if spotlight:
            balance = balance.decode("utf-8").replace("balance_spotlight = null",
                                                      "balance_spotlight = \"%s\"" % spotlight).encode("utf-8")

        return balance