def __get_time(self, time): now = client_timezone(timezone.now()) if time == "today": begin = now.strftime(self.time_format) end = (now + timezone.timedelta(days=1)).strftime(self.time_format) elif time == "yesterday": begin = (now - timezone.timedelta(days=1)).strftime( self.time_format) end = now.strftime(self.time_format) elif time == "week": begin_of_week = now - timezone.timedelta(days=now.weekday()) begin = begin_of_week.strftime(self.time_format) end = (begin_of_week + timezone.timedelta(days=6)).strftime( self.time_format) elif time == "month": begin = now.strftime("%Y-%m-01") end = ( now + timezone.timedelta(days=mdays[now.month])).strftime("%Y-%m-01") elif time == "lastweek": begin_of_week = now - timezone.timedelta(days=now.weekday()) begin = (begin_of_week - timezone.timedelta(days=7)).strftime( self.time_format) end = begin_of_week.strftime(self.time_format) elif time == "lastmonth": begin = ( now - timezone.timedelta(days=mdays[now.month])).strftime("%Y-%m-01") end = now.strftime("%Y-%m-01") print(begin, end) return begin, end
def pay_order(self): customer_pay = int(self.request.POST.get("customerGiven")) if customer_pay < self.calc_total_fees(): raise SalesException("Khách hàng thanh toán chưa đủ.") self.create_new_order(self.request.POST.get("note")) invoice = Invoice(total_products=self.order.get_total_products(), total=self.order.calc_total_money(), discount=self.order.calc_discount(), customer_given=customer_pay, order=self.order, staff=self.request.user, store=self._store) self.invoice = invoice.save() # save to elasticsearch elasticsearch.index_invoice(invoice_id=invoice.id, invoice_code=invoice.invoice_code, total=invoice.must_pay, total_product=invoice.total_products, time_created=client_timezone( invoice.time_create), staff=invoice.staff.username, order_id=invoice.order.id, status=invoice.status, store_id=invoice.store.id) return invoice
def _get_time(self): time = self.request.GET["key_time"] begin_time = None end_time = None if time.lower() == "month": now = client_timezone(timezone.now()) begin_time = now - timezone.timedelta(days=now.day - 1) end_time = now elif time.lower() == "week": now = client_timezone(timezone.now()) begin_time = now - timezone.timedelta(days=now.weekday()) end_time = begin_time + timezone.timedelta(days=6) elif time.lower() == "today": begin_time = end_time = client_timezone(timezone.now()) elif time.lower() == "yesterday": now = client_timezone(timezone.now()) begin_time = end_time = now - timezone.timedelta(days=1) return begin_time, end_time
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() today = client_timezone(timezone.now()) yesterday = today - timezone.timedelta(days=1) last_month = today - timezone.timedelta(days=mdays[today.month]) analysis = elasticsearch.aggregate_sales_today( today=today.strftime("%Y-%m-%d"), today_begin=today.strftime("%Y-%m-01"), yesterday=yesterday.strftime("%Y-%m-%d"), last_month_begin=last_month.strftime("%Y-%m-01"), last_month=last_month.strftime("%Y-%m-%d")) data_response = {"today": analysis["today"]} if analysis["today"]["total_invoices"] > 0 and analysis["yesterday"][ "total_invoices"] > 0: revenue_today = analysis["today"]["revenue"] revenue_yesterday = analysis["yesterday"]["revenue"] compare_revenue_with_yesterday = ( revenue_today - revenue_yesterday) / revenue_yesterday * 100 data_response["compare_with_yesterday"] = round( compare_revenue_with_yesterday, 2) if analysis["lastmonth"]["total_invoices"] > 0: revenue_last_month = analysis["lastmonth"]["revenue"] revenue_this_month = analysis["now"]["revenue"] # print(revenue_last_month) # print(revenue_this_month) compare_revenue_with_last_month = ( revenue_this_month - revenue_last_month) / revenue_last_month * 100 data_response["compare_with_lastmonth"] = round( compare_revenue_with_last_month, 2) context = {'active': 'dashboard', 'store_name': store_name} context.update(data_response) return render(request, template_name='core/dashboard.html', context=context)
def datetime_format(date, client_tz=None): if date: date = client_timezone(date, client_tz) return date.strftime("%d/%m/%Y %H:%M:%S") return ''
def check_out(request): if request.method == 'POST': try: data = json.loads(request.body) license_number = data['license_number'] except (json.decoder.JSONDecodeError, KeyError): return JsonResponse(data={ 'status': 'fail', 'code': 1, 'message': 'Body must have license_number field' }, status=404) cars = Car.objects.filter(license_plate_number=license_number) if cars.exists(): try: history = ParkingHistory.objects.get(car=cars[0], time_out__isnull=True, fees__isnull=True) history.time_out = timezone.now() history.fees, hours = calc_fees(time_in=history.time_in, time_out=history.time_out) history.save() elasticsearch.update_parking_history_time_out( id=history.id, time_out=history.time_out, fees=history.fees) return JsonResponse( data={ 'status': 'success', 'fees': history.fees, 'time_in': client_timezone(history.time_in).strftime( "%d/%m/%Y %H:%M:%S"), 'time_out': client_timezone(history.time_out).strftime( "%d/%m/%Y %H:%M:%S"), 'duration': hours }) except ParkingHistory.DoesNotExist: return JsonResponse( data={ 'status': 'fail', 'code': 3, 'message': 'Fraud detection: the car with the license number %s not in parking' % license_number }) else: return JsonResponse( data={ 'status': 'fail', 'code': 2, 'message': 'License plate number does not exists' }) return JsonResponse(data={ 'status': 'fail', })