Example #1
0
def filter_delivery_create_time(delivery_id):
    """
    销货创建时间
    :param delivery_id:
    :return:
    """
    delivery_info = get_delivery_row_by_id(delivery_id)
    return delivery_info.create_time if delivery_info else None
Example #2
0
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'))
Example #3
0
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)
Example #4
0
def ajax_audit():
    """
    销售出货审核
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

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

    # 检查请求参数
    delivery_id = request.args.get('delivery_id', 0, type=int)
    audit_status = request.args.get('audit_status', 0, type=int)
    if not delivery_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)
    if audit_status not in [STATUS_AUDIT_NO, STATUS_AUDIT_OK]:
        ext_msg = _('Status not exist')
        ajax_failure_msg['msg'] = _('Audit Failure, %(ext_msg)s',
                                    ext_msg=ext_msg)
        return jsonify(ajax_failure_msg)

    delivery_info = get_delivery_row_by_id(delivery_id)
    # 检查资源是否存在
    if not delivery_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 delivery_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 delivery_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)

    try:
        if audit_status == STATUS_AUDIT_OK:
            result = audit_delivery(delivery_id)
        else:
            result = cancel_audit_delivery(delivery_id)
        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)
    except Exception as e:
        ajax_failure_msg['msg'] = e.message
        return jsonify(ajax_failure_msg)
Example #5
0
def ajax_delete():
    """
    销售出货删除
    :return:
    """
    ajax_success_msg = AJAX_SUCCESS_MSG.copy()
    ajax_failure_msg = AJAX_FAILURE_MSG.copy()

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

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

    delivery_info = get_delivery_row_by_id(delivery_id)
    # 检查资源是否存在
    if not delivery_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 delivery_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()
    delivery_data = {
        'status_delete': STATUS_DEL_OK,
        'delete_time': current_time,
        'update_time': current_time,
    }
    result = edit_delivery(delivery_id, delivery_data)
    if result:
        # 发送删除信号
        signal_data = {
            'delivery_id': delivery_id,
            'status_delete': STATUS_DEL_OK,
            'current_time': current_time,
        }
        signal_delivery_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 #6
0
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)