File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/PuntosColombiaController.php
<?php
namespace App\Http\Controllers;
use App\IntegrationProvider;
use App\Order;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
class PuntosColombiaController 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->getOrdersToSend($date);
$this->createFiles($date, $consecutive);
$this->sendFiles($date, $consecutive);
}
}
public function getOrdersToSend($date = null)
{
// Obtener data a enviar.
if ($date) {
$this->orders = Order::whereDate('creation_date', '=', $date->toDateString())
->where('gw_state', 'CONFIRMED')
->with('client', 'orderProducts')
->get();
} else {
$this->orders = Order::where([['creation_date', '>', $this->credentialsIntegration->last_load_date], ['gw_state', 'CONFIRMED']])
->with('client', 'orderProducts')
->get();
}
}
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 = $this->orders->count();
$this->fileContents = "";
// Encabezado
$this->fileContents = "H;{$interface};{$nit};{$consecutive};{$loadDate}" . "\n";
foreach ($this->orders as $o) {
$transactionDate = Carbon::createFromDate($o->creation_date)->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->id};;DIM02;{$transactionDate};{$o->client->document};2;;;" . "\n";
foreach ($o->orderProducts as $op) {
//DP;CodigoProducto;Cantidad;Total
$rowFile = $rowFile . "DP;{$op->product_id};{$op->quantity};{$op->price}" . "\n";
}
//DM;CodigoMedioPago;Cantidad
$rowFile = $rowFile . "DM;UN;{$o->subtotal}" . "\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 = $date ?? Carbon::now();
$this->credentialsIntegration->update();
}
}