CREATE PROCEDURE `new_purchase`
    (IN `in_acct` INT, IN `amount_paid` INT, IN `userId` VARCHAR(50), IN `in_receiptDateSN` VARCHAR(50), 
    IN `in_receiptSN` VARCHAR(50), IN `in_description` VARCHAR(1000), IN `in_mode_of_payment` VARCHAR(20),
    IN `in_facId` VARCHAR(50), IN `sourceAcct` VARCHAR(50), IN `acc_name` VARCHAR(100), IN `acc_type` VARCHAR(20),
    in `paymentStatus` varchar(50), in `goods_amount` int, in `in_goods_head` varchar(50))
    -- goods_amount,paymentStatus
BEGIN
    declare supplier_balance double;
    declare main_balance double;
    DECLARE balance_paid DOUBLE;
    --    #DECLARE remaining_balance double;
    select balance into supplier_balance from suppliersinfo where id=in_acct AND facilityId=in_facId;

    -- #SET remaining_balance = Amount_paid - supplier_balance;
    set main_balance = supplier_balance+Amount_paid;
    set balance_paid = goods_amount - Amount_paid


    IF paymentStatus = 'Full Payment' THEN
   
    -- update patientfileno set balance= instant_balance where accountNo=in_acct AND facilityId=in_facId;
       
       insert into supplier_entries (acct,dr,cr,reference_no,facilityId)
           values (in_acct,0,goods_amount,in_receiptDateSN,in_facId);
   
    insert into transactions (description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment,enteredBy,client_acct,patient_id,facilityId)
           values (in_description,sourceAcct,0,goods_amount,in_receiptDateSN,in_receiptSN,in_mode_of_payment,userId,in_acct,patientId,in_facId);

           insert into transactions (description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment,enteredBy,client_acct,patient_id,facilityId)
           values (in_description,in_goods_head,abs(goods_amount),0 ,in_receiptDateSN,in_receiptSN,in_mode_of_payment,userId,in_acct,patientId,in_facId);
   
    -- # IF customer has an account, but the deposit amount nullifies his account balance
    -- # i.e. he's paying up his debt
    ELSE IF paymentStatus = 'Pay Later' THEN
        update suppliersinfo set balance= main_balance  where id=in_acct AND facilityId=in_facId;

            insert into supplier_entries(acct,dr,cr,reference_no,facilityId)
            values(in_acct, Amount_paid, 0, in_receiptDateSN, in_facId);

            insert into transactions(description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment, enteredBy,client_acct,patient_id,facilityId)
            values(in_description, in_goods_head, Amount_paid, 0, in_receiptDateSN, in_receiptSN, in_mode_of_payment, userId, in_acct, in_acct, in_facId);

            insert into transactions(description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment, enteredBy,client_acct,patient_id,facilityId)
            values(in_description, 'Payable', 0, abs(Amount_paid) , in_receiptDateSN, in_receiptSN, in_mode_of_payment, userId, in_acct, in_acct, in_facId);

    ELSE IF paymentStatus = 'Part Payment' THEN
        update suppliersinfo set balance= main_balance  where id=in_acct AND facilityId=in_facId;

        insert into supplier_entries(acct,dr,cr,reference_no,facilityId)
        values(in_acct, Amount_paid, 0, in_receiptDateSN, in_facId);

        insert into transactions(description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment, enteredBy,client_acct,patient_id,facilityId)
        values(in_description, in_goods_head, goods_amount, 0, in_receiptDateSN, in_receiptSN, in_mode_of_payment, userId, in_acct, in_acct, in_facId);

        insert into transactions(description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment, enteredBy,client_acct,patient_id,facilityId)
        values(in_description, sourceAcct, Amount_paid, 0, in_receiptDateSN, in_receiptSN, in_mode_of_payment, userId, in_acct, in_acct, in_facId);

        insert into transactions(description,acct,debit,credit,receiptDateSN,receiptNo,modeOfPayment, enteredBy,client_acct,patient_id,facilityId)
        values(in_description, 'Payable', 0, abs(balance_paid) , in_receiptDateSN, in_receiptSN, in_mode_of_payment, userId, in_acct, in_acct, in_facId);


    end if;

END