Beispiel #1
0
def search_orders(request):
    user = request.user
    logging.info(user, log_messages['search_order_try'])
    try:
        if not request.user.groups.filter(
                name__in=[admins[5], admins[8]]).exists():
            logging.access_deny(user, log_messages['search_order_access_deny'])
            return Response(status=status.HTTP_401_UNAUTHORIZED)
        sent_filter = request.data['filter']

        orders = Order.objects.filter()
        if sent_filter['order_id'] != '':
            orders = orders.filter(order_id__contains=sent_filter['order_id'])
        if sent_filter['customer_id'] != '':
            orders = orders.filter(
                customer__email__contains=sent_filter['customer_id'])
        if sent_filter['customer_name'] != '':
            orders = orders.filter(
                customer__company_name__contains=sent_filter['customer_name'])
        if sent_filter['city'] != '':
            orders = orders.filter(
                customer__city__contains=sent_filter['city'])
        if len(sent_filter['state_checks']) > 0:
            orders = orders.filter(status=sent_filter['state_checks'][0])
        # for state in sent_filter['state_checks']:
        #     if state == admins[1]:
        #         orders = orders.filter(orderAdmin_confirmed=True)
        #     elif state == admins[2]:
        #         orders = orders.filter(sellAdmin_confirmed=True)
        #     elif state == admins[3]:
        #         orders = orders.filter(warehouseAdmin_confirmed=True)
        #     elif state == admins[4]:
        #         orders = orders.filter(financeAdmin_confirmed=True)
        #     elif state == admins[8]:
        #         orders = orders.filter(administration_process=True)
        orders = OrderSerializer(
            orders.all()[sent_filter['page'] * 30:sent_filter['page'] * 30 +
                         30],
            many=True)
        orders = json.loads(JSONRenderer().render(orders.data))
        for order in orders:
            order['created_date'] = jalali.Gregorian(
                order['created_date'].split('T')[0]).persian_string()
            if order['last_change_date']:
                order['last_change_date'] = jalali.Gregorian(
                    order['last_change_date'].split('T')[0]).persian_string()
        logging.success(user, log_messages['search_order_success'])
        return Response({'orders': orders})
    except Exception as e:
        logging.error(user, log_messages['search_order_failure'])
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Beispiel #2
0
def write_formatted_message(admin, message, msg_type):
    now = timezone.now()
    persian_date = jalali.Gregorian(now.strftime('%Y-%m-%d')).persian_string()
    line = '{date} {time} - {type}: {msg} __ {user}\n'.format(date=persian_date, time=now.strftime('%H:%M:%S'),
                                                              type=msg_type,
                                                              msg=message, user=admin)
    path = os.path.join(ADMIN_LOG_DIR, admin.username)
    write_to_file(path, line)
Beispiel #3
0
def get_new_order_id():
    last_order_made = Order.objects.filter(is_checked_out=True).last()
    last_id = '000001'
    if last_order_made and last_order_made.order_id:
        last_id = str(int(last_order_made.order_id.split('W')[1]) + 1)
        if len(last_id) < 6:
            last_id = '0' * (6 - len(last_id)) + last_id
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    currentDate = jalali.Gregorian(date).persian_string().split('-')[0]
    id = str(currentDate) + 'W' + last_id
    return id
Beispiel #4
0
def get_orders(request):
    # try:
    user = request.user
    if user.groups.filter(name__in=[
            admins[1],
    ]).exists():
        orders = Order.objects.filter(status=Order.CHECKED_OUT) \
            .all().order_by('-created_date')
    elif user.groups.filter(name__in=[
            admins[2],
    ]).exists():
        orders = Order.objects.filter(status=Order.ORDER_ADMIN) \
            .all().order_by('-created_date')
    elif user.groups.filter(name__in=[
            admins[4],
    ]).exists():
        orders = Order.objects.filter(status=Order.SELL_ADMIN) \
            .all().order_by('-created_date')
    elif user.groups.filter(name__in=[
            admins[3],
    ]).exists():
        orders = Order.objects.filter(status=Order.FINANCE_ADMIN) \
            .all().order_by('-created_date')
    elif user.groups.filter(name__in=[
            admins[8],
    ]).exists():
        orders = Order.objects.filter(status=Order.ADMINISTRATION) \
            .all().order_by('-created_date')
    else:
        logging.access_deny(user, log_messages['fetch_order_data_access_deny'])
        return Response(status=status.HTTP_401_UNAUTHORIZED)
    logging.success(user, log_messages['fetch_order_data'])
    orders = OrderSerializer(orders, many=True)
    orders = json.loads(JSONRenderer().render(orders.data))
    for order in orders:
        order['created_date'] = jalali.Gregorian(
            order['created_date'].split('T')[0]).persian_string()
        if order['last_change_date']:
            order['last_change_date'] = jalali.Gregorian(
                order['last_change_date'].split('T')[0]).persian_string()
    return Response({'orders': orders})
