예제 #1
0
def ajax_audit():
    """
    报价审核
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

    # 检查审核权限
    if not permission_enquiry_section_audit.can():
        ext_msg = _('Permission Denied')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    # 检查请求方法
    if not (request.method == 'GET' and request.is_xhr):
        ext_msg = _('Method Not Allowed')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    # 检查请求参数
    enquiry_id = request.args.get('enquiry_id', 0, type=int)
    audit_status = request.args.get('audit_status', 0, type=int)
    if not enquiry_id:
        ext_msg = _('ID does not exist')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    enquiry_info = get_enquiry_row_by_id(enquiry_id)
    # 检查资源是否存在
    if not enquiry_info:
        ext_msg = _('ID does not exist')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)
    # 检查资源是否删除
    if enquiry_info.status_delete == STATUS_DEL_OK:
        ext_msg = _('Already deleted')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)
    # 检查审核状态是否变化
    if enquiry_info.status_audit == audit_status:
        ext_msg = _('Already audited')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    current_time = datetime.utcnow()
    enquiry_data = {
        'status_audit': audit_status,
        'audit_time': current_time,
        'update_time': current_time,
    }
    result = edit_enquiry(enquiry_id, enquiry_data)
    if result:
        ajax_success_msg['msg'] = _('Audit Success')
        return jsonify(ajax_success_msg)
    else:
        ajax_failure_msg['msg'] = _('Audit Failure')
        return jsonify(ajax_failure_msg)
예제 #2
0
def lists():
    """
    询价列表
    :return:
    """
    template_name = 'enquiry/lists.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('enquiry lists')

    # 搜索条件
    form = EnquirySearchForm(request.form)
    form.uid.choices = get_enquiry_user_list_choices()
    # app.logger.info('')

    search_condition = [
        Enquiry.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.uid.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(Enquiry.uid == form.uid.data)
            if form.supplier_cid.data and form.supplier_company_name.data:
                search_condition.append(
                    Enquiry.supplier_cid == form.supplier_cid.data)
            if form.start_create_time.data:
                search_condition.append(
                    Enquiry.create_time >= form.start_create_time.data)
            if form.end_create_time.data:
                search_condition.append(
                    Enquiry.create_time <= form.end_create_time.data)
        # 处理导出
        if form.op.data == OPERATION_EXPORT:
            # 检查导出权限
            if not permission_enquiry_section_export.can():
                abort(403)
            column_names = Enquiry.__table__.columns.keys()
            query_sets = get_enquiry_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 lists'))
        # 批量删除
        if form.op.data == OPERATION_DELETE:
            # 检查删除权限
            if not permission_enquiry_section_del.can():
                abort(403)
            enquiry_ids = request.form.getlist('enquiry_id')
            # 检查删除权限
            permitted = True
            for enquiry_id in enquiry_ids:
                # TODO 资源删除权限验证
                if False:
                    ext_msg = _('Permission Denied')
                    flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg),
                          'danger')
                    permitted = False
                    break
            if permitted:
                result_total = True
                for enquiry_id in enquiry_ids:
                    current_time = datetime.utcnow()
                    enquiry_data = {
                        'status_delete': STATUS_DEL_OK,
                        'delete_time': current_time,
                        'update_time': current_time,
                    }
                    result = edit_enquiry(enquiry_id, enquiry_data)
                    if result:
                        # 发送删除信号
                        signal_data = {
                            'enquiry_id': enquiry_id,
                            'status_delete': STATUS_DEL_OK,
                            'current_time': current_time,
                        }
                        signal_enquiry_status_delete.send(app, **signal_data)
                    result_total = result_total and result
                if result_total:
                    flash(_('Del Success'), 'success')
                else:
                    flash(_('Del Failure'), 'danger')
    # 翻页数据
    pagination = get_enquiry_pagination(form.page.data, PER_PAGE_BACKEND,
                                        *search_condition)

    # 渲染模板
    return render_template(template_name,
                           form=form,
                           pagination=pagination,
                           **document_info)
예제 #3
0
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)
예제 #4
0
def ajax_delete():
    """
    询价删除
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

    # 检查删除权限
    if not permission_enquiry_section_del.can():
        ext_msg = _('Permission Denied')
        ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    # 检查请求方法
    if not (request.method == 'GET' and request.is_xhr):
        ext_msg = _('Method Not Allowed')
        ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    # 检查请求参数
    enquiry_id = request.args.get('enquiry_id', 0, type=int)
    if not enquiry_id:
        ext_msg = _('ID does not exist')
        ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    enquiry_info = get_enquiry_row_by_id(enquiry_id)
    # 检查资源是否存在
    if not enquiry_info:
        ext_msg = _('ID does not exist')
        ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)
    # 检查资源是否删除
    if enquiry_info.status_delete == STATUS_DEL_OK:
        ext_msg = _('Already deleted')
        ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_success_msg)

    current_time = datetime.utcnow()
    enquiry_data = {
        'status_delete': STATUS_DEL_OK,
        'delete_time': current_time,
        'update_time': current_time,
    }
    result = edit_enquiry(enquiry_id, enquiry_data)
    if result:
        # 发送删除信号
        signal_data = {
            'enquiry_id': enquiry_id,
            'status_delete': STATUS_DEL_OK,
            'current_time': current_time,
        }
        signal_enquiry_status_delete.send(app, **signal_data)

        ajax_success_msg['msg'] = _('Del Success')
        return jsonify(ajax_success_msg)
    else:
        ajax_failure_msg['msg'] = _('Del Failure')
        return jsonify(ajax_failure_msg)
예제 #5
0
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)