From b025cc2cfd2b10df84cc1cee458efa4eef108752 Mon Sep 17 00:00:00 2001 From: Aung Ye Kyaw Date: Thu, 24 Oct 2019 05:40:56 +0000 Subject: [PATCH 01/28] Update Gemfile Ruby version change 2.3.1 to 2.4.1 (may be someone changed) --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 1a15a61a..66c149ef 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.3.1' +ruby '2.4.1' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" From 0d8b4f55699b5c3f7f37bbc9092f36d8db4c82c0 Mon Sep 17 00:00:00 2001 From: Myat Zin Wai Maw Date: Thu, 24 Oct 2019 18:16:43 +0630 Subject: [PATCH 02/28] print gap fixed --- Gemfile | 2 +- Gemfile.lock | 2 +- app/pdf/order_item_customise_pdf.rb | 2 +- app/pdf/order_item_pdf.rb | 2 +- app/pdf/order_item_slim_pdf.rb | 2 +- app/pdf/order_item_star_pdf.rb | 2 +- app/pdf/order_set_item_customise_pdf.rb | 2 +- app/pdf/order_set_item_pdf.rb | 2 +- config/initializers/action_controller.rb | 10 +++++----- config/puma.rb | 22 +++++++++++----------- 10 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 1a15a61a..66c149ef 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.3.1' +ruby '2.4.1' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" diff --git a/Gemfile.lock b/Gemfile.lock index 393a2ac8..3f9a8a09 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -371,7 +371,7 @@ DEPENDENCIES whenever RUBY VERSION - ruby 2.3.1p112 + ruby 2.4.1p111 BUNDLED WITH 2.0.2 diff --git a/app/pdf/order_item_customise_pdf.rb b/app/pdf/order_item_customise_pdf.rb index 850a9574..b509a01a 100644 --- a/app/pdf/order_item_customise_pdf.rb +++ b/app/pdf/order_item_customise_pdf.rb @@ -129,7 +129,7 @@ class OrderItemCustomisePdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/app/pdf/order_item_pdf.rb b/app/pdf/order_item_pdf.rb index 58937093..ed079aeb 100755 --- a/app/pdf/order_item_pdf.rb +++ b/app/pdf/order_item_pdf.rb @@ -130,7 +130,7 @@ class OrderItemPdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/app/pdf/order_item_slim_pdf.rb b/app/pdf/order_item_slim_pdf.rb index 33e1e314..3e39644f 100755 --- a/app/pdf/order_item_slim_pdf.rb +++ b/app/pdf/order_item_slim_pdf.rb @@ -130,7 +130,7 @@ class OrderItemSlimPdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/app/pdf/order_item_star_pdf.rb b/app/pdf/order_item_star_pdf.rb index 711fb81e..2686efd7 100644 --- a/app/pdf/order_item_star_pdf.rb +++ b/app/pdf/order_item_star_pdf.rb @@ -130,7 +130,7 @@ class OrderItemStarPdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/app/pdf/order_set_item_customise_pdf.rb b/app/pdf/order_set_item_customise_pdf.rb index ce3e654f..2e1d0a2c 100644 --- a/app/pdf/order_set_item_customise_pdf.rb +++ b/app/pdf/order_set_item_customise_pdf.rb @@ -144,7 +144,7 @@ class OrderSetItemCustomisePdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/app/pdf/order_set_item_pdf.rb b/app/pdf/order_set_item_pdf.rb index a02dc3e2..09cdc6db 100755 --- a/app/pdf/order_set_item_pdf.rb +++ b/app/pdf/order_set_item_pdf.rb @@ -143,7 +143,7 @@ class OrderSetItemPdf < Prawn::Document # add option y_position = cursor - bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + bounding_box([0,y_position], :width => self.item_width) do text "#{options}", :size => self.item_font_size,:align => :left end end diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 85f38ec5..205c330a 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -21,11 +21,11 @@ class ActionController::Base end else # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end + if check_license + current_license(ENV["SX_PROVISION_URL"]) + else + redirect_to activate_path + end end end diff --git a/config/puma.rb b/config/puma.rb index f0013d25..d24425ae 100755 --- a/config/puma.rb +++ b/config/puma.rb @@ -1,11 +1,11 @@ -# application_path="#{File.expand_path("../..", __FILE__)}" -# directory application_path -# #environment ENV.fetch("RAILS_ENV") { "production" } -# environment "production" -# pidfile "#{application_path}/tmp/puma/pid" -# state_path "#{application_path}/tmp/puma/state" -# stdout_redirect "#{application_path}/log/puma.stdout.log", "#{application_path}/log/puma.stderr.log" -# port ENV.fetch("PORT") { 62158 } -# workers 2 -# preload_app! -# threads 1,1 +application_path="#{File.expand_path("../..", __FILE__)}" +directory application_path +#environment ENV.fetch("RAILS_ENV") { "production" } +environment "production" +pidfile "#{application_path}/tmp/puma/pid" +state_path "#{application_path}/tmp/puma/state" +stdout_redirect "#{application_path}/log/puma.stdout.log", "#{application_path}/log/puma.stderr.log" +port ENV.fetch("PORT") { 62158 } +workers 2 +preload_app! +threads 1,1 From 0be67f4f121a9a1e850a01cd1432676e4c3d3ff3 Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 12:33:21 +0630 Subject: [PATCH 03/28] SX cap production setup files --- lib/capistrano/substitute_strings.rb | 12 +++++++ lib/capistrano/tasks/check_revision.cap | 14 ++++++++ .../tasks/compile_assets_locally.cap | 17 ++++++++++ lib/capistrano/tasks/logs.cap | 14 ++++++++ lib/capistrano/tasks/monit.cap | 10 ++++++ lib/capistrano/tasks/nginx.cap | 22 +++++++++++++ lib/capistrano/tasks/restart.cap | 10 ++++++ lib/capistrano/tasks/run_tests.cap | 18 ++++++++++ lib/capistrano/tasks/setup_config.cap | 33 +++++++++++++++++++ lib/capistrano/template.rb | 32 ++++++++++++++++++ 10 files changed, 182 insertions(+) create mode 100644 lib/capistrano/substitute_strings.rb create mode 100644 lib/capistrano/tasks/check_revision.cap create mode 100644 lib/capistrano/tasks/compile_assets_locally.cap create mode 100644 lib/capistrano/tasks/logs.cap create mode 100644 lib/capistrano/tasks/monit.cap create mode 100644 lib/capistrano/tasks/nginx.cap create mode 100644 lib/capistrano/tasks/restart.cap create mode 100644 lib/capistrano/tasks/run_tests.cap create mode 100644 lib/capistrano/tasks/setup_config.cap create mode 100644 lib/capistrano/template.rb diff --git a/lib/capistrano/substitute_strings.rb b/lib/capistrano/substitute_strings.rb new file mode 100644 index 00000000..d4b24432 --- /dev/null +++ b/lib/capistrano/substitute_strings.rb @@ -0,0 +1,12 @@ +# we often want to refer to variables which +# are defined in subsequent stage files. This +# let's us use the {{var}} to represent fetch(:var) +# in strings which are only evaluated at runtime. + +def sub_strings(input_string) + output_string = input_string + input_string.scan(/{{(\w*)}}/).each do |var| + output_string.gsub!("{{#{var[0]}}}", fetch(var[0].to_sym)) + end + output_string +end diff --git a/lib/capistrano/tasks/check_revision.cap b/lib/capistrano/tasks/check_revision.cap new file mode 100644 index 00000000..72b4aa12 --- /dev/null +++ b/lib/capistrano/tasks/check_revision.cap @@ -0,0 +1,14 @@ +namespace :deploy do + desc "checks whether the currently checkout out revision matches the + remote one we're trying to deploy from" + task :check_revision do + branch = fetch(:branch) + unless `git rev-parse HEAD` == `git rev-parse #{branch}` + puts "WARNING: HEAD is not the same as #{branch}" + puts "Run `git push` to sync changes or make sure you've" + puts "checked out the branch: #{branch} as you can only deploy" + puts "if you've got the target branch checked out" + exit + end + end +end diff --git a/lib/capistrano/tasks/compile_assets_locally.cap b/lib/capistrano/tasks/compile_assets_locally.cap new file mode 100644 index 00000000..e6264e81 --- /dev/null +++ b/lib/capistrano/tasks/compile_assets_locally.cap @@ -0,0 +1,17 @@ +namespace :deploy do + desc "compiles assets locally then rsyncs" + task :compile_assets_locally do + run_locally do + execute "RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:precompile" + end + on roles(:app) do |role| + run_locally do + execute"rsync -av ./public/assets/ #{role.user}@#{role.hostname}:#{release_path}/public/assets/;" + end + sudo "chmod -R 755 #{release_path}/public/assets/" + end + run_locally do + execute "rm -rf ./public/assets" + end + end +end diff --git a/lib/capistrano/tasks/logs.cap b/lib/capistrano/tasks/logs.cap new file mode 100644 index 00000000..7bbfac2e --- /dev/null +++ b/lib/capistrano/tasks/logs.cap @@ -0,0 +1,14 @@ +namespace :logs do + task :tail, :file do |t, args| + if args[:file] + on roles(:app) do + execute "tail -f #{shared_path}/log/#{args[:file]}.log" + end + else + puts "please specify a logfile e.g: 'rake logs:tail[logfile]" + puts "will tail 'shared_path/log/logfile.log'" + puts "remember if you use zsh you'll need to format it as:" + puts "rake 'logs:tail[logfile]' (single quotes)" + end + end +end diff --git a/lib/capistrano/tasks/monit.cap b/lib/capistrano/tasks/monit.cap new file mode 100644 index 00000000..5dbac6a2 --- /dev/null +++ b/lib/capistrano/tasks/monit.cap @@ -0,0 +1,10 @@ +namespace :monit do + %w(start stop restart).each do |task_name| + desc "#{task_name} Monit" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "service monit #{task_name}" + end + end + end +end diff --git a/lib/capistrano/tasks/nginx.cap b/lib/capistrano/tasks/nginx.cap new file mode 100644 index 00000000..dfadd5c6 --- /dev/null +++ b/lib/capistrano/tasks/nginx.cap @@ -0,0 +1,22 @@ +namespace :nginx do + %w(start stop restart reload).each do |task_name| + desc "#{task } Nginx" + task task_name do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/nginx #{task_name}" + end + end + end + + desc "Remove default Nginx Virtual Host" + task "remove_default_vhost" do + on roles(:app) do + if test("[ -f /etc/nginx/sites-enabled/default ]") + sudo "rm /etc/nginx/sites-enabled/default" + puts "removed default Nginx Virtualhost" + else + puts "No default Nginx Virtualhost to remove" + end + end + end +end diff --git a/lib/capistrano/tasks/restart.cap b/lib/capistrano/tasks/restart.cap new file mode 100644 index 00000000..ff8c028f --- /dev/null +++ b/lib/capistrano/tasks/restart.cap @@ -0,0 +1,10 @@ +namespace :deploy do + desc 'Commands for unicorn application' + %w(start stop force-stop restart upgrade reopen-logs).each do |command| + task command.to_sym do + on roles(:app), in: :sequence, wait: 5 do + sudo "/etc/init.d/unicorn_#{fetch(:full_app_name)} #{command}" + end + end + end +end diff --git a/lib/capistrano/tasks/run_tests.cap b/lib/capistrano/tasks/run_tests.cap new file mode 100644 index 00000000..32f26f9c --- /dev/null +++ b/lib/capistrano/tasks/run_tests.cap @@ -0,0 +1,18 @@ +namespace :deploy do + desc "Runs test before deploying, can't deploy unless they pass" + task :run_tests do + test_log = "log/capistrano.test.log" + tests = fetch(:tests) + tests.each do |test| + puts "--> Running tests: '#{test}', please wait ..." + unless system "bundle exec rspec #{test} > #{test_log} 2>&1" + puts "--> Tests: '#{test}' failed. Results in: #{test_log} and below:" + system "cat #{test_log}" + exit; + end + puts "--> '#{test}' passed" + end + puts "--> All tests passed" + system "rm #{test_log}" + end +end diff --git a/lib/capistrano/tasks/setup_config.cap b/lib/capistrano/tasks/setup_config.cap new file mode 100644 index 00000000..81ce88d9 --- /dev/null +++ b/lib/capistrano/tasks/setup_config.cap @@ -0,0 +1,33 @@ +namespace :deploy do + task :setup_config do + on roles(:app) do + # make the config dir + execute :mkdir, "-p #{shared_path}/config" + full_app_name = fetch(:full_app_name) + + # config files to be uploaded to shared/config, see the + # definition of smart_template for details of operation. + # Essentially looks for #{filename}.erb in deploy/#{full_app_name}/ + # and if it isn't there, falls back to deploy/#{shared}. Generally + # everything should be in deploy/shared with params which differ + # set in the stage files + config_files = fetch(:config_files) + config_files.each do |file| + smart_template file + end + + # which of the above files should be marked as executable + executable_files = fetch(:executable_config_files) + executable_files.each do |file| + execute :chmod, "+x #{shared_path}/config/#{file}" + end + + # symlink stuff which should be... symlinked + symlinks = fetch(:symlinks) + + symlinks.each do |symlink| + sudo "ln -nfs #{shared_path}/config/#{symlink[:source]} #{sub_strings(symlink[:link])}" + end + end + end +end diff --git a/lib/capistrano/template.rb b/lib/capistrano/template.rb new file mode 100644 index 00000000..07bd0be3 --- /dev/null +++ b/lib/capistrano/template.rb @@ -0,0 +1,32 @@ +# will first try and copy the file: +# config/deploy/#{full_app_name}/#{from}.erb +# to: +# shared/config/to +# if the original source path doesn exist then it will +# search in: +# config/deploy/shared/#{from}.erb +# this allows files which are common to all enviros to +# come from a single source while allowing specific +# ones to be over-ridden +# if the target file name is the same as the source then +# the second parameter can be left out +def smart_template(from, to=nil) + to ||= from + full_to_path = "#{shared_path}/config/#{to}" + if from_erb_path = template_file(from) + from_erb = StringIO.new(ERB.new(File.read(from_erb_path)).result(binding)) + upload! from_erb, full_to_path + info "copying: #{from_erb} to: #{full_to_path}" + else + error "error #{from} not found" + end +end + +def template_file(name) + if File.exist?((file = "config/deploy/#{fetch(:full_app_name)}/#{name}.erb")) + return file + elsif File.exist?((file = "config/deploy/shared/#{name}.erb")) + return file + end + return nil +end From bfc697376286b61049872eee350da51778b4cf27 Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 13:00:40 +0630 Subject: [PATCH 04/28] Gem lock update --- Gemfile.lock | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0db34edf..7928786c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,7 +62,7 @@ GEM builder (3.2.3) byebug (11.0.1) cancancan (1.17.0) - capistrano (3.10.1) + capistrano (3.11.2) airbrussh (>= 1.0.0) i18n rake (>= 10.0.0) @@ -72,15 +72,11 @@ GEM capistrano-rails (1.4.0) capistrano (~> 3.1) capistrano-bundler (~> 1.1) -<<<<<<< HEAD capistrano3-puma (3.1.1) capistrano (~> 3.7) capistrano-bundler puma (~> 3.4) - carrierwave (1.2.1) -======= carrierwave (1.3.1) ->>>>>>> b025cc2cfd2b10df84cc1cee458efa4eef108752 activemodel (>= 4.0.0) activesupport (>= 4.0.0) mime-types (>= 1.16) @@ -107,8 +103,8 @@ GEM factory_girl_rails (4.9.0) factory_girl (~> 4.9.0) railties (>= 3.0.0) - faker (2.5.0) - i18n (~> 1.6.0) + faker (2.6.0) + i18n (>= 1.6, < 1.8) ffi (1.11.1) filterrific (5.2.1) font-awesome-rails (4.7.0.5) @@ -148,7 +144,7 @@ GEM listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - loofah (2.3.0) + loofah (2.3.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -231,23 +227,23 @@ GEM chunky_png (~> 1.0) rqrcode_core (~> 0.1.0) rqrcode_core (0.1.0) - rspec-core (3.8.2) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.5) + rspec-core (3.9.0) + rspec-support (~> 3.9.0) + rspec-expectations (3.9.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.2) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-rails (3.9.0) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.3) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.0) ruby-ole (1.2.12.2) rubyzip (1.0.0) sass (3.7.4) @@ -273,10 +269,9 @@ GEM simple_form (5.0.1) actionpack (>= 5.0) activemodel (>= 5.0) - spreadsheet (1.2.4) + spreadsheet (1.2.5) ruby-ole (>= 1.0) - spring (2.0.2) - activesupport (>= 4.2) + spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) @@ -380,7 +375,7 @@ DEPENDENCIES whenever RUBY VERSION - ruby 2.3.1p112 + ruby 2.4.1p111 BUNDLED WITH 2.0.2 From 77ed76c36864cadee1d17e1adad99eb73a08da08 Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 13:04:12 +0630 Subject: [PATCH 05/28] Update Gemfile to use ruby 2.6.5 --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 73701b6c..9f51df74 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.4.1' +ruby '2.6.5' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" From af3efbe607e68a4fe378ff40c363a2e5d151f88e Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 13:06:45 +0630 Subject: [PATCH 06/28] Update Gemfile to use ruby 2.6.5 --- Gemfile | 2 +- Gemfile.lock | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 9f51df74..c489fc3e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.6.5' + git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" diff --git a/Gemfile.lock b/Gemfile.lock index 7928786c..e28d4fd6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -374,8 +374,5 @@ DEPENDENCIES web-console (>= 3.3.0) whenever -RUBY VERSION - ruby 2.4.1p111 - BUNDLED WITH 2.0.2 From dc1f83ddc9b683c6f09063e442915c94a883ab0b Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 13:14:49 +0630 Subject: [PATCH 07/28] remove cups installation for cloud in Gemfile --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index c489fc3e..143907b6 100644 --- a/Gemfile +++ b/Gemfile @@ -44,7 +44,7 @@ gem 'mini_magick' gem 'jquery-fileupload-rails', '~> 0.4.7' #Report and Printing gems -gem 'cups' +#gem 'cups' #remove for cloud installation gem 'prawn' gem 'prawn-table' gem 'prawn-qrcode' From 2e2117a8203764bd8c85dc96905eccc85fc37b8b Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Fri, 25 Oct 2019 13:20:26 +0630 Subject: [PATCH 08/28] Remove cups for cloud --- Gemfile.lock | 2 -- 1 file changed, 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e28d4fd6..db4f94aa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,7 +93,6 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.5) - cups (0.1.10) database_cleaner (1.7.0) diff-lcs (1.3) erubi (1.9.0) @@ -329,7 +328,6 @@ DEPENDENCIES carrierwave (~> 1.0) chartkick coffee-rails (~> 4.2) - cups database_cleaner factory_girl_rails (~> 4.0) faker From cec212bb394dae01f0e7a21946188e27efe0ef8a Mon Sep 17 00:00:00 2001 From: Nweni Date: Tue, 29 Oct 2019 10:06:00 +0630 Subject: [PATCH 09/28] dump --- dump.rdb | Bin 1864 -> 1864 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/dump.rdb b/dump.rdb index e4cb2863ade5864c70f85f677175fe6da53f30c9..42a27347f402a7cb6e6710a1d8f90f36e350dfb7 100644 GIT binary patch delta 100 zcmX@XcY<$%0rNul4HJz$Cg-!UPX5AbG`W*adg3Y3$?j}wlU3McC%zQD@1@%}v)pp@=pL3YWBo8*DQqP#|C2N?dFKRMjN((!u- E0OzD65&!@I delta 116 zcmX@XcY<$%0khSYWfP4(Ca-3cU^CY>GCRO9`2d^bWOjCW9`53j#1gBt#LS%3l+6zr zAFxc`z$nUMYyegwK3SDfcH%Ud$y|)m6VI|we5^XTpHXV!2HD9@jOIXvQj-s|NldJk Uo_w86ob&(G?^>_Q<2Q8z02jk4^#A|> From d4a90d6563241c995846c8325532a1ce1c9bbf04 Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Tue, 29 Oct 2019 10:38:50 +0630 Subject: [PATCH 10/28] docker changes --- config/deploy.rb | 1 + docker-compose.yml | 60 ++++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/config/deploy.rb b/config/deploy.rb index 6dba4c2d..05bba77d 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -83,6 +83,7 @@ namespace :deploy do before :deploy, "deploy:run_tests" # compile assets locally then rsync after 'deploy:symlink:shared', 'deploy:compile_assets_locally' + Rake::Task["deploy:assets:precompile"].clear_actions after :finishing, 'deploy:cleanup' diff --git a/docker-compose.yml b/docker-compose.yml index d1a961f7..ab61383d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,31 +1,33 @@ version: '3' services: - code2lab: - build: - context: . - dockerfile: ./Dockerfile - links: - - redis - volumes: - - .:/sxrestaurant - env_file: - - .code2lab.env - ports: - - '8082:62158' - environment: - - REDIS_URL=redis://redis:6379/0 - sidekiq: - build: . - command: bundle exec sidekiq -C config/sidekiq.yml - links: - - redis - volumes: - - .:/sxrestaurant - environment: - - REDIS_URL=redis://redis:6379/0 - redis: - image: redis - ports: - - '6380:6379' - volumes: - - ../data/redis:/data \ No newline at end of file + code2lab: + build: + context: . + dockerfile: ./Dockerfile + links: + - redis + volumes: + - .:/sxrestaurant + - "/etc/timezone:/etc/timezone:ro" + - "/etc/localtime:/etc/localtime:ro" + env_file: + - .code2lab.env + ports: + - '62160:62158' + environment: + - REDIS_URL=redis://172.17.0.1:6380/0 + sidekiq: + build: . + command: bundle exec sidekiq -C config/sidekiq.yml + links: + - redis + volumes: + - .:/sxrestaurant + environment: + - REDIS_URL=redis://172.17.0.1:6380/0 + redis: + image: redis + ports: + - '6380:6379' + volumes: + - ../data/redis:/data From 83e7f22b13954d9a92d074c397c2faadb38b32dc Mon Sep 17 00:00:00 2001 From: Min Zeya Phyo Date: Tue, 29 Oct 2019 14:15:43 +0630 Subject: [PATCH 11/28] menu form is_ordering and is_active to data type boolean --- app/views/settings/menus/_form.html.erb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/views/settings/menus/_form.html.erb b/app/views/settings/menus/_form.html.erb index 15842333..770a18b3 100755 --- a/app/views/settings/menus/_form.html.erb +++ b/app/views/settings/menus/_form.html.erb @@ -7,8 +7,8 @@
<%= f.input :name,:input_html=>{:class=>"col-md-9"} %> - <%= f.input :is_active,:input_html=>{:class=>"col-md-9"} %> - <%= f.input :is_ordering,:input_html=>{:class=>"col-md-9"} %> + <%= f.input :is_active,:input_html=>{:class=>"col-md-9"}, as: :boolean %> + <%= f.input :is_ordering,:input_html=>{:class=>"col-md-9"}, as: :boolean %>
@@ -50,7 +50,7 @@ <% end %>
- +
@@ -58,7 +58,7 @@
<% end %> - +
@@ -81,7 +81,7 @@ 2) <%= t("views.right_panel.button.back") %> - <%= t("views.right_panel.detail.back_txt") %> <%= t("views.right_panel.detail.menu_txt") %>

