File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/PuntosColombiaController.php
<?php
namespace App\Http\Controllers;
use App\IntegrationProvider;
use App\Order;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Builder;
use phpseclib\Crypt\RSA;
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()
{
if($this->credentialsIntegration){
$this->getOrdersToSend();
$this->createFiles();
$this->sendFiles();
}
}
public function getOrdersToSend()
{
// Obtener data a enviar.
$this->orders = Order::where([['creation_date','>',$this->credentialsIntegration->last_load_date],['gw_state','CONFIRMED']])
// ->whereHas('client', function (Builder $query) {
// $query->where('document', '<>', '')->whereNotNull('document');
// })
->with('client')
->with('orderProducts')
->get();
}
public function createFiles()
{
// Nombre del archivo
$this->buildFileName();
// Estructura archivo
$this->buildFileContents();
}
public function buildFileName()
{
$nit = $this->credentialsIntegration->nit;
$env = $this->credentialsIntegration->env;
$interface = $this->credentialsIntegration->interface; // IF016
$consecutive = $this->completeLeadingZeros($this->credentialsIntegration->consecutive);
$loadDate = $this->getLoadedDateFormat('plain');
// Example: 901234556_CLM_TST_IF016_0000000001_20191114102501.dsv
$this->fileName = "{$nit}_CLM_{$env}_{$interface}_{$consecutive}_{$loadDate}.dsv";
}
public function buildFileContents()
{
$interface = $this->credentialsIntegration->interface;
$nit = $this->credentialsIntegration->nit;
$consecutive = $this->completeLeadingZeros($this->credentialsIntegration->consecutive);
$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()
{
Storage::put('puntos_colombia_files/'.$this->fileName, $this->fileContents);
$this->credentialsIntegration->consecutive = $this->credentialsIntegration->consecutive + 1;
$this->credentialsIntegration->last_load_date = Carbon::now();
$this->credentialsIntegration->update();
}
}