Ejemplo n.º 1
0
def buy(request):
    """
    a function to commit the cart that will be reflected on the sales.
    :param request:
    :return:
    """

    #verify that the customer logged in session is active
    customer = None
    if request.session.has_key("cust_id"):
        try:
            customer = Customer.objects.get(id=request.session.get('cust_id'))
        except:
            pass
    if customer is None:
        return HttpResponseRedirect('/customer/login')

    #Get all the cart items that is to be bought and identify the farmer associated with the tarkari item. Save the transaction to the sales
    c = Cart.objects.filter(customer=customer)
    for cart in c:
        tarkari = Tarkari.objects.get(id=cart.tarkari.id)
        godam = Godam.objects.get(tarkari=tarkari)
        farmer = Farmer.objects.get(id=godam.farmer_id)
    #     sales = Sales.objects.get(customer=customer, tarkari=tarkari, farmer=farmer)
    # if sales is not None:
    #     sales.number += number
    # else:
        sales = Sales(tarkari=tarkari, customer=customer, farmer=farmer, number=cart.tarkari_count)
        sales.save()
        cart.delete()

    #Redirect to view all other lists of tarkaris
    return HttpResponseRedirect('/listTarkari')
Ejemplo n.º 2
0
def new(request):
    if request.method == 'GET':
        return render_to_response(
            'sale/new.html',
            RequestContext(request, {
                'sale_active': 1,
                'customs': get_customs(),
                'stocks': get_stocks()
            }))
    else:
        data = request.POST.get('data')
        print data
        if not data:
            return failed_response('参数错误')
        else:
            all = json.loads(data)
            s = Sales()
            s.custom = Custom.objects.get(id=all['custom'])
            s.stock = Stock.objects.get(id=all['stock'])
            s.save()
            for item in all['detail']:
                pro = SalesDetail()
                pro.product = Product.objects.get(id=item['pid'])
                pro.num = int(item['pnum'])
                pro.price = float(item['pprice'])
                pro.scale = float(item['pscale'])
                pro.save()
                s.detail.add(pro)
            s.save()
            return success_response('添加成功')
Ejemplo n.º 3
0
 def test_sales_model(self):
     sales = Sales(code="51081789",
                   date="2020-12-15",
                   salesman_id=1,
                   value=8900.0)
     sales.save()
     self.assertEqual(sales.code, "51081789")
     self.assertEqual(sales.date, datetime.date(2020, 12, 15))
     self.assertEqual(sales.salesman.id, 1)
     self.assertEqual(sales.value, 8900.0)
Ejemplo n.º 4
0
def load_sales(csv_file, sheet):
    book = xlrd.open_workbook(csv_file) # Open an .xls file
    sheet = book.sheet_by_index(sheet) # Get the first sheet
    for counter in range(21): # Loop for five times
        if counter:
            # grab the current row
            rows = sheet.row_values(counter)
            sales = Sales(sr_no=rows[0], transaction_id=rows[1], item_id=rows[2],
                          transaction_obj=Transaction.objects.get(transaction_id=rows[1]),
                          item_obj=Item.objects.get(item_id=rows[2]))
            sales.save()


    return True
Ejemplo n.º 5
0
def add_sales(request):
    if request.method == 'POST':
        customer_s = Sales()
        emailID = request.POST.get('ID')
        date = request.POST.get('Date')
        billNo = request.POST.get('Bill No.')
        customer = request.POST.get('Customer_name')
        phone_number = request.POST.get('Contact')
        address = request.POST.get('Address')
        product = request.POST.get('Product_name')
        quantity = request.POST.get('Quantity')
        rate = request.POST.get('Rate')
        avail = request.POST.get('Avail_Qty')
        total = request.POST.get('Total')
        payment = request.POST.get('Payment')
        description = request.POST.get('Description')
        balance = request.POST.get('balance')
        duedate = request.POST.get('Due_date')
        mode = request.POST.get('Mode')
        customer_s.customer = customer
        customer_s.emailID = emailID
        customer_s.billNo = billNo
        customer_s.date = date
        customer_s.phone_number = phone_number
        customer_s.address = address
        customer_s.product = product
        customer_s.quantity = quantity
        customer_s.rate = rate
        customer_s.avail = avail
        customer_s.total = total
        customer_s.payment = payment
        customer_s.description = description
        customer_s.balance = balance
        customer_s.duedate = duedate
        customer_s.mode = mode
        customer_s.save()
    return render(request, 'add_sales.html', {})
