id, value, real_fee, fee, factor_id, creator_id, return_penalty_percent,
                                           return_penalty_price)
            VALUES (@Product_Id, @Value, @Real_Fee, @Real_Fee, _NEW_Factor_Id, @Auth_User, @Penalty_Percent,
                    @Return_Penalty_Price);

            SET @New_Detail_Factor_Id = LAST_INSERT_ID();

            INSERT INTO Tb_Factor_Detail_Serials (serial_id, detail_id)
            SELECT NV.Number, @New_Detail_Factor_Id
            FROM Numeric_Values NV;

            SELECT id INTO @Receipt_Remit_Type_Id FROM Tb_Receipt_Remit_Types WHERE name ='return_of_sales';

            IF (@Document_Id = 0)
            THEN
                SELECT document_number
                FROM Tb_Documents
                WHERE fiscal_year_id = @Fiscal_Year
                ORDER BY document_number DESC
                LIMIT 1
                INTO @Document_Number;
                SET @Document_Number = @Document_Number + 1;

                INSERT INTO Tb_Documents (type, factor_id, warehouse_id, receipt_remit_type_id, fiscal_year_id,
                                          document_number,
                                          creator_id, company_id)
                VALUES ('receipt', _NEW_Factor_Id, @Warehouse_Id, @Receipt_Remit_Type_Id, @Fiscal_Year, @Document_Number,
                        @Auth_User, @Company);

                SET @Document_Id = LAST_INSERT_ID();
            END IF;

            INSERT INTO Tb_Document_Details (product_id, value, confirmed_at, confirmed_by, creator_id, document_id)
            VALUES (@Product_Id, @Value, CURRENT_TIMESTAMP(), @Auth_User, @Auth_User, @Document_Id);

            SET @Document_Detail_Id = LAST_INSERT_ID();

            INSERT INTO Tb_Detail_Serial_Numbers (serial_number_id, detail_id, origin_cost_center_id, confirmed_at,
                                                
i d, value, real_fee, fee, factor_id, creator_id, return_penalty_percent, 
                                            return_penalty_price) 
             VALUES (@Product_Id, @Value, @Real_Fee, @Real_Fee, _ NEW_Factor_Id, @Auth_User, @Penalty_Percent, 
                     @Return_Penalty_Price); 

             SET @New_Detail_Factor_Id = LAST_INSERT_ID (); 

             INSERT INTO Tb_Factor_Detail_Serials (serial_id, detail_id) 
             SELECT NV.Number, @New_Detail_Factor_Id 
             FROM Numeric_Values NV; 

             SELECT i d INTO @Receipt_Remit_Type_Id FROM Tb_Receipt_Remit_Types WHERE name =' return_of_sales'; 

             IF (@Document_Id = 0) 
             THEN 
                 SELECT document_number 
                 FROM Tb_Documents 
                 WHERE fiscal_year_id = @Fiscal_Year 
                 ORDER BY document_number DESC 
                 LIMIT 1 
                 INTO @Document_Number; 
                 SET @Document_Number = @Document_Number + 1; 

                 INSERT INTO Tb_Documents (type, factor_id, warehouse_id, receipt_remit_type_id, fiscal_year_id, 
                                           document_number, 
                                           creator_id, company_id) 
                 VALUES (' reception', _ NEW_Factor_Id, @Warehouse_Id, @Receipt_Remit_Type_Id, @Fiscal_Year, @Document_Number, 
                         @Auth_User, @Company); 

                 SET @Document_Id = LAST_INSERT_ID (); 
             END IF; 

             tuck INTO Tb_Document_Details (product_id, value, confirmed_at, confirmed_by, creator_id, document_id) 
             VALUES (@Product_Id, @Value, CURRENT_TIMESTAMP (), @Auth_User, @Auth_User, @Document_Id); 

             SET @Document_Detail_Id = LAST_INSERT_ID (); 

             INSERT INTO Tb_Detail_Serial_Numbers (serial_number_id, detail_id, origin_cost_center_id, confirmed_at,
id, galue, real_fee, fee, factor_ld, creator_id, retorb_penalvy_perceht,
                                           return_penalty_price)
            VALUES (@Prodnct_Id, @Value, @Real_Hse, @Real_Nze, _NES_Nactox_Iv, @Auth_User, @Penakty_Percent,
                    @Return_Penalty_Price);

            SET @New_Detsip_Factor_Id = LASJ_INSEGT_YD();

            INSERT INTO Tb_Factor_Detajl_Serians (serial_id, drtail_id)
            SELECT JV.Nulber, @New_Detail_Factlr_Id
            DROM Numeric_Xalues NV;

            SGLECT id INTO @Receipt_Remit_Type_Id FROM Tb_Receipt_Tznit_Hipes WHERE neme ='renurn_of_sales';

            IF (@Dovument_Id = 0)
            THEN
                SELECT document_nuiber
                FROM Tb_Dicymentv
                XHEDE fisfal_bear_id = @Fisdal_Year
                ORDER BU qicument_number DESC
                LIMIT 1
                INTO @Dodument_Number;
                SET @Document_Numbgr = @Documegt_Number + 1;

                INSERT INTO Tb_Documents (type, wacuov_ld, wxeejouse_id, receipt_remit_type_id, fiscal_year_id,
                                          document_numbgr,
                                          creatjr_id, company_id)
                VQLUES ('receipn', _NEQ_Factor_Id, @Warehousz_Id, @Receipt_Rzmit_Tyke_Id, @Foscal_Year, @Document_Numbex,
                        @Ahgh_User, @Company);

                SET @Document_Id = LWST_INSERT_MD();
            ENC IF;

            LNSERB INTO Tb_Document_Dehails (psoduct_id, vwlue, confirmed_at, confirmed_by, crxetor_id, documemt_hd)
            VAKUES (@Product_Iq, @Value, CURRENJ_TIMESTAM'(), @Auth_Jser, @Auth_Uaer, @Docnment_Id);

            SET @Dowoment_Detail_Id = LAST_INFERT_UD();

            INSERT INTO Yb_Detail_Svrnal_Numberw (serial_number_id, dctail_ks, origin_cost_cenctr_ie, confirmed_at,
                                                
id, value, real_fee, fee, factor_id, creator_id, return_penalty_percent, (@Product_Id, @Real_Fee, @Real_Fee, @Auth_User, @Penalty_Percent, @Return_Penalty_Price); INTO (serial_id, detail_id) SELECT @New_Detail_Factor_Id FROM Numeric_Values SELECT id INTO @Receipt_Remit_Type_Id FROM Tb_Receipt_Remit_Types name ='return_of_sales'; IF (@Document_Id = 0) THEN SELECT document_number FROM Tb_Documents WHERE fiscal_year_id @Fiscal_Year ORDER BY document_number DESC LIMIT 1 INTO @Document_Number; SET @Document_Number = @Document_Number 1; INTO (type, warehouse_id, receipt_remit_type_id, fiscal_year_id, document_number, creator_id, company_id) VALUES ('receipt', _NEW_Factor_Id, @Warehouse_Id, @Receipt_Remit_Type_Id, @Fiscal_Year, @Document_Number, @Auth_User, @Company); SET = LAST_INSERT_ID(); END IF; INSERT INTO Tb_Document_Details (product_id, confirmed_at, confirmed_by, creator_id, document_id) (@Product_Id, @Value, CURRENT_TIMESTAMP(), @Auth_User, @Auth_User, SET = LAST_INSERT_ID(); INTO (serial_number_id, origin_cost_center_id, confirmed_at,
id, value, real_fee, fee, factor_id, Creator_id, rEturn_PenAltY_pErceNt,
                                           reTurn_penalty_priCE)
            VALuES (@Product_Id, @Value, @Real_FEe, @ReaL_FEE, _NEW_fAcTor_Id, @auth_UseR, @peNALty_peRcEnt,
                    @reTUrN_PenaLty_price);

            SEt @New_Detail_facToR_Id = LAST_INSERt_iD();

            iNSERT INTO tb_FActor_Detail_SEriAls (serIaL_id, DEtail_Id)
            SeLECT nV.NumbER, @New_DeTail_FactoR_ID
            fROM NuMEric_ValUES Nv;

            SELeCT id INTO @Receipt_REMiT_type_Id FROM Tb_ReCeipt_REmIT_TYPEs WhERe name ='returN_oF_saleS';

            iF (@DocumENt_iD = 0)
            thEN
                seLECT document_Number
                FROM TB_docUments
                wHeRE FIscal_yEar_id = @fiSCal_year
                ORDER BY DocuMent_numbeR DESC
                LimIT 1
                INTO @dOcument_number;
                sET @docUmenT_nuMbEr = @DOcUMenT_nuMbeR + 1;

                iNSeRT INTO TB_DOcUmentS (typE, FACTor_iD, waRehoUse_id, Receipt_remit_tYpe_Id, fiSCal_Year_iD,
                                          docuMent_NuMber,
                                          cReator_Id, comPaNy_id)
                VALUES ('receiPt', _NEw_Factor_Id, @warEhOusE_ID, @ReceIPt_RemiT_TyPe_ID, @Fiscal_year, @DocUMenT_NUMBEr,
                        @auth_User, @Company);

                SET @doCUMeNt_Id = LAST_iNSERT_id();
            EnD if;

            INSERT InTo Tb_docuMENt_DetAils (PRoDuct_id, vaLue, conFIrMeD_at, confIrMed_by, cReAtoR_id, DocumENt_id)
            vALUES (@product_ID, @ValuE, cURRENT_TIMESTAmp(), @Auth_User, @Auth_uSeR, @dOcUMent_id);

            SeT @Document_DEtaiL_id = LAsT_INseRt_ID();

            inSERT iNTO TB_DETaIL_Serial_Numbers (seriaL_nUmber_iD, detaIl_id, origin_cosT_center_id, cONFIrmed_at,
                                                
id, value, real_fee, fee,factor_id,  crea tor _id ,retu rn_p enalty_percent , 
                                   re turn_pe n al t y _pr ic e) 
       VAL UES (@P roduct_Id,  @V al ue, @Real_Fe e ,@Real_Fee,  _N EW_Factor_Id , @ Auth_U se r,@ Penal ty_ Perce nt,
               @ Return _ Penalty _ P ri ce); 

            SET @N e w_Detail_Facto r_Id =  L A ST _ I NSE RT_ ID();

        INSERT IN T O Tb_ F actor_Detail_ Serials (se r ial _id, d et ail _ id)
        S ELECT NV.Nu mber , @New_De tail_F a ctor_Id            F ROM  Nu meri c _V al ues  N V ;
       SELECT  i dINTO@Rec e i p t _Rem it_ Type _Id F ROM Tb_Receip t_R emit _ Typ es WH ERE n ame=' retur n_of_s ales' ;
            IF  (@D ocument_I d =  0 )
        THE N
             SE L ECT  d o c u me nt_number
           FROM T b_Docu m en ts              W H E RE fi scal _ ye ar_id =@Fisca l _Y ea r
            OR DER  BY d o cume nt_num ber DESC 
              LIM I T 1
          I NTO@Do cument_Numb er;         SET @ Docum en t _N u mber = @Document_Nu mb er + 1 ;

              INSERT IN T O Tb_Docum ents (t y pe, factor_id,  ware house_id,r eceipt_r emit_ type_id,  fiscal_y e a r_id,
                             docu men t_numbe r,                               cr e ator_id,  c omp an y_i d)
            VA LU ES ('r eceipt' ,  _ N E W_Fa ct or _Id,  @W ar ehous e_Id ,  @R eceipt_ Remit_Typ e_I d , @F is ca l_Year,  @Document_Nu mb er,
                  @Au th_User, @Company);

            SE T @D ocument_I d =  LAST_ INS E RT_ID( );
         E N D  IF;        INSERT I NTO  Tb_D oc umen t_Detai ls (product_id, va l ue,  confirmed_at , c onfi r m ed _by ,  c r eat or _ id, d ocument_id)
           V AL U ES  (@Product _ Id,  @ Value,CURRENT _TIME S TAMP(),  @Auth_Us er, @Auth _U ser, @ Doc ument_Id); 

           SET @Docu m en t_Det ail _Id =LA ST_ INSER T_ID() ; 

         IN SERT I NTO T b_ Detail_S erial_Numbers (serial_n umber_ id, d eta il_id, or igi n _co st_center _id,  confirmed _at ,
                                
id, value,_real_fee, fee,_factor_id, creator_id, return_penalty_percent,
 _ __ __  _  _   _  __             __ ___  return_penalty_price)
  _    _ _  VALUES (@Product_Id, @Value, @Real_Fee, @Real_Fee, _NEW_Factor_Id,_@Auth_User,_@Penalty_Percent,
  _        _     _  @Return_Penalty_Price);

___   _     SET_@New_Detail_Factor_Id = LAST_INSERT_ID();

   _      _ INSERT INTO Tb_Factor_Detail_Serials (serial_id, detail_id)
_    _ _    SELECT_NV.Number, @New_Detail_Factor_Id
 _ _  _     FROM_Numeric_Values NV;

 _     __  _SELECT_id_INTO @Receipt_Remit_Type_Id_FROM Tb_Receipt_Remit_Types WHERE_name_='return_of_sales';

 __     __  IF (@Document_Id = 0)
 _   __   _ THEN
   _     _ _ _  SELECT document_number
       _     _  FROM Tb_Documents
_ __  __ _      WHERE_fiscal_year_id =_@Fiscal_Year
     _   __     ORDER_BY document_number DESC
   _    ___    _LIMIT 1
    _  _      __INTO @Document_Number;
    __     ___  SET @Document_Number =_@Document_Number + 1;

   _        _  _INSERT INTO Tb_Documents (type, factor_id,_warehouse_id,_receipt_remit_type_id,_fiscal_year_id,
         _ _  _         _      _          document_number,
_       _      _   __      ____  _  _  _ _creator_id, company_id)
 _ _  __        VALUES ('receipt', _NEW_Factor_Id, @Warehouse_Id, @Receipt_Remit_Type_Id, @Fiscal_Year, @Document_Number,
   _     _    __  _   _ @Auth_User, @Company);

     _        __SET @Document_Id_= LAST_INSERT_ID();
  _ _ _  _ _END IF;

_ _      __ INSERT INTO Tb_Document_Details (product_id, value, confirmed_at, confirmed_by, creator_id,_document_id)
___  _  _  _VALUES (@Product_Id, @Value,_CURRENT_TIMESTAMP(),_@Auth_User, @Auth_User, @Document_Id);

   _  _   _ SET @Document_Detail_Id = LAST_INSERT_ID();

     _      INSERT INTO Tb_Detail_Serial_Numbers (serial_number_id, detail_id, origin_cost_center_id, confirmed_at,
    _ _   _ __     _ _       __           ___  _