order
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
|||||||
|
|
||||||
use App\Mylog;
|
use App\Mylog;
|
||||||
use App\Order as AppOrder;
|
use App\Order as AppOrder;
|
||||||
|
use App\Services\ProductLogger;
|
||||||
use Codexshaper\WooCommerce\Facades\Order;
|
use Codexshaper\WooCommerce\Facades\Order;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -42,51 +43,68 @@ class OrderUpdateGm extends Command
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$all_orderswoo = new Collection();
|
$productLogger = new ProductLogger(null, 'checkorders', true);
|
||||||
$page = 1;
|
$page = 1;
|
||||||
$orderupdated = 0;
|
$orderupdated = 0;
|
||||||
do{
|
try {
|
||||||
$orderswoo = Order::get($options = ['per_page' => 100, 'page' => $page,'status'=> ["pending","processing","on-hold"]]);
|
$all_orderswoo = new Collection(); // Assicurati che all_orderswoo sia inizializzato se non l'hai già fatto.
|
||||||
$all_orderswoo = $all_orderswoo->merge($orderswoo);
|
$page = 1; // Inizializza la pagina
|
||||||
$page++;
|
|
||||||
} while($orderswoo->count() > 0);
|
do {
|
||||||
|
$options = ['per_page' => 100, 'page' => $page, 'status' => ["pending", "processing", "on-hold"]];
|
||||||
|
$this->info('Fetching orders with options: ' . json_encode($options));
|
||||||
|
$orderswoo = Order::get($options); // Usa 'get' invece di 'all'
|
||||||
|
|
||||||
|
// Controlla se il risultato è una collezione o un errore
|
||||||
|
if (empty($orderswoo) || $orderswoo->isEmpty()) {
|
||||||
|
$productLogger->addLog('Error', 'No orders returned.');
|
||||||
|
break; // Esci dal ciclo in caso di errore
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge le nuove ordinazioni nella collezione esistente
|
||||||
|
$all_orderswoo = $all_orderswoo->merge($orderswoo);
|
||||||
|
$page++;
|
||||||
|
} while ($orderswoo->count() > 0); // Continua finché ci sono ordini
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if (isset($productLogger)) {
|
||||||
|
$productLogger->addLog('Error', $e->getMessage());
|
||||||
|
$productLogger->setLogandSendEmail('Ordini');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($all_orderswoo as $orderwoo) {
|
||||||
foreach($all_orderswoo as $orderwoo ){
|
|
||||||
$this->info("Processing order #{$orderwoo->id}...");
|
$this->info("Processing order #{$orderwoo->id}...");
|
||||||
$ordergm = AppOrder::where('IdInternet', $orderwoo->id)->latest('DataOra')->first();
|
$ordergm = AppOrder::where('IdInternet', $orderwoo->id)->latest('DataOra')->first();
|
||||||
if($ordergm){
|
if ($ordergm) {
|
||||||
$this->info("Order #{$orderwoo->id} found in GM, checking status...");
|
$this->info("Order #{$orderwoo->id} found in GM, checking status...");
|
||||||
if($orderwoo->status == 'processing'){
|
if ($orderwoo->status == 'processing') {
|
||||||
$this->info("Order #{$orderwoo->id} is processing, checking if it needs to be completed...");
|
$this->info("Order #{$orderwoo->id} is processing, checking if it needs to be completed...");
|
||||||
if($ordergm->EnabledWoo == 1){
|
if ($ordergm->EnabledWoo == 1) {
|
||||||
$this->info("Order #{$orderwoo->id} needs to be completed, updating WooCommerce...");
|
$this->info("Order #{$orderwoo->id} needs to be completed, updating WooCommerce...");
|
||||||
$data = [
|
$data = [
|
||||||
'status' => 'completed',
|
'status' => 'completed',
|
||||||
];
|
];
|
||||||
$orderwooupdate = Order::update($orderwoo->id,$data);
|
$orderwooupdate = Order::update($orderwoo->id, $data);
|
||||||
|
|
||||||
// sum the number updated of order to the log
|
// sum the number updated of order to the log
|
||||||
$orderupdated++;
|
$orderupdated++;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
} elseif ($orderwoo->status == 'on-hold') {
|
||||||
elseif ($orderwoo->status == 'on-hold'){
|
|
||||||
$this->info("Order #{$orderwoo->id} is on-hold, checking if it needs to be processed...");
|
$this->info("Order #{$orderwoo->id} is on-hold, checking if it needs to be processed...");
|
||||||
if($ordergm->FlagSospeso == 0) {
|
if ($ordergm->FlagSospeso == 0) {
|
||||||
$this->info("Order #{$orderwoo->id} needs to be processed, updating WooCommerce...");
|
$this->info("Order #{$orderwoo->id} needs to be processed, updating WooCommerce...");
|
||||||
$data = [
|
$data = [
|
||||||
'status' => 'processing',
|
'status' => 'processing',
|
||||||
];
|
];
|
||||||
$orderwooupdate = Order::update($orderwoo->id,$data);
|
$orderwooupdate = Order::update($orderwoo->id, $data);
|
||||||
|
|
||||||
// sum the number updated of order to the log
|
// sum the number updated of order to the log
|
||||||
$orderupdated++;
|
$orderupdated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
$this->info("Order #{$orderwoo->id} not found in GM, skipping...");
|
$this->info("Order #{$orderwoo->id} not found in GM, skipping...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,16 +114,16 @@ class OrderUpdateGm extends Command
|
|||||||
$log = ' Ordini aggiornati' . "\n";
|
$log = ' Ordini aggiornati' . "\n";
|
||||||
|
|
||||||
// check if there are orders to log
|
// check if there are orders to log
|
||||||
if($orderupdated > 0) {
|
if ($orderupdated > 0) {
|
||||||
// Send the email to the admin with the details of the order log
|
// Send the email to the admin with the details of the order log
|
||||||
Mail::raw($log, function ($message, $orderupdated) {
|
Mail::raw($log, function ($message, $orderupdated) {
|
||||||
$message->to(Mylog::getEmail());
|
$message->to(Mylog::getEmail());
|
||||||
$message->subject(Mylog::getSubjectEmail("Ordini Aggiornati su GM:" . $orderupdated));
|
$message->subject(Mylog::getSubjectEmail("Ordini Aggiornati su GM:" . $orderupdated));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if ($productLogger) {
|
||||||
|
$productLogger->setLogandSendEmail('Aggiornamento Ordini');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user