| 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->getCustomer($command->getReceiverId())->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $object->type = $command->getBillingType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 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 = $command->getModelDocument(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $object->cond_reglement_id = $command->getBillingCondition(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $object->mode_reglement_id = $command->getBillType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 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($command, $object, $id); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->validates($object, $id); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->generateBillDocument($command, $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 |  |  | } |