Ejemplo n.º 1
0
        /// <summary>
        /// When inventory inbound is created, find the inventory item that needs to be fullfilled
        /// and fullfil with item from inventory inbound
        /// </summary>
        /// <param name="db"></param>
        /// <param name="obj"></param>
        internal static void ProcessInventoryInboundCreation(NancyBlackDatabase db, InventoryInbound obj)
        {
            // ensures that only one thread will be doing this
            lock (InventoryAdminModule.LockObject)
            {
                db.Transaction(() =>
                {
                    var inboundItems = new List <InventoryItem>();

                    foreach (var item in obj.Items)
                    {
                        InventoryItem ivitm      = new InventoryItem();
                        ivitm.InboundDate        = obj.InboundDate;
                        ivitm.InventoryInboundId = obj.Id;
                        ivitm.ProductId          = item.ProductId;
                        ivitm.BuyingCost         = item.Price;
                        ivitm.BuyingTax          = item.Tax;

                        db.UpsertRecord(ivitm);

                        inboundItems.Add(ivitm);
                    }

                    InventoryAdminModule.InboundCompleted(db, obj, inboundItems);
                });
            }
        }
Ejemplo n.º 2
0
        internal static void ProcessInboundCompleted(NancyBlackDatabase db, InventoryInbound inbound, List <InventoryItem> items)
        {
            // this already in transaction

            // When inventory inbound is created, record into GL about current asset
            {
                var supplierLookup = db.Query <Supplier>().ToDictionary(s => s.Id);

                // Inbound will create 2 entries
                // 1) inventory increase and account decrease (without tax amount)

                AccountingEntry entry1 = new AccountingEntry();
                entry1.TransactionDate    = inbound.PaymentDate;
                entry1.TransactionType    = "buy";
                entry1.DebtorLoanerName   = supplierLookup[inbound.SupplierId].Name;
                entry1.IncreaseAccount    = "Inventory";
                entry1.IncreaseAmount     = inbound.TotalAmountWithoutTax;
                entry1.DecreaseAccount    = inbound.PaymentAccount;
                entry1.DecreaseAmount     = inbound.TotalAmountWithoutTax * -1;
                entry1.InventoryInboundId = inbound.Id;

                db.UpsertRecord(entry1);

                // 2) paid tax increase and account decrease (tax only amount)
                // (ภาษีซื้อทำให้ภาษีขายที่ต้องจ่ายลดลง)
                if (inbound.TotalTax > 0)
                {
                    AccountingEntry entry2 = new AccountingEntry();
                    entry2.TransactionDate    = inbound.PaymentDate;
                    entry2.TransactionType    = "expense";
                    entry2.DebtorLoanerName   = "Tax";
                    entry2.IncreaseAccount    = "Paid Tax";
                    entry2.IncreaseAmount     = inbound.TotalTax;
                    entry2.DecreaseAccount    = inbound.PaymentAccount;
                    entry2.DecreaseAmount     = inbound.TotalTax * -1;
                    entry2.InventoryInboundId = inbound.Id;

                    db.UpsertRecord(entry2);
                }
            }

            // record that inventory was withdrawn
            {
                var allFullfilled = from item in items
                                    where item.IsFullfilled == true
                                    select item;

                if (allFullfilled.Count() > 0)
                {
                    // the inventory is withdrawn as expense
                    AccountingEntry entry1 = new AccountingEntry();
                    entry1.TransactionDate  = inbound.PaymentDate;
                    entry1.TransactionType  = "expense";
                    entry1.DebtorLoanerName = "Inventory Used";
                    entry1.DecreaseAccount  = "Inventory";
                    entry1.DecreaseAmount   = allFullfilled.Sum(item => item.BuyingCost) * -1;
                    entry1.Notes            = "Inventory Used by Sale Order: " + string.Join(",", allFullfilled.Select(item => item.SaleOrderId)) +
                                              "From Inbound Id:" + inbound.Id;

                    db.UpsertRecord(entry1);

                    // if there is net profit/loss - record it
                    // but does not remove the amount from account
                    var totalAmountBuy  = allFullfilled.Sum(i => i.BuyingCost);
                    var totalAmountSold = allFullfilled.Sum(i => i.SellingPrice);

                    if (totalAmountBuy != totalAmountSold)
                    {
                        AccountingEntry entry2 = new AccountingEntry();
                        entry2.TransactionDate  = inbound.PaymentDate;
                        entry2.TransactionType  = "income";
                        entry2.DebtorLoanerName = "n/a";
                        entry2.IncreaseAccount  = "Gross Profit";
                        entry2.IncreaseAmount   = totalAmountSold - totalAmountBuy;
                        entry2.Notes            = "From Inbound Id:" + inbound.Id + " the item were used. Profit/Loss is calculated and recorded into Profit(Loss) account for each account";

                        db.UpsertRecord(entry2);
                    }
                }
            }
        }