From 15f695763d49619ada186e81f929f84e6e347040 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Thu, 29 Sep 2022 03:46:26 +0800 Subject: [PATCH 01/66] convert 1688 purchase orders to po --- routes/web.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/routes/web.php b/routes/web.php index 56e895c2..50c36cbe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,6 +16,7 @@ use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Route; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Maatwebsite\Excel\Excel; use Meneses\LaravelMpdf\Facades\LaravelMpdf; @@ -385,10 +386,12 @@ Route::get('/1688/fix', function(){ $bookings = []; foreach($purchaseOrderDocuments as $document){ foreach($document->files as $file) { - if(!$file->file->file_info) dd($file); - continue; $parser = new \Smalot\PdfParser\Parser(); - $pdf = $parser->parseFile(Storage::disk('documents')->path($file->file->file_info->original->file)); + try { + $pdf = $parser->parseFile(Storage::disk('documents')->path($file->file->file_info->original->file)); + } catch (Exception $e){ + dd($file); + } $text = $pdf->getText(); $tables = explode('Amount (yuan)', $text); From 819bcb7f367f8d53b5cb5b4d6a4770a0f59a968e Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Thu, 29 Sep 2022 03:48:59 +0800 Subject: [PATCH 02/66] convert 1688 purchase orders to po --- routes/web.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/web.php b/routes/web.php index 50c36cbe..5b7f93f5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -389,8 +389,8 @@ Route::get('/1688/fix', function(){ $parser = new \Smalot\PdfParser\Parser(); try { $pdf = $parser->parseFile(Storage::disk('documents')->path($file->file->file_info->original->file)); - } catch (Exception $e){ - dd($file); + } catch (Exception $e) { + continue; } $text = $pdf->getText(); From 0d2dc1ba0dfb3c46edfb80f961838b756be2d97e Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Thu, 29 Sep 2022 04:18:04 +0800 Subject: [PATCH 03/66] convert 1688 purchase orders to po --- routes/web.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/routes/web.php b/routes/web.php index 5b7f93f5..6aadda47 100644 --- a/routes/web.php +++ b/routes/web.php @@ -403,11 +403,14 @@ Route::get('/1688/fix', function(){ array_pop($products); foreach ($products as $product){ $product = preg_split('/[\t]/',$product); - $type = count($product) === 10 ? 2 : 1; + + $adjuster = (count($product) - 7); + $descriptionIndex = 3 + $adjuster; + $bookings[$document->owner->marking] = [ - 'description' => str_replace("\n","",$product[$type === 2 ? 6 : 3]), - 'quantity' => (int) str_replace("\n","",$product[$type === 2 ? 8 : 5]), - 'unit_price' => (float) str_replace("\n","",$product[$type === 2 ? 9 : 6]) + 'description' => str_replace("\n","",$product[$descriptionIndex]), + 'quantity' => (int) str_replace("\n","",$product[$descriptionIndex + 2]), + 'unit_price' => (float) str_replace("\n","",$product[$descriptionIndex + 3]) ]; } } From 16d823f10c75124ee8d37e4545b894d39ab3653a Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Thu, 29 Sep 2022 04:23:51 +0800 Subject: [PATCH 04/66] convert 1688 purchase orders to po --- routes/web.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/web.php b/routes/web.php index 6aadda47..811a6712 100644 --- a/routes/web.php +++ b/routes/web.php @@ -407,7 +407,7 @@ Route::get('/1688/fix', function(){ $adjuster = (count($product) - 7); $descriptionIndex = 3 + $adjuster; - $bookings[$document->owner->marking] = [ + $bookings[$document->owner->marking][] = [ 'description' => str_replace("\n","",$product[$descriptionIndex]), 'quantity' => (int) str_replace("\n","",$product[$descriptionIndex + 2]), 'unit_price' => (float) str_replace("\n","",$product[$descriptionIndex + 3]) From 52974fd5149249943d6074970ff4c643dfd6e467 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Thu, 29 Sep 2022 05:01:45 +0800 Subject: [PATCH 05/66] convert 1688 purchase orders to po --- routes/web.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routes/web.php b/routes/web.php index 811a6712..84980320 100644 --- a/routes/web.php +++ b/routes/web.php @@ -394,6 +394,8 @@ Route::get('/1688/fix', function(){ } $text = $pdf->getText(); + if(str_contains($text, '订单详情单')) continue; + $tables = explode('Amount (yuan)', $text); array_shift($tables); From c9fadcaed51dcb5723aad9f16d564441a6580f32 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 30 Sep 2022 02:19:35 +0800 Subject: [PATCH 06/66] convert 1688 purchase order to po on document upload --- ...eatePurchaseOrderFor1688OrderProcessor.php | 37 +++++++----- .../Convert1688PurchaseOrderToProductList.php | 58 +++++++++++++++++++ 2 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php diff --git a/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php b/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php index aa7f2ed7..1dfbe85c 100644 --- a/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php +++ b/app/Classes/Modules/Bookings/Processors/CreatePurchaseOrderFor1688OrderProcessor.php @@ -3,12 +3,15 @@ namespace App\Classes\Modules\Bookings\Processors; +use App\Classes\Exceptions\MalformedRequestException; +use App\Classes\Modules\Bookings\Services\Convert1688PurchaseOrderToProductList; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor; use App\Classes\Modules\Transactions\Processors\CreatePurchaseOrderTransactionProcessor; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\DocumentType; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\ValueObjects\Constants\TransactionType; use App\Models\Booking; @@ -27,32 +30,39 @@ class CreatePurchaseOrderFor1688OrderProcessor /** @var CreateInvoiceTransactionProcessor */ private $createInvoiceTransactionProcessor; + /** @var Convert1688PurchaseOrderToProductList */ + private $convert1688PurchaseOrderToProductList; + /** - * CreatePurchaseOrderFor1688OrderProcessor constructor. * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber * @param CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor * @param UpdatesTransactionStatus $updatesTransactionStatus * @param CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor + * @param Convert1688PurchaseOrderToProductList $convert1688PurchaseOrderToProductList */ - public function __construct(GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, UpdatesTransactionStatus $updatesTransactionStatus, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor) + public function __construct(GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatePurchaseOrderTransactionProcessor $createPurchaseOrderTransactionProcessor, UpdatesTransactionStatus $updatesTransactionStatus, CreateInvoiceTransactionProcessor $createInvoiceTransactionProcessor, Convert1688PurchaseOrderToProductList $convert1688PurchaseOrderToProductList) { $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; $this->createPurchaseOrderTransactionProcessor = $createPurchaseOrderTransactionProcessor; $this->updatesTransactionStatus = $updatesTransactionStatus; $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; + $this->convert1688PurchaseOrderToProductList = $convert1688PurchaseOrderToProductList; } + public function execute(Booking $booking){ $billNumber = $this->generatesTransactionBillNumber->execute('PO-'); - $products = [ - [ - 'description' => 'please refer to the attachment below', - 'quantity' => 1, - 'stockCode' => '', - 'unit_price' => $booking->fix_amount - ] - ]; + $documents = $booking->documents()->where('document_type', DocumentType::ECOMMERCE_PURCHASE_ORDER)->get(); + foreach($documents as $document) { + foreach ($document->files as $file) { + try { + $products = $this->convert1688PurchaseOrderToProductList->execute($file); + } catch (MalformedRequestException $exception) { + continue; + } + } + } $total = collect($products)->sum(function($product){ return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price'])); @@ -63,11 +73,8 @@ class CreatePurchaseOrderFor1688OrderProcessor $total, $total, $booking->fix_currency_id, $booking->fix_currency_id, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $products); - $purchaseOrder = $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); + $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); - $this->updatesTransactionStatus->execute($purchaseOrder, ApprovalStatus::APPROVED); - - $this->createInvoiceTransactionProcessor->execute($booking); } -} \ No newline at end of file +} diff --git a/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php b/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php new file mode 100644 index 00000000..f22c50e7 --- /dev/null +++ b/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php @@ -0,0 +1,58 @@ +parseFile(Storage::disk('documents')->path($file->file->file_info->original->file)); + } catch (Exception $exception) { + throw new MalformedRequestException('Invalid file format.'); + } + + $text = $pdf->getText(); + if(str_contains($text, '订单详情单')) throw new MalformedRequestException('Can\'t process non english documents'); + + $tables = explode('Amount (yuan)', $text); + array_shift($tables); + + foreach ($tables as $table){ + $products = preg_split('/(yuan\/|yuan \/|元\/|元 \/)/', str_replace("\r\n","",$table)); + + array_pop($products); + foreach ($products as $product){ + $product = preg_split('/[\t]/',$product); + + $adjuster = (count($product) - 7); + $descriptionIndex = 3 + $adjuster; + + $quantity = (int) str_replace("\n","",$product[$descriptionIndex + 2]); + $unitPrice = (float) str_replace("\n","",$product[$descriptionIndex + 3]); + + $productList[] = [ + 'stockCode' => str_replace("\n","",$product[$descriptionIndex]), + 'description' => str_replace("\n","",$product[$descriptionIndex]), + 'quantity' => $quantity, + 'unit_price' => $unitPrice, + 'total' => (float) str_replace("\n","",$product[$descriptionIndex + 3]) + ]; + } + } + + return $productList; + } +} From d1d4b2a770f4f854647578a29d4c85b0552dde7d Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 30 Sep 2022 02:26:39 +0800 Subject: [PATCH 07/66] convert 1688 purchase order to po on document upload --- .../bookings/sections/BookingDetailsSectionComponent.vue | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue index 12d91e5d..39172163 100644 --- a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue +++ b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue @@ -228,7 +228,7 @@
-

Due to the system upgrades, you are no longer required to fill out the Purchase Order. We apologize that the invoices won't be available until the system has been fully upgraded. Thanks for your patience.

+

You are no longer required to key in your purchase order details manually, the system will automatically get your purchase order detail from 1688.

@@ -263,7 +263,7 @@
- +
@@ -494,4 +494,4 @@ } } } - \ No newline at end of file + From ff3baa2b9cd3bb39173550dffc6ef3dd35767fb1 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 30 Sep 2022 02:30:05 +0800 Subject: [PATCH 08/66] convert 1688 purchase order to po on document upload --- .../Bookings/Services/Convert1688PurchaseOrderToProductList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php b/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php index f22c50e7..faeb585a 100644 --- a/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php +++ b/app/Classes/Modules/Bookings/Services/Convert1688PurchaseOrderToProductList.php @@ -44,7 +44,7 @@ class Convert1688PurchaseOrderToProductList $unitPrice = (float) str_replace("\n","",$product[$descriptionIndex + 3]); $productList[] = [ - 'stockCode' => str_replace("\n","",$product[$descriptionIndex]), + 'stockCode' => '', 'description' => str_replace("\n","",$product[$descriptionIndex]), 'quantity' => $quantity, 'unit_price' => $unitPrice, From dea9d5f4150e122ba41e5bda9899fa5e66485533 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Fri, 30 Sep 2022 02:34:22 +0800 Subject: [PATCH 09/66] convert 1688 purchase order to po on document upload --- .../bookings/sections/BookingDetailsSectionComponent.vue | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue index 39172163..d12c42b9 100644 --- a/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue +++ b/resources/assets/vue/components/bookings/sections/BookingDetailsSectionComponent.vue @@ -228,14 +228,14 @@
-

You are no longer required to key in your purchase order details manually, the system will automatically get your purchase order detail from 1688.

+

You are no longer required to key in your purchase order details manually, the system will automatically get your purchase order detail from 1688 directly.

-
- +
+