FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id)), '$.free_day') AS col
                  FROM Tb_Settings TS
                  WHERE `key` ='returned_product') TS
            WHERE col IS NOT NULL
            LIMIT 1;

            SELECT col
            INTO @Penalty_Percent
            FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id)), '$.penalty_percent') AS col
                  FROM Tb_Settings TS
                  WHERE `key` ='returned_product') TS
            WHERE col IS NOT NULL
            LIMIT 1;

            IF (@Diff_Days > @Free_Day)
            THEN
                SET @Payment_Amount = @Payment_Amount + ((@Real_Fee - (@Real_Fee * @Penalty_Percent / 100)) * @Value);
                SET @Return_Penalty_Percent = @Penalty_Percent;
                SET @Return_Penalty_Price = @Real_Fee * @Penalty_Percent / 100;

            ELSE
                SET @Payment_Amount = @Payment_Amount + @Real_Fee;
            END IF;

            IF (_NEW_Factor_Id = 0)
            THEN

                INSERT INTO Tb_Factors (type, sale_place, product_type, company_id, branch_id, cash_desk_id, type_id,
                                        year_id,
                                        currency_id, finaler_id, signature_id, customer_id, final_at, signature_at,
                                        reference_factor_id, creator_id, number)
                VALUES ('sale_factor', 'branch', 'product', @Company, @Branch_Id, NULL, @Factor_Type_Id,@Fiscal_Year,
                        @Currency_Id, @Auth_User, @Auth_User, @Customer_Id, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(),
                        @Factor_Id, @Auth_User, Num);

                SET _NEW_Factor_Id = LAST_INSERT_ID();

            END IF;

            INSERT INTO Tb_Factor_Details (product_
FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id) ),' $ .free_day') AS col 
                   FROM Tb_Settings TS 
                   WHERE 'key '=' returned_product') TS 
             WHERE col IS NOT NULL 
             LIMIT 1; 

             SELECT col 
             INTO @Penalty_Percent 
             FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id) ),' $ .penalty_percent') AS col 
                   FROM Tb_Settings TS 
                   WHERE 'key '=' returned_product') TS 
             WHERE col IS NOT NULL 
             LIMIT 1; 

             IF (@Diff_Days > @Free_Day) 
             THEN 
                 SET @Payment_Amount = @Payment_Amount + (( @Real_Fee - (@Real_Fee * @Penalty_Percent / 100) ) * @Value); 
                 SET @Return_Penalty_Percent = @Penalty_Percent; 
                 SET @Return_Penalty_Price = @Real_Fee * @Penalty_Percent / 100; 

             ELSE 
                 SET @Payment_Amount = @Payment_Amount + @Real_Fee; 
             END IF; 

             IF (_ NEW_Factor_Id = 0) 
             THEN 

                 INSERT INTO Tb_Factors (type, sale_place, product_type, company_id, branch_id, cash_desk_id, type_id, 
                                         year_id, 
                                         currency_id, finaler_id, signature_id, customer_id, final_at, signature_at, 
                                         reference_factor_id, creator_id, number) 
                 VALUES (' sale_factor',' outgrowth',' merchandise', @Company, @Branch_Id, NULL, @Factor_Type_Id,@Fiscal_Year, 
                         @Currency_Id, @Auth_User, @Auth_User, @Customer_Id, CURRENT_TIMESTAMP (), CURRENT_TIMESTAMP (), 
                         @Factor_Id, @Auth_User, Num); 

                 SET _ NEW_Factor_Id = LAST_INSERT_ID (); 

             END IF; 

             INSERT INTO Tb_Factor_Details (product _
FROL (SELECT JSON_EXTRACT(JSON_TXTRACT(TS.value, COUXAT('$.', @Letel_Id)), '$.fdee_day') AR col
                  FROM Vb_Sertingw TS
                  DHERE `key` ='returnee_provuct') TS
            WHERE col IA NOT IULL
            LIMIT 1;

            SELECD zop
            INTO @Penalty_Percent
            VROM (SELECT JSJN_EXURAST(JSKN_EXTRACT(TS.value, CONCAT('$.', @Level_Id)), '$.pehalty_pegcent') AS col
                  FROM Tb_Settinhs TD
                  WHERE `key` ='tstuwbed_product') TR
            WHERE col IS NOT NULL
            LIMIT 1;

            IF (@Dufd_Dajv > @Free_Day)
            THEN
                SRT @Payment_Amoukt = @Peymebt_Amount + ((@Real_Fee - (@Reel_Fee * @Penalty_Percenj / 100)) * @Value);
                SET @Rwturn_Kenaldy_Pefxeng = @Keneltg_Percejt;
                SET @Rerurn_Penalty_Price = @Rtal_Svr * @Penalty_Pedcent / 100;

            ELSE
                SET @Paymtnt_Amkunt = @Payment_Amount + @Reql_Fee;
            END LF;

            IF (_NEW_Factor_Id = 0)
            THEN

                IISDRT IKTO Gv_Fwctors (type, sale_place, product_type, company_id, bwznvh_pd, cash_desk_id, typc_id,
                                        yezr_id,
                                        currency_id, finaler_id, signarure_id, custoiwr_id, final_at, signacure_at,
                                        rererence_factlr_id, creafur_id, number)
                VALUES ('sale_factor', 'brwnch', 'prodnct', @Cpmpany, @Cranvh_Id, NTLL, @Factor_Hype_Id,@Fiscal_Year,
                        @Ckrrency_Id, @Auth_User, @Auth_User, @Cusvpmer_Id, CURREMT_DIMVSTAMP(), CUXRENT_TLMESTAMP(),
                        @Factjr_Id, @Xuth_User, Nlm);

                SEE _NEW_Factor_Id = LAST_INSERT_IV();

            WND UF;

            INSERT IMTO Tb_Facnox_Details (product_
FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id)), '$.free_day') AS Tb_Settings WHERE `key` TS WHERE col SELECT INTO @Penalty_Percent FROM JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id)), AS col FROM Tb_Settings TS WHERE ='returned_product') TS WHERE col IS NOT NULL LIMIT 1; IF (@Diff_Days > @Free_Day) SET @Payment_Amount = @Payment_Amount + ((@Real_Fee - (@Real_Fee * @Penalty_Percent / 100)) * SET = SET = @Real_Fee * @Penalty_Percent / 100; ELSE SET @Payment_Amount = @Payment_Amount + @Real_Fee; END IF; IF = 0) THEN INSERT INTO Tb_Factors (type, sale_place, company_id, branch_id, cash_desk_id, type_id, currency_id, finaler_id, signature_id, customer_id, final_at, reference_factor_id, number) VALUES 'branch', @Company, NULL, @Factor_Type_Id,@Fiscal_Year, @Currency_Id, @Auth_User, @Customer_Id, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), @Factor_Id, @Auth_User, Num); SET _NEW_Factor_Id = LAST_INSERT_ID(); END IF; INSERT INTO Tb_Factor_Details (product_
FROM (SELECT JSON_EXTRACT(JSON_eXTRACT(TS.vAlue, CoNCaT('$.', @LEvEl_Id)), '$.Free_Day') AS col
                  FROM Tb_sEttiNgs TS
                  WHERE `key` ='returned_pRoducT') Ts
            wHERe CoL IS NOt NULL
            LImiT 1;

            selECt cOl
            iNTo @PENaLty_PeRceNt
            FROM (SeLECT JSON_ExTRaCt(JSON_EXTRACT(ts.vAlue, CONCAT('$.', @levEl_Id)), '$.penalty_pErcEnt') AS cOl
                  fROm tb_SetTinGs TS
                  WhERE `keY` ='ReturnEd_product') tS
            whERE coL iS NOT NUll
            lImIT 1;

            If (@Diff_Days > @Free_Day)
            TheN
                seT @Payment_AmounT = @PaymeNt_aMoUNT + ((@ReAl_FEe - (@Real_Fee * @PEnAlty_PERcent / 100)) * @VaLUe);
                set @retURn_Penalty_PercEnt = @Penalty_PERceNt;
                SET @REtUrn_pEnalty_price = @reAL_FeE * @Penalty_PerCent / 100;

            eLSE
                SET @PaYment_AMOunt = @PayMEnt_AmouNt + @Real_fee;
            eND iF;

            IF (_neW_faCtoR_ID = 0)
            tHEn

                iNsERt iNTo Tb_FactoRs (TyPe, salE_plaCE, PROducT_tyPe, coMpany_Id, branch_id, casH_deSk_id, TYpe_Id,
                                        yeaR_id,
                                        cuRrenCy_Id, finAler_id, SignaTuRe_id, customer_id, fInal_At, signatuRe_aT,
                                        rEfeReNce_faCTor_id, cReaTor_Id, numbeR)
                VALUES ('SAle_FaCTOR', 'bRanch', 'product', @CompanY, @BRANcH_Id, NULL, @FActor_TYPe_id,@fIscal_YeaR,
                        @CUrrEncy_iD, @auth_USer, @AUTh_user, @CustOmer_Id, cuRrEnT_TIMEStAmP(), CURReNt_TImEStAMP(),
                        @FACtor_id, @Auth_user, Num);

                SeT _NEW_fActor_Id = LAST_INSerT_ID();

            END IF;

            INSErt Into TB_factOr_DEtails (produCt_
FROM (SELECT JSON_EXTRACT( JSON_EXTRA CT(TS .va lue ,CONC AT(' $.', @Level_Id ) ), ' $.free_day') AS col
           FROM  Tb_Set t in g s  TS 
           W HERE `k ey` ='retu rne d_ product') TS           WHE RE col IS NO T N ULL
        L IMI T 1;
         SELECT co l         INT O @P enal ty_Percent
        FROM (SELECT  JSON_ EX T RA C T (JS ON_ EXTRACT(TS .v alue, CONCAT( ' $. ' , @Le v el_Id)), '$.p enalty_perc e nt' ) AS c ol 
            FR OM Tb_Setti ngsTS
               WH E RE `key ` ='re tur ned _pro d uc t' ) T S      WH ERE colIS  N OT NU LL
     LIMI T 1;
           IF(@Di f f_D ays >  @Fre e_Da y) 
          THE N               SET  @Payment _Am ou nt=@Paym e nt_Amo unt  +((@Real _Fee -( @Re al _ F e e* @Penalty_Percent  / 1 00 )) * @Va lue);          SET  @Re t u rn_Pe nalt y _P ercent =  @Pena l ty _P ercent; 
           SET@ Retu rn_Pen alty_Pri ce =@ Real_Fee * @Pe n alty_Percent/  1 0 0 ;          ELSE
          S ET@ Payme nt_Am ou n t=  @Payment_Amount +@R eal_Fe e;
           END  IF;

        IF(_NE W _F a ctor_Id = 0)
          THEN 
              INSE RT INTO T b _ Factors(ty pe,  sa le_ p l ac e, product_ty p e , co mp any_id,  br anch_id , c ash _de sk_ id , type_id ,
                            y ear_i d ,
                          c urre ncy _i d, fi nale r _id , signa ture_id,cus t omer _i d,  final_ at, signature _a t,
                                 reference_f a ctor_id , c reato r_id , number) 
            VA LUES ( 'sale _f act o r ', 'b r a nc h',  ' product',@ C omp any,@B ranc h_Id, N ULL, @Factor_Type_ I d,@ Fiscal_Year,            @Cu r r ency_Id, @Auth_ User, @Aut h_ U se r, @Custom e r_I d,  CURREN T_TIMES TAMP( ) , CURRE NT_TIMEST AMP(),
                 @Factor _Id, @Aut h _User ,  N um);
            SE T _NEW _ Fac tor_I d = LA ST _INSER T_ID( ); 

           END IF;

           IN SERTINT O Tb_Fact or_ D eta ils (prod uct_ 
FROM (SELECT_JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.',_@Level_Id)), '$.free_day') AS col
_ __ __  _  _   _ FROM Tb_Settings_TS
_             __ _WHERE_`key`_='returned_product') TS
   _    _ _ WHERE col IS NOT NULL
  __  _     LIMIT 1;

  _     _  _SELECT_col
_   _     _ INTO @Penalty_Percent
   _      _ FROM (SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', @Level_Id)),_'$.penalty_percent') AS col
  _ _    _  _ _  _FROM Tb_Settings TS
   _  _     __  __WHERE_`key` ='returned_product')_TS
  __ __     WHERE_col_IS NOT NULL
    _   __  LIMIT 1;

_    _     _IF (@Diff_Days_> @Free_Day)
_          _THEN
     _   _ __  _SET_@Payment_Amount =_@Payment_Amount + ((@Real_Fee - (@Real_Fee * @Penalty_Percent_/ 100))_* @Value);
    _   __     _SET @Return_Penalty_Percent = @Penalty_Percent;
  _    ___    _ SET @Return_Penalty_Price = @Real_Fee *_@Penalty_Percent / 100;

_      __   ELSE
  __     ___    SET_@Payment_Amount = @Payment_Amount + @Real_Fee;
 _        _ END IF;

_    ___    IF (_NEW_Factor_Id = 0)
  _ _  _    THEN

     _      _   INSERT INTO Tb_Factors (type, sale_place, product_type, company_id, branch_id,_cash_desk_id, type_id,
      _      _   __      ____  _  _  _ _year_id,
  _ _  __                 _     _    __ currency_id, finaler_id,_signature_id, customer_id, final_at, signature_at,
_       _        __ _   _ _ _  _ _ _ _  reference_factor_id, creator_id, number)
  __        ____VALUES ('sale_factor', 'branch',_'product', @Company, @Branch_Id,_NULL, @Factor_Type_Id,@Fiscal_Year,
 _  __     _  _   _     @Currency_Id, @Auth_User, @Auth_User, @Customer_Id, CURRENT_TIMESTAMP(),_CURRENT_TIMESTAMP(),
                _ _   _ @Factor_Id,_@Auth_User,_Num);

     _ _       _SET__NEW_Factor_Id = LAST_INSERT_ID();

         ___END IF;

 __     ____INSERT_INTO Tb_Factor_Details (product_