public final int exec(final Object params) throws BasicException {

    Transaction<Integer> t =
        new Transaction<Integer>(m_s) {
          public Integer transact() throws BasicException {
            return execInTransaction(params);
          }
        };

    return t.execute();
  }
  @Override
  public void syncCustomer(final CustomerInfoExt customer) throws BasicException {

    Transaction t =
        new Transaction(s) {
          public Object transact() throws BasicException {
            // Sync the Customer in a transaction

            // Try to update
            if (new PreparedSentence(
                        s,
                        "UPDATE CUSTOMERS SET NAME = ?, ADDRESS = ?, VISIBLE = 1 WHERE ID = ?",
                        new SerializerWrite() {
                          public void writeValues(DataWrite dp, Object obj) throws BasicException {
                            CustomerInfoExt c = (CustomerInfoExt) obj;
                            dp.setString(1, c.getName());
                            dp.setString(2, c.getAddress());
                            dp.setString(3, c.getId());
                          }
                        })
                    .exec(customer)
                == 0) {

              // If not updated, try to insert
              new PreparedSentence(
                      s,
                      "INSERT INTO CUSTOMERS(ID, NAME, ADDRESS, VISIBLE) VALUES (?, ?, ?, 1)",
                      new SerializerWrite() {
                        public void writeValues(DataWrite dp, Object obj) throws BasicException {
                          CustomerInfoExt c = (CustomerInfoExt) obj;
                          dp.setString(1, c.getId());
                          dp.setString(2, c.getName());
                          dp.setString(3, c.getAddress());
                        }
                      })
                  .exec(customer);
            }

            return null;
          }
        };
    t.execute();
  }
Beispiel #3
0
  public final void deleteTicket(final TicketInfo ticket, final String location)
      throws BasicException {

    Transaction t =
        new Transaction(s) {
          public Object transact() throws BasicException {

            // update the inventory
            Date d = new Date();
            for (int i = 0; i < ticket.getLinesCount(); i++) {
              if (ticket.getLine(i).getProductID() != null) {
                // Hay que actualizar el stock si el hay producto
                getStockDiaryInsert()
                    .exec(
                        new Object[] {
                          UUID.randomUUID().toString(),
                          d,
                          ticket.getLine(i).getMultiply() >= 0.0
                              ? MovementReason.IN_REFUND.getKey()
                              : MovementReason.OUT_SALE.getKey(),
                          location,
                          ticket.getLine(i).getProductID(),
                          ticket.getLine(i).getProductAttSetInstId(),
                          new Double(ticket.getLine(i).getMultiply()),
                          new Double(ticket.getLine(i).getPrice())
                        });
              }
            }

            // update customer debts
            for (PaymentInfo p : ticket.getPayments()) {
              if ("debt".equals(p.getName()) || "debtpaid".equals(p.getName())) {

                // udate customer fields...
                ticket.getCustomer().updateCurDebt(-p.getTotal(), ticket.getDate());

                // save customer fields...
                getDebtUpdate()
                    .exec(
                        new DataParams() {
                          public void writeValues() throws BasicException {
                            setDouble(1, ticket.getCustomer().getCurdebt());
                            setTimestamp(2, ticket.getCustomer().getCurdate());
                            setString(3, ticket.getCustomer().getId());
                          }
                        });
              }
            }

            // and delete the receipt
            new StaticSentence(
                    s, "DELETE FROM TAXLINES WHERE RECEIPT = ?", SerializerWriteString.INSTANCE)
                .exec(ticket.getId());
            new StaticSentence(
                    s, "DELETE FROM PAYMENTS WHERE RECEIPT = ?", SerializerWriteString.INSTANCE)
                .exec(ticket.getId());
            new StaticSentence(
                    s, "DELETE FROM TICKETLINES WHERE TICKET = ?", SerializerWriteString.INSTANCE)
                .exec(ticket.getId());
            new StaticSentence(
                    s, "DELETE FROM TICKETS WHERE ID = ?", SerializerWriteString.INSTANCE)
                .exec(ticket.getId());
            new StaticSentence(
                    s, "DELETE FROM RECEIPTS WHERE ID = ?", SerializerWriteString.INSTANCE)
                .exec(ticket.getId());
            return null;
          }
        };
    t.execute();
  }
