SELECT
    *
FROM
    (
        select
            'book_gr' bill,
            gr_no bno,
            '' dr_ac,
            '' dr_amt,
            igst_ac cr_ac,
            service_tax cr_amt
        from
            book_gr
        where
            gr_date between '2023-04-01'
            and '2024-03-31'
            and pay_mode in ('Paid', 'To Pay')
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and service_tax <> 0
            and gr_type != 'Crossing'
            AND `status` != 'done'
            AND `bill_status` != 'Unbilled'
        UNION
        ALL
        select
            'book_gr' bill,
            gr_no bno,
            '' dr_ac,
            '' dr_amt,
            cgst_ac cr_ac,
            cgst cr_amt
        from
            book_gr
        where
            gr_date between '2023-04-01'
            and '2024-03-31'
            and pay_mode in ('Paid', 'To Pay')
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and cgst <> 0
            and gr_type != 'Crossing'
            AND `status` != 'done'
            AND `bill_status` != 'Unbilled'
        UNION
        ALL
        select
            'book_gr' bill,
            gr_no bno,
            '' dr_ac,
            '' dr_amt,
            sgst_ac cr_ac,
            sgst cr_amt
        from
            book_gr
        where
            gr_date between '2023-04-01'
            and '2024-03-31'
            and pay_mode in ('Paid', 'To Pay')
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and sgst <> 0
            and gr_type != 'Crossing'
            AND `status` != 'done'
            AND `bill_status` != 'Unbilled'
        UNION
        ALL
        SELECT
            'book_gr' bill,
            gr_no bno,
            on_ac AS dr_ac,
            gr_tot as dr_amt,
            on_ac_cr cr_ac,
(gr_tot -(service_tax + cgst + sgst)) cr_amt
        FROM
            book_gr
        where
            gr_date between '2023-04-01'
            and '2024-03-31'
            and pay_mode in ('Paid', 'To Pay')
            and gr_tot <> 0
            and gr_type != 'Crossing'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and gr_tot <> 0
            AND `status` != 'done'
            AND `bill_status` != 'Unbilled'
        UNION
        ALL
        SELECT
            'book_gr' bill,
            gr_no bno,
            on_ac AS dr_ac,
            gr_tot as dr_amt,
            on_ac cr_ac,
            pf cr_amt
        FROM
            book_gr
        where
            gr_date between '2023-04-01'
            and '2024-03-31'
            and pay_mode != 'Paid'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and pf > 0
            AND `status` != 'done'
            AND `bill_status` != 'Unbilled'
        UNION
        ALL
        SELECT
            'bill' bill,
            bill_no bno,
            '' dr_ac,
            '' dr_amt,
            igst_ac AS cr_ac,
            st AS cr_amt
        FROM
            bill
        where
            bill_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and cancel_status = 0
        UNION
        ALL
        SELECT
            'bill' bill,
            bill_no bno,
            '' dr_ac,
            '' dr_amt,
            cgst_ac AS cr_ac,
            cgst AS cr_amt
        FROM
            bill
        where
            bill_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and cancel_status = 0
        UNION
        ALL
        SELECT
            'bill' bill,
            bill_no bno,
            '' dr_ac,
            '' dr_amt,
            sgst_ac AS cr_ac,
            sgst AS cr_amt
        FROM
            bill
        where
            bill_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and cancel_status = 0
        UNION
        ALL
        SELECT
            'bill' bill,
            bill_no bno,
            party AS dr_ac,
            net_amt AS dr_amt,
            cr_party AS cr_ac,
(net_amt -(st + cgst + sgst)) AS cr_amt
        FROM
            bill
        where
            bill_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and cancel_status = 0
        UNION
        ALL
        SELECT
            'pod' bill,
            pod.pod_no bno,
            `pon_ac` AS dr_ac,
            net_bal AS dr_amt,
            pon_ac_cr cr_ac,
            net_bal AS cr_amt
        FROM
            pod
            INNER JOIN `pod_desc` ON `pod_desc`.`pod_no` = `pod`.`pod_no`
        where
            pod.pod_date between '2023-04-01'
            and '2024-03-31'
            and `pod`.pay_mode in('Paid', 'To Pay')
            and `pod_desc`.company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and `pod`.company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            AND `pod_desc`.`gr_no` NOT IN (
                SELECT
                    `gr_no`
                FROM
                    `bill_desc`
                WHERE
                    `flag` = 'gr_wise_bill'
                    and `company_name` = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            )
        UNION
        ALL
        SELECT
            'pod' bill,
            pod.pod_no bno,
            rcpt_in AS dr_ac,
            net_bal AS dr_amt,
            `pon_ac` cr_ac,
            net_bal AS cr_amt
        FROM
            pod
            INNER JOIN `pod_desc` ON `pod_desc`.`pod_no` = `pod`.`pod_no`
        where
            pod.pod_date between '2023-04-01'
            and '2024-03-31'
            and `pod`.pay_mode in('Paid', 'To Pay')
            and `pod`.company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            AND `pod`.`pay_status` = 'done'
            AND `pod_desc`.`gr_no` NOT IN (
                SELECT
                    `gr_no`
                FROM
                    `bill_desc`
                WHERE
                    `flag` = 'gr_wise_bill'
                    and `company_name` = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            )
        UNION
        ALL
        select
            'challan1' bill,
            challan_no bno,
            rcvr_brnch dr_ac,
            to_pay dr_amt,
            dr_on_ac cr_ac,
            to_pay cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and dlvr_ac = 0
        UNION
        ALL
        select
            'challan2' bill,
            challan_no bno,
            dlvr_ac dr_ac,
            to_pay dr_amt,
            dr_on_ac cr_ac,
            to_pay cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and dlvr_ac <> 0
        UNION
        ALL
        select
            'challan3' bill,
            challan_no bno,
            kaat_ac dr_ac,
            kaat dr_amt,
            rcvr_brnch cr_ac,
            kaat cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and dlvr_ac = 0
        UNION
        ALL
        select
            'challan5' bill,
            challan_no bno,
            dc_ac dr_ac,
            ch_dc dr_amt,
            rcvr_brnch cr_ac,
            ch_dc cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and dlvr_ac = 0
        UNION
        ALL
        select
            'challan5' bill,
            challan_no bno,
            dd_ac dr_ac,
            door_del dr_amt,
            rcvr_brnch cr_ac,
            door_del cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and door_del <> 0
            and dlvr_ac = 0
        UNION
        ALL
        select
            'challan6' bill,
            challan_no bno,
            rcvr_brnch dr_ac,
            drvr_adv dr_amt,
            cr_on_ac cr_ac,
            drvr_adv cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and drvr_adv <> 0
            and drvdc = 'Cr'
        UNION
        ALL
        select
            'challan7' bill,
            challan_no bno,
            cr_on_ac dr_ac,
            drvr_adv dr_amt,
            rcvr_brnch cr_ac,
            drvr_adv cr_amt
        from
            challan
        where
            challan_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and drvr_adv <> 0
            and drvdc = 'Dr'
        UNION
        ALL
        SELECT
            'lorry_hire' bill,
            lorry_hire_no bno,
            dr_on_ac AS dr_ac,
            net_amt AS dr_amt,
            cr_on_ac AS cr_ac,
            net_amt AS cr_amt
        FROM
            lorry_hire
        where
            hire_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        select
            'Diesel' bill,
            receipt_no bno,
            dr_on_ac dr_ac,
            amount dr_amt,
            cr_on_ac cr_ac,
            amount cr_amt
        from
            diesel_receipt
        where
            entry_date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and amount <> 0
        UNION
        ALL
        select
            'driver_settel' bill,
            settel_no bno,
            cr_ac dr_ac,
            return_freight dr_amt,
            cr_incm cr_ac,
            return_freight cr_amt
        from
            driver_settel
        where
            date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and return_freight <> 0
        UNION
        ALL
        select
            'driver_settel' bill,
            settel_no bno,
            dr_ac,
            trp_expnc dr_amt,
            cr_ac,
            trp_expnc cr_amt
        from
            driver_settel
        where
            date between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
            and trp_expnc <> 0
        UNION
        ALL
        SELECT
            'Receipt' bill,
            voucher_no bno,
            paid_in AS dr_ac,
            cr_amount AS dr_amt,
            paid_to cr_ac,
            amount AS cr_amt
        FROM
            voucher
        WHERE
            v_type = 'Receipt'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Receipt' bill,
            voucher_no bno,
            tdsac AS dr_ac,
            tds AS dr_amt,
            '',
            0
        FROM
            voucher
        WHERE
            v_type = 'Receipt'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Receipt' bill,
            voucher_no bno,
            deductionac AS dr_ac,
            deduction AS dr_amt,
            '',
            0
        FROM
            voucher
        WHERE
            v_type = 'Receipt'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Payment' bill,
            voucher_no bno,
            paid_to AS dr_ac,
            amount AS dr_amt,
            paid_in cr_ac,
            cr_amount AS cr_amt
        FROM
            voucher
        WHERE
            v_type = 'Payment'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Payment' bill,
            voucher_no bno,
            '',
            0,
            tdsac AS cr_ac,
            tds AS cr_amt
        FROM
            voucher
        WHERE
            v_type = 'Payment'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Payment' bill,
            voucher_no bno,
            '',
            0,
            deductionac AS cr_ac,
            deduction AS cr_amt
        FROM
            voucher
        WHERE
            v_type = 'Payment'
            and voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
        UNION
        ALL
        SELECT
            'Contra' bill,
            voucher_no bno,
            from_ac dr_ac,
            dr_amt,
            to_ac cr_ac,
            cr_amt
        FROM
            voucher_contra
        where
            voucher_dt between '2023-04-01'
            and '2024-03-31'
            and company_name = 'NEW NORTHWAYS TRANSPORT ORGANISATION'
    ) AS `CAT`
    where
        cr_ac = '852'
        or dr_ac = '852'