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;
}
}