1688 PO Automation Project

This commit is contained in:
Dillon Ngo
2026-01-04 21:41:42 +08:00
parent fc6863a4de
commit 91ae61185f
38 changed files with 1282 additions and 169 deletions
@@ -4,6 +4,7 @@ namespace App\Classes\Modules\Transactions\Processors;
use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\Modules\Anthropic\Services\CreatesClaudeResponse;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Processors\CreatePurchaseOrderTransactionProcessor;
@@ -17,6 +18,7 @@ use App\Models\KeyValuePair;
use App\Models\Booking;
use Illuminate\Support\Facades\Log;
//This solution has partial dependency on pdf table extractor on front end (output not ideal)
class ETLPurchaseOrderTransactionProcessor
{
/** @var CreatesChatGPTResponse */
@@ -31,19 +33,24 @@ class ETLPurchaseOrderTransactionProcessor
/** @var CreatePurchaseOrderTransactionProcessor */
private $createPurchaseOrderTransactionProcessor;
/** @var CreatesClaudeResponse */
private $createsClaudeResponse;
/**
* ETLPurchaseOrderTransactionProcessor constructor.
* @param FetchesBooking $fetchesBooking
* @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
* @param CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor
* @param CreatesChatGPTResponse $createsChatGPTResponse
* @param CreatesClaudeResponse $createsClaudeResponse
*/
public function __construct(FetchesBooking $fetchesBooking, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, CreatesChatGPTResponse $createsChatGPTResponse)
public function __construct(FetchesBooking $fetchesBooking, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, CreatesChatGPTResponse $createsChatGPTResponse, CreatesClaudeResponse $createsClaudeResponse)
{
$this->fetchesBooking = $fetchesBooking;
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
$this->createPurchaseOrderTransactionProcessor = $createPurchaseOrderTransactionProcessor;
$this->createsChatGPTResponse = $createsChatGPTResponse;
$this->createsClaudeResponse = $createsClaudeResponse;
}
/**
@@ -65,7 +72,7 @@ class ETLPurchaseOrderTransactionProcessor
// Translate descriptions if they contain Chinese characters
$products = collect($products)->map(function ($product) {
if (isset($product['description']) && preg_match("/\p{Han}+/u", $product['description'])) {
$kvp = KeyValuePair::where('key', KVPKey::CHATGPT_PROMPT_PREFIX . "TRANSLATE_1")->first();
$kvp = KeyValuePair::where('key', KVPKey::CLAUDE_PROMPT_PREFIX . "TRANSLATE_1")->first();
$userPrompt = '';
if($kvp){
$templateFromDb = $kvp->value;
@@ -76,7 +83,8 @@ class ETLPurchaseOrderTransactionProcessor
throw new ResourceNotFoundException('AI Setup incomplete!');
}
$result = $this->createsChatGPTResponse->execute($userPrompt);
// $result = $this->createsChatGPTResponse->execute($userPrompt);
$result = $this->createsClaudeResponse->execute($userPrompt);
$content = $result['choices'][0]['message']['content'] ?? null;
// 1. Try raw JSON
@@ -89,7 +97,7 @@ class ETLPurchaseOrderTransactionProcessor
$json = $matches[1];
}
else {
Log::warning('No JSON found in ChatGPT response', ['content' => $content]);
Log::warning('No JSON found in AI service response', ['content' => $content]);
return;
}
@@ -103,7 +111,7 @@ class ETLPurchaseOrderTransactionProcessor
})->toArray();
if (is_array($productsMetadata) && count($productsMetadata) > 0) {
$kvp = KeyValuePair::where('key', KVPKey::CHATGPT_PROMPT_PREFIX . "EXTRACT_1")->first();
$kvp = KeyValuePair::where('key', KVPKey::CLAUDE_PROMPT_PREFIX . "EXTRACT_1")->first();
$prompt = '';
if($kvp){
$templateFromDb = $kvp->value;
@@ -114,7 +122,8 @@ class ETLPurchaseOrderTransactionProcessor
throw new ResourceNotFoundException('AI Setup incomplete!');
}
$result = $this->createsChatGPTResponse->execute($prompt);
// $result = $this->createsChatGPTResponse->execute($prompt);
$result = $this->createsClaudeResponse->execute($prompt);
$content = $result['choices'][0]['message']['content'] ?? null;
$freight = 0;