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

    # 检查审核权限
    if not permission_quotation_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)

    # 检查请求参数
    quotation_id = request.args.get('quotation_id', 0, type=int)
    audit_status = request.args.get('audit_status', 0, type=int)
    if not quotation_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)

    quotation_info = get_quotation_row_by_id(quotation_id)
    # 检查资源是否存在
    if not quotation_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 quotation_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 quotation_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()
    quotation_data = {
        'status_audit': audit_status,
        'audit_time': current_time,
        'update_time': current_time,
    }
    result = edit_quotation(quotation_id, quotation_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)
Example #2
0
def ajax_delete():
    """
    报价删除
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

    # 检查删除权限
    if not permission_quotation_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)

    # 检查请求参数
    quotation_id = request.args.get('quotation_id', 0, type=int)
    if not quotation_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)

    quotation_info = get_quotation_row_by_id(quotation_id)
    # 检查资源是否存在
    if not quotation_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 quotation_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_failure_msg)

    current_time = datetime.utcnow()
    quotation_data = {
        'status_delete': STATUS_DEL_OK,
        'delete_time': current_time,
        'update_time': current_time,
    }
    result = edit_quotation(quotation_id, quotation_data)
    if result:
        # 发送删除信号
        signal_data = {
            'quotation_id': quotation_id,
            'status_delete': STATUS_DEL_OK,
            'current_time': current_time,
        }
        signal_quotation_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)
Example #3
0
def edit(quotation_id):
    """
    报价编辑
    """
    quotation_info = get_quotation_row_by_id(quotation_id)
    # 检查资源是否存在
    if not quotation_info:
        abort(404)
    # 检查资源是否删除
    if quotation_info.status_delete == STATUS_DEL_OK:
        abort(410)

    template_name = 'quotation/edit.html'

    # 加载编辑表单
    form = QuotationEditForm(request.form)
    form.uid.choices = get_user_choices()
    form.status_order.choices = STATUS_ORDER_CHOICES

    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('quotation edit')

    # 进入编辑页面
    if request.method == 'GET':
        # 获取明细
        quotation_items = get_quotation_items_rows(quotation_id=quotation_id)
        # 表单赋值
        form.uid.data = quotation_info.uid
        form.customer_cid.data = quotation_info.customer_cid
        form.customer_contact_id.data = quotation_info.customer_contact_id
        form.delivery_way.data = quotation_info.delivery_way
        form.note.data = quotation_info.note
        form.status_order.data = quotation_info.status_order
        form.amount_quotation.data = quotation_info.amount_quotation
        # form.quotation_items = quotation_items
        while len(form.quotation_items) > 0:
            form.quotation_items.pop_entry()
        for quotation_item in quotation_items:
            quotation_item_form = QuotationItemEditForm()
            quotation_item_form.id = quotation_item.id
            quotation_item_form.quotation_id = quotation_item.quotation_id
            quotation_item_form.uid = quotation_item.uid
            quotation_item_form.enquiry_production_model = quotation_item.enquiry_production_model
            quotation_item_form.enquiry_quantity = quotation_item.enquiry_quantity
            quotation_item_form.production_id = quotation_item.production_id
            quotation_item_form.production_brand = quotation_item.production_brand
            quotation_item_form.production_model = quotation_item.production_model
            quotation_item_form.production_sku = quotation_item.production_sku
            quotation_item_form.note = quotation_item.note
            quotation_item_form.quantity = quotation_item.quantity
            quotation_item_form.unit_price = quotation_item.unit_price
            quotation_item_form.delivery_time = quotation_item.delivery_time
            quotation_item_form.status_ordered = quotation_item.status_ordered
            form.quotation_items.append_entry(quotation_item_form)
        # 渲染页面
        return render_template(template_name,
                               quotation_id=quotation_id,
                               form=form,
                               **document_info)

    # 处理编辑请求
    if request.method == 'POST':
        # 增删数据行不需要校验表单

        # 表单新增空行
        if form.data_line_add.data is not None:
            if form.quotation_items.max_entries and len(
                    form.quotation_items.entries
            ) >= form.quotation_items.max_entries:
                flash('最多创建%s条记录' % form.quotation_items.max_entries, 'danger')
            else:
                form.quotation_items.append_entry()

            return render_template(template_name,
                                   quotation_id=quotation_id,
                                   form=form,
                                   **document_info)
        # 表单删除一行
        if form.data_line_del.data is not None:
            if form.quotation_items.min_entries and len(
                    form.quotation_items.entries
            ) <= form.quotation_items.min_entries:
                flash('最少保留%s条记录' % form.quotation_items.min_entries, 'danger')
            else:
                data_line_index = form.data_line_del.data
                form.quotation_items.entries.pop(data_line_index)

            return render_template(template_name,
                                   quotation_id=quotation_id,
                                   form=form,
                                   **document_info)

        # 表单校验失败
        if not form.validate_on_submit():
            flash(_('Edit Failure'), 'danger')
            # flash(form.errors, 'danger')
            # flash(form.quotation_items.errors, 'danger')
            return render_template(template_name,
                                   quotation_id=quotation_id,
                                   form=form,
                                   **document_info)
        # 表单校验成功

        # 获取明细
        quotation_items = get_quotation_items_rows(quotation_id=quotation_id)
        quotation_items_ids = [item.id for item in quotation_items]

        # 数据新增、数据删除、数据修改

        quotation_items_ids_new = []
        amount_quotation = 0
        for quotation_item in form.quotation_items.entries:
            # 错误
            if quotation_item.form.id.data and quotation_item.form.id.data not in quotation_items_ids:
                continue

            quotation_item_data = {
                'quotation_id':
                quotation_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,
                'enquiry_production_model':
                quotation_item.form.enquiry_production_model.data,
                'enquiry_quantity':
                quotation_item.form.enquiry_quantity.data,
                'production_id':
                quotation_item.form.production_id.data,
                'production_brand':
                quotation_item.form.production_brand.data,
                'production_model':
                quotation_item.form.production_model.data,
                'production_sku':
                quotation_item.form.production_sku.data,
                'note':
                quotation_item.form.note.data,
                'delivery_time':
                quotation_item.form.delivery_time.data,
                'quantity':
                quotation_item.form.quantity.data,
                'unit_price':
                quotation_item.form.unit_price.data,
                'status_ordered':
                quotation_item.form.status_ordered.data,
            }

            if not quotation_item.form.id.data:
                # 新增
                add_quotation_items(quotation_item_data)
                amount_quotation += quotation_item_data[
                    'quantity'] * quotation_item_data['unit_price']
            else:
                # 修改
                edit_quotation_items(quotation_item.form.id.data,
                                     quotation_item_data)
                amount_quotation += quotation_item_data[
                    'quantity'] * quotation_item_data['unit_price']
                quotation_items_ids_new.append(quotation_item.form.id.data)
        # 删除
        quotation_items_ids_del = list(
            set(quotation_items_ids) - set(quotation_items_ids_new))
        for quotation_items_id in quotation_items_ids_del:
            delete_quotation_items(quotation_items_id)

        # 更新报价
        current_time = datetime.utcnow()
        quotation_data = {
            'customer_cid': form.customer_cid.data,
            'uid': form.uid.data,
            'customer_contact_id': form.customer_contact_id.data,
            'delivery_way': form.delivery_way.data,
            'note': form.note.data,
            'status_order': form.status_order.data,
            'amount_production': amount_quotation,
            'amount_quotation': amount_quotation,
            'update_time': current_time,
        }
        result = edit_quotation(quotation_id, quotation_data)

        # 编辑操作成功
        if result:
            flash(_('Edit Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('quotation.lists'))
        # 编辑操作失败
        else:
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   quotation_id=quotation_id,
                                   form=form,
                                   **document_info)
Example #4
0
def lists():
    """
    报价列表
    :return:
    """
    template_name = 'quotation/lists.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('quotation lists')

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

    search_condition = [
        Quotation.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(Quotation.uid == form.uid.data)
            if form.customer_cid.data and form.customer_company_name.data:
                search_condition.append(
                    Quotation.customer_cid == form.customer_cid.data)
            if form.start_create_time.data:
                search_condition.append(
                    Quotation.create_time >= form.start_create_time.data)
            if form.end_create_time.data:
                search_condition.append(
                    Quotation.create_time <= form.end_create_time.data)
        # 处理导出
        if form.op.data == OPERATION_EXPORT:
            # 检查导出权限
            if not permission_quotation_section_export.can():
                abort(403)
            column_names = Quotation.__table__.columns.keys()
            query_sets = get_quotation_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' % _('quotation lists'))
        # 批量删除
        if form.op.data == OPERATION_DELETE:
            # 检查删除权限
            if not permission_quotation_section_del.can():
                abort(403)
            quotation_ids = request.form.getlist('quotation_id')
            # 检查删除权限
            permitted = True
            for quotation_id in quotation_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 quotation_id in quotation_ids:
                    current_time = datetime.utcnow()
                    quotation_data = {
                        'status_delete': STATUS_DEL_OK,
                        'delete_time': current_time,
                        'update_time': current_time,
                    }
                    result = edit_quotation(quotation_id, quotation_data)
                    if result:
                        # 发送删除信号
                        signal_data = {
                            'quotation_id': quotation_id,
                            'status_delete': STATUS_DEL_OK,
                            'current_time': current_time,
                        }
                        signal_quotation_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_quotation_pagination(form.page.data, PER_PAGE_BACKEND,
                                          *search_condition)

    # 渲染模板
    return render_template(template_name,
                           form=form,
                           pagination=pagination,
                           **document_info)
Example #5
0
def add():
    """
    创建报价
    :return:
    """
    template_name = 'quotation/add.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('quotation add')

    # 加载创建表单
    form = QuotationAddForm(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 == 'quotation' and from_id:
            quotation_id = from_id
            quotation_info = get_quotation_row_by_id(quotation_id)
            # 检查资源是否存在
            if not quotation_info:
                abort(404)
            # 检查资源是否删除
            if quotation_info.status_delete == STATUS_DEL_OK:
                abort(410)

            # 获取明细
            quotation_items = get_quotation_items_rows(
                quotation_id=quotation_id)
            # 表单赋值
            form.uid.data = quotation_info.uid
            form.customer_cid.data = quotation_info.customer_cid
            form.customer_contact_id.data = quotation_info.customer_contact_id
            form.delivery_way.data = quotation_info.delivery_way
            form.note.data = quotation_info.note
            form.status_order.data = quotation_info.status_order
            form.amount_quotation.data = quotation_info.amount_quotation
            # form.quotation_items = quotation_items
            while len(form.quotation_items) > 0:
                form.quotation_items.pop_entry()
            for quotation_item in quotation_items:
                quotation_item_form = QuotationItemEditForm()
                quotation_item_form.id = quotation_item.id
                quotation_item_form.quotation_id = quotation_item.quotation_id
                quotation_item_form.uid = quotation_item.uid
                quotation_item_form.enquiry_production_model = quotation_item.enquiry_production_model
                quotation_item_form.enquiry_quantity = quotation_item.enquiry_quantity
                quotation_item_form.production_id = quotation_item.production_id
                quotation_item_form.production_brand = quotation_item.production_brand
                quotation_item_form.production_model = quotation_item.production_model
                quotation_item_form.production_sku = quotation_item.production_sku
                quotation_item_form.note = quotation_item.note
                quotation_item_form.quantity = quotation_item.quantity
                quotation_item_form.unit_price = quotation_item.unit_price
                quotation_item_form.delivery_time = quotation_item.delivery_time
                quotation_item_form.status_ordered = quotation_item.status_ordered
                form.quotation_items.append_entry(quotation_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.quotation_items.max_entries and len(
                    form.quotation_items.entries
            ) >= form.quotation_items.max_entries:
                flash('最多创建%s条记录' % form.quotation_items.max_entries, 'danger')
            else:
                form.quotation_items.append_entry()

            return render_template(template_name, form=form, **document_info)
        # 表单删除一行
        if form.data_line_del.data is not None:
            if form.quotation_items.min_entries and len(
                    form.quotation_items.entries
            ) <= form.quotation_items.min_entries:
                flash('最少保留%s条记录' % form.quotation_items.min_entries, 'danger')
            else:
                data_line_index = form.data_line_del.data
                form.quotation_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()
        quotation_data = {
            'uid':
            form.uid.data,
            'customer_cid':
            form.customer_cid.data,
            'customer_contact_id':
            form.customer_contact_id.data,
            'delivery_way':
            form.delivery_way.data,
            'note':
            form.note.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,
        }
        quotation_id = add_quotation(quotation_data)

        amount_quotation = 0
        for quotation_item in form.quotation_items.entries:
            current_time = datetime.utcnow()
            quotation_item_data = {
                'quotation_id':
                quotation_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,
                'enquiry_production_model':
                quotation_item.form.enquiry_production_model.data,
                'enquiry_quantity':
                quotation_item.form.enquiry_quantity.data,
                'production_id':
                quotation_item.form.production_id.data,
                'production_brand':
                quotation_item.form.production_brand.data,
                'production_model':
                quotation_item.form.production_model.data,
                'production_sku':
                quotation_item.form.production_sku.data,
                'note':
                quotation_item.form.note.data,
                'delivery_time':
                quotation_item.form.delivery_time.data,
                'quantity':
                quotation_item.form.quantity.data,
                'unit_price':
                quotation_item.form.unit_price.data,
                'status_ordered':
                quotation_item.form.status_ordered.data,
                'create_time':
                current_time,
                'update_time':
                current_time,
            }

            # 新增
            add_quotation_items(quotation_item_data)
            amount_quotation += (quotation_item_data['quantity'] or
                                 0) * (quotation_item_data['unit_price'] or 0)

        # 更新报价
        quotation_data = {
            'amount_production': amount_quotation,
            'amount_quotation': amount_quotation,
            'update_time': current_time,
        }
        result = edit_quotation(quotation_id, quotation_data)

        # todo 事务

        # 明细保存
        # 总表保存

        # 创建操作成功
        if result:
            flash(_('Add Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('quotation.lists'))
        # 创建操作失败
        else:
            flash(_('Add Failure'), 'danger')
            return render_template(template_name, form=form, **document_info)