Code Duplication    Length = 184-186 lines in 2 locations

src/Intraface/modules/debtor/DebtorGateway.php 1 location

@@ 213-396 (lines=184) @@
210
     *
211
     * @return array
212
     */
213
    public function findAll()
214
    {
215
        $db = new DB_Sql;
216
217
        $this->dbquery = $this->getDBQuery();
218
219
        $this->dbquery->setCondition("debtor.type = ".$this->getTypeKey());
220
221
        if ($this->dbquery->checkFilter("contact_id")) {
222
            $this->dbquery->setCondition("debtor.contact_id = ".intval($this->dbquery->getFilter("contact_id")));
223
        }
224
225
        if ($this->dbquery->checkFilter("text")) {
226
            $this->dbquery->setCondition("(debtor.description LIKE \"%".$this->dbquery->getFilter("text")."%\" OR debtor.girocode = \"".$this->dbquery->getFilter("text")."\" OR debtor.number = \"".$this->dbquery->getFilter("text")."\" OR address.name LIKE \"%".$this->dbquery->getFilter("text")."%\")");
227
        }
228
229
        if ($this->dbquery->checkFilter("product_id")) {
230
            $this->dbquery->setCondition("debtor_item.product_id = ".$this->dbquery->getFilter('product_id'));
231
            if ($this->dbquery->checkFilter("product_variation_id")) {
232
                $this->dbquery->setCondition("debtor_item.product_variation_id = ".$this->dbquery->getFilter('product_variation_id'));
233
            } else {
234
                $this->dbquery->setCondition("debtor_item.product_variation_id = 0");
235
            }
236
        }
237
238
        if ($this->dbquery->checkFilter("date_field")) {
239
            if (in_array($this->dbquery->getFilter("date_field"), array('this_date', 'date_created', 'date_sent', 'date_executed', 'data_cancelled'))) {
240
                $date_field = $this->dbquery->getFilter("date_field");
241
            } else {
242
                $this->error->set("Ugyldigt datointerval felt");
243
            }
244
        } else {
245
            $date_field = 'this_date';
246
        }
247
248
        if ($this->dbquery->checkFilter("from_date")) {
249
            $date = new Intraface_Date($this->dbquery->getFilter("from_date"));
250
            if ($date->convert2db()) {
251
                $this->dbquery->setCondition("debtor.".$date_field." >= \"".$date->get()."\"");
252
            } else {
253
                $this->error->set("Fra dato er ikke gyldig");
254
            }
255
        }
256
257
        // Poster med fakturadato f�r slutdato.
258
        if ($this->dbquery->checkFilter("to_date")) {
259
            $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
260
            if ($date->convert2db()) {
261
                $this->dbquery->setCondition("debtor.".$date_field." <= \"".$date->get()."\"");
262
            } else {
263
                $this->error->set("Til dato er ikke gyldig");
264
            }
265
        }
266
        // alle ikke bogf�rte skal findes
267
        if ($this->dbquery->checkFilter("not_stated")) {
268
            $this->dbquery->setCondition("voucher_id = 0");
269
        }
270
271
        if ($this->dbquery->checkFilter("status")) {
272
            if ($this->dbquery->getFilter("status") == "-1") {
273
                // Beh�ves ikke, den tager alle.
274
                // $this->dbquery->setCondition("status >= 0");
275
            } elseif ($this->dbquery->getFilter("status") == "-2") {
276
                // Not executed = �bne
277
                if ($this->dbquery->checkFilter("to_date")) {
278
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
279
                    if ($date->convert2db()) {
280
                        // Poster der er executed eller cancelled efter dato, og sikring at executed stadig er det, da faktura kan s�ttes tilbage.
281
                        $this->dbquery->setCondition("(debtor.date_executed >= \"".$date->get()."\" AND debtor.status = 2) OR (debtor.date_cancelled >= \"".$date->get()."\") OR debtor.status < 2");
282
                    }
283
                } else {
284
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
285
                    $this->dbquery->setCondition("debtor.status < 2");
286
                }
287
            } elseif ($this->dbquery->getFilter("status") == "-3") {
288
                //  Afskrevne. Vi tager f�rst alle sendte og executed.
289
290
                if ($this->get("type") != "invoice") {
291
                    throw new Exception("Afskrevne kan kun benyttes ved faktura");
292
                }
293
294
                $this->dbquery->setJoin("INNER", "invoice_payment", "invoice_payment.payment_for_id = debtor.id", "invoice_payment.intranet_id = ".$this->kernel->intranet->get("id")." AND invoice_payment.payment_for = 1");
295
                $this->dbquery->setCondition("invoice_payment.type = -1");
296
297
                if ($this->dbquery->checkFilter("to_date")) {
298
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
299
                    if ($date->convert2db()) {
300
                        // alle som er sendte p� datoen og som ikke er cancelled
301
                        $this->dbquery->setCondition("debtor.date_sent <= '".$date->get()."' AND debtor.status != 3");
302
                        $this->dbquery->setCondition("invoice_payment.payment_date <= '".$date->get()."'");
303
                    }
304
                } else {
305
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
306
                    $this->dbquery->setCondition("status = 1 OR status = 2");
307
                }
308
            } else {
309
                $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
310
311
                /*
312
                // New date_field handles this instead
313
                switch ($this->dbquery->getFilter("status")) {
314
                    case "0":
315
                        $to_date_field = "date_created";
316
                        break;
317
318
                    case "1":
319
                        $to_date_field = "date_sent";
320
                        break;
321
322
                    case "2":
323
                        $to_date_field = "date_executed";
324
                        break;
325
326
                    case "3":
327
                        $to_date_field = "data_cancelled";
328
                        break;
329
                }
330
331
                if ($this->dbquery->checkFilter("to_date")) {
332
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
333
                    if ($date->convert2db()) {
334
                        // This gives a problem: We have an invoice created 20/4 and is executed 5/5
335
                        // If we make a search: executed 1/4-30/4 the above invoice will not be calculated in with date search below.
336
                        // And if we make a search : executed 1/5-30/5 it will not even be included in that search.
337
                        // Why was this made in that way?
338
                        // $this->dbquery->setCondition("debtor.".$to_date_field." <= \"".$date->get()."\"");
339
                        // So instead we add this normal status search: Changed 12/7 2009 /Sune
340
                        $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
341
                    }
342
                } else {
343
                    // tager dem som p� nuv�rende tidspunkt har den angivet status
344
                    $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
345
                }
346
                */
347
            }
348
        }
349
350
        switch ($this->dbquery->getFilter("sorting")) {
351
            case 1:
352
                $this->dbquery->setSorting("debtor.number ASC");
353
                break;
354
            case 2:
355
                $this->dbquery->setSorting("contact.number ASC");
356
                break;
357
            case 3:
358
                $this->dbquery->setSorting("address.name ASC");
359
                break;
360
            default:
361
                $this->dbquery->setSorting("debtor.number DESC");
362
        }
363
364
        $db = $this->dbquery->getRecordset("DISTINCT(debtor.id)", "", false);
365
        $i = 0;
366
        $list = array();
367
368
        while ($db->nextRecord()) {
369
            $debtor = $this->findById((int)$db->f("id"));
370
            $list[$i] = $debtor->get();
371
372
            // $contact = new Contact($this->kernel, $db->f('contact_id'));
373
            if (is_object($debtor->contact->address)) {
374
                $list[$i]['contact'] = $debtor->contact->get();
375
                $list[$i]['contact']['address'] = $debtor->contact->address->get();
376
377
                // f�lgende skal v�k
378
                $list[$i]['contact_id'] = $debtor->contact->get('id');
379
                $list[$i]['name'] = $debtor->contact->address->get('name');
380
                $list[$i]['address'] = $debtor->contact->address->get('address');
381
                $list[$i]['postalcode'] = $debtor->contact->address->get('postcode');
382
                $list[$i]['city'] = $debtor->contact->address->get('city');
383
            }
384
            $debtor->destruct();
385
            unset($debtor);
386
            $i++;
387
        }
388
        unset($db);
389
        return $list;
390
    }
