def calculate_order_remain(orders: models.QuerySet) -> dict: from apps.order.utils import OrderUtils result = {} for order in orders: result[str(order.pk)] = { 'uid': order.uid, 'remain': OrderUtils.get_vnd_total_obj(order) - OrderUtils.get_deposit_amount(order), } return result
def setUp(self): self.customer = CustomerUtils.seeding(1, True) self.staff = StaffUtils.seeding(1, True) self.order = OrderUtils.seeding(1, True, customer=self.customer) self.order.status = Status.NEW OrderItemUtils.seeding(1, True, order=self.order) self.bol = BolUtils.seeding(1, True, order=self.order) self.bol.mass = 5 self.bol.save() OrderUtils.force_cal(self.order)
def export_order_bols( bols: models.QuerySet, receipt: models.QuerySet, customer: models.QuerySet, staff: models.QuerySet ) -> int: from apps.order.utils import OrderUtils from apps.transaction.utils import TransactionUtils total = 0 for bol in bols: bol.receipt = receipt bol.exported_date = timezone.now() bol.exporter = staff bol.save() orders = OrderUtils.get_orders_from_bols(bols) order_remain = BolUtils.calculate_order_remain(orders) for order in orders: remain_obj = order_remain[str(order.pk)] remain = remain_obj['remain'] total = total + remain order.receipt = receipt order.do_not_check_exported = True order.charge_remain = True order.save() TransactionUtils.charge_order_remain(remain, customer, staff, receipt, order) return total
def seeding(index: int, single: bool = False, save: bool = True, **kwargs) -> models.QuerySet: from apps.order.utils import OrderUtils order = OrderUtils.seeding(1, True) if index == 0: raise Exception('Indext must be start with 1.') def get_data(i: int) -> dict: data = { 'order': order.id, 'title': "title{}".format(i), 'url': "url{}".format(i), 'quantity': i, 'unit_price': 10.5 + i } _order = kwargs.get('order', None) if _order is not None: data['order'] = _order.pk if save is False: return data instance = OrderItemBaseSr(data=data) instance.is_valid(raise_exception=True) instance = instance.save() return instance def get_list_data(index): return [get_data(i) for i in range(1, index + 1)] return get_data(index) if single is True else get_list_data(index)
def setUp(self): order = OrderUtils.seeding(1, True) self.items = BolUtils.seeding(3) self.ids = [self.items[0].pk, self.items[1].pk] for item in self.items: item.order = order item.save()
def setUp(self): order = OrderUtils.seeding(1, True) self.items = BolUtils.seeding(2) self.ids = [self.items[0].pk, self.items[1].pk] self.items[0].order = order self.items[0].save()
def test_normal_case(self): order = OrderUtils.seeding(1, True) OrderItemUtils.seeding(2, order=order) staff = StaffUtils.seeding(1, True) TransactionUtils.recharge(100000000, MoneyType.CASH, order.customer, staff) self.assertEqual(Transaction.objects.count(), 1) TransactionUtils.deposit(order, staff) transaction = Transaction.objects.first() self.assertEqual(Transaction.objects.count(), 2) self.assertEqual(transaction.order, order) self.assertEqual(transaction.staff, staff) self.assertEqual(transaction.customer, order.customer) self.assertEqual(transaction.amount, OrderUtils.get_deposit_amount(order)) self.assertEqual(transaction.type, Type.DEPOSIT)
def test_normal_case(self): order = OrderUtils.seeding(1, True) staff = StaffUtils.seeding(1, True) TransactionUtils.deposit(order, staff) self.assertEqual(Transaction.objects.count(), 1) TransactionUtils.undeposit(order) self.assertEqual(Transaction.objects.count(), 0)
def test_normal_order_with_register(self): item = BolUtils.seeding(1, True) order = OrderUtils.seeding(1, True) item.order = order item.insurance = True item.cny_insurance_value = 50 item.save() self.assertEqual(BolUtils.cal_insurance_fee(item), 0)
def test_match(self): purchase_code = '123456' order = OrderUtils.seeding(1, True) order.purchase_code = purchase_code order.save() item = Bol.objects.create(uid='test', purchase_code=purchase_code) self.assertEqual(item.order, order)
def deposit(order: models.QuerySet, staff: models.QuerySet): from .models import Type, MoneyType from .models import Transaction from apps.order.utils import OrderUtils TransactionUtils.undeposit(order) amount = OrderUtils.get_deposit_amount(order) can_deposit = OrderUtils.can_deposit(order) if not can_deposit: raise ValidationError("Đơn hàng {} không đủ tiền đặt cọc.".format( order.uid)) transaction = Transaction(order=order, customer=order.customer, staff=staff, amount=amount, type=Type.DEPOSIT, money_type=MoneyType.INDIRECT, note="Đặt cọc đơn {}".format(order.uid)) order.deposit = amount order.save() transaction.save()
def test_normal_case(self): # Recharge recharge_amount = 500000 money_type = MoneyType.CASH TransactionUtils.recharge(recharge_amount, money_type, self.customer, self.staff) # Approve OrderUtils.approve(self.order, self.staff, self.staff) # Set status self.order.status = Status.VN_STORE self.order.cn_date = Tools.now() self.order.vn_date = Tools.now() self.order.save() # Export receipt = ReceiptUtils.seeding(1, True) BolUtils.export_order_bols(self.order.order_bols.all(), receipt, self.customer, self.staff) deposit = Transaction.objects.get(order=self.order, type=Type.DEPOSIT) pay = Transaction.objects.get(order=self.order, type=Type.PAY) vnd_total = OrderUtils.get_vnd_total_obj(self.order) self.assertEqual(deposit.amount + pay.amount, vnd_total)
def test_normal_case(self): order = OrderUtils.seeding(1, True) data = { 'order': order.id, 'title': "title1", 'url': "url1", 'shop_link': "shop_link1", 'shop_nick': "shop_nick1", 'site': "site1", 'quantity': 3, 'unit_price': 50.5 } order_item = OrderItemBaseSr(data=data) order_item.is_valid(raise_exception=True) order_item.save() self.assertEqual(order_item.data['price'], 151.5)
def check(self, request): from apps.order.models import Order from apps.order.utils import OrderUtils uid = request.data.get('uid', None) checked_items = request.data.get('checked_items', {}) potential_bols = request.data.get('potential_bols', '') order = get_object_or_404(Order, uid=uid) remain = OrderUtils.check(order, checked_items) if len(remain.keys()): order.pending = True order.checked_date = timezone.now() order.checker = request.user.staff order.potential_bols = potential_bols order.save() return res(remain)
def complaint_resolve(self, request, pk=None): order = get_object_or_404(Order, pk=pk) decide = int(request.data.get('decide', ComplaintDecide.AGREE)) if decide == ComplaintDecide.AGREE: OrderUtils.complaint_agree(order) elif decide == ComplaintDecide.MONEY_BACK: OrderUtils.complaint_money_back(order) elif decide == ComplaintDecide.CHANGE: OrderUtils.complaint_change(order) else: raise ValidationError( "Lựa chọn giải quyết khiếu nại không hợp lệ.") return res({})