-
+ - From 6b6c945af7fa25bda39cd53bc8e18830eecc08f2 Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 30 Oct 2019 10:23:28 +0630 Subject: [PATCH 12/28] gemfile --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 143907b6..5df23eb0 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,7 @@ gem 'jquery-fileupload-rails', '~> 0.4.7' #Report and Printing gems #gem 'cups' #remove for cloud installation +# gem 'cups' gem 'prawn' gem 'prawn-table' gem 'prawn-qrcode' From d4e9f3217eb293a483d3977dab86474e24b0e909 Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 30 Oct 2019 10:33:54 +0630 Subject: [PATCH 13/28] gem --- Gemfile.lock | 2 -- 1 file changed, 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 393a2ac8..c0941977 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,7 +89,6 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.5) - cups (0.1.10) database_cleaner (1.7.0) diff-lcs (1.3) erubi (1.9.0) @@ -325,7 +324,6 @@ DEPENDENCIES carrierwave (~> 1.0) chartkick coffee-rails (~> 4.2) - cups database_cleaner factory_girl_rails (~> 4.0) faker From 41178978d23b5959cb242590c6a3063327308e7a Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 30 Oct 2019 10:38:49 +0630 Subject: [PATCH 14/28] gem lock --- Gemfile | 2 +- Gemfile.lock | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 5df23eb0..a49cf828 100644 --- a/Gemfile +++ b/Gemfile @@ -45,7 +45,7 @@ gem 'jquery-fileupload-rails', '~> 0.4.7' #Report and Printing gems #gem 'cups' #remove for cloud installation -# gem 'cups' +gem 'cups', '~>0.0.7' gem 'prawn' gem 'prawn-table' gem 'prawn-qrcode' diff --git a/Gemfile.lock b/Gemfile.lock index db4f94aa..3a70c5bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,7 +80,7 @@ GEM activemodel (>= 4.0.0) activesupport (>= 4.0.0) mime-types (>= 1.16) - chartkick (3.2.1) + chartkick (3.2.2) chronic (0.10.2) chunky_png (1.3.11) coffee-rails (4.2.2) @@ -93,6 +93,7 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.5) + cups (0.0.7) database_cleaner (1.7.0) diff-lcs (1.3) erubi (1.9.0) @@ -270,7 +271,8 @@ GEM activemodel (>= 5.0) spreadsheet (1.2.5) ruby-ole (>= 1.0) - spring (2.1.0) + spring (2.0.2) + activesupport (>= 4.2) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) @@ -328,6 +330,7 @@ DEPENDENCIES carrierwave (~> 1.0) chartkick coffee-rails (~> 4.2) + cups (~> 0.0.7) database_cleaner factory_girl_rails (~> 4.0) faker From fbaf63f07377fa80ad870b1efadcdae7d4fc33ef Mon Sep 17 00:00:00 2001 From: Myat Zin Wai Maw Date: Wed, 30 Oct 2019 16:28:28 +0630 Subject: [PATCH 15/28] printcopies --- app/models/printer/receipt_printer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/printer/receipt_printer.rb b/app/models/printer/receipt_printer.rb index b45c722b..f7b35a5a 100755 --- a/app/models/printer/receipt_printer.rb +++ b/app/models/printer/receipt_printer.rb @@ -227,8 +227,8 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker count = printer_settings.print_copies # override print copies for print worker loop - print_settings.print_copies = 1 - print_settings.save! + # print_settings.print_copies = 1 + # print_settings.save! end directory_name = 'public/receipts' Dir.mkdir(directory_name) unless File.exists?(directory_name) From 359679d41906c7bbb4bfd25d38b37ac6d9e381f2 Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 30 Oct 2019 16:52:16 +0630 Subject: [PATCH 16/28] CBKI --- app/controllers/api/authenticate_controller.rb | 2 +- app/controllers/origami/addorders_controller.rb | 2 +- app/controllers/origami/payments_controller.rb | 2 +- app/controllers/origami/pending_order_controller.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/authenticate_controller.rb b/app/controllers/api/authenticate_controller.rb index 8fb10040..02043be9 100755 --- a/app/controllers/api/authenticate_controller.rb +++ b/app/controllers/api/authenticate_controller.rb @@ -1,5 +1,5 @@ class Api::AuthenticateController < Api::ApiController - skip_before_action :authenticate + # skip_before_action :authenticate def create emp_id = params[:emp_id] diff --git a/app/controllers/origami/addorders_controller.rb b/app/controllers/origami/addorders_controller.rb index ba302f88..597b5d28 100755 --- a/app/controllers/origami/addorders_controller.rb +++ b/app/controllers/origami/addorders_controller.rb @@ -305,7 +305,7 @@ class Origami::AddordersController < BaseOrigamiController def getCloudDomain from = "" if ENV["SERVER_MODE"] == 'cloud' - from = request.subdomain + "." + request.domain + from = request.subdomain.to_s + "." + request.domain.to_s end return from diff --git a/app/controllers/origami/payments_controller.rb b/app/controllers/origami/payments_controller.rb index 5de37662..963d6ee3 100755 --- a/app/controllers/origami/payments_controller.rb +++ b/app/controllers/origami/payments_controller.rb @@ -839,7 +839,7 @@ class Origami::PaymentsController < BaseOrigamiController def getCloudDomain from = "" if ENV["SERVER_MODE"] == 'cloud' - from = request.subdomain + "." + request.domain + from = request.subdomain.to_s + "." + request.domain.to_s end return from diff --git a/app/controllers/origami/pending_order_controller.rb b/app/controllers/origami/pending_order_controller.rb index 4b412c17..773a217e 100644 --- a/app/controllers/origami/pending_order_controller.rb +++ b/app/controllers/origami/pending_order_controller.rb @@ -22,7 +22,7 @@ class Origami::PendingOrderController < BaseOrigamiController else redirect_to "/origami/#{params[:type]}" and return end - elsif id.start_with?("BKI") + elsif (id.start_with?("BKI") || id.start_with?("CBKI")) @bookings = Booking.find(id) @order = @bookings.orders.where(status: "new").first @order_items = @bookings.order_items From d95499f24d9399f272813671e3782c79c252e272 Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 30 Oct 2019 17:58:17 +0630 Subject: [PATCH 17/28] authenticate --- app/controllers/api/api_controller.rb | 2 +- app/controllers/api/authenticate_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index ad38376a..a5112ffe 100755 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -1,5 +1,5 @@ class Api::ApiController < ActionController::API - # include TokenVerification + include TokenVerification # before_action :lookup_domain helper_method :current_token, :current_login_employee, :get_cashier diff --git a/app/controllers/api/authenticate_controller.rb b/app/controllers/api/authenticate_controller.rb index 02043be9..8fb10040 100755 --- a/app/controllers/api/authenticate_controller.rb +++ b/app/controllers/api/authenticate_controller.rb @@ -1,5 +1,5 @@ class Api::AuthenticateController < Api::ApiController - # skip_before_action :authenticate + skip_before_action :authenticate def create emp_id = params[:emp_id] From d2cf13cd3b8d48732f3e37c9086caddceef6fe77 Mon Sep 17 00:00:00 2001 From: Nweni Date: Fri, 1 Nov 2019 12:08:15 +0630 Subject: [PATCH 18/28] dump --- dump.rdb | Bin 1864 -> 1864 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/dump.rdb b/dump.rdb index 42a27347f402a7cb6e6710a1d8f90f36e350dfb7..2b0eb54ec77065e59631cfc96f9a0a9087d16625 100644 GIT binary patch delta 195 zcmX@XcY<$%f%xsbU9rD7N{drdbaPX44}Dn6$MBEgx9H?`jN&ZD1_u}>doUVK=3zGw z0f{GD Date: Fri, 1 Nov 2019 16:26:24 +0630 Subject: [PATCH 19/28] gemfile --- Gemfile.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index c0941977..22c5244f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,6 +89,7 @@ GEM concurrent-ruby (1.1.5) connection_pool (2.2.2) crass (1.0.5) + cups (0.0.7) database_cleaner (1.7.0) diff-lcs (1.3) erubi (1.9.0) @@ -324,6 +325,7 @@ DEPENDENCIES carrierwave (~> 1.0) chartkick coffee-rails (~> 4.2) + cups database_cleaner factory_girl_rails (~> 4.0) faker From b73136dc79f1e815c712d159743bbd008afc3f31 Mon Sep 17 00:00:00 2001 From: Nweni Date: Fri, 1 Nov 2019 16:28:18 +0630 Subject: [PATCH 20/28] print copies --- app/models/printer/receipt_printer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/printer/receipt_printer.rb b/app/models/printer/receipt_printer.rb index b45c722b..f7b35a5a 100755 --- a/app/models/printer/receipt_printer.rb +++ b/app/models/printer/receipt_printer.rb @@ -227,8 +227,8 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker count = printer_settings.print_copies # override print copies for print worker loop - print_settings.print_copies = 1 - print_settings.save! + # print_settings.print_copies = 1 + # print_settings.save! end directory_name = 'public/receipts' Dir.mkdir(directory_name) unless File.exists?(directory_name) From 155cb0a9eb9e8877c60680f1a62aabb655a89a42 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Wed, 6 Nov 2019 17:34:42 +0630 Subject: [PATCH 21/28] rounding decimal to print_settings precision --- .../origami/discounts_controller.rb | 139 +++--- .../origami/payments_controller.rb | 24 +- .../origami/sale_edit_controller.rb | 4 +- app/models/promotion.rb | 442 +++++------------- app/models/sale.rb | 304 ++++-------- app/models/sale_item.rb | 19 + app/models/sale_order.rb | 5 +- app/models/sale_tax.rb | 11 + app/views/origami/discounts/index.html.erb | 179 +++---- app/views/reports/receipt_no/index.html.erb | 70 ++- app/views/reports/receipt_no/index.xls.erb | 72 ++- 11 files changed, 467 insertions(+), 802 deletions(-) diff --git a/app/controllers/origami/discounts_controller.rb b/app/controllers/origami/discounts_controller.rb index cd73d174..827d26c7 100755 --- a/app/controllers/origami/discounts_controller.rb +++ b/app/controllers/origami/discounts_controller.rb @@ -7,9 +7,9 @@ class Origami::DiscountsController < BaseOrigamiController @print_settings = PrintSetting.get_precision_delimiter() @webview = false if check_mobile - @webview = true + @webview = true end - + sale_id = params[:id] @cashier_type = params[:type] if Sale.exists?(sale_id) @@ -32,7 +32,7 @@ class Origami::DiscountsController < BaseOrigamiController discount_items = JSON.parse(params[:discount_items]) overall_discount = params[:overall_discount] sub_total = params[:sub_total] - + if Sale.exists?(sale_id) sale = Sale.find(sale_id) @@ -44,26 +44,29 @@ class Origami::DiscountsController < BaseOrigamiController table_id = nil end - - # sale.total_discount = overall_discount.to_f + + # sale.total_discount = overall_discount.to_f # sale.total_amount = sub_total.to_f - # sale.grand_total = (sub_total.to_f - overall_discount.to_f) + sale.total_tax; - # sale.save + # sale.grand_total = (sub_total.to_f - overall_discount.to_f) + sale.total_tax; + # sale.save if discount_items.length > 0 #save sale item for discount discount_items.each do |di| origin_sale_item = SaleItem.find(di["id"]) - + sale_item = SaleItem.new - if !origin_sale_item.nil? - menu_category = MenuCategory.get_menu_category(origin_sale_item.product_code) #get menu category for menu items - if !menu_category.nil? - sale_item.menu_category_code = menu_category.code - sale_item.menu_category_name = menu_category.name - end - end + # if !origin_sale_item.nil? + # menu_category = MenuCategory.get_menu_category(origin_sale_item.product_code) #get menu category for menu items + # if !menu_category.nil? + # sale_item.menu_category_code = menu_category.code + # sale_item.menu_category_name = menu_category.name + # end + # end + + sale_item.menu_category_code = origin_sale_item.menu_category_code + sale_item.menu_category_name = origin_sale_item.menu_category_name sale_item.sale_id = sale_id sale_item.product_code = origin_sale_item != nil ? origin_sale_item.product_code : sale_id @@ -73,7 +76,7 @@ class Origami::DiscountsController < BaseOrigamiController sale_item.status = "Discount" sale_item.qty = -1 - sale_item.unit_price = di["price"].to_f * (-1) + sale_item.unit_price = di["price"].to_f * -1 sale_item.taxable_price = di["price"] sale_item.is_taxable = 1 sale_item.account_id = origin_sale_item.account_id @@ -83,11 +86,11 @@ class Origami::DiscountsController < BaseOrigamiController action_by = current_user.name remark = "Discount Item Name ->#{sale_item.product_name}-Product Code ->#{sale_item.product_code} | Price [#{sale_item.price}] | Receipt No #{sale.receipt_no} " - + sale_audit = SaleAudit.record_audit_discount(sale_item.sale_id,sale.cashier_name, action_by,remark,"ITEMDISCOUNT" ) - - end - end + + end + end # Re-calc All Amount in Sale if overall_discount.to_f > 0 @@ -97,24 +100,24 @@ class Origami::DiscountsController < BaseOrigamiController else remark = "Discount Overall Price [#{overall_discount}]| Receipt No #{sale.receipt_no} | Table- #{table.name} " end - + sale_audit = SaleAudit.record_audit_discount(sale.sale_id,sale.cashier_name, action_by,remark,"OVERALLDISCOUNT" ) - end - sale.compute_by_sale_items(sale_id, sale.sale_items, overall_discount.to_f, nil,order_source) + end + sale.compute_by_sale_items(sale_id, sale.sale_items, overall_discount.to_f, nil,order_source) if !table.nil? result = {:status=> "Success", :table_id => table_id, :table_type => table.type } else result = {:status=> "Success" } end - else + else if !table.nil? result = {:status=> "Please, Check Again!", :table_id => table_id, :table_type => table.type } else result = {:status=> "Please, Check Again!" } end - end + end + - render :json => result.to_json end @@ -131,45 +134,45 @@ class Origami::DiscountsController < BaseOrigamiController else table_id = nil table = nil - end - - - if discount_items.length > 0 + end + + + if discount_items.length > 0 #destroy sale item for discount - discount_items.each do |di| - sale_item = SaleItem.find(di["id"]) + discount_items.each do |di| + sale_item = SaleItem.find(di["id"]) sale.total_amount = (sale.total_amount + sale_item.price.abs) action_by = current_user.name if table.nil? remark = "Remove Item Discount Item Name ->#{sale_item.product_name}-Product Code ->#{sale_item.product_code} | Price [#{sale_item.price}] | Receipt No #{sale.receipt_no} | Table- No Table " else - remark = "Remove Item Discount Item Name ->#{sale_item.product_name}-Product Code ->#{sale_item.product_code} | Price [#{sale_item.price}] | Receipt No #{sale.receipt_no} | Table- #{table.name} " + remark = "Remove Item Discount Item Name ->#{sale_item.product_name}-Product Code ->#{sale_item.product_code} | Price [#{sale_item.price}] | Receipt No #{sale.receipt_no} | Table- #{table.name} " end sale_audit = SaleAudit.record_audit_discount(sale.sale_id,sale.cashier_name, action_by,remark,"REMOVEITEMDISCOUNT" ) - - sale_item.destroy - end - end - # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax; + sale_item.destroy + end + end + + # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax; # sale.save # Re-calc All Amount in Sale - sale.compute_by_sale_items(sale_id, sale.sale_items, sale.total_discount, nil, order_source) + sale.compute_by_sale_items(sale_id, sale.sale_items, sale.total_discount, nil, order_source) if table.nil? result = {:status=> "Success"} else result = {:status=> "Success", :table_id => table_id, :table_type => table.type } end - - else + + else if table.nil? result = {:status=> "Please, Check Again!"} else result = {:status=> "Please, Check Again!", :table_id => table_id, :table_type => table.type } end - - end + + end render :json => result.to_json end @@ -180,29 +183,29 @@ class Origami::DiscountsController < BaseOrigamiController order_source = params[:type] if Sale.exists?(sale_id) - sale = Sale.find(sale_id) - + sale = Sale.find(sale_id) + if sale.bookings[0].dining_facility_id.to_i > 0 table_id = sale.bookings[0].dining_facility_id table = DiningFacility.find(table_id) - table_type = table.type + table_type = table.type else table_id = nil table = nil table_type = nil - end + end - discount_items = [] + discount_items = [] #destroy all discount sale item - sale.sale_items.each do |si| - if si.status == "Discount" && si.price < 0 - sale.total_amount = (sale.total_amount + si.price.abs) - discount_items.push(si) + sale.sale_items.each do |si| + if si.status == "Discount" && si.price < 0 + sale.total_amount = (sale.total_amount + si.price.abs) + discount_items.push(si) end - end + end # sale.total_discount = 0 - # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax; + # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax; # sale.save #destroy in sale.sale_items @@ -215,23 +218,23 @@ class Origami::DiscountsController < BaseOrigamiController remark = "Remove Discount Sale Id [#{sale.sale_id}]| Receipt No #{sale.receipt_no} | Table- #{table.name} " end sale_audit = SaleAudit.record_audit_discount(sale.sale_id,sale.cashier_name, action_by,remark,"REMOVEALLDISCOUNT" ) - + # Re-calc All Amount in Sale sale.compute_by_sale_items(sale_id, sale.sale_items, 0, nil, order_source) if table.nil? result = {:status=> "Success"} else - result = {:status=> "Success", :table_id => table_id, :dining => table.name, :table_type => table_type } + result = {:status=> "Success", :table_id => table_id, :dining => table.name, :table_type => table_type } end - - else + + else if table.nil? result = {:status=> "Please, Check Again!"} else - result = {:status=> "Please, Check Again!", :table_id => table_id, :dining => table.name, :table_type => table_type } + result = {:status=> "Please, Check Again!", :table_id => table_id, :dining => table.name, :table_type => table_type } end - - end + + end render :json => result.to_json end @@ -251,7 +254,7 @@ class Origami::DiscountsController < BaseOrigamiController end # Check for Card Payment is_card_payment = SalePayment.get_sale_payments_by_card(sale.sale_payments) - + # if is_card != "true" account_types = Account.where("discount=?",true) table_id = sale.bookings[0].dining_facility_id @@ -267,9 +270,9 @@ class Origami::DiscountsController < BaseOrigamiController str={type:pc[:name],amount:pc[:price]} acc_prices.push(str) end - end + end end - generic_customer_id = sale.customer.membership_id + generic_customer_id = sale.customer.membership_id receipt_no = sale.receipt_no membership = MembershipSetting.find_by_membership_type("paypar_url") memberaction = MembershipAction.find_by_membership_type("member_discount") @@ -282,7 +285,7 @@ class Origami::DiscountsController < BaseOrigamiController # Check for items for Paypar Cloud if acc_prices.length > 0 begin - response = HTTParty.post(url, + response = HTTParty.post(url, :body => { account_no: account_no, generic_customer_id:generic_customer_id , campaign_type_id: campaign_type_id, @@ -307,7 +310,7 @@ class Origami::DiscountsController < BaseOrigamiController rescue SocketError response = {"status": false, "message": "Can't open membership server " } end - else + else response = {"status": false, "message": "You have no selected discount item" } end Rails.logger.debug "-------------- Member Discount Osaka ---------" @@ -325,7 +328,7 @@ class Origami::DiscountsController < BaseOrigamiController else result = {:status=> response["message"],:title=>"Alert", :table_id => table_id,:table_type => table_type } end - + render :json => result.to_json # end #end Is Card Payment @@ -376,5 +379,5 @@ class Origami::DiscountsController < BaseOrigamiController # redirect_to origami_path(sale_id) # end - + end diff --git a/app/controllers/origami/payments_controller.rb b/app/controllers/origami/payments_controller.rb index 5de37662..78de240d 100755 --- a/app/controllers/origami/payments_controller.rb +++ b/app/controllers/origami/payments_controller.rb @@ -64,15 +64,9 @@ class Origami::PaymentsController < BaseOrigamiController customer = Customer.find(sale_data.customer_id) # rounding adjustment if shop_detail.is_rounding_adj - a = sale_data.grand_total % 25 # Modulus - b = sale_data.grand_total / 25 # Division - #not calculate rounding if modulus is 0 and division is even - #calculate rounding if modulus is zero or not zero and division are not even - if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0) - new_total = Sale.get_rounding_adjustment(sale_data.grand_total) - rounding_adj = new_total-sale_data.grand_total - sale_data.update_attributes(grand_total: new_total,old_grand_total: sale_data.grand_total,rounding_adjustment:rounding_adj) - end + new_total = Sale.get_rounding_adjustment(sale_data.grand_total) + rounding_adj = new_total - sale_data.grand_total + sale_data.update_attributes(grand_total: new_total,old_grand_total: sale_data.grand_total,rounding_adjustment:rounding_adj) if rounding_adj > 0 end #end rounding adjustment #record for sale audit @@ -151,15 +145,9 @@ class Origami::PaymentsController < BaseOrigamiController # rounding adjustment if !path.include? ("credit_payment") if shop_detail.is_rounding_adj - a = saleObj.grand_total % 25 # Modulus - b = saleObj.grand_total / 25 # Division - #not calculate rounding if modulus is 0 and division is even - #calculate rounding if modulus is zero or not zero and division are not even - if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0) - new_total = Sale.get_rounding_adjustment(saleObj.grand_total) - rounding_adj = new_total-saleObj.grand_total - saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj) - end + new_total = Sale.get_rounding_adjustment(saleObj.grand_total) + rounding_adj = new_total - saleObj.grand_total + saleObj.update_attributes(grand_total: new_total,old_grand_total: saleObj.grand_total,rounding_adjustment:rounding_adj) if rounding_adj > 0 end end #end rounding adjustment diff --git a/app/controllers/origami/sale_edit_controller.rb b/app/controllers/origami/sale_edit_controller.rb index 40f79afd..d537f8e9 100755 --- a/app/controllers/origami/sale_edit_controller.rb +++ b/app/controllers/origami/sale_edit_controller.rb @@ -72,7 +72,7 @@ class Origami::SaleEditController < BaseOrigamiController saleitemObj.status = 'foc' saleitemObj.remark = remark saleitemObj.save - @newsaleitem = SaleItem.new + @newsaleitem = saleitemObj.dup @newsaleitem.qty = saleitemObj.qty * -1 @newsaleitem.unit_price = saleitemObj.unit_price * 1 @@ -143,7 +143,7 @@ class Origami::SaleEditController < BaseOrigamiController sale_audit = SaleAudit.record_audit_for_edit(saleitemObj.sale_id,current_user.name, action_by,remark,"SALEITEMEDIT" ) # saleitemObj.remark = 'edit' - unless saleitemObj.product_name.include? 'updated' + unless saleitemObj.product_name.include? 'UPDATED' saleitemObj.product_name = saleitemObj.product_name + ' (UPDATED)' end diff --git a/app/models/promotion.rb b/app/models/promotion.rb index b636a097..0fa2b3e9 100755 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -11,37 +11,23 @@ class Promotion < ApplicationRecord PROMO_TYPE3 = "Net_price" # 1800 => 1000 => 1000 PROMO_TYPE4 = "Percentage" + def is_promo_day + promo_day.include? Date.today.wday.to_s + end + def self.promo_activate(saleObj) - array_same =[] current_day = Time.now.strftime("%Y-%m-%d") current_time = Time.now.strftime('%H:%M:%S') day = Date.today.wday promoList = is_between_promo_datetime(current_day,current_time) - puts "promoList - " + promoList.size.to_s - if promoList.size > 0 - itemList = combine_item(saleObj) - itemList.each do|list| - list.each do |l| - array_same.push({ - item_instance_code: l.item_instance_code, - order_id:l.order_id, - qty:l.qty - }) + + promoList.each do |promo| + if promo.is_promo_day + if item = saleObj.qty_of(promo.original_product) + check_promo_type(promo, item, saleObj) end end end - item_list =[] - array_same.group_by do |same| - same.values_at :item_instance_code - end.map do |(item_instance_code), array_same| - quantities = array_same.map { |p| p[:qty] } - qty = quantities.all? ? quantities.reduce(:+) : nil - item_list.push({ item_instance_code: item_instance_code, qty: qty }) - end - Rails.logger.debug "-------promo_activate array_same-------" - puts item_list.to_json - is_promo_day(promoList,day, item_list, saleObj.sale_id) - end def self.is_between_promo_datetime(current_day,current_time) #database is not local time @@ -49,45 +35,7 @@ class Promotion < ApplicationRecord return promoList end - def self.combine_item(saleObj) - # order_id = saleObj.sale_orders[0][0].order_id - # itemList = OrderItem.where("order_id = ?", order_id).group(:item_instance_code).sum(:qty) - # array_same =[] - itemList = [] - saleObj.sale_orders.each do |so| - # itemList << OrderItem.where("order_id = ?",so.order_id).group(["item_instance_code", "order_id"]).sum(:qty) - itemList << OrderItem.where("order_id = ?",so.order_id).select(:item_instance_code,:order_id,:qty) - end - - return itemList - end - - def self.is_promo_day(promoList, day, orderitemList, sale_id) - puts "Today date - " + day.to_s - promoList.each do |promo| - dayresult = promo.promo_day.include?(day.to_s) - if dayresult - orderitemList.each do |item| - find_promo_item(promo, item, sale_id) - end - end - end - end - - def self.find_promo_item(promo, orderitem, sale_id) - item_code = OrderItem.find_by_item_instance_code(orderitem[:item_instance_code]).item_code - # orderitem.each do |odr_item| - if promo.original_product.downcase.to_s == orderitem[:item_instance_code] - if promo.min_qty.to_i > orderitem[:qty].to_i - return false - else - check_promo_type(promo,orderitem, sale_id) - end - end - # end - end - - def self.check_promo_type(promo, orderitem, sale_id) + def self.check_promo_type(promo, orderitem, saleObj) promo.promotion_products.each do |promo_product| if promo_product.item_code.downcase == orderitem[:item_instance_code] same = true @@ -97,20 +45,20 @@ class Promotion < ApplicationRecord end # same, promo_product = check_giveaway_product(promo, orderitem[0][0]) if promo.promo_type == Promotion::PROMO_TYPE1 - if same - give_promotion_same_product(orderitem[:qty], promo.min_qty, promo_product.min_qty, orderitem, sale_id,promo_product.item_code) - else - give_promotion_second_product(orderitem[:qty], promo.min_qty, promo_product.item_code, orderitem, sale_id) + if same + give_promotion_same_product(orderitem[:qty], promo.min_qty, promo_product.min_qty, orderitem, saleObj, promo_product.item_code) + else + give_promotion_second_product(orderitem[:qty], promo.min_qty, promo_product.item_code, orderitem, saleObj) end elsif promo.promo_type == Promotion::PROMO_TYPE2 - give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, sale_id) + give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, saleObj) elsif promo.promo_type == Promotion::PROMO_TYPE3 - give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id) + give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, saleObj) elsif promo.promo_type == Promotion::PROMO_TYPE4 - give_promotion_discount(same,promo_product,promo.min_qty, orderitem, sale_id) + give_promotion_discount(same,promo_product,promo.min_qty, orderitem, saleObj) end end end @@ -128,106 +76,66 @@ class Promotion < ApplicationRecord # end end - def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id,promo_product) - item ='' - puts " Order qty: " + qty.to_s + " / promoqty: " + promoqty.to_s + " / giveaway: " + foc_min_qty.to_s - multiple = qty.to_i / promoqty.to_i # loop count - charge_qty = 0 - foc_qty = 0 - if multiple > 0 - multiple.times.each do |key| - if qty > promoqty - charge_qty += promoqty - different = qty - promoqty - qty = different - if different == 0 - foc_qty += foc_min_qty - else - foc_qty += foc_min_qty - qty = qty - foc_min_qty - end + def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, saleObj,promo_product) + puts " Order qty: " + qty.to_s + " / promoqty: " + promoqty.to_s + " / giveaway: " + foc_min_qty.to_s + multiple = qty.to_i / promoqty.to_i # loop count + charge_qty = 0 + foc_qty = 0 + if multiple > 0 + multiple.times.each do |key| + if qty > promoqty + charge_qty += promoqty + different = qty - promoqty + qty = different + if different == 0 + foc_qty += foc_min_qty else - charge_qty += qty + foc_qty += foc_min_qty + qty = qty - foc_min_qty end + else + charge_qty += qty end - if multiple == foc_qty - charge_qty += qty - end - else + end + if multiple == foc_qty charge_qty += qty end - sale_order =SaleOrder.where("sale_id=?",sale_id) - price =0 - source ='' - sale_order.each do|order| - source = Order.find(order.order_id).source - end + else + charge_qty += qty + end - item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - if promo_product.downcase != item[:item_instance_code] + price = 0 + source = saleObj.orders.first.source + item = saleObj.order_items.where(item_instance_code: promo_product).first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_code = ?",promo_product).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - end - if !item.nil? - update_existing_item(foc_qty, item, sale_id, "promotion", item[:price],source) - end + if !item.nil? + update_existing_item(foc_qty, item, sale_id, "promotion", item[:price], source) + end end # AA - 10 # 3 # BB # orderList, #S34345 - def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id) - source ='' - item ='' + def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, saleObj) puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s + promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty - foc_qty = find_second_item_qty(sale_id, promo_product) + foc_qty = saleObj.qty_of(promo_product).qty rescue 0 if (foc_qty < promotion_qty) promotion_qty = foc_qty end # item = OrderItem.find_by_item_instance_code(promo_product) - sale_order =SaleOrder.where("sale_id=?",sale_id) - price =0 - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",promo_product).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - # item = OrderItem.where("item_instance_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first + + price = 0 + source = saleObj.orders.first.source + item = saleObj.order_items.where(item_instance_code: promo_product).first + if !item.nil? - # source = Order.find(item.order_id).source update_existing_item(promotion_qty, item, sale_id, "promotion", item[:price],source) end end - - def self.update_existing_item(qty, item, sale_id, type, item_price,source) - if qty >0 + def self.update_existing_item(qty, item, saleObj, type, item_price,source) + if qty > 0 menu_category = MenuCategory.get_menu_category(item[:item_code]) #get menu category for menu items - + sale_item = SaleItem.new if !menu_category.nil? sale_item.menu_category_code = menu_category.code @@ -247,216 +155,84 @@ class Promotion < ApplicationRecord sale_item.price = qty * item_price * (-1) sale_item.is_taxable = 1 - sale_item.sale_id = sale_id + sale_item.sale = saleObj sale_item.save - sale = Sale.find(sale_id) - sale.compute_by_sale_items(sale.id, sale.sale_items, sale.total_discount,nil,source) + + saleObj.compute_by_sale_items(saleObj.id, saleObj.sale_items, saleObj.total_discount,nil,source) end end - def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, sale_id) - item ='' - source ='' + def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, saleObj) puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s - sale_order =SaleOrder.where("sale_id=?",sale_id) - if same - foc_qty = orderitem[:qty].to_i / foc_min_qty - # item = OrderItem.find_by_item_instance_code(orderitem[0][0]) - - price =0 - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - # item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first - if !item.nil? - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) - end - - else - foc_qty = orderitem[:qty].to_i / foc_min_qty - # foc_qty = find_second_item_qty(sale_id, promo_product.item_code) - # item = OrderItem.find_by_item_instance_code(promo_product.item_code) - # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",promo_product.item_code).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - if !item.nil? - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) - end - end - end - - def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, sale_id) - puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s - sale_order =SaleOrder.where("sale_id=?",sale_id) - source ='' - item ='' - if same - foc_qty = orderitem[:qty].to_i / foc_min_qty - # item = OrderItem.find_by_item_instance_code(orderitem[0][0]) # need to specify with menu item instance - # item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - if !item.nil? - price = item[:price].to_i - promo_product.net_price.to_i - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) - end - - else - order_qty = find_second_item_qty(sale_id, promo_product.item_code)# need to check for qty + foc_qty = orderitem[:qty].to_i / foc_min_qty + if !same + order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0 foc_qty = orderitem[:qty].to_i / foc_min_qty if foc_qty > order_qty foc_qty = order_qty end - # item = OrderItem.find_by_item_instance_code(promo_product.item_code) - # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code",promo_product.item_code).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } + end - if !item.nil? - price = item[:price].to_i - promo_product.net_price.to_i - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) - end - + source = saleObj.orders.first.source + item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first + + if !item.nil? + update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) end end - def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, sale_id) + def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, saleObj) puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s - sale_order =SaleOrder.where("sale_id=?",sale_id) - source ='' - item ='' - if same + + foc_qty = orderitem[:qty].to_i / foc_min_qty + if !same + order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0 foc_qty = orderitem[:qty].to_i / foc_min_qty - # item = OrderItem.find_by_item_instance_code(orderitem[0][0]) - # item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - # total = orderitem[1].to_i * item.price - if !item.nil? - total = item[:price] - price = calculate_discount(total, promo_product.percentage) - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source) - end - - else - order_qty = find_second_item_qty(sale_id, promo_product.item_code) #need to check - foc_qty = orderitem[:qty].to_i / foc_min_qty - # give total qty is 1 - #foc_qty = (foc_qty - foc_qty) + 1 + if foc_qty > order_qty foc_qty = order_qty end - # item = OrderItem.find_by_item_instance_code(promo_product.item_code) - # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first - sale_order.each do|order| - source = Order.find(order.order_id).source - end - item = OrderItem.where("item_instance_code = ?",promo_product.item_code).order("order_items_id DESC").first - item ={ - item_code:item.item_code, - item_instance_code:item.item_instance_code, - item_name:item.item_name, - alt_name:item.alt_name, - account_id:item.account_id, - price:item.price - } - # total = item.price * foc_qty - if !item.nil? - total = item[:price] - price = calculate_discount(total, promo_product.percentage) - # source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source) - end + end + + source = saleObj.orders.first.source + item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first + + if !item.nil? + price = item[:price].to_i - promo_product.net_price.to_i + update_existing_item(foc_qty, item, saleObj, "promotion nett price", price,source) end end - def self.find_second_item_qty(sale_id, promo_item) - array_same =[] - saleObj = Sale.find_by_sale_id(sale_id) - itemList = combine_item(saleObj) - itemList.each do|list| - list.each do |l| - array_same.push({ - item_instance_code: l.item_instance_code, - order_id:l.order_id, - qty:l.qty - }) + def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, saleObj) + puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s + + foc_qty = orderitem[:qty].to_i / foc_min_qty + if !same + order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0 + foc_qty = orderitem[:qty].to_i / foc_min_qty + + if foc_qty > order_qty + foc_qty = order_qty end end - item_list =[] - array_same.group_by do |same| - same.values_at :item_instance_code - end.map do |(item_instance_code), array_same| - quantities = array_same.map { |p| p[:qty] } - qty = quantities.all? ? quantities.reduce(:+) : nil - item_list.push({ item_instance_code: item_instance_code, qty: qty }) - end - item_list.each do |item| - if item[:item_instance_code] == promo_item.downcase - return item[:qty].to_i - end - end - return 0 - end - def self.calculate_discount(total, percentage) - return (total.to_i * percentage.to_i) / 100 - end + source = saleObj.orders.first.source + item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first + puts saleObj + puts item + # total = orderitem[1].to_i * item.price + if item + total = item[:price] + price = calculate_discount(total, promo_product.percentage) + # source = Order.find(item.order_id).source + update_existing_item(foc_qty, item, saleObj, "promotion discount", price,source) + end + end + + def self.calculate_discount(total, percentage) + return (total * percentage) / 100 + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index a7208d50..cb4854b8 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -14,14 +14,20 @@ class Sale < ApplicationRecord has_many :sale_payments has_many :sale_orders has_many :orders, through: :sale_orders + has_many :order_items, through: :sale_orders has_many :bookings has_many :product_commissions + before_validation :round_to_precision after_update :update_stock_journal scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") } scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") } + def qty_of(item_instance_code) + order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first + end + REPORT_TYPE = { "daily" => 0, "monthly" => 1, @@ -418,11 +424,11 @@ class Sale < ApplicationRecord apply_tax(total_taxable, order_source, tax_type) self.total_amount = subtotal_price - self.total_discount = total_discount + # self.total_discount = total_discount self.grand_total = (self.total_amount - self.total_discount) + self.total_tax - self.grand_total_round + # self.grand_total_round #compute rounding adjustment - adjust_rounding + # adjust_rounding self.save! @@ -455,7 +461,7 @@ class Sale < ApplicationRecord sale.grand_total = 0 else sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax - sale.grand_total_round + # sale.grand_total_round end if discount_type == "member_discount" @@ -463,7 +469,6 @@ class Sale < ApplicationRecord end #compute rounding adjustment # adjust_rounding - sale.rounding_adjustment = compute_adjust_rounding(sale.grand_total) sale.save! end @@ -491,11 +496,11 @@ class Sale < ApplicationRecord apply_tax(total_taxable, order_source) self.total_amount = subtotal_price - self.total_discount = total_discount + # self.total_discount = total_discount self.grand_total = (self.total_amount - self.total_discount) + self.total_tax - self.grand_total_round + # self.grand_total_round #compute rounding adjustment - adjust_rounding + # adjust_rounding self.save! end @@ -509,89 +514,45 @@ class Sale < ApplicationRecord # #delete existing and create new # existing_tax.delete # end - taxes = SaleTax.where("sale_id='#{sale.sale_id}'").pluck(:sale_tax_id) - SaleTax.where("sale_tax_id IN (?)", taxes).destroy_all + taxes = SaleTax.where("sale_id='#{self.sale_id}'").destroy_all total_tax_amount = 0 tax_incl_exec = "exclusive" #tax_profile - list by order_by # tax_profiles = TaxProfile.all.order("order_by asc") # customer = Customer.find(sale.customer_id) - arr_tax = [] - arr_tax = unique_tax_profiles(order_source, self.customer_id) - if !arr_tax.empty? - if tax_type.nil? - tax_profiles = TaxProfile.where(:id => arr_tax) - else - tax_profiles = TaxProfile.where("group_type=?",order_source) - end - else - tax_profiles = TaxProfile.where("group_type=?",order_source) - end - - # #Creat new tax records if order_source.to_s == "emenu" order_source = "cashier" end + tax_profiles = unique_tax_profiles(order_source, self.customer_id) + + # #Creat new tax records if sale.payment_status != 'foc' tax_profiles.each do |tax| - if tax.group_type.to_s == order_source.to_s - if tax_type - if tax_type.to_s == tax.name.to_s || tax_type == 'all' - sale_tax = SaleTax.new(:sale => sale) - sale_tax.tax_name = tax.name - sale_tax.tax_rate = tax.rate + sale_tax = SaleTax.new(:sale => sale) + sale_tax.tax_name = tax.name + sale_tax.tax_rate = tax.rate - # substract , to give after discount - total_tax = total_taxable - total_discount - #include or execulive - if tax.inclusive - tax_incl_exec = "inclusive" - rate = tax.rate - divided_value = (100 + rate)/rate - sale_tax.tax_payable_amount = total_tax / divided_value - else - sale_tax.tax_payable_amount = total_tax * tax.rate / 100 - total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount - end - #new taxable amount is standard rule for step by step - if shop.calc_tax_order - total_taxable = total_taxable + sale_tax.tax_payable_amount - end - sale_tax.inclusive = tax.inclusive - sale_tax.save - end - else - # customer.tax_profiles.each do |cus_tax| - # if cus_tax.to_i == tax.id - sale_tax = SaleTax.new(:sale => sale) - sale_tax.tax_name = tax.name - sale_tax.tax_rate = tax.rate - - # substract , to give after discount - total_tax = total_taxable - total_discount - #include or execulive - if tax.inclusive - tax_incl_exec = "inclusive" - rate = tax.rate - divided_value = (100 + rate)/rate - sale_tax.tax_payable_amount = total_tax / divided_value - else - sale_tax.tax_payable_amount = total_tax * tax.rate / 100 - total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount - end - #new taxable amount is standard rule for step by step - if shop.calc_tax_order - total_taxable = total_taxable + sale_tax.tax_payable_amount - end - sale_tax.inclusive = tax.inclusive - sale_tax.save - # end - # end - end + # substract , to give after discount + total_tax = total_taxable - total_discount + #include or execulive + if tax.inclusive + tax_incl_exec = "inclusive" + rate = tax.rate + divided_value = (100 + rate)/rate + sale_tax.tax_payable_amount = total_tax / divided_value + else + sale_tax.tax_payable_amount = total_tax * tax.rate / 100 + total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount end + #new taxable amount is standard rule for step by step + if shop.calc_tax_order + total_taxable = total_taxable + sale_tax.tax_payable_amount + end + sale_tax.inclusive = tax.inclusive + sale_tax.save end end sale.tax_type = tax_incl_exec @@ -608,8 +569,7 @@ class Sale < ApplicationRecord # existing_tax.delete # end - taxes = SaleTax.where("sale_id='#{self.sale_id}'").pluck(:sale_tax_id) - SaleTax.where("sale_tax_id IN (?)", taxes).destroy_all + taxes = SaleTax.where("sale_id='#{self.sale_id}'").destroy_all total_tax_amount = 0 tax_incl_exec = "exclusive" @@ -622,77 +582,34 @@ class Sale < ApplicationRecord # tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id) # customer = Customer.find(self.customer_id).tax_profiles - arr_tax = [] - arr_tax = unique_tax_profiles(order_source, self.customer_id) + tax_profiles = unique_tax_profiles(order_source, self.customer_id) - if !arr_tax.empty? - if tax_type.nil? - tax_profiles = TaxProfile.where(:id => arr_tax) - else - tax_profiles = TaxProfile.where("group_type=?",order_source) - end - else - tax_profiles = TaxProfile.where("group_type=?",order_source) - end #Create new tax records tax_profiles.each do |tax| - if tax.group_type.to_s == order_source.to_s - if tax_type - if tax_type.to_s == tax.name.to_s || tax_type == 'all' - sale_tax = SaleTax.new(:sale => self) - sale_tax.tax_name = tax.name - sale_tax.tax_rate = tax.rate + sale_tax = SaleTax.new(:sale => self) + sale_tax.tax_name = tax.name + sale_tax.tax_rate = tax.rate - # substract , to give after discount - total_tax = total_taxable - total_discount - #include or execulive - if tax.inclusive - tax_incl_exec = "inclusive" - rate = tax.rate - divided_value = (100 + rate)/rate - sale_tax.tax_payable_amount = total_tax / divided_value - else - sale_tax.tax_payable_amount = total_tax * tax.rate / 100 - total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount - end - #new taxable amount is standard rule for step by step - if shop.calc_tax_order - total_taxable = total_taxable + sale_tax.tax_payable_amount - end - sale_tax.inclusive = tax.inclusive - sale_tax.save - end - else - # customer.tax_profiles.each do |cus_tax| - # if cus_tax.to_i == tax.id - sale_tax = SaleTax.new(:sale => self) - sale_tax.tax_name = tax.name - sale_tax.tax_rate = tax.rate - - # substract , to give after discount - total_tax = total_taxable - self.total_discount - #include or execulive - if tax.inclusive - tax_incl_exec = "inclusive" - rate = tax.rate - divided_value = (100 + rate)/rate - sale_tax.tax_payable_amount = total_tax / divided_value - else - sale_tax.tax_payable_amount = total_tax * tax.rate / 100 - total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount - end - - #new taxable amount is standard rule for step by step - if shop.calc_tax_order - total_taxable = total_taxable + sale_tax.tax_payable_amount - end - - sale_tax.inclusive = tax.inclusive - sale_tax.save - # end - # end - end + # substract , to give after discount + total_tax = total_taxable - self.total_discount + #include or execulive + if tax.inclusive + tax_incl_exec = "inclusive" + rate = tax.rate + divided_value = (100 + rate)/rate + sale_tax.tax_payable_amount = total_tax / divided_value + else + sale_tax.tax_payable_amount = total_tax * tax.rate / 100 + total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount end + + #new taxable amount is standard rule for step by step + if shop.calc_tax_order + total_taxable = total_taxable + sale_tax.tax_payable_amount + end + + sale_tax.inclusive = tax.inclusive + sale_tax.save end self.tax_type = tax_incl_exec self.total_tax = total_tax_amount @@ -727,42 +644,16 @@ class Sale < ApplicationRecord shop_details = Shop.first # rounding adjustment if shop_details.is_rounding_adj - a = self.grand_total % 25 # Modulus - b = self.grand_total / 25 # Division - #not calculate rounding if modulus is 0 and division is even - #calculate rounding if modulus is zero or not zero and division are not even - if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0) - new_total = Sale.get_rounding_adjustment(self.grand_total) - self.rounding_adjustment = new_total - self.grand_total - else - self.rounding_adjustment = 0.00 - end + new_total = Sale.get_rounding_adjustment(self.grand_total) + self.rounding_adjustment = new_total - self.grand_total + self.old_grand_total = self.grand_total + self.grand_total = new_total else self.rounding_adjustment = 0.00 end end - def compute_adjust_rounding(grand_total) - shop_details = Shop.first - # rounding adjustment - if shop_details.is_rounding_adj - a = grand_total % 25 # Modulus - b = grand_total / 25 # Division - #not calculate rounding if modulus is 0 and division is even - #calculate rounding if modulus is zero or not zero and division are not even - if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0) - new_total = Sale.get_rounding_adjustment(grand_total) - rounding_adjustment = new_total - grand_total - else - rounding_adjustment = 0.00 - end - else - rounding_adjustment = 0.00 - end - return rounding_adjustment - end - #Generate new Receipt No when it is not assigned def generate_receipt_no #shop_code and client_code @@ -857,36 +748,16 @@ class Sale < ApplicationRecord # if get_rounded_amt == true value = 0 - num = num.to_f.round - get_last_no = num.to_s.last(2).to_f - if get_last_no.between?(0,25) - ## down to 0 - num -= get_last_no - else - if get_last_no.between?(26,50) - ## up to 50 - value = 50 - get_last_no.to_f - num += value - puts 'up to 50' - else - if get_last_no.between?(51, 75) - ## down to 50 - value = get_last_no.to_f - 50 - num -= value - puts 'down to 50' - else - ## up to 100 - value = 100 - get_last_no.to_f - num += value - puts 'up to 100' - end - end + # num = num.to_f.round + mod = num % 50 + if mod > 0 && mod <= 25 + num -= mod + elsif mod > 25 + num += 50 - mod end - # end return num end - def self.daily_sales_list(from,to) sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN (SUM(sale_payments.payment_amount) + SUM(sale_payments.outstanding_amount)) ELSE 0 END) @@ -2646,20 +2517,15 @@ def self.get_sale_data_for_other_payment_credit(sale_id) end def unique_tax_profiles(order_source, customer_id) - tax_data = TaxProfile.where("group_type='#{order_source}'").pluck(:id) - customer_tax_profiles = Customer.find(customer_id).tax_profiles + tax_data = TaxProfile + .where(group_type: order_source) - arr_data = [] - if !customer_tax_profiles.empty? - customer_tax_profiles.each do |value1| - if tax_data.include? value1.to_i - arr_data.push(value1.to_i) - end - end - return arr_data - else - return tax_data + customer_tax_profiles = Customer.select(:tax_profiles).where(customer_id: customer_id).first + if customer_tax_profiles.present? + tax_data = tax_data.where(id: customer_tax_profiles.tax_profiles) end + + return tax_data end def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2892,6 +2758,20 @@ private end end + def round_to_precision + if (self.total_amount != self.total_amount_was || self.total_discount != self.total_discount_was || self.total_tax != self.total_tax_was) + if (self.total_amount % 1 > 0 || self.total_discount % 1 > 0 || self.total_tax % 1 > 0) + precision = PrintSetting.get_precision_delimiter().precision.to_i + + self.total_amount = self.total_amount.round(precision) + self.total_discount = self.total_discount.round(precision) + self.total_tax = self.total_tax.round(precision) + self.grand_total = (self.total_amount - self.total_discount) + self.total_tax + end + adjust_rounding + end + end + def update_stock_journal if self.sale_status == "void" && self.sale_status_before_last_save != "void" self.sale_items.each do |item| diff --git a/app/models/sale_item.rb b/app/models/sale_item.rb index d17edc6e..0e073f13 100755 --- a/app/models/sale_item.rb +++ b/app/models/sale_item.rb @@ -11,6 +11,7 @@ class SaleItem < ApplicationRecord def compute_item end + before_validation :round_to_precision after_update :update_stock_journal # Add Sale Items @@ -231,6 +232,24 @@ class SaleItem < ApplicationRecord end end + def round_to_precision + unit_price_fraction = self.unit_price % 1 + price_fraction = self.unit_price % 1 + # is_dining_charge = self.menu_category_code == 'DingingCharge' + + if self.unit_price != self.unit_price_was || self.price != self.price_was + if unit_price_fraction > 0 || price_fraction > 0 + if ['Discount', 'promotion'].include?(self.status) + precision = PrintSetting.get_precision_delimiter().precision.to_i + + self.unit_price = self.unit_price.round(precision) + self.price = (self.unit_price * self.qty).round(precision) + self.taxable_price = self.price + end + end + end + end + def update_stock_journal is_void = self.status == "void" && self.status_before_last_save != "void" cancel_void = self.status_before_last_save == "void" && self.status.nil? diff --git a/app/models/sale_order.rb b/app/models/sale_order.rb index bbd68829..bb1375a8 100755 --- a/app/models/sale_order.rb +++ b/app/models/sale_order.rb @@ -6,8 +6,9 @@ class SaleOrder < ApplicationRecord belongs_to :sale belongs_to :order + has_many :order_items, foreign_key: "order_id", primary_key: "order_id" - def create_sale_order(sale_id, order_id) + def create_sale_order(sale_id, order_id) # sale_order = SaleOrder.new self.sale_id = sale_id self.order_id = order_id @@ -37,6 +38,6 @@ class SaleOrder < ApplicationRecord if self.sale_order_id.nil? self.sale_order_id = SeedGenerator.generate_id(self.class.name, 'SOI') end - + end end diff --git a/app/models/sale_tax.rb b/app/models/sale_tax.rb index 06c73253..2e35d2b5 100755 --- a/app/models/sale_tax.rb +++ b/app/models/sale_tax.rb @@ -5,6 +5,8 @@ class SaleTax < ApplicationRecord before_create :generate_custom_id belongs_to :sale + before_validation :round_to_precision + def self.sync_sale_tax_records(sale_taxes) if !sale_taxes.nil? sale_taxes.each do |t| @@ -38,4 +40,13 @@ class SaleTax < ApplicationRecord self.sale_tax_id = SeedGenerator.generate_id(self.class.name, "STI") end end + + def round_to_precision + if self.tax_payable_amount != self.tax_payable_amount_was + if self.tax_payable_amount % 1 > 0 + precision = PrintSetting.get_precision_delimiter().precision.to_i + self.tax_payable_amount = self.tax_payable_amount.round(precision) + end + end + end end diff --git a/app/views/origami/discounts/index.html.erb b/app/views/origami/discounts/index.html.erb index 3b14e05a..86cf26c7 100755 --- a/app/views/origami/discounts/index.html.erb +++ b/app/views/origami/discounts/index.html.erb @@ -34,7 +34,7 @@ <%end%> - +

