def delivery_status_delete(sender, **extra): """ 状态跟踪 - 删除状态 1、明细删除状态同步更新 :param sender: :param extra: :return: """ # print(sender, extra) delivery_id = extra.get('delivery_id') status_delete = extra.get('status_delete') current_time = extra.get('current_time') if not delivery_id: return delivery_items = get_delivery_items_rows(delivery_id=delivery_id) result = True for delivery_item in delivery_items: delivery_item_id = delivery_item.id delivery_item_data = { 'status_delete': status_delete, 'delete_time': current_time, 'update_time': current_time, } result = result and edit_delivery_items(delivery_item_id, delivery_item_data) return result
def pdf(delivery_id): """ 文件下载 :param delivery_id: :return: """ delivery_info = get_delivery_row_by_id(delivery_id) # 检查资源是否存在 if not delivery_info: abort(404) # 检查资源是否删除 if delivery_info.status_delete == STATUS_DEL_OK: abort(410) delivery_print_date = time_utc_to_local( delivery_info.update_time).strftime('%Y-%m-%d') delivery_code = '%s%s' % ( g.ENQUIRIES_PREFIX, time_utc_to_local( delivery_info.create_time).strftime('%y%m%d%H%M%S')) # 获取客户公司信息 customer_info = get_customer_row_by_id(delivery_info.customer_cid) # 获取客户联系方式 customer_contact_info = get_customer_contact_row_by_id( delivery_info.customer_contact_id) # 获取出货人员信息 user_info = get_user_row_by_id(delivery_info.uid) delivery_items = get_delivery_items_rows(delivery_id=delivery_id) # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('delivery pdf') template_name = 'delivery/pdf.html' html = render_template(template_name, delivery_id=delivery_id, delivery_info=delivery_info, customer_info=customer_info, customer_contact_info=customer_contact_info, user_info=user_info, delivery_items=delivery_items, delivery_print_date=delivery_print_date, delivery_code=delivery_code, **document_info) # return html return render_pdf( html=HTML(string=html), stylesheets=[CSS(string='@page {size:A4; margin:35px;}')], download_filename='销售出货.pdf'.encode('utf-8'))
def preview(delivery_id): """ 打印预览 :param delivery_id: :return: """ delivery_info = get_delivery_row_by_id(delivery_id) # 检查资源是否存在 if not delivery_info: abort(404) # 检查资源是否删除 if delivery_info.status_delete == STATUS_DEL_OK: abort(410) delivery_print_date = time_utc_to_local( delivery_info.update_time).strftime('%Y-%m-%d') delivery_code = '%s%s' % ( g.ENQUIRIES_PREFIX, time_utc_to_local( delivery_info.create_time).strftime('%y%m%d%H%M%S')) # 获取客户公司信息 customer_info = get_customer_row_by_id(delivery_info.customer_cid) # 获取客户联系方式 customer_contact_info = get_customer_contact_row_by_id( delivery_info.customer_contact_id) # 获取出货人员信息 user_info = get_user_row_by_id(delivery_info.uid) delivery_items = get_delivery_items_rows(delivery_id=delivery_id) # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('delivery preview') template_name = 'delivery/preview.html' return render_template(template_name, delivery_id=delivery_id, delivery_info=delivery_info, customer_info=customer_info, customer_contact_info=customer_contact_info, user_info=user_info, delivery_items=delivery_items, delivery_print_date=delivery_print_date, delivery_code=delivery_code, **document_info)
def edit(delivery_id): """ 销售出货编辑 """ delivery_info = get_delivery_row_by_id(delivery_id) # 检查资源是否存在 if not delivery_info: abort(404) # 检查资源是否删除 if delivery_info.status_delete == STATUS_DEL_OK: abort(410) # 检查资源是否核准 if delivery_info.status_audit == STATUS_AUDIT_OK: resource = _('Delivery') abort( exceptions.Locked.code, _('The %(resource)s has been approved, it cannot be modified', resource=resource)) template_name = 'delivery/edit.html' # 加载编辑表单 form = DeliveryEditForm(request.form) form.uid.choices = get_user_choices() form.warehouse_id.choices = get_warehouse_choices(option_type='update') # 内嵌表单货架选项 for item_form in form.delivery_items: item_form.rack_id.choices = get_rack_choices(form.warehouse_id.data, option_type='update') # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('delivery edit') # 进入编辑页面 if request.method == 'GET': # 获取明细 delivery_items = get_delivery_items_rows(delivery_id=delivery_id) # 表单赋值 form.uid.data = delivery_info.uid form.customer_cid.data = delivery_info.customer_cid form.customer_contact_id.data = delivery_info.customer_contact_id form.type_tax.data = delivery_info.type_tax form.warehouse_id.data = delivery_info.warehouse_id form.amount_delivery.data = delivery_info.amount_delivery # form.buyer_delivery_items = buyer_delivery_items while len(form.delivery_items) > 0: form.delivery_items.pop_entry() for delivery_item in delivery_items: delivery_item_form = DeliveryItemsEditForm() delivery_item_form.id = delivery_item.id delivery_item_form.delivery_id = delivery_item.delivery_id delivery_item_form.uid = delivery_item.uid # delivery_item_form.supplier_cid = delivery_item.supplier_cid # delivery_item_form.supplier_company_name = delivery_item.supplier_company_name delivery_item_form.custom_production_brand = delivery_item.custom_production_brand delivery_item_form.custom_production_model = delivery_item.custom_production_model delivery_item_form.production_id = delivery_item.production_id delivery_item_form.production_brand = delivery_item.production_brand delivery_item_form.production_model = delivery_item.production_model delivery_item_form.production_sku = delivery_item.production_sku delivery_item_form.quantity = delivery_item.quantity delivery_item_form.unit_price = delivery_item.unit_price delivery_item_form.rack_id = delivery_item.rack_id delivery_item_form.note = delivery_item.note delivery_item_form.type_tax = delivery_item.type_tax form.delivery_items.append_entry(delivery_item_form) # 内嵌表单货架选项 for item_form in form.delivery_items: item_form.rack_id.choices = get_rack_choices( form.warehouse_id.data, option_type='update') # 渲染页面 return render_template(template_name, delivery_id=delivery_id, form=form, **document_info) # 处理编辑请求 if request.method == 'POST': # 修改仓库 - 不做校验 if form.warehouse_changed.data: form.warehouse_changed.data = '' return render_template(template_name, form=form, **document_info) # 增删数据行不需要校验表单 # 表单新增空行 if form.data_line_add.data is not None: if form.delivery_items.max_entries and len( form.delivery_items.entries ) >= form.delivery_items.max_entries: flash('最多创建%s条记录' % form.delivery_items.max_entries, 'danger') else: form.delivery_items.append_entry() # 内嵌表单货架选项 for item_form in form.delivery_items: item_form.rack_id.choices = get_rack_choices( form.warehouse_id.data, option_type='update') return render_template(template_name, delivery_id=delivery_id, form=form, **document_info) # 表单删除一行 if form.data_line_del.data is not None: if form.delivery_items.min_entries and len( form.delivery_items.entries ) <= form.delivery_items.min_entries: flash('最少保留%s条记录' % form.delivery_items.min_entries, 'danger') else: data_line_index = form.data_line_del.data form.delivery_items.entries.pop(data_line_index) return render_template(template_name, delivery_id=delivery_id, form=form, **document_info) # 表单校验失败 if not form.validate_on_submit(): flash(_('Edit Failure'), 'danger') # flash(form.errors, 'danger') # flash(form.delivery_items.errors, 'danger') return render_template(template_name, delivery_id=delivery_id, form=form, **document_info) # 表单校验成功 # 获取明细 delivery_items = get_delivery_items_rows(delivery_id=delivery_id) delivery_items_ids = [item.id for item in delivery_items] # 数据新增、数据删除、数据修改 delivery_items_ids_new = [] amount_delivery = 0 for delivery_item in form.delivery_items.entries: # 错误 if delivery_item.form.id.data and delivery_item.form.id.data not in delivery_items_ids: continue delivery_item_data = { 'delivery_id': delivery_id, 'uid': form.uid.data, 'customer_cid': form.customer_cid.data, 'customer_company_name': get_customer_row_by_id(form.customer_cid.data).company_name, 'custom_production_brand': delivery_item.form.custom_production_brand.data, 'custom_production_model': delivery_item.form.custom_production_model.data, 'production_id': delivery_item.form.production_id.data, 'production_brand': delivery_item.form.production_brand.data, 'production_model': delivery_item.form.production_model.data, 'production_sku': delivery_item.form.production_sku.data, 'quantity': delivery_item.form.quantity.data, 'unit_price': delivery_item.form.unit_price.data, 'warehouse_id': form.warehouse_id.data, 'rack_id': delivery_item.form.rack_id.data, 'note': delivery_item.form.note.data, 'type_tax': form.type_tax.data, } if not delivery_item.form.id.data: # 新增 add_delivery_items(delivery_item_data) amount_delivery += delivery_item_data[ 'quantity'] * delivery_item_data['unit_price'] else: # 修改 edit_delivery_items(delivery_item.form.id.data, delivery_item_data) amount_delivery += delivery_item_data[ 'quantity'] * delivery_item_data['unit_price'] delivery_items_ids_new.append(delivery_item.form.id.data) # 删除 delivery_items_ids_del = list( set(delivery_items_ids) - set(delivery_items_ids_new)) for delivery_items_id in delivery_items_ids_del: delete_delivery_items(delivery_items_id) # 更新销售出货 current_time = datetime.utcnow() delivery_data = { 'uid': form.uid.data, 'customer_cid': form.customer_cid.data, 'customer_contact_id': form.customer_contact_id.data, 'type_tax': form.type_tax.data, 'amount_production': amount_delivery, 'amount_delivery': amount_delivery, 'warehouse_id': form.warehouse_id.data, 'update_time': current_time, } result = edit_delivery(delivery_id, delivery_data) # 编辑操作成功 if result: flash(_('Edit Success'), 'success') return redirect( request.args.get('next') or url_for('delivery.lists')) # 编辑操作失败 else: flash(_('Edit Failure'), 'danger') return render_template(template_name, delivery_id=delivery_id, form=form, **document_info)