1919
2020namespace FacturaScripts \Plugins \Proyectos \Lib ;
2121
22- use FacturaScripts \Core \Base \DataBase \DataBaseWhere ;
23- use FacturaScripts \Core \Tools ;
24-
22+ use FacturaScripts \Core \Where ;
2523use FacturaScripts \Dinamic \Model \AlbaranCliente ;
2624use FacturaScripts \Dinamic \Model \AlbaranProveedor ;
2725use FacturaScripts \Dinamic \Model \FacturaCliente ;
2826use FacturaScripts \Dinamic \Model \FacturaProveedor ;
2927use FacturaScripts \Dinamic \Model \PedidoCliente ;
3028use FacturaScripts \Dinamic \Model \PedidoProveedor ;
3129use FacturaScripts \Dinamic \Model \PresupuestoCliente ;
32- use FacturaScripts \Plugins \ Proyectos \Model \Proyecto ;
30+ use FacturaScripts \Dinamic \Model \Proyecto ;
3331
3432/**
3533 * Description of ProjectTotalManager
3836 */
3937class ProjectTotalManager
4038{
41- public static function recalculate (int $ idproyecto )
39+ public static function recalculate (int $ idproyecto ): void
4240 {
4341 $ project = new Proyecto ();
4442 if (false === $ project ->load ($ idproyecto )) {
4543 return ;
4644 }
4745
46+ // Compras
4847 $ project ->totalcompras = 0.0 ;
49- foreach (static ::purchaseInvoices ($ idproyecto ) as $ invoice ) {
50- $ project ->totalcompras += $ invoice ->total ;
51- }
52- foreach (static ::purchaseDeliveryNotes ($ idproyecto ) as $ delivery ) {
53- $ project ->totalcompras += $ delivery ->total ;
54- }
55- foreach (static ::purchaseOrders ($ idproyecto ) as $ order ) {
56- $ project ->totalcompras += $ order ->total ;
57- }
48+ $ project ->totalcompras += static ::purchaseInvoices ($ idproyecto );
49+ $ project ->totalcompras += static ::purchaseDeliveryNotes ($ idproyecto );
50+ $ project ->totalcompras += static ::purchaseOrders ($ idproyecto );
5851
52+ // Ventas
5953 $ netoPresupuestos = 0.0 ;
6054 $ netoPedidos = 0.0 ;
6155 $ netoAlbaranes = 0.0 ;
6256 $ netoFacturas = 0.0 ;
57+
6358 $ project ->totalventas = 0.0 ;
64- foreach (static ::salesEstimations ($ idproyecto ) as $ estimation ) {
65- $ project ->totalventas += $ estimation ->total ;
66- $ netoPresupuestos += $ estimation ->neto ;
67- }
68- foreach (static ::salesInvoices ($ idproyecto ) as $ invoice ) {
69- $ project ->totalventas += $ invoice ->total ;
70- $ netoFacturas += $ invoice ->neto ;
71- }
72- foreach (static ::salesDeliveryNotes ($ idproyecto ) as $ delivery ) {
73- $ project ->totalventas += $ delivery ->total ;
74- $ netoAlbaranes += $ delivery ->neto ;
75- }
76- foreach (static ::salesOrders ($ idproyecto ) as $ order ) {
77- $ project ->totalventas += $ order ->total ;
78- $ netoPedidos += $ order ->neto ;
79- }
59+ $ project ->totalventas += static ::salesEstimations ($ idproyecto , $ netoPresupuestos );
60+ $ project ->totalventas += static ::salesOrders ($ idproyecto , $ netoPedidos );
61+ $ project ->totalventas += static ::salesDeliveryNotes ($ idproyecto , $ netoAlbaranes );
62+ $ project ->totalventas += static ::salesInvoices ($ idproyecto , $ netoFacturas );
8063
81- $ project ->totalpendientefacturar = ( $ netoPresupuestos + $ netoPedidos + $ netoAlbaranes) - $ netoFacturas ;
64+ $ project ->totalpendientefacturar = $ netoPresupuestos + $ netoPedidos + $ netoAlbaranes ;
8265 if ($ project ->totalpendientefacturar < 0 ) {
8366 $ project ->totalpendientefacturar = 0 ;
8467 }
8568
8669 $ project ->save ();
8770 }
8871
89- /**
90- * @param int $idproyecto
91- *
92- * @return AlbaranProveedor[]
93- */
94- protected static function purchaseDeliveryNotes ($ idproyecto ): array
72+ protected static function purchaseDeliveryNotes (int $ idproyecto ): float
9573 {
96- $ delivery = new AlbaranProveedor ();
97- $ where = [
98- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
99- new DataBaseWhere ('editable ' , true )
100- ];
101- $ results = $ delivery ->all ($ where , [], 0 , 0 );
102-
103- // Also include non-editable delivery notes that are invoiced but whose invoice is not part of the project
104- $ whereNonEditable = [
105- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
106- new DataBaseWhere ('editable ' , false )
107- ];
108- $ nonEditables = $ delivery ->all ($ whereNonEditable , [], 0 , 0 );
109- foreach ($ nonEditables as $ del ) {
110- $ children = $ del ->childrenDocuments ();
111- $ hasInvoiceInProject = false ;
112- foreach ($ children as $ child ) {
113- if ($ child instanceof FacturaProveedor && $ child ->idproyecto == $ idproyecto ) {
114- $ hasInvoiceInProject = true ;
115- break ;
116- }
74+ $ total = 0.0 ;
75+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
76+ foreach (AlbaranProveedor::all ($ where ) as $ delivery ) {
77+ $ childrens = $ delivery ->childrenDocuments ();
78+ if (empty ($ childrens )) {
79+ $ total += $ delivery ->total ;
80+ continue ;
11781 }
118- if (! $ hasInvoiceInProject ) {
119- $ results [] = $ del ;
82+
83+ $ totalHijos = 0.0 ;
84+ foreach ($ childrens as $ child ) {
85+ if ($ child ->idproyecto == $ idproyecto ) {
86+ $ totalHijos += $ child ->total ;
87+ }
12088 }
89+
90+ $ total += ($ delivery ->total - $ totalHijos );
12191 }
12292
123- return $ results ;
93+ return $ total ;
12494 }
12595
126- /**
127- * @param int $idproyecto
128- * @return FacturaProveedor[]
129- */
130- protected static function purchaseInvoices (int $ idproyecto ): array
96+ protected static function purchaseInvoices (int $ idproyecto ): float
13197 {
132- $ invoice = new FacturaProveedor ();
133- $ where = [new DataBaseWhere ('idproyecto ' , $ idproyecto )];
134- return $ invoice ->all ($ where , [], 0 , 0 );
98+ $ total = 0.0 ;
99+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
100+ foreach (FacturaProveedor::all ($ where ) as $ invoice ) {
101+ $ total += $ invoice ->total ;
102+ }
103+ return $ total ;
135104 }
136105
137- /**
138- * @param int $idproyecto
139- * @return PedidoProveedor[]
140- */
141- protected static function purchaseOrders (int $ idproyecto ): array
106+ protected static function purchaseOrders (int $ idproyecto ): float
142107 {
143- $ order = new PedidoProveedor ();
144- $ where = [
145- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
146- new DataBaseWhere ('editable ' , true )
147- ];
148- return $ order ->all ($ where , [], 0 , 0 );
108+ $ total = 0.0 ;
109+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
110+ foreach (PedidoProveedor::all ($ where ) as $ order ) {
111+ $ childrens = $ order ->childrenDocuments ();
112+ if (empty ($ childrens )) {
113+ $ total += $ order ->total ;
114+ continue ;
115+ }
116+
117+ $ totalHijos = 0.0 ;
118+ foreach ($ childrens as $ child ) {
119+ if ($ child ->idproyecto == $ idproyecto ) {
120+ $ totalHijos += $ child ->total ;
121+ }
122+ }
123+
124+ $ total += ($ order ->total - $ totalHijos );
125+ }
126+
127+ return $ total ;
149128 }
150129
151- /**
152- * @param int $idproyecto
153- * @return AlbaranCliente[]
154- */
155- protected static function salesDeliveryNotes (int $ idproyecto ): array
130+ protected static function salesDeliveryNotes (int $ idproyecto , float &$ neto ): float
156131 {
157- $ delivery = new AlbaranCliente ();
158- $ where = [
159- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
160- new DataBaseWhere ('editable ' , true )
161- ];
162- $ results = $ delivery ->all ($ where , [], 0 , 0 );
163-
164- // Also include non-editable delivery notes that are invoiced but whose invoice is not part of the project
165- $ whereNonEditable = [
166- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
167- new DataBaseWhere ('editable ' , false )
168- ];
169- $ nonEditables = $ delivery ->all ($ whereNonEditable , [], 0 , 0 );
170- foreach ($ nonEditables as $ del ) {
171- $ children = $ del ->childrenDocuments ();
172- $ hasInvoiceInProject = false ;
173- foreach ($ children as $ child ) {
174- if ($ child instanceof FacturaCliente && $ child ->idproyecto == $ idproyecto ) {
175- $ hasInvoiceInProject = true ;
176- break ;
177- }
132+ $ total = 0.0 ;
133+ $ neto = 0.0 ;
134+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
135+ foreach (AlbaranCliente::all ($ where ) as $ delivery ) {
136+ $ childrens = $ delivery ->childrenDocuments ();
137+ if (empty ($ childrens )) {
138+ $ total += $ delivery ->total ;
139+ $ neto += $ delivery ->neto ;
140+ continue ;
178141 }
179- if (! $ hasInvoiceInProject ) {
180- $ results [] = $ del ;
142+
143+ $ totalHijos = 0.0 ;
144+ $ netoHijos = 0.0 ;
145+ foreach ($ childrens as $ child ) {
146+ if ($ child ->idproyecto == $ idproyecto ) {
147+ $ totalHijos += $ child ->total ;
148+ $ netoHijos += $ child ->neto ;
149+ }
181150 }
151+
152+ $ total += ($ delivery ->total - $ totalHijos );
153+ $ neto += ($ delivery ->neto - $ netoHijos );
182154 }
183155
184- return $ results ;
156+ return $ total ;
185157 }
186158
187- /**
188- * @param int $idproyecto
189- * @return PresupuestoCliente[]
190- */
191- protected static function salesEstimations (int $ idproyecto ): array
159+ protected static function salesEstimations (int $ idproyecto , float &$ neto ): float
192160 {
193- $ estimation = new PresupuestoCliente ();
194- $ where = [new DataBaseWhere ('idproyecto ' , $ idproyecto )];
195- return $ estimation ->all ($ where , [], 0 , 0 );
161+ $ total = 0.0 ;
162+ $ neto = 0.0 ;
163+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
164+ foreach (PresupuestoCliente::all ($ where ) as $ estimation ) {
165+ $ childrens = $ estimation ->childrenDocuments ();
166+ if (empty ($ childrens )) {
167+ $ total += $ estimation ->total ;
168+ $ neto += $ estimation ->neto ;
169+ continue ;
170+ }
171+
172+ $ totalHijos = 0.0 ;
173+ $ netoHijos = 0.0 ;
174+ foreach ($ childrens as $ child ) {
175+ if ($ child ->idproyecto == $ idproyecto ) {
176+ $ totalHijos += $ child ->total ;
177+ $ netoHijos += $ child ->neto ;
178+ }
179+ }
180+
181+ $ total += ($ estimation ->total - $ totalHijos );
182+ $ neto += ($ estimation ->neto - $ netoHijos );
183+ }
184+
185+ return $ total ;
196186 }
197187
198- /**
199- * @param int $idproyecto
200- * @return FacturaCliente[]
201- */
202- protected static function salesInvoices (int $ idproyecto ): array
188+ protected static function salesInvoices (int $ idproyecto , float &$ neto ): float
203189 {
204- $ invoice = new FacturaCliente ();
205- $ where = [new DataBaseWhere ('idproyecto ' , $ idproyecto )];
206- return $ invoice ->all ($ where , [], 0 , 0 );
190+ $ total = 0.0 ;
191+ $ neto = 0.0 ;
192+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
193+ foreach (FacturaCliente::all ($ where ) as $ invoice ) {
194+ $ total += $ invoice ->total ;
195+ $ neto += $ invoice ->neto ;
196+ }
197+
198+ return $ total ;
207199 }
208200
209- /**
210- * @param int $idproyecto
211- * @return PedidoCliente[]
212- */
213- protected static function salesOrders (int $ idproyecto ): array
201+ protected static function salesOrders (int $ idproyecto , float &$ neto ): float
214202 {
215- $ order = new PedidoCliente ();
216- $ where = [
217- new DataBaseWhere ('idproyecto ' , $ idproyecto ),
218- new DataBaseWhere ('editable ' , true )
219- ];
220- return $ order ->all ($ where , [], 0 , 0 );
203+ $ total = 0.0 ;
204+ $ neto = 0.0 ;
205+ $ where = [Where::eq ('idproyecto ' , $ idproyecto )];
206+ foreach (PedidoCliente::all ($ where ) as $ order ) {
207+ $ childrens = $ order ->childrenDocuments ();
208+ if (empty ($ childrens )) {
209+ $ total += $ order ->total ;
210+ $ neto += $ order ->neto ;
211+ continue ;
212+ }
213+
214+ $ totalHijos = 0.0 ;
215+ $ netoHijos = 0.0 ;
216+ foreach ($ childrens as $ child ) {
217+ if ($ child ->idproyecto == $ idproyecto ) {
218+ $ totalHijos += $ child ->total ;
219+ $ netoHijos += $ child ->neto ;
220+ }
221+ }
222+
223+ $ total += ($ order ->total - $ totalHijos );
224+ $ neto += ($ order ->neto - $ netoHijos );
225+ }
226+
227+ return $ total ;
221228 }
222- }
229+ }
0 commit comments