@@ -222,25 +222,89 @@ public function triggerEvasione(Component $trigger)
222222 $ qta_evasa = $ righe ->sum ('qta_evasa ' );
223223 $ parziale = $ qta != $ qta_evasa ;
224224
225+ $ fatture_collegate = database ()->table ('co_righe_documenti ' )
226+ ->where ('idddt ' , $ this ->id )
227+ ->join ('co_documenti ' , 'co_righe_documenti.iddocumento ' , '= ' , 'co_documenti.id ' )
228+ ->count ();
229+
230+ $ qta_fatturate = 0 ;
231+ $ parziale_fatturato = true ;
225232 $ fattura = Fattura::find ($ trigger ->iddocumento );
226233 if (!empty ($ fattura )) {
227- $ righe_fatturate = $ fattura ->getRighe ()->where ('idddt ' , ' = ' , $ this ->id );
234+ $ righe_fatturate = $ fattura ->getRighe ()->where ('idddt ' , $ this ->id );
228235 $ qta_fatturate = $ righe_fatturate ->sum ('qta ' );
229236 $ parziale_fatturato = $ qta != $ qta_fatturate ;
230237 }
231238
239+ $ collegato_a_ordine = false ;
240+ foreach ($ righe as $ riga ) {
241+ if (!empty ($ riga ->original_id ) && !empty ($ riga ->original_type ) && strpos ($ riga ->original_type , 'Ordini ' ) !== false ) {
242+ $ collegato_a_ordine = true ;
243+ break ;
244+ }
245+ }
246+
232247 // Impostazione del nuovo stato
233- if ($ qta_evasa == 0 ) {
248+ if ($ qta_evasa == 0 && ! $ collegato_a_ordine ) {
234249 $ descrizione = 'Bozza ' ;
235- } elseif (empty ($ qta_fatturate )) {
236- $ descrizione = $ parziale ? 'Parzialmente evaso ' : 'Evaso ' ;
237- } else {
250+ } elseif ($ fatture_collegate > 0 ) {
238251 $ descrizione = $ parziale_fatturato ? 'Parzialmente fatturato ' : 'Fatturato ' ;
252+ } else {
253+ $ descrizione = $ parziale ? 'Parzialmente evaso ' : 'Evaso ' ;
239254 }
240255
241256 $ stato = Stato::where ('name ' , $ descrizione )->first ()->id ;
242257 $ this ->stato ()->associate ($ stato );
243258 $ this ->save ();
259+
260+ if ($ descrizione == 'Fatturato ' || $ descrizione == 'Parzialmente fatturato ' ) {
261+ $ this ->aggiornaStatiOrdiniCollegati ();
262+ }
263+ }
264+ }
265+
266+ /**
267+ * Aggiorna lo stato degli ordini collegati a questo DDT.
268+ * Quando il DDT passa a "Fatturato" o "Parzialmente fatturato", anche gli ordini collegati
269+ * devono passare a "Fatturato" o "Parzialmente fatturato".
270+ */
271+ public function aggiornaStatiOrdiniCollegati ()
272+ {
273+ $ righe_ddt = $ this ->getRighe ();
274+
275+ foreach ($ righe_ddt as $ riga_ddt ) {
276+ if (!empty ($ riga_ddt ->original_id ) && !empty ($ riga_ddt ->original_type ) && strpos ($ riga_ddt ->original_type , 'Ordini ' ) !== false ) {
277+ $ riga_ordine = $ riga_ddt ->getOriginalComponent ();
278+
279+ if (!empty ($ riga_ordine )) {
280+ $ ordine = $ riga_ordine ->getDocument ();
281+
282+ if (!empty ($ ordine )) {
283+ $ fatture_collegate = database ()->table ('co_righe_documenti ' )
284+ ->where ('idddt ' , $ this ->id )
285+ ->join ('co_documenti ' , 'co_righe_documenti.iddocumento ' , '= ' , 'co_documenti.id ' )
286+ ->count ();
287+
288+ if ($ fatture_collegate > 0 ) {
289+ $ righe_ordine = $ ordine ->getRighe ();
290+ $ qta = $ righe_ordine ->sum ('qta ' );
291+ $ qta_evasa = $ righe_ordine ->sum ('qta_evasa ' );
292+ $ parziale = $ qta != $ qta_evasa ;
293+
294+ $ descrizione = $ parziale ? 'Parzialmente fatturato ' : 'Fatturato ' ;
295+
296+ if (database ()->isConnected () && database ()->tableExists ('or_statiordine_lang ' )) {
297+ $ stato = \Modules \Ordini \Stato::where ('name ' , $ descrizione )->first ()->id ;
298+ } else {
299+ $ stato = \Modules \Ordini \Stato::where ('descrizione ' , $ descrizione )->first ()->id ;
300+ }
301+
302+ $ ordine ->stato ()->associate ($ stato );
303+ $ ordine ->save ();
304+ }
305+ }
306+ }
307+ }
244308 }
245309 }
246310
0 commit comments