@@ -34,7 +34,7 @@ public function __construct()
34
34
public $ sortField = 'label ' ; // default sorting field
35
35
public $ sortAsc = true ; // default sort direction
36
36
37
- public $ LastDelivery = ' 1 ' ;
37
+ public $ LastDelivery = null ;
38
38
39
39
public $ DeliverysRequestsLineslist ;
40
40
public $ code , $ label , $ companies_id , $ companies_addresses_id , $ companies_contacts_id , $ user_id ;
@@ -71,16 +71,9 @@ public function sortBy($field)
71
71
public function mount ()
72
72
{
73
73
$ this ->user_id = Auth::id ();
74
- $ this ->LastDelivery = Deliverys::latest ()->first ();
75
- if ($ this ->LastDelivery == Null ){
76
- $ this ->code = "DN-0 " ;
77
- $ this ->label = "DN-0 " ;
78
- }
79
- else {
80
- $ this ->code = "DN- " . $ this ->LastDelivery ->id ;
81
- $ this ->label = "DN- " . $ this ->LastDelivery ->id ;
82
- }
83
-
74
+ $ this ->LastDelivery = Deliverys::latest ()->first ();
75
+ $ this ->code = $ this ->LastDelivery ? "DN- " . $ this ->LastDelivery ->id : "DN-0 " ;
76
+ $ this ->label = $ this ->code ;
84
77
}
85
78
86
79
public function render ()
@@ -129,120 +122,131 @@ function($query) {
129
122
]);
130
123
}
131
124
132
- public function storeDeliveryNote (){
133
- //check rules
134
- $ this ->validate ();
135
-
136
- //check if line exist
137
- $ i = 0 ;
138
-
125
+ public function storeDeliveryNote ()
126
+ {
127
+ // Validate the request
128
+ $ this ->validate ();
129
+
130
+ // Check if any order line exists
131
+ $ orderLineExists = $ this ->checkOrderLineExists ();
132
+
133
+ if ($ orderLineExists ) {
134
+ // Create delivery note
135
+ $ deliveryCreated = $ this ->createDeliveryNote ();
136
+
137
+ // Create delivery note lines
138
+ $ this ->createDeliveryNoteLines ($ deliveryCreated );
139
+
140
+ // Redirect to the newly created delivery note
141
+ return redirect ()->route ('deliverys.show ' , ['id ' => $ deliveryCreated ->id ])
142
+ ->with ('success ' , 'Successfully created new delivery note ' );
143
+ } else {
144
+ $ errors = $ this ->getErrorBag ();
145
+ $ errors ->add ('errors ' , 'No lines selected ' );
146
+ }
147
+ }
148
+
149
+ private function checkOrderLineExists ()
150
+ {
151
+ foreach ($ this ->data as $ item ) {
152
+ if ($ this ->isOrderLineValid ($ item )) {
153
+ return true ;
154
+ }
155
+ }
156
+ return false ;
157
+ }
158
+
159
+ private function createDeliveryNote ()
160
+ {
161
+ return Deliverys::create ([
162
+ 'uuid ' => Str::uuid (),
163
+ 'code ' => $ this ->code ,
164
+ 'label ' => $ this ->label ,
165
+ 'companies_id ' => $ this ->companies_id ,
166
+ 'companies_addresses_id ' => $ this ->companies_addresses_id ,
167
+ 'companies_contacts_id ' => $ this ->companies_contacts_id ,
168
+ 'user_id ' => $ this ->user_id ,
169
+ ]);
170
+ }
171
+
172
+ private function createDeliveryNoteLines ($ deliveryCreated )
173
+ {
139
174
foreach ($ this ->data as $ key => $ item ) {
140
- if (array_key_exists ("order_line_id " ,$ this ->data [$ key ])){
141
- if ($ this ->data [$ key ]['order_line_id ' ] != false ){
142
- $ i ++;
143
- }
175
+ if ($ this ->isOrderLineValid ($ item )) {
176
+ $ this ->deliveryLineService ->createDeliveryLine ($ deliveryCreated , $ key , $ this ->ordre , $ item ['scumQty ' ]);
177
+ $ this ->updateOrderLine ($ key , $ item ['scumQty ' ]);
178
+ $ this ->handleStockMovement ($ key , $ item ['scumQty ' ]);
179
+ $ this ->ordre += 10 ;
144
180
}
145
181
}
146
-
147
- if ($ i >0 ){
148
- // Create delivery note
149
- $ DeliveryCreated = Deliverys::create ([
150
- 'uuid ' => Str::uuid (),
151
- 'code ' =>$ this ->code ,
152
- 'label ' =>$ this ->label ,
153
- 'companies_id ' =>$ this ->companies_id ,
154
- 'companies_addresses_id ' =>$ this ->companies_addresses_id ,
155
- 'companies_contacts_id ' =>$ this ->companies_contacts_id ,
156
- 'user_id ' =>$ this ->user_id ,
182
+ }
183
+
184
+ private function isOrderLineValid ($ item )
185
+ {
186
+ return array_key_exists ('order_line_id ' , $ item ) && $ item ['order_line_id ' ] !== false && !empty ($ item ['scumQty ' ]);
187
+ }
188
+
189
+ private function updateOrderLine ($ orderLineId , $ scumQty )
190
+ {
191
+ $ orderLine = OrderLines::find ($ orderLineId );
192
+
193
+ if ($ this ->CreateSerialNumber ) {
194
+ $ this ->generateSerialNumbers ($ orderLine ->product_id , $ orderLineId , $ scumQty );
195
+ }
196
+
197
+ $ orderLine ->delivered_qty += $ scumQty ;
198
+ $ orderLine ->delivered_remaining_qty -= $ scumQty ;
199
+
200
+ if ($ orderLine ->delivered_remaining_qty == 0 ) {
201
+ $ orderLine ->delivery_status = 3 ;
202
+ } else {
203
+ $ orderLine ->delivery_status = 2 ;
204
+ }
205
+
206
+ $ orderLine ->save ();
207
+ event (new OrderLineUpdated ($ orderLine ->id ));
208
+ }
209
+
210
+ private function generateSerialNumbers ($ productId , $ orderLineId , $ scumQty )
211
+ {
212
+ for ($ i = 0 ; $ i < $ scumQty ; $ i ++) {
213
+ SerialNumbers::create ([
214
+ 'products_id ' => $ productId ,
215
+ 'order_line_id ' => $ orderLineId ,
216
+ 'serial_number ' => Str::uuid (),
217
+ 'status ' => 2 , // sold
157
218
]);
158
-
159
- // Create delivery note lines
160
- foreach ($ this ->data as $ key => $ item ) {
161
- //check if add line to new delivery note is aviable
162
- if (array_key_exists ("order_line_id " ,$ this ->data [$ key ])){
163
- if ($ this ->data [$ key ]['order_line_id ' ] != false ){
164
- // Create delivery line
165
- $ this ->deliveryLineService ->createDeliveryLine ($ DeliveryCreated , $ key , $ this ->ordre , $ this ->data [$ key ]['scumQty ' ]);
166
-
167
- // update order line info
168
- //same function from stock location product controller
169
- $ OrderLine = OrderLines::find ($ key );
170
-
171
- if ($ this ->CreateSerialNumber ){
172
- $ productId = null ;
173
- if ($ OrderLine ->product_id ) {
174
- $ productId =$ OrderLine ->product_id ;
175
- }
176
- // Generate and insert serial numbers for each qt delivered
177
- for ($ i = 0 ; $ i < $ this ->data [$ key ]['scumQty ' ]; $ i ++) {
178
- SerialNumbers::create ([
179
- 'products_id ' => $ productId ,
180
- 'order_line_id ' => $ key ,
181
- 'serial_number ' => Str::uuid (),
182
- 'status ' => 2 , // sold
183
- ]);
184
- }
185
- }
186
-
187
- $ OrderLine ->delivered_qty = $ OrderLine ->delivered_qty + $ this ->data [$ key ]['scumQty ' ];
188
- $ OrderLine ->delivered_remaining_qty = $ OrderLine ->delivered_remaining_qty - $ this ->data [$ key ]['scumQty ' ];
189
- //if we are delivered all part
190
- if ($ OrderLine ->delivered_remaining_qty == 0 ){
191
- $ OrderLine ->delivery_status = 3 ;
192
- $ OrderLine ->save ();
193
- // update order statu info
194
- // we must be check if all entry are delivered
195
- event (new OrderLineUpdated ($ OrderLine ->id ));
196
- }
197
- else {
198
- $ OrderLine ->delivery_status = 2 ;
199
- $ OrderLine ->save ();
200
- // update order statu info
201
- event (new OrderLineUpdated ($ OrderLine ->id ));
202
- }
203
-
204
- $ TaskRelation = $ OrderLine ->Task ()->get ();
205
-
206
- if ($ this ->RemoveFromStock && $ OrderLine ->product_id && $ TaskRelation ->isEmpty ()){
207
- $ quantityRemaining = $ this ->data [$ key ]['scumQty ' ];
208
-
209
- $ StockLocationProduct = StockLocationProducts::where ('products_id ' , $ OrderLine ->product_id )->get ();
210
- foreach ($ StockLocationProduct as $ stock ) {
211
-
212
- // Calculate the quantity to exit from this stock
213
- $ quantityToWithdraw = min ($ stock ->getCurrentStockMove (), $ quantityRemaining );
214
-
215
- if ($ quantityToWithdraw != 0 ){
216
- // Create a negative stock movement to record the stock issue
217
- $ stockMove = StockMove::create (['user_id ' => Auth::id (),
218
- 'qty ' => $ quantityToWithdraw ,
219
- 'stock_location_products_id ' => $ stock ->id ,
220
- 'order_line_id ' =>$ OrderLine ->id ,
221
- 'typ_move ' =>9 ,
222
- ]);
223
- }
224
-
225
- // Update remaining quantity
226
- $ quantityRemaining -= $ quantityToWithdraw ;
227
-
228
- // Exit the loop if the requested quantity has been satisfied
229
- if ($ quantityRemaining <= 0 ) {
230
- break ;
231
- }
232
- }
233
- }
234
-
235
- $ this ->ordre = $ this ->ordre +10 ;
236
- }
237
- }
238
- }
239
-
240
- // return view on new document
241
- return redirect ()->route ('deliverys.show ' , ['id ' => $ DeliveryCreated ->id ])->with ('success ' , 'Successfully created new delivery note ' );
242
219
}
243
- else {
244
- $ errors = $ this ->getErrorBag ();
245
- $ errors ->add ('errors ' , 'no lines selected ' );
220
+ }
221
+
222
+ private function handleStockMovement ($ orderLineId , $ scumQty )
223
+ {
224
+ $ orderLine = OrderLines::find ($ orderLineId );
225
+ $ taskRelation = $ orderLine ->Task ()->get ();
226
+
227
+ if ($ this ->RemoveFromStock && $ orderLine ->product_id && $ taskRelation ->isEmpty ()) {
228
+ $ quantityRemaining = $ scumQty ;
229
+ $ stockLocationProducts = StockLocationProducts::where ('products_id ' , $ orderLine ->product_id )->get ();
230
+
231
+ foreach ($ stockLocationProducts as $ stock ) {
232
+ $ quantityToWithdraw = min ($ stock ->getCurrentStockMove (), $ quantityRemaining );
233
+
234
+ if ($ quantityToWithdraw != 0 ) {
235
+ StockMove::create ([
236
+ 'user_id ' => Auth::id (),
237
+ 'qty ' => $ quantityToWithdraw ,
238
+ 'stock_location_products_id ' => $ stock ->id ,
239
+ 'order_line_id ' => $ orderLine ->id ,
240
+ 'typ_move ' => 9 ,
241
+ ]);
242
+ }
243
+
244
+ $ quantityRemaining -= $ quantityToWithdraw ;
245
+
246
+ if ($ quantityRemaining <= 0 ) {
247
+ break ;
248
+ }
249
+ }
246
250
}
247
251
}
248
252
}
0 commit comments