Beispiel #5
0
def get_orders_report(request):
    try:
        user = request.user
        customer = CustomerProfile.objects.filter(user=user).all().first()
        orders = Order.objects.filter(customer=customer) \
            .all().order_by('-created_date')
        orders = OrderSerializer(orders, many=True).data
        orders = json.loads(JSONRenderer().render(orders))
        for order in orders:
            order['created_date'] = jalali.Gregorian(
                order['created_date'].split('T')[0]).persian_string()
        reports = Report.objects.filter(owner=customer).all().order_by('-date')
        reports = ReportSerializer(reports, many=True).data
        reports = json.loads(JSONRenderer().render(reports))
        for report in reports:
            report['date'] = jalali.Gregorian(
                report['date'].split('T')[0]).persian_string()
        return Response({'orders': orders, 'reports': reports})
    except:
        return Response({'msg': 'مشکلی در سرور به وجود آمده'},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Beispiel #6
0
def get_reports(request):
    try:
        user = request.user
        reports = Report.objects.filter(owner=user).all().order_by('-date')
        reports = ReportSerializer(reports, many=True).data
        reports = json.loads(JSONRenderer().render(reports))
        for report in reports:
            report['date'] = jalali.Gregorian(
                report['date'].split('T')[0]).persian_string()
        return Response({'reports': reports})
    except:
        return Response({'msg': 'مشکلی در سرور به وجود آمده'},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Beispiel #7
0
def get_persian_date(date):
    date = jalali.Gregorian(date).persian_string().split('-')
    date = date[2] + '-' + date[1] + '-' + date[0]
    return date
Beispiel #8
0
def get_order_excel(request):
    user = request.user
    logging.info(user, log_messages['get_excel_try'])
    try:
        response = HttpResponse(content_type='application/ms-excel')
        response['Content-Disposition'] = 'attachment; filename="orders.xls"'

        wb = xlwt.Workbook(encoding='utf-8')
        ws = wb.add_sheet('گزارشات')

        ws.cols_right_to_left = 1
        font_style = xlwt.XFStyle()
        font_style.font.bold = True

        columns = [
            'نام خریدار',
            'کد سفارش',
            'استان',
            'شهر',
            'آدرس خریدار',
            'قیمت',
            'تاریخ سفارش',
            'تاریخ تغییر',
            'تاریخ تایید',
            'تاریخ ارسال',
            'تاریخ تحویل',
            'وضعیت',
        ]

        row_name = 0

        for col in range(len(columns)):
            ws.write(row_name, col, columns[col], font_style)

        font_style = xlwt.XFStyle()

        orders = Order.objects.filter(
            is_checked_out=True).all().order_by('-created_date')
        for order in orders:
            row_name += 1
            ws.write(row_name, 0, order.customer.company_name, font_style)
            ws.write(row_name, 1, order.order_id, font_style)
            ws.write(row_name, 2, '', font_style)  # TODO add state
            ws.write(row_name, 3, order.customer.city, font_style)
            ws.write(row_name, 4, order.customer.address, font_style)
            ws.write(row_name, 5, order.cost, font_style)
            ws.write(
                row_name, 6,
                jalali.Gregorian(
                    order.created_date.strftime('%Y-%m-%d')).persian_string(),
                font_style)
            if order.last_change_date:
                ws.write(
                    row_name, 7,
                    jalali.Gregorian(
                        order.last_change_date.strftime(
                            '%Y-%m-%d')).persian_string(), font_style)
            if order.confirmed_date:
                ws.write(
                    row_name, 8,
                    jalali.Gregorian(order.confirmed_date.strftime(
                        '%Y-%m-%d')).persian_string(), font_style)
            if order.sent_date:
                ws.write(
                    row_name, 9,
                    jalali.Gregorian(
                        order.sent_date.strftime('%Y-%m-%d')).persian_string(),
                    font_style)
            if order.received_date:
                ws.write(
                    row_name, 10,
                    jalali.Gregorian(order.received_date.strftime(
                        '%Y-%m-%d')).persian_string(), font_style)
            ws.write(row_name, 11, order.status, font_style)

        wb.save(response)
        logging.success(user, log_messages['get_excel_success'])
        return response
    except:
        logging.error(user, log_messages['get_excel_failure'])
        return Response({'msg': 'مشکلی در سرور به وجود آمده'},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)