From d910c4078bc3bec66d464b302f32e77777981494 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 24 May 2018 17:02:54 +0630 Subject: [PATCH] Add Order item & summary customise font pdf --- README.md | 11 ++ app/controllers/api/orders_controller.rb | 6 + app/controllers/oqs/edit_controller.rb | 6 + app/controllers/oqs/print_controller.rb | 12 ++ app/models/order_queue_station.rb | 12 ++ app/models/printer/order_queue_printer.rb | 30 +++++ app/models/printer/receipt_printer.rb | 36 ++++++ app/pdf/order_item_customise_pdf.rb | 145 +++++++++++++++++++++ app/pdf/order_summary_customise_pdf.rb | 147 ++++++++++++++++++++++ 9 files changed, 405 insertions(+) create mode 100644 app/pdf/order_item_customise_pdf.rb create mode 100644 app/pdf/order_summary_customise_pdf.rb diff --git a/README.md b/README.md index 146f216e..4e817c7a 100755 --- a/README.md +++ b/README.md @@ -53,6 +53,17 @@ For Order Item & Order Summary Slim app/models/printer/receipt_printer.rb } +For Order Item & Order Summary Customise PDF + *** change OrderItemPdf to OrderItemCustomisePdf and OrderSummaryPdf to OrderSummaryCustomisePdf + 1) settings/lookups => { type:print_settings, name:OrderCustomisePdf, value:1 } + * no need to change these files + { app/controllers/oqs/edit_controller.rb + app/controllers/oqs/print_controller.rb + app/models/order_queue_station.rb + app/models/printer/order_queue_printer.rb + app/models/printer/receipt_printer.rb + * Can change Header font size and Item Font Size as you like + For ReceiptBillA5Pdf *** change ReceiptBillPdf to ReceiptBillA5Pdf 1) settings/print_settings , width:680, height:1450, Header font Size:16, Item font size:14 diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 49afe833..1e010007 100755 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -244,6 +244,12 @@ class Api::OrdersController < Api::ApiController else unique_code="OrderItemPdf" end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + unique_code="OrderItemCustomisePdf" + else + unique_code="OrderItemPdf" + end end end end diff --git a/app/controllers/oqs/edit_controller.rb b/app/controllers/oqs/edit_controller.rb index 73458b82..49addea1 100644 --- a/app/controllers/oqs/edit_controller.rb +++ b/app/controllers/oqs/edit_controller.rb @@ -65,6 +65,12 @@ class Oqs::EditController < BaseOqsController else unique_code="OrderItemPdf" end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + unique_code="OrderItemCustomisePdf" + else + unique_code="OrderItemPdf" + end end end end diff --git a/app/controllers/oqs/print_controller.rb b/app/controllers/oqs/print_controller.rb index bf5b803a..1fd3f7f1 100755 --- a/app/controllers/oqs/print_controller.rb +++ b/app/controllers/oqs/print_controller.rb @@ -20,6 +20,12 @@ class Oqs::PrintController < ApplicationController else unique_code="OrderItemPdf" end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + unique_code="OrderItemCustomisePdf" + else + unique_code="OrderItemPdf" + end end end end @@ -68,6 +74,12 @@ class Oqs::PrintController < ApplicationController else unique_code="OrderSummaryPdf" end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + unique_code="OrderSummaryCustomisePdf" + else + unique_code="OrderSummaryPdf" + end end end end diff --git a/app/models/order_queue_station.rb b/app/models/order_queue_station.rb index d731c3d2..54435658 100755 --- a/app/models/order_queue_station.rb +++ b/app/models/order_queue_station.rb @@ -223,6 +223,12 @@ class OrderQueueStation < ApplicationRecord else unique_code="OrderSummaryPdf" end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + unique_code="OrderSummaryCustomisePdf" + else + unique_code="OrderSummaryPdf" + end end end end @@ -257,6 +263,12 @@ class OrderQueueStation < ApplicationRecord else unique_code="OrderItemPdf" end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + unique_code="OrderItemCustomisePdf" + else + unique_code="OrderItemPdf" + end end end end diff --git a/app/models/printer/order_queue_printer.rb b/app/models/printer/order_queue_printer.rb index 62e6f0a7..f00d898f 100755 --- a/app/models/printer/order_queue_printer.rb +++ b/app/models/printer/order_queue_printer.rb @@ -32,6 +32,12 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker else pdf = OrderItemPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty) end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + pdf = OrderItemCustomisePdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty) + else + pdf = OrderItemPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty) + end end end end @@ -97,6 +103,12 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker else pdf = OrderItemPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty) end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + pdf = OrderItemCustomisePdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty) + else + pdf = OrderItemPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty) + end end end end @@ -136,6 +148,12 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty) end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty) + else + pdf = OrderSummaryPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty) + end end end end @@ -195,6 +213,12 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker else pdf = OrderItemPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty) end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + pdf = OrderItemCustomisePdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty) + else + pdf = OrderItemPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty) + end end end end @@ -236,6 +260,12 @@ class Printer::OrderQueuePrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty) end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty) + else + pdf = OrderSummaryPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty) + end end end end diff --git a/app/models/printer/receipt_printer.rb b/app/models/printer/receipt_printer.rb index 8562fca6..b9a693aa 100755 --- a/app/models/printer/receipt_printer.rb +++ b/app/models/printer/receipt_printer.rb @@ -20,6 +20,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderItemPdf.new end + elsif order_item_slim[0] == 'OrderCustomisePdf' + if order_item_slim[1] == '1' + pdf = OrderItemCustomisePdf.new + else + pdf = OrderItemPdf.new + end end end end @@ -51,6 +57,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new + else + pdf = OrderSummaryPdf.new + end end end end @@ -82,6 +94,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new + else + pdf = OrderSummaryPdf.new + end end end end @@ -113,6 +131,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new + else + pdf = OrderSummaryPdf.new + end end end end @@ -144,6 +168,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new + else + pdf = OrderSummaryPdf.new + end end end end @@ -175,6 +205,12 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker else pdf = OrderSummaryPdf.new end + elsif order_summary_slim[0] == 'OrderCustomisePdf' + if order_summary_slim[1] == '1' + pdf = OrderSummaryCustomisePdf.new + else + pdf = OrderSummaryPdf.new + end end end end diff --git a/app/pdf/order_item_customise_pdf.rb b/app/pdf/order_item_customise_pdf.rb new file mode 100644 index 00000000..4124a075 --- /dev/null +++ b/app/pdf/order_item_customise_pdf.rb @@ -0,0 +1,145 @@ +class OrderItemCustomisePdf < Prawn::Document + include ActionView::Helpers::NumberHelper + attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:order_no_font_size,:item_height,:qty_width,:total_width,:item_description_width + def initialize(print_settings,order_item, print_status, options, alt_name, before_updated_qty) + self.page_width = print_settings.page_width + self.page_height = print_settings.page_height + self.header_font_size = print_settings.header_font_size.to_i + self.item_font_size = print_settings.item_font_size.to_i + self.order_no_font_size = 8 + self.margin = 0 + self.price_width = 40 # No Need for item + self.qty_width = 40 + self.total_width = 40 # No Need for item + self.item_width = self.page_width - (self.qty_width - self.margin) + self.item_height = 15 + self.item_description_width = self.page_width - (self.price_width + self.qty_width + self.total_width) + self.label_width=90 + + super(:margin => [print_settings.heading_space, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height]) + # super(:margin => [10, 5, 30, 5], :page_size => [200,400]) + + # db font setup + if print_settings.font != "" + font_families.update("#{print_settings.font}" => { + :normal => "public/fonts/#{print_settings.font}.ttf", + :italic => "public/fonts/#{print_settings.font}.ttf", + :bold => "public/fonts/#{print_settings.font}.ttf", + :bold_italic => "public/fonts/#{print_settings.font}.ttf" + }) + + font "#{print_settings.font}" + fallback_fonts ["Courier", "Helvetica", "Times-Roman"] + end + # font "public/fonts/Zawgyi-One.ttf" + # font "public/fonts/padauk.ttf" + #font "public/fonts/Chinese.ttf" + if !order_item.dining.nil? + text "#{ order_item.type + '-' + order_item.dining + print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20 + else + text "#{ print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20 + end + + stroke_horizontal_rule + move_down 3 + + #order_info + order_info(order_item.order_id, order_item.order_by,order_item.order_at) + + # order items + order_items(order_item, options, alt_name, print_settings.precision, before_updated_qty) + end + + # Write Order Information to PDF + def order_info(order_no, order_by, order_at) + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "OrderNo: #{order_no} ", :size => self.order_no_font_size,:align => :left + end + + move_down 1 + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "OrderBy: #{order_by} ", :size => self.order_no_font_size,:align => :left + end + + move_down 1 + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "Date: #{order_at.utc.getlocal.strftime("%Y-%m-%d %I:%M %p")}", :size => self.order_no_font_size,:align => :left + end + + stroke_horizontal_rule + + move_down 10 + end + + # Write Order items to PDF + def order_items(order_item, options, alt_name, precision, before_updated_qty) + y_position = cursor + + #Add Order Item + add_order_items(order_item, options, alt_name, precision) + + dash(1, :space => 1, :phase => 1) + stroke_horizontal_line 0, (self.page_width - self.margin) + add_updated_qty_text(before_updated_qty, order_item.qty, precision) + move_down 5 + end + + # Add order items under order info + def add_order_items(order_item, options, alt_name, precision) + y_position = cursor + + move_down 5 + + bounding_box([0,y_position], :width => self.item_width) do + text "#{order_item.item_code} - #{order_item.item_name}", :size => self.item_font_size,:align => :left + end + + bounding_box([self.item_width,y_position], :width => self.qty_width) do + text "[#{number_with_precision(order_item.qty.to_i, :precision => precision.to_i)}]", :size => self.item_font_size,:align => :left + end + + bounding_box([0,y_position], :width => self.item_width) do + text "#{order_item.item_code} - #{order_item.item_name}", :size => self.item_font_size,:align => :left + + end + + if alt_name + if order_item.alt_name + move_down 4 + font("public/fonts/NotoSansCJKtc-Regular.ttf") do + text "(#{order_item.alt_name})", :size => self.item_font_size,:align => :left, :inline_format => true + end + end + end + + if !options.empty? + move_down 5 + + # add option + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "#{options}", :size => self.item_font_size,:align => :left + end + end + + move_down 5 + + end + + #add updated qty text + def add_updated_qty_text(before_updated_qty, updated_qty, precision) + if before_updated_qty.to_i > 0 && !before_updated_qty.nil? + move_down 5 + + # add option + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "* Change quantity [#{number_with_precision(before_updated_qty.to_i, :precision => precision.to_i)}] to [#{number_with_precision(updated_qty.to_i, :precision => precision.to_i)}]", :size => self.item_font_size,:align => :left + end + end + end + +end diff --git a/app/pdf/order_summary_customise_pdf.rb b/app/pdf/order_summary_customise_pdf.rb new file mode 100644 index 00000000..666d6755 --- /dev/null +++ b/app/pdf/order_summary_customise_pdf.rb @@ -0,0 +1,147 @@ +class OrderSummaryCustomisePdf < Prawn::Document + include ActionView::Helpers::NumberHelper + attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:order_no_font_size,:item_height,:qty_width,:total_width,:item_description_width + def initialize(print_settings,order, print_status, order_items = nil,alt_name,before_updated_qty) + self.page_width = print_settings.page_width + self.page_height = print_settings.page_height + self.header_font_size = print_settings.header_font_size.to_i + self.item_font_size = print_settings.item_font_size.to_i + self.order_no_font_size = 8 + self.margin = 0 + self.price_width = 40 # No Need for item + self.qty_width = 40 + self.total_width = 40 # No Need for item + self.item_width = self.page_width - (self.qty_width - self.margin) + self.item_height = 15 + self.item_description_width = self.page_width - (self.price_width + self.qty_width + self.total_width) + self.label_width=90 + + super(:margin => [print_settings.heading_space, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height]) + + # db font setup + if print_settings.font != "" + font_families.update("#{print_settings.font}" => { + :normal => "public/fonts/#{print_settings.font}.ttf", + :italic => "public/fonts/#{print_settings.font}.ttf", + :bold => "public/fonts/#{print_settings.font}.ttf", + :bold_italic => "public/fonts/#{print_settings.font}.ttf" + }) + + font "#{print_settings.font}" + fallback_fonts ["Courier", "Helvetica", "Times-Roman"] + end + + # font "public/fonts/Zawgyi-One.ttf" + # font "public/fonts/padauk.ttf" + + if !order[0].dining.nil? + text "#{ order[0].type + '-' + order[0].dining + print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20 + else + text "#{ print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20 + end + + stroke_horizontal_rule + move_down 5 + + #order_info + order_info(order[0].order_id, order[0].order_by,order[0].order_at) + + # order items + if order_items == nil + order_items(order, alt_name, print_settings.precision) + else + order_items(order_items, alt_name, print_settings.precision) + end + end + + # Write Order Information to PDF + def order_info(order_no, order_by, order_at) + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "OrderNo: #{order_no} ", :size => self.order_no_font_size,:align => :left + end + + move_down 1 + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "OrderBy: #{order_by} ", :size => self.order_no_font_size,:align => :left + end + + move_down 1 + y_position = cursor + bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do + text "Date: #{order_at.utc.getlocal.strftime("%Y-%m-%d %I:%M %p")}", :size => self.order_no_font_size,:align => :left + end + + stroke_horizontal_rule + + move_down 10 + end + + # Write Order items to PDF + def order_items(order_item, alt_name, precision) + y_position = cursor + + #Add Order Item + add_order_items(order_item, alt_name, precision) + + end + + # Add order items under order info + def add_order_items(order_item, alt_name, precision) + y_position = cursor + + move_down 5 + + order_item.each do|odi| + # check for item not to show + # if odi.price != 0 + y_position = cursor + + bounding_box([0,y_position], :width => self.item_width) do + text "#{odi.item_code} - #{odi.item_name}", :size => self.item_font_size,:align => :left + + end + + bounding_box([self.item_width,y_position], :width => self.qty_width) do + text " [#{number_with_precision(odi.qty, :precision => precision.to_i)}]", :size => self.item_font_size,:align => :left + end + + bounding_box([0,y_position], :width => self.item_width) do + text "#{odi.item_code} - #{odi.item_name}", :size => self.item_font_size,:align => :left + + end + + if alt_name + if !(odi.alt_name).empty? + move_down 4 + font("public/fonts/NotoSansCJKtc-Regular.ttf") do + text "(#{odi.alt_name})", :size => self.item_font_size,:align => :left, :inline_format => true + end + end + + end + + # add option + options = odi.options == "[]"? "" : odi.options + + if options != "" + move_down 5 + + y_position = cursor + bounding_box([0,y_position], :width => self.item_width) do + text "#{options}", :size => self.item_font_size,:align => :left + end + + move_down 5 + end + + move_down 5 + + dash(1, :space => 1, :phase => 1) + stroke_horizontal_line 0, (self.page_width - self.margin) + move_down 5 + # end + end + end +end