#!/usr/bin/env ruby

ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../config/environment"

# import categories data from old presta shop

class PsCategory < ApplicationRecord
  self.table_name =  'ps_category'
  establish_connection :prestashop_old
end

class PsCategoryLang < ApplicationRecord
  self.table_name =  'ps_category_lang'
  establish_connection :prestashop_old
end

pcls = PsCategoryLang.where('id_lang = 3')
for pcl in pcls
  category = Category.where(id: pcl.id_category).first
  if category.blank?
    pc = PsCategory.where('id_category = ?',pcl.id_category).first
    if !pc.blank?
      cat = Category.new(parent_id: pc.id_parent, level_depth: pc.level_depth, active: pc.active, name: pcl.name, created_by: 1, updated_by: 1)
      cat.id = pc.id_category
      cat.save
    end
  end
end
