예제 #1
0
def add():
    """
    创建货架
    :return:
    """
    template_name = 'rack/add.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('rack add')

    # 加载创建表单
    form = RackAddForm(request.form)
    form.warehouse_id.choices = get_warehouse_choices(option_type='create')

    # 进入创建页面
    if request.method == 'GET':
        # 渲染页面
        return render_template(template_name, form=form, **document_info)

    # 处理创建请求
    if request.method == 'POST':
        # 表单校验失败
        if not form.validate_on_submit():
            flash(_('Add Failure'), 'danger')
            return render_template(template_name, form=form, **document_info)

        # 表单校验成功
        current_time = datetime.utcnow()
        rack_data = {
            'warehouse_id': form.warehouse_id.data,
            'name': form.name.data,
            'create_time': current_time,
            'update_time': current_time,
        }
        result = add_rack(rack_data)
        # 创建操作成功
        if result:
            flash(_('Add Success'), 'success')
            return redirect(request.args.get('next') or url_for('rack.lists'))
        # 创建操作失败
        else:
            flash(_('Add Failure'), 'danger')
            return render_template(template_name, form=form, **document_info)
예제 #2
0
def lists():
    """
    货架列表
    :return:
    """
    template_name = 'rack/lists.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('rack lists')

    # 搜索条件
    form = RackSearchForm(request.form)
    form.warehouse_id.choices = get_warehouse_choices()
    # app.logger.info('')

    search_condition = [
        Rack.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.warehouse_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(
                    Rack.warehouse_id == form.warehouse_id.data)
            if form.name.data:
                search_condition.append(Rack.name == form.name.data)
        # 处理导出
        if form.op.data == OPERATION_EXPORT:
            # 检查导出权限
            if not permission_rack_section_export.can():
                abort(403)
            column_names = Rack.__table__.columns.keys()
            query_sets = get_rack_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' % _('rack lists'))
        # 批量删除
        if form.op.data == OPERATION_DELETE:
            # 检查删除权限
            if not permission_rack_section_del.can():
                abort(403)
            rack_ids = request.form.getlist('rack_id')
            permitted = True
            for rack_id in rack_ids:
                # 检查是否正在使用
                # 库存
                if count_inventory(**{
                        'rack_id': rack_id,
                        'status_delete': STATUS_DEL_NO
                }):
                    ext_msg = _('Currently In Use')
                    flash(_('Del Failure, %(ext_msg)s', ext_msg=ext_msg),
                          'danger')
                    permitted = False
                    break
            if permitted:
                result_total = True
                for rack_id in rack_ids:
                    current_time = datetime.utcnow()
                    rack_data = {
                        'status_delete': STATUS_DEL_OK,
                        'delete_time': current_time,
                        'update_time': current_time,
                    }
                    result = edit_rack(rack_id, rack_data)
                    result_total = result_total and result
                if result_total:
                    flash(_('Del Success'), 'success')
                else:
                    flash(_('Del Failure'), 'danger')
    # 翻页数据
    pagination = get_rack_pagination(form.page.data, PER_PAGE_BACKEND,
                                     *search_condition)

    # 渲染模板
    return render_template(template_name,
                           form=form,
                           pagination=pagination,
                           **document_info)
예제 #3
0
def edit(rack_id):
    """
    货架编辑
    """
    rack_info = get_rack_row_by_id(rack_id)
    # 检查资源是否存在
    if not rack_info:
        abort(404)
    # 检查资源是否删除
    if rack_info.status_delete == STATUS_DEL_OK:
        abort(410)

    template_name = 'rack/edit.html'

    # 加载编辑表单
    form = RackEditForm(request.form)
    form.warehouse_id.choices = get_warehouse_choices(option_type='update')

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

    # 进入编辑页面
    if request.method == 'GET':
        # 表单赋值
        form.warehouse_id.data = rack_info.warehouse_id
        form.name.data = rack_info.name
        # form.create_time.data = rack_info.create_time
        # form.update_time.data = rack_info.update_time
        # 渲染页面
        return render_template(template_name,
                               rack_id=rack_id,
                               form=form,
                               **document_info)

    # 处理编辑请求
    if request.method == 'POST':
        # 表单校验失败
        if not form.validate_on_submit():
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   rack_id=rack_id,
                                   form=form,
                                   **document_info)
        # 表单校验成功
        current_time = datetime.utcnow()
        rack_data = {
            'warehouse_id': form.warehouse_id.data,
            'name': form.name.data,
            'update_time': current_time,
        }
        result = edit_rack(rack_id, rack_data)
        # 编辑操作成功
        if result:
            flash(_('Edit Success'), 'success')
            return redirect(request.args.get('next') or url_for('rack.lists'))
        # 编辑操作失败
        else:
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   rack_id=rack_id,
                                   form=form,
                                   **document_info)
