Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
24 / 32
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ShippingPackagingTotalTransferService
75.00% covered (warning)
75.00%
24 / 32
0.00% covered (danger)
0.00%
0 / 2
6.56
0.00% covered (danger)
0.00%
0 / 1
 transfer
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
2.15
 processProducts
78.26% covered (warning)
78.26%
18 / 23
0.00% covered (danger)
0.00%
0 / 1
4.16
1<?php
2
3namespace Domain\ShippingPackaging\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\Item\Item;
7use App\Models\PickingOrder\PickingOrder;
8use App\Models\ShippingPackaging\ShippingPackaging;
9use App\Models\ShippingPackaging\ShippingPackagingProduct;
10use App\Models\User;
11use Illuminate\Support\Facades\DB;
12use Throwable;
13
14readonly class ShippingPackagingTotalTransferService
15{
16    /**
17     * @param ShippingPackaging $shippingPackaging
18     * @param ShippingPackaging $shippingPackagingDest
19     * @param User $user
20     * @return void
21     * @throws Throwable
22     */
23    public function transfer(ShippingPackaging $shippingPackaging, ShippingPackaging $shippingPackagingDest, User $user): void
24    {
25        try {
26            DB::beginTransaction();
27
28            $this->processProducts($shippingPackaging, $shippingPackagingDest);
29            $shippingPackaging->pickingOrders()->update(['shipping_packaging_id' => $shippingPackagingDest->getKey()]);
30            $shippingPackaging->delete();
31
32            DB::commit();
33        } catch (Throwable $throwable) {
34            DB::rollBack();
35            throw $throwable;
36        }
37
38        ServiceProcessed::dispatch(self::class, $user);
39    }
40
41    private function processProducts(ShippingPackaging $shippingPackaging, ShippingPackaging $shippingPackagingDest)
42    {
43        foreach($shippingPackaging->products as $product){
44
45            $shippingPackagingProductId = DB::table('shipping_packaging_products')
46                ->join('picked_items', 'shipping_packaging_products.picked_item_id', '=', 'picked_items.id')
47                ->join('stock_io_products', 'picked_items.stock_io_product_id', '=', 'stock_io_products.id')
48                ->join('items', 'picked_items.item_id', '=', 'items.id')
49                ->join('integrations', function ($join) {
50                    $join->on('integrations.integrable_id', '=', 'items.id')
51                        ->where('integrations.integrable_type', Item::class);
52                })
53                ->where('picked_items.id', $product->pickedItem->getKey())
54                ->where('shipping_packaging_products.shipping_packaging_id', $shippingPackagingDest->getKey())
55                ->where('items.product_id', $product->pickedItem->item->product->getKey())
56                ->where('stock_io_products.stock_io_id', $product->pickedItem->stockIOProduct->stockIO->getKey())
57                ->whereRaw("integrations.parameters->>'production_order' = ?", $product->pickedItem->item->integration->getAttribute('parameters')['production_order'])
58                ->select('shipping_packaging_products.id')
59                ->get();
60
61            if(!$shippingPackagingProductId->isEmpty()){
62                $shippingPackagingProductDest = ShippingPackagingProduct::find($shippingPackagingProductId[0]->id);
63
64                if($shippingPackagingProductDest){
65                    $shippingPackagingProductDest->setAttribute('amount', round($shippingPackagingProductDest->amount + $product->amount, 2));
66                    $shippingPackagingProductDest->save();
67
68                    $product->delete();
69                }
70            } else {
71                $product->update(['shipping_packaging_id' => $shippingPackagingDest->getKey()]);
72            }
73        }
74    }
75}