Ejemplo n.º 1
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;
     }
 }