def enquiry_status_delete(sender, **extra): """ 状态跟踪 - 删除状态 1、明细删除状态同步更新 :param sender: :param extra: :return: """ # print(sender, extra) enquiry_id = extra.get('enquiry_id') status_delete = extra.get('status_delete') current_time = extra.get('current_time') if not enquiry_id: return enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) result = True for enquiry_item in enquiry_items: enquiry_item_id = enquiry_item.id enquiry_item_data = { 'status_delete': status_delete, 'delete_time': current_time, 'update_time': current_time, } result = result and edit_enquiry_items(enquiry_item_id, enquiry_item_data) return result
def pdf(enquiry_id): """ 文件下载 :param enquiry_id: :return: """ enquiry_info = get_enquiry_row_by_id(enquiry_id) # 检查资源是否存在 if not enquiry_info: abort(404) # 检查资源是否删除 if enquiry_info.status_delete == STATUS_DEL_OK: abort(410) enquiry_print_date = time_utc_to_local( enquiry_info.update_time).strftime('%Y-%m-%d') enquiry_code = '%s%s' % ( g.ENQUIRIES_PREFIX, time_utc_to_local( enquiry_info.create_time).strftime('%y%m%d%H%M%S')) # 获取客户公司信息 supplier_info = get_supplier_row_by_id(enquiry_info.supplier_cid) # 获取客户联系方式 supplier_contact_info = get_supplier_contact_row_by_id( enquiry_info.supplier_contact_id) # 获取询价人员信息 user_info = get_user_row_by_id(enquiry_info.uid) enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('enquiry edit') template_name = 'enquiry/pdf.html' html = render_template(template_name, enquiry_id=enquiry_id, enquiry_info=enquiry_info, supplier_info=supplier_info, supplier_contact_info=supplier_contact_info, user_info=user_info, enquiry_items=enquiry_items, enquiry_print_date=enquiry_print_date, enquiry_code=enquiry_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 info(enquiry_id): """ 询价详情 :param enquiry_id: :return: """ # 详情数据 enquiry_info = get_enquiry_row_by_id(enquiry_id) # 检查资源是否存在 if not enquiry_info: abort(404) # 检查资源是否删除 if enquiry_info.status_delete == STATUS_DEL_OK: abort(410) enquiry_print_date = time_utc_to_local( enquiry_info.update_time).strftime('%Y-%m-%d') enquiry_code = '%s%s' % ( g.ENQUIRIES_PREFIX, time_utc_to_local( enquiry_info.create_time).strftime('%y%m%d%H%M%S')) # 获取渠道公司信息 supplier_info = get_supplier_row_by_id(enquiry_info.supplier_cid) # 获取渠道联系方式 supplier_contact_info = get_supplier_contact_row_by_id( enquiry_info.supplier_contact_id) # 获取询价人员信息 user_info = get_user_row_by_id(enquiry_info.uid) enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('enquiry info') template_name = 'enquiry/info.html' return render_template(template_name, enquiry_id=enquiry_id, enquiry_info=enquiry_info, supplier_info=supplier_info, supplier_contact_info=supplier_contact_info, user_info=user_info, enquiry_items=enquiry_items, enquiry_print_date=enquiry_print_date, enquiry_code=enquiry_code, **document_info)
def edit(enquiry_id): """ 询价编辑 """ enquiry_info = get_enquiry_row_by_id(enquiry_id) # 检查资源是否存在 if not enquiry_info: abort(404) # 检查资源是否删除 if enquiry_info.status_delete == STATUS_DEL_OK: abort(410) template_name = 'enquiry/edit.html' # 加载编辑表单 form = EnquiryEditForm(request.form) form.uid.choices = get_user_choices() form.status_order.choices = STATUS_ORDER_CHOICES # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('enquiry edit') # 进入编辑页面 if request.method == 'GET': # 获取明细 enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) # 表单赋值 form.uid.data = enquiry_info.uid form.supplier_cid.data = enquiry_info.supplier_cid form.supplier_contact_id.data = enquiry_info.supplier_contact_id form.status_order.data = enquiry_info.status_order form.amount_enquiry.data = enquiry_info.amount_enquiry # form.enquiry_items = enquiry_items while len(form.enquiry_items) > 0: form.enquiry_items.pop_entry() for enquiry_item in enquiry_items: enquiry_item_form = EnquiryItemEditForm() enquiry_item_form.id = enquiry_item.id enquiry_item_form.enquiry_id = enquiry_item.enquiry_id enquiry_item_form.uid = enquiry_item.uid enquiry_item_form.enquiry_production_model = enquiry_item.enquiry_production_model enquiry_item_form.enquiry_quantity = enquiry_item.enquiry_quantity enquiry_item_form.production_id = enquiry_item.production_id enquiry_item_form.production_brand = enquiry_item.production_brand enquiry_item_form.production_model = enquiry_item.production_model enquiry_item_form.production_sku = enquiry_item.production_sku enquiry_item_form.note = enquiry_item.note enquiry_item_form.quantity = enquiry_item.quantity enquiry_item_form.unit_price = enquiry_item.unit_price enquiry_item_form.delivery_time = enquiry_item.delivery_time enquiry_item_form.status_ordered = enquiry_item.status_ordered form.enquiry_items.append_entry(enquiry_item_form) # 渲染页面 return render_template(template_name, enquiry_id=enquiry_id, form=form, **document_info) # 处理编辑请求 if request.method == 'POST': # 增删数据行不需要校验表单 # 表单新增空行 if form.data_line_add.data is not None: if form.enquiry_items.max_entries and len( form.enquiry_items.entries ) >= form.enquiry_items.max_entries: flash('最多创建%s条记录' % form.enquiry_items.max_entries, 'danger') else: form.enquiry_items.append_entry() return render_template(template_name, enquiry_id=enquiry_id, form=form, **document_info) # 表单删除一行 if form.data_line_del.data is not None: if form.enquiry_items.min_entries and len( form.enquiry_items.entries ) <= form.enquiry_items.min_entries: flash('最少保留%s条记录' % form.enquiry_items.min_entries, 'danger') else: data_line_index = form.data_line_del.data form.enquiry_items.entries.pop(data_line_index) return render_template(template_name, enquiry_id=enquiry_id, form=form, **document_info) # 表单校验失败 if not form.validate_on_submit(): flash(_('Edit Failure'), 'danger') # flash(form.errors, 'danger') # flash(form.enquiry_items.errors, 'danger') return render_template(template_name, enquiry_id=enquiry_id, form=form, **document_info) # 表单校验成功 # 获取明细 enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) enquiry_items_ids = [item.id for item in enquiry_items] # 数据新增、数据删除、数据修改 enquiry_items_ids_new = [] amount_enquiry = 0 for enquiry_item in form.enquiry_items.entries: # 错误 if enquiry_item.form.id.data and enquiry_item.form.id.data not in enquiry_items_ids: continue enquiry_item_data = { 'enquiry_id': enquiry_id, 'uid': form.uid.data, 'supplier_cid': form.supplier_cid.data, 'supplier_company_name': get_supplier_row_by_id(form.supplier_cid.data).company_name, 'enquiry_production_model': enquiry_item.form.enquiry_production_model.data, 'enquiry_quantity': enquiry_item.form.enquiry_quantity.data, 'production_id': enquiry_item.form.production_id.data, 'production_brand': enquiry_item.form.production_brand.data, 'production_model': enquiry_item.form.production_model.data, 'production_sku': enquiry_item.form.production_sku.data, 'note': enquiry_item.form.note.data, 'delivery_time': enquiry_item.form.delivery_time.data, 'quantity': enquiry_item.form.quantity.data, 'unit_price': enquiry_item.form.unit_price.data, 'status_ordered': enquiry_item.form.status_ordered.data, } if not enquiry_item.form.id.data: # 新增 add_enquiry_items(enquiry_item_data) amount_enquiry += enquiry_item_data[ 'quantity'] * enquiry_item_data['unit_price'] else: # 修改 edit_enquiry_items(enquiry_item.form.id.data, enquiry_item_data) amount_enquiry += enquiry_item_data[ 'quantity'] * enquiry_item_data['unit_price'] enquiry_items_ids_new.append(enquiry_item.form.id.data) # 删除 enquiry_items_ids_del = list( set(enquiry_items_ids) - set(enquiry_items_ids_new)) for enquiry_items_id in enquiry_items_ids_del: delete_enquiry_items(enquiry_items_id) # 更新询价 current_time = datetime.utcnow() enquiry_data = { 'supplier_cid': form.supplier_cid.data, 'uid': form.uid.data, 'supplier_contact_id': form.supplier_contact_id.data, 'status_order': form.status_order.data, 'amount_production': amount_enquiry, 'amount_enquiry': amount_enquiry, 'update_time': current_time, } result = edit_enquiry(enquiry_id, enquiry_data) # 编辑操作成功 if result: flash(_('Edit Success'), 'success') return redirect( request.args.get('next') or url_for('enquiry.lists')) # 编辑操作失败 else: flash(_('Edit Failure'), 'danger') return render_template(template_name, enquiry_id=enquiry_id, form=form, **document_info)
def add(): """ 创建询价 :return: """ template_name = 'enquiry/add.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('enquiry add') # 加载创建表单 form = EnquiryAddForm(request.form) form.uid.choices = get_user_choices() form.uid.data = current_user.id form.status_order.choices = STATUS_ORDER_CHOICES # 进入创建页面 if request.method == 'GET': # 克隆单据 from_type = request.args.get('from_type') from_id = request.args.get('from_id', type=int) # 克隆单据 - 报价单 if from_type == 'enquiry' and from_id: enquiry_id = from_id enquiry_info = get_enquiry_row_by_id(enquiry_id) # 检查资源是否存在 if not enquiry_info: abort(404) # 检查资源是否删除 if enquiry_info.status_delete == STATUS_DEL_OK: abort(410) # 获取明细 enquiry_items = get_enquiry_items_rows(enquiry_id=enquiry_id) # 表单赋值 form.uid.data = enquiry_info.uid form.supplier_cid.data = enquiry_info.supplier_cid form.supplier_contact_id.data = enquiry_info.supplier_contact_id form.delivery_way.data = enquiry_info.delivery_way form.note.data = enquiry_info.note form.status_order.data = enquiry_info.status_order form.amount_enquiry.data = enquiry_info.amount_enquiry # form.enquiry_items = enquiry_items while len(form.enquiry_items) > 0: form.enquiry_items.pop_entry() for enquiry_item in enquiry_items: enquiry_item_form = EnquiryItemEditForm() enquiry_item_form.id = enquiry_item.id enquiry_item_form.enquiry_id = enquiry_item.enquiry_id enquiry_item_form.uid = enquiry_item.uid enquiry_item_form.enquiry_production_model = enquiry_item.enquiry_production_model enquiry_item_form.enquiry_quantity = enquiry_item.enquiry_quantity enquiry_item_form.production_id = enquiry_item.production_id enquiry_item_form.production_brand = enquiry_item.production_brand enquiry_item_form.production_model = enquiry_item.production_model enquiry_item_form.production_sku = enquiry_item.production_sku enquiry_item_form.note = enquiry_item.note enquiry_item_form.quantity = enquiry_item.quantity enquiry_item_form.unit_price = enquiry_item.unit_price enquiry_item_form.delivery_time = enquiry_item.delivery_time enquiry_item_form.status_ordered = enquiry_item.status_ordered form.enquiry_items.append_entry(enquiry_item_form) # 渲染页面 return render_template(template_name, form=form, **document_info) # 处理创建请求 if request.method == 'POST': # 表单新增空行 if form.data_line_add.data is not None: if form.enquiry_items.max_entries and len( form.enquiry_items.entries ) >= form.enquiry_items.max_entries: flash('最多创建%s条记录' % form.enquiry_items.max_entries, 'danger') else: form.enquiry_items.append_entry() return render_template(template_name, form=form, **document_info) # 表单删除一行 if form.data_line_del.data is not None: if form.enquiry_items.min_entries and len( form.enquiry_items.entries ) <= form.enquiry_items.min_entries: flash('最少保留%s条记录' % form.enquiry_items.min_entries, 'danger') else: data_line_index = form.data_line_del.data form.enquiry_items.entries.pop(data_line_index) return render_template(template_name, form=form, **document_info) # 表单校验失败 if not form.validate_on_submit(): flash(_('Add Failure'), 'danger') # flash(form.errors, 'danger') return render_template(template_name, form=form, **document_info) # 表单校验成功 # 创建询价 current_time = datetime.utcnow() enquiry_data = { 'uid': form.uid.data, 'supplier_cid': form.supplier_cid.data, 'supplier_contact_id': form.supplier_contact_id.data, 'status_order': form.status_order.data, 'expiry_date': (datetime.utcnow() + timedelta(days=7)).strftime('%Y-%m-%d'), 'create_time': current_time, 'update_time': current_time, } enquiry_id = add_enquiry(enquiry_data) amount_enquiry = 0 for enquiry_item in form.enquiry_items.entries: current_time = datetime.utcnow() enquiry_item_data = { 'enquiry_id': enquiry_id, 'uid': form.uid.data, 'supplier_cid': form.supplier_cid.data, 'supplier_company_name': get_supplier_row_by_id(form.supplier_cid.data).company_name, 'enquiry_production_model': enquiry_item.form.enquiry_production_model.data, 'enquiry_quantity': enquiry_item.form.enquiry_quantity.data, 'production_id': enquiry_item.form.production_id.data, 'production_brand': enquiry_item.form.production_brand.data, 'production_model': enquiry_item.form.production_model.data, 'production_sku': enquiry_item.form.production_sku.data, 'note': enquiry_item.form.note.data, 'delivery_time': enquiry_item.form.delivery_time.data, 'quantity': enquiry_item.form.quantity.data, 'unit_price': enquiry_item.form.unit_price.data, 'status_ordered': enquiry_item.form.status_ordered.data, 'create_time': current_time, 'update_time': current_time, } # 新增 add_enquiry_items(enquiry_item_data) amount_enquiry += (enquiry_item_data['quantity'] or 0) * (enquiry_item_data['unit_price'] or 0) # 更新询价 enquiry_data = { 'amount_production': amount_enquiry, 'amount_enquiry': amount_enquiry, 'update_time': current_time, } result = edit_enquiry(enquiry_id, enquiry_data) # todo 事务 # 明细保存 # 总表保存 # 创建操作成功 if result: flash(_('Add Success'), 'success') return redirect( request.args.get('next') or url_for('enquiry.lists')) # 创建操作失败 else: flash(_('Add Failure'), 'danger') return render_template(template_name, form=form, **document_info)
def lists(): """ 询价列表 :return: """ template_name = 'enquiry/items/lists.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('enquiry item lists') # 搜索条件 form = EnquiryItemsSearchForm(request.form) # form.uid.choices = get_enquiry_user_list_choices() # app.logger.info('') search_condition = [ EnquiryItems.status_delete == STATUS_DEL_NO, ] if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Search Failure'), 'danger') # 单独处理csrf_token if hasattr(form, 'csrf_token') and getattr(form, 'csrf_token').errors: map(lambda x: flash(x, 'danger'), form.csrf_token.errors) else: if form.supplier_cid.data and form.supplier_company_name.data: search_condition.append( EnquiryItems.supplier_cid == form.supplier_cid.data) if form.production_model.data: # 注意查询效率 search_condition.append( or_( EnquiryItems.production_model.like( '%%%s%%' % form.production_model.data), EnquiryItems.enquiry_production_model.like( '%%%s%%' % form.production_model.data))) if form.start_create_time.data: search_condition.append( EnquiryItems.create_time >= form.start_create_time.data) if form.end_create_time.data: search_condition.append( EnquiryItems.create_time <= form.end_create_time.data) # 处理导出 if form.op.data == OPERATION_EXPORT: # 检查导出权限 if not permission_enquiry_section_export.can(): abort(403) column_names = EnquiryItems.__table__.columns.keys() query_sets = get_enquiry_items_rows(*search_condition) return excel.make_response_from_query_sets( query_sets=query_sets, column_names=column_names, file_type='csv', file_name='%s.csv' % _('enquiry item lists')) # 翻页数据 pagination = get_enquiry_items_pagination(form.page.data, PER_PAGE_BACKEND, *search_condition) # 渲染模板 return render_template(template_name, form=form, pagination=pagination, **document_info)