def get_done_bills(self, request, **kwargs): supplier = self.get_object() queryset = supplier.bills.all() page = self.paginate_queryset(queryset) if page is not None: serializer = SupplierBillSerializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = SupplierBillSerializer(queryset, many=True) return Response(serializer.data)
def add_payments(self, request, **kwargs): payment_type = self.request.data.get('type') bill = self.get_object() amount = self.request.data.get('amount') payment = OurPayment.objects.create(create_date=timezone.now(), amount=float(amount), bill=bill, type=payment_type) if payment_type == "cheque": amount = self.request.data.get('amount') bank = self.request.data.get('bank') number = self.request.data.get('number') issue_date = self.request.data.get('issue_date', timezone.now().date()) expiry_date = self.request.data.get('expiry_date') cheque = OurCheque.objects.create(number=int(number), bank=bank, issue_date=issue_date, expiry_date=expiry_date, amount=int(amount), customer=bill.buyer) payment.cheque = cheque payment.save() data = SupplierBillSerializer( SupplierBill.objects.get(pk=bill.pk)).data return Response(data, status=status.HTTP_201_CREATED)
def add_bill(self, request, **kwargs): data = self.request.data items = data.get('items') supplier = self.get_object() currency_price = data.get('currency_price', 1) currency = data.get('currency', 'ریال') bill = SupplierBill.objects.create(supplier=supplier, currency_price=currency_price, currency=currency) for item in items: product_code = item['product'] try: product = Product.objects.get(code=product_code) except ObjectDoesNotExist: raise ValidationError( 'محصولی با کد {} وجود ندارد.'.format(product_code)) amount = item['amount'] raw_price = item.get('price', 0) SupplierBillItem.objects.create(product=product, amount=amount, bill=bill, raw_price=raw_price) serializer = SupplierBillSerializer(bill) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def destroy(self, request, *args, **kwargs): bill = self.get_object().bill bill_item_product = self.get_object().product bill_item_amount = self.get_object().amount bill_item_product.update_stock_amount(bill_item_amount) serializer = SupplierBillSerializer(bill) headers = self.get_success_headers(serializer.data) return Response(serializer.data, headers=headers)
def create(self, request, *args, **kwargs): bill = Bill.objects.get(pk=self.request.data.get('bill', None)) staff = Staff.objects.get(username=request.user.username) if staff.job in self.non_creator: raise PermissionDenied item = self.request.data product_code = item['product'] try: product = Product.objects.get(code=product_code) except ObjectDoesNotExist: raise ValidationError( 'محصولی با کد {} وجود ندارد.'.format(product_code)) amount = item['amount'] raw_price = item.get('price', 0) SupplierBillItem.objects.create(product=product, amount=amount, bill=bill, raw_price=raw_price) serializer = SupplierBillSerializer(bill) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def update(self, request, *args, **kwargs): bill = self.get_object().bill super(SupplierBillItemViewSet, self).update(request, *args, **kwargs) serializer = SupplierBillSerializer(bill) headers = self.get_success_headers(serializer.data) return Response(serializer.data, headers=headers)