Ejemplo n.º 6
0
    def execute(self):
        import os
        import sys
        import csv
        import re
        import datetime

        from sales.models import App, Date, Sales, Country
        from django.core.mail import mail_admins

        print self.help

        for i in ACCOUNT_INFO:
            print "Processing directory : %s" % i['DATA_DIR']
            
            if (i['DATA_DIR'] != '' and not os.path.exists(i['DATA_DIR'])):
                os.makedirs(i['DATA_DIR'])

            filenames = [os.path.join(i['DATA_DIR'], f)
                         for f in os.listdir(i['DATA_DIR']) if f.startswith(DAILY_SALES_PREFIX)]

            for f in filenames:
                match = re.findall('S_D_(.*)\.txt', f)
                try:
                    date = datetime.datetime.strptime(match[0], '%m-%d-%Y').date()
                except ValueError:
                    # for old format data
                    try:
                        date = datetime.datetime.strptime(match[0], '%m%d%Y').date()
                    except ValueError:
                        continue

                d_id, created = Date.objects.get_or_create(account=i['APPSTORE_ID'], date=date)
                if created:
                    d_id.populated = False
                else:
                    if d_id.populated == True:
                        continue

                print "[%s] is now processing..." % d_id

                reader = csv.DictReader(open(f), delimiter='\t')

                data_set = []
                for row in reader:
                    data = {}

                    if 'Title' in row:
                        appname = row['Title'].strip()
                    elif 'Title / Episode / Season' in row:
                        appname = row['Title / Episode / Season'].strip()
                    else:
                        raise ValueError

                    data['appname'] = appname
                    data['date'] = datetime.datetime.strptime(row['Begin Date'].strip(), '%m/%d/%Y').date()
                    data['units'] = int(row['Units'])

                    if 'Developer Proceeds' in row:
                        proceeds = float(row['Developer Proceeds'])
                    elif 'Royalty Price' in row:
                        proceeds = float(row['Royalty Price'])
                    else:
                        raise ValueError


                    data['proceeds'] = float(proceeds)

                    country_code = row['Country Code'].strip()
                    try:
                        country = Country.objects.get(code=country_code)
                    except:
                        country = Country.objects.get(code='ZZ') # unknown

                    data['country'] = country

                    if 'Currency of Proceeds' in row:
                        currency = row['Currency of Proceeds'].strip()
                    elif 'Royalty Currency' in row:
                        currency = row['Royalty Currency'].strip()
                    else:
                        raise ValueError

                    data['currency'] = currency
                    data['price'] = float(row['Customer Price'].strip())
                    data['ptype'] = row['Product Type Identifier'].strip()

                    if 'SKU' in row:
                        sku = row['SKU'].strip()
                    elif 'Vendor Identifier' in row:
                        sku = row['Vendor Identifier'].strip()
                    else:
                        raise ValueError
                    data['sku'] = sku

                    if 'Apple Identifier' in row:
                        appleid = row['Apple Identifier']
                    else:
                        raise ValueError

                    data['appleid'] = appleid

                    data_set.append(data)

                for data in data_set:

                    app_id, created = App.objects.get_or_create(sku=data['sku'])
                    if created:
                        app_id.name = data['appname']
                        app_id.appleid = data['appleid']
                        app_id.save()
                    else:
                        if app_id.appleid is None:
                            app_id.appleid = data['appleid']
                            app_id.save()

                    sales = Sales()
                    sales.app = app_id
                    sales.date = data['date']
                    sales.units = data['units']

                    sales.proceeds = data['proceeds']
                    sales.country = data['country']
                    sales.currency = data['currency']

                    sales.price = data['price']
                    sales.ptype = data['ptype']
                    sales.category = sales.calc_category(data['ptype'], data['price'])

                    sales.save()

                d_id.populated = True
                d_id.save()

            pass