Beispiel #1
0
def confirm_inventories(request):
    inventories = Inventory.objects.filter(confirmed=False)
    date = datetime.date.today()
    period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
    try:
        period = Period.objects.get(name=period_name)
    except ObjectDoesNotExist:
        period = Period(name=period_name)
        period.save()
    for inventory in inventories:
        shortage = inventory.supposed_quantity - inventory.fact_quantity
        if shortage:
            product = inventory.product
            sale = Sale(manager=User.objects.get(username='******'),
                        department=Department.objects.get(name='Офис'),
                        price=product.internet_price,
                        purchase_price=product.purchase_price,
                        product=product,
                        quantity=shortage,
                        date=date,
                        period=period)
            sale.save()
        inventory.confirmed = True
        inventory.save()
    return redirect('inventories')
Beispiel #2
0
def add_sales_internet2(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode())
        date = datetime.date.today()
        department = Department.objects.get(name='Интернет')
        manager = request.user
        period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
        try:
            period = Period.objects.get(name=period_name)
        except ObjectDoesNotExist:
            period = Period(name=period_name)
            period.save()
        new_sales = data['sales']
        for key, value in new_sales.items():
            product = Product.objects.get(name=key)
            price = value[
                'internet_price'] - value['discount'] / value['quantity']
            quantity = value['quantity']
            new_sale = Sale(date=date,
                            manager=manager,
                            price=price,
                            product=product,
                            department=department,
                            quantity=quantity,
                            purchase_price=product.purchase_price,
                            period=period)
            new_sale.save()
        return HttpResponse()

    else:
        filter_form = modelform_factory(Product, fields=('category', 'brand'))
        context = {'form': filter_form}
        return render(request, 'add_internet_sales2.html', context)
Beispiel #3
0
def get_period():
    period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
    try:
        period = Period.objects.get(name=period_name)
    except ObjectDoesNotExist:
        period = Period(name=period_name)
        period.save()
    return period
Beispiel #4
0
def expand_context(request):
    period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
    try:
        period = Period.objects.get(name=period_name)
    except ObjectDoesNotExist:
        period = Period(name=period_name)
        period.save()
    if request.user.is_authenticated:
        user = request.user
        if user.username == 'fisher':
            tasks = Task.objects.filter(done=False)
        else:
            tasks = Task.objects.filter(user_to=user, done=False)
        tasks_count = tasks.count()
    else:
        tasks_count = 0
    return {'period_id': period.id, 'tasks_count': tasks_count}
Beispiel #5
0
def add_sales_internet(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode())
        date = datetime.date.today()
        department = Department.objects.get(name='Интернет')
        manager = request.user
        period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
        try:
            period = Period.objects.get(name=period_name)
        except ObjectDoesNotExist:
            period = Period(name=period_name)
            period.save()
        new_sales = data['sales']
        for key, value in new_sales.items():
            product = Product.objects.get(id=key)
            price = value['shop_price'] - value['discount'] / value['quantity']
            quantity = value['quantity']
            new_sale = Sale(date=date,
                            manager=manager,
                            price=price,
                            product=product,
                            department=department,
                            quantity=quantity,
                            purchase_price=product.purchase_price,
                            period=period)
            new_sale.save()
        return HttpResponse()

    else:
        products = {}
        for product in Product.objects.filter(is_active=True).order_by(
                'category', 'brand', 'name'):
            products[str(product.id)] = {
                'name': product.name,
                'shop_price': product.internet_price,
                'brand': product.brand.id if product.brand else None,
                'category': product.category.id if product.category else None,
            }
        js_data = json.dumps(products)
        filter_form = modelform_factory(Product, fields=('category', 'brand'))
        context = {'js_data': js_data, 'form': filter_form}
        return render(request, 'add_sales.html', context)
