public TicketInfo copyTicket() {
    TicketInfo t = new TicketInfo();

    t.tickettype = tickettype;
    t.m_iTicketId = m_iTicketId;
    t.m_iTicketNCF = m_iTicketNCF; // NCF
    t.m_dDate = m_dDate;
    t.m_sActiveCash = m_sActiveCash;
    t.attributes = (Properties) attributes.clone();
    t.m_User = m_User;
    t.m_Customer = m_Customer;

    t.m_aLines = new ArrayList<TicketLineInfo>();
    for (TicketLineInfo l : m_aLines) {
      t.m_aLines.add(l.copyTicketLine());
    }
    t.refreshLines();

    t.payments = new LinkedList<PaymentInfo>();
    for (PaymentInfo p : payments) {
      t.payments.add(p.copyPayment());
    }

    // taxes are not copied, must be calculated again.

    return t;
  }
  public double getTotalPaid() {

    double sum = 0.0;
    for (PaymentInfo p : payments) {
      if (!"debtpaid".equals(p.getName())) {
        sum += p.getTotal();
      }
    }
    return sum;
  }
  private void btnPayActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnPayActionPerformed

    paymentdialog.setPrintSelected(true);
    // MSL : get total of selected tickets
    double tt = 0;
    for (Object tl : jList1.getSelectedValues()) {

      FindTicketsInfoCustomer sTicket = (FindTicketsInfoCustomer) tl;
      TicketInfo t = new TicketInfo();
      try {
        t = dlsales.loadTicket(0, sTicket.getTicketId());
        tt = tt + t.getTotal(); // - t.getTotalPaid();
      } catch (BasicException e) {

      }
    }
    // -------------

    // if (paymentdialog.showDialog(customerext.getCurdebt(), null)) {
    if (paymentdialog.showDialog(tt, null)) {

      // Save the ticket
      /*TicketInfo ticket = new TicketInfo();
      ticket.setTicketType(TicketInfo.RECEIPT_PAYMENT);

      List<PaymentInfo> payments = paymentdialog.getSelectedPayments();

      double total = 0.0;
      for (PaymentInfo p : payments) {
          total += p.getTotal();
      }

      payments.add(new PaymentInfoTicket(-total, "debtpaid"));

      ticket.setPayments(payments);

      ticket.setUser(app.getAppUserView().getUser().getUserInfo());
      ticket.setActiveCash(app.getActiveCashIndex());
      ticket.setDate(new Date());
      ticket.setCustomer(customerext);

      try {
          dlsales.saveTicket(ticket, app.getInventoryLocation());
      } catch (BasicException eData) {
          MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.nosaveticket"), eData);
          msg.show(this);
      }
       *
       */

      // Save the ticket
      TicketInfo ticket = new TicketInfo();
      ticket.setTicketType(TicketInfo.RECEIPT_PAYMENT);

      List<PaymentInfo> payments = paymentdialog.getSelectedPayments();

      double total = 0.0;
      for (PaymentInfo p : payments) {
        total += p.getTotal();
      }
      double reste = total;

      for (Object tl : jList1.getSelectedValues()) {
        FindTicketsInfoCustomer sTicket = (FindTicketsInfoCustomer) tl;
        TicketInfo t = new TicketInfo();

        if (reste > 0) {
          try {
            t = dlsales.loadTicket(0, sTicket.getTicketId());

            payments.add(new PaymentInfoTicket(-t.getTotal(), "debtpaid"));
            reste = total - t.getTotal();

            ticket.setPayments(payments);
            ticket.setUser(app.getAppUserView().getUser().getUserInfo());
            ticket.setActiveCash(app.getActiveCashIndex());
            ticket.setDate(new Date());
            ticket.setCustomer(customerext);
            dlsales.saveTicket(ticket, app.getInventoryLocation());
            new StaticSentence(
                    this.app.getSession(),
                    "UPDATE TICKETS Set STATUS=9 WHERE ID = ?",
                    SerializerWriteString.INSTANCE)
                .exec(t.getId());

            // MSL : break the system : set the receipt with original one
            // TODO : split the amount paid to the ticket values
            /*new StaticSentence(this.app.getSession()
                        , "UPDATE RECEIPTS Set ID=? WHERE ID = ?"
                        , new SerializerWriteBasic(new Datas[] {Datas.STRING, Datas.STRING}))
                        .exec(new Object[] {t.getId(), ticket.getId()});
            */
            new StaticSentence(
                    this.app.getSession(),
                    "UPDATE PAYMENTS Set TICKETID=? WHERE RECEIPT = ?",
                    new SerializerWriteBasic(new Datas[] {Datas.STRING, Datas.STRING}))
                .exec(new Object[] {t.getId(), ticket.getId()});

          } catch (BasicException eData) {
            MessageInf msg =
                new MessageInf(
                    MessageInf.SGN_NOTICE, AppLocal.getIntString("message.nosaveticket"), eData);
            msg.show(this);
          }
        }
      }

      // MSL : no tickets selected
      if ((jList1.getSelectedValues() == null) || (reste > 0)) {

        payments.add(new PaymentInfoTicket(-reste, "debtpaid"));

        ticket.setPayments(payments);

        ticket.setUser(app.getAppUserView().getUser().getUserInfo());
        ticket.setActiveCash(app.getActiveCashIndex());
        ticket.setDate(new Date());
        ticket.setCustomer(customerext);

        try {
          dlsales.saveTicket(ticket, app.getInventoryLocation());
        } catch (BasicException eData) {
          MessageInf msg =
              new MessageInf(
                  MessageInf.SGN_NOTICE, AppLocal.getIntString("message.nosaveticket"), eData);
          msg.show(this);
        }
      }

      // reload customer
      CustomerInfoExt c;
      try {
        c = dlsales.loadCustomerExt(customerext.getId());
        if (c == null) {
          MessageInf msg =
              new MessageInf(
                  MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotfindcustomer"));
          msg.show(this);
        } else {
          editCustomer(c);
        }
      } catch (BasicException ex) {
        c = null;
        MessageInf msg =
            new MessageInf(
                MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotfindcustomer"), ex);
        msg.show(this);
      }

      printTicket(
          paymentdialog.isPrintSelected() ? "Printer.CustomerPaid" : "Printer.CustomerPaid2",
          ticket,
          c);
    }

    editorcard.reset();
    editorcard.activate();
  } // GEN-LAST:event_btnPayActionPerformed