fix duplicated invoices

This commit is contained in:
Omair Saleh
2023-02-28 12:03:17 +08:00
parent cbda7aa6ba
commit d61a0c2104
3 changed files with 21 additions and 89 deletions
@@ -2,7 +2,9 @@
namespace App\Classes\Modules\PackingLists\Processors;
use App\Classes\Exceptions\AccessForbiddenException;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Exceptions\RequestValidationException;
use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Orders\Services\FetchesDataFromYDPortal;
@@ -38,6 +40,7 @@ use App\Models\Order;
use App\Models\PackingList;
use App\Models\Transport;
use Carbon\Carbon;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
@@ -47,18 +50,6 @@ class FetchContainersFromYdPortalProcessor
/** @var FetchesDataFromYDPortal */
private $fetchesDataFRomYDPortal;
/** @var FetchesOrder */
private $fetchesOrder;
/** @var UpdatesContractObligation */
private $updatesContractObligations;
/** @var CreatePackingListProcessor */
private $createPackingListProcessor;
/** @var CreatePackageProcessor */
private $createPackageProcessor;
/** @var CreatesTransport */
private $createsTransport;
@@ -68,75 +59,31 @@ class FetchContainersFromYdPortalProcessor
/** @var FetchesContainer */
private $fetchesContainer;
/** @var FetchesPackingList */
private $fetchesPackingList;
/** @var FetchesCompanyModule */
private $fetchesCompanyModule;
/** @var CreateContainerProcessor */
private $createContainerProcessor;
/** @var CreatesContract */
private $unityCreateContract;
/** @var AssignContractEntityProcessor */
private $unityAssignContractEntity;
/** @var CreateContractEntityProcessor */
private $unityCreateContractEntity;
/** @var CreatesStep */
private $createsStep;
/** @var ActivateContractProcessor */
private $unityActivateContract;
/**
* FetchOrderListsFromYdPortalProcessor constructor.
* @param FetchesDataFromYDPortal $fetchesDataFRomYDPortal
* @param FetchesOrder $fetchesOrder
* @param UpdatesContractObligation $updatesContractObligations
* @param CreatePackingListProcessor $createPackingListProcessor
* @param CreatePackageProcessor $createPackageProcessor
* @param CreatesTransport $createsTransport
* @param CreatesSchedule $createsSchedule
* @param FetchesContainer $fetchesContainer
* @param FetchesPackingList $fetchesPackingList
* @param FetchesCompanyModule $fetchesCompanyModule
* @param CreateContainerProcessor $createContainerProcessor
* @param CreatesContract $unityCreateContract
* @param AssignContractEntityProcessor $unityAssignContractEntity
* @param CreateContractEntityProcessor $unityCreateContractEntity
* @param CreatesStep $createsStep
* @param ActivateContractProcessor $unityActivateContract
*/
public function __construct(FetchesDataFromYDPortal $fetchesDataFRomYDPortal, FetchesOrder $fetchesOrder, UpdatesContractObligation $updatesContractObligations, CreatePackingListProcessor $createPackingListProcessor, CreatePackageProcessor $createPackageProcessor, CreatesTransport $createsTransport, CreatesSchedule $createsSchedule, FetchesContainer $fetchesContainer, FetchesPackingList $fetchesPackingList, FetchesCompanyModule $fetchesCompanyModule, CreateContainerProcessor $createContainerProcessor, CreatesContract $unityCreateContract, AssignContractEntityProcessor $unityAssignContractEntity, CreateContractEntityProcessor $unityCreateContractEntity, CreatesStep $createsStep, ActivateContractProcessor $unityActivateContract)
public function __construct(FetchesDataFromYDPortal $fetchesDataFRomYDPortal, CreatesTransport $createsTransport, CreatesSchedule $createsSchedule, FetchesContainer $fetchesContainer, CreateContainerProcessor $createContainerProcessor)
{
$this->fetchesDataFRomYDPortal = $fetchesDataFRomYDPortal;
$this->fetchesOrder = $fetchesOrder;
$this->updatesContractObligations = $updatesContractObligations;
$this->createPackingListProcessor = $createPackingListProcessor;
$this->createPackageProcessor = $createPackageProcessor;
$this->createsTransport = $createsTransport;
$this->createsSchedule = $createsSchedule;
$this->fetchesContainer = $fetchesContainer;
$this->fetchesPackingList = $fetchesPackingList;
$this->fetchesCompanyModule = $fetchesCompanyModule;
$this->createContainerProcessor = $createContainerProcessor;
$this->unityCreateContract = $unityCreateContract;
$this->unityAssignContractEntity = $unityAssignContractEntity;
$this->unityCreateContractEntity = $unityCreateContractEntity;
$this->createsStep = $createsStep;
$this->unityActivateContract = $unityActivateContract;
}
/**
* @param Carbon|null $start
* @param Carbon|null $end
* @return void
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws MalformedRequestException
* @throws AccessForbiddenException
* @throws RequestValidationException
*/
public function execute()
{
@@ -51,7 +51,7 @@ class FetchContainersUpdatesFromYdPortalProcessor
foreach ($containers as $container) {
$time_start = microtime(true);
$packingList = $container->packingLists()->first();
$packingList = $container->packingLists()->random();
$trackingRequest = $this->fetchesDataFRomYDPortal->clientRequest('http://www.yd-wl.com/api/ApiTracking.ashx', 'GET', [
'trakingno' => $packingList->reference
+13 -28
View File
@@ -807,7 +807,7 @@ Route::get('/invoices/show-duplicated', function(Request $request){
->having('count', '>', 1)
->get();
// delete invoice type suspended
// delete invoice type suspended
echo '<table>';
echo "<tr>";
@@ -835,7 +835,7 @@ Route::get('/invoices/show-duplicated', function(Request $request){
echo "<td style='border:1px solid'>Invoice Owner ID</td>";
echo "<td style='border:1px solid'>Invoice Owner ID</td>";
echo "</tr>";
foreach($duplicatedInvoice as $invoice) {
$order = $invoice->owner->owner;
echo "<tr>";
@@ -851,48 +851,33 @@ Route::get('/invoices/show-duplicated', function(Request $request){
Route::get('/invoices/delete-duplicated', function(Request $request){
$duplicatedInvoiceId = DB::table('transactions')
$duplicatedInvoices = DB::table('transactions')
->where('type', TransactionType::SHIPPING_INVOICE)
->where('status', '!=' , ApprovalStatus::COMPLETED)
->where('deleted_at', null)
->select('owner_id', DB::raw('count(*) as count'))
->groupBy('owner_id')
->having('count', '>', 1)
->get()->pluck('owner_id');
->get();
// delete invoice type suspended
// delete invoice type suspended
$duplicatedInvoice = Transaction::whereIn('owner_id', $duplicatedInvoiceId)->get();
foreach($duplicatedInvoice as $invoice) {
$packingList = $invoice->owner;
$duplicatedInvoices = Transaction::whereIn('owner_id', $duplicatedInvoices->pluck('owner_id'))->get()->groupBy('owner_id');
foreach($duplicatedInvoices as $invoice) {
$packingList = $invoice->first()->owner;
$order = $packingList->owner;
$order_marking = $order->reference;
$paidInvoice = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::COMPLETED)->get();
// check inside packingList has how many unpaid invoice
$unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->get()->sortByDesc('created_at');
$unpaidInvoices = $packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->orderBy('id', 'desc')->get();
print_r('unpaidInvoices');
dump($unpaidInvoices);
if (count($paidInvoice)) {
// delete all other invoice
print_r('unpaidInvoices - has count paidInvoice');
dump($unpaidInvoices);
// $unpaidInvoices->delete();
$packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->delete();
continue;
}
// does not have paid invoice, then delete all but the latest
$unpaidInvoicesIds = $unpaidInvoices->pluck('id')->toArray();
$latestInvoiceId = array_shift($unpaidInvoicesIds);
array_shift($unpaidInvoicesIds);
print_r('unpaidInvoicesIds');
dump($unpaidInvoicesIds);
// Transaction::whereIn('id', $unpaidInvoicesIds)->delete();
// dump(Transaction::whereIn('id', $unpaidInvoicesIds)->get());
Transaction::whereIn('id', $unpaidInvoicesIds)->delete();
}
});
});