Beispiel #1
0
def apply_pay(invoice_id):
    agent_invoice = searchAdAgentInvoice.get(invoice_id)
    if not agent_invoice:
        abort(404)
    invoice_pay_ids = request.values.getlist('invoices')
    invoice_pays = searchAdAgentInvoicePay.gets(invoice_pay_ids)
    if not invoice_pays:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))
    to_users = agent_invoice.client_order.direct_sales + agent_invoice.client_order.agent_sales + \
        [agent_invoice.client_order.creator, g.user] + \
        agent_invoice.client_order.leaders
    to_emails = list(set(emails + [x.email for x in to_users]))
    if action == 2:
        action_msg = u'代理打款申请'
        for invoice in invoice_pays:
            invoice.pay_status = AGENT_INVOICE_STATUS_APPLY
            invoice.save()
            flash(u'[%s代理申请打款,打款金额: %s, 发票号: %s]  %s ' %
                  (invoice.agent_invoice.company, invoice.money,
                   invoice.agent_invoice.invoice_num, invoice.detail), 'success')

            invoice.agent_invoice.client_order.add_comment(g.user, u"%s,%s" % (
                action_msg, u'[%s代理申请打款,打款金额: %s, 发票号: %s]  %s ' %
                (invoice.agent_invoice.company, invoice.money,
                    invoice.agent_invoice.invoice_num, invoice.detail)), msg_channel=5)
        send_type = "saler"
    elif action == 3:
        action_msg = u'同意代理订单打款申请,请财务打款'
        to_emails += [k.email for k in User.finances()]
        for invoice in invoice_pays:
            invoice.pay_status = AGENT_INVOICE_STATUS_AGREE
            invoice.save()
            flash(u'[同意%s代理订单打款申请,打款金额: %s, 发票号: %s]  %s ' %
                  (invoice.agent_invoice.company, invoice.money,
                   invoice.agent_invoice.invoice_num, invoice.detail), 'success')

            invoice.agent_invoice.client_order.add_comment(g.user, u"%s,%s" % (
                action_msg, u'[%s代理同意打款,打款金额: %s, 发票号: %s]  %s ' %
                (invoice.agent_invoice.company, invoice.money,
                    invoice.agent_invoice.invoice_num, invoice.detail)), msg_channel=5)
        send_type = "finance"
    else:
        action_msg = u'消息提醒'

    apply_context = {"to_other": emails,
                     "title": action_msg,
                     "to": to_emails,
                     "action_msg": action_msg,
                     "msg": msg,
                     "invoice": agent_invoice,
                     "send_type": send_type,
                     "invoice_pays": invoice_pays}
    agent_invoice_apply_signal.send(
        current_app._get_current_object(), apply_context=apply_context)
    flash(u'[%s 打款发票开具申请] 已发送邮件给 %s ' %
          (agent_invoice.client_order, ', '.join(to_emails)), 'info')
    return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
Beispiel #2
0
def invoice(invoice_id):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    form = get_invoice_form(invoice)
    reminder_emails = [(u.name, u.email) for u in User.all_active()]
    return tpl('/saler/searchAd_order/agent/invoice.html', form=form, invoice=invoice, reminder_emails=reminder_emails,
               INVOICE_TYPE_CN=INVOICE_TYPE_CN, AGENT_INVOICE_STATUS_CN=AGENT_INVOICE_STATUS_CN)