Beispiel #4
0
  public final void saveTicket(final TicketInfo ticket, final String location)
      throws BasicException {

    Transaction t =
        new Transaction(s) {
          public Object transact() throws BasicException {

            // Set Receipt Id
            if (ticket.getTicketId() == 0) {
              switch (ticket.getTicketType()) {
                case TicketInfo.RECEIPT_NORMAL:
                  ticket.setTicketId(getNextTicketIndex().intValue());
                  break;
                case TicketInfo.RECEIPT_REFUND:
                  ticket.setTicketId(getNextTicketRefundIndex().intValue());
                  break;
                case TicketInfo.RECEIPT_PAYMENT:
                  ticket.setTicketId(getNextTicketPaymentIndex().intValue());
                  break;
                default:
                  throw new BasicException();
              }
            }

            // new receipt
            new PreparedSentence(
                    s,
                    "INSERT INTO RECEIPTS (ID, MONEY, DATENEW, ATTRIBUTES) VALUES (?, ?, ?, ?)",
                    SerializerWriteParams.INSTANCE)
                .exec(
                    new DataParams() {
                      public void writeValues() throws BasicException {
                        setString(1, ticket.getId());
                        setString(2, ticket.getActiveCash());
                        setTimestamp(3, ticket.getDate());
                        try {
                          ByteArrayOutputStream o = new ByteArrayOutputStream();
                          ticket.getProperties().storeToXML(o, AppLocal.APP_NAME, "UTF-8");
                          setBytes(4, o.toByteArray());
                        } catch (IOException e) {
                          setBytes(4, null);
                        }
                      }
                    });

            // new ticket
            new PreparedSentence(
                    s,
                    "INSERT INTO TICKETS (ID, TICKETTYPE, TICKETID, PERSON, CUSTOMER) VALUES (?, ?, ?, ?, ?)",
                    SerializerWriteParams.INSTANCE)
                .exec(
                    new DataParams() {
                      public void writeValues() throws BasicException {
                        setString(1, ticket.getId());
                        setInt(2, ticket.getTicketType());
                        setInt(3, ticket.getTicketId());
                        setString(4, ticket.getUser().getId());
                        setString(5, ticket.getCustomerId());
                      }
                    });

            SentenceExec ticketlineinsert =
                new PreparedSentence(
                    s,
                    "INSERT INTO TICKETLINES (TICKET, LINE, PRODUCT, ATTRIBUTESETINSTANCE_ID, UNITS, PRICE, TAXID, ATTRIBUTES) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                    SerializerWriteBuilder.INSTANCE);

            for (TicketLineInfo l : ticket.getLines()) {
              ticketlineinsert.exec(l);
              if (l.getProductID() != null) {
                // update the stock
                getStockDiaryInsert()
                    .exec(
                        new Object[] {
                          UUID.randomUUID().toString(),
                          ticket.getDate(),
                          l.getMultiply() < 0.0
                              ? MovementReason.IN_REFUND.getKey()
                              : MovementReason.OUT_SALE.getKey(),
                          location,
                          l.getProductID(),
                          l.getProductAttSetInstId(),
                          new Double(-l.getMultiply()),
                          new Double(l.getPrice())
                        });
              }
            }

            SentenceExec paymentinsert =
                new PreparedSentence(
                    s,
                    "INSERT INTO PAYMENTS (ID, RECEIPT, PAYMENT, TOTAL, TRANSID, RETURNMSG) VALUES (?, ?, ?, ?, ?, ?)",
                    SerializerWriteParams.INSTANCE);
            for (final PaymentInfo p : ticket.getPayments()) {
              paymentinsert.exec(
                  new DataParams() {
                    public void writeValues() throws BasicException {
                      setString(1, UUID.randomUUID().toString());
                      setString(2, ticket.getId());
                      setString(3, p.getName());
                      setDouble(4, p.getTotal());
                      setString(5, ticket.getTransactionID());
                      setBytes(6, (byte[]) Formats.BYTEA.parseValue(ticket.getReturnMessage()));
                    }
                  });

              if ("debt".equals(p.getName()) || "debtpaid".equals(p.getName())) {

                // udate customer fields...
                ticket.getCustomer().updateCurDebt(p.getTotal(), ticket.getDate());

                // save customer fields...
                getDebtUpdate()
                    .exec(
                        new DataParams() {
                          public void writeValues() throws BasicException {
                            setDouble(1, ticket.getCustomer().getCurdebt());
                            setTimestamp(2, ticket.getCustomer().getCurdate());
                            setString(3, ticket.getCustomer().getId());
                          }
                        });
              }
            }

            SentenceExec taxlinesinsert =
                new PreparedSentence(
                    s,
                    "INSERT INTO TAXLINES (ID, RECEIPT, TAXID, BASE, AMOUNT)  VALUES (?, ?, ?, ?, ?)",
                    SerializerWriteParams.INSTANCE);
            if (ticket.getTaxes() != null) {
              for (final TicketTaxInfo tickettax : ticket.getTaxes()) {
                taxlinesinsert.exec(
                    new DataParams() {
                      public void writeValues() throws BasicException {
                        setString(1, UUID.randomUUID().toString());
                        setString(2, ticket.getId());
                        setString(3, tickettax.getTaxInfo().getId());
                        setDouble(4, tickettax.getSubTotal());
                        setDouble(5, tickettax.getTax());
                      }
                    });
              }
            }

            return null;
          }
        };
    t.execute();
  }