public Division GetDivision(string divisionName, int year)
        {
            var division = Divisions.FirstOrDefault(x => x.Year == year.ToString() && x.Name.Equals(divisionName, StringComparison.OrdinalIgnoreCase));

            if (division == null)
            {
                return(null);
            }

            return(division);
        }
        private async Task AutoCreateJournalTransaction(PPHBankExpenditureNote model)
        {
            var journalTransactionToPost = new JournalTransaction()
            {
                Date        = model.Date,
                Description = "Bon Terima Unit",
                ReferenceNo = model.No,
                Status      = "POSTED",
                Items       = new List <JournalTransactionItem>()
            };

            int.TryParse(model.BankId, out int bankAccountId);
            var bankAccount = BankAccounts.FirstOrDefault(entity => entity.Id == bankAccountId);

            if (bankAccount == null)
            {
                bankAccount = new BankAccountCOAResult()
                {
                    AccountCOA = "9999.00.00.00"
                };
            }

            int.TryParse(model.IncomeTaxId, out int incomeTaxId);
            var incomeTax = IncomeTaxes.FirstOrDefault(entity => entity.Id == incomeTaxId);

            if (incomeTax == null)
            {
                incomeTax = new IncomeTaxCOAResult()
                {
                    COACodeCredit = "9999.00"
                };
            }

            var journalDebitItems  = new List <JournalTransactionItem>();
            var journalCreditItems = new List <JournalTransactionItem>();

            journalCreditItems.Add(new JournalTransactionItem()
            {
                COA = new COA()
                {
                    Code = bankAccount.AccountCOA
                },
                Credit = (decimal)model.TotalIncomeTax
            });

            var purchasingDocumentExpeditionIds = model.Items.Select(item => item.PurchasingDocumentExpeditionId).ToList();
            var purchasingDocumentExpeditions   = await dbContext.PurchasingDocumentExpeditions.Include(entity => entity.Items).Where(entity => purchasingDocumentExpeditionIds.Contains(entity.Id)).ToListAsync();

            foreach (var item in model.Items)
            {
                var purchasingDocumentExpedition = purchasingDocumentExpeditions.FirstOrDefault(entity => entity.Id == item.PurchasingDocumentExpeditionId);
                var division = Divisions.FirstOrDefault(entity => entity.Code == purchasingDocumentExpedition.DivisionCode);
                if (division == null)
                {
                    division = new IdCOAResult()
                    {
                        COACode = "0"
                    };
                }

                journalDebitItems.Add(new JournalTransactionItem()
                {
                    COA = new COA()
                    {
                        Code = $"{incomeTax.COACodeCredit}.{division.COACode}.00"
                    },
                    Debit = (decimal)purchasingDocumentExpedition.IncomeTax
                });
            }

            journalDebitItems = journalDebitItems.GroupBy(grouping => grouping.COA.Code).Select(s => new JournalTransactionItem()
            {
                COA = new COA()
                {
                    Code = s.Key
                },
                Debit  = s.Sum(sum => Math.Round(sum.Debit.GetValueOrDefault(), 4)),
                Credit = 0,
                //Remark = string.Join("\n", s.Select(grouped => grouped.Remark).ToList())
            }).ToList();
            journalTransactionToPost.Items.AddRange(journalDebitItems);

            journalCreditItems = journalCreditItems.GroupBy(grouping => grouping.COA.Code).Select(s => new JournalTransactionItem()
            {
                COA = new COA()
                {
                    Code = s.Key
                },
                Debit  = 0,
                Credit = s.Sum(sum => Math.Round(sum.Credit.GetValueOrDefault(), 4)),
                //Remark = string.Join("\n", s.Select(grouped => grouped.Remark).ToList())
            }).ToList();
            journalTransactionToPost.Items.AddRange(journalCreditItems);

            if (journalTransactionToPost.Items.Any(item => item.COA.Code.Split(".").FirstOrDefault().Equals("9999")))
            {
                journalTransactionToPost.Status = "DRAFT";
            }

            string journalTransactionUri = "journal-transactions";
            var    httpClient            = (IHttpClientService)_serviceProvider.GetService(typeof(IHttpClientService));
            var    response = await httpClient.PostAsync($"{APIEndpoint.Finance}{journalTransactionUri}", new StringContent(JsonConvert.SerializeObject(journalTransactionToPost).ToString(), Encoding.UTF8, General.JsonMediaType));

            response.EnsureSuccessStatusCode();
        }
