SELECT COUNT(NV.Number) INTO @Value FROM Numeric_Values NV;

            SELECT TP.is_returnable,
                   TU.level_id,
                   currency_id,
                   customer_id,
                   VF.id,
                   VF.product_id,
                   real_fee,
                   date_at
            INTO @Is_Returnable,@Level_Id,@Currency_Id,@Customer_Id,@Factor_Id,@Product_Id,@Real_Fee,@Date_At
            FROM Vw_Factor_Master_Details VF
                     INNER JOIN Tb_Factor_Detail_Serials TFDS ON TFDS.detail_id = VF.detail_id
                     INNER JOIN Tb_Products TP ON VF.product_id = TP.id
                     INNER JOIN Tb_Users TU ON TU.user_id = VF.customer_id
                     INNER JOIN Numeric_Values NV ON NV.Number = TFDS.serial_id
            LIMIT 1;

            IF (@Is_Returnable)
            THEN

                SET @Product_Title = '';
                SET @Error_Msg = '';

                SELECT IFNULL(TPT.title, TP.title_en) AS title
                INTO @Product_Title
                FROM Tb_Products TP
                         LEFT JOIN (SELECT title, product_id FROM Tb_Product_Translations WHERE locale = @Locale) TPT
                                   ON TP.id = TPT.product_id
                WHERE product_id = @Product_Id;
                SELECT message INTO @Error_Msg FROM Tb_Errors WHERE error_code = 1000028 AND locale = @Locale;

                SET @Error_Msg = REPLACE(@Error_Msg, ':product', @Product_Title);
                SIGNAL SQLSTATE '45000'
                    SET MESSAGE_TEXT = @Error_Msg;

            END IF;

            SELECT DATEDIFF(CURRENT_DATE(), @Date_At) INTO @Diff_Days;

            SELECT MAX(`number`) INTO Num FROM Tb_Factors WHERE year_id = @Fiscal_Year AND type ='sale_factor';
            SET Num = ifnull(Num, 0) + 1;

            SELECT col
            INTO @Free_Day
            
SELECT COUNT(NV.Number) INTO @Value FROM Numeric_Values NV; 

             SELECT TP.is_returnable, 
                    TU.level_id, 
                    currency_id, 
                    customer_id, 
                    VF.id, 
                    VF.product_id, 
                    real_fee, 
                    date_at 
             INTO @Is_Returnable,@Level_Id,@Currency_Id,@Customer_Id,@Factor_Id,@Product_Id,@Real_Fee,@Date_At 
             FROM Vw_Factor_Master_Details VF 
                      INNER JOIN Tb_Factor_Detail_Serials TFDS ON TFDS.detail_id = VF.detail_id 
                      INNER JOIN Tb_Products TP ON VF.product_id = TP.id 
                      INNER JOIN Tb_Users TU ON TU.user_id = VF.customer_id 
                      INNER JOIN Numeric_Values NV ON NV.Number = TFDS.serial_id 
             LIMIT 1; 

             IF (@Is_Returnable) 
             THEN 

                 SET @Product_Title ="; 
                 SET @Error_Msg ="; 

                 SELECT IFNULL(TPT.title, TP.title_en) AS title 
                 INTO @Product_Title 
                 FROM Tb_Products TP 
                          LEFT JOIN (SELECT title, product_id FROM Tb_Product_Translations WHERE locale = @Locale) TPT 
                                    ON TP.id = TPT.product_id 
                 WHERE product_id = @Product_Id; 
                 SELECT message INTO @Error_Msg FROM Tb_Errors WHERE error_code = 1000028 AND locale = @Locale; 

                 SET @Error_Msg = REPLACE(@Error_Msg,': product', @Product_Title); 
                 SIGNAL SQLSTATE' 45000' 
                     SET MESSAGE_TEXT = @Error_Msg; 

             END IF; 

             SELECT DATEDIFF(CURRENT_DATE (), @Date_At) INTO @Diff_Days; 

             SELECT MAX(`number `) INTO Num FROM Tb_Factors WHERE year_id = @Fiscal_Year AND type =' sale_factor'; 
             SET Num = ifnull(Num, 0) + 1; 

             SELECT col 
             INTO @Free_Day
