HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/PuntosColombiaDimController.php
<?php

namespace App\Http\Controllers;

use DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use App\IntegrationProvider;

class PuntosColombiaDimController extends Controller
{

    private $orders;
    private $credentialsIntegration;
    private $fileName;
    private $loadedDate;
    private $fileContents;


    public function __construct()
    {
        // Consultar data sftp de PCO.
        $this->credentialsIntegration = IntegrationProvider::where([['ecommerce_type_id', 1], ['active', true]])->first();
    }

    public function startBatchProcess($date = null, $consecutive = null)
    {
        if ($this->credentialsIntegration) {
            $this->getOrders($date);
            $this->createFiles($date, $consecutive);
            $this->sendFiles($date, $consecutive);
        }
    }

    public function getOrders($date = null)
    {
        if ($date) {
            $sql = "SELECT
                    p.ID AS 'OrderID',
                    p.post_date AS 'PurchaseDate',
                    MAX( CASE WHEN pm.meta_key = '_billing_document' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'Document',
                    MAX( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'OrderTotal',
                    MAX( CASE WHEN pm.meta_key = '_order_shipping' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'OrderShipping',
                    MAX( CASE WHEN pm.meta_key = '_paid_date' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'PaidDate',
                    ( SELECT GROUP_CONCAT(CONCAT(m.meta_value, ';', i.order_item_id, ';', s.meta_value) separator '</br>' )
                FROM wp_woocommerce_order_items i
                JOIN wp_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND m.meta_key = '_qty'
                JOIN wp_woocommerce_order_itemmeta s ON i.order_item_id = s.order_item_id AND s.meta_key = '_line_subtotal'
                WHERE i.order_id = p.ID AND i.order_item_type = 'line_item') AS 'ItemsOrdered'
                FROM wp_posts AS p
                JOIN wp_postmeta AS pm ON p.ID = pm.post_id
                JOIN wp_woocommerce_order_items AS oi ON p.ID = oi.order_id
                WHERE
                    post_type = 'shop_order' AND
                    DATE(post_date) = '" . $date->toDateString() . "' AND
                    post_status = 'wc-completed'
                    GROUP BY p.ID;";
        } else {
            $sql = "SELECT
                    p.ID AS 'OrderID',
                    p.post_date AS 'PurchaseDate',
                    MAX( CASE WHEN pm.meta_key = '_billing_document' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'Document',
                    MAX( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'OrderTotal',
                    MAX( CASE WHEN pm.meta_key = '_order_shipping' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'OrderShipping',
                    MAX( CASE WHEN pm.meta_key = '_paid_date' AND p.ID = pm.post_id THEN pm.meta_value END ) AS 'PaidDate',
                    ( SELECT GROUP_CONCAT(CONCAT(m.meta_value, ';', i.order_item_id, ';', s.meta_value) separator '</br>' )
                FROM wp_woocommerce_order_items i
                JOIN wp_woocommerce_order_itemmeta m ON i.order_item_id = m.order_item_id AND m.meta_key = '_qty'
                JOIN wp_woocommerce_order_itemmeta s ON i.order_item_id = s.order_item_id AND s.meta_key = '_line_subtotal'
                WHERE i.order_id = p.ID AND i.order_item_type = 'line_item') AS 'ItemsOrdered'
                FROM wp_posts AS p
                JOIN wp_postmeta AS pm ON p.ID = pm.post_id
                JOIN wp_woocommerce_order_items AS oi ON p.ID = oi.order_id
                WHERE
                    post_type = 'shop_order' AND
                    post_date >= '" . $this->credentialsIntegration->last_load_date_web_shop . "' AND
                    post_status = 'wc-completed'
                    GROUP BY p.ID;";
        }
        $this->orders = DB::connection('wp-woocommerce')->select($sql);
    }

    public function createFiles($date = null, $consecutive = null)
    {
        // Nombre del archivo
        $this->buildFileName($date, $consecutive);

        // Estructura archivo
        $this->buildFileContents($date, $consecutive);
    }

    public function buildFileName($date = null, $consecutive = null)
    {
        $nit = $this->credentialsIntegration->nit;
        $env = $this->credentialsIntegration->env;
        $interface = $this->credentialsIntegration->interface; // IF016
        $consecutive = $this->completeLeadingZeros($consecutive ?? $this->credentialsIntegration->consecutive);
        if ($date) {
            $loadDate = $date->format("YmdHis");
        } else {
            $loadDate = $this->getLoadedDateFormat('plain');
        }
        // Example: 901234556_CLM_TST_IF016_0000000001_20191114102501.dsv
        $this->fileName = "{$nit}_CLM_{$env}_{$interface}_{$consecutive}_{$loadDate}.dsv";
    }

    public function buildFileContents($date = null, $consecutive = null)
    {
        $interface = $this->credentialsIntegration->interface;
        $nit = $this->credentialsIntegration->nit;
        $consecutive = $this->completeLeadingZeros($consecutive ?? $this->credentialsIntegration->consecutive);
        if ($date) {
            $loadDate = $date->toDateTimeLocalString();
        } else {
            $loadDate = $this->getLoadedDateFormat('dt');
        }
        $totalOrders = count($this->orders);
        $this->fileContents = "";

        // Encabezado
        $this->fileContents = "H;{$interface};{$nit};{$consecutive};{$loadDate}" . "\n";

        foreach ($this->orders as $o) {
            $transactionDate = Carbon::createFromDate($o->PurchaseDate)->toDateTimeLocalString();
            //D;NumeroTerminal(O);NumeroTransacción(R);IDVendedor(O);CodeTienda(R);FechaTransaccion(R);NumeroDocumento(R);TipoIdentificacion(R);PuntosAcum(O);NUT(O);Comentarios(O)
            $rowFile = "D;;{$o->OrderID};;DIM02;{$transactionDate};{$o->Document};2;;;" . "\n";


            // Separar Items de la Orden
            $ItemsOrdered = $o->ItemsOrdered;
            $ListItemsOrdered = explode("</br>", $ItemsOrdered);


            foreach ($ListItemsOrdered as $op) {
                //DP;CodigoProducto;Cantidad;Total
                $rowFile = $rowFile . "DP;{$op}" . "\n";
            }

            //DM;CodigoMedioPago;Cantidad
            $differenceWithout = $o->OrderTotal - $o->OrderShipping;
            $rowFile = $rowFile . "DM;UN;{$differenceWithout}" . "\n";
            $this->fileContents = $this->fileContents . $rowFile;
        }
        // Footer
        $this->fileContents = $this->fileContents . "F;{$totalOrders}";
    }

    public function completeLeadingZeros($consecutive)
    {
        $consecutive = $consecutive + 1; // Nuevo consecutivo
        do {
            $consecutive = "0" . $consecutive;
        } while (strlen($consecutive) < 10);
        return $consecutive;
    }

    public function getLoadedDateFormat($type)
    {
        if (!$this->loadedDate) {
            $this->loadedDate = Carbon::now();
        }
        if ($type == 'plain') {
            return $this->loadedDate->format("YmdHis");
        }
        if ($type == 'dt') {
            return $this->loadedDate->toDateTimeLocalString();
        }
    }

    public function sendFiles($date = null, $consecutive = null)
    {
        Storage::put('puntos_colombia_files/' . $this->fileName, $this->fileContents);
        $this->credentialsIntegration->consecutive = ($consecutive ?? $this->credentialsIntegration->consecutive) + 1;
        $this->credentialsIntegration->last_load_date_web_shop = $date ?? Carbon::now();
        $this->credentialsIntegration->update();
    }
}