예제 #4
0
def lists():
    """
    库存列表
    :return:
    """
    template_name = 'inventory/lists.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('inventory lists')

    # 搜索条件
    form = InventorySearchForm(request.form)
    form.warehouse_id.choices = get_warehouse_choices()
    form.rack_id.choices = get_rack_choices(form.warehouse_id.data)
    # app.logger.info('')

    inventory_brand_choices = [
        (brand, brand)
        for brand in get_distinct_inventory_brand(status_delete=STATUS_DEL_NO)
        if brand != ''
    ]

    form.production_brand.choices = DEFAULT_SEARCH_CHOICES_STR + inventory_brand_choices

    search_condition = [
        Inventory.status_delete == STATUS_DEL_NO,
        Inventory.stock_qty_current > 0,
    ]
    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.warehouse_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(
                    Inventory.warehouse_id == form.warehouse_id.data)
            if form.rack_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION:
                search_condition.append(Inventory.rack_id == form.rack_id.data)
            if form.production_brand.data != DEFAULT_SEARCH_CHOICES_STR_OPTION:
                search_condition.append(
                    Inventory.production_brand == form.production_brand.data)
            if form.production_model.data:
                search_condition.append(
                    Inventory.production_model.like(
                        '%%%s%%' % form.production_model.data))
        # 处理导出
        if form.op.data == OPERATION_EXPORT:
            # 检查导出权限
            if not permission_inventory_section_export.can():
                abort(403)
            column_names = Inventory.__table__.columns.keys()
            query_sets = get_inventory_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' % _('inventory lists'))
        # 批量删除
        if form.op.data == OPERATION_DELETE:
            # 检查删除权限
            if not permission_inventory_section_del.can():
                abort(403)

            inventory_ids = request.form.getlist('inventory_id')
            result_total = True
            for inventory_id in inventory_ids:
                current_time = datetime.utcnow()
                inventory_data = {
                    'status_delete': STATUS_DEL_OK,
                    'delete_time': current_time,
                    'update_time': current_time,
                }
                result = edit_inventory(inventory_id, inventory_data)
                result_total = result_total and result
            if result_total:
                flash(_('Del Success'), 'success')
            else:
                flash(_('Del Failure'), 'danger')
    # 翻页数据
    pagination = get_inventory_pagination(form.page.data, PER_PAGE_BACKEND,
                                          *search_condition)

    # 渲染模板
    return render_template(template_name,
                           form=form,
                           pagination=pagination,
                           **document_info)
