Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.37% covered (danger)
47.37%
27 / 57
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TransactionStoreService
47.37% covered (danger)
47.37%
27 / 57
50.00% covered (danger)
50.00%
3 / 6
11.25
0.00% covered (danger)
0.00%
0 / 1
 recordTransaction
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 input
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 output
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 transfer
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 adjustment
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 populateTransactionDTO
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\Transaction\Services;
4
5use App\Models\Item\Item;
6use App\Models\Transaction\Transaction;
7use App\Models\User;
8use Domain\Item\Interfaces\HasItemsInterface;
9use Domain\StockIO\Enums\StockItemAdjustmentReasonEnum;
10use Domain\Transaction\DTOs\TransactionDTO;
11use Domain\Transaction\Enums\{TransactionInputReasonEnum, TransactionOutputReasonEnum, TransactionTransferReasonEnum, TransactionTypeEnum};
12
13class TransactionStoreService
14{
15    public function recordTransaction(TransactionDTO $transactionDTO, User $user): Transaction
16    {
17        $transaction = new Transaction();
18        $transaction->fill($transactionDTO->toArray());
19        $transaction->user()->associate($user);
20        $transaction->save();
21
22        return $transaction;
23    }
24
25    /**
26     * Summary of input
27     * @param  Item                       $item   The item being moved or recorded.
28     * @param  HasItemsInterface          $from   The source entity of the transaction (e.g., supplier, warehouse).
29     * @param  HasItemsInterface          $to     The destination entity of the transaction (e.g., stock, department).
30     * @param  float                      $amount The quantity of the item being input.
31     * @param  TransactionInputReasonEnum $reason The reason for the input (e.g., purchase, return, adjustment).
32     * @param  User                       $user   The user responsible for performing the transaction.
33     * @param  int                        $sector The sector identifier associated with the transaction.
34     *
35     * @return void
36     */
37    public function input(
38        Item $item,
39        HasItemsInterface $from,
40        HasItemsInterface $to,
41        float $amount,
42        TransactionInputReasonEnum $reason,
43        User $user,
44        int $sector
45    ): void {
46        $transactionDTO = $this->populateTransactionDTO(
47            $item,
48            $to,
49            $from,
50            $amount,
51            TransactionTypeEnum::INPUT,
52            $reason->value,
53            $sector
54        );
55
56        $this->recordTransaction($transactionDTO, $user);
57    }
58
59    /**
60     * Summary of output
61     * @param  Item                       $item   The item being moved or recorded.
62     * @param  HasItemsInterface          $from   The source entity of the transaction (e.g., supplier, warehouse).
63     * @param  HasItemsInterface          $to     The destination entity of the transaction (e.g., stock, department).
64     * @param  float                      $amount The quantity of the item being input.
65     * @param  TransactionInputReasonEnum $reason The reason for the input (e.g., purchase, return, adjustment).
66     * @param  User                       $user   The user responsible for performing the transaction.
67     * @param  int                        $sector The sector identifier associated with the transaction.
68     *
69     * @return void
70     */
71    public function output(
72        Item $item,
73        HasItemsInterface $from,
74        HasItemsInterface $to,
75        float $amount,
76        TransactionOutputReasonEnum $reason,
77        User $user,
78        int $sector
79    ): void {
80        $transactionDTO = $this->populateTransactionDTO(
81            $item,
82            $to,
83            $from,
84            $amount,
85            TransactionTypeEnum::OUTPUT,
86            $reason->value,
87            $sector
88        );
89
90        $this->recordTransaction($transactionDTO, $user);
91    }
92
93    /**
94     * Summary of transfer
95     * @param  Item                       $item   The item being moved or recorded.
96     * @param  HasItemsInterface          $from   The source entity of the transaction (e.g., supplier, warehouse).
97     * @param  HasItemsInterface          $to     The destination entity of the transaction (e.g., stock, department).
98     * @param  float                      $amount The quantity of the item being input.
99     * @param  TransactionInputReasonEnum $reason The reason for the input (e.g., purchase, return, adjustment).
100     * @param  User                       $user   The user responsible for performing the transaction.
101     * @param  int                        $sector The sector identifier associated with the transaction.
102     *
103     * @return void
104     */
105    public function transfer(
106        Item $item,
107        HasItemsInterface $from,
108        HasItemsInterface $to,
109        float $amount,
110        TransactionTransferReasonEnum $reason,
111        User $user,
112        int $sector
113    ): void {
114        $transactionDTO = $this->populateTransactionDTO(
115            $item,
116            $to,
117            $from,
118            $amount,
119            TransactionTypeEnum::TRANSFER,
120            $reason->value,
121            $sector
122        );
123
124        $this->recordTransaction($transactionDTO, $user);
125    }
126
127    /**
128     * Summary of adjustment
129     * @param  Item                       $item   The item being moved or recorded.
130     * @param  HasItemsInterface          $from   The source entity of the transaction (e.g., supplier, warehouse).
131     * @param  HasItemsInterface          $to     The destination entity of the transaction (e.g., stock, department).
132     * @param  float                      $amount The quantity of the item being input.
133     * @param  TransactionInputReasonEnum $reason The reason for the input (e.g., purchase, return, adjustment).
134     * @param  User                       $use    The user responsible for performing the transaction.
135     * @param  int                        $sector The sector identifier associated with the transaction.
136     *
137     * @return void
138     */
139    public function adjustment(
140        Item $item,
141        HasItemsInterface $from,
142        HasItemsInterface $to,
143        float $amount,
144        StockItemAdjustmentReasonEnum $reason,
145        User $user,
146        int $sector
147    ): void {
148        $transactionDTO = $this->populateTransactionDTO(
149            $item,
150            $to,
151            $from,
152            $amount,
153            TransactionTypeEnum::ADJUSTMENT,
154            $reason->value,
155            $sector
156        );
157
158        $this->recordTransaction($transactionDTO, $user);
159    }
160
161    /**
162     * Summary of populateTransactionDTO
163     * @param  Item                       $item   The item being moved or recorded.
164     * @param  HasItemsInterface          $from   The source entity of the transaction (e.g., supplier, warehouse).
165     * @param  HasItemsInterface          $to     The destination entity of the transaction (e.g., stock, department).
166     * @param  float                      $amount The quantity of the item being input.
167     * @param  TransactionInputReasonEnum $reason The reason for the input (e.g., purchase, return, adjustment).
168     * @param  User                       $user   The user responsible for performing the transaction.
169     * @param  int                        $sector The sector identifier associated with the transaction.
170     *
171     * @return TransactionDTO
172     */
173    private function populateTransactionDTO(
174        Item $item,
175        HasItemsInterface $to,
176        HasItemsInterface $from,
177        float $amount,
178        TransactionTypeEnum $type,
179        int $reason,
180        int $sector
181    ): TransactionDTO {
182        return new TransactionDTO([
183            'item' => $item->load(
184                ['product', 'storage', 'integration', 'product.integration', 'product.measure', 'storage.integration']
185            ),
186            'to' => $to,
187            'from' => $from,
188            'amount' => $amount,
189            'type' => $type,
190            'reason' => $reason,
191            'observation' => '',
192            'sector' => $sector
193        ]);
194    }
195}