def post(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() orders = request.session['orders'] if len(orders) == 15: response = { "status": "failed", "msg": "Không thể vượt quá 15 hoá đơn cùng lúc." } return JsonResponse(data=response, status=200) list_oids = [o['id'] for o in orders] list_oids.sort() new_id = 1 for x in list_oids: if x == new_id: new_id += 1 orders.append({'id': new_id, 'data': []}) response = { "status": "success", "oid": new_id, } request.session.modified = True return JsonResponse(data=response, status=200)
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() form = self.form_class() context = {"form": form, "store_name": store_name} return render(request, template_name=self.template_name, context=context)
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() query = request.GET.get('q') products = services.search_products(query) html = render(request, template_name='sales/product_search_item.html', context={'data': products}) return HttpResponse(html)
def get(self, request, store_name, *args, **kwargs): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() request.session['orders'] = [{'id': 1, 'data': []}] customer_form = CustomerForm() context = {'oid': 1, 'form': customer_form, 'store_name': store_name} return render(request, template_name='sales/sales.html', context=context)
def get(self, request, store_name, pk): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() category = get_object_or_404(self.model, pk=pk) form = self.form_class(instance=category) context = {"form": form, "id": category.id, "store_name": store_name} return render(request, template_name="category/update_category.html", context=context)
def get(self, request, store_name, pk): """ Get invoice detail """ try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() invoice = get_object_or_404(Invoice, pk=pk) context = {"invoice": invoice, "store_name": store_name} return render(request, template_name=self.template_name, context=context)
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() form = self.form_class() context = { 'form': form, 'action': 'add', 'url': reverse('products:product-creation', args=[store_name]), 'active': 'products', 'store_name': store_name } return render(request, self.template_name, context=context)
def post(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() try: service = self.service_class(request, store, oid) except OrderDoesNotExists: return HttpResponse(status=404) pid = request.POST.get('productId') product = get_object_or_404(Product, pk=pid) context = service.add_product_to_order(product) request.session.modified = True print(request.session['orders']) html = render_to_string(template_name='sales/product_item.html', context=context) response = { "eid": product.id, "order": html, "payment": service.make_payment() } return JsonResponse(response, status=200)
def patch(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() pid = request.PATCH.get('productId') quantity = float(request.PATCH.get('quantity')) try: service = self.service_class(request, store, oid) pdict = service.update_quantity_product_item(product_id=int(pid), quantity=quantity) except OrderDoesNotExists: return HttpResponse(status=404) except (ObjectDoesNotExist, SalesException) as e: response = {"status": "failed", "msg": str(e)} return JsonResponse(data=response, status=200) request.session.modified = True response = { "status": "success", "subtotal": int(pdict['quantity'] * pdict['product']['sell_price']), "payment": service.make_payment() } return JsonResponse(data=response, status=200)
def post(self, request, store_name, iid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() response = {"status": "failed"} try: service = self.service_class(request=request, store=store, oid=iid) except OrderDoesNotExists: return HttpResponse(status=404) try: invoice = service.pay_order() except SalesException as e: response["msg"] = str(e) return JsonResponse(data=response, status=200) request.session.modified = True response["status"] = "success" context = { "invoice": invoice, "list_product_items": invoice.order.get_list_product_items(), "customer": invoice.order.customer, "store": store } html = render_to_string(template_name="sales/invoice.html", context=context) response["data"] = html return JsonResponse(data=response, status=200)
def post(self, request, store_name): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() service = self.service_class(request, store) response = service.get_invoices_datatables() return JsonResponse(response)
def get(self, request, store_name, pk): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() product = get_object_or_404(self.model, pk=pk) list_imgs = product.get_list_images() if not list_imgs: list_imgs.append('/resource/images/default_product.jpg') context = { self.context_object_name: product, 'list_imgs': list_imgs, 'store_name': store_name } html = render(request, self.template_name, context=context) return HttpResponse(html)
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() table_columns = [ '', 'Mã hoá đơn', 'Thời gian', 'Khách hàng', 'Giảm giá', 'Tổng hoá đơn' ] context = { "table_columns": table_columns, "active": "transactions", "store_name": store_name } return render(request, template_name="invoice/invoice.html", context=context)
def get(self, request, store_name): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() table_columns = [ '', 'Mã sản phẩm', 'Tên sản phẩm', 'Giá bán', 'Giá vốn', 'Tồn kho' ] categories = Category.objects.all() context = { 'table_columns': table_columns, 'list_categories': categories, 'active': 'products', 'store_name': store_name } return render(request, template_name='product/products.html', context=context)
def post(self, request, store_name, pk): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() category = get_object_or_404(self.model, pk=pk) form = self.form_class(request.POST, instance=category) response = {'status': 'success'} if form.is_valid(): form.save() else: response['status'] = "failed" errors = [] for field in form: for error in field.errors: errors.append({'id': field.auto_id, 'error': error}) response['data'] = errors return JsonResponse(data=response, status=200)
def post(self, request, store_name): """ Handle operations sorting, paging, getting of datatables. Return json response to datatables """ try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() response = self.service_class(request, store).get_products_datatables() return JsonResponse(response)
def post(self, request, store_name, oid): try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() oid = int(oid) try: service = self.service_class(request, oid) except OrderDoesNotExists: return HttpResponse(status=400) try: service.create_new_order(note=request.POST.get("note"), status=2) except SalesException as e: response = {"status": "failed", "msg": str(e)} return JsonResponse(data=response, status=200) response = { "status": "success", } return JsonResponse(data=response, status=200)
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 delete(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() try: service = self.service_class(request, store, oid) service.remove_customer() request.session.modified = True except OrderDoesNotExists: return HttpResponse(status=404) return HttpResponse(status=200)
def patch(self, request, store_name, pk): """ Method for updating invoice status """ try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() new_status = int(request.PATCH["status"]) self.service_class(request, store).change_invoice_status(pk, new_status) return HttpResponse(200)
def delete(self, request, store_name, *args, **kwargs): """ Move a product or list products to trash (status = 0) """ try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() if self.lookup_field in kwargs: _id = kwargs[self.lookup_field] product = get_object_or_404(self.model, pk=_id) product.status = 0 product.save() else: ids = request.DELETE.getlist('list_ids[]') delete_objs = self.queryset.filter(id__in=ids) if delete_objs and len(delete_objs) > 0: for obj in delete_objs: obj.status = 0 self.model.objects.bulk_update(delete_objs, ['status']) return JsonResponse({'status': 'success'})
def get(self, request, store_name, pk): """ Return a product form as html. Change product's available property to INT type if product's unit property equal UNIT_INT """ try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() product = get_object_or_404(self.model, pk=pk) if product.unit == Product.UNIT_INT: # change available to INT type product.available = int(product.available) form = self.form_class(instance=product) context = { 'action': 'update', 'form': form, 'url': reverse('products:product-update', args=[store_name, pk]), } html = render(request, self.template_name, context=context) return HttpResponse(html)
def patch(self, request, store_name, *args, **kwargs): """ Change status of product or list products. """ try: StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() if self.lookup_field in kwargs: _id = kwargs[self.lookup_field] product = get_object_or_404(self.model, pk=_id) product.status = 2 product.save() else: ids = request.PATCH.getlist('list_ids[]') new_status = int(request.PATCH.get("newStatus")) objs = self.model.objects.filter(id__in=ids) if objs and len(objs) > 0: for obj in objs: obj.status = new_status self.model.objects.bulk_update(objs, ['status']) return JsonResponse({'status': 'success'})
def patch(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() try: service = self.service_class(request, store, oid) except OrderDoesNotExists: return HttpResponse(status=404) customer_id = request.PATCH.get("customerId") customer = get_object_or_404(Customer, pk=customer_id) service.add_customer_to_order(customer) request.session.modified = True response = {"id": customer.id, "name": customer.customer_name} return JsonResponse(data=response, status=200)
def get(self, request, store_name): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() key_search = request.GET.get('q') result = self.service_class(request, store).search_customer_by_key(key_search) if result["num_of_results"] == 0: context = {"empty": True} else: context = {"list_customers": result["list_customers"]} return render(request=request, template_name="sales/customer_search_item.html", context=context)
def post(self, request, store_name): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() form = self.form_class(request.POST) if form.is_valid(): form.save(store=store) response = {"status": "success"} return JsonResponse(data=response, status=200) errors = [] for field in form: for error in field.errors: errors.append({'id': field.auto_id, 'error': error}) response = {"status": "failed", "data": errors} return JsonResponse(data=response, status=200)
def post(self, request, store_name): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() form = self.form_class(request.POST, request.FILES) if form.is_valid(): form.save(store=store) return self.get(request, store_name) context = { 'form': form, 'url': reverse('products:product-creation', args=[store_name]), 'store_name': store_name } return render(request, self.template_name, context=context)
def delete(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() try: service = self.service_class(request, store, oid) except OrderDoesNotExists: return HttpResponse(status=404) pid = request.DELETE.get('productId') order_data = service.remove_product_item(int(pid)) request.session.modified = True html = render_to_string(template_name='sales/cart.html', context={'list_items': order_data}) response = {"order": html, "payment": service.make_payment()} return JsonResponse(data=response, status=200)
def get(self, request, store_name, oid): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() oid = int(oid) try: service = self.service_class(request, store, oid) except OrderDoesNotExists: return HttpResponse(status=400) index, order = service.get_order_dict() context = {'oid': order['id'], 'list_items': order['data']} html = render_to_string(template_name='sales/cart.html', context=context) response = {"order": html, "payment": service.make_payment()} if "customer" in order: response.update({"customer": order["customer"]}) return JsonResponse(data=response, status=200)
def post(self, request, store_name): try: store = StoreManagement.valid_store_user(store_name, request.user) except UserNotInStoreException: raise Http404() service = self.service_class(request, store) response = {"status": "success"} try: instance = service.validate_form() response["data"] = { "id": instance.id, "name": instance.customer_name } except FormInvalidException: errors = service.get_errors() response["status"] = "failed" response["data"] = errors print(response) return JsonResponse(data=response, status=200)