<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sunxung.factoring.mapper.ledger.LedgerManagementMapper">

	<select id="findAccountsReceivableIdsBySupplierId" parameterType="java.lang.Long" resultType="java.lang.Long">
		SELECT DISTINCT
			t7.sop_accounts_receivable_id
		FROM
			c_invoice_basic_info t1,
			c_invoice_goods_info t2,
			c_settlement_basic_info t4,
			c_settlement_goods_info t5,
			c_acceptance_basic_info t6,
		    c_acceptance_goods_info t11,
			c_financing_basic_info t7,
			c_financing_goods_info t8,
			c_enterprise t10
		WHERE
			t1.id = t2.c_invoice_basic_info_id
		  AND t2.c_settlement_basic_info_id = t4.id
		  AND t4.id = t5.c_settlement_basic_info_id
		  AND t5.c_acceptance_basic_info_id = t6.id
		  AND t6.id = t11.c_acceptance_basic_info_id
		  AND t7.id = t8.c_financing_basic_info_id
		  AND t8.acceptance_basic_info_id = t6.id
		  AND t8.acceptance_goods_info_id = t11.id
		  AND t6.c_supplier_info_id = t10.id
		  AND t10.id = #{enterpriseId}
		  AND t1.invoice_status = ( SELECT t3.id FROM t_dictionary t3 WHERE t3.CODE = 'invoice_pass' )
		  AND t6.is_settlement = 1
		  AND t11.is_financing = 1
		  AND t7.financing_status = 'project_init_completed'
	</select>

    <select id="findAccountsReceivableIdsByEnterpriseId" parameterType="java.lang.Long" resultType="java.lang.Long">
		SELECT DISTINCT
			t7.sop_accounts_receivable_id
		FROM
			c_invoice_basic_info t1,
			c_invoice_goods_info t2,
			c_settlement_basic_info t4,
			c_settlement_goods_info t5,
			c_acceptance_basic_info t6,
			c_acceptance_goods_info t11,
			c_financing_basic_info t7,
			c_financing_goods_info t8,
			c_enterprise t10
		WHERE
			t1.id = t2.c_invoice_basic_info_id
		  AND t2.c_settlement_basic_info_id = t4.id
		  AND t4.id = t5.c_settlement_basic_info_id
		  AND t5.c_acceptance_basic_info_id = t6.id
		  AND t6.id = t11.c_acceptance_basic_info_id
		  AND t7.id = t8.c_financing_basic_info_id
		  AND t8.acceptance_basic_info_id = t6.id
		  AND t8.acceptance_goods_info_id = t11.id
		  AND t6.c_project_core_enterprise_id = t10.id
		  AND t10.id = #{enterpriseId}
		  AND t1.invoice_status = ( SELECT t3.id FROM t_dictionary t3 WHERE t3.CODE = 'invoice_pass' )
		  AND t6.is_settlement = 1
		  AND t11.is_financing = 1
		  AND t7.financing_status = 'project_init_completed'
    </select>

    <select id="findAcceptanceBySupplierId" parameterType="java.lang.Long" resultType="com.sunxung.factoring.entity.ledger.LedgerFundGoodsAssign">
		SELECT DISTINCT
			t4.id,
		    t1.business_number,
		    t4.settlement_number,
		    t7.acceptance_goods_number,
		    t4.settlement_date loan_date,
		    t6.c_supplier_info_id supplier_id,
		    t9.corporate_name supplier_name,
		    t10.name acceptance_status,
		    t11.name settlement_status,
		    t12.name invoice_status
		FROM
			c_invoice_basic_info t1,
			c_invoice_goods_info t2,
			c_settlement_basic_info t4,
			c_settlement_goods_info t5,
			c_acceptance_basic_info t6,
		  	c_acceptance_goods_info t7,
			c_enterprise t9,
			t_dictionary t10,
			t_dictionary t11,
			t_dictionary t12
		WHERE
			t1.id = t2.c_invoice_basic_info_id
		  AND t2.c_settlement_basic_info_id = t4.id
		  AND t4.id = t5.c_settlement_basic_info_id
		  AND t5.c_acceptance_goods_info_id = t7.id
		  AND t6.id = t7.c_acceptance_basic_info_id
		  AND t9.id = t6.c_supplier_info_id
		  AND t6.acceptance_status = t10.code
		  AND t4.settlement_status = t11.code
		  AND t1.invoice_status = t12.id
		  AND t9.id = #{enterpriseId}
		  AND t1.invoice_status = ( SELECT t3.id FROM t_dictionary t3 WHERE t3.CODE = 'invoice_pass' )
		  AND t6.is_settlement = 1
		  AND t7.is_financing = 0
	</select>

	<select id="findAcceptanceByEnterpriseId" parameterType="java.lang.Long" resultType="com.sunxung.factoring.entity.ledger.LedgerFundGoodsAssign">
		SELECT DISTINCT
			t4.id,
			t6.business_number,
			t4.settlement_number,
			t7.acceptance_goods_number acceptance_number,
			t4.settlement_date loan_date,
			t6.c_supplier_info_id supplier_id,
			t9.corporate_name supplier_name,
			t6.acceptance_status,
			t4.settlement_status,
			t1.invoice_status
		FROM
			c_invoice_basic_info t1,
			c_invoice_goods_info t2,
			c_settlement_basic_info t4,
			c_settlement_goods_info t5,
			c_acceptance_basic_info t6,
			c_acceptance_goods_info t7,
			c_enterprise t9
		WHERE
			t1.id = t2.c_invoice_basic_info_id
		  AND t2.c_settlement_basic_info_id = t4.id
		  AND t4.id = t5.c_settlement_basic_info_id
		  AND t5.c_acceptance_goods_info_id = t7.id
		  AND t6.id = t7.c_acceptance_basic_info_id
		  AND t9.id = t6.c_project_core_enterprise_id
		  AND t9.id = #{enterpriseId}
		  AND t1.invoice_status = ( SELECT t3.id FROM t_dictionary t3 WHERE t3.CODE = 'invoice_pass' )
		  AND t6.is_settlement = 1
		  AND t7.is_financing = 0
	</select>
</mapper>
