order item iptal completed
This commit is contained in:
parent
6e52f0cd0c
commit
1ea37e3c6e
|
|
@ -3,11 +3,20 @@
|
|||
namespace Sarga\API\Http\Controllers;
|
||||
|
||||
use Sarga\API\Http\Resources\Customer\OrderResource;
|
||||
use Sarga\Shop\Repositories\OrderItemRepository;
|
||||
use Sarga\Shop\Repositories\OrderRepository;
|
||||
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\OrderController;
|
||||
|
||||
|
||||
class Orders extends OrderController
|
||||
{
|
||||
public function __construct(OrderRepository $orderRepository, OrderItemRepository $orderItemRepository)
|
||||
{
|
||||
$this->orderRepository = $orderRepository;
|
||||
|
||||
$this->orderItemRepository = $orderItemRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resource class name.
|
||||
*
|
||||
|
|
@ -18,7 +27,29 @@ class Orders extends OrderController
|
|||
return OrderResource::class;
|
||||
}
|
||||
|
||||
public function cancelItem(){
|
||||
public function cancelItem($order_id,$item_id){
|
||||
$order = request()->user()->all_orders()->findOrFail($order_id);
|
||||
|
||||
$orderItem = $this->orderItemRepository->with('order')
|
||||
->findOrFail($item_id);
|
||||
|
||||
if($this->orderItemRepository->cancel($orderItem))
|
||||
{
|
||||
$order = $this->orderRepository->calculateTotals($order,$orderItem);
|
||||
$order = $this->orderRepository->updateOrderStatus($order);
|
||||
|
||||
return response(['data'=>[
|
||||
'order' => new OrderResource($order)],
|
||||
'success'=>true,
|
||||
'message'=>trans('admin::app.response.cancel-success', ['name' => 'Order Item'])
|
||||
]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return response([
|
||||
'success'=>false,
|
||||
'message'=>trans('admin::app.response.cancel-error', ['name' => 'Order Item'])
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ class Orders extends OrderController
|
|||
if($this->orderItemRepository->cancel($orderItem))
|
||||
{
|
||||
$this->orderRepository->updateOrderStatus($order);
|
||||
|
||||
$this->orderRepository->calculateTotals($order,$orderItem);
|
||||
session()->flash('success', trans('admin::app.response.cancel-success', ['name' => 'Order Item']));
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -353,7 +353,7 @@
|
|||
|
||||
<td>{{ core()->formatPrice($item->total + $item->tax_amount - $item->discount_amount,$order->order_currency_code) }}</td>
|
||||
<td class="action">
|
||||
@if($item->canCancel())
|
||||
@if($item->canCancel() && in_array($order->status, ['pending','processing']) && $order->total_item_count>1)
|
||||
<a href="{{ route('admin.sales.orders.cancel_item', $item->id) }}">
|
||||
<i class="icon trash-icon"></i>
|
||||
</a>
|
||||
|
|
@ -459,18 +459,6 @@
|
|||
<td>-</td>
|
||||
<td>{{ core()->formatPrice($order->grand_total_refunded,$order->order_currency_code) }}</td>
|
||||
</tr>
|
||||
|
||||
<tr class="bold">
|
||||
<td>{{ __('admin::app.sales.orders.total-due') }}</td>
|
||||
|
||||
<td>-</td>
|
||||
|
||||
@if($order->status !== 'canceled')
|
||||
<td>{{ core()->formatPrice($order->total_due,$order->order_currency_code) }}</td>
|
||||
@else
|
||||
<td id="due-amount-on-cancelled">{{ core()->formatBasePrice(0.00) }}</td>
|
||||
@endif
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -5,10 +5,9 @@ namespace Sarga\Shop\Repositories;
|
|||
use Webkul\Sales\Repositories\OrderItemRepository as WOrderItemRepo;
|
||||
class OrderItemRepository extends WOrderItemRepo
|
||||
{
|
||||
public function cancel($itemId){
|
||||
$item = $this->find($itemId);
|
||||
public function cancel($item){
|
||||
|
||||
if(! $item || ! $item->qty_to_cancel) {
|
||||
if(! $item->qty_to_cancel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +40,8 @@ class OrderItemRepository extends WOrderItemRepo
|
|||
}
|
||||
}
|
||||
|
||||
$this->collectTotals($item);
|
||||
//todo order status check, calculate totals correct
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -40,9 +40,43 @@ class OrderRepository extends WOrderRepository
|
|||
$order->save();
|
||||
|
||||
Event::dispatch('sales.order.update-status.after', $order);
|
||||
return $order;
|
||||
}
|
||||
|
||||
public function isInProcessingState($order){
|
||||
return $order->items()->sum('qty_invoiced') > 0;
|
||||
}
|
||||
|
||||
public function calculateTotals($order, $order_item){
|
||||
if($order && $order_item && $order->shipping_amount >0){
|
||||
$total_weight = $order_item->total_weight;
|
||||
foreach($order->items as $item ){
|
||||
if($item->id !== $order_item->id)
|
||||
$total_weight += ($item->qty_ordered - $item->qty_canceled) * $item->weight;
|
||||
}
|
||||
$shipping_price = $order->shipping_amount/$total_weight;
|
||||
$base_shipping_price = $order->base_shipping_amount/$total_weight;
|
||||
$canceled_amount = $shipping_price * $order_item->qty_canceled * $order_item->weight;
|
||||
$canceled_base_amount = $base_shipping_price * $order_item->qty_canceled * $order_item->weight;
|
||||
$order->shipping_amount = $order->shipping_amount - $canceled_amount;
|
||||
$order->base_shipping_amount = $order->base_shipping_amount - $canceled_base_amount;
|
||||
$order->grand_total = $order->grand_total - $canceled_amount;
|
||||
$order->base_grand_total = $order->base_grand_total - $canceled_base_amount;
|
||||
}
|
||||
|
||||
if($order && $order_item){
|
||||
$total_price = $order_item->price * $order_item->qty_canceled;
|
||||
$total_base_price = $order_item->base_price * $order_item->qty_canceled;
|
||||
$total_discount = $total_price * $order_item->discount_percent/100;
|
||||
$total_base_discount = $total_base_price * $order_item->discount_percent/100;
|
||||
$order->sub_total = $order->sub_total - $total_price;
|
||||
$order->base_sub_total = $order->base_sub_total - $total_base_price;
|
||||
$order->grand_total = $order->grand_total - ($total_price + $total_discount);
|
||||
$order->base_grand_total = $order->base_grand_total-($total_base_price + $total_base_discount);
|
||||
}
|
||||
|
||||
$order->save();
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue