#!/usr/bin/env ruby

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

# Produkty
class PsProduct < ApplicationRecord
  self.table_name = 'ps_product'
  establish_connection :prestashop_old
end
# Nazwy produktow
class PsProductLang < ApplicationRecord
  self.table_name = 'ps_product_lang'
  establish_connection :prestashop_old
end

pcls = PsProductLang.where('id_lang = 3')

pcls.each do |pcl|
  product = Product.where(id: pcl.id_product).first
  if product.blank?
    pc = PsProduct.where('id_product = ?', pcl.id_product).first
    unless pc.blank?
      prod = Product.new(description: pcl.description, description_short: ActionController::Base.helpers.strip_tags(pcl.description_short), default_category_id: pc.id_category_default,
                         price: pc.price, quantity: pc.quantity, active: pc.active, name: pcl.name, created_by: 1, updated_by: 1)
      prod.id = pc.id_product
      prod.save
    end
  end
end
