@@ -10,619 +10,619 @@ |
||
10 | 10 | trait HasItemStocks |
11 | 11 | { |
12 | 12 | /** |
13 | - * Stores the quantity before an update. |
|
14 | - * |
|
15 | - * @var int|float|string |
|
16 | - */ |
|
17 | - private $beforeQuantity = 0; |
|
18 | - |
|
19 | - /** |
|
20 | - * Stores the reason for updating / creating a stock. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $reason = ''; |
|
25 | - |
|
26 | - /** |
|
27 | - * Stores the cost for updating a stock. |
|
28 | - * |
|
29 | - * @var int|float|string |
|
30 | - */ |
|
13 | + * Stores the quantity before an update. |
|
14 | + * |
|
15 | + * @var int|float|string |
|
16 | + */ |
|
17 | + private $beforeQuantity = 0; |
|
18 | + |
|
19 | + /** |
|
20 | + * Stores the reason for updating / creating a stock. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $reason = ''; |
|
25 | + |
|
26 | + /** |
|
27 | + * Stores the cost for updating a stock. |
|
28 | + * |
|
29 | + * @var int|float|string |
|
30 | + */ |
|
31 | 31 | public $cost = 0; |
32 | 32 | |
33 | 33 | protected static function bootHasItemStocks() |
34 | 34 | { |
35 | 35 | static::creating(function (Model $model) { |
36 | 36 | |
37 | - /* |
|
37 | + /* |
|
38 | 38 | * Check if a reason has been set, if not |
39 | 39 | * let's retrieve the default first entry reason |
40 | 40 | */ |
41 | - if (!$model->reason) { |
|
42 | - $model->reason = 'First Item Record; Stock Increase'; |
|
43 | - } |
|
41 | + if (!$model->reason) { |
|
42 | + $model->reason = 'First Item Record; Stock Increase'; |
|
43 | + } |
|
44 | 44 | }); |
45 | 45 | |
46 | 46 | static::created(function (Model $model) { |
47 | - $model->postCreate(); |
|
47 | + $model->postCreate(); |
|
48 | 48 | }); |
49 | 49 | |
50 | 50 | static::updating(function (Model $model) { |
51 | - /* |
|
51 | + /* |
|
52 | 52 | * Retrieve the original quantity before it was updated, |
53 | 53 | * so we can create generate an update with it |
54 | 54 | */ |
55 | - $model->beforeQuantity = $model->getOriginal('quantity'); |
|
55 | + $model->beforeQuantity = $model->getOriginal('quantity'); |
|
56 | 56 | |
57 | - /* |
|
57 | + /* |
|
58 | 58 | * Check if a reason has been set, if not let's retrieve the default change reason |
59 | 59 | */ |
60 | - if (!$model->reason) { |
|
61 | - $model->reason = 'Stock Adjustment'; |
|
62 | - } |
|
60 | + if (!$model->reason) { |
|
61 | + $model->reason = 'Stock Adjustment'; |
|
62 | + } |
|
63 | 63 | }); |
64 | 64 | |
65 | 65 | static::updated(function (Model $model) { |
66 | - $model->postUpdate(); |
|
67 | - }); |
|
66 | + $model->postUpdate(); |
|
67 | + }); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
71 | - * Generates a stock movement on the creation of a stock. |
|
72 | - */ |
|
73 | - public function postCreate() |
|
74 | - { |
|
75 | - if (!$this->getLastMovement()) { |
|
76 | - $this->generateStockMovement(0, $this->quantity, $this->reason, $this->cost); |
|
77 | - } |
|
71 | + * Generates a stock movement on the creation of a stock. |
|
72 | + */ |
|
73 | + public function postCreate() |
|
74 | + { |
|
75 | + if (!$this->getLastMovement()) { |
|
76 | + $this->generateStockMovement(0, $this->quantity, $this->reason, $this->cost); |
|
77 | + } |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
81 | - * Generates a stock movement after a stock is updated. |
|
82 | - */ |
|
83 | - public function postUpdate() |
|
84 | - { |
|
85 | - $this->generateStockMovement($this->beforeQuantity, $this->quantity, $this->reason, $this->cost); |
|
81 | + * Generates a stock movement after a stock is updated. |
|
82 | + */ |
|
83 | + public function postUpdate() |
|
84 | + { |
|
85 | + $this->generateStockMovement($this->beforeQuantity, $this->quantity, $this->reason, $this->cost); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
89 | - * Performs a quantity update. Automatically determining |
|
90 | - * depending on the quantity entered if stock is being taken |
|
91 | - * or added. |
|
92 | - * |
|
93 | - * @param int|float|string $quantity |
|
94 | - * @param string $reason |
|
95 | - * @param int|float|string $cost |
|
96 | - * |
|
97 | - * @throws InvalidQuantityException |
|
98 | - * |
|
99 | - * @return $this |
|
100 | - */ |
|
101 | - public function updateQuantity($quantity, $reason = '', $cost = 0) |
|
102 | - { |
|
103 | - if ($this->isValidQuantity($quantity)) { |
|
104 | - return $this->processUpdateQuantityOperation($quantity, $reason, $cost); |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Removes the specified quantity from the current stock. |
|
110 | - * |
|
111 | - * @param int|float|string $quantity |
|
112 | - * @param string $reason |
|
113 | - * @param int|float|string $cost |
|
114 | - * |
|
115 | - * @return $this|bool |
|
116 | - */ |
|
117 | - public function remove($quantity, $reason = '', $cost = 0) |
|
118 | - { |
|
119 | - return $this->take($quantity, $reason, $cost); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Processes a 'take' operation on the current stock. |
|
124 | - * |
|
125 | - * @param int|float|string $quantity |
|
126 | - * @param string $reason |
|
127 | - * @param int|float|string $cost |
|
128 | - * |
|
129 | - * @throws InvalidQuantityException |
|
130 | - * @throws NotEnoughStockException |
|
131 | - * |
|
132 | - * @return $this|bool |
|
133 | - */ |
|
134 | - public function take($quantity, $reason = '', $cost = 0) |
|
135 | - { |
|
136 | - if ($this->isValidQuantity($quantity) && $this->hasEnoughStock($quantity)) { |
|
137 | - return $this->processTakeOperation($quantity, $reason, $cost); |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Alias for put function. |
|
143 | - * |
|
144 | - * @param int|float|string $quantity |
|
145 | - * @param string $reason |
|
146 | - * @param int|float|string $cost |
|
147 | - * |
|
148 | - * @return $this |
|
149 | - */ |
|
150 | - public function add($quantity, $reason = '', $cost = 0) |
|
151 | - { |
|
152 | - return $this->put($quantity, $reason, $cost); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Processes a 'put' operation on the current stock. |
|
157 | - * |
|
158 | - * @param int|float|string $quantity |
|
159 | - * @param string $reason |
|
160 | - * @param int|float|string $cost |
|
161 | - * |
|
162 | - * @throws InvalidQuantityException |
|
163 | - * |
|
164 | - * @return $this |
|
165 | - */ |
|
166 | - public function put($quantity, $reason = '', $cost = 0) |
|
167 | - { |
|
168 | - if ($this->isValidQuantity($quantity)) { |
|
169 | - return $this->processPutOperation($quantity, $reason, $cost); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Moves a stock to the specified location. |
|
175 | - * |
|
176 | - * @param $location |
|
177 | - * |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - public function moveTo($location) |
|
181 | - { |
|
182 | - $location = $this->getLocation($location); |
|
183 | - |
|
184 | - return $this->processMoveOperation($location); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Rolls back the last movement, or the movement specified. If recursive is set to true, |
|
189 | - * it will rollback all movements leading up to the movement specified. |
|
190 | - * |
|
191 | - * @param mixed $movement |
|
192 | - * @param bool $recursive |
|
193 | - * |
|
194 | - * @return $this|bool |
|
195 | - */ |
|
196 | - public function rollback($movement = null, $recursive = false) |
|
197 | - { |
|
198 | - if ($movement) { |
|
199 | - return $this->rollbackMovement($movement, $recursive); |
|
200 | - } else { |
|
201 | - $movement = $this->getLastMovement(); |
|
202 | - |
|
203 | - if ($movement) { |
|
204 | - return $this->processRollbackOperation($movement, $recursive); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Rolls back a specific movement. |
|
213 | - * |
|
214 | - * @param mixed $movement |
|
215 | - * @param bool $recursive |
|
216 | - * |
|
217 | - * @throws InvalidMovementException |
|
218 | - * |
|
219 | - * @return $this|bool |
|
220 | - */ |
|
221 | - public function rollbackMovement($movement, $recursive = false) |
|
222 | - { |
|
223 | - $movement = $this->getMovement($movement); |
|
224 | - |
|
225 | - return $this->processRollbackOperation($movement, $recursive); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns true if there is enough stock for the specified quantity being taken. |
|
230 | - * Throws NotEnoughStockException otherwise. |
|
231 | - * |
|
232 | - * @param int|float|string $quantity |
|
233 | - * |
|
234 | - * @throws NotEnoughStockException |
|
235 | - * |
|
236 | - * @return bool |
|
237 | - */ |
|
238 | - public function hasEnoughStock($quantity = 0) |
|
239 | - { |
|
240 | - /* |
|
89 | + * Performs a quantity update. Automatically determining |
|
90 | + * depending on the quantity entered if stock is being taken |
|
91 | + * or added. |
|
92 | + * |
|
93 | + * @param int|float|string $quantity |
|
94 | + * @param string $reason |
|
95 | + * @param int|float|string $cost |
|
96 | + * |
|
97 | + * @throws InvalidQuantityException |
|
98 | + * |
|
99 | + * @return $this |
|
100 | + */ |
|
101 | + public function updateQuantity($quantity, $reason = '', $cost = 0) |
|
102 | + { |
|
103 | + if ($this->isValidQuantity($quantity)) { |
|
104 | + return $this->processUpdateQuantityOperation($quantity, $reason, $cost); |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Removes the specified quantity from the current stock. |
|
110 | + * |
|
111 | + * @param int|float|string $quantity |
|
112 | + * @param string $reason |
|
113 | + * @param int|float|string $cost |
|
114 | + * |
|
115 | + * @return $this|bool |
|
116 | + */ |
|
117 | + public function remove($quantity, $reason = '', $cost = 0) |
|
118 | + { |
|
119 | + return $this->take($quantity, $reason, $cost); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Processes a 'take' operation on the current stock. |
|
124 | + * |
|
125 | + * @param int|float|string $quantity |
|
126 | + * @param string $reason |
|
127 | + * @param int|float|string $cost |
|
128 | + * |
|
129 | + * @throws InvalidQuantityException |
|
130 | + * @throws NotEnoughStockException |
|
131 | + * |
|
132 | + * @return $this|bool |
|
133 | + */ |
|
134 | + public function take($quantity, $reason = '', $cost = 0) |
|
135 | + { |
|
136 | + if ($this->isValidQuantity($quantity) && $this->hasEnoughStock($quantity)) { |
|
137 | + return $this->processTakeOperation($quantity, $reason, $cost); |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Alias for put function. |
|
143 | + * |
|
144 | + * @param int|float|string $quantity |
|
145 | + * @param string $reason |
|
146 | + * @param int|float|string $cost |
|
147 | + * |
|
148 | + * @return $this |
|
149 | + */ |
|
150 | + public function add($quantity, $reason = '', $cost = 0) |
|
151 | + { |
|
152 | + return $this->put($quantity, $reason, $cost); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Processes a 'put' operation on the current stock. |
|
157 | + * |
|
158 | + * @param int|float|string $quantity |
|
159 | + * @param string $reason |
|
160 | + * @param int|float|string $cost |
|
161 | + * |
|
162 | + * @throws InvalidQuantityException |
|
163 | + * |
|
164 | + * @return $this |
|
165 | + */ |
|
166 | + public function put($quantity, $reason = '', $cost = 0) |
|
167 | + { |
|
168 | + if ($this->isValidQuantity($quantity)) { |
|
169 | + return $this->processPutOperation($quantity, $reason, $cost); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Moves a stock to the specified location. |
|
175 | + * |
|
176 | + * @param $location |
|
177 | + * |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + public function moveTo($location) |
|
181 | + { |
|
182 | + $location = $this->getLocation($location); |
|
183 | + |
|
184 | + return $this->processMoveOperation($location); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Rolls back the last movement, or the movement specified. If recursive is set to true, |
|
189 | + * it will rollback all movements leading up to the movement specified. |
|
190 | + * |
|
191 | + * @param mixed $movement |
|
192 | + * @param bool $recursive |
|
193 | + * |
|
194 | + * @return $this|bool |
|
195 | + */ |
|
196 | + public function rollback($movement = null, $recursive = false) |
|
197 | + { |
|
198 | + if ($movement) { |
|
199 | + return $this->rollbackMovement($movement, $recursive); |
|
200 | + } else { |
|
201 | + $movement = $this->getLastMovement(); |
|
202 | + |
|
203 | + if ($movement) { |
|
204 | + return $this->processRollbackOperation($movement, $recursive); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Rolls back a specific movement. |
|
213 | + * |
|
214 | + * @param mixed $movement |
|
215 | + * @param bool $recursive |
|
216 | + * |
|
217 | + * @throws InvalidMovementException |
|
218 | + * |
|
219 | + * @return $this|bool |
|
220 | + */ |
|
221 | + public function rollbackMovement($movement, $recursive = false) |
|
222 | + { |
|
223 | + $movement = $this->getMovement($movement); |
|
224 | + |
|
225 | + return $this->processRollbackOperation($movement, $recursive); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns true if there is enough stock for the specified quantity being taken. |
|
230 | + * Throws NotEnoughStockException otherwise. |
|
231 | + * |
|
232 | + * @param int|float|string $quantity |
|
233 | + * |
|
234 | + * @throws NotEnoughStockException |
|
235 | + * |
|
236 | + * @return bool |
|
237 | + */ |
|
238 | + public function hasEnoughStock($quantity = 0) |
|
239 | + { |
|
240 | + /* |
|
241 | 241 | * Using double equals for validation of complete value only, not variable type. For example: |
242 | 242 | * '20' (string) equals 20 (int) |
243 | 243 | */ |
244 | - if ($this->quantity == $quantity || $this->quantity > $quantity) { |
|
245 | - return true; |
|
246 | - } |
|
247 | - |
|
248 | - $message = 'Not enough stock. Tried to take '. $quantity.' but only '. $this->quantity .' is available'; |
|
249 | - |
|
250 | - throw new NotEnoughStockException($message); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Returns the last movement on the current stock record. |
|
255 | - * |
|
256 | - * @return mixed |
|
257 | - */ |
|
258 | - public function getLastMovement() |
|
259 | - { |
|
260 | - $movement = $this->movements()->orderBy('created_at', 'DESC')->first(); |
|
261 | - |
|
262 | - if ($movement) { |
|
263 | - return $movement; |
|
264 | - } |
|
265 | - |
|
266 | - return false; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Returns a movement depending on the specified argument. If an object is supplied, it is checked if it |
|
271 | - * is an instance of an eloquent model. If a numeric value is entered, it is retrieved by it's ID. |
|
272 | - * |
|
273 | - * @param mixed $movement |
|
274 | - * |
|
275 | - * @throws InvalidMovementException |
|
276 | - * |
|
277 | - * @return mixed |
|
278 | - */ |
|
279 | - public function getMovement($movement) |
|
280 | - { |
|
281 | - if ($this->isModel($movement)) { |
|
282 | - return $movement; |
|
283 | - } elseif (is_numeric($movement)) { |
|
284 | - return $this->getMovementById($movement); |
|
285 | - } else { |
|
286 | - $message = 'Movement '. $movement .' is invalid'; |
|
287 | - |
|
288 | - throw new InvalidMovementException($message); |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Creates and returns a new un-saved stock transaction |
|
294 | - * instance with the current stock ID attached. |
|
295 | - * |
|
296 | - * @param string $name |
|
297 | - * |
|
298 | - * @return \Illuminate\Database\Eloquent\Model |
|
299 | - */ |
|
300 | - public function newTransaction($name = '') |
|
301 | - { |
|
302 | - $transaction = $this->transactions()->getRelated(); |
|
303 | - |
|
304 | - /* |
|
244 | + if ($this->quantity == $quantity || $this->quantity > $quantity) { |
|
245 | + return true; |
|
246 | + } |
|
247 | + |
|
248 | + $message = 'Not enough stock. Tried to take '. $quantity.' but only '. $this->quantity .' is available'; |
|
249 | + |
|
250 | + throw new NotEnoughStockException($message); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Returns the last movement on the current stock record. |
|
255 | + * |
|
256 | + * @return mixed |
|
257 | + */ |
|
258 | + public function getLastMovement() |
|
259 | + { |
|
260 | + $movement = $this->movements()->orderBy('created_at', 'DESC')->first(); |
|
261 | + |
|
262 | + if ($movement) { |
|
263 | + return $movement; |
|
264 | + } |
|
265 | + |
|
266 | + return false; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Returns a movement depending on the specified argument. If an object is supplied, it is checked if it |
|
271 | + * is an instance of an eloquent model. If a numeric value is entered, it is retrieved by it's ID. |
|
272 | + * |
|
273 | + * @param mixed $movement |
|
274 | + * |
|
275 | + * @throws InvalidMovementException |
|
276 | + * |
|
277 | + * @return mixed |
|
278 | + */ |
|
279 | + public function getMovement($movement) |
|
280 | + { |
|
281 | + if ($this->isModel($movement)) { |
|
282 | + return $movement; |
|
283 | + } elseif (is_numeric($movement)) { |
|
284 | + return $this->getMovementById($movement); |
|
285 | + } else { |
|
286 | + $message = 'Movement '. $movement .' is invalid'; |
|
287 | + |
|
288 | + throw new InvalidMovementException($message); |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Creates and returns a new un-saved stock transaction |
|
294 | + * instance with the current stock ID attached. |
|
295 | + * |
|
296 | + * @param string $name |
|
297 | + * |
|
298 | + * @return \Illuminate\Database\Eloquent\Model |
|
299 | + */ |
|
300 | + public function newTransaction($name = '') |
|
301 | + { |
|
302 | + $transaction = $this->transactions()->getRelated(); |
|
303 | + |
|
304 | + /* |
|
305 | 305 | * Set the transaction attributes so they don't |
306 | 306 | * need to be set manually |
307 | 307 | */ |
308 | - $transaction->stock_id = $this->getKey(); |
|
309 | - $transaction->name = $name; |
|
310 | - |
|
311 | - return $transaction; |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Retrieves a movement by the specified ID. |
|
316 | - * |
|
317 | - * @param int|string $id |
|
318 | - * |
|
319 | - * @return mixed |
|
320 | - */ |
|
321 | - private function getMovementById($id) |
|
322 | - { |
|
323 | - return $this->movements()->find($id); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Processes a quantity update operation. |
|
328 | - * |
|
329 | - * @param int|float|string $quantity |
|
330 | - * @param string $reason |
|
331 | - * @param int|float|string $cost |
|
332 | - * |
|
333 | - * @return $this |
|
334 | - */ |
|
335 | - private function processUpdateQuantityOperation($quantity, $reason = '', $cost = 0) |
|
336 | - { |
|
337 | - if ($quantity > $this->quantity) { |
|
338 | - $putting = $quantity - $this->quantity; |
|
339 | - |
|
340 | - return $this->put($putting, $reason, $cost); |
|
341 | - } else { |
|
342 | - $taking = $this->quantity - $quantity; |
|
343 | - |
|
344 | - return $this->take($taking, $reason, $cost); |
|
345 | - } |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Processes removing quantity from the current stock. |
|
350 | - * |
|
351 | - * @param int|float|string $taking |
|
352 | - * @param string $reason |
|
353 | - * @param int|float|string $cost |
|
354 | - * |
|
355 | - * @return $this|bool |
|
356 | - */ |
|
357 | - private function processTakeOperation($taking, $reason = '', $cost = 0) |
|
358 | - { |
|
359 | - $left = $this->quantity - $taking; |
|
360 | - |
|
361 | - /* |
|
308 | + $transaction->stock_id = $this->getKey(); |
|
309 | + $transaction->name = $name; |
|
310 | + |
|
311 | + return $transaction; |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Retrieves a movement by the specified ID. |
|
316 | + * |
|
317 | + * @param int|string $id |
|
318 | + * |
|
319 | + * @return mixed |
|
320 | + */ |
|
321 | + private function getMovementById($id) |
|
322 | + { |
|
323 | + return $this->movements()->find($id); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Processes a quantity update operation. |
|
328 | + * |
|
329 | + * @param int|float|string $quantity |
|
330 | + * @param string $reason |
|
331 | + * @param int|float|string $cost |
|
332 | + * |
|
333 | + * @return $this |
|
334 | + */ |
|
335 | + private function processUpdateQuantityOperation($quantity, $reason = '', $cost = 0) |
|
336 | + { |
|
337 | + if ($quantity > $this->quantity) { |
|
338 | + $putting = $quantity - $this->quantity; |
|
339 | + |
|
340 | + return $this->put($putting, $reason, $cost); |
|
341 | + } else { |
|
342 | + $taking = $this->quantity - $quantity; |
|
343 | + |
|
344 | + return $this->take($taking, $reason, $cost); |
|
345 | + } |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Processes removing quantity from the current stock. |
|
350 | + * |
|
351 | + * @param int|float|string $taking |
|
352 | + * @param string $reason |
|
353 | + * @param int|float|string $cost |
|
354 | + * |
|
355 | + * @return $this|bool |
|
356 | + */ |
|
357 | + private function processTakeOperation($taking, $reason = '', $cost = 0) |
|
358 | + { |
|
359 | + $left = $this->quantity - $taking; |
|
360 | + |
|
361 | + /* |
|
362 | 362 | * If the updated total and the beginning total are the same, we'll check if |
363 | 363 | * duplicate movements are allowed. We'll return the current record if |
364 | 364 | * they aren't. |
365 | 365 | */ |
366 | - if ($left == $this->quantity && !$this->allowDuplicateMovementsEnabled()) { |
|
367 | - return $this; |
|
368 | - } |
|
366 | + if ($left == $this->quantity && !$this->allowDuplicateMovementsEnabled()) { |
|
367 | + return $this; |
|
368 | + } |
|
369 | 369 | |
370 | - $this->quantity = $left; |
|
370 | + $this->quantity = $left; |
|
371 | 371 | |
372 | - $this->setReason($reason); |
|
372 | + $this->setReason($reason); |
|
373 | 373 | |
374 | - $this->setCost($cost); |
|
374 | + $this->setCost($cost); |
|
375 | 375 | |
376 | - DB::beginTransaction(); |
|
376 | + DB::beginTransaction(); |
|
377 | 377 | |
378 | - try { |
|
379 | - if ($this->save()) { |
|
378 | + try { |
|
379 | + if ($this->save()) { |
|
380 | 380 | |
381 | - $this->fireModelEvent('inventory.stock.taken', [ |
|
382 | - 'stock' => $this, |
|
383 | - ]); |
|
381 | + $this->fireModelEvent('inventory.stock.taken', [ |
|
382 | + 'stock' => $this, |
|
383 | + ]); |
|
384 | 384 | |
385 | - return $this; |
|
386 | - } |
|
387 | - } catch (\Exception $e) { |
|
388 | - DB::rollBack(); |
|
389 | - } |
|
385 | + return $this; |
|
386 | + } |
|
387 | + } catch (\Exception $e) { |
|
388 | + DB::rollBack(); |
|
389 | + } |
|
390 | 390 | |
391 | - return false; |
|
392 | - } |
|
391 | + return false; |
|
392 | + } |
|
393 | 393 | |
394 | - /** |
|
395 | - * Processes adding quantity to current stock. |
|
396 | - * |
|
397 | - * @param int|float|string $putting |
|
398 | - * @param string $reason |
|
399 | - * @param int|float|string $cost |
|
400 | - * |
|
401 | - * @return $this|bool |
|
402 | - */ |
|
403 | - private function processPutOperation($putting, $reason = '', $cost = 0) |
|
404 | - { |
|
405 | - $before = $this->quantity; |
|
394 | + /** |
|
395 | + * Processes adding quantity to current stock. |
|
396 | + * |
|
397 | + * @param int|float|string $putting |
|
398 | + * @param string $reason |
|
399 | + * @param int|float|string $cost |
|
400 | + * |
|
401 | + * @return $this|bool |
|
402 | + */ |
|
403 | + private function processPutOperation($putting, $reason = '', $cost = 0) |
|
404 | + { |
|
405 | + $before = $this->quantity; |
|
406 | 406 | |
407 | - $total = $putting + $before; |
|
407 | + $total = $putting + $before; |
|
408 | 408 | |
409 | - /* |
|
409 | + /* |
|
410 | 410 | * If the updated total and the beginning total are the same, |
411 | 411 | * we'll check if duplicate movements are allowed |
412 | 412 | */ |
413 | - if ($total == $this->quantity && !$this->allowDuplicateMovementsEnabled()) { |
|
414 | - return $this; |
|
415 | - } |
|
413 | + if ($total == $this->quantity && !$this->allowDuplicateMovementsEnabled()) { |
|
414 | + return $this; |
|
415 | + } |
|
416 | 416 | |
417 | - $this->quantity = $total; |
|
417 | + $this->quantity = $total; |
|
418 | 418 | |
419 | - $this->setReason($reason); |
|
419 | + $this->setReason($reason); |
|
420 | 420 | |
421 | - $this->setCost($cost); |
|
421 | + $this->setCost($cost); |
|
422 | 422 | |
423 | - DB::beginTransaction(); |
|
423 | + DB::beginTransaction(); |
|
424 | 424 | |
425 | - try { |
|
426 | - if ($this->save()) { |
|
427 | - DB::commit(); |
|
425 | + try { |
|
426 | + if ($this->save()) { |
|
427 | + DB::commit(); |
|
428 | 428 | |
429 | - $this->fireModelEvent('inventory.stock.added', [ |
|
430 | - 'stock' => $this, |
|
431 | - ]); |
|
429 | + $this->fireModelEvent('inventory.stock.added', [ |
|
430 | + 'stock' => $this, |
|
431 | + ]); |
|
432 | 432 | |
433 | - return $this; |
|
434 | - } |
|
435 | - } catch (\Exception $e) { |
|
436 | - DB::rollBack(); |
|
437 | - } |
|
433 | + return $this; |
|
434 | + } |
|
435 | + } catch (\Exception $e) { |
|
436 | + DB::rollBack(); |
|
437 | + } |
|
438 | 438 | |
439 | - return false; |
|
440 | - } |
|
439 | + return false; |
|
440 | + } |
|
441 | 441 | |
442 | - /** |
|
443 | - * Processes the stock moving from it's current |
|
444 | - * location, to the specified location. |
|
445 | - * |
|
446 | - * @param mixed $location |
|
447 | - * |
|
448 | - * @return bool |
|
449 | - */ |
|
450 | - private function processMoveOperation(Model $location) |
|
451 | - { |
|
452 | - $this->location_id = $location->getKey(); |
|
442 | + /** |
|
443 | + * Processes the stock moving from it's current |
|
444 | + * location, to the specified location. |
|
445 | + * |
|
446 | + * @param mixed $location |
|
447 | + * |
|
448 | + * @return bool |
|
449 | + */ |
|
450 | + private function processMoveOperation(Model $location) |
|
451 | + { |
|
452 | + $this->location_id = $location->getKey(); |
|
453 | 453 | |
454 | - DB::beginTransaction(); |
|
454 | + DB::beginTransaction(); |
|
455 | 455 | |
456 | - try { |
|
457 | - if ($this->save()) { |
|
456 | + try { |
|
457 | + if ($this->save()) { |
|
458 | 458 | |
459 | 459 | |
460 | - $this->fireModelEvent('inventory.stock.moved', [ |
|
461 | - 'stock' => $this, |
|
462 | - ]); |
|
460 | + $this->fireModelEvent('inventory.stock.moved', [ |
|
461 | + 'stock' => $this, |
|
462 | + ]); |
|
463 | 463 | |
464 | 464 | DB::commit(); |
465 | - return $this; |
|
466 | - } |
|
467 | - } catch (\Exception $e) { |
|
468 | - DB::rollBack(); |
|
469 | - } |
|
470 | - |
|
471 | - return false; |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * Processes a single rollback operation. |
|
476 | - * |
|
477 | - * @param mixed $movement |
|
478 | - * @param bool $recursive |
|
479 | - * |
|
480 | - * @return $this|bool |
|
481 | - */ |
|
482 | - private function processRollbackOperation(Model $movement, $recursive = false) |
|
483 | - { |
|
484 | - if ($recursive) { |
|
485 | - return $this->processRecursiveRollbackOperation($movement); |
|
486 | - } |
|
487 | - |
|
488 | - $this->quantity = $movement->before; |
|
489 | - |
|
490 | - $reason = 'Rolled back to movement ID: '. $movement->getOriginal('id') .' on '. $movement->getOriginal('created_at'); |
|
491 | - |
|
492 | - $this->setReason($reason); |
|
493 | - |
|
494 | - if ($this->rollbackCostEnabled()) { |
|
495 | - $this->setCost($movement->cost); |
|
496 | - |
|
497 | - $this->reverseCost(); |
|
498 | - } |
|
499 | - |
|
500 | - DB::beginTransaction(); |
|
501 | - |
|
502 | - try { |
|
503 | - if ($this->save()) { |
|
504 | - DB::commit(); |
|
505 | - |
|
506 | - $this->fireModelEvent('inventory.stock.rollback', [ |
|
507 | - 'stock' => $this, |
|
508 | - ]); |
|
509 | - |
|
510 | - return $this; |
|
511 | - } |
|
512 | - } catch (\Exception $e) { |
|
513 | - DB::rollBack(); |
|
514 | - } |
|
515 | - |
|
516 | - return false; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * Processes a recursive rollback operation. |
|
521 | - * |
|
522 | - * @param mixed $movement |
|
523 | - * |
|
524 | - * @return array |
|
525 | - */ |
|
526 | - private function processRecursiveRollbackOperation(Model $movement) |
|
527 | - { |
|
528 | - /* |
|
465 | + return $this; |
|
466 | + } |
|
467 | + } catch (\Exception $e) { |
|
468 | + DB::rollBack(); |
|
469 | + } |
|
470 | + |
|
471 | + return false; |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * Processes a single rollback operation. |
|
476 | + * |
|
477 | + * @param mixed $movement |
|
478 | + * @param bool $recursive |
|
479 | + * |
|
480 | + * @return $this|bool |
|
481 | + */ |
|
482 | + private function processRollbackOperation(Model $movement, $recursive = false) |
|
483 | + { |
|
484 | + if ($recursive) { |
|
485 | + return $this->processRecursiveRollbackOperation($movement); |
|
486 | + } |
|
487 | + |
|
488 | + $this->quantity = $movement->before; |
|
489 | + |
|
490 | + $reason = 'Rolled back to movement ID: '. $movement->getOriginal('id') .' on '. $movement->getOriginal('created_at'); |
|
491 | + |
|
492 | + $this->setReason($reason); |
|
493 | + |
|
494 | + if ($this->rollbackCostEnabled()) { |
|
495 | + $this->setCost($movement->cost); |
|
496 | + |
|
497 | + $this->reverseCost(); |
|
498 | + } |
|
499 | + |
|
500 | + DB::beginTransaction(); |
|
501 | + |
|
502 | + try { |
|
503 | + if ($this->save()) { |
|
504 | + DB::commit(); |
|
505 | + |
|
506 | + $this->fireModelEvent('inventory.stock.rollback', [ |
|
507 | + 'stock' => $this, |
|
508 | + ]); |
|
509 | + |
|
510 | + return $this; |
|
511 | + } |
|
512 | + } catch (\Exception $e) { |
|
513 | + DB::rollBack(); |
|
514 | + } |
|
515 | + |
|
516 | + return false; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * Processes a recursive rollback operation. |
|
521 | + * |
|
522 | + * @param mixed $movement |
|
523 | + * |
|
524 | + * @return array |
|
525 | + */ |
|
526 | + private function processRecursiveRollbackOperation(Model $movement) |
|
527 | + { |
|
528 | + /* |
|
529 | 529 | * Retrieve movements that were created after |
530 | 530 | * the specified movement, and order them descending |
531 | 531 | */ |
532 | - $movements = $this |
|
533 | - ->movements() |
|
534 | - ->where('created_at', '>=', $movement->getOriginal('created_at')) |
|
535 | - ->orderBy('created_at', 'DESC') |
|
536 | - ->get(); |
|
537 | - |
|
538 | - $rollbacks = []; |
|
539 | - |
|
540 | - if ($movements->count() > 0) { |
|
541 | - foreach ($movements as $movement) { |
|
542 | - $rollbacks = $this->processRollbackOperation($movement); |
|
543 | - } |
|
544 | - } |
|
545 | - |
|
546 | - return $rollbacks; |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Creates a new stock movement record. |
|
551 | - * |
|
552 | - * @param int|float|string $before |
|
553 | - * @param int|float|string $after |
|
554 | - * @param string $reason |
|
555 | - * @param int|float|string $cost |
|
556 | - * |
|
557 | - * @return \Illuminate\Database\Eloquent\Model |
|
558 | - */ |
|
559 | - private function generateStockMovement($before, $after, $reason = '', $cost = 0) |
|
560 | - { |
|
561 | - $insert = [ |
|
562 | - 'stock_id' => $this->getKey(), |
|
563 | - 'before' => $before, |
|
564 | - 'after' => $after, |
|
565 | - 'reason' => $reason, |
|
566 | - 'cost' => $cost, |
|
567 | - ]; |
|
568 | - |
|
569 | - return $this->movements()->create($insert); |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * Sets the cost attribute. |
|
574 | - * |
|
575 | - * @param int|float|string $cost |
|
576 | - */ |
|
577 | - private function setCost($cost = 0) |
|
578 | - { |
|
579 | - $this->cost = $cost; |
|
580 | - } |
|
581 | - |
|
582 | - /** |
|
583 | - * Reverses the cost of a movement. |
|
584 | - */ |
|
585 | - private function reverseCost() |
|
586 | - { |
|
587 | - if ($this->isPositive($this->cost)) { |
|
588 | - $this->setCost(-abs($this->cost)); |
|
589 | - } else { |
|
590 | - $this->setCost(abs($this->cost)); |
|
591 | - } |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * Sets the reason attribute. |
|
596 | - * |
|
597 | - * @param string $reason |
|
598 | - */ |
|
599 | - private function setReason($reason = '') |
|
600 | - { |
|
601 | - $this->reason = $reason; |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
605 | - * Returns true/false from the configuration file determining |
|
606 | - * whether or not stock movements can have the same before and after |
|
607 | - * quantities. |
|
608 | - * |
|
609 | - * @return bool |
|
610 | - */ |
|
611 | - private function allowDuplicateMovementsEnabled() |
|
612 | - { |
|
613 | - return false; |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Returns true/false from the configuration file determining |
|
618 | - * whether or not to rollback costs when a rollback occurs on |
|
619 | - * a stock. |
|
620 | - * |
|
621 | - * @return bool |
|
622 | - */ |
|
623 | - private function rollbackCostEnabled() |
|
624 | - { |
|
625 | - return true; |
|
626 | - } |
|
532 | + $movements = $this |
|
533 | + ->movements() |
|
534 | + ->where('created_at', '>=', $movement->getOriginal('created_at')) |
|
535 | + ->orderBy('created_at', 'DESC') |
|
536 | + ->get(); |
|
537 | + |
|
538 | + $rollbacks = []; |
|
539 | + |
|
540 | + if ($movements->count() > 0) { |
|
541 | + foreach ($movements as $movement) { |
|
542 | + $rollbacks = $this->processRollbackOperation($movement); |
|
543 | + } |
|
544 | + } |
|
545 | + |
|
546 | + return $rollbacks; |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Creates a new stock movement record. |
|
551 | + * |
|
552 | + * @param int|float|string $before |
|
553 | + * @param int|float|string $after |
|
554 | + * @param string $reason |
|
555 | + * @param int|float|string $cost |
|
556 | + * |
|
557 | + * @return \Illuminate\Database\Eloquent\Model |
|
558 | + */ |
|
559 | + private function generateStockMovement($before, $after, $reason = '', $cost = 0) |
|
560 | + { |
|
561 | + $insert = [ |
|
562 | + 'stock_id' => $this->getKey(), |
|
563 | + 'before' => $before, |
|
564 | + 'after' => $after, |
|
565 | + 'reason' => $reason, |
|
566 | + 'cost' => $cost, |
|
567 | + ]; |
|
568 | + |
|
569 | + return $this->movements()->create($insert); |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * Sets the cost attribute. |
|
574 | + * |
|
575 | + * @param int|float|string $cost |
|
576 | + */ |
|
577 | + private function setCost($cost = 0) |
|
578 | + { |
|
579 | + $this->cost = $cost; |
|
580 | + } |
|
581 | + |
|
582 | + /** |
|
583 | + * Reverses the cost of a movement. |
|
584 | + */ |
|
585 | + private function reverseCost() |
|
586 | + { |
|
587 | + if ($this->isPositive($this->cost)) { |
|
588 | + $this->setCost(-abs($this->cost)); |
|
589 | + } else { |
|
590 | + $this->setCost(abs($this->cost)); |
|
591 | + } |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * Sets the reason attribute. |
|
596 | + * |
|
597 | + * @param string $reason |
|
598 | + */ |
|
599 | + private function setReason($reason = '') |
|
600 | + { |
|
601 | + $this->reason = $reason; |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | + * Returns true/false from the configuration file determining |
|
606 | + * whether or not stock movements can have the same before and after |
|
607 | + * quantities. |
|
608 | + * |
|
609 | + * @return bool |
|
610 | + */ |
|
611 | + private function allowDuplicateMovementsEnabled() |
|
612 | + { |
|
613 | + return false; |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Returns true/false from the configuration file determining |
|
618 | + * whether or not to rollback costs when a rollback occurs on |
|
619 | + * a stock. |
|
620 | + * |
|
621 | + * @return bool |
|
622 | + */ |
|
623 | + private function rollbackCostEnabled() |
|
624 | + { |
|
625 | + return true; |
|
626 | + } |
|
627 | 627 | |
628 | 628 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | |
33 | 33 | protected static function bootHasItemStocks() |
34 | 34 | { |
35 | - static::creating(function (Model $model) { |
|
35 | + static::creating(function(Model $model) { |
|
36 | 36 | |
37 | 37 | /* |
38 | 38 | * Check if a reason has been set, if not |
@@ -43,11 +43,11 @@ discard block |
||
43 | 43 | } |
44 | 44 | }); |
45 | 45 | |
46 | - static::created(function (Model $model) { |
|
46 | + static::created(function(Model $model) { |
|
47 | 47 | $model->postCreate(); |
48 | 48 | }); |
49 | 49 | |
50 | - static::updating(function (Model $model) { |
|
50 | + static::updating(function(Model $model) { |
|
51 | 51 | /* |
52 | 52 | * Retrieve the original quantity before it was updated, |
53 | 53 | * so we can create generate an update with it |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | } |
63 | 63 | }); |
64 | 64 | |
65 | - static::updated(function (Model $model) { |
|
65 | + static::updated(function(Model $model) { |
|
66 | 66 | $model->postUpdate(); |
67 | 67 | }); |
68 | 68 | } |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | return true; |
246 | 246 | } |
247 | 247 | |
248 | - $message = 'Not enough stock. Tried to take '. $quantity.' but only '. $this->quantity .' is available'; |
|
248 | + $message = 'Not enough stock. Tried to take '.$quantity.' but only '.$this->quantity.' is available'; |
|
249 | 249 | |
250 | 250 | throw new NotEnoughStockException($message); |
251 | 251 | } |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | } elseif (is_numeric($movement)) { |
284 | 284 | return $this->getMovementById($movement); |
285 | 285 | } else { |
286 | - $message = 'Movement '. $movement .' is invalid'; |
|
286 | + $message = 'Movement '.$movement.' is invalid'; |
|
287 | 287 | |
288 | 288 | throw new InvalidMovementException($message); |
289 | 289 | } |
@@ -487,7 +487,7 @@ discard block |
||
487 | 487 | |
488 | 488 | $this->quantity = $movement->before; |
489 | 489 | |
490 | - $reason = 'Rolled back to movement ID: '. $movement->getOriginal('id') .' on '. $movement->getOriginal('created_at'); |
|
490 | + $reason = 'Rolled back to movement ID: '.$movement->getOriginal('id').' on '.$movement->getOriginal('created_at'); |
|
491 | 491 | |
492 | 492 | $this->setReason($reason); |
493 | 493 |
@@ -16,7 +16,7 @@ |
||
16 | 16 | public function getWarehouses(): Collection |
17 | 17 | { |
18 | 18 | return collect($this->warehouses) |
19 | - ->map(function ($item) { |
|
19 | + ->map(function($item) { |
|
20 | 20 | return $item->warehouse; |
21 | 21 | }) |
22 | 22 | ->unique('id') |
@@ -15,12 +15,12 @@ |
||
15 | 15 | protected static function bootSluggable(): void |
16 | 16 | { |
17 | 17 | static::creating(function (Model $model) { |
18 | - $model->createUniqueSlug(); |
|
19 | - }); |
|
18 | + $model->createUniqueSlug(); |
|
19 | + }); |
|
20 | 20 | |
21 | - static::updating(function (Model $model) { |
|
22 | - $model->createUniqueSlug(); |
|
23 | - }); |
|
21 | + static::updating(function (Model $model) { |
|
22 | + $model->createUniqueSlug(); |
|
23 | + }); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | protected function createUniqueSlug(): void |
@@ -14,11 +14,11 @@ discard block |
||
14 | 14 | */ |
15 | 15 | protected static function bootSluggable(): void |
16 | 16 | { |
17 | - static::creating(function (Model $model) { |
|
17 | + static::creating(function(Model $model) { |
|
18 | 18 | $model->createUniqueSlug(); |
19 | 19 | }); |
20 | 20 | |
21 | - static::updating(function (Model $model) { |
|
21 | + static::updating(function(Model $model) { |
|
22 | 22 | $model->createUniqueSlug(); |
23 | 23 | }); |
24 | 24 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $slug = Str::slug($this->attributes[$this->sluggable]); |
40 | 40 | $i = 1; |
41 | 41 | |
42 | - while($this->isSlugExists($slug) || $slug === '') { |
|
42 | + while ($this->isSlugExists($slug) || $slug === '') { |
|
43 | 43 | $slug = $slug.'-'.$i++; |
44 | 44 | } |
45 | 45 |
@@ -7,26 +7,26 @@ |
||
7 | 7 | trait HasItemMovements |
8 | 8 | { |
9 | 9 | /** |
10 | - * Rolls back the current movement. |
|
11 | - * |
|
12 | - * @param bool $recursive |
|
13 | - * |
|
14 | - * @return mixed |
|
15 | - */ |
|
16 | - public function rollback($recursive = false) |
|
17 | - { |
|
18 | - $stock = $this->stock; |
|
10 | + * Rolls back the current movement. |
|
11 | + * |
|
12 | + * @param bool $recursive |
|
13 | + * |
|
14 | + * @return mixed |
|
15 | + */ |
|
16 | + public function rollback($recursive = false) |
|
17 | + { |
|
18 | + $stock = $this->stock; |
|
19 | 19 | |
20 | - return $stock->rollback($this, $recursive); |
|
20 | + return $stock->rollback($this, $recursive); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
24 | - * Stock relation |
|
25 | - * |
|
26 | - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
27 | - */ |
|
28 | - public function stock(): BelongsTo |
|
29 | - { |
|
30 | - return $this->belongsTo('Ronmrcdo\Inventory\Models\InventoryStock', 'stock_id', 'id'); |
|
31 | - } |
|
24 | + * Stock relation |
|
25 | + * |
|
26 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
27 | + */ |
|
28 | + public function stock(): BelongsTo |
|
29 | + { |
|
30 | + return $this->belongsTo('Ronmrcdo\Inventory\Models\InventoryStock', 'stock_id', 'id'); |
|
31 | + } |
|
32 | 32 | } |
33 | 33 | \ No newline at end of file |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | */ |
114 | 114 | public function hasAttributes(): bool |
115 | 115 | { |
116 | - return !! $this->attributes()->count(); |
|
116 | + return !!$this->attributes()->count(); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | { |
150 | 150 | $attribute = $this->attributes()->where('name', $option)->first(); |
151 | 151 | |
152 | - if (! $attribute) { |
|
152 | + if (!$attribute) { |
|
153 | 153 | throw new InvalidAttributeException("Invalid attribute", 422); |
154 | 154 | } |
155 | 155 |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | protected function getVariants(): array |
84 | 84 | { |
85 | - $variants = ProductVariant::where('product_id' , $this->id)->get(); |
|
85 | + $variants = ProductVariant::where('product_id', $this->id)->get(); |
|
86 | 86 | |
87 | 87 | return $this->transformVariant($variants); |
88 | 88 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | { |
99 | 99 | return collect($variant) |
100 | 100 | ->sortBy('option') |
101 | - ->map(function ($item) { |
|
101 | + ->map(function($item) { |
|
102 | 102 | return [ |
103 | 103 | 'option' => strtolower($item['option']), |
104 | 104 | 'value' => strtolower($item['value']) |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | protected function transformVariant($variants): array |
120 | 120 | { |
121 | 121 | return collect($variants) |
122 | - ->map(function ($item) { |
|
122 | + ->map(function($item) { |
|
123 | 123 | return [ |
124 | 124 | 'id' => $item->id, |
125 | 125 | 'sku' => $item->productSku->code, |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | }) |
130 | 130 | ->keyBy('id') |
131 | 131 | ->groupBy('sku') |
132 | - ->map(function ($item) { |
|
132 | + ->map(function($item) { |
|
133 | 133 | return collect($item) |
134 | - ->map(function ($var) { |
|
134 | + ->map(function($var) { |
|
135 | 135 | return [ |
136 | 136 | 'option' => strtolower($var['attribute']), |
137 | 137 | 'value' => strtolower($var['option']) |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | */ |
152 | 152 | public function hasSku(): bool |
153 | 153 | { |
154 | - return !! $this->skus()->count(); |
|
154 | + return !!$this->skus()->count(); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | */ |
175 | 175 | public function scopeWhereSku(Builder $query, string $sku): Builder |
176 | 176 | { |
177 | - return $query->whereHas('skus', function ($q) use ($sku) { |
|
177 | + return $query->whereHas('skus', function($q) use ($sku) { |
|
178 | 178 | $q->where('code', $sku); |
179 | 179 | }); |
180 | 180 | } |
@@ -31,7 +31,7 @@ |
||
31 | 31 | */ |
32 | 32 | public function hasProductBySku(string $sku): bool |
33 | 33 | { |
34 | - return $this->products()->whereHas('skus', function ($q) use ($sku) { |
|
34 | + return $this->products()->whereHas('skus', function($q) use ($sku) { |
|
35 | 35 | $q->where('code', $sku); |
36 | 36 | })->exists(); |
37 | 37 | } |
@@ -8,32 +8,32 @@ |
||
8 | 8 | |
9 | 9 | class ProductResource extends JsonResource |
10 | 10 | { |
11 | - /** |
|
12 | - * Transform the resource into an array. |
|
13 | - * |
|
14 | - * @param \Illuminate\Http\Request $request |
|
15 | - * @return array |
|
16 | - */ |
|
17 | - public function toArray($request) |
|
18 | - { |
|
19 | - return [ |
|
20 | - 'id' => $this->id, |
|
11 | + /** |
|
12 | + * Transform the resource into an array. |
|
13 | + * |
|
14 | + * @param \Illuminate\Http\Request $request |
|
15 | + * @return array |
|
16 | + */ |
|
17 | + public function toArray($request) |
|
18 | + { |
|
19 | + return [ |
|
20 | + 'id' => $this->id, |
|
21 | 21 | 'name' => $this->name, |
22 | - 'slug' => $this->slug, |
|
23 | - 'sku' => $this->hasSku() ? $this->skus()->first()->code : null, |
|
22 | + 'slug' => $this->slug, |
|
23 | + 'sku' => $this->hasSku() ? $this->skus()->first()->code : null, |
|
24 | 24 | 'short_description' => $this->short_description, |
25 | - 'description' => $this->description, |
|
26 | - 'price' => $this->hasSku() ? number_format($this->skus()->first()->price, 2, '.', '') : 0.00, |
|
27 | - 'cost' => $this->hasSku() ? number_format($this->skus()->first()->price, 2, '.', '') : 0.00, |
|
28 | - 'is_active' => $this->is_active, |
|
29 | - 'category' => [ |
|
30 | - 'id' => $this->category->id, |
|
31 | - 'name' => $this->category->name |
|
32 | - ], |
|
33 | - 'attributes' => AttributeResource::collection($this->attributes)->toArray(app('request')), |
|
34 | - 'variations' => $this->when($this->hasAttributes() && $this->hasSku(), |
|
35 | - VariantResource::collection($this->skus)->toArray(app('request')) |
|
36 | - ) |
|
37 | - ]; |
|
38 | - } |
|
25 | + 'description' => $this->description, |
|
26 | + 'price' => $this->hasSku() ? number_format($this->skus()->first()->price, 2, '.', '') : 0.00, |
|
27 | + 'cost' => $this->hasSku() ? number_format($this->skus()->first()->price, 2, '.', '') : 0.00, |
|
28 | + 'is_active' => $this->is_active, |
|
29 | + 'category' => [ |
|
30 | + 'id' => $this->category->id, |
|
31 | + 'name' => $this->category->name |
|
32 | + ], |
|
33 | + 'attributes' => AttributeResource::collection($this->attributes)->toArray(app('request')), |
|
34 | + 'variations' => $this->when($this->hasAttributes() && $this->hasSku(), |
|
35 | + VariantResource::collection($this->skus)->toArray(app('request')) |
|
36 | + ) |
|
37 | + ]; |
|
38 | + } |
|
39 | 39 | } |
40 | 40 | \ No newline at end of file |
@@ -1,15 +1,15 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | return [ |
4 | - /** |
|
5 | - * Base Product class |
|
6 | - * |
|
7 | - */ |
|
8 | - 'product' => \Ronmrcdo\Inventory\Models\Product::class, |
|
4 | + /** |
|
5 | + * Base Product class |
|
6 | + * |
|
7 | + */ |
|
8 | + 'product' => \Ronmrcdo\Inventory\Models\Product::class, |
|
9 | 9 | |
10 | - /** |
|
11 | - * Base Category Class |
|
12 | - * |
|
13 | - */ |
|
14 | - 'category' => \Ronmrcdo\Inventory\Models\Category::class |
|
10 | + /** |
|
11 | + * Base Category Class |
|
12 | + * |
|
13 | + */ |
|
14 | + 'category' => \Ronmrcdo\Inventory\Models\Category::class |
|
15 | 15 | ]; |
16 | 16 | \ No newline at end of file |