mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
183 lines
8.8 KiB
PHP
183 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Orders;
|
|
|
|
use App\Classes\ValueObjects\Constants\BusinessType;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
|
|
use App\Classes\ValueObjects\Constants\RemarkTypes;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\CompanyModule;
|
|
use App\Models\Order;
|
|
use App\Models\User;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class MoveOrderToCustomerController
|
|
{
|
|
public function orderInfo(Request $request): JsonResponse
|
|
{
|
|
$ref = $request->query('reference');
|
|
if (!$ref) {
|
|
return response()->json(['error' => 'reference required'], 422);
|
|
}
|
|
$order = Order::where('reference', $ref)->with('companyModule')->first();
|
|
if (!$order) {
|
|
return response()->json(['error' => 'Order not found'], 404);
|
|
}
|
|
$cm = $order->companyModule;
|
|
return response()->json([
|
|
'order_id' => $order->id,
|
|
'reference' => $order->reference,
|
|
'from_company_module_id' => $order->company_module_id,
|
|
'from_customer_name' => $cm->name ?? '',
|
|
'from_customer_marking' => $cm->getMarking(),
|
|
]);
|
|
}
|
|
|
|
public function customers(Request $request): JsonResponse
|
|
{
|
|
$list = CompanyModule::where('type', BusinessType::IMPORTER)
|
|
->whereHas('connections', fn ($q) => $q->whereNotNull('invitee_reference')->where('invitee_reference', '!=', ''))
|
|
->get()
|
|
->map(fn ($cm) => ['id' => $cm->id, 'name' => $cm->name, 'marking' => $cm->getMarking()])
|
|
->filter(fn ($r) => (string) $r['marking'] !== '')
|
|
->values();
|
|
|
|
return response()->json($list);
|
|
}
|
|
|
|
public function move(Request $request): JsonResponse
|
|
{
|
|
$ref = $request->input('order_reference');
|
|
$targetId = (int) $request->input('target_company_module_id');
|
|
if (!$ref || !$targetId) {
|
|
return response()->json(['success' => false, 'error' => 'order_reference and target_company_module_id required', 'log' => []]);
|
|
}
|
|
|
|
$order = Order::with('companyModule')->where('reference', $ref)->first();
|
|
if (!$order) {
|
|
return response()->json(['success' => false, 'error' => 'Order not found', 'log' => []]);
|
|
}
|
|
|
|
$from = $order->company_module_id;
|
|
if ($from == $targetId) {
|
|
return response()->json(['success' => false, 'error' => 'Order already belongs to target customer', 'log' => []]);
|
|
}
|
|
|
|
$target = CompanyModule::find($targetId);
|
|
if (!$target) {
|
|
return response()->json(['success' => false, 'error' => 'Target customer not found', 'log' => []]);
|
|
}
|
|
|
|
$log = [];
|
|
$log[] = 'Changing order: company_module_id ' . $from . ' → ' . $targetId;
|
|
$log[] = 'Changing order_roles: company_module_id ' . $from . ' → ' . $targetId;
|
|
$log[] = 'Changing packing list: no update (remain linked to order)';
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$now = now();
|
|
DB::table('orders')->where('id', $order->id)->update(['company_module_id' => $targetId, 'updated_at' => $now]);
|
|
DB::table('order_roles')
|
|
->where('order_id', $order->id)
|
|
->where('role_id', OrderRoleTypes::IMPORTER)
|
|
->update(['company_module_id' => $targetId, 'updated_at' => $now]);
|
|
|
|
$marking = $target->getMarking() ?: (string) $targetId;
|
|
DB::table('remarks')->insert([
|
|
'owner_type' => Order::class,
|
|
'owner_id' => $order->id,
|
|
'commenter_id' => auth()->id() ?? 1,
|
|
'content' => 'Order moved to customer (marking: ' . $marking . ') on ' . $now->toDateTimeString(),
|
|
'type' => RemarkTypes::INTERNAL,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
$fromMarking = $order->companyModule ? ($order->companyModule->getMarking() ?: (string) $from) : (string) $from;
|
|
DB::table(config('activitylog.table_name', 'activity_log'))->insert([
|
|
'log_name' => 'admn_move_order',
|
|
'description' => 'Order ' . $order->reference . ' moved from customer (marking: ' . $fromMarking . ') to customer (marking: ' . $marking . ')',
|
|
'subject_type' => Order::class,
|
|
'subject_id' => $order->id,
|
|
'causer_type' => auth()->id() ? User::class : null,
|
|
'causer_id' => auth()->id(),
|
|
'properties' => json_encode([
|
|
'from_company_module_id' => $from,
|
|
'to_company_module_id' => $targetId,
|
|
'from_marking' => $fromMarking,
|
|
'to_marking' => $marking,
|
|
]),
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
|
|
$transactions = $order->transactions()->get();
|
|
$createShipInv = app(\App\Classes\Modules\Transactions\Processors\CreateShippingInvoiceTransactionDocProcessor::class);
|
|
$createSalesOrder = app(\App\Classes\Modules\Transactions\Processors\CreateSalesOrderDocProcessor::class);
|
|
$createShipEInv = app(\App\Classes\Modules\Transactions\Processors\CreateShippingEInvoiceDocProcessor::class);
|
|
$createStorageInv = app(\App\Classes\Modules\Transactions\Processors\CreateStorageInvoiceTransactionDocProcessor::class);
|
|
$createStorageEInv = app(\App\Classes\Modules\Transactions\Processors\CreateStorageEInvoiceDocProcessor::class);
|
|
|
|
foreach ($transactions as $tx) {
|
|
if ($tx->type == TransactionType::SHIPPING_INVOICE) {
|
|
try {
|
|
$tx->documents()->where('document_type', DocumentType::SHIPPING_INVOICE)->delete();
|
|
$createShipInv->execute($tx);
|
|
$log[] = 'Regenerating Shipping Invoice PDF: transaction #' . $tx->id;
|
|
} catch (\Throwable $e) {
|
|
$log[] = 'Regenerating Shipping Invoice PDF: transaction #' . $tx->id . ' — failed: ' . $e->getMessage();
|
|
}
|
|
try {
|
|
$tx->documents()->where('document_type', DocumentType::SALES_ORDER)->delete();
|
|
$createSalesOrder->execute($tx);
|
|
$log[] = 'Regenerating Sales Order PDF: transaction #' . $tx->id;
|
|
} catch (\Throwable $e) {
|
|
$log[] = 'Regenerating Sales Order PDF: transaction #' . $tx->id . ' — failed: ' . $e->getMessage();
|
|
}
|
|
$isEInv = $tx->owner->owner->companyModule->company->e_invoice ?? 0;
|
|
if ($isEInv == 1) {
|
|
try {
|
|
$tx->documents()->where('document_type', DocumentType::SHIPPING_EINVOICE)->delete();
|
|
$createShipEInv->execute($tx);
|
|
$log[] = 'Regenerating E-Invoice PDF: transaction #' . $tx->id;
|
|
} catch (\Throwable $e) {
|
|
$log[] = 'Regenerating E-Invoice PDF: transaction #' . $tx->id . ' — failed: ' . $e->getMessage();
|
|
}
|
|
}
|
|
} elseif ($tx->type == TransactionType::STORAGE_INVOICE) {
|
|
try {
|
|
$tx->documents()->where('document_type', DocumentType::STORAGE_INVOICE)->delete();
|
|
$createStorageInv->execute($tx);
|
|
$log[] = 'Regenerating Storage Invoice PDF: transaction #' . $tx->id;
|
|
} catch (\Throwable $e) {
|
|
$log[] = 'Regenerating Storage Invoice PDF: transaction #' . $tx->id . ' — failed: ' . $e->getMessage();
|
|
}
|
|
$isEInv = $tx->owner->owner->companyModule->company->e_invoice ?? 0;
|
|
if ($isEInv == 1) {
|
|
try {
|
|
$tx->documents()->where('document_type', DocumentType::STORAGE_EINVOICE)->delete();
|
|
$createStorageEInv->execute($tx);
|
|
$log[] = 'Regenerating Storage E-Invoice PDF: transaction #' . $tx->id;
|
|
} catch (\Throwable $e) {
|
|
$log[] = 'Regenerating Storage E-Invoice PDF: transaction #' . $tx->id . ' — failed: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$log[] = 'Done.';
|
|
DB::commit();
|
|
} catch (\Throwable $e) {
|
|
DB::rollBack();
|
|
$log[] = 'Error: ' . $e->getMessage();
|
|
return response()->json(['success' => false, 'error' => $e->getMessage(), 'log' => $log]);
|
|
}
|
|
|
|
return response()->json(['success' => true, 'log' => $log]);
|
|
}
|
|
}
|