<?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.fjhx.jxst.mapper.sales.SalesContractMapper">
    <select id="getPage" resultType="com.fjhx.jxst.entity.sales.vo.SalesContractVo">
        select
            sc.id,
            sc.code,
            sc.customer_id,
            sc.delivery_date,
            sc.pay_method,
            sc.freight_payer,
            sc.remark,
            sc.create_user,
            sc.create_time,
            sc.update_user,
            sc.update_time,
            sc.contract_amount,
            sc.flow_id,
            sc.is_settled,
            ( SELECT IFNULL( SUM( cc.contract_money ), 0 ) FROM bytesailing_sale.claim_contract cc WHERE cc.contract_id = sc.id ) AS claimAmount
        from sales_contract sc
            ${ew.customSqlSegment}
    </select>
    <select id="getProfitClearingPage" resultType="com.fjhx.jxst.entity.statement.bo.SettlementBo">
        <include refid="getProfitClearing"/>
    </select>
    <select id="getProfitSettlementHead" resultType="com.fjhx.jxst.entity.statement.bo.SettlementBo">
        SELECT
            IFNULL( sum( contractClaimAmount ), 0 ) AS contractClaimAmount,
            IFNULL( sum( materialFee ), 0 ) AS materialFee,
            IFNULL( sum( afterSalesFee ), 0 ) AS afterSalesFee,
            IFNULL( sum( trailerFee ), 0 ) AS trailerFee,
            IFNULL( sum( inspectionRedPackFee ), 0 ) AS inspectionRedPackFee,
            IFNULL( sum( commissionFee ), 0 ) AS commissionFee,
            IFNULL( sum( otherFee ), 0 ) AS otherFee,
            IFNULL( sum( contractAmount ), 0 ) AS contractAmount,
            IFNULL( sum( requestFundsAmount ), 0 ) AS requestFundsAmount
        FROM
            (
                <include refid="getProfitClearing"/>
            ) t1
    </select>

    <sql id="getProfitClearing">
        SELECT
            sc.id,
            sc.`code` AS salesContractCode,
            sc.customer_id,
            cu.`name` AS customerName,
            ( SELECT IFNULL( sum( cc.contract_money ), 0 ) FROM bytesailing_sale.claim_contract cc WHERE cc.del_flag=0 and cc.contract_id = sc.id ) AS contractClaimAmount,
            sc.contract_amount AS contractAmount,
            (
                SELECT
                    IFNULL(sum( IF ( sj.op_type = 2,( sjd.price * sjd.quantity ),-(sjd.price * sjd.quantity )) ),0)
                FROM
                    bytesailing_wms.stock_journal_details sjd
                        LEFT JOIN bytesailing_wms.stock_journal sj ON sjd.stock_journal_id = sj.id
                        JOIN bytesailing_mes.work_order wo ON sj.work_order_id = wo.id
                        JOIN sales_contract_details scd ON wo.contract_details_id = scd.id
                WHERE
                    sjd.del_flag=0
                    and scd.sales_contract_id = sc.id
            ) AS materialFee,
            (
                SELECT
                    IFNULL( sum( asr.amount ), 0 )
                FROM
                    after_sales_record asr
                        JOIN bytesailing_mes.production_task_detail ptd ON asr.product_sn = ptd.product_sn
                        JOIN bytesailing_mes.production_task pt ON ptd.production_task_id = pt.id
                        JOIN bytesailing_mes.production_plan pp ON pt.production_plan_id = pp.id
                        JOIN bytesailing_mes.work_order wo ON pp.work_order_id = wo.id
                        JOIN sales_contract_details scd ON wo.contract_details_id = scd.id
                WHERE
                    scd.sales_contract_id = sc.id
            ) AS afterSalesFee,
            sc.trailer_fee AS trailerFee,
            sc.inspection_red_pack_fee AS inspectionRedPackFee,
            sc.commission_fee AS commissionFee,
            sc.other_fee AS otherFee,
            sc.is_settled,
            (SELECT IFNULL( sum( arfd.amount ), 0 ) FROM bytesailing_account.account_request_funds_detail arfd WHERE arfd.contract_id = sc.id) as requestFundsAmount
        FROM
            sales_contract sc
            LEFT JOIN bytesailing_customer.customer cu ON sc.customer_id = cu.id
            ${ew.customSqlSegment}
    </sql>

</mapper>
