class Api::VoidController <  Api::ApiController
   	# skip_before_action  :authenticate
   	def overall_void
		sale_id = params[:sale_id]
		order_source = "emenu" #tax profile source
		if Sale.exists?(sale_id)
			sale = Sale.find_by_sale_id(sale_id)

			if sale.discount_type == "member_discount"
				sale.update_attributes(total_discount: 0)
				sale.compute_by_sale_items(sale_id, sale.sale_items,0,order_source)
			end

			# update count for shift sale
			if(sale.sale_status == "completed")
				if sale.shift_sale_id != nil
				  shift = ShiftSale.find(sale.shift_sale_id)
				  shift.calculate(sale_id, "void")
				end
			else
				# void before sale payment complete
				if sale.shift_sale_id != nil
				  shift = ShiftSale.find(sale.shift_sale_id)
				  shift.total_void = shift.total_void + sale.grand_total
				  shift.save
				end
			end
		        
			sale.rounding_adjustment = 0.0
			sale.payment_status = 'void'
			sale.sale_status = 'void'
			sale.save
		  
		  	# remark = "Void Sale ID #{sale_id} | Receipt No #{sale.receipt_no} | Receipt No #{sale.receipt_no} | Table ->#{table.name}"
		  	sale_audit = SaleAudit.record_audit_for_edit(sale_id,current_login_employee.name, current_login_employee.name,nil,"SALEVOID" )
			render json: JSON.generate({:status => true, :message => "Void successful."})
		else
			render json: JSON.generate({:status => false, :error_message => "There is no sale for '#{params[:sale_id]}'!"})
		end
	end 
end

