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/agile-selling-wpb/app/Console/Commands/SyncOrdersBO.php
<?php

namespace App\Console\Commands;

use App\Order;
use Illuminate\Console\Command;
use App\Http\Controllers\SAPB1ServiceLayerController;
use Carbon\Carbon;

class SyncOrdersBO extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'bo:sync_orders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sincroniza las ordenes de Agile Selling con SAP Business ONE del cliente';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $orders = $this->getOrdersToSync();

        if(count($orders) <= 0){
            return;
        }

        $c = new SAPB1ServiceLayerController();

        foreach ($orders as $order) {
            $form_params = [
                'CardCode' => $order->client->code_b1,
                'DocDueDate' => $order->creation_date,
                'U_CAC_PPNOTES' => $order->paymentType->name . '/$' . $order->total_price . '',
                'BPL_IDAssignedToInvoice' => $order->bo_business_place_id,
                'ShippingMethod' => $order->bo_shipping_type_id,
                'TransportationCode' => $order->bo_shipping_type_id,
                'DocumentLines' => []
            ];

            $documentLines = [];

            foreach ($order->orderProducts as $op) {
                array_push($documentLines, [
                    'ItemCode' => $op->bo_item_code,
                    'Quantity' => $op->quantity,
                    'ShipDate' => Carbon::now()->format('Y-m-d'),
                    'WarehouseCode' => $op->quantity,
                    'ShippingMethod' => $order->bo_shipping_type_id,
                    'PackageQuantity' => $order->number_packages,
                    'OwnerCode' => 6
                ]);
            }

            $response = $c->createOrder($form_params);

            if($response->getStatusCode() == "200"){
                $body = json_decode($response->getBody()->getContents());
                $order->order_reference = $body->DocNum;
            }

        }

        return 0;
    }

    private function getOrdersToSync(){
        $orders = Order::where('order_reference', null)
                    ->where('id', 1)
                    ->with('client')
                    ->with('paymentType')
                    ->with('orderProducts')
                    ->get();

        return $orders;
    }
}