Ejemplo n.º 1
0
 /**
  * Create
  *
  * @param 	User		$user				User object
  * @param 	int			$id					Id
  * @param 	string		$motif				Motif
  * @param 	int	$date_rejet			Date rejet
  * @param 	int			$bonid				Bon id
  * @param 	int			$facturation		Facturation
  * @return	void
  */
 function create($user, $id, $motif, $date_rejet, $bonid, $facturation = 0)
 {
     global $langs, $conf;
     $error = 0;
     $this->id = $id;
     $this->bon_id = $bonid;
     $now = dol_now();
     dol_syslog("RejetPrelevement::Create id {$id}");
     $bankaccount = $conf->global->PRELEVEMENT_ID_BANKACCOUNT;
     $facs = $this->getListInvoices(1);
     $this->db->begin();
     // Insert refused line into database
     $sql = "INSERT INTO " . MAIN_DB_PREFIX . "prelevement_rejet (";
     $sql .= "fk_prelevement_lignes";
     $sql .= ", date_rejet";
     $sql .= ", motif";
     $sql .= ", fk_user_creation";
     $sql .= ", date_creation";
     $sql .= ", afacturer";
     $sql .= ") VALUES (";
     $sql .= $id;
     $sql .= ", '" . $this->db->idate($date_rejet) . "'";
     $sql .= ", " . $motif;
     $sql .= ", " . $user->id;
     $sql .= ", '" . $this->db->idate($now) . "'";
     $sql .= ", " . $facturation;
     $sql .= ")";
     $result = $this->db->query($sql);
     if (!$result) {
         dol_syslog("RejetPrelevement::create Erreur 4");
         dol_syslog("RejetPrelevement::create Erreur 4 {$sql}");
         $error++;
     }
     // Tag the line to refused
     $sql = " UPDATE " . MAIN_DB_PREFIX . "prelevement_lignes ";
     $sql .= " SET statut = 3";
     $sql .= " WHERE rowid = " . $id;
     if (!$this->db->query($sql)) {
         dol_syslog("RejetPrelevement::create Erreur 5");
         $error++;
     }
     $num = count($facs);
     for ($i = 0; $i < $num; $i++) {
         $fac = new Facture($this->db);
         $fac->fetch($facs[$i][0]);
         // Make a negative payment
         $pai = new Paiement($this->db);
         $pai->amounts = array();
         /*
          * We replace the comma with a point otherwise some
          * PHP installs sends only the part integer negative
          */
         $pai->amounts[$facs[$i][0]] = price2num($facs[$i][1] * -1);
         $pai->datepaye = $date_rejet;
         $pai->paiementid = 3;
         // type of payment: withdrawal
         $pai->num_paiement = $fac->ref;
         if ($pai->create($this->user) < 0) {
             $error++;
             dol_syslog("RejetPrelevement::Create Error creation payment invoice " . $facs[$i][0]);
         } else {
             $result = $pai->addPaymentToBank($user, 'payment', '(InvoiceRefused)', $bankaccount, '', '');
             if ($result < 0) {
                 dol_syslog("RejetPrelevement::Create AddPaymentToBan Error");
                 $error++;
             }
             // Payment validation
             if ($pai->valide() < 0) {
                 $error++;
                 dol_syslog("RejetPrelevement::Create Error payment validation");
             }
         }
         //Tag invoice as unpaid
         dol_syslog("RejetPrelevement::Create set_unpaid fac " . $fac->ref);
         $fac->set_unpaid($user);
         //TODO: Must be managed by notifications module
         // Send email to sender of the standing order request
         $this->_send_email($fac);
     }
     if ($error == 0) {
         dol_syslog("RejetPrelevement::Create Commit");
         $this->db->commit();
     } else {
         dol_syslog("RejetPrelevement::Create Rollback");
         $this->db->rollback();
     }
 }
Ejemplo n.º 2
0
 /**
  *	Check return management
  *	Reopen linked invoices and create a new negative payment.
  *
  *	@param	int		$bank_id 		   Id of bank transaction line concerned
  *	@param	date	$rejection_date    Date to use on the negative payment
  * 	@return	int                        Id of negative payment line created 
  */
 function rejectCheck($bank_id, $rejection_date)
 {
     global $db, $user;
     $payment = new Paiement($db);
     $payment->fetch(0, 0, $bank_id);
     $bankline = new AccountLine($db);
     $bankline->fetch($bank_id);
     /* Conciliation is allowed because when check is returned, a new line is created onto bank transaction log.
     		if ($bankline->rappro)
     		{
                 $this->error='ActionRefusedLineAlreadyConciliated';
     		    return -1;
     		}*/
     $this->db->begin();
     // Not conciliated, we can delete it
     //$bankline->delete($user);    // We delete
     $bankaccount = $payment->fk_account;
     // Get invoices list to reopen them
     $sql = 'SELECT pf.fk_facture, pf.amount';
     $sql .= ' FROM ' . MAIN_DB_PREFIX . 'paiement_facture as pf';
     $sql .= ' WHERE pf.fk_paiement = ' . $payment->id;
     $resql = $db->query($sql);
     if ($resql) {
         $rejectedPayment = new Paiement($db);
         $rejectedPayment->amounts = array();
         $rejectedPayment->datepaye = $rejection_date;
         $rejectedPayment->paiementid = dol_getIdFromCode($this->db, 'CHQ', 'c_paiement');
         $rejectedPayment->num_paiement = $payment->numero;
         while ($obj = $db->fetch_object($resql)) {
             $invoice = new Facture($db);
             $invoice->fetch($obj->fk_facture);
             $invoice->set_unpaid($user);
             $rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1;
         }
         $result = $rejectedPayment->create($user);
         if ($result > 0) {
             // We created a negative payment, we also add the line as bank transaction
             $result = $rejectedPayment->addPaymentToBank($user, 'payment', '(CheckRejected)', $bankaccount, '', '');
             if ($result > 0) {
                 $result = $payment->reject();
                 if ($result > 0) {
                     $this->db->commit();
                     return $rejectedPayment->id;
                 } else {
                     $this->db->rollback();
                     return -1;
                 }
             } else {
                 $this->error = $rejectedPayment->error;
                 $this->errors = $rejectedPayment->errors;
                 $this->db->rollback();
                 return -1;
             }
         } else {
             $this->error = $rejectedPayment->error;
             $this->errors = $rejectedPayment->errors;
             $this->db->rollback();
             return -1;
         }
     } else {
         $this->error = $this->db->lasterror();
         $this->db->rollback();
         return -1;
     }
 }