Beispiel #6
0
def salary(request):
    this_period_name = datetime.datetime.strftime(timezone.now(), '%B %Y')
    try:
        this_period = Period.objects.get(name=this_period_name)
    except ObjectDoesNotExist:
        this_period = Period(name=this_period_name)
        this_period.save()
    last_period_name = datetime.datetime.strftime(
        timezone.now() + relativedelta(months=-1), '%B %Y')
    last_period = Period.objects.get(name=last_period_name)

    # New99 this period
    New99_this_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=this_period)
    New99_this_period_sales_by_dates = defaultdict(int)
    for sale in New99_this_period_sales:
        New99_this_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    New99_this_period = []
    New99_this_period_sum = 0
    for date, daily_sales in New99_this_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.03, 2),
            'rate': 200
        }
        New99_this_period_sum += 200 + daily_sales * 0.03
        New99_this_period.append(day)

    # New99 last period
    New99_last_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=last_period)
    New99_last_period_sales_by_dates = defaultdict(int)
    for sale in New99_last_period_sales:
        New99_last_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    New99_last_period = []
    New99_last_period_sum = 0
    for date, daily_sales in New99_last_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.03, 2),
            'rate': 200
        }
        New99_last_period_sum += 200 + daily_sales * 0.03
        New99_last_period.append(day)

    # Banan this period
    banan_this_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=this_period)
    banan_this_period_sales_by_dates = defaultdict(int)
    for sale in banan_this_period_sales:
        banan_this_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    Banan_this_period = []
    Banan_this_period_sum = 0
    for date, daily_sales in banan_this_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.05, 2),
            'rate': 225
        }
        Banan_this_period_sum += 225 + daily_sales * 0.05
        Banan_this_period.append(day)
    # Banan last period
    banan_last_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=last_period)
    banan_last_period_sales_by_dates = defaultdict(int)
    for sale in banan_last_period_sales:
        banan_last_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    Banan_last_period = []
    Banan_last_period_sum = 0
    for date, daily_sales in banan_last_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.05, 2),
            'rate': 225
        }
        Banan_last_period_sum += 225 + daily_sales * 0.05
        Banan_last_period.append(day)

    # Kolya this period
    kolya_this_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=this_period)
    kolya_this_period_sales_by_dates = defaultdict(int)
    for sale in kolya_this_period_sales:
        kolya_this_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    Kolya_this_period = []
    Kolya_this_period_sum = 0
    for date, daily_sales in kolya_this_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.05, 2),
            'rate': 200
        }
        Kolya_this_period_sum += 200 + daily_sales * 0.05
        Kolya_this_period.append(day)
    # Kolya last period
    kolya_last_period_sales = Sale.objects.filter(manager__username='******',
                                                  period=last_period)
    kolya_last_period_sales_by_dates = defaultdict(int)
    for sale in kolya_last_period_sales:
        kolya_last_period_sales_by_dates[sale.date.strftime(
            "%d.%m.%Y")] += sale.cost()
    Kolya_last_period = []
    Kolya_last_period_sum = 0
    for date, daily_sales in kolya_last_period_sales_by_dates.items():
        day = {
            'date': date,
            'daily_sales': daily_sales,
            'percent': round(daily_sales * 0.05, 2),
            'rate': 200
        }
        Kolya_last_period_sum += 200 + daily_sales * 0.05
        Kolya_last_period.append(day)
    # Bogdan last period
    sales_last_period = Sale.objects.filter(period=last_period)
    margin_last_period = 0
    sales_last_period_sum = 0
    for sale in sales_last_period:
        margin_last_period += sale.price * sale.quantity - sale.purchase_price * sale.quantity
        sales_last_period_sum += sale.cost()
    BOGDAN_RATE = 4000
    Bogdan_percent_last_period = margin_last_period * 0.08
    Bogdan_last_period_sum = BOGDAN_RATE + Bogdan_percent_last_period
    # Bogdan this period
    sales_this_period = Sale.objects.filter(period=this_period)
    margin_this_period = 0
    sales_this_period_sum = 0
    for sale in sales_this_period:
        margin_this_period += sale.price * sale.quantity - sale.purchase_price * sale.quantity
        sales_this_period_sum += sale.cost()
    BOGDAN_RATE = 4000
    Bogdan_percent_this_period = margin_this_period * 0.08
    Bogdan_this_period_sum = BOGDAN_RATE + Bogdan_percent_this_period

    context = {
        'this_period': this_period_name,
        'last_period': last_period_name,
        'Banan_this_period': Banan_this_period,
        'Banan_this_period_sum': round(Banan_this_period_sum, 2),
        'Banan_last_period': Banan_last_period,
        'Banan_last_period_sum': round(Banan_last_period_sum, 2),
        'New99_this_period': New99_this_period,
        'New99_this_period_sum': round(New99_this_period_sum, 2),
        'New99_last_period': New99_last_period,
        'New99_last_period_sum': round(New99_last_period_sum, 2),
        'Kolya_this_period': Kolya_this_period,
        'Kolya_this_period_sum': round(Kolya_this_period_sum, 2),
        'Kolya_last_period': Kolya_last_period,
        'Kolya_last_period_sum': round(Kolya_last_period_sum, 2),
        'sales_last_period_sum': sales_last_period_sum,
        'sales_this_period_sum': sales_this_period_sum,
        'margin_last_period': round(margin_last_period, 2),
        'margin_this_period': round(margin_this_period, 2),
        'BOGDAN_RATE': BOGDAN_RATE,
        'Bogdan_percent_last_period': round(Bogdan_percent_last_period, 2),
        'Bogdan_percent_this_period': round(Bogdan_percent_this_period, 2),
        'Bogdan_last_period_sum': round(Bogdan_last_period_sum, 2),
        'Bogdan_this_period_sum': round(Bogdan_this_period_sum, 2),
    }
    return render(request, 'salary.html', context)