|
# -*- coding: utf-8 -*-
from __future__ import division
import os
import string
import sys
from django.conf import settings
from django.db import transaction
from django_logit import logit
from django_response import response
from paginator import pagination
from TimeConvert import TimeConvert as tc
from account.models import UserInfo
from coupon.models import UserCouponInfo
from integral.models import SaleclerkIntegralIncomeExpensesInfo, SaleclerkSubmitLogInfo
from logs.models import MchInfoEncryptLogInfo
from mch.models import BrandInfo, ConsumeInfoSubmitLogInfo, DistributorInfo, ModelInfo, SaleclerkInfo, MaintenancemanInfo
from statistic.models import (DistributorSaleStatisticInfo, ModelSaleStatisticInfo, ProvinceSaleStatisticInfo,
SaleclerkSaleStatisticInfo, SaleStatisticInfo)
from member.models import RightInfo
from utils.error.errno_utils import (CouponStatusCode, ProductBrandStatusCode, ProductDistributorStatusCode,
ProductMachineStatusCode, ProductModelStatusCode, SaleclerkStatusCode)
@logit
@transaction.atomic
def clerk_sale_submit_api(request):
user_id = request.POST.get('user_id', '')
iv = request.POST.get('iv', '')
encryptedData = request.POST.get('encryptedData', '')
lat = request.POST.get('lat', .0)
lon = request.POST.get('lon', .0)
brandID = request.POST.get('BrandID', settings.KODO_DEFAULT_BRAND_PK)
modelID = request.POST.get('ModelID', '')
distributorID = request.POST.get('DistributorID', '')
serialNo = request.POST.get('SerialNo', '')
verifyResult = request.POST.get('verifyResult', '')
remark = request.POST.get('remark', '')
is_wxwork = request.POST.get('is_wxwork', False)
consumer_name = request.POST.get('consumer_name', '')
consumer_phone = request.POST.get('consumer_phone', '')
file_path = request.POST.get('file_path', '')
code_image_path = request.POST.get('code_image_path', '')
trackingNo = request.POST.get('trackingNo', '')
code_version = request.POST.get('code_version', 0)
test_sn = serialNo in settings.TESTING_SNS
if lat == 'undefined':
lat = .0
if lon == 'undefined':
lon = .0
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
model = ModelInfo.objects.get(pk=modelID)
except ModelInfo.DoesNotExist:
return response(ProductModelStatusCode.MODEL_NOT_FOUND)
except ValueError:
return response(ProductModelStatusCode.MODEL_NOT_FOUND)
try:
clerk = SaleclerkInfo.objects.select_for_update().get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
if not clerk.is_auth:
return response(SaleclerkStatusCode.CLERK_NOT_AUTH)
try:
distributor = DistributorInfo.objects.get(distributor_id=clerk.distributor_id)
except DistributorInfo.DoesNotExist:
return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)
except ValueError:
return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)
ymd = tc.local_string(format='%Y%m%d')
# 是否被消费者扫过
has_scan = ConsumeInfoSubmitLogInfo.objects.filter(model_id=model.model_id, serialNo=serialNo, status=True).exists()
# 是否是新二维码,即统览码2
if not code_version:
code_version = 1
code = filter(lambda ch: ch in '0123456789', serialNo)
encrypt_logs = MchInfoEncryptLogInfo.objects.filter(model_pk=model.pk, sn=code)
if encrypt_logs:
code_version = encrypt_logs[0].version
# 店员提交记录
ssli = SaleclerkSubmitLogInfo.objects.create(
brand_pk=brand.pk,
brand_name=brand.brand_name,
model_pk=modelID,
model_name=model.model_name,
model_uni_name=model.model_uni_name,
distributor_pk=distributorID,
distributor_name=distributor.distributor_name,
clerk_id=clerk.clerk_id,
clerk_name=clerk.clerk_name,
code=serialNo,
consumer_name=consumer_name,
consumer_phone=consumer_phone,
remark=remark,
lat=lat,
lon=lon,
image=file_path,
code_image=code_image_path,
test_user=clerk.test_user,
test_sn=test_sn,
ym=ymd[:6],
ymd=ymd,
has_scan=has_scan,
trackingNo=trackingNo,
is_wxwork=is_wxwork,
code_version=code_version,
)
if test_sn:
return response(200, data={
'integral': 0,
'total_integral': clerk.integral,
})
try:
sci = SaleclerkIntegralIncomeExpensesInfo.objects.get(
brand_id=brand.brand_id,
model_id=model.model_id,
code=serialNo,
status=True
)
except SaleclerkIntegralIncomeExpensesInfo.DoesNotExist:
sci = None
if sci:
ssli.dupload = True
ssli.save()
try:
clerk = SaleclerkInfo.objects.get(clerk_id=sci.clerk_id, status=True)
except SaleclerkInfo.DoesNotExist:
clerk = None
return response(SaleclerkStatusCode.DUPLICATE_SUBMIT, data={
'franchiser_name': clerk.distributor_name,
'clerk_name': clerk.clerk_name,
} if clerk else {})
# 店员积分
integral = model.integral
clerk.num += 1
clerk.integral += integral
clerk.total_integral += integral
clerk.save()
# 店员积分记录
if integral > 0:
SaleclerkIntegralIncomeExpensesInfo.objects.create(
clerk_id=clerk.clerk_id,
type=SaleclerkIntegralIncomeExpensesInfo.INCOME,
brand_id=brand.brand_id,
brand_name=brand.brand_name,
model_id=model.model_id,
model_name=model.model_name,
distributor_id=distributor.distributor_id,
distributor_name=distributor.distributor_name,
code=serialNo,
consumer_name=consumer_name,
consumer_phone=consumer_phone,
lat=lat,
lon=lon,
image=file_path,
integral=integral,
left_integral=clerk.total_integral,
test_user=clerk.test_user,
)
# TODO: Make statistic async
if not clerk.test_user and not sci:
# 日销量统计
ssi, _ = SaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
ymd=ymd,
)
ssi.num += 1
ssi.save()
# 月销量统计
ssi, _ = SaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
ymd=ymd[:6],
)
ssi.num += 1
ssi.save()
# 年销量统计
ssi, _ = SaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
ymd=ymd[:4],
)
ssi.num += 1
ssi.save()
# 型号销量统计
mssi, _ = ModelSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
model_name=model.model_uni_name,
ymd=ymd,
)
mssi.saleclerks += [clerk.clerk_id]
mssi.num = len(mssi.saleclerks)
mssi.save()
mssi, _ = ModelSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
model_name=model.model_uni_name,
ymd=ymd[:6],
)
mssi.saleclerks += [clerk.clerk_id]
mssi.num = len(mssi.saleclerks)
mssi.save()
mssi, _ = ModelSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
model_name=model.model_uni_name,
ymd=ymd[:4],
)
mssi.saleclerks += [clerk.clerk_id]
mssi.num = len(mssi.saleclerks)
mssi.save()
# 经销商销量统计
dssi, _ = DistributorSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
distributor_id=distributor.distributor_id,
ymd=ymd,
)
dssi.distributor_name = distributor.distributor_name
dssi.num += 1
dssi.save()
dssi2, _ = DistributorSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
distributor_id=distributor.distributor_id,
ymd=0,
)
dssi2.distributor_name = distributor.distributor_name
dssi2.num += 1
dssi2.save()
# 日省份销量统计
pssi, _ = ProvinceSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
province_code=distributor.distributor_province_code,
ymd=ymd,
)
pssi.province_name = distributor.distributor_province_name
pssi.num += 1
pssi.save()
# 月省份销量统计
pssi, _ = ProvinceSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
province_code=distributor.distributor_province_code,
ymd=ymd[:6],
)
pssi.province_name = distributor.distributor_province_name
pssi.num += 1
pssi.save()
# 年省份销量统计
pssi, _ = ProvinceSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
province_code=distributor.distributor_province_code,
ymd=ymd[:4],
)
pssi.province_name = distributor.distributor_province_name
pssi.num += 1
pssi.save()
# 日销售员销量统计
sssi, _ = SaleclerkSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
clerk_id=clerk.clerk_id,
ymd=ymd,
)
sssi.distributor_id = distributor.distributor_id
sssi.distributor_name = distributor.distributor_name
sssi.distributor_short_name = distributor.distributor_short_name
sssi.clerk_name = clerk.clerk_name
sssi.num += 1
sssi.save()
# 月销售员销量统计
sssi, _ = SaleclerkSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
clerk_id=clerk.clerk_id,
ymd=ymd[:6],
)
sssi.distributor_id = distributor.distributor_id
sssi.distributor_name = distributor.distributor_name
sssi.distributor_short_name = distributor.distributor_short_name
sssi.clerk_name = clerk.clerk_name
sssi.num += 1
sssi.save()
# 年销售员销量统计
sssi, _ = SaleclerkSaleStatisticInfo.objects.select_for_update().get_or_create(
brand_id=brand.brand_id,
clerk_id=clerk.clerk_id,
ymd=ymd[:4],
)
sssi.distributor_id = distributor.distributor_id
sssi.distributor_name = distributor.distributor_name
sssi.distributor_short_name = distributor.distributor_short_name
sssi.clerk_name = clerk.clerk_name
sssi.num += 1
sssi.save()
return response(200, data={
'integral': integral,
'total_integral': clerk.integral,
})
@logit
def clerk_integral_list_api(request):
brandID = request.POST.get('BrandID', settings.KODO_DEFAULT_BRAND_PK)
user_id = request.POST.get('user_id', '')
ftime = request.POST.get('ftime', '')
ttime = request.POST.get('ttime', '')
page = int(request.POST.get('page', 1))
num = int(request.POST.get('num', settings.GROUP_NUM_PER_PAGE))
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
clerk = SaleclerkInfo.objects.get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
integrals = SaleclerkIntegralIncomeExpensesInfo.objects.filter(clerk_id=clerk.clerk_id).order_by('-pk')
if ftime:
integrals = integrals.filter(created_at__gte=tc.string_to_utc_datetime(ftime))
if ttime:
integrals = integrals.filter(created_at__lte=tc.string_to_utc_datetime(ttime))
integrals, left = pagination(integrals, page, num)
integrals = [integral.data for integral in integrals]
return response(200, data={
'integrals': integrals,
'total_integral': clerk.integral,
'left_integral': clerk.integral,
})
@logit
def clerk_model_list_api(request):
brandID = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_PK)
user_id = request.POST.get('user_id', '')
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
clerk = SaleclerkInfo.objects.get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
models = ModelInfo.objects.filter(status=True).order_by('position')
models = [model.data for model in models]
return response(200, data={
'models': models,
})
@logit
def clerk_query_coupon(request):
brandID = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_PK)
user_id = request.POST.get('user_id', '')
user_coupon_id = request.POST.get('user_coupon_id', '')
is_maintenance = int(request.POST.get('is_maintenance', 0))
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
user_coupon = UserCouponInfo.objects.get(user_coupon_id=user_coupon_id)
except UserCouponInfo.DoesNotExist:
return response(CouponStatusCode.COUPON_NOT_FOUND)
right_detail = ''
if is_maintenance:
try:
maintenance = MaintenancemanInfo.objects.get(brand_id=brand.brand_id, user_id=user.user_id, status=True)
# 维修人工费
right = RightInfo.objects.get(right_id='X457xEV8KVxHQiTvhA7Dtf')
member = UserInfo.objects.get(user_id=user_coupon.user_id)
right_detail = right.maintaindata(level=member.level)
except:
return response(400001, 'MaintenancemanInfo Not Found', u'维修员不存在')
else:
try:
clerk = SaleclerkInfo.objects.get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
return response(200, data={
'user_coupon': user_coupon.data,
'right': right_detail
})
@logit
def clerk_writeoff_coupon(request):
brandID = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_PK)
user_id = request.POST.get('user_id', '')
user_coupon_id = request.POST.get('user_coupon_id', '')
is_maintenance = int(request.POST.get('is_maintenance', 0))
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
user_coupon = UserCouponInfo.objects.get(user_coupon_id=user_coupon_id)
if user_coupon.has_used:
return response(CouponStatusCode.COUPON_HAS_USED)
elif user_coupon.is_coupon_admin_writeoff and not is_maintenance:
return response(CouponStatusCode.COUPON_PERMISSION_DENIED)
elif user_coupon.has_expired:
return response(CouponStatusCode.COUPON_EXPIRED)
except UserCouponInfo.DoesNotExist:
return response(CouponStatusCode.COUPON_NOT_FOUND)
user_coupon.has_used = True
user_coupon.used_at = tc.utc_datetime()
if is_maintenance:
try:
maintenance = MaintenancemanInfo.objects.get(brand_id=brand.brand_id, user_id=user.user_id, status=True)
user_coupon.admin_id = maintenance.maintenance_id
user_coupon.admin_name = maintenance.maintenance_name
user_coupon.save()
except:
return response(400001, 'MaintenancemanInfo Not Found', u'维修员不存在')
else:
try:
clerk = SaleclerkInfo.objects.get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
user_coupon.clerk_id = clerk.clerk_id
user_coupon.clerk_name = clerk.clerk_name
user_coupon.distributor_id = clerk.distributor_id
user_coupon.distributor_name = clerk.distributor_name
user_coupon.save()
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
return response(200, 'Write Off Coupon Success', u'劵核销成功')
@logit
def clerk_checkout_serialNo_api(request):
brandID = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_PK)
user_id = request.POST.get('user_id', '')
serialNo = request.POST.get('serial', '')
model_pk = request.POST.get('model_pk', '')
try:
user = UserInfo.objects.get(user_id=user_id, status=True)
except UserInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
try:
brand = BrandInfo.objects.get(pk=brandID)
except BrandInfo.DoesNotExist:
brand = None
except ValueError:
brand = None
if not brand:
try:
brand = BrandInfo.objects.get(brand_id=brandID)
except BrandInfo.DoesNotExist:
return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
try:
clerk = SaleclerkInfo.objects.get(brand_id=brand.brand_id, clerk_phone=user.phone, status=True)
except SaleclerkInfo.DoesNotExist:
return response(SaleclerkStatusCode.CLERK_NOT_FOUND)
log = MchInfoEncryptLogInfo.objects.filter(model_pk=model_pk, sn=serialNo).order_by('-created_at')
if not log:
return response(ProductMachineStatusCode.SN_NOT_FOUND)
return response(200, 'SerialNo Checkout Success', u'序列号校验成功', data={
'code_version': log[0].version
})
|