namespace :consolidate do
  desc "Receipt"
  task :receipt => :environment do
    # Receipt.delete_all
    sales = Sale.all_receipts
    shop  = Shop.current_shop #Shop.current_shop
    sales.each do |sale|
      
     if sale.shift_sale_id.present?
       shift = ShiftSale.find(sale.shift_sale_id)
      receipt = Receipt.new
      receipt.client_code         = shop.client_code
      receipt.client_name         = shop.client_name
      receipt.shop_code           = shop.shop_code
      receipt.shop_name           = shop.name
      receipt.receipt_no          = sale.receipt_no
      receipt.transaction_time    = sale.requested_at #requet-at
      receipt.receipt_open_time   = sale.requested_at #before disco/after tax /after/ser_char
      receipt.receipt_close_time  = sale.receipt_close_time
      receipt.shift_id            = sale.shift_sale_id
      receipt.shift_open_time     = shift.shift_started_at
      receipt.shift_close_time    = shift.shift_closed_at
      receipt.gross_sales         = sale.total_amount + sale.total_tax #before disco/after tax /after/ser_char
      receipt.discount_amount     = sale.total_discount + sale.item_discount
      receipt.sales               = sale.grand_total #after disco/after tax /after/ser_char /no roundingadn
      receipt.tax_amount          = sale.total_tax
      receipt.service_charges     = sale.service_charges
      receipt.net_sales           = sale.total_amount + sale.total_discount #after disco/before tax / no rounding
      receipt.credit_card_sales   = sale.mpu_amount + sale.master_amount + sale.visa_amount + sale.jcb_amount + sale.unionpay_amount + sale.credit_amount # 
      receipt.voucher_sales       = 0
      receipt.foc_amount          = sale.foc_amount + sale.item_foc
      receipt.staff_meal_amount   = 0
      receipt.rounding_amount     = sale.rounding_adjustment
      receipt.raw_wastage_amount  = 0
      receipt.semi_wastage_amount = 0
      receipt.wastage_amount      = 0
      receipt.spoilage_amount     = 0
      receipt.sampling_amount     = 0
      receipt.qty                 = sale.qty
      receipt.no_of_pax           = 0
      receipt.no_of_adult         = (sale.adult) ? sale.adult : 0
      receipt.no_of_child         = (sale.child) ? sale.child : 0
      receipt.terminal_id         = ShiftSale.find(sale.shift_sale_id).cashier_terminal_id
      receipt.employee_code       = Employee.find(sale.cashier_id).emp_id
      receipt.employee_name       = sale.cashier_name
      receipt.is_valid            = (sale.payment_status=="void") ? false : true #void is false
      receipt.overing             =false
      receipt.cancle              = (sale.payment_status=="void") ? false : true # void is cancel
      receipt.remarks             = sale.remark
      receipt.save
     end
    end
  end

  desc "Receipt Detail"
  task :receipt_detail => :environment do
    ReceiptDetail.delete_all
    sales = Sale.all_receipt_details

    shop  = Shop.current_shop #Shop.current_shop

    sale_id = ''
    count   = 0
    @total_qty                 = 0
    @total_gross_sales         = 0
    @total_sales               = 0
    @total_net_sales           = 0