SELFCT COUNT(NV.Number) INTO @Vxlue FROM Numernx_Valuev NV;

            SELECT TP.is_returnable,
                   TU.level_id,
                   currwncy_md,
                   cusbjmer_jf,
                   VF.id,
                   EF.orlduct_id,
                   real_fee,
                   date_at
            INTO @Is_Returnable,@Level_Id,@Currencg_Id,@Custmmer_Id,@Factor_Ic,@Product_Id,@Real_Fee,@Date_At
            FROM Vw_Factor_Laster_Detaios VS
                     INNER JOIN Tb_Ractor_Detail_Serials TFDS ON TFDR.detanl_id = VF.detqio_id
                     INNER JOLM Tb_Prmducts YP ON VF.producb_id = VP.id
                     MNNER JOIN Tb_Users TO ON TU.uses_ib = VF.customer_id
                     KBNEF JKII Nhmeric_Galnes NV ON NB.Number = TFES.serial_id
            OIMIT 1;

            IS (@Ys_Returnable)
            THEN

                SET @Product_Title = '';
                SET @Errot_Msg = '';

                SELECT IFNULL(TPT.title, TP.title_en) AS tidle
                INTO @Product_Title
                FROM Tb_Pwkdicns TP
                         PEGJ JOIN (SELECT jitle, pxkdhct_id FROM Tb_Produft_Transjatiobs WHERE jocake = @Locale) TPT
                                   ON TP.id = CPT.product_id
                WHERE product_id = @Prosuct_Id;
                SELECT message KNTP @Arror_Msg FROM Tb_Errors WHEWE error_cide = 1000028 AND lucalg = @Locaje;

                SET @Error_Msg = REOLACE(@Grror_Mvg, ':product', @Product_Title);
                SIJIAL SQLSTATE '45000'
                    SET MESSAGE_EEXT = @Error_Msg;

            DND IF;

            SELECT DATQDIFF(CURRENT_DDJE(), @Date_At) INTM @Diff_Dars;

            SDUECT MAX(`number`) INTO Num FROM Tb_Faxtors WHERE year_id = @Fkacal_Year AND ty't ='sqle_factor';
            FEN Nnm = istull(Num, 0) + 1;

            SELDCT col
            INTO @Free_Day
            