Beispiel #3
0
def pay_info(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    form = get_invoice_from(invoice)
    reminder_emails = [(u.name, u.email) for u in User.all_active()]
    return tpl('/finance/searchAd_order/agent_pay/pay_info.html',
               form=form, invoice=invoice, reminder_emails=reminder_emails,
               INVOICE_TYPE_CN=INVOICE_TYPE_CN)
Beispiel #4
0
def delete_invoice_pay(invoice_id, invoice_pay_id):
    pay = searchAdAgentInvoicePay.get(invoice_pay_id)
    money = pay.money
    mi = searchAdAgentInvoice.get(invoice_id)
    pay.delete()
    mi.client_order.add_comment(g.user, u"删除已打款信息  发票号:%s  打款金额:%s元" % (
        mi.invoice_num, str(money)), msg_channel=5)
    flash(u'删除已打款信息成功!', 'success')
    return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
Beispiel #5
0
def apply_invoice(invoice_id):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    invoices_ids = request.values.getlist('invoices')
    invoices = searchAdAgentInvoice.gets(invoices_ids)
    if not invoices:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))
    to_users = invoice.client_order.direct_sales + invoice.client_order.agent_sales + \
        [invoice.client_order.creator, g.user] + \
        invoice.client_order.leaders
    to_emails = list(set(emails + [x.email for x in to_users]))

    send_type = "saler"
    if action == 2:
        invoice_status = AGENT_INVOICE_STATUS_APPLY
        action_msg = u'打款发票开具申请'
    if action != 10:
        for invoice in invoices:
            invoice.invoice_status = invoice_status
            invoice.save()
            flash(
                u'[%s 打款发票申请,发票金额: %s, 发票号: %s]  %s ' %
                (invoice.company, invoice.money, invoice.invoice_num,
                 action_msg), 'success')
            invoice.client_order.add_comment(
                g.user,
                u"%s,%s" %
                (action_msg, u'打款发票内容: %s; 发票金额: %s元; 发票号: %s' %
                 (invoice.detail, str(invoice.money), invoice.invoice_num)),
                msg_channel=5)
    else:
        action_msg = u'消息提醒'

    apply_context = {
        "sender": g.user,
        "title": action_msg,
        "to": to_emails,
        "action_msg": action_msg,
        "msg": msg,
        "order": invoice.client_order,
        "send_type": send_type,
        "invoices": invoices
    }
    agent_invoice_apply_signal.send(current_app._get_current_object(),
                                    apply_context=apply_context)
    flash(
        u'[%s 打款发票开具申请] 已发送邮件给 %s ' %
        (invoice.client_order, ', '.join(to_emails)), 'info')
    return redirect(
        url_for("searchAd_saler_client_order_agent_invoice.index",
                order_id=invoice.client_order.id))
Beispiel #6
0
def pay_info(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    form = get_invoice_from(invoice)
    reminder_emails = [(u.name, u.email) for u in User.all_active()]
    return tpl('/finance/searchAd_order/agent_pay/pay_info.html',
               form=form,
               invoice=invoice,
               reminder_emails=reminder_emails,
               INVOICE_TYPE_CN=INVOICE_TYPE_CN)
Beispiel #7
0
def invoice_pass(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    invoices_ids = request.values.getlist('invoices')
    invoices_pay = searchAdAgentInvoicePay.gets(invoices_ids)
    if not invoices_pay:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))

    to_users = invoice.client_order.direct_sales + invoice.client_order.agent_sales + \
        [invoice.client_order.creator, g.user] + \
        User.operater_leaders() + User.medias()
    to_emails = list(set(emails + [x.email for x in to_users]))

    if action != 10:
        invoice_status = AGENT_INVOICE_STATUS_PASS
        action_msg = u'代理订单款已打'
        for invoice_pay in invoices_pay:
            invoice_pay.pay_status = invoice_status
            invoice_pay.save()
            flash(
                u'代理订单款已打,名称:%s, 打款金额%s' %
                (invoice_pay.agent_invoice.client_order.name + '-' +
                 invoice_pay.agent_invoice.agent.name, str(invoice_pay.money)),
                'success')
            invoice_pay.agent_invoice.client_order.add_comment(
                g.user,
                u'代理订单款已打款,名称%s, 打款金额%s ' %
                (invoice_pay.agent_invoice.client_order.name + '-' +
                 invoice_pay.agent_invoice.agent.name, str(invoice_pay.money)),
                msg_channel=5)
    else:
        action_msg = u'消息提醒'
    apply_context = {
        "title": "代理订单款已打款",
        "sender": g.user,
        "to": to_emails,
        "action_msg": action_msg,
        "msg": msg,
        "send_type": "saler",
        "invoice": invoice,
        "invoice_pays": invoices_pay
    }
    agent_invoice_apply_signal.send(current_app._get_current_object(),
                                    apply_context=apply_context)
    flash(u'已发送邮件给 %s ' % (', '.join(to_emails)), 'info')
    return redirect(
        url_for("searchAd_finance_client_order_agent_pay.pay_info",
                invoice_id=invoice_id))
