namespace :shop do
  desc "Create shop and seed data"
  task :create, [:name, :shop_code, :subdomain, :client_name, :client_code, :address, :phone_no] => :environment do |t, args|

    ActiveRecord::Base.connection.execute("INSERT INTO shops (logo, name, shop_code, subdomain, client_name, client_code, address, township, city, state, country, phone_no, reservation_no, license, activated_at, license_data, base_currency, cloud_url, cloud_token, owner_token, id_prefix, is_rounding_adj, quick_sale_summary, calc_tax_order, show_account_info, note, created_at, updated_at)
    VALUES (NULL, '#{args.name}', '#{args.shop_code}', '#{args.subdomain}', '#{args.client_name}', '#{args.client_code}', '#{args.address}', '', '', '', '', '#{args.phone_no}', '111', '', CURRENT_TIMESTAMP(), 'test', '111', NULL, NULL, NULL, '111', 0, 0, 0, 1, '',CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())")
    Rails.logger.info " Shop created for #{args.name}-#{args.shop_code}"

    gateway_communication_type =Lookup.create([{lookup_type:'gateway_communication_type',name: 'API',value: 'api',shop_code: args.shop_code},
                                              {lookup_type:'gateway_communication_type',name: 'USB',value: 'usb',shop_code: args.shop_code}])
    Rails.logger.info " Finished gateway_communication_type Set Up Data"


    payment_methods = Lookup.create([{lookup_type:'payment_methods',name: 'Cash',value: 'cash',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'CreditNote',value: 'creditnote',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Card - VISA',value: 'visa',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Card - MASTER',value: 'master',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Card - JCB',value: 'jcb',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Card - UnionPay',value: 'unionpay',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Card - MPU',value: 'jcb',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Vochure',value: 'vochure',shop_code: args.shop_code},
                                     {lookup_type:'payment_methods',name: 'Giftcard',value: 'gift',shop_code: args.shop_code}])
    Rails.logger.info " Finished payment_methods Set Up Data"

    payment_status = Lookup.create([{lookup_type:'payment_status',name: 'New',value: 'new',shop_code: args.shop_code},
                                    {lookup_type:'payment_status',name: 'Paid',value: 'paid',shop_code: args.shop_code},
                                    {lookup_type:'payment_status',name: 'Fail',value: 'fail',shop_code: args.shop_code},
                                    {lookup_type:'payment_status',name: 'Outstanding',value: 'outstanding',shop_code: args.shop_code}])
    Rails.logger.info " Finished payment_status Set Up Data"

    sales_status = Lookup.create([{lookup_type:'sales_status',name: 'New',value: 'new',shop_code: args.shop_code},
                                  {lookup_type:'sales_status',name: 'Void',value: 'void',shop_code: args.shop_code},
                                  {lookup_type:'sales_status',name: 'Completed',value: 'completed',shop_code: args.shop_code}])
    Rails.logger.info " Finished sales_status Set Up Data"

    order_status = Lookup.create([{lookup_type:'order_status',name: 'New',value: 'new',shop_code: args.shop_code},
                                  {lookup_type:'order_status',name: 'Completed',value: 'completed',shop_code: args.shop_code}])
    Rails.logger.info " Finished order_status Set Up Data"

    order_item_status = Lookup.create([{lookup_type:'order_item_status',name: 'New',value: 'new',shop_code: args.shop_code},
                                      {lookup_type:'order_item_status',name: 'Processing',value: 'processing',shop_code: args.shop_code},
                                      {lookup_type:'order_item_status',name: 'Served',value: 'served',shop_code: args.shop_code},
                                      {lookup_type:'order_item_status',name: 'BIlled',value: 'billed',shop_code: args.shop_code}])
    Rails.logger.info " Finished order_item_status Set Up Data"

    #order_source [tablet,order_station,emenu,api]
    order_source = Lookup.create([{lookup_type:'order_source',name: 'API',value: 'api',shop_code: args.shop_code},
                                  {lookup_type:'order_source',name: 'Tablet',value: 'tablet',shop_code: args.shop_code},
                                  {lookup_type:'order_source',name: 'EMenu',value: 'emenu',shop_code: args.shop_code},
                                  {lookup_type:'order_source',name: 'Order Station',value: 'order_station',shop_code: args.shop_code}])
    Rails.logger.info " Finished order_source Set Up Data"

    #order_type [dine-in,takeaway,delivery]
    order_type = Lookup.create([{lookup_type:'order_type',name: 'Dine-in',value: 'dine-in',shop_code: args.shop_code},
                                  {lookup_type:'order_type',name: 'Takeaway',value: 'takeaway',shop_code: args.shop_code},
                                  {lookup_type:'order_type',name: 'Delivery',value: 'delivery',shop_code: args.shop_code}])
    Rails.logger.info " Finished order_type Set Up Data"

    #menu_item_type:[simple| set| DIY]
    menu_item_type = Lookup.create([{lookup_type:'menu_item_type',name: 'SIMPLE',value: 'simpleItem',shop_code: args.shop_code},
                                    {lookup_type:'menu_item_type',name: 'Set Menu',value: 'setMenu',shop_code: args.shop_code},
                                    {lookup_type:'menu_item_type',name: 'DIY',value: 'diy',shop_code: args.shop_code}])
    Rails.logger.info " Finished menu_item_type Set Up Data"

    member_group_type = Lookup.create([{lookup_type:'member_group_type',name: 'Normal',value: '1',shop_code: args.shop_code},
                                     {lookup_type:'member_group_type',name: 'Platinum',value: '5',shop_code: args.shop_code},
                                     {lookup_type:'member_group_type',name: 'Silver',value: '4',shop_code: args.shop_code},
                                     {lookup_type:'member_group_type',name: 'Gold',value: '3',shop_code: args.shop_code}])
    Rails.logger.info " Finished member_group_type Set Up Data"

    #menu_item_attribute:[size|]
    menu_item_attribute_type = Lookup.create([{lookup_type:'menu_item_attribute_type',name: 'Size',value: 'size',shop_code: args.shop_code}])
    Rails.logger.info " Finished menu_item_attribute_type Set Up Data"

    #Employee Roles
    employee_roles =  Lookup.create([{lookup_type:'employee_roles',name: 'Cashier',value: 'cashier',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Waiter',value: 'waiter',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Supervisor',value: 'supervisor',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Manager',value: 'manager',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Accountant',value: 'account',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Administrator',value: 'administrator',shop_code: args.shop_code},
                                    {lookup_type:'employee_roles',name: 'Application',value: 'application',shop_code: args.shop_code}])
    Rails.logger.info " Finished employee_roles Set Up Data"

    #booking_status
    booking_status = Lookup.create([{lookup_type:'booking_status',name: 'Available',value: 'available',shop_code: args.shop_code},
                                    {lookup_type:'booking_status',name: 'Reserved',value: 'reserved',shop_code: args.shop_code},
                                    {lookup_type:'booking_status',name: 'Occupied',value: 'occupied',shop_code: args.shop_code},
                                    {lookup_type:'booking_status',name: 'Cleaning',value: 'cleaning',shop_code: args.shop_code},
                                    {lookup_type:'booking_status',name: 'Moved',value: 'moved',shop_code: args.shop_code}])
    Rails.logger.info " Finished booking_status Set Up Data"

    #account_type
    account_type = Lookup.create([{lookup_type:'account_type',name: 'Income',value: '0',shop_code: args.shop_code},
                                  {lookup_type:'account_type',name: 'Expense',value: '1',shop_code: args.shop_code}])
    Rails.logger.info " Finished account_type Set Up Data"

    # sale void reason
    void_reason = Lookup.create([{lookup_type:'void_reason',name: 'Approve By Manager',value: 'Approve By Manager',shop_code: args.shop_code},
                                    {lookup_type:'void_reason',name: 'Customer Mistake',value: 'Customer Mistake',shop_code: args.shop_code},
                                    {lookup_type:'void_reason',name: 'Cashier Mistake',value: 'Cashier Mistake',shop_code: args.shop_code},
                                    {lookup_type:'void_reason',name: 'Waiter Mistake',value: 'Waiter Mistake',shop_code: args.shop_code}])
    Rails.logger.info " Finished void_reason Set Up Data"

    # float_value
    float_value = Lookup.create([{lookup_type:'float_value',name: '500',value: '500',shop_code: args.shop_code},
                                      {lookup_type:'float_value',name: '1000',value: '1000',shop_code: args.shop_code},
                                      {lookup_type:'float_value',name: '5000',value: '5000',shop_code: args.shop_code},
                                      {lookup_type:'float_value',name: '10000',value: '10000',shop_code: args.shop_code}])
    Rails.logger.info " Finished float_value Set Up Data"

    # customer type
    customer_type = Lookup.create([{lookup_type:'customer_type',name: 'Dinein',value: 'Dinein',shop_code: args.shop_code},
                                    {lookup_type:'customer_type',name: 'FoodCourt',value: 'FoodCourt',shop_code: args.shop_code},
                                    {lookup_type:'customer_type',name: 'Takeaway',value: 'Takeaway',shop_code: args.shop_code},
                                    {lookup_type:'customer_type',name: 'Delivery',value: 'Delivery',shop_code: args.shop_code}])
    Rails.logger.info " Finished customer_type Set Up Data"

    #unit
    units = Lookup.create([{lookup_type:'unit',name: 'PCS',value: 'pcs',shop_code: args.shop_code},
                           {lookup_type:'unit',name: 'KG',value: 'kg',shop_code: args.shop_code}])

    Rails.logger.info " Finished units Set Up Data"

    # Country
    countries =  Lookup.create({lookup_type:'country',name: 'Japan',value: 'Japan',shop_code: args.shop_code})
    Rails.logger.info " Finished countries Set Up Data"

    # number formats
    number_formats = Lookup.create([{lookup_type: 'number_format',name: 'precision',value: '2',shop_code: args.shop_code},
                                    {lookup_type: 'number_format',name: 'delimiter',value:',',shop_code: args.shop_code},
                                    {lookup_type: 'number_format',name: 'strip_insignificant_zeros',value:'0',shop_code: args.shop_code}])
    Rails.logger.info " Finished number_formats Set Up Data"

    # checkin_time_limit
    checkin_time_limit = Lookup.create([{lookup_type: 'checkin_time_limit',name: 'CheckinTimeLimit',value: '48',shop_code: args.shop_code}])
    Rails.logger.info " Finished checkin_time_limit Set Up Data"

    # display_type
    display_type =  Lookup.create({lookup_type:'display_type',name: 'Display Type',value: '2',shop_code: args.shop_code})
    Rails.logger.info " Finished display_type Set Up Data"

    # show_total_before_tax
    show_total_before_tax =  Lookup.create({lookup_type:'show_total_before_tax',name: 'Show Total Before Tax',value: '0',shop_code: args.shop_code})
    Rails.logger.info " Finished show_total_before_tax Set Up Data"

    # tax_profiles
    tax_profiles = Lookup.create([{lookup_type:'tax_profiles',name: 'Cashier',value: 'cashier',shop_code: args.shop_code},
                                    {lookup_type:'tax_profiles',name: 'FoodCourt',value: 'food_court',shop_code: args.shop_code},
                                    {lookup_type:'tax_profiles',name: 'Online Order',value: 'online_order',shop_code: args.shop_code}])
    Rails.logger.info " Finished tax_profiles Set Up Data"

    # changable_tax
    changable_tax = Lookup.create([{lookup_type:'changable_tax',name: 'change',value: '1',shop_code: args.shop_code}])

    Rails.logger.info " Finished changable_tax Set Up Data"

    # expity_time
    expity_time = Lookup.create([{lookup_type:'expiry_time',name: 'login',value: '30',shop_code: args.shop_code}])
    Rails.logger.info " Finished expity_time Set Up Data"

    #foodcourt use
    foodcourt = Lookup.create([{lookup_type:'food_court',name: 'FoodCourt',value: '1',shop_code: args.shop_code}])
    Rails.logger.info " Finished foodcourt Set Up Data"

    # Tax Profile FoodCourt
    commercial_taxes_food_court = TaxProfile.create({name: "Commercial Tax",group_type: "food_court",rate:5.0,order_by:2,created_by:"SYSTEM DEFAULT",shop_code: args.shop_code})
    service_charges_food_court = TaxProfile.create({name: "Service Charges",group_type: "food_court",rate:0,order_by:1,created_by:"SYSTEM DEFAULT",shop_code: args.shop_code})
    Rails.logger.info " Finished Tax Profile FoodCourt Set Up Data"

    #Tax Profile Online Order
    commercial_taxes_online_order = TaxProfile.create({name: "Commercial Tax",group_type: "online_order",rate:5.0,order_by:2,created_by:"SYSTEM DEFAULT",shop_code: args.shop_code})
    service_charges_online_order = TaxProfile.create({name: "Service Charges",group_type: "online_order",rate:0,order_by:1,created_by:"SYSTEM DEFAULT",shop_code: args.shop_code})
    convenience_charges_online_order= TaxProfile.create({name: "Convenience Charges",group_type: "online_order",rate:5.0,order_by:1,created_by:"SYSTEM DEFAULT",shop_code: args.shop_code})
    Rails.logger.info " Finished Tax Profile Online Order Set Up Data"

    tax_ids =[]
    tax_ids << "#{commercial_taxes_food_court.id}"
    tax_ids << "#{service_charges_food_court.id}"
    takeaway_tax =["#{service_charges_food_court.id}"]
    # # Default CUSTOMER
    ActiveRecord::Base.connection.execute("INSERT INTO customers(customer_id,name,email,contact_no,customer_type,tax_profiles,shop_code) VALUES ('CUS-L#{args.shop_code}00000001','WALK-IN','cus1@customer.com','000000000','Dinein','#{tax_ids}','#{args.shop_code}'),('CUS-L#{args.shop_code}00000002','TAKEAWAY','cus2@customer.com','111111111','Takeaway','#{takeaway_tax}','#{args.shop_code}');")
    Rails.logger.info " Finished Default CUSTOMER Set Up Data"

    #seed_generators
    ActiveRecord::Base.connection.execute("INSERT INTO seed_generators(model,current,next,shop_code,created_at,updated_at) VALUES ('Customer',3,4,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('TableBooking',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('Order',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('OrderItem',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('AssignedOrderItem',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('sale',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('SaleOrder',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('SaleItem',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('SaleAudit',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('SalePayment',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('SaleTax',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()),('Reservation',1,2,'#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP());")
    Rails.logger.info " Finished seed_generators Set Up Data"


    #Create Adminstrator employee
    ActiveRecord::Base.connection.execute("INSERT INTO employees(name,role,password_digest,emp_id,created_by,shop_code,created_at,updated_at,app_id,app_token) VALUES ('Administrator','administrator','$2a$10$OKFA4b4yh0xCJNmzjMMijusnO712LUSEL4/y56p2XmdKBRPWgXwDa','999','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Cashier','cashier','$2a$10$rXWBQro.YqUu5klH9oxqA.vcdq7webHm75FeXylY4uoJlek82luOq','333','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL),('Manager','manager','$2a$10$JmzePPoejam8alpR6hrjr.8jIe/pm36IhErtoP4WwK788x2JfHUL2','777','SYSTEM DEFAULT','#{args.shop_code}',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),NULL,NULL);")
    Rails.logger.info " Finished Create Adminstrator employee Set Up Data"

    #PrintSetting
    order_item_printer = PrintSetting.create({name: "OrderItemPdf",unique_code: "OrderItemPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    order_summary_printer = PrintSetting.create({name: "Order Summary",unique_code: "OrderSummaryPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    request_bill_printer = PrintSetting.create({name: "Receipt Bill",unique_code: "ReceiptBillPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    close_cashier_printer = PrintSetting.create({name: "Close Cashier",unique_code: "CloseCashierPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    crm_order_printer = PrintSetting.create({name: "CRM Order",unique_code: "CrmOrderPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    queue_no_printer = PrintSetting.create({name: "Queue No",unique_code: "QueueNoPdf",printer_name: "Cashier",api_settings: 'socket://192.168.1.118',print_copies: 1,header_font_size: 12,item_font_size: 8,precision: "0",delimiter: 0,heading_space: 5,page_width: 210,page_height: 1450,shop_code: args.shop_code})
    Rails.logger.info " Finished PrintSetting Set Up Data"


    member_setting = MembershipSetting.create({membership_type:"paypar_url",gateway_url: "http://www.smartpay.net",shop_code: args.shop_code})
    Rails.logger.info " Finished member_setting Set Up Data"

    member_actions= MembershipAction.create([{membership_type:"get_account_balance",is_active:1,gateway_url:"/api/membership_campaigns/get_correspond_account_data",additional_parameter:{campaign_type_id:1},shop_code: args.shop_code},
      {membership_type:"redeem",is_active:1,gateway_url:"/api/membership_campaigns/redeem",additional_parameter:{campaign_type_id:1},shop_code: args.shop_code},
      {membership_type:"create_membership_customer",is_active:1,gateway_url:"/api/generic_customer/create_membership_customer",shop_code: args.shop_code},
      {membership_type:"update_membership_customer",is_active:1,gateway_url:"/api/generic_customer/update_membership_customer",shop_code: args.shop_code},
      {membership_type:"get_all_member_group",is_active:1,gateway_url:"/api/member_group/get_all_member_group",shop_code: args.shop_code},
      {membership_type:"rebate",is_active:1,gateway_url:"/api/membership_campaigns/rebate",additional_parameter:{campaign_type_id:1},shop_code: args.shop_code},
      {membership_type:"get_all_member_account",is_active:1,gateway_url:"/api/generic_customer/get_member_data",shop_code: args.shop_code},
      {membership_type:"get_member_transactions",is_active:1,gateway_url:"/api/generic_customer/get_membership_transactions",shop_code: args.shop_code},
      {membership_type:"member_discount",is_active:1,gateway_url:"/api/membership_campaigns/discount",additional_parameter:{campaign_type_id:5},shop_code: args.shop_code},
      {membership_type:"get_member_campaign",is_active:1,gateway_url:"/api/membership_campaigns/get_member_campaign",additional_parameter:{campaign_type_id:5},shop_code: args.shop_code},
      {membership_type:"search_paypar_account_no",is_active:1,gateway_url:"/api/generic_customer/get_membership_customer_data",shop_code: args.shop_code},
      {membership_type:"void",is_active:1,gateway_url:"/api/payments/payment_void",shop_code: args.shop_code},
      {membership_type:"search_paypar_security_code",is_active:1,gateway_url:"/api/generic_customer/get_membership_customer_security_code",shop_code: args.shop_code},
      {membership_type:"create_payment",is_active:1, gateway_url:"/api/create_payment",shop_code: args.shop_code}])
    Rails.logger.info " Finished member_actions Set Up Data"

    payment_methods = PaymentMethodSetting.create({payment_method:"PAYMAL" ,shop_code: args.shop_code})
    Rails.logger.info " Finished payment_methods Set Up Data"
    Rails.logger.info " Finished System Default Set Up Data"
  end
end