예제 #5
0
def transfer(inventory_id):
    """
    库存转移
    :param inventory_id:
    :return:
    """
    inventory_info = get_inventory_row_by_id(inventory_id)  # type: Inventory
    # 检查资源是否存在
    if not inventory_info:
        abort(404)
    # 检查资源是否删除
    if inventory_info.status_delete == STATUS_DEL_OK:
        abort(410)

    template_name = 'inventory/transfer.html'

    # 加载编辑表单
    form = InventoryTransferForm(request.form)

    form.warehouse_id.choices = get_warehouse_choices(option_type='update')
    form.rack_id.choices = get_rack_choices(form.warehouse_id.data
                                            or inventory_info.warehouse_id,
                                            option_type='update')

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

    # 进入编辑页面
    if request.method == 'GET':
        # 表单赋值
        form.production_id.data = inventory_info.production_id
        form.production_brand.data = inventory_info.production_brand
        form.production_model.data = inventory_info.production_model
        form.production_sku.data = inventory_info.production_sku
        form.warehouse_id.data = inventory_info.warehouse_id
        form.rack_id.data = inventory_info.rack_id
        form.warehouse_name_from.data = inventory_info.warehouse_name
        form.rack_name_from.data = inventory_info.rack_name
        form.stock_qty.data = inventory_info.stock_qty_current
        form.note.data = inventory_info.note
        # 渲染页面
        return render_template(template_name,
                               inventory_id=inventory_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 not form.validate_on_submit():
            flash(_('Transfer Failure'), 'danger')
            return render_template(template_name,
                                   inventory_id=inventory_id,
                                   form=form,
                                   **document_info)
        # 表单校验成功
        try:
            transfer_inventory(inventory_id, form.warehouse_id.data,
                               form.rack_id.data, form.stock_qty.data)
            # 编辑操作成功
            flash(_('Transfer Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('inventory.lists'))
        except Exception as e:
            # 编辑操作失败
            flash('%s, %s' % (_('Transfer Failure'), e.message), 'danger')
            return render_template(template_name,
                                   inventory_id=inventory_id,
                                   form=form,
                                   **document_info)
예제 #6
0
def edit(inventory_id):
    """
    库存编辑
    """
    inventory_info = get_inventory_row_by_id(inventory_id)  # type: Inventory
    # 检查资源是否存在
    if not inventory_info:
        abort(404)
    # 检查资源是否删除
    if inventory_info.status_delete == STATUS_DEL_OK:
        abort(410)

    template_name = 'inventory/edit.html'

    # 加载编辑表单
    form = InventoryEditForm(request.form)

    form.warehouse_id.choices = get_warehouse_choices(option_type='update')
    form.rack_id.choices = get_rack_choices(form.warehouse_id.data
                                            or inventory_info.warehouse_id,
                                            option_type='update')

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

    # 进入编辑页面
    if request.method == 'GET':
        # 表单赋值
        form.production_id.data = inventory_info.production_id
        form.production_brand.data = inventory_info.production_brand
        form.production_model.data = inventory_info.production_model
        form.production_sku.data = inventory_info.production_sku
        form.warehouse_id.data = inventory_info.warehouse_id
        form.rack_id.data = inventory_info.rack_id
        form.stock_qty.data = inventory_info.stock_qty_current
        form.note.data = inventory_info.note
        # 渲染页面
        return render_template(template_name,
                               inventory_id=inventory_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 not form.validate_on_submit():
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   inventory_id=inventory_id,
                                   form=form,
                                   **document_info)
        # 表单校验成功
        current_time = datetime.utcnow()
        # 获取产品信息
        production_info = get_production_row_by_id(form.production_id.data)
        if not production_info:
            abort(404, exceptions.NotFound(description='production'))
        # 获取仓库信息
        warehouse_info = get_warehouse_row_by_id(form.warehouse_id.data)
        if not warehouse_info:
            abort(404, exceptions.NotFound(description='warehouse'))
        # 获取货架信息
        rack_info = get_rack_row_by_id(form.rack_id.data)
        if not rack_info:
            abort(404, exceptions.NotFound(description='rack'))
        inventory_data = {
            'production_id': form.production_id.data,
            'production_brand': production_info.production_brand,
            'production_model': production_info.production_model,
            'production_sku': production_info.production_sku,
            'warehouse_id': form.warehouse_id.data,
            'warehouse_name': warehouse_info.name,
            'rack_id': form.rack_id.data,
            'rack_name': rack_info.name,
            'stock_qty_current': form.stock_qty.data,
            'note': form.note.data,
            'create_time': current_time,
            'update_time': current_time,
        }
        result = edit_inventory(inventory_id, inventory_data)
        # 编辑操作成功
        if result:
            flash(_('Edit Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('inventory.lists'))
        # 编辑操作失败
        else:
            flash(_('Edit Failure'), 'danger')
            return render_template(template_name,
                                   inventory_id=inventory_id,
                                   form=form,
                                   **document_info)
예제 #7
0
def add():
    """
    创建库存
    :return:
    """
    template_name = 'inventory/add.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('inventory add')

    # 加载创建表单
    form = InventoryAddForm(request.form)

    form.warehouse_id.choices = get_warehouse_choices(option_type='update')
    form.rack_id.choices = get_rack_choices(form.warehouse_id.data,
                                            option_type='update')

    # 进入创建页面
    if request.method == 'GET':
        # 渲染页面
        return render_template(template_name, 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 not form.validate_on_submit():
            flash(_('Add Failure'), 'danger')
            return render_template(template_name, form=form, **document_info)

        # 表单校验成功
        current_time = datetime.utcnow()
        # 获取产品信息
        production_info = get_production_row_by_id(form.production_id.data)
        if not production_info:
            abort(404, exceptions.NotFound(description='production'))
        # 获取仓库信息
        warehouse_info = get_warehouse_row_by_id(form.warehouse_id.data)
        if not warehouse_info:
            abort(404, exceptions.NotFound(description='warehouse'))
        # 获取货架信息
        rack_info = get_rack_row_by_id(form.rack_id.data)
        if not rack_info:
            abort(404, exceptions.NotFound(description='rack'))
        inventory_data = {
            'production_id': form.production_id.data,
            'production_brand': production_info.production_brand,
            'production_model': production_info.production_model,
            'production_sku': production_info.production_sku,
            'warehouse_id': form.warehouse_id.data,
            'warehouse_name': warehouse_info.name,
            'rack_id': form.rack_id.data,
            'rack_name': rack_info.name,
            'stock_qty_current': form.stock_qty.data,
            'note': form.note.data,
            'create_time': current_time,
            'update_time': current_time,
        }
        result = add_inventory(inventory_data)
        # 创建操作成功
        if result:
            flash(_('Add Success'), 'success')
            return redirect(
                request.args.get('next') or url_for('inventory.lists'))
        # 创建操作失败
        else:
            flash(_('Add Failure'), 'danger')
            return render_template(template_name, form=form, **document_info)
예제 #8
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)
예제 #9
0
def add():
    """
    新增销售出货
    :return:
    """
    # return jsonify({})
    template_name = 'delivery/add.html'
    # 文档信息
    document_info = DOCUMENT_INFO.copy()
    document_info['TITLE'] = _('delivery add')

    # 加载创建表单
    form = DeliveryAddForm(request.form)
    form.uid.choices = get_user_choices()
    form.uid.data = current_user.id
    form.warehouse_id.choices = get_warehouse_choices(option_type='create')
    # 内嵌表单货架选项
    for item_form in form.delivery_items:
        item_form.rack_id.choices = get_rack_choices(form.warehouse_id.data,
                                                     option_type='create')

    # 进入创建页面
    if request.method == 'GET':
        # 渲染页面
        return render_template(template_name, 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='create')

            return render_template(template_name, 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, 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()
        delivery_data = {
            'uid': form.uid.data,
            'customer_cid': form.customer_cid.data,
            'customer_contact_id': form.customer_contact_id.data,
            # 'type_delivery': form.type_delivery.data,
            'warehouse_id': form.warehouse_id.data,
            'create_time': current_time,
            'update_time': current_time,
        }
        delivery_id = add_delivery(delivery_data)

        amount_delivery = 0
        for delivery_item in form.delivery_items.entries:
            current_time = datetime.utcnow()
            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,
                '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,
                'warehouse_id':
                form.warehouse_id.data,
                'rack_id':
                delivery_item.form.rack_id.data,
                'note':
                delivery_item.form.note.data,
                'quantity':
                delivery_item.form.quantity.data,
                'unit_price':
                delivery_item.form.unit_price.data,
                'create_time':
                current_time,
                'update_time':
                current_time,
            }

            # 新增
            add_delivery_items(delivery_item_data)
            amount_delivery += (delivery_item_data['quantity']
                                or 0) * (delivery_item_data['unit_price'] or 0)

        # 更新报价
        delivery_data = {
            'amount_production': amount_delivery,
            'amount_delivery': amount_delivery,
            'update_time': current_time,
        }
        result = edit_delivery(delivery_id, delivery_data)

        # todo 事务

        # 明细保存
        # 总表保存

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