Last active 1716781549

Malin's Avatar Malin revised this gist 1716781549. Go to revision

1 file changed, 155 insertions

Order.php(file created)

@@ -0,0 +1,155 @@
1 + // Original Order.php B2C API
2 +
3 + <?php
4 +
5 + namespace Encodigoweb\Woocommerce\Handlers;
6 +
7 + use Encodigoweb\Woocommerce\Repository\OrderRepository;
8 + use Exception;
9 + use WC_Customer;
10 +
11 + final class Order extends Handler
12 + {
13 + public function __construct()
14 + {
15 + parent::__construct();
16 +
17 + $this->log_context = ['source' => 'Order'];
18 +
19 + $this->apis = ['order'];
20 +
21 + $this->order_repository = new OrderRepository();
22 + }
23 +
24 + public function process()
25 + {
26 + $this->logger->info('Inicio sincronización pedidos', $this->log_context);
27 +
28 + $posts = $this->order_repository->getOrders();
29 +
30 + if (!empty($posts))
31 + {
32 + foreach ($posts as $post)
33 + {
34 + $response = $this->sendOrder($post);
35 + }
36 + }
37 + else
38 + {
39 + $this->logger->info('No hay pedidos para procesar', $this->log_context);
40 +
41 + echo 'No hay pedidos para procesar';
42 + }
43 +
44 + $this->logger->info('Fin sincronización pedidos', $this->log_context);
45 +
46 + echo 'Proceso Terminado';
47 + }
48 +
49 + public function sendOrder($post)
50 + {
51 + $xml = $this->generateXml($post);
52 +
53 + $api_config = $this->config['apis'][$this->apis[0]];
54 +
55 + $api = new Api($api_config);
56 +
57 + $result = $api->callApi($xml);
58 +
59 + if ($result['response'] === true){
60 + $this->order_repository->insertOrder($post->ID);
61 +
62 + $this->logger->info('Pedido '.$post->ID.' enviado correctamente', $this->log_context);
63 + }else{
64 + $this->logger->error('Pedido '.$post->ID.' '.$result['msg'], $this->log_context);
65 + }
66 + }
67 +
68 + public function generateXml($post)
69 + {
70 + $order = wc_get_order($post->ID);
71 +
72 +
73 + $items = $order->get_items();
74 +
75 + $order_lines = '';
76 +
77 + foreach ($items as $item)
78 + {
79 + $product = wc_get_product($item->get_product_id());
80 +
81 + $total_line_tax_excl = round($item->get_subtotal(), 2);
82 +
83 + $total_line_tax_incl = round($item->get_subtotal() + $item->get_subtotal_tax(), 2);
84 +
85 + $order_lines .= '<linea>
86 + <EcomPedidoId>'.$item->get_order_id().'</EcomPedidoId>
87 + <EcomNumLineaId>'.$item->get_id().'</EcomNumLineaId>
88 + <ProductoReferencia>'.$product->get_sku().'</ProductoReferencia>
89 + <Cantidad>'.$item->get_quantity().'</Cantidad>
90 + <Importe>'.$total_line_tax_excl.'</Importe>
91 + <ImporteConIva>'.$total_line_tax_incl.'</ImporteConIva>
92 + </linea>';
93 + }
94 +
95 + //$customer = new WC_Customer($order->get_customer_id());
96 +
97 + $dni = get_post_meta($post->ID, '_billing_vat', true);
98 +
99 + $country_billing = $order->get_billing_country();
100 + $state_billing = $order->get_billing_state();
101 + $billing_state_name = WC()->countries->get_states( $country_billing )[$state_billing];
102 +
103 + $country_shipping = $order->get_shipping_country();
104 + $state_shipping = $order->get_shipping_state();
105 + $shipping_state_name = WC()->countries->get_states( $country_shipping )[$state_shipping];
106 +
107 + $total_tax_excl = round($order->get_subtotal(), 2);
108 +
109 + $total_tax_incl = round($order->get_total(), 2);
110 +
111 + $xml = '<?xml version="1.0"?>
112 + <pedido>
113 + <CabeceraPedido>
114 + <EcomPedidoId>'.$order->get_id().'</EcomPedidoId>
115 + <CustomerId>'.$order->get_customer_id().'</CustomerId>
116 + <CustomerName>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</CustomerName>
117 + <CustomerCIF>'.$dni.'</CustomerCIF>
118 + <CustomerMail>'.$order->get_billing_email().'</CustomerMail>
119 + <CustomerTelefono>'.$order->get_billing_phone().'</CustomerTelefono>
120 + <CustomerPais>'.$country_billing.'</CustomerPais>
121 + <DirFactId>'.$order->get_id().'</DirFactId>
122 + <DirFactNombre>'.$order->get_billing_first_name().' '.$order->get_billing_last_name().'</DirFactNombre>
123 + <DirFactDireccion>'.$order->get_billing_address_1().' '.$order->get_billing_address_2().'</DirFactDireccion>
124 + <DirFactCodigoPostal>'.$order->get_billing_postcode().'</DirFactCodigoPostal>
125 + <DirFactPoblacion>'.$order->get_shipping_city().'</DirFactPoblacion>
126 + <DirFactProvincia>'.$billing_state_name.'</DirFactProvincia>
127 + <DirFactProvinciaCodigo>'.$state_billing.'</DirFactProvinciaCodigo>
128 + <DirFactPais>'.$country_billing.'</DirFactPais>
129 + <DirFactTelefono>'.$order->get_billing_phone().'</DirFactTelefono>
130 + <DirFactMail>'.$order->get_billing_email().'</DirFactMail>
131 + <DirFactCIF>'.$dni.'</DirFactCIF>
132 + <DirEnvId>'.$order->get_id().'</DirEnvId>
133 + <DirEnvNombre>'.$order->get_shipping_first_name().' '.$order->get_shipping_last_name().'</DirEnvNombre>
134 + <DirEnvDireccion>'.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().'</DirEnvDireccion>
135 + <DirEnvCodigoPostal>'.$order->get_shipping_postcode().'</DirEnvCodigoPostal>
136 + <DirEnvPoblacion>'.$order->get_shipping_city().'</DirEnvPoblacion>
137 + <DirEnvProvincia>'.$shipping_state_name.'</DirEnvProvincia>
138 + <DirEnvProvinciaCodigo>'.$state_shipping.'</DirEnvProvinciaCodigo>
139 + <DirEnvPais>'.$country_shipping.'</DirEnvPais>
140 + <DirEnvTelefono>'.$order->get_billing_phone().'</DirEnvTelefono>
141 + <DirEnvMail>'.$order->get_billing_email().'</DirEnvMail>
142 + <DirEnvCIF>'.$dni.'</DirEnvCIF>
143 + <EcomTotalGastosEnvio>'.$order->get_shipping_total().'</EcomTotalGastosEnvio>
144 + <Importe>'.$total_tax_excl.'</Importe>
145 + <ImporteConIva>'.$total_tax_incl.'</ImporteConIva>
146 + <MetodoPago>'.$order->get_payment_method().'</MetodoPago>
147 + </CabeceraPedido>
148 + <LineasPedido>
149 + '.$order_lines.'
150 + </LineasPedido>
151 + </pedido>';
152 +
153 + return $xml;
154 + }
155 + }
Newer Older