示例#1
0
 public function addInvoiceFromTickets($clientId, $tickets, $month)
 {
     if (!$clientId) {
         return array('success' => false, 'message' => 'Le code client est inconnu sur Dolibarr', 'id' => null);
     }
     $now = new \Datetime();
     $date31 = new \Datetime($month->format('Y-m') . '-01');
     $date31->modify('+1 month');
     $date31->modify('-1 day');
     $invoice = new Invoice();
     $invoice->setCustomerCode($clientId);
     $invoice->setAuthorId(1);
     $invoice->setDate($date31);
     $invoice->setDateDue($date31);
     $invoice->setDateCreated($now);
     $invoice->setType(0);
     usort($tickets, array($this, 'sortTicket'));
     foreach ($tickets as $ticket) {
         $description = '';
         if (strpos($ticket['entity']['name'], 'MARE NOSTRUM >') || strpos($ticket['entity']['name'], 'MALURA')) {
             $description = $ticket['entity']['name'] . '<br/>';
         }
         $invoice_line = new InvoiceLine($invoice);
         $invoice_line->setType(1);
         $invoice_line->setDescription('<em>' . $ticket['solvedate']->format('d-m-Y') . '</em> : ' . $description . $ticket['name'] . '<br/>' . $ticket['content']);
         $invoice_line->setTva($this->tva * 100);
         $invoice_line->setQuantity(number_format($this->formatActionTime($ticket['actiontime']) / 60 / 60, 2));
         $invoice_line->setUnitPrice($this->unit_price);
         $invoice_line->setDateStart($ticket['date']);
         $invoice_line->setDateEnd($ticket['solvedate']);
         $invoice_line->calculTotals();
     }
     $result = $this->dolibarr_invoice_connector->addInvoice($invoice);
     if ($result['result']->result_code == 'OK') {
         return array('success' => true, 'message' => $result['ref'], 'id' => $result['id']);
     } else {
         return array('success' => false, 'message' => $result['result']->result_label, 'id' => null);
     }
 }
 public function billingClient(ClientInterface $client, $tickets, $month, $generate_facture = true)
 {
     $listBillableTicket = array();
     $dateDu = \DateTime::createFromFormat('Y-m-d', date('Y-') . $month . '-01');
     $dateDu->modify('+1 month')->modify('-1 day');
     $invoice = new Invoice();
     $invoice->setCustomerCode($client->getId());
     $invoice->setAuthorId(1);
     $invoice->setType(0);
     $invoice->setDate($dateDu);
     $invoice->setDateDue($dateDu);
     $invoice->setDateCreated(new \Datetime());
     $customer = $this->customerManager->getByCustomerCode($client->getCustomerCode());
     $stat = array();
     //Trie des tickets
     $ticketsTries = array();
     foreach ($tickets as $ticket) {
         $dateLineEnd = new \DateTime($ticket["Changed"]);
         $ticketsTries[$dateLineEnd->format('Y-m-d')] = $ticket;
     }
     ksort($ticketsTries);
     foreach ($ticketsTries as $ticket) {
         if ($ticket) {
             $is_billiable = true;
             if ($ticket['ServiceID']) {
                 $billableService = $this->defaultBillingServiceManager->getByService($ticket['ServiceID']);
                 $is_default_billiable = true;
                 if ($billableService instanceof OTRSDefaultBillingService) {
                     $is_default_billiable = $billableService->getBillable();
                 }
                 // Si le service doit être facturé par défaut
                 if ($is_default_billiable) {
                     if ($customer instanceof Customer) {
                         // Si le client dispose d'un forfait pour ce service
                         if (($suscription = $customer->getSuscription($billableService)) instanceof CustomerSuscription) {
                             // Et que le forfait est en cours
                             if ($suscription->getDateStart() <= new \DateTime() && ($suscription->getEnd() ? $suscription->getEnd()->getDateEnd() > new \DateTime() : true)) {
                                 // On veut des stats dans le mail de récap
                                 if (isset($stat[$suscription->getId()]['time'])) {
                                     $stat[$suscription->getId()]['time'] += $ticket['total_time'];
                                 } else {
                                     $stat[$suscription->getId()] = array('name' => $suscription->getName(), 'time' => $ticket['total_time'], 'max' => $suscription->getMonthlyHour() * 60);
                                 }
                                 $is_billiable = false;
                                 // Cas de MARE NOSTRUM
                                 if ($client->getCustomerCode() == 'CL11036') {
                                     $totalTicketTime = 0;
                                     foreach ($tickets as $t) {
                                         if ($t['ServiceID'] == $ticket['ServiceID']) {
                                             $totalTicketTime += $t['total_time'];
                                         }
                                     }
                                     if ($totalTicketTime > $suscription->getMonthlyHour() * 60) {
                                         $is_billiable = true;
                                         $ticket['info'] = 'MARE_SUP';
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     $is_billiable = false;
                 }
             } else {
                 $is_billiable = false;
             }
             // Si le ticket est facturable, on ajoute une ligne dans la facture
             if ($is_billiable) {
                 $listBillableTicket[] = $ticket;
                 $unit_price = $this->defaultUnitPrice;
                 $round = 15;
                 if ($customer) {
                     if (($hourlyCost = $customer->getHourlyCost($billableService)) instanceof CustomerHourlyCost) {
                         $unit_price = $hourlyCost->getHourlyCost();
                         $round = $hourlyCost->getRound();
                     }
                 }
                 //                    $dateLineStart = new \DateTime($ticket["Created"]);
                 $dateLineEnd = new \DateTime($ticket["Changed"]);
                 $contact = $this->dolibarrService->getContact($ticket["CustomerUserID"]);
                 $_contact = '';
                 if ($contact instanceof ContactInterface) {
                     $_contact = ' - ' . $contact->getFirstname() . ' ' . $contact->getName();
                 }
                 $invoice_line = new InvoiceLine($invoice);
                 $invoice_line->setType(1);
                 $invoice_line->setDescription($dateLineEnd->format('d/m/Y') . $_contact . ' : ' . $ticket['Title']);
                 if ($client->getCountry() == 'Switzerland') {
                     $invoice_line->setTva(0);
                 } else {
                     $invoice_line->setTva($this->tva * 100);
                 }
                 $invoice_line->setQuantity($this->formatActionTime($ticket['total_time'], $round));
                 $invoice_line->setUnitPrice($unit_price);
                 //                    $invoice_line->setDateStart(new \DateTime($ticket["Created"]));
                 //                    $invoice_line->setDateEnd(new \DateTime($ticket["Changed"]));
                 $invoice_line->calculTotals();
                 if ($ticket['info'] == 'MARE_SUP') {
                     $invoice->setNotePublic('Dépassement de forfait');
                 }
             }
         }
     }
     $result = null;
     if ($invoice->getLines()->count() && $generate_facture) {
         $result = $this->dolibarrService->addInvoice($invoice);
     }
     if ($result) {
         if ($result['result']->result_code == 'OK') {
             $result = array('success' => true, 'message' => $result['ref'], 'id' => $result['id']);
         } else {
             $result = array('success' => false, 'message' => $result['result']->result_label, 'id' => null);
         }
     } else {
         $result = array('success' => true, 'message' => null, 'id' => null);
     }
     $result['stat'] = $stat;
     $result['client'] = $client->getName();
     $result['listBillableTicket'] = $listBillableTicket;
     return $result;
 }