2019-06-28 14:18:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Sales\Repositories;
|
|
|
|
|
|
2022-08-08 13:10:37 +00:00
|
|
|
use Illuminate\Container\Container;
|
2019-06-28 14:18:52 +00:00
|
|
|
use Webkul\Core\Eloquent\Repository;
|
|
|
|
|
use Webkul\Sales\Contracts\DownloadableLinkPurchased;
|
|
|
|
|
use Webkul\Product\Repositories\ProductDownloadableLinkRepository;
|
|
|
|
|
|
|
|
|
|
class DownloadableLinkPurchasedRepository extends Repository
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Create a new repository instance.
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
|
2022-08-08 13:10:37 +00:00
|
|
|
* @param \Illuminate\Container\Container $container
|
2019-06-28 14:18:52 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2022-04-01 14:57:11 +00:00
|
|
|
protected ProductDownloadableLinkRepository $productDownloadableLinkRepository,
|
2022-08-08 13:10:37 +00:00
|
|
|
Container $container
|
2019-06-28 14:18:52 +00:00
|
|
|
)
|
|
|
|
|
{
|
2022-08-08 13:10:37 +00:00
|
|
|
parent::__construct($container);
|
2019-06-28 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @return string
|
2019-06-28 14:18:52 +00:00
|
|
|
*/
|
|
|
|
|
function model()
|
|
|
|
|
{
|
|
|
|
|
return DownloadableLinkPurchased::class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\Sales\Contracts\OrderItem $orderItem
|
2019-06-28 14:18:52 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function saveLinks($orderItem)
|
|
|
|
|
{
|
2019-12-27 09:03:01 +00:00
|
|
|
if (! $this->isValidDownloadableProduct($orderItem)) {
|
2019-06-28 14:18:52 +00:00
|
|
|
return;
|
2019-12-17 12:11:02 +00:00
|
|
|
}
|
2019-06-28 14:18:52 +00:00
|
|
|
|
|
|
|
|
foreach ($orderItem->additional['links'] as $linkId) {
|
2020-02-20 06:47:44 +00:00
|
|
|
if (! $productDownloadableLink = $this->productDownloadableLinkRepository->find($linkId)) {
|
2019-06-28 14:18:52 +00:00
|
|
|
continue;
|
2020-02-20 06:47:44 +00:00
|
|
|
}
|
2019-06-28 14:18:52 +00:00
|
|
|
|
|
|
|
|
$this->create([
|
2020-02-20 06:47:44 +00:00
|
|
|
'name' => $productDownloadableLink->title,
|
|
|
|
|
'product_name' => $orderItem->name,
|
|
|
|
|
'url' => $productDownloadableLink->url,
|
|
|
|
|
'file' => $productDownloadableLink->file,
|
|
|
|
|
'file_name' => $productDownloadableLink->file_name,
|
|
|
|
|
'type' => $productDownloadableLink->type,
|
2019-06-28 14:18:52 +00:00
|
|
|
'download_bought' => $productDownloadableLink->downloads * $orderItem->qty_ordered,
|
2020-02-20 06:47:44 +00:00
|
|
|
'status' => 'pending',
|
|
|
|
|
'customer_id' => $orderItem->order->customer_id,
|
|
|
|
|
'order_id' => $orderItem->order_id,
|
2020-03-04 06:32:53 +00:00
|
|
|
'order_item_id' => $orderItem->id,
|
2019-06-28 14:18:52 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 12:11:02 +00:00
|
|
|
/**
|
|
|
|
|
* Return true, if ordered item is valid downloadable product with links
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\Sales\Contracts\OrderItem $orderItem
|
2019-12-17 12:11:02 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2019-12-27 09:03:01 +00:00
|
|
|
private function isValidDownloadableProduct($orderItem) : bool {
|
2022-07-12 12:04:13 +00:00
|
|
|
if (
|
|
|
|
|
stristr($orderItem->type,'downloadable') !== false
|
|
|
|
|
&& isset($orderItem->additional['links'])
|
|
|
|
|
) {
|
2019-12-17 12:11:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-02-04 09:56:14 +00:00
|
|
|
|
2019-12-17 12:11:02 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
/**
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\Sales\Contracts\OrderItem $orderItem
|
|
|
|
|
* @param string $status
|
2019-06-28 14:18:52 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-10-11 05:02:40 +00:00
|
|
|
public function updateStatus($orderItem, $status)
|
2019-06-28 14:18:52 +00:00
|
|
|
{
|
|
|
|
|
$purchasedLinks = $this->findByField('order_item_id', $orderItem->id);
|
|
|
|
|
|
|
|
|
|
foreach ($purchasedLinks as $purchasedLink) {
|
2021-02-04 09:56:14 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 05:17:31 +00:00
|
|
|
$orderedQty = $purchasedLink->order_item->qty_ordered;
|
|
|
|
|
$totalInvoiceQty = $totalInvoiceQty * ($purchasedLink->download_bought / $orderedQty);
|
|
|
|
|
|
2021-02-04 09:56:14 +00:00
|
|
|
$this->update([
|
2021-02-04 11:35:32 +00:00
|
|
|
'status' => $purchasedLink->download_used == $totalInvoiceQty ? $status : $purchasedLink->status,
|
2021-02-04 09:56:14 +00:00
|
|
|
'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);
|
|
|
|
|
}
|
2019-06-28 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|