Receipt No: <%=@sale_data.receipt_no rescue ' '%>

@@ -57,8 +57,8 @@ <% sub_total = 0 %> <% @sale_data.sale_items.each do |sale_item| %> - <% - sub_total += sale_item.qty*sale_item.unit_price + <% + sub_total += sale_item.qty*sale_item.unit_price if sale_item.price != 0 && sale_item.status != "void" && sale_item.status != "foc" %> > @@ -76,7 +76,7 @@ <% end - end + end %> @@ -130,18 +130,18 @@
-
-
+
+

-
+
<% @accounts.each do |acc| %> <% end %>
-
+
@@ -217,11 +217,11 @@
- +
-
+
@@ -235,7 +235,7 @@
-
+
@@ -243,11 +243,11 @@
-
+
-
+
- + --> @@ -259,7 +259,7 @@ <% if @member_discount%> - <%end %> @@ -300,10 +300,10 @@ var cashier_type = "<%= @cashier_type %>"; case 'num': var input_value = $(this).attr("data-value"); if (original_value == "0.0"){ - $('#discount-amount').val(input_value); + $('#discount-amount').val(input_value); } else{ - $('#discount-amount').val(original_value + '' + input_value); + $('#discount-amount').val(original_value + '' + input_value); } break; case 'add': @@ -313,10 +313,10 @@ var cashier_type = "<%= @cashier_type %>"; break; case 'del' : var discount_text=$('#discount-amount').val(); - $('#discount-amount').val(discount_text.substr(0,discount_text.length-1)); + $('#discount-amount').val(discount_text.substr(0,discount_text.length-1)); break; case 'clr': - $('#discount-amount').val("0.0"); + $('#discount-amount').val("0.0"); break; } event.handled = true; @@ -374,7 +374,7 @@ var cashier_type = "<%= @cashier_type %>"; } } }); - } + } }); // Calculate Net Discount for Payment @@ -384,7 +384,7 @@ var cashier_type = "<%= @cashier_type %>"; var discount_value = $('#discount-amount').val(); var sub_total = parseFloat($('#order-sub-total').text()); var ajax_url = "/origami/" + sale_id + "/discount"; - + if(discount_value!=""){ if(discount_value > 0){ if(parseFloat(discount_value) > sub_total){ @@ -393,7 +393,7 @@ var cashier_type = "<%= @cashier_type %>"; }else{ $("#discount-amountErr").html(""); // Selected Items - var sale_items = get_selected_sale_items(); + var sale_items = get_selected_sale_items(); // Selected Account var account_types = get_selected_account_types(); @@ -402,8 +402,8 @@ var cashier_type = "<%= @cashier_type %>"; calculate_overall_discount(0, discount_value); } else { - calculate_item_discount(0, discount_value, sale_items, account_types); - } + calculate_item_discount(0, discount_value, sale_items, account_types); + } // Remove Selection selection_remove(); @@ -414,7 +414,7 @@ var cashier_type = "<%= @cashier_type %>"; }else{ $("#discount-amountErr").html("can't be blank"); } - + }); // Calculate Percentage Discount for Payment @@ -425,19 +425,19 @@ var cashier_type = "<%= @cashier_type %>"; var ajax_url = "/origami/" + sale_id + "/discount"; // Selected Items - var sale_items = get_selected_sale_items(); + var sale_items = get_selected_sale_items(); // Selected Account var account_types = get_selected_account_types(); - + if(sale_items.length == 0 && account_types.length == 0){ calculate_overall_discount(1, discount_value); } else { - calculate_item_discount(1, discount_value, sale_items, account_types); + calculate_item_discount(1, discount_value, sale_items, account_types); } // Remove Selection - selection_remove(); + selection_remove(); }); // Remove selected discount items @@ -447,8 +447,8 @@ var cashier_type = "<%= @cashier_type %>"; var total = 0; $('.discount-item-row.selected-item').each(function(i){ - var amount = parseFloat($(this).find('#item-total-price').text()); - total = total + Math.abs(amount); + var amount = parseFloat($(this).find('#item-total-price').text()); + total = total + Math.abs(amount); $(this).remove(); }); $("#order-sub-total").text(origin_sub_total + total); @@ -464,8 +464,8 @@ var cashier_type = "<%= @cashier_type %>"; var sale_id = $('#sale-id').text(); var discount_items = JSON.stringify(get_discount_item_rows()); var overall_discount = $("#order-discount").text(); - var sub_total = $('#order-sub-total').text(); - var ajax_url = "/origami/" + sale_id + "/discount"; + var sub_total = $('#order-sub-total').text(); + var ajax_url = "/origami/" + sale_id + "/discount"; var params = { 'cashier_type' : cashier_type,'sale_id': sale_id, 'sub_total': sub_total, 'discount_items': discount_items, 'overall_discount': overall_discount }; @@ -473,8 +473,8 @@ var cashier_type = "<%= @cashier_type %>"; type: "POST", url: ajax_url, data: params, - success:function(result){ - $("#loading_wrapper").hide(); + success:function(result){ + $("#loading_wrapper").hide(); swal({ title: "Information!", text: result.status, @@ -489,7 +489,7 @@ var cashier_type = "<%= @cashier_type %>"; window.location.href = "/origami/room/" + result.table_id } } - }); + }); } }); }); @@ -500,12 +500,12 @@ var cashier_type = "<%= @cashier_type %>"; var sale_id = $('#sale-id').text(); var discount_items = []; - // Selected Items - var sale_items = get_selected_sale_items(); + // Selected Items + var sale_items = get_selected_sale_items(); if(sale_items.length == 0){ //swal("Information!", "You have no selected item!"); - swal ( "Oops" , "You have no selected item!" , "error" ); - return; + swal ( "Oops" , "You have no selected item!" , "error" ); + return; } for(var i=0;i < sale_items.length;i++){ @@ -514,7 +514,7 @@ var cashier_type = "<%= @cashier_type %>"; } else { swal ("Oops" , "You have no selected item!" , "error" ); - return; + return; } } @@ -539,7 +539,7 @@ var cashier_type = "<%= @cashier_type %>"; window.location.href = "/origami/room/" + result.table_id } } - }); + }); } }); }); @@ -566,7 +566,7 @@ var cashier_type = "<%= @cashier_type %>"; window.location.href = "/origami/room/" + result.table_id } } - }); + }); } }); }); @@ -575,8 +575,8 @@ var cashier_type = "<%= @cashier_type %>"; $("#member-discount").on('click', function(e){ e.preventDefault(); var sale_id = $('#sale-id').text(); - var sub_total = $('#order-sub-total').text(); - var ajax_url = "/origami/" + sale_id + "/member_discount"; + var sub_total = $('#order-sub-total').text(); + var ajax_url = "/origami/" + sale_id + "/member_discount"; // Selected Account //var account_types = JSON.stringify(get_selected_account_types()); @@ -618,7 +618,7 @@ var cashier_type = "<%= @cashier_type %>"; }); } }); - + });//end member discount }); @@ -631,7 +631,7 @@ var cashier_type = "<%= @cashier_type %>"; /* Get Item rows but not discount*/ function get_item_rows(){ - var sale_items = []; + var sale_items = []; $('.item-row').not('.discount-item-row').each(function(i){ var sale_item = {}; sale_item.id = $(this).attr('id').substr(0,16); @@ -639,13 +639,13 @@ var cashier_type = "<%= @cashier_type %>"; sale_item.account_id = $(this).find('#item-account-type').text(); sale_item.price = $(this).find('#item-total-price').text(); sale_items.push(sale_item); - }); + }); return sale_items; } /* Get discount Item rows */ function get_discount_item_rows(){ - var sale_items = []; + var sale_items = []; $('.discount-item-row').each(function(i){ var sale_item = {}; sale_item.id = $(this).attr('id'); @@ -653,13 +653,13 @@ var cashier_type = "<%= @cashier_type %>"; sale_item.account_id = $(this).find('#item_account_type').text(); sale_item.price = $(this).find('#item-total-price').text(); sale_items.push(sale_item); - }); + }); return sale_items; } /* Get discount Item rows */ // function get_selected_discount_items(){ -// var sale_items = []; +// var sale_items = []; // $('.discount-item-row').hasClass('.selected-item').each(function(i){ // var sale_item = {}; // sale_item.id = $(this).attr('id'); @@ -667,13 +667,13 @@ var cashier_type = "<%= @cashier_type %>"; // sale_item.account_id = $(this).find('#item_account_type').text(); // sale_item.price = $(this).find('#item-total-price').text(); // sale_items.push(sale_item); -// }); +// }); // return sale_items; // } /* Get Selected Sale Item's ID and Price */ function get_selected_sale_items(){ - var sale_items = []; + var sale_items = []; $('.item-row.selected-item').each(function(i){ var sale_item = {}; sale_item.id = $(this).attr('id').substr(0,16); @@ -681,33 +681,33 @@ function get_selected_sale_items(){ sale_item.account_id = $(this).find('#item-account-type').text(); sale_item.price = $(this).find('#item-total-price').text(); sale_items.push(sale_item); - }); + }); return sale_items; } /* Get Selected Accounts ID and Price */ -function get_selected_account_types(){ +function get_selected_account_types(){ var account_types = []; $('.selected-account').each(function(i){ var account= {}; account.id = $(this).attr('id').substr(8); - account.name = $(this).text(); + account.name = $(this).text(); account_types.push(account); - }); + }); return account_types; } /* Calculate Overall Discount*/ function calculate_overall_discount(type, amount){ - var origin_sub_total = parseFloat($("#order-sub-total").text()); + var origin_sub_total = parseFloat($("#order-sub-total").text()); var dis_amount = 0; var sub_total = 0; var total_discount = 0; // For Net Pay if(type == 0){ - total_discount = amount; + total_discount = Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision); } // For Percentage Pay @@ -719,10 +719,10 @@ function calculate_overall_discount(type, amount){ type: "error", confirmButtonText: 'OK', confirmButtonColor:"red" - }); + }); } else{ - total_discount = (origin_sub_total * amount)/100; + total_discount = Math.round(origin_sub_total * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision); } } @@ -731,22 +731,23 @@ function calculate_overall_discount(type, amount){ /* Calculate Items Discount*/ function calculate_item_discount(type, amount, sale_items, account_types){ - var origin_sub_total = parseFloat($("#order-sub-total").text()); + var origin_sub_total = parseFloat($("#order-sub-total").text()); var dis_amount = 0; var sub_total = 0; var total_discount = 0; var arrItemName = ""; + var precision = <%= precision %>; $("#discount_itemsErr").html(""); $("#discount-amountErr").html(""); // For Net Pay if(origin_sub_total > 0){ if(type == 0){ - dis_amount = (0 - amount); + dis_amount = -Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision); if(sale_items.length > 0){ for(var i=0;i < sale_items.length;i++){ // if(account_types.length > 0){ // for(var j=0; j < account_types.length; j++){ - // if(sale_items[i].account_id == account_types[j].id){ + // if(sale_items[i].account_id == account_types[j].id){ // // Discount Items // var discount_item_row = item_row_template(type, sale_items[i], dis_amount, amount); // $("#order-items-table tbody").append(discount_item_row); @@ -768,7 +769,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){ $("#discount-amountErr").html("Discount is greater than sub total!"); } } - + // } } } @@ -782,11 +783,11 @@ function calculate_item_discount(type, amount, sale_items, account_types){ }else{ $("#discount_itemsErr").html("Discount is greater than "+arrItemName+" price"); } - + } - + // No Needs For Auto Selected - // if(account_types.length > 0){ + // if(account_types.length > 0){ // var item_rows=get_item_rows(); // if(item_rows.length > 0){ // for(var k=0; k < item_rows.length; k++){ @@ -802,7 +803,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){ // } // else { // alert("No Items!"); - // } + // } // } sub_total = parseFloat(origin_sub_total) - parseFloat(total_discount); @@ -817,7 +818,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){ type: "error", confirmButtonText: 'OK', confirmButtonColor:"red" - }); + }); } else{ // Check sale items exists @@ -825,7 +826,7 @@ function calculate_item_discount(type, amount, sale_items, account_types){ for(var i=0;i < sale_items.length;i++){ // if(account_types.length > 0){ // for(var j=0; j < account_types.length; j++){ - // if(sale_items[i].account_id == account_types[j].id){ + // if(sale_items[i].account_id == account_types[j].id){ // // Discount Items // dis_amount = 0 - ((sale_items[i].price * amount)/100); // var discount_item_row = item_row_template(type,sale_items[i], dis_amount, amount); @@ -835,9 +836,9 @@ function calculate_item_discount(type, amount, sale_items, account_types){ // } // } // else { - dis_amount = 0 - ((sale_items[i].price * amount)/100); + dis_amount = -Math.round(sale_items[i].price * amount / 100 * Math.pow(10, precision)) / Math.pow(10, precision); var discount_item_row = item_row_template(type,sale_items[i], dis_amount, amount); - + total_discount = total_discount + dis_amount; if(parseFloat(origin_sub_total) >= parseFloat(total_discount)){ $("#order-items-table tbody").append(discount_item_row); @@ -852,14 +853,14 @@ function calculate_item_discount(type, amount, sale_items, account_types){ } // No Needs For Auto Selected // Check account types exists - // if(account_types.length > 0){ + // if(account_types.length > 0){ // var item_rows=get_item_rows(); // console.log(account_types); // if(item_rows.length > 0){ // for(var k=0; k < item_rows.length; k++){ // for(var j=0; j < account_types.length; j++){ // if(item_rows[k].account_id == account_types[j].id){ - // // Discount Items + // // Discount Items // dis_amount = 0 - ((item_rows[k].price * amount)/100); // var discount_item_row = item_row_template(type, item_rows[k], dis_amount, amount); // $("#order-items-table tbody").append(discount_item_row); @@ -870,15 +871,15 @@ function calculate_item_discount(type, amount, sale_items, account_types){ // } // else { // alert("No Items!"); - // } - // } + // } + // } } - + $("#order-sub-total").text(parseFloat(sub_total).toFixed(<%= precision.to_i %>)); }else{ $("#discount-amountErr").html("Discount is greater than sub total!"); } - + } function item_row_template(type, item, discount_amount, amount){ @@ -887,24 +888,24 @@ function item_row_template(type, item, discount_amount, amount){ dis_str = dis_str + "(" + amount + "%)" } var discount_item_row = "" + - "" + - "" + "" + - "" + + "" + "1" + "" + - "" + - "" + + "" + + "" + discount_amount + - "" + + "" + "" + ""; - return discount_item_row; + return discount_item_row; } diff --git a/app/views/reports/receipt_no/index.html.erb b/app/views/reports/receipt_no/index.html.erb index fbd49e3e..ab8edfd0 100755 --- a/app/views/reports/receipt_no/index.html.erb +++ b/app/views/reports/receipt_no/index.html.erb @@ -98,8 +98,8 @@ <%if !@sale_data.nil? %> <% @sale_data.each do |result| %> - <% grand_total +=result.grand_total.to_f %> - <% old_grand_total += result.grand_total_after_rounding() %> + <% grand_total += result.grand_total.to_f %> + <% old_grand_total += result.grand_total.to_f - result.rounding_adjustment.to_f %> <% total_tax += result.total_tax.to_f %> <% total_sum += result.total_amount.to_f %> <% discount_amt += result.total_discount.to_f %> @@ -127,47 +127,35 @@ <%= number_with_precision(result.total_amount, precision: precision.to_i ,delimiter: delimiter) %> <%= number_with_precision(result.total_discount, precision: precision.to_i ,delimiter: delimiter) rescue '0' %> - <% if !result.sale_taxes.empty? %> - <% num = 1 - if tax_flag && tax_count > 0 %> - <% while num <= tax_count %> - - <%= number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> - - <% num += 1 - end %> - <% end %> - <% result.sale_taxes.each do |tax| %> - - <%= number_with_precision(tax.tax_payable_amount, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> - - <%end%> - <% num = 1 - if tax_flag==false && tax_count > 0 %> - <% while num <= tax_count %> - - <%= number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> - - <% num += 1 - end %> - <% end %> - <% else %> - <% @tax_profiles.each do |tax| %> - <%= number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> - <% end %> - <%end%> + <% num = 1 %> + <% if tax_flag && tax_count > 0 %> + <% while num <= tax_count %> + 0 + <% num += 1 %> + <% end %> + <% end %> + <% result.sale_taxes.each do |tax| %> + + <%= number_with_precision(tax.tax_payable_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> + + <%end%> + <% num = 1 %> + <% if !tax_flag && tax_count > 0 %> + <% while num <= tax_count %> + 0 + <% num += 1 %> + <% end %> + <% end %> + <% else %> + <% @tax_profiles.each do |tax| %> + 0 + <% end %> + <%end%> - <% if result.old_grand_total.nil? %> - <%= number_with_precision(result.grand_total, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> - <%else%> - <%= number_with_precision(result.old_grand_total, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> - <%end%> - - <%= result.rounding_adjustment.to_f rescue '-' %> - <%= number_with_precision(result.grand_total, precision: precision.to_i ,delimiter: delimiter) %> + <%= number_with_precision(result.grand_total - result.rounding_adjustment, precision: precision.to_i, delimiter: delimiter) rescue '-' %> + <%= number_with_precision(result.rounding_adjustment.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %> + <%= number_with_precision(result.grand_total, precision: precision.to_i, delimiter: delimiter) %> <% if @lookup.value.to_i == 1 %> diff --git a/app/views/reports/receipt_no/index.xls.erb b/app/views/reports/receipt_no/index.xls.erb index 66734e56..a221934f 100755 --- a/app/views/reports/receipt_no/index.xls.erb +++ b/app/views/reports/receipt_no/index.xls.erb @@ -16,21 +16,21 @@ <% if @shift_from %> - <% if @shift_data.employee %> + <% if @shift_data.employee %> <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> - <% end %> + <% end %> <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> ) - <% end %> + <% end %> <%= t("views.right_panel.detail.dining") %> <%= t("views.right_panel.detail.receipt_no") %> - <%= t :cashier %> <%= t("views.right_panel.detail.name") %> + <%= t :cashier %> <%= t("views.right_panel.detail.name") %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %> <%= t("views.right_panel.detail.discount") %> <%= t("views.right_panel.detail.amount") %> <% @tax_profiles.each do |tax| %> - <%= tax.name %> + <%= tax.name %> <% end %> <%= t("views.right_panel.detail.grand_total") %> @@ -45,36 +45,36 @@ <% old_grand_total = 0 %> <% total_tax = 0 %> <% guest_count = 0 %> - <% total_sum = 0 %> - <% discount_amt = 0 %> + <% total_sum = 0 %> + <% discount_amt = 0 %> <% other_amt = 0 %> - <% total_nett = 0 %> - <% rounding_adj = 0%> <% gov_tax = 0 %> <% service_charge = 0 %> + <% total_nett = 0 %> + <% rounding_adj = 0%> <% gov_tax = 0 %> <% service_charge = 0 %> <% tax_profile_count = @tax_profiles.length %> - <% + <% ttax_count = tax_profile_count - @sale_taxes.length ttax_flag = true - @sale_taxes.each do |tax| + @sale_taxes.each do |tax| if tax.tax_name.downcase.include?("service") ttax_flag = false end - end + end %> - <%if @sale_data %> + <%if @sale_data %> <% @sale_data.each do |result| %> - <% grand_total = grand_total.to_f + result.grand_total.to_f %> - <% old_grand_total = old_grand_total.to_f + result.old_grand_total.to_f %> + <% grand_total += result.grand_total.to_f %> + <% old_grand_total += result.grand_total.to_f - result.rounding_adjustment.to_f %> <% total_tax += result.total_tax.to_f %> - <% total_sum += result.total_amount.to_f %> + <% total_sum += result.total_amount.to_f %> <% discount_amt += result.total_discount.to_f %> <% rounding_adj += result.rounding_adjustment.to_f %> <% sale_tax_count = result.sale_taxes.length %> <% tax_count = tax_profile_count - sale_tax_count %> <% tax_flag = true %> - <% result.sale_taxes.each do |tax| + <% result.sale_taxes.each do |tax| if tax.tax_name.downcase.include?("service") tax_flag = false end @@ -82,7 +82,7 @@ <%if result.type %> - <%= result.type %> - <%= result.name %> + <%= result.type %> - <%= result.name %> <% else %> - <% end %> @@ -91,15 +91,13 @@ <%= result.cashier_name rescue '-' %> <%= result.total_amount rescue '-' %> <%= result.total_discount rescue '-' %> - + <% if !result.sale_taxes.empty? %> - <% num = 1 + <% num = 1 if tax_flag && tax_count > 0 %> <% while num <= tax_count %> 0 - <% num += 1 + <% num += 1 end %> <% end %> <% result.sale_taxes.each do |tax| %> @@ -107,11 +105,11 @@ <%= tax.tax_payable_amount rescue '-' %> <%end%> - <% num = 1 + <% num = 1 if tax_flag==false && tax_count > 0 %> <% while num <= tax_count %> 0 - <% num += 1 + <% num += 1 end %> <% end %> <% else %> @@ -120,9 +118,9 @@ <% end %> <%end%> - <%= result.grand_total %> - <%= result.rounding_adjustment.to_f rescue '-' %> - <%= result.grand_total_after_rounding() rescue '-'%> + <%= result.grand_total - result.rounding_adjustment %> + <%= result.rounding_adjustment.to_f rescue '-' %> + <%= result.grand_total rescue '-'%> <% end %> @@ -134,11 +132,11 @@ <%= tax.st_amount.round(2) %> <% end %> --> <% if !@sale_taxes.empty? %> - <% num = 1 + <% num = 1 if ttax_flag && ttax_count > 0 %> <% while num <= ttax_count %> 0.00 - <% num += 1 + <% num += 1 end %> <% end %> <% @sale_taxes.each do |tax| %> @@ -146,11 +144,11 @@ <%= tax.st_amount.to_f.round(2) %> <%end%> - <% num = 1 + <% num = 1 if ttax_flag==false && ttax_count > 0 %> <% while num <= ttax_count %> 0.00 - <% num += 1 + <% num += 1 end %> <% end %> <% else %> @@ -158,15 +156,15 @@ 0.00 <% end %> <%end%> - <%= grand_total.to_f.round(2) rescue '-'%> - <%= rounding_adj rescue '-'%> - <%= grand_total.to_f.round + rounding_adj %> + <%= grand_total.to_f.round(2) rescue '-'%> + <%= rounding_adj rescue '-'%> + <%= grand_total.to_f.round + rounding_adj %>   <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %> <%= t("views.right_panel.detail.discount") %> <%= t("views.right_panel.detail.amount") %> - + <% @tax_profiles.each do |tax| %> <%= tax.name %> <% end %> @@ -184,4 +182,4 @@ - \ No newline at end of file + From 834df19e037ed0cfbc8c152fdd1d12082d083342 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Fri, 8 Nov 2019 09:58:52 +0630 Subject: [PATCH 22/28] use precision for rounding adj in receipt report. bug fixes for discount. --- app/views/origami/discounts/index.html.erb | 200 ++++++++++---------- app/views/reports/receipt_no/index.html.erb | 25 +-- 2 files changed, 116 insertions(+), 109 deletions(-) diff --git a/app/views/origami/discounts/index.html.erb b/app/views/origami/discounts/index.html.erb index 86cf26c7..abeeb442 100755 --- a/app/views/origami/discounts/index.html.erb +++ b/app/views/origami/discounts/index.html.erb @@ -44,24 +44,22 @@
-
- - - - - - +
+ +
ItemsQTY
+ + + + - - + + <% sub_total = 0 %> <% @sale_data.sale_items.each do |sale_item| %> - <% - sub_total += sale_item.qty*sale_item.unit_price - if sale_item.price != 0 && sale_item.status != "void" && sale_item.status != "foc" - %> - > + <% sub_total += sale_item.qty * sale_item.unit_price %> + <% if sale_item.price > 0 && sale_item.status.blank? %> + > - <% - end - end - %> - -
ItemsQTY Price
@@ -74,14 +72,26 @@ <%=(sale_item.qty*sale_item.unit_price)%>
-
-
+ <% elsif sale_item.price < 0 && sale_item.status == "Discount" %> + > + + + + <%=sale_item.product_name%>@<%=sale_item.unit_price%> + + + <%=sale_item.qty.abs%> + + + <%=(sale_item.qty*sale_item.unit_price)%> + + + <% end %> + <% end %> + + + +