def clean(self): data = super(CheckoutForm, self).clean(skip_card_verification=True) if not self._errors: request = self.request user = request.user if not user.is_authenticated(): p = Profile.create(request, None, entry_point=ProfileEntryPoint.Buy) else: p = user.get_profile() self.profile = p order_data = { 'card_display_number': self.cached_card['display_number'], 'card_data': self.cached_card['data'], 'card_type': self.cached_card['type'], 'billing_state': self.cached_address['state'], 'billing_county': self.cached_address.get('county'),} self.order = BuyOrder.create(request, order_data, p) wizard = self.request.checkout_wizard f = wizard.get_form(0, self.request.POST) f.is_valid() # it's a long way to get info from here email = user.email if user.is_authenticated() else f.cleaned_data['email'] shipping_address = f.cached_address shipping_address.update(f.cached_name) shipping_address['country'] = 'USA' billing_address = self.cached_address billing_address.update(self.cached_name) billing_address['country'] = 'USA' self.billing_history = BillingHistory.create(user if user.is_authenticated() else None, order_data['card_display_number'], debit=self.order.get_order_total(), description = 'Shop Order #%s' % self.order.get_order_number(), reason='buy', type=TransactionType.BuyCheckout) if self.order.user: invoice_num = 'BUY_%s_%s' % (self.order.user.id, self.billing_history.id) else: invoice_num = 'BUY_%s' % self.billing_history.id card = self.cached_card['data'] aim_data = { 'number': card['number'], 'exp': '/'.join((card['exp_month'], card['exp_year'][-2:])), 'code': card['code'], 'billing': billing_address, 'shipping': shipping_address, 'invoice_num': invoice_num, 'description': self.order.get_aim_description(), 'x_customer_ip': self.request.META.get('REMOTE_ADDR'), 'x_email': email, 'x_po_num': self.order.order_no(), } if p.user: aim_data['x_cust_id'] = p.user.id self.billing_history.tax = self.order.get_tax_amount() self.billing_history.applied_credits = self.order.applied_credits aim_data['x_tax'] = self.billing_history.tax self.billing_history.aim_transaction_id = self.order.take_charge(aim_data) if self.billing_history.aim_transaction_id or not self.order.get_charge_amount(): self.billing_history.card_data = self.cached_card['data'] self.billing_history.save() self.order.payment_transaction = self.billing_history self.order.save() request.cart.empty() else: msg = self.order.message self.order.delete() if p.user: self.billing_history.status = TransactionStatus.Declined self.billing_history.save() else: self.billing_history.delete() raise forms.ValidationError(msg) return data
def import_buy_orders(self, user_id): ocursor = self.mconn.cursor() icursor = self.mconn.cursor() ocursor.execute("select * from shop_orders where ref_user=%s order by id", [user_id,]) order_field_names = map(lambda x: x[0], ocursor.description) for ro in ocursor.fetchall(): o = dict(zip(order_field_names, ro)) data = {} update_dict(data, self.fix_name({ 'first_name': o['first_name'], 'last_name': o['last_name'], })) update_dict(data, self.fix_address({ 'address1': o['address'], 'address2': o['address2'], 'city': o['city'], 'state': self.STATES[o['ref_state']], #'country': 'USA', 'zip_code': o['postal_code'], }), 'shipping') update_dict(data, self.fix_name({ 'first_name': o['billing_first_name'], 'last_name': o['billing_last_name'], }), 'billing') update_dict(data, self.fix_address({ 'address1': o['billing_address'], 'address2': o['billing_address2'], 'city': o['billing_city'], 'state': self.STATES[o['billing_ref_state']], #'country': 'USA', 'zip_code': o['billing_postal_code'], }), 'billing') status = self.BUY_STATUS_MAP[o['ref_order_status']] if status == 6: #shipped if o['ref_shipping_status'] == 4: # cancelled status = 4 # Canceled bo = BuyOrder( id = o['id'], user_id = user_id, status = status, create_date = o['order_date'], tax = decimal.Decimal('%.02f' % o['tax_fee']), total = decimal.Decimal('%.02f' % o['total']), **data ) bo.save() icursor = self.mconn.cursor() icursor.execute("select i.price, i.count, r.upc, r.id from shop_order_items i left join items r on i.ref_item=r.id where i.ref_order=%s order by i.id", o['id']) for i_price, i_count, i_upc, r_id in icursor.fetchall(): item = self.get_item(i_upc) if not item: self.missing_items.add(r_id) continue status = BuyOrderItemStatus.Shipped if bo.status == BuyOrderStatus.Shipped else BuyOrderItemStatus.Canceled for _qty in xrange(i_count): boi = BuyOrderItem( order = bo, status = status, item = item, price = decimal.Decimal(str(i_price)) ) boi.save() if bo.status == BuyOrderStatus.Shipped: icursor = self.mconn.cursor() image_filename, tracking_code = None, None icursor.execute("SELECT image_filename, tracking_code FROM endicia_labels e where ref_shop_order = %s", o['id']) for image_filename, tracking_code in icursor.fetchall(): break if image_filename: image_filename = 'media/labels/old/endicia/' + image_filename pack_slip = PackSlip( order = bo, created = bo.create_date, mail_label = image_filename, tracking_number = tracking_code, date_shipped = bo.create_date, ) pack_slip.save() for order_item in bo.items.all(): PackSlipItem( slip = pack_slip, order_item = order_item, added = bo.create_date, ).save()
def view(request, user_id): user = get_object_or_404(User, id=user_id) profile = Profile.objects.get(user=user) if 'impersonate' in request.GET: user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return redirect('/') not_activated_user = not user.is_active and profile.activation_code account_is_blocked = not user.is_active and not profile.activation_code can_block_account = request.user.is_superuser can_change_extra_slots = request.user.is_superuser #or request.user.get_profile().group in [Group.DC_Manager] if request.is_ajax(): status = 'FAIL' action = request.REQUEST.get('action') if action == 'resend-activation-link': if not_activated_user: profile.send_email_confirmation_mail() status = 'OK' elif action == 'block': if can_block_account: profile.block_account() status = 'OK' elif action == 'unblock': if can_block_account: profile.unblock_account() status = 'OK' elif action == 'add-extra-slot': if can_change_extra_slots: profile.extra_rent_slots += 1 profile.save() status = 'OK' elif action == 'rm-extra-slot': if can_change_extra_slots: profile.extra_rent_slots -= 1 if profile.extra_rent_slots < 0: profile.extra_rent_slots = 0 profile.save() status = 'OK' elif action == 'reactivate': if request.user.is_superuser: plan = MemberRentalPlan.get_current_plan(user) if plan and plan.status in [RentalPlanStatus.Delinquent, RentalPlanStatus.Collection]: plan.take_delinquent_payment(True) status = 'OK' return JsonResponse({'status': status}) last_orders_buy = BuyOrder.list_recent(user) last_order_buy = last_orders_buy[0] if last_orders_buy.count() > 0 else None last_order_trade = user.tradeorder_set.order_by('-id') last_order_trade = last_order_trade[0] if len(last_order_trade) > 0 else None last_order_rent = user.rentorder_set.order_by('-id') last_order_rent = last_order_rent[0] if len(last_order_rent) > 0 else None rent_plan = MemberRentalPlan.get_current_plan(user) rent_plan_cancelable = rent_plan and rent_plan.status in [RentalPlanStatus.CanceledP, RentalPlanStatus.Pending, RentalPlanStatus.Delinquent, RentalPlanStatus.Active, RentalPlanStatus.OnHold] stat = { 'buy': { 'last_date': last_order_buy.create_date if last_order_buy else '', 'status': 'Active' if last_order_buy else 'Inactive', 'earned_total': user.buyorder_set.aggregate(Sum('total'))['total__sum'] or 0, 'earned_avg': user.buyorder_set.aggregate(Avg('total'))['total__avg'] or 0, }, 'trade': { 'last_date': last_order_trade.create_date if last_order_trade else '', 'status': 'Active' if last_order_trade else 'Inactive', 'earned_total': user.tradeorder_set.aggregate(Sum('total'))['total__sum'] or 0, 'earned_avg': user.tradeorder_set.aggregate(Avg('total'))['total__avg'] or 0, }, 'rent': { 'last_date': last_order_rent.date_rent if last_order_rent else '', 'status': profile.get_rental_status(False), 'cancelable': rent_plan_cancelable, 'earned_total': user.billinghistory_set.filter(status=0, type=1).aggregate(Sum('debit'))['debit__sum'] or 0, 'earned_avg': user.billinghistory_set.filter(status=0, type=1).aggregate(Avg('debit'))['debit__avg'] or 0, 'rent_plan': rent_plan, 'hold_until': rent_plan.hold_reactivate_timestamp if (rent_plan and rent_plan.status == RentalPlanStatus.OnHold) else None, }, } stat['earned_total'] = (stat['buy']['earned_total'] + stat['trade']['earned_total'] + stat['rent']['earned_total']) / 3 stat['earned_avg'] = (stat['buy']['earned_avg'] + stat['trade']['earned_avg'] + stat['rent']['earned_avg']) / 3 last_order = {} last_order['buy'] = last_orders_buy last_order['trade'] = last_order_trade last_order['rent'] = last_order_rent buy_cart, created = BuyCart.objects.get_or_create(user=user) #@UnusedVariable trade_list = user.tradelist.all() return { 'current_view': 'customer_view', 'user': user, 'billing_address': user.get_profile().get_billing_address_data(), 'stat': stat, 'last_order': last_order, 'cart': buy_cart, 'cart_total': sum([item.get_retail_price() * item.quantity for item in buy_cart.items.all()]), 'trade_list': trade_list, 'trade_total': sum([item.get_price() for item in trade_list]), 'not_activated_user': not_activated_user, 'account_is_blocked': account_is_blocked, 'can_block_account': can_block_account, 'can_change_extra_slots': can_change_extra_slots, 'claims': Claim.objects.filter(user=user), }