class SaleItemsPdf < 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,:item_height,:qty_width,:total_width,:item_description_width,:text_width def initialize(printer_settings, shop_details, period, type, account, from_date, to_date, shift, sale_items, acc_cate_count, menu_cate_count, total_by_acc) self.page_width = printer_settings.page_width #PrintSetting.where("name = ?","Close Cashier").first.page_width self.page_height = printer_settings.page_height self.header_font_size = printer_settings.header_font_size.to_i self.item_font_size = printer_settings.item_font_size.to_i self.margin = 5 self.price_width = 60 self.qty_width = 20 self.total_width = 40 self.item_width = self.page_width - ((self.price_width + self.qty_width + self.total_width)) self.item_height = 15 self.item_description_width = (self.page_width-20) / 2 @label_name_width = 70 @label_item_width = self.page_width-@label_name_width self.label_width = 100 self.text_width = (self.page_width - 80) - self.price_width / 3 # @item_width = self.page_width.to_i / 2 # @qty_width = @item_width.to_i / 3 # @double = @qty_width * 1.3 # @half_qty = @qty_width / 2 #setting page margin and width super(:margin => [printer_settings.heading_space, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height]) # db font setup if printer_settings.font != "" font_families.update("#{printer_settings.font}" => { :normal => "public/fonts/#{printer_settings.font}.ttf", :italic => "public/fonts/#{printer_settings.font}.ttf", :bold => "public/fonts/#{printer_settings.font}.ttf", :bold_italic => "public/fonts/#{printer_settings.font}.ttf" }) font "#{printer_settings.font}" fallback_fonts ["Courier", "Helvetica", "Times-Roman"] end # font "public/fonts/Zawgyi-One.ttf" # font "public/fonts/padauk.ttf" #precision checked if printer_settings.precision.to_i > 2 printer_settings.precision = 2 end #check delimiter if printer_settings.delimiter delimiter = "," else delimiter = "" end header( shop_details) stroke_horizontal_rule sale_details(period, type, account, from_date, to_date, shift) sale_items_detail(sale_items, acc_cate_count, menu_cate_count, total_by_acc) end def header (shop_details) move_down 7 text "#{shop_details.name}", :left_margin => -10, :size => self.header_font_size,:align => :center move_down 5 text "#{shop_details.address}", :size => self.item_font_size,:align => :center # move_down self.item_height move_down 5 text "#{shop_details.phone_no}", :size => self.item_font_size,:align => :center move_down 5 stroke_horizontal_rule end def sale_details(period, type, account, from_date, to_date, shift) move_down 7 y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "Period : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{period}" , :size => self.item_font_size,:align => :left end y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "Type : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{type}" , :size => self.item_font_size,:align => :left end y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "Account : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{account}" , :size => self.item_font_size,:align => :left end y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "From Date : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{ from_date}" , :size => self.item_font_size,:align => :left end y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "To Date : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{ to_date}" , :size => self.item_font_size,:align => :left end y_position = cursor bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do text "Shift : ", :size => self.item_font_size,:align => :left end bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do text "#{shift}" , :size => self.item_font_size,:align => :left end move_down 10 end def sale_items_detail(sale_items, acc_cate_count, menu_cate_count, total_by_acc) self.item_width = 73 self.price_width = 60 item_label_qty_front_width = (self.item_width+self.price_width) + 2 item_label_qty_end_width = 32 item_label_total_front_width = (self.item_width+self.price_width) + 2 item_label_total_end_width = 64 y_position = cursor bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do text "Sale Items Summary", :size => self.header_font_size, :align => :center end total_items = 0 total_amount = 0 total_qty = 0 sub_total = 0 arr = Array.new unless sale_items.nil? JSON.parse(sale_items).each do |item| if !arr.include?(item['menu_category_id']) unless total_qty == 0 and sub_total == 0 total_details('Sub Total', total_qty, sub_total) end total_qty = 0 sub_total = 0 move_down 10 y_position = cursor bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do text "#{item['menu_category_name']}", :size => self.header_font_size, :align => :left end # write_stroke_dash # dash(1, :space => 1, :phase => 1) # stroke_horizontal_rule # move_down 2 y_position = cursor pad_top(15) { text_box "Items", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix text_box "Price", :at =>[(self.item_width),y_position], :width => self.price_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix text_box "Qty", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix text_box "Total", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix } move_down 2 stroke_horizontal_rule end move_down 3 y_position = cursor pad_top(15) { text_box "#{item['product_name']}", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix text_box "#{item['unit_price'].to_i}", :at =>[(self.item_width),y_position], :width => self.price_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix text_box "#{item['total_item'].to_i}", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix text_box "#{item['grand_total'].to_i}", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix } total_qty += item['total_item'].to_i sub_total += item['grand_total'].to_i total_items += item['total_item'].to_i total_amount += item['grand_total'].to_i arr.push(item['menu_category_id']) end # stroke_horizontal_rule total_details('Sub Total', total_qty, sub_total) move_down 5 total_details('Total Amount', total_items, total_amount) move_down 10 end end def total_details(col_name, total_qty, sub_total) self.item_width = 73 self.price_width = 60 item_label_qty_front_width = (self.item_width+self.price_width) + 2 item_label_qty_end_width = 32 item_label_total_front_width = (self.item_width+self.price_width) + 2 item_label_total_end_width = 64 move_down 5 stroke_horizontal_rule y_position = cursor pad_top(15) { text_box "#{col_name}", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix text_box "#{total_qty.to_i}", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix text_box "#{sub_total.to_i}", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix } end end