<?php

namespace app\api\controller;
use think\facade\Lang;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 发票控制器
 */
class Memberinvoice extends MobileMember
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberinvoice.lang.php');
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_list 发票信息列表
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} page 页码
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.invoice_list  发票列表
     * @apiSuccess {String} result.invoice_list.invoice_code  纳税人识别号[普通发票]
     * @apiSuccess {String} result.invoice_list.invoice_company  单位名称
     * @apiSuccess {String} result.invoice_list.invoice_company_code  纳税人识别号
     * @apiSuccess {String} result.invoice_list.invoice_content  发票内容[普通发票]
     * @apiSuccess {String} result.invoice_list.invoice_goto_addr  送票地址
     * @apiSuccess {Int} result.invoice_list.invoice_id  发票信息ID
     * @apiSuccess {String} result.invoice_list.invoice_rec_mobphone  收票人手机号
     * @apiSuccess {String} result.invoice_list.invoice_rec_name  收票人姓名
     * @apiSuccess {String} result.invoice_list.invoice_rec_province  收票人省份
     * @apiSuccess {String} result.invoice_list.invoice_reg_addr  注册地址
     * @apiSuccess {String} result.invoice_list.invoice_reg_baccount  银行帐户
     * @apiSuccess {String} result.invoice_list.invoice_reg_bname  开户银行
     * @apiSuccess {String} result.invoice_list.invoice_reg_phone  注册电话
     * @apiSuccess {Int} result.invoice_list.invoice_state  发票类型 1:普通发票 2:增值税发票
     * @apiSuccess {String} result.invoice_list.invoice_title  发票抬头[普通发票]
     * @apiSuccess {Int} result.invoice_list.member_id 用户ID
     */
    public function invoice_list()
    {
        $invoice_model = model('invoice');
        $condition = array();
        $condition[] = array('member_id','=',$this->member_info['member_id']);
        $invoice_list = $invoice_model->getInvoiceList($condition, 10, '*');
        ds_json_encode(10000, '',array('invoice_list' => $invoice_list));
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_info 发票信息详情
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String} result.invoice_code  纳税人识别号[普通发票]
     * @apiSuccess {String} result.invoice_company  单位名称
     * @apiSuccess {String} result.invoice_company_code  纳税人识别号
     * @apiSuccess {String} result.invoice_content  发票内容[普通发票]
     * @apiSuccess {String} result.invoice_goto_addr  送票地址
     * @apiSuccess {Int} result.invoice_id  发票信息ID
     * @apiSuccess {String} result.invoice_rec_mobphone  收票人手机号
     * @apiSuccess {String} result.invoice_rec_name  收票人姓名
     * @apiSuccess {String} result.invoice_rec_province  收票人省份
     * @apiSuccess {String} result.invoice_reg_addr  注册地址
     * @apiSuccess {String} result.invoice_reg_baccount  银行帐户
     * @apiSuccess {String} result.invoice_reg_bname  开户银行
     * @apiSuccess {String} result.invoice_reg_phone  注册电话
     * @apiSuccess {Int} result.invoice_state  发票类型 1:普通发票 2:增值税发票
     * @apiSuccess {String} result.invoice_title  发票抬头[普通发票]
     * @apiSuccess {Int} result.member_id 用户ID
     */
    public function invoice_info() {
        $invoice_id = intval(input('post.invoice_id'));
        if ($invoice_id <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }
        $invoice_model = model('invoice');
        $result = $invoice_model->getInvoiceInfo(array('invoice_id' => $invoice_id, 'member_id' => $this->member_info['member_id']));
        ds_json_encode(10000, '', $result);
    }
    /**
     * @api {POST} api/Memberinvoice/invoice_del 发票信息删除
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function invoice_del()
    {
        $invoice_id = intval(input('post.invoice_id'));
        if ($invoice_id <= 0) {
            ds_json_encode(10001,lang('param_error'));
        }

        $invoice_model = model('invoice');

        $result = $invoice_model->delInvoice(array('invoice_id' => $invoice_id, 'member_id' => $this->member_info['member_id']));
        if ($result) {
            ds_json_encode(10000, '',1);
        }
        else {
            ds_json_encode(10001,lang('ds_common_op_fail'));
        }
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_add 发票信息添加
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {String} invoice_state 发票类型
     * @apiParam {String} invoice_title 发票抬头[普通发票]
     * @apiParam {String} invoice_code 纳税人识别号[普通发票]
     * @apiParam {String} invoice_content 发票内容[普通发票]
     * @apiParam {String} invoice_company 单位名称
     * @apiParam {String} invoice_company_code 纳税人识别号
     * @apiParam {String} invoice_reg_addr 注册地址
     * @apiParam {String} invoice_reg_phone 注册电话
     * @apiParam {String} invoice_reg_bname 开户银行
     * @apiParam {String} invoice_reg_baccount 银行帐户
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Int} result.invoice_id  发票ID
     */
    public function invoice_add()
    {
        $invoice_model = model('invoice');

        $data = array();
        $data['invoice_state'] = input('post.invoice_state');
        $data['invoice_title'] = input('post.invoice_title');
        $data['invoice_content'] = input('post.invoice_content');
        $data['invoice_code'] = input('post.invoice_code');
        $data['invoice_company'] = input('post.invoice_company');
        $data['invoice_company_code'] = input('post.invoice_company_code');
        $data['invoice_reg_addr'] = input('post.invoice_reg_addr');
        $data['invoice_reg_phone'] = input('post.invoice_reg_phone');
        $data['invoice_reg_bname'] = input('post.invoice_reg_bname');
        $data['invoice_reg_baccount'] = input('post.invoice_reg_baccount');
        $data['member_id'] = $this->member_info['member_id'];
        $result = $invoice_model->addInvoice($data);
        if ($result) {
            ds_json_encode(10000, '',array('invoice_id' => $result));
        }
        else {
            ds_json_encode(10001,lang('ds_common_op_fail'));
        }
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_edit 发票信息编辑
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     * @apiParam {String} invoice_state 发票类型
     * @apiParam {String} invoice_title 发票抬头[普通发票]
     * @apiParam {String} invoice_code 纳税人识别号[普通发票]
     * @apiParam {String} invoice_content 发票内容[普通发票]
     * @apiParam {String} invoice_company 单位名称
     * @apiParam {String} invoice_company_code 纳税人识别号
     * @apiParam {String} invoice_reg_addr 注册地址
     * @apiParam {String} invoice_reg_phone 注册电话
     * @apiParam {String} invoice_reg_bname 开户银行
     * @apiParam {String} invoice_reg_baccount 银行帐户
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function invoice_edit() {
        $invoice_id = intval(input('post.invoice_id'));
        $invoice_model = model('invoice');
        $data['invoice_state'] = input('post.invoice_state');
        $data['invoice_title'] = input('post.invoice_title');
        $data['invoice_content'] = input('post.invoice_content');
        $data['invoice_code'] = input('post.invoice_code');
        $data['invoice_company'] = input('post.invoice_company');
        $data['invoice_company_code'] = input('post.invoice_company_code');
        $data['invoice_reg_addr'] = input('post.invoice_reg_addr');
        $data['invoice_reg_phone'] = input('post.invoice_reg_phone');
        $data['invoice_reg_bname'] = input('post.invoice_reg_bname');
        $data['invoice_reg_baccount'] = input('post.invoice_reg_baccount');
        $result = $invoice_model->editInvoice($data, array('invoice_id' => $invoice_id,'member_id'=>$this->member_info['member_id']));
        if ($result) {
            ds_json_encode(10000,'');
        }
        else {
            ds_json_encode(10001,lang('ds_common_op_fail'));
        }
    }
    /**
     * 发票内容列表
     */
    public function invoice_content_list()
    {
        $invoice_content_list = array(
            lang('detailed'), lang('alcohol'), lang('food'), lang('drinks'), lang('toys'), lang('daily_necessities'), lang('decoration_materials'), lang('cosmetics'), lang('office_supplies'), lang('student_supplies'), lang('furnishings'), lang('ornaments'), lang('clothing'), lang('luggage'), lang('boutique'), lang('electrical_appliances'),
            lang('building_supplies'), lang('consumables'), lang('computer_parts')
        );
        ds_json_encode(10000, '',array('invoice_content_list' => $invoice_content_list));
    }
}