Completed
Push — feature/fixing_cost ( ef981e )
by Laurent
01:53
created

SupplierBillSelect::buildOptions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Http\Web\Form;
5
6
7
use flightlog\form\Select;
8
9
final class SupplierBillSelect extends Select
10
{
11
    /**
12
     * @var \DoliDB
13
     */
14
    private $db;
15
16
    /**
17
     * @param string $name
18
     * @param \DoliDB $db
19
     * @param array $options
20
     */
21
    public function __construct($name, \DoliDB $db, $options = [])
22
    {
23
        parent::__construct($name, $options);
24
        $this->db = $db;
25
        $this->buildOptions();
26
    }
27
28
    /**
29
     * Build the options of the select
30
     */
31
    private function buildOptions()
32
    {
33
34
        if ((bool) $this->getOption('show_empty')) {
35
            $this->addValueOption(-1, ' ');
36
        }
37
38
        $sql = 'SELECT f.rowid, f.ref as ref_supplier, f.total_ttc, society.nom';
39
        $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as f';
40
        $sql.= ' INNER JOIN '.MAIN_DB_PREFIX.'societe as society ON society.rowid = f.fk_soc';
41
        $sql.= ' ORDER BY f.datec DESC';
42
43
        $resql = $this->db->query($sql);
44
        if ($resql) {
45
            $num = $this->db->num_rows($resql);
46
            $i = 0;
47
            if ($num) {
48
                while ($i < $num) {
49
                    $obj = $this->db->fetch_object($resql);
50
51
                    $this->addValueOption($obj->rowid, sprintf('(%s) %s (%s€)', $obj->ref_supplier, $obj->nom, $obj->total_ttc));
52
                    $i++;
53
                }
54
            }
55
56
57
        }
58
    }
59
60
61
}