Completed
Push — master ( f14102...3c5c0f )
by Laurent
01:43
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
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 = (new DateTime())->getTimestamp();
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->generateBillDocument($object, $id);
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    private function getBankAccount()
66
    {
67
        return $this->conf->BBC_DEFAULT_BANK_ACCOUNT;
68
    }
69
70
    /**
71
     * Get the default billing condition if not set in the customer.
72
     *
73
     * @return int
74
     */
75 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...
76
    {
77
        if (!empty($this->getCustomer()->cond_reglement_id)) {
78
            return $this->getCustomer()->cond_reglement_id;
79
        }
80
81
        if (!empty($this->conf->BBC_DEFAULT_PAYMENT_TERM_ID)) {
82
            return $this->conf->BBC_DEFAULT_PAYMENT_TERM_ID;
83
        }
84
85
        throw new \InvalidArgumentException('Billing condition / MAIN_DEFAULT_PAYMENT_TERM_ID not set');
86
    }
87
88
    /**
89
     * Get the default billing type if not set in the customer.
90
     *
91
     * @return int
92
     */
93 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...
94
    {
95
        if (!empty($this->getCustomer()->mode_reglement_id)) {
96
            return $this->getCustomer()->mode_reglement_id;
97
        }
98
99
        if (!empty($this->conf->BBC_DEFAULT_PAYMENT_TYPE_ID)) {
100
            return $this->conf->BBC_DEFAULT_PAYMENT_TYPE_ID;
101
        }
102
103
        throw new \InvalidArgumentException('Billing type / MAIN_DEFAULT_PAYMENT_TYPE_ID not set');
104
    }
105
106
107
}