Despues: save_document

mail@pastecode.io avatar
unknown
ruby
2 years ago
5.5 kB
6
Indexable
Never
def save_document
    render_error_with_mssgs(@error_mssgs) and return if @error_mssgs.present?

    save_translation_document and return if @type_document == 'is_translate'

      products = get_array_products(@array_products, @array_quantitys, @array_observations, @array_product_names, @delete_products, @array_parent_types, @array_number_elements)
      movement = MovementProduct.save_movement_activity(1, @store_id, @arrival_store, products, @observations, @number_document, 0, nil, current_user, @serie)
      if movement
        document = SaleDocument.new(user_id: current_user.id, store_id: @store_id, vehicle_id: @vehicle_id, driver_id: @driver_id,products: products, number_document: @number_document, translate_store: true, type_document: 0, startin_address: @startin_address, arrival_address: @arrival_address, observations: @observations, status: 2, date_translate: @date_translate, branch_office_id: @branch_office_id, serie: @serie, date_document: @date_document)
        if document.save
          render_success_mssg('Traslado guardado correctamente.', "/show_document/#{document.id}")
        else
          render_generic_error
        end
      else
        render_generic_error
      end

    save_guia_document and return if @type_document == '0'

      products = get_array_products(@array_products, @array_quantitys, @array_observations, @array_product_names, @delete_products, @array_parent_types, @array_number_elements)
      movement = MovementProduct.save_movement_activity(2, @store_id, '', products, @observations, @number_document, 0, nil, current_user, @serie)
       if movement
         document = SaleDocument.new(user_id: current_user.id, store_id: @store_id, vehicle_id: @vehicle_id, driver_id: @driver_id, number_document: @number_document, products: products, translate_store: false, type_document: 0, startin_address: @startin_address, arrival_address: @arrival_address, observations: @observations, status: 0, reason: @reason, customer_id: @customer_id, date_translate: @date_translate, branch_office_id: @branch_office_id, serie: @serie, date_document: @date_document, seller_id:@seller,user_area: @user_area)
         if document.save
           # Requerimiento de cambio de facturas entre sucursal Percamar y Almar
           guide_percamar = document.generate_guide_percamar(@guide_ids, products, @date_document) if current_user.branch_office_id == 5
           guide_percamar.translate_products_to_almar unless guide_percamar.nil?
           #####################################################################
           type_invoice = document.customer.identity_document.length == 11 ? 'factura' : 'boleta'
           render_location = @reason == 'Venta' ? "/new_document?type_invoice=#{type_invoice}" : "/show_document/#{document.id}"
           render_success_mssg('Guía guardada correctamente.', render_location)
         else
           render_generic_error
         end
       end

    save_boleta_and_factura_document and return if @type_document == '1' || @type_document == '2' # Boleta o factura

      if @customer_invoicing.present?
        products = get_products_invoicing(@guide_ids, @products_invoicing, @quantitys_invoicing, @array_obs_with_g ,@unit_price, @discounts, @sale_values, @price_without_igv, @consider_products, @product_names, @invoice_parent_types)
        pay_mode_customer = get_pay_mode_customer(@pay_mode_customer, @date_expiration, @coin, params[:date_of_issue].to_datetime)
        data_document = {
          purchase_order: @purchase_order,
          user_id: current_user.id,
          number_document: @number_document,
          translate_store: false,
          type_document: @type_document.to_i,
          status: 0,
          customer_id: @customer_invoicing,
          startin_address: @startin_address,
          arrival_address: @arrival_address,
          observations: @observations,
          pay_mode_customer: pay_mode_customer,
          coin: @coin,
          products: products,
          sale_value: @sum_sale_value,
          discount_total: @discount_total,
          percentage_discount: @percentage_discount,
          net_value: @net_value,
          igv_total: @igv_total,
          total_value: @total_value,
          total_in_letter: @total_in_letter,
          date_translate: @date_translate,
          branch_office_id: @branch_office_id,
          date_document: @date_document,
          serie: @serie,
          pay_mode: @pay_mode,
          expiration_date: @expiration_date,
          term_pay: @term_pay,
          document_fees_attributes: @document_fees
        }
        document = SaleDocument.new(data_document)
        if document.save
          name_document = @type_document == '1' ? 'Factura' : 'Boleta'
          document.save_accounting_movement
          if params[:send_factesol].present?
            notice = document.send_document_factesol ? "#{name_document} guardada correctamente y enviada a factesol." : "#{name_document} guardada correctamente, con error al enviar a factesol."
            document.emitida!
          else
            notice = "#{name_document} guardada correctamente."
          end
          render_success_mssg(notice, "/show_document/#{document.id}")
        else
          render_custom_error(document.errors.full_messages.join(' '))
        end
      else
        render js: 'swal("Alerta","Por favor seleccione cliente.", "warning");'
      end
    render_generic_error
  end