Пример #1
0
def send_gift_for_users(users, add_days, add_package, add_days_for_limited=False):
    # assert isinstance(users, User.objects)
    for u in users:
        # assert isinstance(u, User)
        if not u.is_active:
            continue
        if not u.fk_user_current_service_user.exists():
            continue
        user_service = u.fk_user_current_service_user.get()
        if not u.fk_ibs_user_info_user.exists():
            continue
        ibi = u.fk_ibs_user_info_user.get().ibs_uid
        if not user_service.is_active:
            continue
        ibs = IBSManager()
        gh = GiftHistory()
        gh.target_user = u
        gh.extended_days = int(add_days)
        gh.extended_package = int(add_package)
        gh.save()
        if add_days > 0:
            if user_service.service_property.initial_package > 0 and add_days_for_limited:
                ibs.set_expire_date_by_uid(ibi, add_days)
            if user_service.service_property.initial_package == 0:
                ibs.set_expire_date_by_uid(ibi, add_days)
        if add_package > 0 and user_service.service_property.initial_package > 0:
            ibs.change_credit_by_uid(add_package, ibi)
            add_package = convert_credit(add_package)
            trm = convert_credit(ibs.get_user_credit_by_user_id(ibi))
        else:
            trm = None
        kw = {'name': '', 'ta': add_package,
              'exp': convert_date(parse_date_from_str_to_julian(str(ibs.get_expire_date_by_uid(ibi)))),
              'ads': add_days,
              'trm': trm}
        send_from_template(u.pk, 17, **kw)
Пример #2
0
def mellat_post_back(request):
    if request.method == 'POST':
        ref_id = request.POST.get('RefId', False)
        res_code = request.POST.get('ResCode', False)
        order_id = request.POST.get('SaleOrderId', False)
        sf_id = request.POST.get('SaleReferenceId', False)
        message = _('the payment has been canceled')
        banking_done = False
        if not (ref_id or res_code or order_id or sf_id):
            message = _('invalid parameters')
            fire_event(4645, None, get_client_ip(request))
            banking_done = False
        try:
            if not validate_integer(order_id):
                banking_done = False
            if not InvoicePaymentTracking.objects.filter(pk=order_id).exists():
                message = _('banking parameters are not correct')
                error_page = render(request, 'finance/payment/PostBack.html', {'msg': message})
                return error_page
            ep = InvoicePaymentTracking.objects.get(pk=order_id)
            i = Invoice.objects.get(pk=ep.invoice.pk)
            if i.is_paid:
                return redirect('/')
            ep.end_time = datetime.today()
            ep.final_res = sf_id
            ep.save()
            if not isinstance(res_code, bool) and res_code == '0':
                message = _('your account has been charged')
                banking_done = True
            if banking_done:
                data = BankProperties.objects.filter(bank__internal_value=identifier)
                terminal_id = long(data.get(name=properties[2]).value)
                bank_username = data.get(name=properties[0]).value
                bank_password = data.get(name=properties[1]).value
                bml = BMLPaymentAPI(bank_username, bank_password, terminal_id)
                error_counter = 0
                while error_counter < 10:
                    verify_res = bml.verify_payment(long(order_id), long(sf_id))
                    if verify_res[0] and (verify_res[1] == '0' or verify_res[1] == 0):
                        bml.settle_payment(long(order_id), sf_id)
                        ep.is_success = True
                        break
                    elif verify_res[0] and (verify_res[1] != '0' and verify_res[1] != 0):
                        banking_done = False
                        break
                    else:
                        error_counter += 1
                if error_counter > 9:
                    ep.is_success = False
                    message = _('unable to complete payment')
                    banking_done = False
                ep.save()
            if banking_done:
                pi = PayInvoice(invoice=i.pk, ref_code=str(sf_id), use_discount=False, is_online=True,
                                price=i.price, comment=None, request=request)
                if pi.pay().get_invoice():
                    pi.commit()
                fire_event(4632, i)
            else:
                fire_event(5707, i)
            if banking_done:
                if i.service.service_type == 1:
                    if get_is_new_service(i.pk):
                        extra = get_next_charge_transfer(i.user_id)
                    else:
                        extra = 0
                    InvoicePaidNotification().send(user_id=i.user_id, service_type=i.service.service_type,
                                                   service=i.service_text, extra_data=i.extra_data,
                                                   expire_date=convert_date(
                                                       i.user.fk_user_current_service_user.get().expire_date),
                                                   bank_ref=i.ref_number,
                                                   transfer=extra
                                                   )
                    # send_from_template.delay(i.user_id, 13,
                    #                          cus=i.service_text,
                    #                          adt=extra,
                    #                          fid=i.pk,
                    #                          exp=convert_date(i.user.fk_user_current_service_user.get().expire_date,
                    #                                           True),
                    #                          brn=str(sf_id))
                elif i.service.service_type == 2:
                    InvoicePaidNotification().send(user_id=i.user_id, service_type=i.service.service_type,
                                                   service=i.service_text + ' - ' + convert_credit(
                                                       i.service.content_object.amount),
                                                   extra_data=i.extra_data,
                                                   expire_date=convert_date(
                                                       i.user.fk_user_current_service_user.get().expire_date),
                                                   bank_ref=i.ref_number)
                    # send_from_template.delay(i.user_id, 14,
                    #                          ta=convert_credit(i.service.content_object.amount),
                    #                          fid=i.pk,
                    #                          brn=str(sf_id))
            page_res = render(request, 'finance/payment/PostBack.html', {'msg': message, 'i': i,
                                                                         'done': banking_done})
            return page_res
        except Exception as e:
            print ("Error in post back : %s \r\n with this params : \r\n%s"
                   % (" ".join((str(a) for a in e.args)), " , ".join(request.POST)))
            return render(request, 'errors/ServerError.html')
    else:
        return redirect(reverse(show_all_invoices))