Beispiel #8
0
def invoice(invoice_id):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    form = get_invoice_form(invoice)
    reminder_emails = [(u.name, u.email) for u in User.all_active()]
    return tpl('/saler/searchAd_order/agent/invoice.html',
               form=form,
               invoice=invoice,
               reminder_emails=reminder_emails,
               INVOICE_TYPE_CN=INVOICE_TYPE_CN,
               AGENT_INVOICE_STATUS_CN=AGENT_INVOICE_STATUS_CN)
Beispiel #9
0
def delete_invoice_pay(invoice_id, invoice_pay_id):
    pay = searchAdAgentInvoicePay.get(invoice_pay_id)
    money = pay.money
    mi = searchAdAgentInvoice.get(invoice_id)
    pay.delete()
    mi.client_order.add_comment(g.user,
                                u"删除已打款信息  发票号:%s  打款金额:%s元" %
                                (mi.invoice_num, str(money)),
                                msg_channel=5)
    flash(u'删除已打款信息成功!', 'success')
    return redirect(
        url_for('searchAd_saler_client_order_agent_invoice.invoice',
                invoice_id=invoice_id))
Beispiel #10
0
def invoice_pay_num(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    pay_money = request.form.get('pay_money', 0)
    invoice.pay_money = pay_money
    invoice.save()
    flash(u'保存成功!', 'success')
    invoice.client_order.add_comment(g.user,
                                     u"更新了打款金额:\n\r%s" % invoice.pay_money,
                                     msg_channel=5)
    return redirect(url_for("searchAd_finance_client_order_agent_pay.info", order_id=invoice.client_order.id))
Beispiel #11
0
def update_invoice_pay(invoice_id, invoice_pay_id):
    money = float(request.values.get('money', 0))
    pay_time = request.values.get('pay_time', '')
    detail = request.values.get('detail', '')
    pay = searchAdAgentInvoicePay.get(invoice_pay_id)
    mi = searchAdAgentInvoice.get(invoice_id)
    # if mi.pay_invoice_money - pay.money + money > mi.money:
    #     flash(u'付款金额大于发票金额,请重新填写!', 'danger')
    #     return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
    pay.money = money
    pay.pay_time = pay_time
    pay.detail = detail
    pay.save()
    return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
Beispiel #12
0
def invoice_pass(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    invoices_ids = request.values.getlist('invoices')
    invoices_pay = searchAdAgentInvoicePay.gets(invoices_ids)
    if not invoices_pay:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))

    to_users = invoice.client_order.direct_sales + invoice.client_order.agent_sales + \
        [invoice.client_order.creator, g.user] + \
        User.operater_leaders() + User.medias()
    to_emails = list(set(emails + [x.email for x in to_users]))

    if action != 10:
        invoice_status = AGENT_INVOICE_STATUS_PASS
        action_msg = u'代理订单款已打'
        for invoice_pay in invoices_pay:
            invoice_pay.pay_status = invoice_status
            invoice_pay.save()
            flash(u'代理订单款已打,名称:%s, 打款金额%s' % (
                invoice_pay.agent_invoice.client_order.name +
                '-' + invoice_pay.agent_invoice.agent.name,
                str(invoice_pay.money)), 'success')
            invoice_pay.agent_invoice.client_order.add_comment(
                g.user, u'代理订单款已打款,名称%s, 打款金额%s ' % (
                    invoice_pay.agent_invoice.client_order.name +
                        '-' + invoice_pay.agent_invoice.agent.name,
                    str(invoice_pay.money)),
                msg_channel=5)
    else:
        action_msg = u'消息提醒'
    apply_context = {"title": "代理订单款已打款",
                     "sender": g.user,
                     "to": to_emails,
                     "action_msg": action_msg,
                     "msg": msg,
                     "send_type": "saler",
                     "invoice": invoice,
                     "invoice_pays": invoices_pay}
    agent_invoice_apply_signal.send(
        current_app._get_current_object(), apply_context=apply_context)
    flash(u'已发送邮件给 %s ' % (', '.join(to_emails)), 'info')
    return redirect(url_for("searchAd_finance_client_order_agent_pay.pay_info", invoice_id=invoice_id))
