Completed
Push — master ( 547415...3a683c )
by Laurent
01:39
created

getBillingCondition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . '/../class/bbcvols.class.php';
4
require_once __DIR__ . '/CreateReceiverMonthBillCommand.php';
5
require_once __DIR__ . '/../../product/class/product.class.php';
6
require_once __DIR__ . '/../../compta/facture/class/facture.class.php';
7
require_once __DIR__ . '/../../user/class/user.class.php';
8
require_once __DIR__ . '/../../adherents/class/adherent.class.php';
9
require_once __DIR__ . '/AbstractBillCommandHandler.php';
10
require_once __DIR__ . '/CommandInterface.php';
11
12
/**
13
 * @author Laurent De Coninck <[email protected]>
14
 */
15
class CreateReceiverMonthBillCommandHandler extends AbstractBillCommandHandler
16
{
17
18
    /**
19
     * @param CreateReceiverMonthBillCommand|CommandInterface $command
20
     */
21
    public function handle(CommandInterface $command)
22
    {
23
        if (!($command instanceof CreateReceiverMonthBillCommand)) {
24
            throw new \InvalidArgumentException('Command not correct');
25
        }
26
27
        $object = new Facture($this->db);
28
        $object->fetch_thirdparty();
29
30
        $object->socid = $this->fetchCustomer($command->getReceiverId())->id;
31
        $object->type = $command->getBillType();
32
        $object->number = "provisoire";
33
        $object->date = $this->generateBillDate($command->getYear(), $command->getMonth());
34
        $object->date_pointoftax = "";
35
        $object->note_public = $command->getPublicNote();
36
        $object->note_private = $command->getPrivateNote();
37
        $object->ref_client = "";
38
        $object->ref_int = "";
39
        $object->modelpdf = $this->getModelDocument();
40
        $object->cond_reglement_id = $this->getBillingCondition();
41
        $object->mode_reglement_id = $this->getBillingType();
42
        $object->fk_account = $this->getBankAccount();
43
44
        $id = $object->create($this->user);
45
46
        if ($id <= 0) {
47
            throw new \InvalidArgumentException('Error during bill creation');
48
        }
49
50
        $flightProduct = $this->getProduct();
51
        foreach ($command->getFlights() as $flight) {
52
            $this->addOrderLine($object, $flightProduct, $flight);
53
            $this->addLinks($object, $flight);
54
            $this->flagFlightAsBilled($flight);
55
        }
56
57
        $this->generateBillDocument($object, $id);
58
59
        $this->validates($object, $id);
60
61
        $this->generateBillDocument($object, $id);
62
    }
63
64
65
    /**
66
     * @return int
67
     */
68
    private function getBankAccount()
69
    {
70
        return $this->conf->BBC_DEFAULT_BANK_ACCOUNT;
71
    }
72
73
    /**
74
     * @param int $year
75
     * @param int $month
76
     *
77
     * @return int
78
     */
79
    private function generateBillDate($year, $month)
80
    {
81
        $day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
82
        $date = (new DateTime())->setDate($year, $month, $day);
83
        return $date->getTimestamp();
84
    }
85
86
    /**
87
     * Get the default billing condition if not set in the customer.
88
     *
89
     * @return int
90
     */
91 View Code Duplication
    private function getBillingCondition()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        if (!empty($this->getCustomer()->cond_reglement_id)) {
94
            return $this->getCustomer()->cond_reglement_id;
95
        }
96
97
        if (!empty($this->conf->BBC_DEFAULT_PAYMENT_TERM_ID)) {
98
            return $this->conf->BBC_DEFAULT_PAYMENT_TERM_ID;
99
        }
100
101
        throw new \InvalidArgumentException('Billing condition / MAIN_DEFAULT_PAYMENT_TERM_ID not set');
102
    }
103
104
    /**
105
     * Get the default billing type if not set in the customer.
106
     *
107
     * @return int
108
     */
109 View Code Duplication
    private function getBillingType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if (!empty($this->getCustomer()->mode_reglement_id)) {
112
            return $this->getCustomer()->mode_reglement_id;
113
        }
114
115
        if (!empty($this->conf->BBC_DEFAULT_PAYMENT_TYPE_ID)) {
116
            return $this->conf->BBC_DEFAULT_PAYMENT_TYPE_ID;
117
        }
118
119
        throw new \InvalidArgumentException('Billing type / MAIN_DEFAULT_PAYMENT_TYPE_ID not set');
120
    }
121
122
123
}