Exemplo n.º 1
0
        public void MergePayments(List <ReceiptOrPayment> found)
        {
            // add total, delete all but the latest date
            decimal          totalPayment     = 0;
            decimal          totalReceipt     = 0;
            DateTime         oldestPayment    = new DateTime(0);
            DateTime         oldestReceipt    = new DateTime(0);
            ReceiptOrPayment oldestPaymentObj = null;
            ReceiptOrPayment oldestReceiptObj = null;

            foreach (ReceiptOrPayment receiptOrPayment in found)
            {
                if (receiptOrPayment.Lines == null || receiptOrPayment.Lines.Length != 1)
                {
                    continue;
                }
                if (receiptOrPayment.Date == null)
                {
                    continue;
                }

                TransactionLine transactionLine = receiptOrPayment.Lines [0];
                if (receiptOrPayment.Type == Manager.Model.Enums.ReceiptOrPaymentType.Payment)
                {
                    totalPayment += transactionLine.Amount.Value;
                    if (oldestPayment.CompareTo(receiptOrPayment.Date) < 0)
                    {
                        oldestPayment    = receiptOrPayment.Date;
                        oldestPaymentObj = receiptOrPayment;
                    }
                }
                else if (receiptOrPayment.Type == Manager.Model.Enums.ReceiptOrPaymentType.Receipt)
                {
                    totalReceipt += transactionLine.Amount.Value;
                    if (oldestReceipt.CompareTo(receiptOrPayment.Date) < 0)
                    {
                        oldestReceipt    = receiptOrPayment.Date;
                        oldestReceiptObj = receiptOrPayment;
                    }
                }
                else
                {
                    continue;
                }

                DeleteObject(receiptOrPayment.Key);
            }
            if (oldestPaymentObj != null)
            {
                oldestPaymentObj.Lines [0].Amount = totalPayment;
                InsertObject(oldestPaymentObj);
            }
            if (oldestReceiptObj != null)
            {
                oldestReceiptObj.Lines [0].Amount = totalReceipt;
                InsertObject(oldestReceiptObj);
            }
        }
Exemplo n.º 2
0
        public ImportResult Import(Guid account, dynamic rows, bool execute)
        {
            List <ReceiptOrPayment> existingPayments = GetPayments();
            //List<BankReceipt> existingReceipts=GetReceipts();
            ImportResult importResult = new ImportResult {
                exists = 0, done = 0, failed = 0
            };

            List <Manager.Model.Object> newObjs = new List <Manager.Model.Object>();

            foreach (dynamic row in rows)
            {
                Decimal  amount;
                DateTime dateTime = DateTime.Now;
                if (!Decimal.TryParse(row.Amount, out amount) ||
                    !DateTime.TryParse(row.Date, out dateTime)
                    )
                {
                    ++importResult.failed;
                    continue;
                }
                String description = row.Description;
                if (hasTransaction(account, existingPayments,
                                   dateTime, amount, description) != null)
                {
                    ++importResult.exists;
                    continue;
                }
                Manager.Model.Object mObj;

                /*
                 *              if(amount > 0) {
                 *                      BankReceipt receipt = new BankReceipt() {
                 *                              Date = dateTime,
                 *                              Description = description,
                 *                              BankAccount=account
                 *                      };
                 *                      //receipt.DebitAccount;
                 *                      //receipt.Description;
                 *                      //Amount = 0 - amount;
                 *                      TransactionLine line = new TransactionLine() {
                 *                              Amount = amount
                 *                      };
                 *                      receipt.Lines = new TransactionLine[1] { line };
                 *                      mObj = receipt;
                 *              } else {
                 */
                ReceiptOrPayment payment = new ReceiptOrPayment()
                {
                    Date        = dateTime,
                    BankAccount = account,
                    Description = description
                };
                TransactionLine line = new TransactionLine()
                {
                    Amount = amount
                };
                payment.Lines = new TransactionLine[1] {
                    line
                };
                mObj = payment;

                mObj.Key = Guid.NewGuid();
                newObjs.Add(mObj);
                ++importResult.done;
            }
            PreChangeEvents();
            if (execute)
            {
                foreach (var obj in newObjs)
                {
                    InsertObject(obj);
                }
            }
            return(importResult);
        }