示例#3
0
        public async Task <int> AutoJournalVBRequest(VBFormDto form)
        {
            var externalPurchaseOrders     = _dbContext.ExternalPurchaseOrders.Where(entity => form.EPOIds.Contains(entity.Id)).Select(entity => new { entity.Id, entity.IncomeTaxId, entity.UseIncomeTax, entity.IncomeTaxName, entity.IncomeTaxRate, entity.CurrencyCode, entity.IncomeTaxBy, entity.SupplierIsImport }).ToList();
            var externalPurchaseOrderItems = _dbContext.ExternalPurchaseOrderItems.Where(entity => form.EPOIds.Contains(entity.EPOId)).Select(entity => new { entity.Id, entity.EPOId, entity.PRId, entity.UnitId }).ToList();
            var epoItemIds = externalPurchaseOrderItems.Select(element => element.Id).ToList();
            var externalPurchaseOrderDetails = _dbContext.ExternalPurchaseOrderDetails.Where(entity => epoItemIds.Contains(entity.EPOItemId)).Select(entity => new { entity.Id, entity.EPOItemId, entity.DealQuantity, entity.PricePerDealUnit, entity.IncludePpn }).ToList();

            var purchaseRequestIds = externalPurchaseOrderItems.Select(element => element.PRId).ToList();
            var purchaseRequests   = _dbContext.PurchaseRequests.Where(w => purchaseRequestIds.Contains(w.Id)).Select(s => new { s.Id, s.CategoryCode, s.CategoryId, s.UnitId, s.DivisionId }).ToList();

            var journalTransactionToPost = new JournalTransaction()
            {
                Date        = form.Date,
                Description = "Approval VB",
                ReferenceNo = form.DocumentNo,
                Status      = "POSTED",
                Items       = new List <JournalTransactionItem>()
            };

            var journalDebitItems  = new List <JournalTransactionItem>();
            var journalCreditItems = new List <JournalTransactionItem>();

            foreach (var externalPurchaseOrderDetail in externalPurchaseOrderDetails)
            {
                var externalPurchaseOrderItem = externalPurchaseOrderItems.FirstOrDefault(element => element.Id == externalPurchaseOrderDetail.EPOItemId);
                var externalPurchaseOrder     = externalPurchaseOrders.FirstOrDefault(element => element.Id == externalPurchaseOrderItem.EPOId);
                var purchaseRequest           = purchaseRequests.FirstOrDefault(element => element.Id == externalPurchaseOrderItem.PRId);

                int.TryParse(purchaseRequest.CategoryId, out var categoryId);
                var category = Categories.FirstOrDefault(f => f._id.Equals(categoryId));
                if (category == null)
                {
                    category = new CategoryCOAResult()
                    {
                        ImportDebtCOA = "9999.00",
                        LocalDebtCOA  = "9999.00",
                        PurchasingCOA = "9999.00",
                        StockCOA      = "9999.00"
                    };
                }
                else
                {
                    if (string.IsNullOrEmpty(category.ImportDebtCOA))
                    {
                        category.ImportDebtCOA = "9999.00";
                    }
                    if (string.IsNullOrEmpty(category.LocalDebtCOA))
                    {
                        category.LocalDebtCOA = "9999.00";
                    }
                    if (string.IsNullOrEmpty(category.PurchasingCOA))
                    {
                        category.PurchasingCOA = "9999.00";
                    }
                    if (string.IsNullOrEmpty(category.StockCOA))
                    {
                        category.StockCOA = "9999.00";
                    }
                }

                int.TryParse(purchaseRequest.DivisionId, out var divisionId);
                var division = Divisions.FirstOrDefault(f => f.Id.Equals(divisionId));
                if (division == null)
                {
                    division = new IdCOAResult()
                    {
                        COACode = "0"
                    };
                }
                else
                {
                    if (string.IsNullOrEmpty(division.COACode))
                    {
                        division.COACode = "0";
                    }
                }

                int.TryParse(purchaseRequest.UnitId, out var unitId);
                var unit = Units.FirstOrDefault(f => f.Id.Equals(unitId));
                if (unit == null)
                {
                    unit = new IdCOAResult()
                    {
                        COACode = "00"
                    };
                }
                else
                {
                    if (string.IsNullOrEmpty(unit.COACode))
                    {
                        unit.COACode = "0";
                    }
                }

                int.TryParse(externalPurchaseOrder.IncomeTaxId, out var incomeTaxId);
                var incomeTax = IncomeTaxes.FirstOrDefault(f => f.Id.Equals(incomeTaxId));

                if (incomeTax == null || string.IsNullOrWhiteSpace(incomeTax.COACodeCredit))
                {
                    incomeTax = new IncomeTaxCOAResult()
                    {
                        COACodeCredit = "9999.00"
                    };
                }

                double.TryParse(externalPurchaseOrder.IncomeTaxRate, out var incomeTaxRate);

                var currency = await _currencyProvider.GetCurrencyByCurrencyCode(externalPurchaseOrder.CurrencyCode);

                var currencyRate = currency != null?currency.Rate.GetValueOrDefault() : 1;

                var basePrice  = externalPurchaseOrderDetail.PricePerDealUnit * externalPurchaseOrderDetail.DealQuantity * currencyRate;
                var totalPrice = basePrice;

                if (!externalPurchaseOrderDetail.IncludePpn)
                {
                    totalPrice += basePrice * 0.1;
                }

                if (externalPurchaseOrder.UseIncomeTax && externalPurchaseOrder.IncomeTaxBy.ToUpper() == "SUPPLIER")
                {
                    totalPrice -= basePrice * (incomeTaxRate / 100);
                }


                journalDebitItems.Add(new JournalTransactionItem()
                {
                    COA = new COA()
                    {
                        Code = externalPurchaseOrder.SupplierIsImport ? $"{category.ImportDebtCOA}.{division.COACode}.{unit.COACode}" : $"{category.LocalDebtCOA}.{division.COACode}.{unit.COACode}"
                    },
                    Debit = (decimal)totalPrice
                });



                journalCreditItems.Add(new JournalTransactionItem()
                {
                    COA = new COA()
                    {
                        Code = currency.Code.ToUpper() == "IDR" ? $"1011.00.{division.COACode}.{unit.COACode}" : $"1012.00.{division.COACode}.{unit.COACode}"
                    },
                    Credit = (decimal)totalPrice
                });
            }

            journalDebitItems = journalDebitItems.GroupBy(grouping => grouping.COA.Code).Select(s => new JournalTransactionItem()
            {
                COA = new COA()
                {
                    Code = s.Key
                },
                Debit  = s.Sum(sum => Math.Round(sum.Debit.GetValueOrDefault(), 4)),
                Credit = 0
            }).ToList();
            journalTransactionToPost.Items.AddRange(journalDebitItems);

            journalCreditItems = journalCreditItems.GroupBy(grouping => grouping.COA.Code).Select(s => new JournalTransactionItem()
            {
                COA = new COA()
                {
                    Code = s.Key
                },
                Debit  = 0,
                Credit = s.Sum(sum => Math.Round(sum.Credit.GetValueOrDefault(), 4))
            }).ToList();
            journalTransactionToPost.Items.AddRange(journalCreditItems);

            if (journalTransactionToPost.Items.Any(item => item.COA.Code.Split(".").FirstOrDefault().Equals("9999")))
            {
                journalTransactionToPost.Status = "DRAFT";
            }

            var journalTransactionUri = "journal-transactions";
            var httpClient            = _serviceProvider.GetService <IHttpClientService>();
            var response = await httpClient.PostAsync($"{APIEndpoint.Finance}{journalTransactionUri}", new StringContent(JsonConvert.SerializeObject(journalTransactionToPost).ToString(), Encoding.UTF8, General.JsonMediaType));

            return((int)response.StatusCode);
        }