Example #1
0
        /// <summary>
        /// Ensure a record matching a doc type exists
        /// </summary>
        void ensureDocTypeExists(DocType enumValue, string nameType, int?primaryAccountid)
        {
            Table   table = TableFor("DocumentType");
            JObject d     = new JObject().AddRange(table.PrimaryKey.Name, (int)enumValue,
                                                   table.Indexes[1].Fields[0].Name, enumValue.UnCamel(),
                                                   "NameType", nameType,
                                                   "Sign", AppModule.SignFor(enumValue),
                                                   "PrimaryAccountId", primaryAccountid);

            updateIfChanged(table, d);
        }
Example #2
0
        /// <summary>
        /// Update a journal line
        /// </summary>
        public override void Update(JObject dataOut)
        {
            if (dataOut["AccountId"] == null)
            {
                return;                                                 // Can't post if no account
            }
            int id = dataOut.AsInt("idDocument");

            if (id == 0)
            {
                return;                                 // Can't post if no document
            }
            bool    newTran = id != _tranId;
            DocType docType = (DocType)dataOut.AsInt("DocumentTypeId");

            if (newTran)
            {
                // New document
                _vat       = 0;
                _vatAmount = 0;
                dataOut["DocumentAddress"] = string.Join("\r\n", Enumerable.Range(1, 5).Select(i => dataOut.AsString("Address" + i)).Where(s => !string.IsNullOrEmpty(s)).ToArray());
                if (!_module.Database.RecordExists("Document", dataOut.AsInt("idDocument")))
                {
                    dataOut["VatPaid"] = 0;
                }
                base.Update(dataOut);                           // Save the document
                _tranId = id;
                _line   = 0;
                // Save the last invoice/cheque/etc. no
                int number = Utils.ExtractNumber(dataOut.AsString("DocumentIdentifier"));
                switch (docType)
                {
                case DocType.Invoice:
                case DocType.CreditMemo:
                    if (number > _lastInvoiceNumber)
                    {
                        _lastInvoiceNumber = number;
                    }
                    break;

                case DocType.Bill:
                case DocType.Credit:
                    if (number > _lastBillNumber)
                    {
                        _lastBillNumber = number;
                    }
                    break;

                case DocType.Withdrawal:
                case DocType.CreditCardCharge:
                    registerNumber(_lastChequeNumber, dataOut.AsInt("AccountId"), number);
                    break;

                case DocType.Deposit:
                case DocType.CreditCardCredit:
                    registerNumber(_lastDepositNumber, dataOut.AsInt("AccountId"), number);
                    break;

                case DocType.GeneralJournal:
                    if (number > _lastJournalNumber)
                    {
                        _lastJournalNumber = number;
                    }
                    break;
                }
                // Delete any existing lines
                _module.Database.Execute("DELETE FROM Line WHERE idLine IN (SELECT idJournal FROM Journal WHERE DocumentId = " + _tranId + ")");
                _module.Database.Execute("DELETE FROM Journal WHERE DocumentId = " + _tranId);
            }
            dataOut["DocumentId"] = _tranId;
            dataOut["JournalNum"] = ++_line;
            dataOut["Memo"]       = dataOut["DocumentMemo"];
            _module.Database.Update("Journal", dataOut);                // Save the journal
            if (dataOut.AsInt("AccountId") == (int)Acct.VATControl)
            {
                // This is the VAT journal
                _vatAmount += dataOut.AsDecimal("Amount");
                if (_vat != 0)
                {
                    // There is already a VAT journal - delete it
                    _module.Database.Execute("DELETE FROM Line WHERE idLine IN (SELECT idJournal FROM Journal WHERE DocumentId = " + _tranId + " AND JournalNum = " + _vat + ")");
                    _module.Database.Execute("DELETE FROM Journal WHERE DocumentId = " + _tranId + " AND JournalNum = " + _vat);
                    _module.Database.Execute("UPDATE Journal SET JournalNum = JournalNum - 1 WHERE DocumentId = " + _tranId + " AND JournalNum > " + _vat);
                    // Bump this journal a line earlier
                    dataOut["JournalNum"] = --_line;
                    dataOut["Amount"]     = _vatAmount;
                    _module.Database.Update("Journal", dataOut);
                }
                _vat = _line;                   // Remember, to avoid 2 VAT lines
            }
            // 2nd and subsequent journals (except VAT journal) have lines
            // NB VAT Payments to HMRC have are exceptions - they have a line for the VAT payment
            if (!newTran && (_line == 2 || dataOut.AsInt("AccountId") != (int)Acct.VATControl))
            {
                int sign = AppModule.SignFor(docType);
                dataOut["idLine"]     = dataOut["idJournal"];
                dataOut["LineAmount"] = sign * dataOut.AsDecimal("Amount");
                dataOut["Qty"]        = sign * dataOut.AsDecimal("Qty");
                dataOut["VatRate"]    = dataOut.AsDecimal("VatRate");
                dataOut["VatAmount"]  = sign * dataOut.AsDecimal("VatAmount");
                _module.Database.Update("Line", dataOut);
            }
        }