Пример #3
0
def convert_credit_sign(credit):
    try:
        return convert_credit(credit, False, True)
    except Exception as e:
        print e.message
        return convert_credit(0)
Пример #4
0
def convert_text_credit(credit, byte_data=False):
    try:
        return convert_credit(float(credit), byte_data)
    except Exception as e:
        print e.message
        return convert_credit(0)
Пример #5
0
def personnel_payment(request):
    if request.method == 'GET':
        fid = request.GET.get('f')
        f = validate_invoice(fid, request.RSL_ID)
        if not f:
            return send_error(request, _('invalid invoice'))
        comment = request.GET.get('c')
        bank = request.GET.get('b')
        price = request.GET.get('p')
        if not validate_empty_str(bank):
            return send_error(request, _('please enter bank reference number'))
        if request.RSL_ID:
            if not change_reseller_deposit(request.RSL_ID,
                                           (f.price * -1), True):
                return send_error(
                    request, _('system can not update your credit : 30500'))
        comment = '%s | %s' % (request.user.username, comment)
        try:
            # res = update_user_services(fid)
            # if res[0]:
            if get_is_new_service(fid):
                extra = get_next_charge_transfer(f.user_id)
            else:
                extra = 0
            if not validate_integer(price):
                price = f.price - f.debit_price
            pi = PayInvoice(ref_code=bank,
                            invoice=fid,
                            price=int(price),
                            use_discount=request.GET.get('dd') == '1',
                            comment=comment,
                            is_online=False,
                            is_personnel=True,
                            request=request)
            p_res = pi.pay().get_invoice()
            if p_res is not None:
                pi.commit()
            else:
                return send_error(request, _('unable to calculate price info'))
            if f.service.service_type == 1:
                InvoicePaidNotification().send(
                    user_id=f.user_id,
                    service_type=f.service.service_type,
                    service=f.service_text,
                    extra_data=f.extra_data,
                    expire_date=convert_date(
                        f.user.fk_user_current_service_user.get().expire_date),
                    bank_ref=f.ref_number)
                # send_from_template.delay(f.user_id, 13,
                #                          cus=f.service_text,
                #                          adt=extra,
                #                          fid=f.pk,
                #                          exp=convert_date(f.user.fk_user_current_service_user.get().expire_date,
                #                                           True,
                #                                           False),
                #                          brn=f.ref_number)
                kill_user(f.user_id, None)
            elif f.service.service_type == 2:
                InvoicePaidNotification().send(
                    user_id=f.user_id,
                    service_type=f.service.service_type,
                    service=f.service_text + ' - ' +
                    convert_credit(f.service.content_object.amount),
                    extra_data=f.extra_data,
                    expire_date=convert_date(
                        f.user.fk_user_current_service_user.get().expire_date),
                    bank_ref=f.ref_number)
                # send_from_template.delay(f.user_id, 14,
                #                          ta=convert_credit(f.service.content_object.amount),
                #                          fid=f.pk,
                #                          brn=f.ref_number)
            if check_ajax(request):
                return HttpResponse('200')
            return reverse(show_all_invoices)
        except Exception as e:
            print e.message
            return redirect(reverse(show_all_invoices))
    else:
        return render(request, 'errors/AccessDenied.html')
