Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workbook's close method seems to be appending a timestamp at the end of my file #89

Open
Hey-Feres opened this issue Nov 2, 2022 · 2 comments

Comments

@Hey-Feres
Copy link

When I execute my code it returns me the following error:

Errno::ENOENT (No such file or directory @ rb_sysopen - /Users/brunoferesvillela/Developer/Ruby/ReportsAutomator/reports/my_cool_store/Novembro_Daily_Revenue_Report.xlsx20221102-21189-qq6z65)

Here an example of my code

require 'write_xlsx'

# Example of Execution
# data = [
#         {:seller_name=>"John", :items_quantity=>9, :completed_sales=>3, :revenue=>"5.381,00"},
#         {:seller_name=>"Samantha", :items_quantity=>6, :completed_sales=>4, :revenue=>"4.134,00"},
#         {:seller_name=>"Ben", :items_quantity=>5, :completed_sales=>5, :revenue=>"2.695,00"},
#         {:seller_name=>"Maria", :items_quantity=>4, :completed_sales=>3, :revenue=>"1.937,20"},
#         {:seller_name=>"Alessandra", :items_quantity=>1, :completed_sales=>1, :revenue=>"699,00"}
#        ]
# options = {
#   daily_revenue_data: data,
#   day_to_insert_data: 1
# }
# ReportGenerator::DailyRevenueReportGenerator.new('my_cool_store', options).call

module ReportGenerator
	class DailyRevenueReportGenerator < ApplicationReportGenerator
		GREEN 			= '#52EAB0'
		RED 				= '#F28E9A'
		PURPLE 			= '#4C0646'
		DARK_PURPLE = '#2F074C'
		DARK_GREEN 	= '#053523'
		DARK_RED 		= '#3D050C'
		WHITE 			= '#FFFFFF'

		SUBHEADERS = [
			{range: 'A2:B2', title: "Dia do Mês"},
			{range: 'C2:D2', title: "Dia da Semana"},
			{range: 'E2:F2', title: "Esperado"},
			{range: 'G2:H2', title: "Recebido"},
			{range: 'I2:J2', title: "Saldo"},
			{range: 'K2:L2', title: "Saldo Devedor"},
			{range: 'M2:N2', title: "PA"}
		]

		def initialize(store_name, options = {})
			@store_name = store_name
			@file = define_file_path
			@daily_revenue_data = options[:daily_revenue_data] || RevenueScrapper.new.daily_revenue
			@day_to_insert_data = options[:day_to_insert_data] || current_day
		end

		def call
			# Initialize Workbook
			initialize_workbook
			# Setup Cell Formatting
			setup_formatting
			# Insert New Report Base Structure
			insert_new_report_base_structure
			# Insert Current Day Data
			insert_current_day_report_data
			# Close
			@workbook.close
		end

		private

		def current_day
			Time.now.day
		end

		def current_month
			Time.now.month
		end

		def current_year
			Time.now.year
		end

		def month_name
			Date::MONTHNAMES[current_month]
		end

		def last_day_of_month
			Time.now.end_of_month.day
		end

		def initialize_workbook
			@workbook = WriteXLSX.new @file
			@worksheet = @workbook.add_worksheet
		end

		def insert_current_day_report_data
			# Revenue Cell
			revenue = @daily_revenue_data.map{|d| d[:revenue].gsub('.', '').to_d}.sum
			@worksheet.merge_range("G#{@day_to_insert_data+2}:H#{@day_to_insert_data+2}", revenue, @f_currency)
			# Balance Cell
			goal = "E#{@day_to_insert_data+2}"
			revenue = "G#{@day_to_insert_data+2}"
			@worksheet.merge_range("I#{@day_to_insert_data+2}:J#{@day_to_insert_data+2}", "=#{revenue} - #{goal}", @f_currency)
			# Debit Balance Cell
			balance_cell = "I#{@day_to_insert_data+2}"
			@worksheet.merge_range("K#{@day_to_insert_data+2}:L#{@day_to_insert_data+2}", "=IF(#{balance_cell}<0,#{balance_cell},0)", @f_currency)
			@worksheet.conditional_formatting("K#{@day_to_insert_data+2}:L#{@day_to_insert_data+2}", { type: 'cell', criteria: '!=', value: 0, format: @f_currency_red })
			@worksheet.conditional_formatting("K#{@day_to_insert_data+2}:L#{@day_to_insert_data+2}", { type: 'cell', criteria: '==', value: 0, format: @f_currency_green })
			# PA Cell
			pa = @daily_revenue_data.map{|d| d[:items_quantity]}.sum.to_f / @daily_revenue_data.map{|d| d[:completed_sales]}.sum.to_f
			@worksheet.merge_range("M#{@day_to_insert_data+2}:N#{@day_to_insert_data+2}", pa, @f_default)
		end

		def insert_new_report_base_structure
			# Add Header Month Name
			@worksheet.merge_range('A1:N1', month_name, @f_header)
			# Add SubHeaders
			SUBHEADERS.each do |subheader|
				@worksheet.merge_range(subheader[:range], subheader[:title], @f_subheader)
			end
			# Add Month Days
			(1..last_day_of_month).each_with_index do |day, index|
				@worksheet.merge_range("A#{index+3}:B#{index+3}", day, @f_default)
			end
			# Add Week Days
			(1..last_day_of_month).each_with_index do |day, index|
				date = Date.new(current_year, current_month, day)
				@worksheet.merge_range("C#{index+3}:D#{index+3}", I18n.t(date.strftime("%A")), @f_default)
			end
			# Add Goal
			goals = Goal.current_month
			goals.each_with_index do |goal, index|
				@worksheet.merge_range("E#{index+3}:F#{index+3}", goal.value, @f_currency)
			end
		end

		def setup_formatting
			@f_header    			= @workbook.add_format(bold: 1, align: 'center', size: 15, bg_color: DARK_PURPLE, color: WHITE)
			@f_subheader 			= @workbook.add_format(align: 'center', size: 13, bg_color: PURPLE, color: WHITE)
			@f_default 				= @workbook.add_format(align: 'center', size: 13)
			@f_currency 			= @workbook.add_format(align: 'center', size: 13, num_format: 'R$0.00')
			@f_currency_green = @workbook.add_format(align: 'center', size: 13, num_format: 'R$0.00', bg_color: GREEN, color: DARK_GREEN)
			@f_currency_red 	= @workbook.add_format(align: 'center', size: 13, num_format: 'R$0.00', bg_color: RED, color: DARK_RED)
		end

		def report_name
			"#{month_name}_Daily_Revenue_Report.xlsx"
		end

		def path
			Rails.root.join('reports', @store_name)
		end

		def define_file_path
			"#{path}/#{report_name}"
		end
	end
end
@cxn03651
Copy link
Owner

cxn03651 commented Nov 3, 2022

Thank you for using write_xlsx.

Can you see a line number of the source file where this exception raises around this error message ?

@Hey-Feres
Copy link
Author

Thanks for your answer!!
The error seems to being triggered at line 897 in workbook.rb.
Executing ZipFileUtils.zip("#{tempdir}", filename) with tempdir = "/var/folders/4q/_p7sshsj5m7dkdlz82ttllbm0000gn/T/d8c0ed7aa0eb67b148a0a9bc9d4aa756" and filename = "/Users/brunoferesvillela/Developer/Ruby/ReportsAutomator/reports/store_name/November_Daily_Revenue_Report.xlsx"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants