Precisei desenvolver um modulo de cheques, onde o cliente paga com um cheque e esse cheque é atrelado a um ou mais titulos (transações abertas). Quando recebemos do banco que este cheque foi compensado surge a necessidade de dar baixa no cheque e dar baixa nos titulos em aberto.

Segue o código que faz o serviço, o _checkModule nada mais é do que um registro que me traz um recId, de posse desse recId eu faço uma pesquisa na CustTransOpen e pego todos os registros que estão amarrado a este cheque.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
static server boolean processCheckPayment(CAM_CheckModule    _checkModule)
{
    LedgerJournalName       ledgerJournalName;
    LedgerJournalTable      ledgerJournalTable;
    LedgerJournalTrans      ledgerJournalTrans;
    LedgerJournalCheckPost  ledgerJournalCheckPost;
 
    Common                  common;
    NumberSeq               numberseq;
    CustTrans               custTrans;
    CustTransOpen           custTransOpen;
    Specification           specOffsetVoucher;
 
    CAM_Parameters          parameters  = CAM_Parameters::find();
    CustTable               custTable   = CustTable::find(_checkModule.CustAccount);
    boolean                 result      = false;
    InvoiceId               invoiceId;
    ;
 
    while select custTransOpen
      where custTransOpen.RefRecIdCheck == _checkModule.RecId  &&
            custTransOpen.UseRefRecId
    {
        custTrans = custTransOpen.custTrans();
        invoiceId = custTrans.Invoice;
 
        try
        {
            ttsbegin;
 
            select firstonly ledgerJournalName
              where ledgerJournalName.JournalName == parameters.LedgerJournalNameId &&
                    ledgerJournalName.JournalType == LedgerJournalType::CustPayment;
 
            if(!ledgerJournalName.RecId)
                throw error("@CAM184");
 
            if(ledgerJournalName)
            {
                ledgerJournalTable.JournalName  = ledgerJournalName.JournalName;
                ledgerJournalTable.Name         = ledgerJournalName.Name;
                ledgerJournalTable.insert();
 
                if(ledgerJournalTable)
                {
                    numberseq                               = NumberSeq::NewGetVoucherFromCode(ledgerJournalName.VoucherSeries);
                    ledgerJournalTrans.JournalNum           = ledgerJournalTable.JournalNum;
                    ledgerJournalTrans.Voucher              = numberseq.voucher();
                    ledgerJournalTrans.TransDate            = today();
                    ledgerJournalTrans.AccountType          = LedgerJournalACType::Cust;
                    ledgerJournalTrans.Txt                  = CustTable::find(ledgerJournalTrans.AccountNum).Name;
                    ledgerJournalTrans.OffsetAccountType    = ledgerJournalName.OffsetAccountType;
                    ledgerJournalTrans.OffsetAccount        = ledgerJournalName.OffsetAccount;
                    ledgerJournalTrans.Dimension            = ledgerJournalName.Dimension;
                    ledgerJournalTrans.CurrencyCode         = ledgerJournalTable.CurrencyCode;
                    ledgerJournalTrans.ExchRate             = ExchRates::find(ledgerJournalTable.CurrencyCode, datenull(), NoYes::No, NoYes::No).ExchRate;
                    ledgerJournalTrans.TransactionType      = LedgerTransType::Payment;
                    ledgerJournalTrans.PaymMode             = parameters.PaymMode;
                    ledgerJournalTrans.SettleVoucher        = SettlementType::SelectedTransact;
                    ledgerJournalTrans.AccountNum           = custTable.AccountNum;
                    ledgerJournalTrans.AmountCurCredit      = custTransOpen.AmountCur;
                    ledgerJournalTrans.insert();
 
                    select custTransOpen 
                      where custTransOpen.AccountNum == custTable.AccountNum && 
                            custTransOpen.RefRecId == CustTrans::findFromInvoice(invoiceId).RecId;
 
                    if(custTransOpen)
                    {
                        common = ledgerJournalTrans;
                        specOffsetVoucher = Specification_OffsetVoucher::newCommon(common, true);
                        if (!specOffsetVoucher.isMarked(custTransOpen.TableId, custTransOpen.RecId))
                            if (ledgerJournalTrans.RecId)
                                specOffsetVoucher.create(custTransOpen.TableId, custTransOpen.RecId, custTransOpen.AmountCur, ledgerJournalTable.CurrencyCode);
                    }
 
                    ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable, NoYes::Yes);
                    ledgerJournalCheckPost.run();
                    result = true;
                }
            }
            ttscommit;
        }
        catch
        {
            ttsabort;
            result = false;
        }
    }
    return result;
}

Abraços,
Ricardo Pichler