def add(request): data = {} order = None num = [0, 1, 2, 3, 4] data['num'] = num steels = Steel.objects.all() data['steels'] = steels clients = Client.objects.all() data['clients'] = clients order_day = date.today() data['order_day'] = order_day oid = datetime.now().strftime('%Y%m%d%H%M%S') data['oid'] = str(oid) + '_3' today = date.today() if request.method == 'POST': post = request.POST.copy() is_youpiao = post.get('is_youpiao', 0) # check if not youpiao=1, wupiao=0 if is_youpiao: is_youpiao = int(is_youpiao) client_id = post.get('client_id', None) i = 0 oid = post.get('oid', None) if oid: pass else: return False order_item_list = [] order_total_price = 0.0 if not client_id: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) create_time = datetime.now() modify_time = create_time total_profit = 0.0 # order total profit while i < 5: temp_order_item = OrderItem() sid = 'sid_' + str(i) unit_price = 'unit_price_' + str(i) number = 'number_' + str(i) total_price = 'total_price_' + str(i) temp_sid = post.get(sid, None) if not temp_sid: break temp_number = post.get(number, None) if temp_number: temp_number = int(temp_number) else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) temp_unit_price = post.get(unit_price, None) #print temp_unit_price if temp_unit_price: temp_unit_price = float(temp_unit_price) else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) temp_total_price = post.get(total_price, None) if temp_total_price: temp_total_price = float(temp_total_price) else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) temp_steel = None if temp_sid: #print temp_sid temp_steels = steels.filter(id=int(temp_sid)) if temp_steels.exists(): temp_steel = temp_steels[0] storage_number = int(temp_steel.number) + int(temp_steel.retail_number) if storage_number < int(temp_number): form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) else: break temp_profit = 0.0 # each order item profit # 获取来源订单号 temp_from_oid = '' need_number = int(temp_number) # 需要的数量 if temp_steel.retail_number > 0: inexhausted_retails = Retail.objects.filter(sid=temp_sid).filter(status=2).order_by('id') if inexhausted_retails.exists(): total_used_retail_num = 0 # 之前用了多少数量 inexhausted_retail = inexhausted_retails[0] same_retail_orders = OrderItem.objects.filter(from_oid__contains=inexhausted_retail.oid) excess_retail_num = int(inexhausted_retail.number) if same_retail_orders.exists(): for item in same_retail_orders: temp_from_oid_str = item.from_oid temp_from_oid_list = temp_from_oid_str.split(',') for term in temp_from_oid_list: if inexhausted_retail.oid in term: total_used_retail_num += int(term.split('|')[1]) excess_retail_num = excess_retail_num - total_used_retail_num if excess_retail_num < need_number or excess_retail_num == need_number: need_number = need_number - excess_retail_num temp_from_oid += inexhausted_retail.oid + '|' + str(excess_retail_num) + ',' temp_profit = temp_profit + float(excess_retail_num) * float(inexhausted_retail.unit_price) inexhausted_retail.status = 3 inexhausted_retail.save() else: temp_from_oid += inexhausted_retail.oid + '|' + str(need_number) + ',' temp_profit = temp_profit + float(need_number) * float(inexhausted_retail.unit_price) need_number = 0 inexhausted_retail.status = 2 inexhausted_retail.save() if need_number > 0: unexhausted_retails = Retail.objects.filter(sid=temp_sid).filter(status=1).order_by('id') if unexhausted_retails.exists(): for item in unexhausted_retails: if need_number == 0: break if int(item.number) < need_number or int(item.number) == need_number: need_number = need_number - int(item.number) temp_from_oid += item.oid + '|' + str(item.number) + ',' temp_profit = temp_profit + float(item.number) * float(item.unit_price) item.status = 3 item.save() else: temp_from_oid += item.oid + '|' + str(need_number) + ',' temp_profit = temp_profit + float(need_number) * float(item.unit_price) need_number = 0 item.status = 2 item.save() if need_number > 0: inexhausted_batches = Batch.objects.filter(sid=temp_sid).filter(status=2).order_by('id') if inexhausted_batches.exists(): total_used_batch_num = 0 # 之前用了多少数量 inexhausted_batch = inexhausted_batches[0] same_batch_orders = OrderItem.objects.filter(from_oid__contains=inexhausted_batch.oid) excess_batch_num = int(inexhausted_batch.number) if same_batch_orders.exists(): for item in same_batch_orders: temp_from_oid_str = item.from_oid temp_from_oid_list = temp_from_oid_str.split(',') for term in temp_from_oid_list: if inexhausted_batch.oid in term: total_used_batch_num += int(term.split('|')[1]) excess_batch_num = excess_batch_num - total_used_batch_num #print excess_batch_num if excess_batch_num < need_number or excess_batch_num == need_number: need_number = need_number - excess_batch_num temp_from_oid += inexhausted_batch.oid + '|' + str(excess_batch_num) + ',' if is_youpiao == 1: temp_profit = temp_profit + float(excess_batch_num) * float(inexhausted_batch.cost_a) elif is_youpiao == 0: temp_profit = temp_profit + float(excess_batch_num) * float(inexhausted_batch.cost_b) inexhausted_batch.status = 3 inexhausted_batch.save() else: temp_from_oid += inexhausted_batch.oid + '|' + str(need_number) + ',' if is_youpiao == 1: temp_profit = temp_profit + float(need_number) * float(inexhausted_batch.cost_a) elif is_youpiao == 0: temp_profit = temp_profit + float(need_number) * float(inexhausted_batch.cost_b) need_number = 0 inexhausted_batch.status = 2 inexhausted_batch.save() if need_number > 0: unexhausted_batches = Batch.objects.filter(sid=temp_sid).filter(status=1).order_by('id') if unexhausted_batches.exists(): for item in unexhausted_batches: if need_number == 0: break if int(item.number) < need_number or int(item.number) == need_number: need_number = need_number - int(item.number) temp_from_oid += item.oid + '|' + str(item.number) + ',' if is_youpiao == 1: temp_profit = temp_profit + float(item.number) * float(item.cost_a) elif is_youpiao == 0: temp_profit = temp_profit + float(item.number) * float(item.cost_b) item.status = 3 item.save() else: temp_from_oid += item.oid + '|' + str(need_number) + ',' if is_youpiao == 1: temp_profit = temp_profit + float(need_number) * float(item.cost_a) elif is_youpiao == 0: temp_profit = temp_profit + float(need_number) * float(item.cost_b) need_number = 0 item.status = 2 item.save() #print need_number if need_number > 0: return False temp_order_item.oid = oid temp_order_item.from_oid = temp_from_oid temp_order_item.status = 1 temp_order_item.sid = temp_sid temp_order_item.number = temp_number round(temp_profit, 2) temp_order_item.profit = temp_profit round(temp_unit_price, 2) temp_order_item.unit_price = temp_unit_price check_total_price = float(temp_number) * float(temp_unit_price) if check_total_price == float(temp_total_price): temp_order_item.total_price = temp_total_price else: round(check_total_price, 2) temp_order_item.total_price = check_total_price temp_order_item.create_time = create_time temp_order_item.modify_time = modify_time temp_order_item.order_day = today order_item_list.append(temp_order_item) i = i + 1 order_total_price = order_total_price + float(temp_total_price) total_profit = total_profit + float(temp_profit) check = None check = OrderItem.objects.bulk_create(order_item_list) if check: pass else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) if order_total_price > 0.0: order = Order() order.client_id = client_id order.oid = oid order.is_youpiao = int(is_youpiao) order.order_day = date.today() order.total_price = order_total_price round(total_profit, 2) order.profit = total_profit order.create_time = timezone.now() order.modify_time = timezone.now() order.save() current_order_items = OrderItem.objects.filter(oid=order.oid) if current_order_items.exists(): for item in current_order_items: temp_steel = get_object_or_404(Steel, id=item.sid) total_number = int(temp_steel.retail_number) + int(temp_steel.number) retail_number = int(temp_steel.retail_number) if total_number > item.number or total_number == item.number: if temp_steel.retail_number > 0: if int(temp_steel.retail_number) > int(item.number) or int(temp_steel.retail_number) == int(item.number): temp_steel.retail_number = int(temp_steel.retail_number) - int(item.number) else: temp_steel.number = int(temp_steel.number) - (int(item.number) - int(temp_steel.retail_number)) temp_steel.retail_number = 0 else: temp_steel.number = int(temp_steel.number) - int(item.number) temp_steel.save() else: return False return HttpResponseRedirect('/order/?id=%d' % int(order.id)) else: form = OrderForm(post, instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) ) else: form = OrderForm(instance=order) data['form'] = form return render_to_response( 'order/add.html', data, context_instance=RequestContext(request) )