From 27c0a4f8ee1d4279e8febd1ae6c0e5ca2b3623b4 Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Thu, 4 Feb 2021 15:26:14 +0530 Subject: [PATCH] Order cancelation for downloadble product --- ...n_in_downloadable_link_purchased_table.php | 32 +++++++++++++++++++ .../src/Models/DownloadableLinkPurchased.php | 1 + .../DownloadableLinkPurchasedRepository.php | 27 +++++++++++++--- .../DataGrids/DownloadableProductDataGrid.php | 2 +- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 packages/Webkul/Sales/src/Database/Migrations/2021_02_04_150033_add_download_canceled_column_in_downloadable_link_purchased_table.php diff --git a/packages/Webkul/Sales/src/Database/Migrations/2021_02_04_150033_add_download_canceled_column_in_downloadable_link_purchased_table.php b/packages/Webkul/Sales/src/Database/Migrations/2021_02_04_150033_add_download_canceled_column_in_downloadable_link_purchased_table.php new file mode 100644 index 000000000..dfd1a503e --- /dev/null +++ b/packages/Webkul/Sales/src/Database/Migrations/2021_02_04_150033_add_download_canceled_column_in_downloadable_link_purchased_table.php @@ -0,0 +1,32 @@ +integer('download_canceled')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('downloadable_link_purchased', function (Blueprint $table) { + $table->integer('download_canceled')->default(0); + }); + } +} diff --git a/packages/Webkul/Sales/src/Models/DownloadableLinkPurchased.php b/packages/Webkul/Sales/src/Models/DownloadableLinkPurchased.php index d769f2d29..08a25f214 100644 --- a/packages/Webkul/Sales/src/Models/DownloadableLinkPurchased.php +++ b/packages/Webkul/Sales/src/Models/DownloadableLinkPurchased.php @@ -24,6 +24,7 @@ class DownloadableLinkPurchased extends Model implements DownloadableLinkPurchas 'customer_id', 'order_id', 'order_item_id', + 'download_canceled', ]; /** diff --git a/packages/Webkul/Sales/src/Repositories/DownloadableLinkPurchasedRepository.php b/packages/Webkul/Sales/src/Repositories/DownloadableLinkPurchasedRepository.php index bab3bdb25..9bec3c655 100644 --- a/packages/Webkul/Sales/src/Repositories/DownloadableLinkPurchasedRepository.php +++ b/packages/Webkul/Sales/src/Repositories/DownloadableLinkPurchasedRepository.php @@ -84,7 +84,7 @@ class DownloadableLinkPurchasedRepository extends Repository if (stristr($orderItem->type,'downloadable') !== false && isset($orderItem->additional['links'])) { return true; } - + return false; } @@ -98,9 +98,28 @@ class DownloadableLinkPurchasedRepository extends Repository $purchasedLinks = $this->findByField('order_item_id', $orderItem->id); foreach ($purchasedLinks as $purchasedLink) { - $this->update([ - 'status' => $status, - ], $purchasedLink->id); + if ($status == 'expired') { + if (count($purchasedLink->order_item->invoice_items) > 0) { + $totalInvoiceQty = 0; + + foreach ($purchasedLink->order_item->invoice_items as $invoice_item) { + $totalInvoiceQty = $totalInvoiceQty + $invoice_item->qty; + } + + $this->update([ + 'download_canceled' => $purchasedLink->download_bought - $totalInvoiceQty, + ], $purchasedLink->id); + } else { + $this->update([ + 'status' => $status, + 'download_canceled' => $purchasedLink->download_bought, + ], $purchasedLink->id); + } + } else { + $this->update([ + 'status' => $status, + ], $purchasedLink->id); + } } } } \ No newline at end of file diff --git a/packages/Webkul/Shop/src/DataGrids/DownloadableProductDataGrid.php b/packages/Webkul/Shop/src/DataGrids/DownloadableProductDataGrid.php index 3c9db72d2..4fcd54e08 100644 --- a/packages/Webkul/Shop/src/DataGrids/DownloadableProductDataGrid.php +++ b/packages/Webkul/Shop/src/DataGrids/DownloadableProductDataGrid.php @@ -18,7 +18,7 @@ class DownloadableProductDataGrid extends DataGrid ->leftJoin('orders', 'downloadable_link_purchased.order_id', '=', 'orders.id') ->leftJoin('invoices', 'downloadable_link_purchased.order_id', '=', 'invoices.order_id') ->addSelect('downloadable_link_purchased.*', 'invoices.state as invoice_state', 'orders.increment_id') - ->addSelect(DB::raw('(' . DB::getTablePrefix() . 'downloadable_link_purchased.download_bought - ' . DB::getTablePrefix() . 'downloadable_link_purchased.download_used) as remaining_downloads')) + ->addSelect(DB::raw('(' . DB::getTablePrefix() . 'downloadable_link_purchased.download_bought - ' . DB::getTablePrefix() . 'downloadable_link_purchased.download_canceled - ' . DB::getTablePrefix() . 'downloadable_link_purchased.download_used) as remaining_downloads')) ->where('downloadable_link_purchased.customer_id', auth()->guard('customer')->user()->id); $this->addFilter('status', 'downloadable_link_purchased.status');