@z =1
    @item_sequence = 1
    sale_count = sales.to_a.count
    sales.each do |sale|
      if sale.qty > 0

        if sale.item_instance_code !=nil
          # byebug
          menu_category = MenuCategory.find(MenuItem.find_by_item_code(sale.product_code).menu_category_id)
          name  = menu_category.name
          code  = menu_category.code
          menu_item = MenuItem.find_by_item_code(sale.product_code)
          menu_name = menu_item.name
          menu_code = menu_item.item_code
          menu_type = menu_item.type
        end
        if sale.product_code == "Other Charges" && sale.status == "Other Charges"
          code  = sale.product_code
          name  = sale.product_code
          menu_name = sale.product_code
          menu_code = sale.product_code
          menu_type = sale.product_code
        end
        
        @receipt_no                = sale.receipt_no
        @requested_at              = sale.requested_at
        # if @receipt_no == '20171130-10464'
        #   byebug
        # end
        if sale_id == sale.sale_id || count == 0 || @z ==0
          @total_qty                 += sale.qty
          @total_gross_sales         += sale.price
          @total_sales               += sale.price
          @total_net_sales           += sale.taxable_price
        end

        if sale_id != sale.sale_id && count !=0 

          receipt = ReceiptDetail.new
          receipt.client_code         = shop.client_code
          receipt.client_name         = shop.client_name
          receipt.shop_code           = shop.shop_code
          receipt.shop_name           = shop.name
          receipt.receipt_no          = @receipt_no
          receipt.receipt_date        = @requested_at #requet-at
          receipt.transaction_date    = @requested_at #before disco/after tax /after/ser_char
          receipt.item_sequence       = @item_sequence
          receipt.category_code       = sale.product_code
          receipt.category_name       = sale.product_name
          receipt.sub_category_code   = 0
          receipt.sub_category_name   = 0
          receipt.report_group_code   = 0
          receipt.report_group_name   = 0
          receipt.item_code           = (sale.product_code.to_s=="NULL") ? "0" : sale.product_code
          receipt.item_name           = sale.product_name
          receipt.qty                 = @total_qty
          receipt.transaction_type    = "Tender"
          receipt.gross_sales         = @total_gross_sales
          receipt.discount_code       = ''
          receipt.discount_amount     = 0
          receipt.sales               = @total_sales
          receipt.tax_amount          = 0
          receipt.service_charges     = 0
          receipt.net_sales           = @total_net_sales
          receipt.is_set_item         = false
          receipt.is_foc              = false
          receipt.is_staff_meal       = false
          receipt.is_raw_wastage      = false
          receipt.is_semi_wastage     = false
          receipt.is_wastage          = false
          receipt.is_spoilage         = false
          receipt.is_sampling         = false
          receipt.tax_able            = false
          receipt.is_void             = false 
          receipt.remarks             = ""
          receipt.save
          @type = "Tender"
          @item_sequence             = 1
          @total_qty                 = 0
          @total_gross_sales         = 0
          @total_sales               = 0
          @total_net_sales           = 0
  @z = 0
        end

        
        discount_amount = (sale.status=="Discount") ? sale.price : 0
          receipt = ReceiptDetail.new
          receipt.client_code         = shop.client_code
          receipt.client_name         = shop.client_name
          receipt.shop_code           = shop.shop_code
          receipt.shop_name           = shop.name
          receipt.receipt_no          = sale.receipt_no
          receipt.receipt_date        = sale.requested_at #requet-at
          receipt.transaction_date    = sale.requested_at #before disco/after tax /after/ser_char
          receipt.item_sequence       = @item_sequence
          receipt.category_code       = sale.product_code
          receipt.category_name       = sale.product_name
          receipt.sub_category_code   = 0
          receipt.sub_category_name   = 0
          receipt.report_group_code   = 0
          receipt.report_group_name   = 0
          receipt.item_code           = (menu_code.to_s=="NULL") ? "0" : sale.product_code
          receipt.item_name           = menu_name
          receipt.qty                 = sale.qty
          receipt.transaction_type    = (sale.status==nil) ? "Sales" : sale.status
          receipt.gross_sales         = sale.price
          receipt.discount_code       = sale.sale_item_id
          receipt.discount_amount     = discount_amount
          receipt.sales               = sale.price
          receipt.tax_amount          = 0
          receipt.service_charges     = 0
          receipt.net_sales           = sale.taxable_price
          receipt.is_set_item         = (menu_type=="SetMenuItem")? true : false 
          receipt.is_foc              = (sale.status=="foc") ? true : false
          receipt.is_staff_meal       = false
          receipt.is_raw_wastage      = false
          receipt.is_semi_wastage     = false
          receipt.is_wastage          = false
          receipt.is_spoilage         = false
          receipt.is_sampling         = false
          receipt.tax_able            = sale.is_taxable
          receipt.is_void             = (sale.status=="void")? true : false 
          receipt.remarks             = sale.remark
          receipt.save
      
      end
      
        @z += 1
     

      count += 1
      @item_sequence += 1
      sale_id = sale.sale_id
    
      if count == sale_count
        receipt = ReceiptDetail.new
            receipt.client_code         = shop.client_code
            receipt.client_name         = shop.client_name
            receipt.shop_code           = shop.shop_code
            receipt.shop_name           = shop.name
            receipt.receipt_no          = @receipt_no
            receipt.receipt_date        = @requested_at #requet-at
            receipt.transaction_date    = @requested_at #before disco/after tax /after/ser_char
            receipt.item_sequence       = @item_sequence
            receipt.category_code       = sale.product_code
            receipt.category_name       = sale.product_name
            receipt.sub_category_code   = 0
            receipt.sub_category_name   = 0
            receipt.report_group_code   = 0
            receipt.report_group_name   = 0
            receipt.item_code           = (menu_code.to_s=="NULL") ? "0" : sale.product_code
            receipt.item_name           = menu_name
            receipt.qty                 = @total_qty
            receipt.transaction_type    = "Tender"
            receipt.gross_sales         = @total_gross_sales
            receipt.discount_code       = ''
            receipt.discount_amount     = 0
            receipt.sales               = @total_sales
            receipt.tax_amount          = 0
            receipt.service_charges     = 0
            receipt.net_sales           = @total_net_sales
            receipt.is_set_item         = false
            receipt.is_foc              = false
            receipt.is_staff_meal       = false
            receipt.is_raw_wastage      = false
            receipt.is_semi_wastage     = false
            receipt.is_wastage          = false
            receipt.is_spoilage         = false
            receipt.is_sampling         = false
            receipt.tax_able            = false
            receipt.is_void             = false 
            receipt.remarks             = ""
            receipt.save
      end

    end
  end

end