def show_mobile(request): user_id = get_current_user() context = {} spents = Spent.objects.filter(spent_by=user_id) if len(spents): spents = spents.filter(category=Category.objects.get(code="MB")) context['spents'] = spents.order_by('-id') status = [] valids = [] for spent in context['spents']: delta = datetime.date.today() - spent.date validity = 0 if spent.validity == "NA" else int(spent.validity) diff = validity - delta.days if diff > 3: status.append("Green") valids.append(diff) elif diff in range(1, 4): status.append("Yellow") valids.append(diff) else: status.append("Red") valids.append(0) context['status'] = status context['valid'] = valids if request.method == "GET": return render(request, 'kharch/mobile.html', context)
def delete_item(request): user_id = get_current_user() if request.method == "GET": form = ItemForm() items = Item.objects.filter(added_by=user_id) context = {} context['form'] = form context['items'] = items return render(request, 'kharch/delete_item.html', context) elif request.method == "POST": edited = request.POST.getlist('items[]') flag = False for item in edited: spent = Spent.objects.filter(item=Item.objects.get(name=item)) if len(spent) < 1: obj = Item.objects.get(name=item) obj.delete() else: flag = True if flag: form = ItemForm() items = Item.objects.filter(added_by=user_id) context = {} context['form'] = form context[ 'error'] = "Already purchased items cannot be deleted. Contact Admin" context['items'] = items return render(request, 'kharch/delete_item.html', context) else: url = urlresolvers.reverse('kharch:delete_item') return HttpResponseRedirect(url)
def edit_item(request): user_id = get_current_user() if request.method == "GET": form = ItemForm() items = Item.objects.filter(added_by=user_id) context = {} context['form'] = form context['items'] = items return render(request, 'kharch/edit_item.html', context) elif request.method == "POST": edited = request.POST.getlist('items[]') for item in edited: obj = Item.objects.get(name=item) name = request.POST[item] cost = request.POST[item + "_cost"] category = request.POST[item + "_sel"] try: exists = Item.objects.get(name=name) exists = True except: exists = False if not exists: obj.name = name obj.cost = cost obj.category = Category.objects.get(code=category) obj.save() url = urlresolvers.reverse('kharch:edit_item') return HttpResponseRedirect(url)
def debit_credit(request): user_id = get_current_user() context = {} spent = Spent.objects.filter(spent_by=user_id) if len(spent): credit = spent.filter(payment=Payment.objects.get(code="CR")) online = spent.filter(payment=Payment.objects.get(code="DB")) context['credit'] = credit context['online'] = online if request.method == "GET": return render(request, 'kharch/db_cr.html', context)
def pay_due(request): user_id = get_current_user() if request.method == "POST": spent = Spent.objects.filter(spent_by=user_id) credit = spent.filter(payment=Payment.objects.get(code="CR")) for buy in credit: buy.paid = True buy.pay_date = datetime.datetime.now() buy.save() url = urlresolvers.reverse('kharch:pay_due') return HttpResponseRedirect(url) if request.method == "GET": return render(request, 'kharch/pay_due.html', {})
def dashboard(request, template_name="accounts/registration/dashboard.html"): user_id = get_current_user() context = {} cash = Cash.objects.filter(loaded_by=user_id) cash = sum(cash.values_list('capital', flat=True)) context['total'] = cash spent = Spent.objects.filter(spent_by=user_id) context['spent_on'] = spent roti = spent.filter(category=Category.objects.get(code='RT')) roti = int(sum(roti.values_list('price', flat=True))) kapda = spent.filter(category=Category.objects.get(code='KP')) kapda = int(sum(kapda.values_list('price', flat=True))) makan = spent.filter(category=Category.objects.get(code='MK')) makan = int(sum(makan.values_list('price', flat=True))) other = spent.filter(category=Category.objects.get(code='OT')) other = int(sum(other.values_list('price', flat=True))) mobile = spent.filter(category=Category.objects.get(code='MB')) mobile = int(sum(mobile.values_list('price', flat=True))) context['spent'] = sum([roti, kapda, makan, other, mobile]) credit = spent.filter(payment=Payment.objects.get(code="CR")).filter( paid=False) credit = int(sum(credit.values_list('price', flat=True))) online = spent.filter(payment=Payment.objects.get(code="DB")).filter( paid=True) online = int(sum(online.values_list('price', flat=True))) context['online'] = online context['credit'] = credit context['remain'] = context['total'] - context['spent'] + context[ 'credit'] + context['online'] return render(request, template_name, context)
def edit_spent(request): user_id = get_current_user() if request.method == "GET": context = {} return render(request, 'kharch/mobile.html', context) else: if "dashboard" in request.POST: spent_id = int(request.POST['dashboard']) spent = Spent.objects.get(pk=spent_id) spent_date = request.POST['spent_date_%s' % request.POST['dashboard']].split("-") year, month, day = [int(part) for part in spent_date] spent.date = datetime.date(year, month, day) spent.validity = int( request.POST['validity'] ) if request.POST['validity'] != "NA" else "NA" spent.cost = float(request.POST['cost']) spent.payment = Payment.objects.get(code=request.POST['%d_sel' % spent_id]) spent.save() url = urlresolvers.reverse('accounts:dashboard') return HttpResponseRedirect(url) else: spent_id = int(request.POST['hidden']) spent = Spent.objects.get(pk=spent_id) spent_date = request.POST['spent_date_%s' % request.POST['hidden']].split("-") year, month, day = [int(part) for part in spent_date] spent.date = datetime.date(year, month, day) spent.validity = int( request.POST['validity'] ) if request.POST['validity'] != "NA" else "NA" spent.cost = float(request.POST['cost']) spent.payment = Payment.objects.get(code=request.POST['%d_sel' % spent_id]) spent.save() url = urlresolvers.reverse('kharch:mobile') return HttpResponseRedirect(url)
def history(request): user_id = get_current_user() context = {} items = list( chain(Item.objects.filter(added_by=user_id), Item.objects.filter(inherited_by=user_id))) spent = Spent.objects.filter(spent_by=user_id) roti = spent.filter(category=Category.objects.get(code="RT")) roti = int(sum(roti.values_list('price', flat=True))) kapda = spent.filter(category=Category.objects.get(code="KP")) kapda = int(sum(kapda.values_list('price', flat=True))) makan = spent.filter(category=Category.objects.get(code="MK")) makan = int(sum(makan.values_list('price', flat=True))) other = spent.filter(category=Category.objects.get(code="OT")) other = int(sum(other.values_list('price', flat=True))) mobile = spent.filter(category=Category.objects.get(code="MB")) mobile = int(sum(mobile.values_list('price', flat=True))) series = [] for item in items: data = int( sum( spent.filter(item=Item.objects.get( name=item.name)).values_list('price', flat=True))) series.append("%s-%s-%d" % (item.category.code, item.name, data)) context['series'] = "<|>".join(series) return render(request, 'kharch/history.html', context)
def index(request): user_id = get_current_user() if request.method == "GET" and user_id is not None: form = CapitalForm() cash = Cash.objects.filter(loaded_by=user_id) cash = sum(cash.values_list('capital', flat=True)) items = list( chain(Item.objects.filter(added_by=user_id), Item.objects.filter(inherited_by=user_id))) spent = Spent.objects.filter(spent_by=user_id) if len(spent): roti = spent.filter(category=Category.objects.get(code="RT")) roti = int(sum(roti.values_list('price', flat=True))) kapda = spent.filter(category=Category.objects.get(code="KP")) kapda = int(sum(kapda.values_list('price', flat=True))) makan = spent.filter(category=Category.objects.get(code="MK")) makan = int(sum(makan.values_list('price', flat=True))) other = spent.filter(category=Category.objects.get(code="OT")) other = int(sum(other.values_list('price', flat=True))) mobile = spent.filter(category=Category.objects.get(code="MB")) mobile = int(sum(mobile.values_list('price', flat=True))) credit = spent.filter(payment=Payment.objects.get( code="CR")).filter(paid=False) credit = int(sum(credit.values_list('price', flat=True))) online = spent.filter(payment=Payment.objects.get( code="DB")).filter(paid=True) online = int(sum(online.values_list('price', flat=True))) remain = cash - sum([roti, kapda, makan, other, mobile ]) + credit + online else: roti = kapda = makan = other = mobile = 0 remain = cash context = {} context['cash'] = remain context['roti'] = roti context['kapda'] = kapda context['makan'] = makan context['other'] = other context['mobile'] = mobile context['form'] = form context['items'] = items context['meta_keywords'] = "Overrided Meta Keywords" return render(request, 'kharch/index.html', context) if request.method == "POST" and user_id is not None: if request.POST['hidden'] == 'Capital': form = CapitalForm(request.POST) if form.is_valid(): capital = int(request.POST['capital']) if capital > 0: Cash.objects.create(capital=capital) elif request.POST['hidden'] == 'Item': total = 0.0 brought = request.POST.getlist('items[]') for item in brought: payment_mode = "%s_sel" % request.POST[item] if payment_mode == "RK": total += float(request.POST[item]) cash = Cash.objects.filter(loaded_by=user_id) cash = sum(cash.values_list('capital', flat=True)) spent = Spent.objects.filter(spent_by=user_id) if len(spent): roti = spent.filter(category=Category.objects.get(code="RT")) roti = int(sum(roti.values_list('price', flat=True))) kapda = spent.filter(category=Category.objects.get(code="KP")) kapda = int(sum(kapda.values_list('price', flat=True))) makan = spent.filter(category=Category.objects.get(code="MK")) makan = int(sum(makan.values_list('price', flat=True))) other = spent.filter(category=Category.objects.get(code="OT")) other = int(sum(other.values_list('price', flat=True))) mobile = spent.filter(category=Category.objects.get(code="MB")) mobile = int(sum(mobile.values_list('price', flat=True))) credit = spent.filter(payment=Payment.objects.get( code="CR")).filter(paid=False) credit = int(sum(credit.values_list('price', flat=True))) online = spent.filter(payment=Payment.objects.get( code="DB")).filter(paid=True) online = int(sum(online.values_list('price', flat=True))) remain = cash - sum([roti, kapda, makan, other, mobile ]) + credit + online else: roti = kapda = makan = other = mobile = 0 remain = cash if total > remain: url = urlresolvers.reverse('kharch:buy_error') return HttpResponseRedirect(url) for item in brought: payment = request.POST[item + "_sel"] obj = Item.objects.get(name=item) category = obj.category added_by = obj.added_by append = request.POST.getlist('append[]') date = request.POST["buy_date_" + item] date = datetime.datetime.strptime( date, '%d/%m/%Y').strftime('%Y-%m-%d') if item in append: obj.inherited_by.add(user_id) obj.save() paid = False if payment == "CR" else True validity = 28 if obj.category.code == "MB" else 0 Spent.objects.create( item=obj, paid=paid, validity=validity, category=Category.objects.get(id=obj.category_id), payment=Payment.objects.get(code=payment), price=request.POST[item], date=date, pay_date=date) return HttpResponseRedirect(".") else: url = urlresolvers.reverse('accounts:login') return HttpResponseRedirect(url)
def save(self, *args, **kwargs): current_user = get_current_user() if not self.pk: self.author = current_user self.last_update = current_user super(Classes, self).save(*args, **kwargs)