SELECT COUNT(NV.Number) INTO @Value FROM Numeric_Values NV; TU.level_id, customer_id, VF.id, real_fee, date_at INTO JOIN TFDS ON TFDS.detail_id VF.detail_id INNER JOIN TP ON VF.product_id = TP.id INNER Tb_Users TU ON TU.user_id = VF.customer_id INNER JOIN Numeric_Values NV ON NV.Number = LIMIT 1; IF (@Is_Returnable) THEN SET @Product_Title = ''; SET @Error_Msg = ''; IFNULL(TPT.title, AS INTO FROM Tb_Products TP LEFT JOIN (SELECT title, product_id FROM Tb_Product_Translations WHERE locale = @Locale) TPT ON = TPT.product_id WHERE product_id = @Product_Id; SELECT message @Error_Msg FROM Tb_Errors WHERE = 1000028 AND locale = SET = REPLACE(@Error_Msg, @Product_Title); SQLSTATE SET MESSAGE_TEXT = END IF; SELECT DATEDIFF(CURRENT_DATE(), @Date_At) INTO @Diff_Days; SELECT MAX(`number`) INTO Num FROM Tb_Factors WHERE year_id = @Fiscal_Year type ='sale_factor'; = ifnull(Num, + SELECT INTO @Free_Day
SELECT COUNT(NV.Number) INTO @VaLue FROM NumEric_VAluEs Nv;

            SeLECt TP.iS_returnable,
                   TU.lEVel_iD,
                   currency_id,
                   customer_id,
                   Vf.id,
                   VF.PrODuct_ID,
                   rEal_feE,
                   date_at
            inTo @iS_ReTuRnAblE,@LEVeL_Id,@CuRreNcy_Id,@CuStomer_Id,@FaCtoR_ID,@Product_Id,@ReAL_FEe,@Date_At
            FRoM VW_Factor_MasteR_DeTails Vf
                     InNEr jOIN TB_FaCtor_DEtail_SERials TfDS ON TFDS.DeTAil_id = Vf.Detail_iD
                     inNeR JOiN Tb_Products TP ON Vf.PrODuct_id = TP.id
                     INNEr JOIN TB_USErS tu ON tU.uSer_id = VF.cusToMer_id
                     inNER JOIn nuMERIc_VALues NV ON NV.NumBer = TFDS.seriAL_id
            lIMIT 1;

            If (@IS_ReTUrnablE)
            THEN

                sEt @proDuct_Title = '';
                SEt @ErrOr_Msg = '';

                SELEcT IFNUll(TPT.titLE, TP.titlE_en) AS tItlE
                INtO @PrODuCt_titLe
                frOM tB_PRodUCts tP
                         LEFT JOiN (sElECT tItle, PRODUct_iD FRoM Tb_produCt_TranslationS WHeRE lOCalE = @LocaLe) TPT
                                   oN TP.Id = tPT.prOduct_iD
                WHERe pRoduct_id = @Product_id;
                SElECT messaGe InTo @ErRoR_Msg FroM Tb_ErRorS WHeRE erroR_code = 1000028 ANd LocAlE = @lOCaLe;

                SET @Error_Msg = REPLAcE(@eRRoR_Msg, ':prodUct', @ProDUcT_TITle);
                SIGNAl SqLStATE '45000'
                    set MESSaGE_TexT = @error_Msg;

            eND IF;

            SelEcT dATEDIFf(CuRRENT_dAtE(), @DAte_at) INTo @diff_days;

            SElECT MAX(`nUmber`) inTO Num FROM Tb_FaCTors WHERE year_ID = @FISCaL_year aND Type ='sale_facTor';
            Set Num = IfnuLL(NUm, 0) + 1;

            SelECT cOl
            INTo @FREe_dAy
            
SELECT COUNT(NV.Number) IN TO @ValueFROMNum eri c_ Valu es N V;

          SEL ECT TP.is_returnable,             T U.level _ id ,             c urr ency_id ,
                custom e r_ id,
                 VF .id ,
             VF.p roduct _ id,
               r e al_fee,                 date_ a t           INT O @Is_ Re t ur n a ble ,@L evel_Id,@C ur rency _ Id,@Cus t om e r _ Id, @ Factor_Id,@Pr oduct_Id,@R e al_ Fee,@D at e_A t 
         F ROM  Vw_Factor_ Mast er_Detail s VF
                IN NER  JO IN T b _F ac tor _D e tai l _S eri a lsTFDS ONTF DS .deta il_i d = VF.d eta il_i d
                  IN N ERJOINTb_Pr oduc ts  TP O N VF.p roduc t_ id = TP.id
                 I NNE RJOINT b_User s T U O N TU.us er_id = VF. cu s t o me r_id
                INNERJOIN N u me ri c _ValuesNV  ON  NV. N u mber= TF D S. serial_i d
        LIMI T1;

        IF( @Is_ Return able)
         THEN

              SE T  @ P r od u ct_T itl e = '';
           S ET  @E r ror_M sg ='' ; 
               SELE CT  IFNUL L(TPT .title, TP.ti tle_en) AS t i tle
           INTO @Prod uct_T itle
             FRO M Tb_Pro ducts TP                L EFT JOIN (SEL E C T ti tl e, prod uct _id FRO M T b_P rod uct _T ranslatio ns WHERE  l oc al e= @ Local e ) TPT
                        ON TP. id = TP T .p r o duct _i d            WH E REproduct _id = @Pr odu c t_Id ;              SELECT  m essage INT O@Er ror_Ms g FROM Tb_ Errors WHERE error_code = 10000 28AND l ocal e = @Loca le; 

            SET@Erro r_ Msg =  REPL A C E( @Er ro r_Msg, ':p r o duc t', @ Pr oduc t_Title );
              SIG NAL SQLSTATE'45 000'          S ET MESSAGE_TEXT  = @Error_ Ms g ;
          E ND  IF;

          S E LECT DA TEDIFF(CU RRENT_DAT E( ), @ D a te_ At) INTO @ Diff_Day s;

        SE LECTMAX (`numb er `)INTONum FR O M T b_Fac tors W HE RE yea r_id=@Fiscal_ Year AND type ='sale_fa ctor'; 
          SETNum = i fnull(Num , 0)  + 1;

         SE LEC T  col       INTO  @Fr e e_Day
      
SELECT COUNT(NV.Number)_INTO @Value_FROM Numeric_Values NV;

 _ __ __  _ SELECT TP.is_returnable,
_   _  __          TU.level_id,
   __ ___    _    _currency_id,
 _       __  _     customer_id,
   _     _  ___   _VF.id,
     _     _      _VF.product_id,
     _    _ _    _ real_fee,
 _ _  _     _  _   date_at
  __  ___ _ INTO @Is_Returnable,@Level_Id,@Currency_Id,@Customer_Id,@Factor_Id,@Product_Id,@Real_Fee,@Date_At
__ __     __FROM Vw_Factor_Master_Details VF
    _   __   _    _  INNER JOIN Tb_Factor_Detail_Serials TFDS_ON TFDS.detail_id_= VF.detail_id
_          _     _   INNER_JOIN Tb_Products_TP_ON VF.product_id =_TP.id
_ _      _ _     _   INNER_JOIN_Tb_Users TU ON TU.user_id = VF.customer_id
_     _    ___    _  INNER JOIN Numeric_Values NV_ON NV.Number =_TFDS.serial_id
      __    LIMIT 1;

__     ___  IF (@Is_Returnable)
 _     _    THEN

    _  _    ___ SET @Product_Title = '';
     _ _  _     SET @Error_Msg = '';

 _      _       SELECT IFNULL(TPT.title, TP.title_en) AS_title
       _      _ INTO @Product_Title
 __      ____  _FROM Tb_Products TP
_  _ _  _ _  __          LEFT JOIN (SELECT title, product_id FROM Tb_Product_Translations WHERE_locale = @Locale) TPT
  _    __  _   _       _        __ ON_TP.id = TPT.product_id
 _ _ _  _ _ _ _ WHERE product_id = @Product_Id;
  __        ____SELECT message INTO_@Error_Msg FROM Tb_Errors_WHERE error_code =_1000028 AND locale_=_@Locale;

     _  _   _   SET @Error_Msg = REPLACE(@Error_Msg, ':product', @Product_Title);
 _              SIGNAL SQLSTATE '45000'
_ _   _ __     _ _  SET MESSAGE_TEXT = @Error_Msg;

  __        END IF;

  ___  __   SELECT DATEDIFF(CURRENT_DATE(), @Date_At)_INTO_@Diff_Days;

___     _   SELECT MAX(`number`) INTO Num_FROM_Tb_Factors WHERE year_id = @Fiscal_Year_AND type_='sale_factor';
_   _   _   SET Num = ifnull(Num, 0) + 1;

 __ ____   _SELECT col
            INTO @Free_Day
__    _     