Пример #6
0
 def __calculate_ibs_service__(self):
     if not self.service.__class__.__name__ == IBSService.__name__:
         return
     srv = IBSService.objects.get(pk=self.service.pk)
     if 'service_property' not in self.kwargs:
         self.__set_error('service property is empty', 204041)
         return
     if 'extra_month' not in self.kwargs:
         self.__set_error('extra month is not set', 204043)
         return
     if 'uid' not in self.kwargs:
         self.__set_error('uid is empty', 204044)
         return
     user_id = int(self.kwargs.get('uid'))
     extra_month = int(self.kwargs.get('extra_month'))
     service_property = int(self.kwargs.get('service_property'))
     if not srv.is_visible:
         self.__set_error('service is not visible', 204011)
         return
     tax = float(read_config(name='invoice_tax', default=0.09))
     if service_property > -1:
         if not ServiceProperty.objects.filter(
                 pk=service_property,
                 is_deleted=False,
                 fk_ibs_service_properties_properties__service__pk=self.
                 service.pk).exists():
             self.__set_error('no such service matched', 204042)
             return
         service_property_value = ServiceProperty.objects.get(
             pk=service_property)
     else:
         service_property_value = ServiceProperty.objects.get(
             pk=srv.fk_default_service_property_service.get().pk)
     if IBSServiceDiscount.objects.filter(service=service_property,
                                          charge_days=extra_month).exists():
         discount = IBSServiceDiscount.objects.get(service=service_property,
                                                   charge_days=extra_month)
         discount_price = discount.price_discount
         extended_days = discount.extended_days
         extended_package = discount.extra_traffic
     else:
         discount = None
         discount_price = 0
         extended_days = 0
         extended_package = 0
     base_service_price = service_property_value.base_price
     sp = round(base_service_price * int(extra_month), -1)
     service_discount = round((sp * discount_price) / 100, -1)
     service_price = round(sp - service_discount, -1)
     service_tax = round(service_price * tax, -1)
     final_price = round(service_price + service_tax, -1)
     if UserCurrentService.objects.filter(user=user_id).exists():
         if UserCurrentService.objects.get(
                 user=user_id).service_id == srv.pk:
             transfer_amount = get_next_charge_transfer(user_id)
         else:
             transfer_amount = 0
     else:
         transfer_amount = 0
     service_traffic = service_property_value.initial_package
     if discount:
         self.discount = discount
     self.base_price = base_service_price
     self.__add_extra_data__('service_price', sp)
     self.__add_extra_data__('discount_price', service_discount)
     self.__add_extra_data__('tax', service_tax)
     self.__add_extra_data__('transfer', transfer_amount)
     self.__add_extra_data__('transfer_converted',
                             convert_credit(transfer_amount))
     self.__add_extra_data__('service_package', service_traffic)
     self.__add_extra_data__('service_package_converted',
                             convert_credit(service_traffic))
     self.__add_extra_data__('extended_days', extended_days)
     self.__add_extra_data__('extended_package', extended_package)
     self.__add_extra_data__('extended_package_converted',
                             convert_credit(extended_package))
     self.__add_extra_data__('uid', user_id)
     self.__add_extra_data__('service_property', service_property_value)
     self.final_price = final_price
     self.is_success = True
     self.error_cod = 0
     self.error = None
     self.service_type = 1
     self.invoice = None
Пример #7
0
    def __calculate_for_package__(self):
        if not self.service.__class__.__name__ == Traffic.__name__:
            return
        service = self.service
        if 'uid' not in self.kwargs:
            self.__set_error('user id is empty', 194041)
            return
        user_id = int(self.kwargs.get('uid'))
        tax = float(read_config(name='invoice_tax', default=0.09))
        if service.fk_vip_packages_package.exists():
            is_vip_package = service.fk_vip_packages_package.get()
        else:
            is_vip_package = None
        if is_vip_package and not VIPUsersGroup.objects.filter(
                user=user_id).exists():
            self.__set_error('vip package for none vip user', 194011)
            return
        user_service_group = UserServiceGroup.objects.get(user=user_id)
        if is_vip_package:
            if user_service_group.service_group_id != is_vip_package.group.group_id:
                self.__set_error(
                    'vip group for user and vip group for service is not the same!',
                    194012)
                return
        # if the selected package is not in the user group services then return false
        if not user_service_group.service_group.fk_package_groups_group.filter(
                package=service.pk).exists():
            self.__set_error(
                'user service group does not contains this package', 194013)
            return
        if is_vip_package or service.price > 2:
            gig_price = service.price
        else:
            # Read the price from service property price
            gig_price = UserCurrentService.objects.get(
                user=user_id).service_property.package_price
        if PackageDiscount.objects.filter(package=service.pk,
                                          is_deleted=False).exists():
            discount = PackageDiscount.objects.get(package=service.pk)
        else:
            discount = None

        if gig_price <= 2:
            gig_price = int(
                read_config(name='invoice_package_price', default=5000))
        if discount:
            extra_package = discount.extended_package
            discount_percent = discount.price_discount
        else:
            extra_package = 0
            discount_percent = 0
        if is_vip_package or service.price > 2:
            price = round(gig_price, -1)
        else:
            price = round(gig_price * (service.amount / 1024), -1)
        discounted_price = round((price * discount_percent) / 100, -1)
        final_price = price - discounted_price
        tax *= final_price
        final_price += tax
        final_price = round(final_price, -1)
        extra_package = convert_credit(extra_package)
        self.discount = discount
        self.extra_data['service_price'] = gig_price  # Extra Fields
        self.extra_data['extra_package'] = extra_package
        self.final_price = final_price
        self.extra_data['tax'] = tax
        self.extra_data['discount_price'] = discounted_price
        self.base_price = price
        self.service_type = 2
        self.is_success = True
        self.error = None
        self.error_cod = 0