/**
  * Borrar los vencimientos de la factura
  * siempre y cuando no este traspasado a contabilidad (Asiento=0)
  *
  * Por lo tanto, borra los que no están traspasados; y los que si lo están
  * los deja.
  *
  * @return boolean
  */
 public function borraVctos()
 {
     $recibos = new RecibosClientes();
     $recibos->queryDelete("IDFactura='{$this->IDFactura}' and Asiento='0'");
     $ok = count($recibos->getErrores()) == 0;
     unset($recibos);
     return $ok;
 }
 public function RecibosClientes()
 {
     $nItems = 0;
     $nErrores = 0;
     $dbLink = mysql_connect("localhost", "root", "albatronic");
     $query = "TRUNCATE {$this->dbDestino}.ErpRecibosClientes";
     mysql_query($query);
     // Correspondencia entre número de factura e id de factura
     $query = "SELECT r.NumeroFactura as Numero, f.IDFactura as Id FROM {$this->dbOrigen}.recibos_clientes AS r LEFT JOIN {$this->dbOrigen}.femitidas_cab AS f ON r.NumeroFactura = f.NumeroFactura";
     $result = mysql_query($query, $dbLink);
     while ($row1 = mysql_fetch_array($result, MYSQL_ASSOC)) {
         $correspondencia[$row1['Numero']] = $row1['Id'];
     }
     $query = "select * from {$this->dbOrigen}.recibos_clientes";
     $result = mysql_query($query, $dbLink);
     while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         $c = new RecibosClientes();
         $c->setIDRecibo($row['IDRecibo']);
         $c->setRecibo($row['Recibo']);
         $c->setIDSucursal(1);
         $c->setIDFactura($correspondencia[$row['NumeroFactura']]);
         $c->setIDCliente($row['IDCliente']);
         $c->setIDComercial(8);
         $c->setFecha($row['Fecha']);
         $c->setVencimiento($row['Vencimiento']);
         $c->setImporte($row['Importe']);
         $c->setIban(Utils::iban($row['CBanco']));
         $c->setMandato($row['IDCliente']);
         $c->setFechaMandato('2013-01-01');
         $c->setIDRemesa($row['IDRemesa']);
         $c->setCContable($row['CContable']);
         $c->setAsiento($row['Asiento']);
         $c->setConcepto($row['Concepto']);
         if ($row['Estado'] == 'C') {
             $c->setIDEstado(6);
         } else {
             $c->setIDEstado(0);
         }
         $c->setPrimaryKeyMD5(md5($row['IDRecibo']));
         if (!$c->create()) {
             $errores[] = $c->getErrores();
             $nErrores++;
         } else {
             $nItems++;
         }
     }
     //mysql_close($dbLink);
     echo "Recibos clientes creados {$nItems}<br/>";
     if (count($errores)) {
         echo "<pre>";
         print_r($errores);
         echo "</pre>";
     }
 }
 /**
  * Realiza el cambio de cliente en la factura, albaranes y recibos
  */
 private function cambiarCliente()
 {
     $ok = false;
     // Cambiar factura
     $femitidas = new FemitidasCab();
     $filtro = "NumeroFactura='{$this->request['numeroFactura']}' AND IDCliente='{$this->request['idClienteAnterior']}'";
     $okFactura = $femitidas->queryUpdate(array("IDCliente" => $this->request['idClienteNuevo']), $filtro);
     $this->values['errores'] = $femitidas->getErrores();
     if ($okFactura) {
         $this->values['mensaje'][] = "Se ha cambiado " . $okFactura . " factura.";
         // Cambiar albaran/es
         $albaranes = new AlbaranesCab();
         $filtro = "IDFactura='{$this->request['idFactura']}' AND IDCliente='{$this->request['idClienteAnterior']}'";
         $nAlbaranes = $albaranes->queryUpdate(array("IDCliente" => $this->request['idClienteNuevo']), $filtro);
         $this->values['errores'] = $albaranes->getErrores();
         $this->values['mensaje'][] = "Se han cambiado " . $nAlbaranes . " albaranes.";
         // Cambiar recibos
         $recibos = new RecibosClientes();
         $filtro = "IDFactura='{$this->request['idFactura']}' AND IDCliente='{$this->request['idClienteAnterior']}'";
         $nRecibos = $recibos->queryUpdate(array("IDCliente" => $this->request['idClienteNuevo']), $filtro);
         $this->values['errores'] = $recibos->getErrores();
         $this->values['mensaje'][] = "Se han cambiado " . $nRecibos . " recibos.";
     }
 }