public void DeleteManualTransaction(TransactionViewModel transaction, int userId)
 {
     Transaction entity = _transactionRepository.GetById(transaction.Id);
     entity.ModifyDate = DateTime.Now;
     entity.ModifyUserId = userId;
     entity.Status = (int)DbConstant.DefaultDataStatus.Deleted;
     _transactionRepository.AttachNavigation<User>(entity.CreateUser);
     _transactionRepository.AttachNavigation<User>(entity.ModifyUser);
     _transactionRepository.AttachNavigation<Reference>(entity.PaymentMethod);
     _transactionRepository.AttachNavigation<Reference>(entity.ReferenceTable);
     _transactionRepository.Update(entity);
     _unitOfWork.SaveChanges();
 }
        public void DeleteCredit(TransactionViewModel transaction, int userID)
        {
            DateTime serverTime = DateTime.Now;

            Transaction transactionEntity = _transactionRepository.GetById(transaction.Id);
            transactionEntity.ModifyDate = serverTime;
            transactionEntity.ModifyUserId = userID;
            transactionEntity.Status = (int)DbConstant.DefaultDataStatus.Deleted;

            Invoice InvoiceEntity = _invoiceRepository.GetById(transaction.PrimaryKeyValue);
            NeutralizeInvoice(ref InvoiceEntity, transactionEntity);

            _transactionRepository.Update(transactionEntity);
            _invoiceRepository.Update(InvoiceEntity);
            _unitOfWork.SaveChanges();
        }
        public void DeleteDebt(TransactionViewModel transaction, int userID)
        {
            DateTime serverTime = DateTime.Now;

            Transaction transactionEntity = _transactionRepository.GetById(transaction.Id);
            transactionEntity.ModifyDate = serverTime;
            transactionEntity.ModifyUserId = userID;
            transactionEntity.Status = (int)DbConstant.DefaultDataStatus.Deleted;

            Purchasing purchasingEntity = _purchasingRepository.GetById(transaction.PrimaryKeyValue);
            NeutralizePurchasing(ref purchasingEntity, transactionEntity);

            _transactionRepository.Update(transactionEntity);
            _purchasingRepository.Update(purchasingEntity);
            _unitOfWork.SaveChanges();
        }
        public void InsertTransaction(TransactionViewModel parentTransaction,
            List<TransactionDetailViewModel> detailTransactionList, int userId)
        {
            Reference refTable = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_TRANSTBL_MANUAL).FirstOrDefault();

            DateTime currentTime = DateTime.Now;
            Transaction parentEntity = new Transaction();
            Map(parentTransaction, parentEntity);
            parentEntity.CreateUserId = userId;
            parentEntity.CreateDate = currentTime;
            parentEntity.ModifyUserId = userId;
            parentEntity.ModifyDate = currentTime;
            parentEntity.ReferenceTableId = refTable.Id;
            parentEntity.PrimaryKeyValue = -1;
            parentEntity.Status = (int)DbConstant.DefaultDataStatus.Active;
            _transactionRepository.AttachNavigation<User>(parentEntity.CreateUser);
            _transactionRepository.AttachNavigation<User>(parentEntity.ModifyUser);
            _transactionRepository.AttachNavigation<Reference>(parentEntity.PaymentMethod);
            _transactionRepository.AttachNavigation<Reference>(parentEntity.ReferenceTable);
            parentEntity = _transactionRepository.Add(parentEntity);
            _unitOfWork.SaveChanges();

            // update primary key value into previous inserted transaction id
            parentEntity = _transactionRepository.GetById(parentEntity.Id);
            parentEntity.PrimaryKeyValue = parentEntity.Id;
            _transactionRepository.Update(parentEntity);
            _unitOfWork.SaveChanges();

            foreach (var detailTransaction in detailTransactionList)
            {
                TransactionDetail detailEntity = new TransactionDetail();
                Map(detailTransaction, detailEntity);
                detailEntity.ParentId = parentEntity.Id;
                _transactionDetailRepository.AttachNavigation<Transaction>(detailEntity.Parent);
                _transactionDetailRepository.AttachNavigation<JournalMaster>(detailEntity.Journal);
                _transactionDetailRepository.Add(detailEntity);
            }

            _unitOfWork.SaveChanges();
        }
        public void InsertDebt(TransactionViewModel transaction, decimal purchasingPrice, int userID)
        {
            DateTime serverTime = DateTime.Now;
            Reference transactionReferenceTable = _referenceRepository.GetMany(c => c.Code == DbConstant.REF_TRANSTBL_PURCHASING).FirstOrDefault();
            transaction.CreateDate = serverTime;
            transaction.ModifyDate = serverTime;
            transaction.CreateUserId = userID;
            transaction.ModifyUserId = userID;
            transaction.TransactionDate = serverTime;
            transaction.TotalTransaction = purchasingPrice.AsDouble();
            transaction.ReferenceTableId = transactionReferenceTable.Id;
            transaction.Status = (int)DbConstant.DefaultDataStatus.Active;
            transaction.Description = "Pembayaran hutang";

            Transaction entity = new Transaction();
            Map(transaction, entity);
            Transaction transactionInserted = _transactionRepository.Add(entity);

            Purchasing purchasingEntity = _purchasingRepository.GetById(transaction.PrimaryKeyValue);
            purchasingEntity.TotalHasPaid += transaction.TotalPayment.AsDecimal();
            if (purchasingEntity.TotalHasPaid == purchasingEntity.TotalPrice)
            {
                purchasingEntity.PaymentStatus = (int)DbConstant.PaymentStatus.Settled;
            }
            _purchasingRepository.Update(purchasingEntity);

            Reference paymentMethod = _referenceRepository.GetById(transaction.PaymentMethodId);

            switch (paymentMethod.Code)
            {
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_EKONOMI:
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA1:
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA2:
                    {
                        // Bank Kredit --> Karena berkurang
                        TransactionDetail detailBank = new TransactionDetail();
                        detailBank.Credit = transaction.TotalPayment.AsDecimal();
                        if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_EKONOMI)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id;
                        }
                        else if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA1)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id;
                        }
                        else if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA2)
                        {
                            detailBank.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id;
                        }
                        detailBank.Parent = transactionInserted;
                        _transactionDetailRepository.Add(detailBank);
                        break;
                    }

                case DbConstant.REF_DEBT_PAYMENTMETHOD_KAS:
                    // Kas Kredit --> Karena berkurang
                    TransactionDetail detailKas = new TransactionDetail();
                    detailKas.Credit = transaction.TotalPayment.AsDecimal();
                    detailKas.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id;
                    detailKas.Parent = transactionInserted;
                    _transactionDetailRepository.Add(detailKas);
                    break;
            }

            TransactionDetail detailDebt = new TransactionDetail();
            detailDebt.Debit = transaction.TotalPayment.AsDecimal();
            detailDebt.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id;
            detailDebt.Parent = transactionInserted;
            _transactionDetailRepository.Add(detailDebt);
            _unitOfWork.SaveChanges();
        }
        public void UpdateDebt(TransactionViewModel transaction, int userID)
        {
            DateTime serverTime = DateTime.Now;

            transaction.ModifyDate = serverTime;
            transaction.CreateUserId = userID;

            Transaction transactionUpdated = _transactionRepository.GetById<int>(transaction.Id);
            Transaction transactionOld = transactionUpdated;

            Purchasing purchasingEntity = _purchasingRepository.GetById(transaction.PrimaryKeyValue);
            NeutralizePurchasing(ref purchasingEntity, transactionOld);

            purchasingEntity.TotalHasPaid += transaction.TotalPayment.AsDecimal();
            if (purchasingEntity.TotalHasPaid == purchasingEntity.TotalPrice)
            {
                purchasingEntity.PaymentStatus = (int)DbConstant.PaymentStatus.Settled;
            }
            _purchasingRepository.Update(purchasingEntity);

            Map(transaction, transactionUpdated);
            _transactionRepository.Update(transactionUpdated);

            Reference paymentMethod = _referenceRepository.GetById(transaction.PaymentMethodId);

            TransactionDetail debitDetail = _transactionDetailRepository.GetMany(x=>x.ParentId == transaction.Id && x.Credit == null).FirstOrDefault();
            TransactionDetail creditDetail = _transactionDetailRepository.GetMany(x => x.ParentId == transaction.Id && x.Debit == null).FirstOrDefault();

            switch (paymentMethod.Code)
            {
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_EKONOMI:
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA1:
                case DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA2:
                    {
                        // Bank Kredit --> Karena berkurang
                        creditDetail.Credit = transaction.TotalPayment.AsDecimal();
                        if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_EKONOMI)
                        {
                            creditDetail.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.01").FirstOrDefault().Id;
                        }
                        else if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA1)
                        {
                            creditDetail.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.02").FirstOrDefault().Id;
                        }
                        else if (paymentMethod.Code == DbConstant.REF_DEBT_PAYMENTMETHOD_BANK_BCA2)
                        {
                            creditDetail.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.02.03").FirstOrDefault().Id;
                        }
                        _transactionDetailRepository.Update(creditDetail);
                        break;
                    }

                case DbConstant.REF_DEBT_PAYMENTMETHOD_KAS:
                    // Kas Kredit --> Karena berkurang
                    creditDetail.Credit = transaction.TotalPayment.AsDecimal();
                    creditDetail.JournalId = _journalMasterRepository.GetMany(j => j.Code == "1.01.01.01").FirstOrDefault().Id;
                    _transactionDetailRepository.Update(creditDetail);
                    break;
            }

            debitDetail.Debit = transaction.TotalPayment.AsDecimal();
            debitDetail.JournalId = _journalMasterRepository.GetMany(j => j.Code == "2.01.01.01").FirstOrDefault().Id;
            _transactionDetailRepository.Update(debitDetail);
            _unitOfWork.SaveChanges();
        }
        public void UpdateTransaction(TransactionViewModel parentTransaction,
            List<TransactionDetailViewModel> detailTransactionList, int userId)
        {
            Transaction parentEntity = _transactionRepository.GetById(parentTransaction.Id);
            Map(parentTransaction, parentEntity);
            parentEntity.ModifyUserId = userId;
            parentEntity.ModifyDate = DateTime.Now;
            _transactionRepository.AttachNavigation<User>(parentEntity.CreateUser);
            _transactionRepository.AttachNavigation<User>(parentEntity.ModifyUser);
            _transactionRepository.AttachNavigation<Reference>(parentEntity.PaymentMethod);
            _transactionRepository.AttachNavigation<Reference>(parentEntity.ReferenceTable);
            _transactionRepository.Update(parentEntity);
            _unitOfWork.SaveChanges();

            foreach (var detailTransaction in detailTransactionList)
            {
                if (detailTransaction.Id > 0)
                {
                    TransactionDetail detailEntity = _transactionDetailRepository.GetById(detailTransaction.Id);
                    Map(detailTransaction, detailEntity);
                    _transactionDetailRepository.AttachNavigation<Transaction>(detailEntity.Parent);
                    _transactionDetailRepository.AttachNavigation<JournalMaster>(detailEntity.Journal);
                    _transactionDetailRepository.Update(detailEntity);
                }
                else
                {
                    TransactionDetail detailEntity = new TransactionDetail();
                    Map(detailTransaction, detailEntity);
                    detailEntity.ParentId = parentEntity.Id;
                    _transactionDetailRepository.AttachNavigation<Transaction>(detailEntity.Parent);
                    _transactionDetailRepository.AttachNavigation<JournalMaster>(detailEntity.Journal);
                    _transactionDetailRepository.Add(detailEntity);
                }
            }
            _unitOfWork.SaveChanges();
        }