Beispiel #13
0
def update_invoice(
        invoice_id,
        redirect_endpoint='searchAd_saler_client_order_agent_invoice.index'):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    form = AgentInvoiceForm(request.form)
    form.client_order.choices = [(invoice.client_order.id,
                                  invoice.client_order.name)]
    form.agent.choices = [(invoice.agent.id, invoice.agent.name)]
    form.bool_invoice.bool_invoice = AGENT_INVOICE_BOOL_INVOICE_CN.items()
    # order = invoice.client_order
    # if order.agent_money < order.agents_invoice_sum + float(form.money.data):
    #    flash(u'新建打款发票失败,发票超过代理总金额!', 'danger')
    #    return redirect(url_for(redirect_endpoint, order_id=order.id))
    if request.method == 'POST':
        if not form.invoice_num.data:
            flash(u"修改打款发票失败,发票号不能为空", 'danger')
        elif not form.money.data:
            flash(u"修改打款发票失败,发票金额不能为空", 'danger')
        else:
            invoice.company = form.company.data,
            invoice.tax_id = form.tax_id.data,
            invoice.address = form.address.data,
            invoice.phone = form.phone.data,
            invoice.bank_id = form.bank_id.data,
            invoice.bank = form.bank.data,
            invoice.detail = form.detail.data,
            invoice.money = form.money.data,
            invoice.pay_money = form.money.data,
            invoice.invoice_type = form.invoice_type.data,
            invoice.invoice_num = form.invoice_num.data,
            invoice.creator = g.user
            invoice.create_time = datetime.date.today()
            invoice.add_time = form.add_time.data
            invoice.bool_invoice = form.bool_invoice.data
            invoice.save()
            flash(u'修改打款发票(%s)成功!' % form.company.data, 'success')
            invoice.client_order.add_comment(
                g.user,
                u"修改打款发票信息,%s" %
                (u'打款发票内容: %s; 发票金额: %s元; 发票号: %s' %
                 (invoice.detail, str(invoice.money), invoice.invoice_num)),
                msg_channel=5)
    else:
        for k in form.errors:
            flash(u"修改打款发票失败,%s" % (form.errors[k][0]), 'danger')
    return redirect(
        url_for(redirect_endpoint, order_id=invoice.client_order.id))
Beispiel #14
0
def invoice_pay_num(invoice_id):
    if not g.user.is_finance():
        abort(404)
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    pay_money = request.form.get('pay_money', 0)
    invoice.pay_money = pay_money
    invoice.save()
    flash(u'保存成功!', 'success')
    invoice.client_order.add_comment(g.user,
                                     u"更新了打款金额:\n\r%s" % invoice.pay_money,
                                     msg_channel=5)
    return redirect(
        url_for("searchAd_finance_client_order_agent_pay.info",
                order_id=invoice.client_order.id))
Beispiel #15
0
def update_invoice_pay(invoice_id, invoice_pay_id):
    money = float(request.values.get('money', 0))
    pay_time = request.values.get('pay_time', '')
    detail = request.values.get('detail', '')
    pay = searchAdAgentInvoicePay.get(invoice_pay_id)
    mi = searchAdAgentInvoice.get(invoice_id)
    # if mi.pay_invoice_money - pay.money + money > mi.money:
    #     flash(u'付款金额大于发票金额,请重新填写!', 'danger')
    #     return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
    pay.money = money
    pay.pay_time = pay_time
    pay.detail = detail
    pay.save()
    return redirect(
        url_for('searchAd_saler_client_order_agent_invoice.invoice',
                invoice_id=invoice_id))
