diff --git a/app/controllers/estimate_controller.rb b/app/controllers/estimate_controller.rb index f58f6bd..c9d01d9 100644 --- a/app/controllers/estimate_controller.rb +++ b/app/controllers/estimate_controller.rb @@ -8,71 +8,71 @@ # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class EstimateController < ApplicationController - include AuthHelper + + before_action :require_user, unless: -> { session[:token].nil? } + skip_before_action :verify_authenticity_token, :check_if_login_required, unless: -> { session[:token].nil? } + before_action :load_estimate, only: [:show, :doc] - before_action :require_user, unless: proc {|c| session[:token].nil? } - skip_before_action :verify_authenticity_token, :check_if_login_required, unless: proc {|c| session[:token].nil? } - - def get_estimate - log "Searching for estimate with params: #{params.inspect}" - - e = Estimate.find_by_doc_number(params[:search]) if params[:search] - e = Estimate.find_by_id(params[:id]) if params[:id] - - # Force sync for estimate by doc number if not found - if e.nil? && params[:search] - begin - Estimate.sync_by_doc_number(params[:search]) - e = Estimate.find_by_doc_number(params[:search]) - rescue - log "Estimate.find_by_doc_number failed" - end - end - - # Force sync for estimate by id if not found - if e.nil? && params[:id] - begin - Estimate.sync_by_id(params[:id]) - e = Estimate.find_by_id(params[:id]) - rescue - log "Estimate.find_by_id failed" - end - end - - return e - end - - # - # Downloads and forwards the estimate pdf - # - def show - estimate = get_estimate - - begin - send_data estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", disposition: :inline, type: "application/pdf" - rescue - redirect_to :back, flash: { error: I18n.t(:notice_estimate_not_found) } - end - end - - # - # Downloads estimate by document number - # + # Displays the estimate PDF in the browser or redirects with an error if not found. def doc - estimate = get_estimate - - begin - send_data estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", disposition: :inline, type: "application/pdf" - rescue - redirect_to :back, flash: { error: I18n.t(:notice_estimate_not_found) } - end + render_pdf(@estimate) + end + + # Displays the estimate PDF in the browser or redirects with an error if not found. + def show + render_pdf(@estimate) end private + # Loads the estimate based on ID or doc number, with a fallback to sync if not found locally. + def load_estimate + log "Attempting to load Estimate with params: #{params.inspect}" + @estimate = find_estimate || sync_and_find_estimate + + unless @estimate + redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_estimate_not_found) } + end + end + + # Attempts to find the estimate locally by ID or doc number. + def find_estimate + return Estimate.find_by(doc_number: params[:search]) if params[:search].present? + return Estimate.find_by(id: params[:id]) if params[:id].present? + end + + # If the estimate is not found locally, attempts to sync it from the source and find it again. + def sync_and_find_estimate + + if params[:search].present? + log "Estimate #{params[:search]} not found locally. Syncing by doc number." + Estimate.sync_by_doc_number(params[:search]) + return Estimate.find_by(doc_number: params[:search]) + end + + if params[:id].present? + log "Estimate #{params[:id]} not found locally. Syncing by ID." + Estimate.sync_by_id(params[:id]) + return Estimate.find_by(id: params[:id]) + end + + nil + rescue StandardError => e + log "Estimate sync failed: #{e.message}" + nil + end + + # Renders the estimate PDF or redirects with an error if rendering fails. + def render_pdf(estimate) + send_data( estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", disposition: :inline, type: "application/pdf" ) + rescue StandardError => e + log "PDF render failed for Estimate #{estimate&.id}: #{e.message}" + redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_estimate_not_found) } + end + + # Logs messages with a consistent prefix for easier debugging. def log(msg) Rails.logger.info "[EstimateController] #{msg}" end - -end +end \ No newline at end of file