
#############################
DADOS PARA INSERﾃ�ﾃグ DE PEDIDO
** Sﾃ｢o Uteis para ver os insert das outras tabels
#################################
Array
(
    [invoice_prefix] => INV-2017-00
    [store_id] => 0
    [store_name] => Saﾃｺde e Sabor
    [store_url] => https://jp.saudeesabor.co.jp/
    [customer_id] => 6347
    [customer_group_id] => 1
    [firstname] => Reginaldo
    [lastname] => Gomes
    [email] => regygom@gmail.com
    [telephone] => 19999999999
    [fax] => 
    [custom_field] => 
    [payment_firstname] => Reginaldo
    [payment_lastname] => Gomes 2
    [payment_company] => 
    [payment_company_id] => 
    [payment_address_1] => Rua teste
    [payment_address_2] => 
    [payment_city] => Americana
    [payment_postcode] => 1346812
    [payment_country] => Brazil
    [payment_country_id] => 30
    [payment_tax_id] => 
    [payment_zone] => Sﾃ｣o Paulo
    [payment_zone_id] => 464
    [payment_address_format] => 
    [payment_method] => 
    [payment_code] => 
    [payment_custom_field] => 
    [shipping_firstname] => Reginaldo
    [shipping_lastname] => Gomes 2
    [shipping_company] => 
    [shipping_address_1] => Rua teste
    [shipping_address_2] => 
    [shipping_city] => Americana
    [shipping_postcode] => 1346812
    [shipping_country] => Brazil
    [shipping_country_id] => 30
    [shipping_zone] => Sﾃ｣o Paulo
    [shipping_zone_id] => 464
    [shipping_address_format] => 
    [shipping_method] => 
    [shipping_code] => 
    [shipping_custom_field] => 
    [comment] => 
    [total] => 1980
    [affiliate_id] => 0
    [commission] => 0
    [marketing_id] => 0
    [tracking] => 
    [language_id] => 2
    [currency_id] => 4
    [currency_code] => JPY
    [currency_value] => 1.00000000
    [ip] => 177.185.220.131
    [forwarded_ip] => 
    [user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
    [accept_language] => pt,en-US;q=0.9,en;q=0.8,ca;q=0.7,fr;q=0.6,gl;q=0.5,mt;q=0.4,es;q=0.3,la;q=0.2,ja;q=0.1
    [totals] => Array
        (
            [0] => Array
                (
                    [code] => sub_total
                    [title] => Produtos
                    [value] => 1980
                    [sort_order] => 
                )

            [1] => Array
                (
                    [code] => sub_total_prod_frete
                    [title] => Total
                    [value] => 1980
                    [sort_order] => 2
                )

            [2] => Array
                (
                    [code] => total
                    [title] => Total a ser Pago
                    [value] => 1980
                    [sort_order] => 9
                )

        )

    [products] => Array
        (
            [0] => Array
                (
                    [product_id] => 62
                    [name] => Churrasco na Brasa &quot;G&quot;
                    [model] => product_id=62
                    [option] => Array
                        (
                        )

                    [download] => Array
                        (
                        )

                    [quantity] => 1
                    [subtract] => 1
                    [price] => 1000
                    [total] => 1000
                    [tax] => 0
                    [reward] => 10
                )

            [1] => Array
                (
                    [product_id] => 404
                    [name] => Polenta Cremosa com Ragu de Carne &quot;G&quot;
                    [model] => product_id=821
                    [option] => Array
                        (
                        )

                    [download] => Array
                        (
                        )

                    [quantity] => 1
                    [subtract] => 1
                    [price] => 980
                    [total] => 980
                    [tax] => 0
                    [reward] => 10
                )

        )

    [vouchers] => Array
        (
        )

    [payment_address_id] => 11031
    [payment_zone_code] => SP
    [payment_iso_code_2] => BR
    [payment_iso_code_3] => BRA
    [payment_country_name] => Brazil
    [payment_zone_name] => Sﾃ｣o Paulo
    [shipping_address_id] => 11031
    [shipping_zone_code] => SP
    [shipping_iso_code_2] => BR
    [shipping_iso_code_3] => BRA
    [shipping_country_name] => Brazil
    [shipping_zone_name] => Sﾃ｣o Paulo
)

###########################
QUERY INSERﾃ�ﾃグ PEDIDO
####################

INSERT INTO `oc_order` 
SET invoice_prefix = 'INV-2017-00', 
store_id = '0', 
store_name = 'Saﾃｺde e Sabor', 
store_url = 'https://jp.saudeesabor.co.jp/', 
customer_id = '6347', 
customer_group_id = '1', 
firstname = 'Reginaldo', 
lastname = 'Gomes', 
email = 'regygom@gmail.com', 
telephone = '19999999999', 
custom_field = '\"\"', 
payment_firstname = 'Reginaldo', 
payment_lastname = 'Gomes 2', 
payment_company = '', 
payment_address_1 = 'Rua teste', 
payment_address_2 = '', 
payment_city = 'Americana', 
payment_postcode = '1346812', 
payment_country = 'Brazil', 
payment_country_id = '30', 
payment_zone = 'Sﾃ｣o Paulo', 
payment_zone_id = '464', 
payment_address_format = '', 
payment_custom_field = '', 
payment_method = '', 
payment_code = '', 
shipping_firstname = 'Reginaldo', 
shipping_lastname = 'Gomes 2', 
shipping_company = '', 
shipping_address_1 = 'Rua teste', 
shipping_address_2 = '', 
shipping_city = 'Americana', 
shipping_postcode = '1346812', 
shipping_country = 'Brazil', 
shipping_country_id = '30', 
shipping_zone = 'Sﾃ｣o Paulo', 
shipping_zone_id = '464', 
shipping_address_format = '', 
shipping_custom_field = '', 
shipping_method = '', 
shipping_code = '', 
comment = '', 
total = '1980', 
affiliate_id = '0', 
commission = '0',
marketing_id = '0', 
tracking = '', 
language_id = '2', 
currency_id = '4', 
currency_code = 'JPY', 
currency_value = '1', 
ip = '177.185.220.131', 
forwarded_ip = '', 
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36', 
accept_language = 'pt,en-US;q=0.9,en;q=0.8,ca;q=0.7,fr;q=0.6,gl;q=0.5,mt;q=0.4,es;q=0.3,la;q=0.2,ja;q=0.1', 
date_added = NOW(), 
date_modified = NOW()


---------------------------------------------------------------------------------------------------------------

#############################
DADOS PARA INSERﾃ�ﾃグ DOS PRODUTOS
#################################

Array
(
    [product_id] => 367
    [name] => FRANGO NA BRASA &quot;FAMﾃ広IA&quot;
    [model] => product_id=107
    [option] => Array
        (
        )

    [download] => Array
        (
        )

    [quantity] => 1
    [subtract] => 1
    [price] => 1590
    [total] => 1590
    [tax] => 0
    [reward] => 16
)

###########################
QUERY INSERﾃ�ﾃグ PRODUTOS 
####################

INSERT INTO oc_order_product 
SET order_id = '55213', 
product_id = '367', 
name = 'FRANGO NA BRASA &quot;FAMﾃ広IA&quot;', 
model = 'product_id=107', 
quantity = '1', 
price = '1590', 
total = '1590', 
tax = '0', 
reward = '16'

-------------------------------------------------------------------------------------------------------------

##############################
TOTAIS + QUERY DE CADA TOTAL DO PEDIDO
+ INSERﾃ�ﾃ０ DO CUPON (VAI JUNTO NO TOTAL)
###################

Totais
Array
(
    [code] => sub_total
    [title] => Produtos
    [value] => 900
    [sort_order] => 
)
INSERT INTO oc_order_total 
SET order_id = '55215', 
code = 'sub_total', 
title = 'Produtos', 
`value` = '900', 
sort_order = '0'Array

(
    [code] => sub_total_prod_frete
    [title] => Total
    [value] => 900
    [sort_order] => 2
)

INSERT INTO oc_order_total 
SET order_id = '55215', 
code = 'sub_total_prod_frete', 
title = 'Total', 
`value` = '900', 
sort_order = '2'Array


(
    [code] => total
    [title] => Total a ser Pago
    [value] => 900
    [sort_order] => 9
)
INSERT INTO oc_order_total 
SET order_id = '55215', 
code = 'total', 
title = 'Total a ser Pago', 
`value` = '900', 
sort_order = '9'


(
    [code] => coupon
    [title] => Cupom (CrazyJapan)
    [value] => -900
    [sort_order] => 5
)
INSERT INTO oc_order_total 
SET order_id = '55215', 
coupon = 'total', 
title = 'Cupom (CrazyJapan)', 
`value` = '-900', 
sort_order = '5'

(
    [code] => SHIPPROG_5_2.SHIPPROG_5_2
    [title] => 2-Retirada na loja
    [value] => 0
    [sort_order] => 1
)
INSERT INTO oc_order_total 
SET order_id = '55215', 
coupon = 'SHIPPROG_5_2.SHIPPROG_5_2', 
title = '2-Retirada na loja', 
`value` = '0', 
sort_order = '1'

---------------------------------------------------------------------

#####################################
VOUCHER DATA + QUERY
** VOUCHER nao eh usado atualmente, entao o que eu inseri ﾃｩ um exemplo
######################################


"INSERT INTO oc_order_voucher 
SET order_id = '55215', 
description = 'Teste Voucher', 
code = 'VOUC1', 
from_name = 'DE NOME', 
from_email = 'NOME FROM VOUCHER', 
to_name = 'PARA VOUVCHER', 
to_email = 'PARA EMAIL', 
voucher_theme_id = 'TEMA DO VOUCHER', 
message = 'MESSAGE VOUCHER', 
amount = '50.00'"


#######################################3
INSERﾃ�ﾃ０ CLIENTE CLIENTE, DADOS + QUERY
############################

Array
(
    [customer_group_id] => 1
    [firstname] => Joﾃ｣o
    [lastname] => Silva
    [email] => regygom@hotmail.com
    [custom_field] => Array
        (
            [account] => Array
                (
                    [1] => 10/10/2000
                    [2] => 1
                )

        )

    [telephone] => 19999999999
    [password] => abc123
    [confirm] => abc123
    [postcode] => 123-4568
    [country_id] => 107
    [zone_id] => 1672
    [city] => teste
    [address_1] => teste
    [address_2] => 
    [company] => ref teste
    [newsletter] => 0
    [agree] => 1
)

INSERT INTO oc_customer 
SET customer_group_id = '1', 
store_id = '0', 
language_id = '2', 
firstname = 'Joﾃ｣o', 
lastname = 'Silva', 
email = 'regygom@hotmail.com', 
telephone = '19999999999', 
custom_field = '{\"1\":\"10\\/10\\/2000\",\"2\":\"1\"}', 
salt = 'rrNlhFWAJ', 
password = '01fc9f49a8802ee34dcfdf5ade67ce9a1a42ba16', 
newsletter = '0', 
ip = '177.185.220.131', 
status = '1', 
date_added = NOW()



-- INSERÇÃO DO ENDEREÇO

Dados:
$customer_id = 6347;
$data = [
    'firstname' => 'Reginaldo',
    'lastname' => 'Gomes',
    'company' => '',
    'address_1' => 'Rua teste',
    'address_2' => '',
    'postcode' => '134-6812',
    'city' => 'Americana',
    'zone_id' => '464', // Estado
    'country_id' => '30' // Pais
    'custom_field' => '[]'
];
QUERY
"INSERT INTO oc_address 
SET customer_id = '" . (int)$customer_id . "', 
firstname = '" . $this->db->escape($data['firstname']) . "', 
lastname = '" . $this->db->escape($data['lastname']) . "', 
company = '" . $this->db->escape($data['company']) . "', 
address_1 = '" . $this->db->escape($data['address_1']) . "', 
address_2 = '" . $this->db->escape($data['address_2']) . "',
postcode = '" . $this->db->escape($data['postcode']) . "', 
city = '" . $this->db->escape($data['city']) . "', 
zone_id = '" . (int)$data['zone_id'] . "', 
country_id = '" . (int)$data['country_id'] . "', 
custom_field = '" . $this->db->escape(isset($data['custom_field']['address']) ? json_encode($data['custom_field']['address']) : '') . "'"

--- A busca é um Select Simples, passando o ID do endereco e o ID do cliente

"SELECT DISTINCT * FROM oc_address WHERE address_id = '" . (int)$address_id . "' AND customer_id = '" . (int)$this->customer->getId() . "'"

--  Caso seja para listar os endereços basta omitir o id do endereço na busca


#########################
- Inserção do endereço Pagamento
- Inserção do endereço Entrega
################
---- No momento da geração do pedido ele insere os textos em cada campo da tabela "oc_order", enviada anteriormente (Entrega começa com Shipping e Pagamento com payment)

---- BUSCA DE PROGRAMAÇÂO DE ENTREGA
---- Busca-se primeiro a programação permitida para aquele CEP:
SELECT * FROM shipping_programation_cep WHERE Replace(cep,'-','') = '123-45'
---  Depois busca as datas permitidas para aquela programação + a descrição para aquele idioma:
SELECT sp.*,
(SELECT name FROM shipping_programation_name spn WHERE spn.shipping_programation_id = sp.id AND spn.language_id = 'ID_LINGUAGEM_ATUAL' ) as name ";
FROM shipping_programation sp
WHERE sp.status = 1 // -- status ativo
AND programation_data IS NOT NULL
AND programation_data != ''
AND id in (IDS_PROGRAMACOES_ENCONTRADAS_ACIMA)


---- DESCONTO PACOTES
-- Apos a adição do pacote no carrinho temos o ID do pacote
-- Para saber os descontos pode ser feito um select simples para verificar as regras
-- As regras são auto explicativas

Select
SELECT * FROM oc_package WHERE id = {ID}
-- discount => valor do desconto
-- group_discount_id => A qual grupo de desconto o pacote faz parte, ISTO é, o cliente que fizer parte do grupo definido se beneficiará com o desconto
-- new_customer_free_shipping => FLAG Novo cliente ganha frete gratis
-- old_customer_free_shipping => FLAG Cliente já cadastrado ganha frete gratis
-- new_customer_double_discount => FLAG Novo clinte ganha desconto em dobro
-- old_customer_double_discount => FLAG Cliente já cadastrado ganha desconto em dobro
-- show_in_departament => Mostrar em Departamentos
-- departament_id => Departamento a qual o Pacote faz parte