Beispiel #16
0
def apply_invoice(invoice_id):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    invoices_ids = request.values.getlist('invoices')
    invoices = searchAdAgentInvoice.gets(invoices_ids)
    if not invoices:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))
    to_users = invoice.client_order.direct_sales + invoice.client_order.agent_sales + \
        [invoice.client_order.creator, g.user] + \
        invoice.client_order.leaders
    to_emails = list(set(emails + [x.email for x in to_users]))

    send_type = "saler"
    if action == 2:
        invoice_status = AGENT_INVOICE_STATUS_APPLY
        action_msg = u'打款发票开具申请'
    if action != 10:
        for invoice in invoices:
            invoice.invoice_status = invoice_status
            invoice.save()
            flash(u'[%s 打款发票申请,发票金额: %s, 发票号: %s]  %s ' %
                  (invoice.company, invoice.money, invoice.invoice_num, action_msg), 'success')
            invoice.client_order.add_comment(g.user, u"%s,%s" % (
                action_msg, u'打款发票内容: %s; 发票金额: %s元; 发票号: %s' %
                (invoice.detail, str(invoice.money), invoice.invoice_num)), msg_channel=5)
    else:
        action_msg = u'消息提醒'

    apply_context = {"sender": g.user,
                     "title": action_msg,
                     "to": to_emails,
                     "action_msg": action_msg,
                     "msg": msg,
                     "order": invoice.client_order,
                     "send_type": send_type,
                     "invoices": invoices}
    agent_invoice_apply_signal.send(
        current_app._get_current_object(), apply_context=apply_context)
    flash(u'[%s 打款发票开具申请] 已发送邮件给 %s ' %
          (invoice.client_order, ', '.join(to_emails)), 'info')
    return redirect(url_for("searchAd_saler_client_order_agent_invoice.index", order_id=invoice.client_order.id))
Beispiel #17
0
def new_invoice_pay(invoice_id):
    money = float(request.values.get('money', 0))
    pay_time = request.values.get('pay_time', '')
    detail = request.values.get('detail', '')
    mi = searchAdAgentInvoice.get(invoice_id)
    # if mi.pay_invoice_money + money > mi.money:
    #     flash(u'付款金额大于发票金额,请重新填写!', 'danger')
    #     return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
    pay = searchAdAgentInvoicePay.add(money=money,
                              agent_invoice=mi,
                              pay_time=pay_time,
                              pay_status=0,
                              detail=detail)
    pay.save()
    flash(u'添加已打款信息成功!', 'success')
    mi.client_order.add_comment(g.user, u"添加已打款信息  发票号:%s  打款金额:%s元  打款时间:%s" % (
        mi.invoice_num, str(money), pay_time), msg_channel=5)
    return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
Beispiel #18
0
def update_invoice(invoice_id, redirect_endpoint='searchAd_saler_client_order_agent_invoice.index'):
    invoice = searchAdAgentInvoice.get(invoice_id)
    if not invoice:
        abort(404)
    form = AgentInvoiceForm(request.form)
    form.client_order.choices = [
        (invoice.client_order.id, invoice.client_order.name)]
    form.agent.choices = [(invoice.agent.id, invoice.agent.name)]
    form.bool_invoice.bool_invoice = AGENT_INVOICE_BOOL_INVOICE_CN.items()
    # order = invoice.client_order
    # if order.agent_money < order.agents_invoice_sum + float(form.money.data):
    #    flash(u'新建打款发票失败,发票超过代理总金额!', 'danger')
    #    return redirect(url_for(redirect_endpoint, order_id=order.id))
    if request.method == 'POST':
        if not form.invoice_num.data:
            flash(u"修改打款发票失败,发票号不能为空", 'danger')
        elif not form.money.data:
            flash(u"修改打款发票失败,发票金额不能为空", 'danger')
        else:
            invoice.company = form.company.data,
            invoice.tax_id = form.tax_id.data,
            invoice.address = form.address.data,
            invoice.phone = form.phone.data,
            invoice.bank_id = form.bank_id.data,
            invoice.bank = form.bank.data,
            invoice.detail = form.detail.data,
            invoice.money = form.money.data,
            invoice.pay_money = form.money.data,
            invoice.invoice_type = form.invoice_type.data,
            invoice.invoice_num = form.invoice_num.data,
            invoice.creator = g.user
            invoice.create_time = datetime.date.today()
            invoice.add_time = form.add_time.data
            invoice.bool_invoice = form.bool_invoice.data
            invoice.save()
            flash(u'修改打款发票(%s)成功!' % form.company.data, 'success')
            invoice.client_order.add_comment(g.user, u"修改打款发票信息,%s" % (
                u'打款发票内容: %s; 发票金额: %s元; 发票号: %s' %
                (invoice.detail, str(invoice.money), invoice.invoice_num)), msg_channel=5)
    else:
        for k in form.errors:
            flash(u"修改打款发票失败,%s" % (form.errors[k][0]), 'danger')
    return redirect(url_for(redirect_endpoint, order_id=invoice.client_order.id))
