json.status @status
json.message @message

if @status
  json.data do
    json.shop do
      json.name current_shop.name
      json.address current_shop.address
      json.phone_no current_shop.phone_no
    end

    json.cashier         @shift.employee.name
    json.cashier_station @shift.cashier_terminal.name
    json.opening_date    @shift.shift_started_at.strftime('%d-%m-%Y %I:%M %p')
    json.closing_date    @shift.shift_closed_at.strftime('%d-%m-%Y %I:%M %p')
    json.opening_float   @shift.opening_balance
    json.closing_float   @shift.closing_balance

    json.sale_by_payments do
      json.child! do
        json.name   'Cash'
        json.total_amount @shift.credit_sales
      end
      json.child! do
        json.name   'Credit'
        json.total_amount @shift.credit_sales
      end

      @payment_methods.each do |payment_method|
        json.child! do
          json.name payment_method
          json.total_amount @other_payment.first[payment_method.parameterize.to_sym]
        end
      end
    end

    json.rounding_adjustment @shift.total_rounding
    json.total               @shift.grand_total

    json.sale_taxes @sale_taxes do |sale_tax|
      json.tax_name sale_tax.tax_name
      json.tax_amount sale_tax.st_amount
    end

    json.total_taxes @shift.total_taxes
    json.nett_sales  @shift.nett_sales

    json.total_credit_payment @total_credit_payments

    json.sale_discounts do
      @total_discount_by_account.each do |account|
        json.child! do
          json.name   account.account_name
          json.total_amount account.total_price
        end
      end

      if @total_member_discount && @total_member_discount.first.member_discount
        json.child! do
          json.name "Member"
          json.total_amount @total_member_discount.first.member_discount
        end
      elsif @total_discount_by_account.size < 1
        json.array!
      end
    end

    json.total_discount @shift.total_discounts

    json.total_foc @other_payment.first.foc_amount
    json.total_void @shift.total_void.to_d
    json.total_waste @total_waste
    json.total_spoile @total_spoile

    json.sale_by_accounts @total_amount_by_account do |account|
      json.name account.account_name
      json.total_amount account.total_price
    end

    json.total_other_amount @total_other_charges
    json.total_dine_in_count @shift.dining_count
    json.total_takeaway_count @shift.takeaway_count
    json.total_receipts @shift.total_receipt

    if @sale_items.present?
      json.sale_items_summary do
        total_items = 0
        total_amount = BigDecimal("0.0")
        json.sale_items_groups do
          @sale_items.group_by(&:menu_category_name).each do |menu|
            json.child! do
              json.category_name menu[0]
              json.sale_items menu[1] do |item|
                json.product_name item.product_name
                json.unit_price   item.unit_price
                json.total_qty    item.total_item
                json.total_price  item.grand_total

                total_items += item.total_item
                total_amount += item.grand_total
              end
            end
          end

          unless @total_other_charges_info.nil?
            json.child! do
              json.category_name 'Other Charges'
              json.sale_items @total_other_charges_info do |item|
                json.product_name item.product_name
                json.unit_price   item.unit_price
                json.total_qty    item.total_item
                json.total_price  item.grand_total

                total_items += item.total_item
                total_amount += item.grand_total
              end
            end
          end
        end

        json.total_items total_items
        json.total_amount total_amount
      end
    end
  end
end
