Class ExportService

java.lang.Object
ru.bgcrm.dao.CommonDAO
org.bgerp.plugin.inventoru.sync1c.service.ExportService

public class ExportService extends CommonDAO
Service for managing the export queue and sending write-offs to 1C.
  • Constructor Details

    • ExportService

      public ExportService(Connection con)
  • Method Details

    • enqueue

      public void enqueue(int processId) throws SQLException
      Enqueues a write-off export for the given process. For each reserve movement in the process, looks up the engineer warehouse by store_id and adds a pending entry to the export queue — the stock is written off in ERP only once 1C has the write-off, whether it was sent there (push protocol) or carried by a person (file). The exception is OData: that protocol only reads 1C and can never tell it anything, so there is nothing to wait for — the write-off is applied at once and the entry recorded as 'success'. Duplicate (process_id, instance_id) entries are silently ignored. Does NOT commit — caller is responsible.
      Throws:
      SQLException
    • unmappedPositions

      public String unmappedPositions(int processId, Sync1cConnector instance, int storeId) throws SQLException
      The positions of the write-off 1C has no code for, by name — for a caller that is about to claim 1C holds this write-off (exportQueueConfirm) and must not, while a part of it cannot be named there.
      Returns:
      the listing, or null when every position can be named to 1C.
      Throws:
      SQLException
    • sendExportQueue

      public ExportService.SendResult sendExportQueue(ExportQueue q) throws SQLException
      Sends a queued write-off export to 1C over the HS protocol.
      Parameters:
      q - the queue entry to process
      Throws:
      SQLException
    • buildWriteoffItems

      public List<Api1cClient.WriteoffItem> buildWriteoffItems(int processId, Sync1cConnector instance, int storeId) throws SQLException
      Resolves the write-off items (product_id + quantity) for a process's active reserves from ONE store, protocol-agnostic — used both by the HS sender (sendExportQueue(ExportQueue)) and by the File-protocol CSV export. Scoped to storeId because a process can reserve from more than one store mapped to the same 1C instance — each gets its own queue entry/call.
      Throws:
      SQLException
    • buildWriteoffRows

      public List<ExportService.WriteoffRow> buildWriteoffRows(int processId, Sync1cConnector instance, int storeId) throws SQLException
      Everything a queue entry covers — the CSV for 1C is the exportable part of this, and the screen showing the entry is all of it. Reserves are what an export sends while the write-off in ERP is still to come: it happens only after 1C has the document, sent to it or carried by a person. On OData the ERP wrote the stock off at enqueue time, so the reserves are already gone and the same list has to be read off the write-offs.
      Throws:
      SQLException
    • createWriteoffMovements

      public void createWriteoffMovements(int processId, int storeId) throws SQLException
      Creates writeoff movements in ERP for the active reserves of the process FROM ONE STORE. Called after successful export to 1C so the ERP balance reflects the physical write-off. Updates both quantity and reserved via the "writeoff" balance rule. Scoped to storeId for the same reason as buildWriteoffItems(int, Sync1cConnector, int) — one export queue entry per store. Does NOT commit — caller is responsible.
      Parameters:
      processId - the process whose reserves were written off
      storeId - the store whose reserves this particular export queue entry covered
      Throws:
      SQLException
    • postAccepted

      public ExportService.PostResult postAccepted(ExportQueue q) throws SQLException
      Posts in ERP the write-off of an entry 1C has just accepted, in a transaction of its own: commits on success, and on failure rolls back and marks the entry ExportQueue.STATUS_UNPOSTED (committed). 1C holds the document by now, so a failure must neither leave half of the movements behind nor put the entry back in line: the export poller used to mark such an entry 'failed' and commit, which kept whatever movements were written before the failure and let the next run send the same write-off to 1C a second time. An 'unposted' entry is never sent again; it is posted from the export queue once the cause is fixed. The entry is claimed 'pending' → 'success' BEFORE any movement is written, the same way the file protocol confirms an entry: a process sent back for rework cancels its pending entry, and a cancellation landing between the send and this call used to get the movements written anyway — for reserves that were being released at that very moment. Anything the caller left uncommitted on the connection is committed or rolled back together with the movements, so what the send itself recorded (the warehouse id it used) has to be committed before.
      Throws:
      SQLException
    • reverseWriteoffMovements

      public void reverseWriteoffMovements(int processId) throws SQLException, BGMessageException
      Reverses a previously exported write-off: returns the written-off quantity back to the engineer's store as plain stock (does not re-reserve — the process restarted from scratch, the engineer reserves again if/when they redo the work). Uses "receipt" so only quantity changes, not reserved — a "return" movement would incorrectly touch reserved balance shared with other processes on the same item/store. No negative-balance guard: 1C-side reconciliation of the physical write-off is out of ERP's scope once the process owner decided to reopen the process. Marks the export queue entries as reversed so materials editing unlocks. Does NOT commit — caller is responsible.
      Parameters:
      processId - the process whose write-off is being reversed
      Throws:
      SQLException
      BGMessageException