| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | require_once __DIR__.'/CommandHandlerInterface.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Methods to create a bill. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Laurent De Coninck <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | abstract class AbstractBillCommandHandler implements CommandHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var stdClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $conf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var Translate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $langs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var \DoliDB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param DoliDB    $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param stdClass  $conf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param User      $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Translate $langs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 35 |  | View Code Duplication |     public function __construct($db, $conf, $user, $langs) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->db = $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->conf = $conf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->langs = $langs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return Product | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 46 |  | View Code Duplication |     protected function getProduct() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $flightProduct = new Product($this->db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($flightProduct->fetch($this->conf->BBC_FLIGHT_TYPE_CUSTOMER) <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw new \InvalidArgumentException('Default product not configured'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $flightProduct; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param int $receiverId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws CustomerNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     protected function getCustomer($receiverId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $user = new User($this->db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $res = $user->fetch($receiverId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($res <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new CustomerNotFoundException('User not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $member = new Adherent($this->db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $res = $member->fetch($user->fk_member); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($res <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new CustomerNotFoundException('Member not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $customer = new Client($this->db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if ($customer->fetch($member->fk_soc) <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             throw new CustomerNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $customer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param Facture $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param Bbcvols $flight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     protected function addLinks($object, $flight) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $object->add_object_linked('flightlog_bbcvols', $flight->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param Facture $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param int     $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     protected function validates($object, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $object->fetch($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $object->validate($this->user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     protected function isReferenceHidden() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     protected function isDescriptionHidden() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     protected function isDetailHidden() | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         return (!empty($this->conf->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param CreateReceiverMonthBillCommand $command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param Facture                        $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param int                            $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 133 |  | View Code Duplication |     protected function generateBillDocument(CreateReceiverMonthBillCommand $command, $object, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $object->fetch($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $object->generateDocument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $command->getModelDocument(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->langs, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $this->isDetailHidden(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $this->isDescriptionHidden(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->isReferenceHidden() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param Product $flightProduct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param Bbcvols $flight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return float|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     protected function computeDiscounts($flightProduct, $flight) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return ($flightProduct->price_ttc - ($flight->cost / $flight->nbrPax)) * 100 / $flightProduct->price_ttc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param Facture $facture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @param Product $flightProduct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * @param Bbcvols $flight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     protected function addOrderLine($facture, $flightProduct, $flight) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $localtax1_tx = get_localtax(0, 1, $facture->thirdparty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $localtax2_tx = get_localtax(0, 2, $facture->thirdparty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $pu_ht = price2num($flightProduct->price, 'MU'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $pu_ttc = price2num($flightProduct->price_ttc, 'MU'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $pu_ht_devise = price2num($flightProduct->price, 'MU'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $discount = $this->computeDiscounts($flightProduct, $flight); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         if (!is_numeric($flight->nbrPax)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             throw new \InvalidArgumentException(sprintf('%s is not a number', $flight->nbrPax)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         $result = $facture->addline( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $flight->toString(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $pu_ht, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             $flight->nbrPax, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $flightProduct->tva_tx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $localtax1_tx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $localtax2_tx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $flightProduct->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $discount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $flight->date, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             $flight->date, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             'TTC', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             $pu_ttc, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             Facture::TYPE_STANDARD, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             -1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             $flightProduct->label, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             100, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             $pu_ht_devise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         if ($result <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             throw new \InvalidArgumentException('Error during order line creation'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * @param Bbcvols $flight | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     protected function flagFlightAsBilled($flight) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         $flight->is_facture = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         $flight->update($this->user); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 220 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 221 |  |  | } | 
            
                        
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.