Beispiel #19
0
def new_invoice_pay(invoice_id):
    money = float(request.values.get('money', 0))
    pay_time = request.values.get('pay_time', '')
    detail = request.values.get('detail', '')
    mi = searchAdAgentInvoice.get(invoice_id)
    # if mi.pay_invoice_money + money > mi.money:
    #     flash(u'付款金额大于发票金额,请重新填写!', 'danger')
    #     return redirect(url_for('searchAd_saler_client_order_agent_invoice.invoice', invoice_id=invoice_id))
    pay = searchAdAgentInvoicePay.add(money=money,
                                      agent_invoice=mi,
                                      pay_time=pay_time,
                                      pay_status=0,
                                      detail=detail)
    pay.save()
    flash(u'添加已打款信息成功!', 'success')
    mi.client_order.add_comment(g.user,
                                u"添加已打款信息  发票号:%s  打款金额:%s元  打款时间:%s" %
                                (mi.invoice_num, str(money), pay_time),
                                msg_channel=5)
    return redirect(
        url_for('searchAd_saler_client_order_agent_invoice.invoice',
                invoice_id=invoice_id))
Beispiel #20
0
def apply_pay(invoice_id):
    agent_invoice = searchAdAgentInvoice.get(invoice_id)
    if not agent_invoice:
        abort(404)
    invoice_pay_ids = request.values.getlist('invoices')
    invoice_pays = searchAdAgentInvoicePay.gets(invoice_pay_ids)
    if not invoice_pays:
        abort(403)
    emails = request.values.getlist('email')
    msg = request.values.get('msg', '')
    action = int(request.values.get('action', 0))
    to_users = agent_invoice.client_order.direct_sales + agent_invoice.client_order.agent_sales + \
        [agent_invoice.client_order.creator, g.user] + \
        agent_invoice.client_order.leaders
    to_emails = list(set(emails + [x.email for x in to_users]))
    if action == 2:
        action_msg = u'代理打款申请'
        for invoice in invoice_pays:
            invoice.pay_status = AGENT_INVOICE_STATUS_APPLY
            invoice.save()
            flash(
                u'[%s代理申请打款,打款金额: %s, 发票号: %s]  %s ' %
                (invoice.agent_invoice.company, invoice.money,
                 invoice.agent_invoice.invoice_num, invoice.detail), 'success')

            invoice.agent_invoice.client_order.add_comment(
                g.user,
                u"%s,%s" %
                (action_msg, u'[%s代理申请打款,打款金额: %s, 发票号: %s]  %s ' %
                 (invoice.agent_invoice.company, invoice.money,
                  invoice.agent_invoice.invoice_num, invoice.detail)),
                msg_channel=5)
        send_type = "saler"
    elif action == 3:
        action_msg = u'同意代理订单打款申请,请财务打款'
        to_emails += [k.email for k in User.finances()]
        for invoice in invoice_pays:
            invoice.pay_status = AGENT_INVOICE_STATUS_AGREE
            invoice.save()
            flash(
                u'[同意%s代理订单打款申请,打款金额: %s, 发票号: %s]  %s ' %
                (invoice.agent_invoice.company, invoice.money,
                 invoice.agent_invoice.invoice_num, invoice.detail), 'success')

            invoice.agent_invoice.client_order.add_comment(
                g.user,
                u"%s,%s" %
                (action_msg, u'[%s代理同意打款,打款金额: %s, 发票号: %s]  %s ' %
                 (invoice.agent_invoice.company, invoice.money,
                  invoice.agent_invoice.invoice_num, invoice.detail)),
                msg_channel=5)
        send_type = "finance"
    else:
        action_msg = u'消息提醒'

    apply_context = {
        "to_other": emails,
        "title": action_msg,
        "to": to_emails,
        "action_msg": action_msg,
        "msg": msg,
        "invoice": agent_invoice,
        "send_type": send_type,
        "invoice_pays": invoice_pays
    }
    agent_invoice_apply_signal.send(current_app._get_current_object(),
                                    apply_context=apply_context)
    flash(
        u'[%s 打款发票开具申请] 已发送邮件给 %s ' %
        (agent_invoice.client_order, ', '.join(to_emails)), 'info')
    return redirect(
        url_for('searchAd_saler_client_order_agent_invoice.invoice',
                invoice_id=invoice_id))