import config
from datetime import datetime

class ReportHelper:

    @staticmethod
    def search_contractor(subcontractor_name, pmc_no, state, district, block, village, year_from, year_to):

        connection = config.get_db_connection()
        cursor = connection.cursor(dictionary=True)

        cursor.callproc("search_contractor_info", [
            subcontractor_name or None,
            pmc_no or None,
            state or None,
            district or None,
            block or None,
            village or None,
            year_from or None,
            year_to or None
        ])

        data = []
        for result in cursor.stored_results():
            data = result.fetchall()

        cursor.close()
        connection.close()

        return data

    
    @staticmethod
    def get_contractor_report(contractor_id):

            connection = config.get_db_connection()
            cursor = connection.cursor(dictionary=True, buffered=True)


            try:
                # Contractor Info
                cursor.callproc('GetContractorInfo', [contractor_id])
                for result in cursor.stored_results():
                    contInfo = result.fetchone()

                # Hold Types
                cursor.callproc('GetContractorHoldTypes', [contractor_id])
                for result in cursor.stored_results():
                    hold_types = result.fetchall()

                # Invoices
                cursor.callproc('GetContractorInvoices', [contractor_id])
                for result in cursor.stored_results():
                    invoices = result.fetchall()

                # GST Release
                cursor.callproc('GetGSTRelease', [contractor_id])
                for result in cursor.stored_results():
                    gst_rel = result.fetchall()

                # Hold Release
                cursor.callproc('GetHoldRelease', [contractor_id])
                for result in cursor.stored_results():
                    hold_release = result.fetchall()

                # Credit Note
                cursor.callproc('GetCreditNote', [contractor_id])
                for result in cursor.stored_results():
                    credit_note = result.fetchall()

                # Payments
                cursor.callproc('GetPayments', [contractor_id])
                for result in cursor.stored_results():
                    payments = result.fetchall()


                # Totals
                total = {
                    "sum_invo_basic_amt": float(sum(row['Basic_Amount'] or 0 for row in invoices)),
                    "sum_invo_debit_amt": float(sum(row['Debit_Amount'] or 0 for row in invoices)),
                    "sum_invo_after_debit_amt": float(sum(row['After_Debit_Amount'] or 0 for row in invoices)),
                    "sum_invo_amt": float(sum(row['Amount'] or 0 for row in invoices)),
                    "sum_invo_gst_amt": float(sum(row['GST_Amount'] or 0 for row in invoices)),
                    "sum_invo_tds_amt": float(sum(row['TDS_Amount'] or 0 for row in invoices)),
                    "sum_invo_ds_amt": float(sum(row['SD_Amount'] or 0 for row in invoices)),
                    "sum_invo_on_commission": float(sum(row['On_Commission'] or 0 for row in invoices)),
                    "sum_invo_hydro_test": float(sum(row['Hydro_Testing'] or 0 for row in invoices)),
                    "sum_invo_gst_sd_amt": float(sum(row['GST_SD_Amount'] or 0 for row in invoices)),
                    "sum_invo_final_amt": float(sum(row['Final_Amount'] or 0 for row in invoices)),
                    "sum_invo_hold_amt": float(sum(row['hold_amount'] or 0 for row in invoices)),

                    "sum_gst_basic_amt": float(sum(row['basic_amount'] or 0 for row in gst_rel)),
                    "sum_gst_final_amt": float(sum(row['final_amount'] or 0 for row in gst_rel)),

                    "sum_pay_payment_amt": float(sum(row['Payment_Amount'] or 0 for row in payments)),
                    "sum_pay_tds_payment_amt": float(sum(row['TDS_Payment_Amount'] or 0 for row in payments)),
                    "sum_pay_total_amt": float(sum(row['Total_amount'] or 0 for row in payments))
                }

                current_date = datetime.now().strftime('%Y-%m-%d')

            finally:
                cursor.close()
                connection.close()

            return {
                "contInfo": contInfo,
                "invoices": invoices,
                "hold_types": hold_types,
                "gst_rel": gst_rel,
                "payments": payments,
                "credit_note": credit_note,
                "hold_release": hold_release,
                "total": total,
                "current_date": current_date
            }