391
392
    /**
393
     * Funktion til at finde ud af, om der er oprettet nogen poster af den aktuelle bruger
394
     *
395
     * @return integer
396
     */
397
    public function isFilledIn()
398
    {
399
        $db = new DB_Sql;

src/Intraface/modules/debtor/Debtor.php 1 location

@@ 782-967 (lines=186) @@
779
     *
780
     * @return array
781
     */
782
    public function getList()
783
    {
784
        $db = new DB_Sql;
785
786
        $this->dbquery = $this->getDBQuery();
787
788
        $this->dbquery->setCondition("debtor.type = ".$this->get("type_key"));
789
790
        if ($this->dbquery->checkFilter("contact_id")) {
791
            $this->dbquery->setCondition("debtor.contact_id = ".intval($this->dbquery->getFilter("contact_id")));
792
        }
793
794
        if ($this->dbquery->checkFilter("text")) {
795
            $this->dbquery->setCondition("(debtor.description LIKE \"%".$this->dbquery->getFilter("text")."%\" OR debtor.girocode = \"".$this->dbquery->getFilter("text")."\" OR debtor.number = \"".$this->dbquery->getFilter("text")."\" OR address.name LIKE \"%".$this->dbquery->getFilter("text")."%\")");
796
        }
797
798
        if ($this->dbquery->checkFilter("product_id")) {
799
            $this->dbquery->setCondition("debtor_item.product_id = ".$this->dbquery->getFilter('product_id'));
800
            if ($this->dbquery->checkFilter("product_variation_id")) {
801
                $this->dbquery->setCondition("debtor_item.product_variation_id = ".$this->dbquery->getFilter('product_variation_id'));
802
            } else {
803
                $this->dbquery->setCondition("debtor_item.product_variation_id = 0");
804
            }
805
        }
806
807
        if ($this->dbquery->checkFilter("date_field")) {
808
            if (in_array($this->dbquery->getFilter("date_field"), array('this_date', 'date_created', 'date_sent', 'date_executed', 'data_cancelled'))) {
809
                $date_field = $this->dbquery->getFilter("date_field");
810
            } else {
811
                $this->error->set("Ugyldigt datointerval felt");
812
            }
813
        } else {
814
            $date_field = 'this_date';
815
        }
816
817
        if ($this->dbquery->checkFilter("from_date")) {
818
            $date = new Intraface_Date($this->dbquery->getFilter("from_date"));
819
            if ($date->convert2db()) {
820
                $this->dbquery->setCondition("debtor.".$date_field." >= \"".$date->get()."\"");
821
            } else {
822
                $this->error->set("Fra dato er ikke gyldig");
823
            }
824
        }
825
826
        // Poster med fakturadato f�r slutdato.
827
        if ($this->dbquery->checkFilter("to_date")) {
828
            $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
829
            if ($date->convert2db()) {
830
                $this->dbquery->setCondition("debtor.".$date_field." <= \"".$date->get()."\"");
831
            } else {
832
                $this->error->set("Til dato er ikke gyldig");
833
            }
834
        }
835
        // alle ikke bogf�rte skal findes
836
        if ($this->dbquery->checkFilter("not_stated")) {
837
            $this->dbquery->setCondition("voucher_id = 0");
838
        }
839
840
841
        if ($this->dbquery->checkFilter("status")) {
842
            if ($this->dbquery->getFilter("status") == "-1") {
843
                // Beh�ves ikke, den tager alle.
844
                // $this->dbquery->setCondition("status >= 0");
845
            } elseif ($this->dbquery->getFilter("status") == "-2") {
846
                // Not executed = �bne
847
                if ($this->dbquery->checkFilter("to_date")) {
848
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
849
                    if ($date->convert2db()) {
850
                        // Poster der er executed eller cancelled efter dato, og sikring at executed stadig er det, da faktura kan s�ttes tilbage.
851
                        $this->dbquery->setCondition("(debtor.date_executed >= \"".$date->get()."\" AND debtor.status = 2) OR (debtor.date_cancelled >= \"".$date->get()."\") OR debtor.status < 2");
852
                    }
853
                } else {
854
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
855
                    $this->dbquery->setCondition("debtor.status < 2");
856
                }
857
            } elseif ($this->dbquery->getFilter("status") == "-3") {
858
                //  Afskrevne. Vi tager f�rst alle sendte og executed.
859
860
                if ($this->get("type") != "invoice") {
861
                    throw new Exception("Afskrevne kan kun benyttes ved faktura");
862
                }
863
864
                $this->dbquery->setJoin("INNER", "invoice_payment", "invoice_payment.payment_for_id = debtor.id", "invoice_payment.intranet_id = ".$this->kernel->intranet->get("id")." AND invoice_payment.payment_for = 1");
865
                $this->dbquery->setCondition("invoice_payment.type = -1");
866
867
                if ($this->dbquery->checkFilter("to_date")) {
868
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
869
                    if ($date->convert2db()) {
870
                        // alle som er sendte p� datoen og som ikke er cancelled
871
                        $this->dbquery->setCondition("debtor.date_sent <= '".$date->get()."' AND debtor.status != 3");
872
                        $this->dbquery->setCondition("invoice_payment.payment_date <= '".$date->get()."'");
873
                    }
874
                } else {
875
                    // Hvis der ikke er nogen dato s� tager vi alle dem som p� nuv�rende tidspunkt har status under
876
                    $this->dbquery->setCondition("status = 1 OR status = 2");
877
                }
878
            } else {
879
                $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
880
881
                /*
882
                // New date_field handles this instead
883
                switch ($this->dbquery->getFilter("status")) {
884
                    case "0":
885
                        $to_date_field = "date_created";
886
                        break;
887
888
                    case "1":
889
                        $to_date_field = "date_sent";
890
                        break;
891
892
                    case "2":
893
                        $to_date_field = "date_executed";
894
                        break;
895
896
                    case "3":
897
                        $to_date_field = "data_cancelled";
898
                        break;
899
                }
900
901
                if ($this->dbquery->checkFilter("to_date")) {
902
                    $date = new Intraface_Date($this->dbquery->getFilter("to_date"));
903
                    if ($date->convert2db()) {
904
                        // This gives a problem: We have an invoice created 20/4 and is executed 5/5
905
                        // If we make a search: executed 1/4-30/4 the above invoice will not be calculated in with date search below.
906
                        // And if we make a search : executed 1/5-30/5 it will not even be included in that search.
907
                        // Why was this made in that way?
908
                        // $this->dbquery->setCondition("debtor.".$to_date_field." <= \"".$date->get()."\"");
909
                        // So instead we add this normal status search: Changed 12/7 2009 /Sune
910
                        $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
911
                    }
912
                } else {
913
                    // tager dem som p� nuv�rende tidspunkt har den angivet status
914
                    $this->dbquery->setCondition("debtor.status = ".intval($this->dbquery->getFilter("status")));
915
                }
916
                */
917
            }
918
        }
919
920
        switch ($this->dbquery->getFilter("sorting")) {
921
            case 1:
922
                $this->dbquery->setSorting("debtor.number ASC");
923
                break;
924
            case 2:
925
                $this->dbquery->setSorting("contact.number ASC");
926
                break;
927
            case 3:
928
                $this->dbquery->setSorting("address.name ASC");
929
                break;
930
            default:
931
                $this->dbquery->setSorting("debtor.number DESC");
932
        }
933
934
        $db = $this->dbquery->getRecordset("DISTINCT(debtor.id)", "", false);
935
        $i = 0;
936
        $list = array();
937
938
        while ($db->nextRecord()) {
939
            $debtor = self::factory($this->kernel, (int)$db->f("id"));
940
            $list[$i] = $debtor->get();
941
942
            // $contact = new Contact($this->kernel, $db->f('contact_id'));
943
            if (is_object($debtor->contact->address)) {
944
                $list[$i]['contact'] = $debtor->contact->get();
945
                $list[$i]['contact']['address'] = $debtor->contact->address->get();
946
947
                // f�lgende skal v�k
948
                $list[$i]['contact_id'] = $debtor->contact->get('id');
949
                $list[$i]['name'] = $debtor->contact->address->get('name');
950
                $list[$i]['address'] = $debtor->contact->address->get('address');
951
                $list[$i]['postalcode'] = $debtor->contact->address->get('postcode');
952
                $list[$i]['city'] = $debtor->contact->address->get('city');
953
            }
954
            $debtor->destruct();
955
            unset($debtor);
956
            $i++;
957
        }
958
        unset($db);
959
        return $list;
960
    }
961
962
    /**
963
     * Checks whether a product number is available
964
     *
965
     * @return boolean
966
     */
967
    private function isNumberFree($number)
968
    {
969
        $number = safeToDb($number);
970
        $db = new DB_Sql;