예제 #1
0
        // ===================================
        // Lock Sales Invoice (CSV Integrator)
        // ===================================
        public void LockCSVIntegratorSalesInvoice(Int32 id)
        {
            try
            {
                var salesInvoice = from d in db.TrnSalesInvoices where d.Id == id select d;
                if (salesInvoice.Any())
                {
                    Decimal paidAmount       = 0;
                    Decimal adjustmentAmount = 0;

                    var collectionLines = from d in db.TrnCollectionLines where d.SIId == id && d.TrnCollection.IsLocked == true select d;
                    if (collectionLines.Any())
                    {
                        paidAmount = collectionLines.Sum(d => d.Amount);
                    }

                    var journalVoucherLines = from d in db.TrnJournalVoucherLines where d.ARSIId == id && d.TrnJournalVoucher.IsLocked == true select d;
                    if (journalVoucherLines.Any())
                    {
                        Decimal debitAmount  = journalVoucherLines.Sum(d => d.DebitAmount);
                        Decimal creditAmount = journalVoucherLines.Sum(d => d.CreditAmount);

                        adjustmentAmount = debitAmount - creditAmount;
                    }

                    var lockSalesInvoice = salesInvoice.FirstOrDefault();
                    lockSalesInvoice.Amount           = GetSalesInvoiceAmount(id);
                    lockSalesInvoice.PaidAmount       = paidAmount;
                    lockSalesInvoice.AdjustmentAmount = adjustmentAmount;
                    lockSalesInvoice.BalanceAmount    = (GetSalesInvoiceAmount(id) - paidAmount) + adjustmentAmount;
                    lockSalesInvoice.IsLocked         = true;
                    db.SubmitChanges();

                    Business.Inventory inventory = new Business.Inventory();
                    inventory.InsertSalesInvoiceInventory(id);

                    Business.Journal journal = new Business.Journal();
                    journal.InsertSalesInvoiceJournal(id);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
예제 #2
0
        // ==============================
        // Lock Stock In (CSV Integrator)
        // ==============================
        public void LockCSVIntegratorStockIn(Int32 id)
        {
            try
            {
                var stockIn = from d in db.TrnStockIns where d.Id == id select d;
                if (stockIn.Any())
                {
                    var lockStockIn = stockIn.FirstOrDefault();
                    lockStockIn.IsLocked = true;
                    db.SubmitChanges();

                    Business.Journal journal = new Business.Journal();
                    journal.InsertStockInJournal(id);

                    Business.Inventory inventory = new Business.Inventory();
                    inventory.InsertStockInInventory(id);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
        public HttpResponseMessage UnlockStockWithdrawal(String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d;
                if (currentUser.Any())
                {
                    Int32 currentUserId   = currentUser.FirstOrDefault().Id;
                    Int32 currentBranchId = currentUser.FirstOrDefault().BranchId;

                    IQueryable <Data.MstUserForm>        userForms       = from d in db.MstUserForms where d.UserId == currentUserId && d.SysForm.FormName.Equals("StockWithdrawalDetail") select d;
                    IQueryable <Data.TrnStockWithdrawal> stockWithdrawal = from d in db.TrnStockWithdrawals where d.Id == Convert.ToInt32(id) select d;

                    Boolean isValid       = false;
                    String  returnMessage = "";

                    if (!userForms.Any())
                    {
                        returnMessage = "Sorry. You have no access for this stock withdrawal page..";
                    }
                    else if (!userForms.FirstOrDefault().CanUnlock)
                    {
                        returnMessage = "Sorry. You have no rights to unlock stock withdrawal.";
                    }
                    else if (!stockWithdrawal.Any())
                    {
                        returnMessage = "Data not found. These stock withdrawal details are not found in the server.";
                    }
                    else if (!stockWithdrawal.FirstOrDefault().IsLocked)
                    {
                        returnMessage = "Unlocking Error. These stock withdrawal details are already unlocked.";
                    }
                    else
                    {
                        isValid = true;
                    }

                    if (isValid)
                    {
                        var unlockStockWithdrawal = stockWithdrawal.FirstOrDefault();
                        unlockStockWithdrawal.IsLocked        = false;
                        unlockStockWithdrawal.UpdatedById     = currentUserId;
                        unlockStockWithdrawal.UpdatedDateTime = DateTime.Now;
                        db.SubmitChanges();

                        // =====================
                        // Journal and Inventory
                        // =====================
                        Business.Journal   journal   = new Business.Journal();
                        Business.Inventory inventory = new Business.Inventory();

                        if (!unlockStockWithdrawal.IsLocked)
                        {
                            journal.DeleteStockWithdrawalJournal(Convert.ToInt32(id));
                            inventory.DeleteStockWithdrawalInventory(Convert.ToInt32(id));
                        }

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, returnMessage));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
        public HttpResponseMessage LockStockWithdrawal(Entities.TrnStockWithdrawal objStockWithdrawal, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d;
                if (currentUser.Any())
                {
                    Int32 currentUserId   = currentUser.FirstOrDefault().Id;
                    Int32 currentBranchId = currentUser.FirstOrDefault().BranchId;

                    IQueryable <Data.MstUserForm>        userForms       = from d in db.MstUserForms where d.UserId == currentUserId && d.SysForm.FormName.Equals("StockWithdrawalDetail") select d;
                    IQueryable <Data.TrnSalesInvoice>    salesInvoice    = from d in db.TrnSalesInvoices where d.Id == objStockWithdrawal.SIId && d.BranchId == objStockWithdrawal.SIBranchId && d.BalanceAmount > 0 && d.IsLocked == true select d;
                    IQueryable <Data.MstUser>            users           = from d in db.MstUsers.OrderBy(d => d.FullName) where d.IsLocked == true select d;
                    IQueryable <Data.TrnStockWithdrawal> stockWithdrawal = from d in db.TrnStockWithdrawals where d.Id == Convert.ToInt32(id) select d;

                    Boolean isValid       = false;
                    String  returnMessage = "";

                    if (!userForms.Any())
                    {
                        returnMessage = "Sorry. You have no access for this stock withdrawal page.";
                    }
                    else if (!userForms.FirstOrDefault().CanLock)
                    {
                        returnMessage = "Sorry. You have no rights to lock stock withdrawal.";
                    }
                    else if (!salesInvoice.Any())
                    {
                        returnMessage = "No sales invoice found. Please setup more sales invoices.";
                    }
                    else if (!users.Any())
                    {
                        returnMessage = "No user found. Please setup more users.";
                    }
                    else if (!stockWithdrawal.Any())
                    {
                        returnMessage = "Data not found. These stock withdrawal details are not found in the server.";
                    }
                    else if (stockWithdrawal.FirstOrDefault().IsLocked)
                    {
                        returnMessage = "Locking Error. These stock withdrawal details are already locked.";
                    }
                    else
                    {
                        isValid = true;
                    }

                    if (isValid)
                    {
                        var lockStockWithdrawal = stockWithdrawal.FirstOrDefault();
                        lockStockWithdrawal.SWDate            = Convert.ToDateTime(objStockWithdrawal.SWDate);
                        lockStockWithdrawal.SIBranchId        = objStockWithdrawal.SIBranchId;
                        lockStockWithdrawal.SIId              = objStockWithdrawal.SIId;
                        lockStockWithdrawal.Remarks           = objStockWithdrawal.Remarks;
                        lockStockWithdrawal.DocumentReference = objStockWithdrawal.DocumentReference;
                        lockStockWithdrawal.ContactPerson     = objStockWithdrawal.ContactPerson;
                        lockStockWithdrawal.ContactNumber     = objStockWithdrawal.ContactNumber;
                        lockStockWithdrawal.Address           = objStockWithdrawal.Address;
                        lockStockWithdrawal.PreparedById      = objStockWithdrawal.PreparedById;
                        lockStockWithdrawal.CheckedById       = objStockWithdrawal.CheckedById;
                        lockStockWithdrawal.ApprovedById      = objStockWithdrawal.ApprovedById;
                        lockStockWithdrawal.IsLocked          = true;
                        lockStockWithdrawal.UpdatedById       = currentUserId;
                        lockStockWithdrawal.UpdatedDateTime   = DateTime.Now;
                        db.SubmitChanges();

                        if (currentBranchId != objStockWithdrawal.SIBranchId)
                        {
                            // =====================
                            // Journal and Inventory
                            // =====================
                            Business.Journal   journal   = new Business.Journal();
                            Business.Inventory inventory = new Business.Inventory();

                            if (lockStockWithdrawal.IsLocked)
                            {
                                journal.InsertStockWithdrawalJournal(Convert.ToInt32(id));
                                inventory.InsertStockWithdrawalInventory(Convert.ToInt32(id));
                            }
                        }

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, returnMessage));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
        public HttpResponseMessage UnlockStockTransfer(String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("StockTransferDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanUnlock)
                        {
                            var stockTransfer = from d in db.TrnStockTransfers
                                                where d.Id == Convert.ToInt32(id)
                                                select d;

                            if (stockTransfer.Any())
                            {
                                if (stockTransfer.FirstOrDefault().IsLocked)
                                {
                                    String oldObject = at.GetObjectString(stockTransfer.FirstOrDefault());

                                    var unlockStockTransfer = stockTransfer.FirstOrDefault();
                                    unlockStockTransfer.IsLocked        = false;
                                    unlockStockTransfer.UpdatedById     = currentUserId;
                                    unlockStockTransfer.UpdatedDateTime = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Journal and Inventory
                                    // =====================
                                    Business.Journal   journal   = new Business.Journal();
                                    Business.Inventory inventory = new Business.Inventory();

                                    if (!unlockStockTransfer.IsLocked)
                                    {
                                        journal.DeleteStockTransferJournal(Convert.ToInt32(id));
                                        inventory.DeleteStockTransferInventory(Convert.ToInt32(id));
                                    }

                                    String newObject = at.GetObjectString(stockTransfer.FirstOrDefault());
                                    at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Unlocking Error. These stock transfer details are already unlocked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These stock transfer details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to unlock stock transfer."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock transfer page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
예제 #6
0
        public HttpResponseMessage LockStockIn(Entities.TrnStockIn objStockIn, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("StockInDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var stockIn = from d in db.TrnStockIns
                                          where d.Id == Convert.ToInt32(id)
                                          select d;

                            if (stockIn.Any())
                            {
                                if (!stockIn.FirstOrDefault().IsLocked)
                                {
                                    var lockStockIn = stockIn.FirstOrDefault();
                                    lockStockIn.INDate          = Convert.ToDateTime(objStockIn.INDate);
                                    lockStockIn.AccountId       = objStockIn.AccountId;
                                    lockStockIn.ArticleId       = objStockIn.ArticleId;
                                    lockStockIn.Particulars     = objStockIn.Particulars;
                                    lockStockIn.ManualINNumber  = objStockIn.ManualINNumber;
                                    lockStockIn.IsProduced      = objStockIn.IsProduced;
                                    lockStockIn.CheckedById     = objStockIn.CheckedById;
                                    lockStockIn.ApprovedById    = objStockIn.ApprovedById;
                                    lockStockIn.IsLocked        = true;
                                    lockStockIn.UpdatedById     = currentUserId;
                                    lockStockIn.UpdatedDateTime = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Journal and Inventory
                                    // =====================
                                    Business.Journal   journal   = new Business.Journal();
                                    Business.Inventory inventory = new Business.Inventory();

                                    if (lockStockIn.IsLocked)
                                    {
                                        journal.InsertStockInJournal(Convert.ToInt32(id));
                                        inventory.InsertStockInInventory(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These stock in details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These stock in details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock stockIn."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock in page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
예제 #7
0
        public HttpResponseMessage UnlockReceivingReceipt(String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("ReceivingReceiptDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanUnlock)
                        {
                            var receivingReceipt = from d in db.TrnReceivingReceipts
                                                   where d.Id == Convert.ToInt32(id)
                                                   select d;

                            if (receivingReceipt.Any())
                            {
                                if (receivingReceipt.FirstOrDefault().IsLocked)
                                {
                                    var unlockReceivingReceipt = receivingReceipt.FirstOrDefault();
                                    unlockReceivingReceipt.IsLocked        = false;
                                    unlockReceivingReceipt.UpdatedById     = currentUserId;
                                    unlockReceivingReceipt.UpdatedDateTime = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Inventory and Journal
                                    // =====================
                                    Business.Inventory inventory = new Business.Inventory();
                                    Business.Journal   journal   = new Business.Journal();

                                    if (!unlockReceivingReceipt.IsLocked)
                                    {
                                        inventory.DeleteReceivingReceiptInventory(Convert.ToInt32(id));
                                        journal.DeleteReceivingReceiptJournal(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Unlocking Error. These receiving receipt details are already unlocked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to unlock receiving receipt."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this receiving receipt page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
예제 #8
0
        public HttpResponseMessage LockReceivingReceipt(Entities.TrnReceivingReceipt objReceivingReceipt, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("ReceivingReceiptDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var receivingReceipt = from d in db.TrnReceivingReceipts
                                                   where d.Id == Convert.ToInt32(id)
                                                   select d;

                            if (receivingReceipt.Any())
                            {
                                if (!receivingReceipt.FirstOrDefault().IsLocked)
                                {
                                    Decimal paidAmount       = 0;
                                    Decimal adjustmentAmount = 0;

                                    var disbursementLines = from d in db.TrnDisbursementLines
                                                            where d.RRId == Convert.ToInt32(id) &&
                                                            d.TrnDisbursement.IsLocked == true
                                                            select d;

                                    if (disbursementLines.Any())
                                    {
                                        paidAmount = disbursementLines.Sum(d => d.Amount);
                                    }

                                    var journalVoucherLines = from d in db.TrnJournalVoucherLines
                                                              where d.APRRId == Convert.ToInt32(id) &&
                                                              d.TrnJournalVoucher.IsLocked == true
                                                              select d;

                                    if (journalVoucherLines.Any())
                                    {
                                        Decimal debitAmount  = journalVoucherLines.Sum(d => d.DebitAmount);
                                        Decimal creditAmount = journalVoucherLines.Sum(d => d.CreditAmount);

                                        adjustmentAmount = creditAmount - debitAmount;
                                    }

                                    var lockReceivingReceipt = receivingReceipt.FirstOrDefault();
                                    lockReceivingReceipt.RRDate            = Convert.ToDateTime(objReceivingReceipt.RRDate);
                                    lockReceivingReceipt.DocumentReference = objReceivingReceipt.DocumentReference;
                                    lockReceivingReceipt.SupplierId        = objReceivingReceipt.SupplierId;
                                    lockReceivingReceipt.TermId            = objReceivingReceipt.TermId;
                                    lockReceivingReceipt.Remarks           = objReceivingReceipt.Remarks;
                                    lockReceivingReceipt.ManualRRNumber    = objReceivingReceipt.ManualRRNumber;
                                    lockReceivingReceipt.Amount            = GetReceivingReceiptAmount(Convert.ToInt32(id));
                                    lockReceivingReceipt.WTaxAmount        = 0;
                                    lockReceivingReceipt.PaidAmount        = paidAmount;
                                    lockReceivingReceipt.AdjustmentAmount  = adjustmentAmount;
                                    lockReceivingReceipt.BalanceAmount     = (GetReceivingReceiptAmount(Convert.ToInt32(id)) - paidAmount) + adjustmentAmount;
                                    lockReceivingReceipt.ReceivedById      = objReceivingReceipt.ReceivedById;
                                    lockReceivingReceipt.CheckedById       = objReceivingReceipt.CheckedById;
                                    lockReceivingReceipt.ApprovedById      = objReceivingReceipt.ApprovedById;
                                    lockReceivingReceipt.IsLocked          = true;
                                    lockReceivingReceipt.UpdatedById       = currentUserId;
                                    lockReceivingReceipt.UpdatedDateTime   = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Inventory and Journal
                                    // =====================
                                    Business.Inventory inventory = new Business.Inventory();
                                    Business.Journal   journal   = new Business.Journal();

                                    if (lockReceivingReceipt.IsLocked)
                                    {
                                        inventory.InsertReceivingReceiptInventory(Convert.ToInt32(id));
                                        journal.InsertReceivingReceiptJournal(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These receiving receipt details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock receiving receipt."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this receiving receipt page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
예제 #9
0
        public List <Entities.ReturnedDocument> QuintaIntegrationAddSalesInvoice(Entities.RootObject objRoot)
        {
            try
            {
                List <Entities.ReturnedDocument> listSINumber = new List <Entities.ReturnedDocument>();

                var branch = from d in db.MstBranches select d;
                if (branch.Any())
                {
                    Int32 currentBranchId = branch.FirstOrDefault().Id;

                    if (objRoot.TRN.Any())
                    {
                        foreach (var objSales in objRoot.TRN)
                        {
                            if (objSales != null)
                            {
                                Boolean unitExist = false;
                                var     units     = from d in db.MstUnits select d;
                                if (units.Any())
                                {
                                    unitExist = true;
                                }

                                Boolean termExist = false;
                                var     terms     = from d in db.MstTerms where d.Term.Equals(objRoot.DefaultTerm) select d;
                                if (terms.Any())
                                {
                                    termExist = true;
                                }

                                Boolean userExist = false;
                                var     users     = from d in db.MstUsers where d.UserName.Equals("admin") select d;
                                if (users.Any())
                                {
                                    userExist = true;
                                }

                                Int32 customerId = 0;
                                var   customers  = from d in db.MstArticles
                                                   where d.ManualArticleCode.Equals(objSales.ACI) &&
                                                   d.ArticleTypeId == 2
                                                   select d;

                                if (customers.Any())
                                {
                                    customerId = customers.FirstOrDefault().Id;
                                }
                                else
                                {
                                    var defaultCustomerCode = "0000000001";
                                    var lastCustomer        = from d in db.MstArticles.OrderByDescending(d => d.Id)
                                                              where d.ArticleTypeId == 2
                                                              select d;

                                    if (lastCustomer.Any())
                                    {
                                        var customerCode = Convert.ToInt32(lastCustomer.FirstOrDefault().ArticleCode) + 0000000001;
                                        defaultCustomerCode = ZeroFill(customerCode, 10);
                                    }

                                    var articleGroups = from d in db.MstArticleGroups
                                                        where d.ArticleTypeId == 2
                                                        select d;

                                    if (articleGroups.Any())
                                    {
                                        Data.MstArticle newCustomer = new Data.MstArticle
                                        {
                                            ArticleCode       = defaultCustomerCode,
                                            ManualArticleCode = objSales.ACI,
                                            Article           = objSales.ACC,
                                            Category          = "NA",
                                            ArticleTypeId     = 2,
                                            ArticleGroupId    = articleGroups.FirstOrDefault().Id,
                                            AccountId         = articleGroups.FirstOrDefault().AccountId,
                                            SalesAccountId    = articleGroups.FirstOrDefault().SalesAccountId,
                                            CostAccountId     = articleGroups.FirstOrDefault().CostAccountId,
                                            AssetAccountId    = articleGroups.FirstOrDefault().AssetAccountId,
                                            ExpenseAccountId  = articleGroups.FirstOrDefault().ExpenseAccountId,
                                            UnitId            = units.FirstOrDefault().Id,
                                            OutputTaxId       = db.MstTaxTypes.FirstOrDefault().Id,
                                            InputTaxId        = db.MstTaxTypes.FirstOrDefault().Id,
                                            WTaxTypeId        = db.MstTaxTypes.FirstOrDefault().Id,
                                            Price             = 0,
                                            Cost                 = 0,
                                            IsInventory          = false,
                                            Particulars          = "NA",
                                            Address              = "NA",
                                            TermId               = terms.FirstOrDefault().Id,
                                            ContactNumber        = "NA",
                                            ContactPerson        = "NA",
                                            EmailAddress         = "NA",
                                            TaxNumber            = "NA",
                                            CreditLimit          = 0,
                                            DateAcquired         = DateTime.Now,
                                            UsefulLife           = 0,
                                            SalvageValue         = 0,
                                            ManualArticleOldCode = "NA",
                                            Kitting              = 0,
                                            IsLocked             = true,
                                            CreatedById          = users.FirstOrDefault().Id,
                                            CreatedDateTime      = DateTime.Now,
                                            UpdatedById          = users.FirstOrDefault().Id,
                                            UpdatedDateTime      = DateTime.Now
                                        };

                                        db.MstArticles.InsertOnSubmit(newCustomer);
                                        db.SubmitChanges();

                                        customerId = newCustomer.Id;
                                    }
                                }

                                Boolean taxTypeExist = false;
                                var     taxTypes     = from d in db.MstTaxTypes select d;
                                if (taxTypes.Any())
                                {
                                    taxTypeExist = true;
                                }

                                Boolean discountExist = false;
                                var     discounts     = from d in db.MstDiscounts where d.Discount.Equals(objRoot.DefaultDiscount) select d;
                                if (discounts.Any())
                                {
                                    discountExist = true;
                                }

                                if (userExist)
                                {
                                    if (termExist)
                                    {
                                        if (customerId != 0)
                                        {
                                            Int32 salesInvoiceId = 0;
                                            var   salesInvoice   = from d in db.TrnSalesInvoices
                                                                   where d.ManualSINumber.Equals(objSales.FTN)
                                                                   select d;

                                            if (salesInvoice.Any())
                                            {
                                                salesInvoiceId = salesInvoice.FirstOrDefault().Id;

                                                Business.Inventory inventory = new Business.Inventory();
                                                inventory.DeleteSalesInvoiceInventory(Convert.ToInt32(salesInvoiceId));

                                                Business.Journal journal = new Business.Journal();
                                                journal.DeleteSalesInvoiceJournal(Convert.ToInt32(salesInvoiceId));

                                                listSINumber.Add(new Entities.ReturnedDocument()
                                                {
                                                    SINumber       = salesInvoice.FirstOrDefault().SINumber,
                                                    ManualSINumber = salesInvoice.FirstOrDefault().ManualSINumber
                                                });
                                            }
                                            else
                                            {
                                                var SINumberResult   = "0000000001";
                                                var lastSalesInvoice = from d in db.TrnSalesInvoices.OrderByDescending(d => d.Id)
                                                                       where d.BranchId == currentBranchId
                                                                       select d;

                                                if (lastSalesInvoice.Any())
                                                {
                                                    var SINumber = Convert.ToInt32(lastSalesInvoice.FirstOrDefault().SINumber) + 0000000001;
                                                    SINumberResult = ZeroFill(SINumber, 10);
                                                }

                                                Data.TrnSalesInvoice addSalesInvoice = new Data.TrnSalesInvoice
                                                {
                                                    BranchId          = currentBranchId,
                                                    SINumber          = SINumberResult,
                                                    SIDate            = DateTime.Today,
                                                    CustomerId        = customerId,
                                                    TermId            = terms.FirstOrDefault().Id,
                                                    DocumentReference = objSales.FTN,
                                                    ManualSINumber    = objSales.FTN,
                                                    Remarks           = "NA",
                                                    Amount            = objSales.NAM,
                                                    PaidAmount        = 0,
                                                    AdjustmentAmount  = 0,
                                                    BalanceAmount     = objSales.NAM,
                                                    SoldById          = users.FirstOrDefault().Id,
                                                    PreparedById      = users.FirstOrDefault().Id,
                                                    CheckedById       = users.FirstOrDefault().Id,
                                                    ApprovedById      = users.FirstOrDefault().Id,
                                                    IsLocked          = true,
                                                    CreatedById       = users.FirstOrDefault().Id,
                                                    CreatedDateTime   = DateTime.Now,
                                                    UpdatedById       = users.FirstOrDefault().Id,
                                                    UpdatedDateTime   = DateTime.Now
                                                };

                                                db.TrnSalesInvoices.InsertOnSubmit(addSalesInvoice);
                                                db.SubmitChanges();

                                                salesInvoiceId = addSalesInvoice.Id;

                                                listSINumber.Add(new Entities.ReturnedDocument()
                                                {
                                                    SINumber       = addSalesInvoice.SINumber,
                                                    ManualSINumber = addSalesInvoice.ManualSINumber
                                                });
                                            }

                                            if (salesInvoiceId != 0)
                                            {
                                                Int32 itemId = 0;
                                                var   items  = from d in db.MstArticles
                                                               where d.ManualArticleCode.Equals(objSales.SAI) &&
                                                               d.ArticleTypeId == 1
                                                               select d;

                                                if (items.Any())
                                                {
                                                    itemId = items.FirstOrDefault().Id;
                                                }
                                                else
                                                {
                                                    var defaultItemCode = "0000000001";
                                                    var lastItem        = from d in db.MstArticles.OrderByDescending(d => d.Id)
                                                                          where d.ArticleTypeId == 1
                                                                          select d;

                                                    if (lastItem.Any())
                                                    {
                                                        var itemCode = Convert.ToInt32(lastItem.FirstOrDefault().ArticleCode) + 0000000001;
                                                        defaultItemCode = ZeroFill(itemCode, 10);
                                                    }

                                                    var articleGroups = from d in db.MstArticleGroups
                                                                        where d.ArticleTypeId == 1
                                                                        select d;

                                                    if (articleGroups.Any())
                                                    {
                                                        Data.MstArticle newItem = new Data.MstArticle
                                                        {
                                                            ArticleCode       = defaultItemCode,
                                                            ManualArticleCode = objSales.SAI,
                                                            Article           = objSales.SAM,
                                                            Category          = "NA",
                                                            ArticleTypeId     = 1,
                                                            ArticleGroupId    = articleGroups.FirstOrDefault().Id,
                                                            AccountId         = articleGroups.FirstOrDefault().AccountId,
                                                            SalesAccountId    = articleGroups.FirstOrDefault().SalesAccountId,
                                                            CostAccountId     = articleGroups.FirstOrDefault().CostAccountId,
                                                            AssetAccountId    = articleGroups.FirstOrDefault().AssetAccountId,
                                                            ExpenseAccountId  = articleGroups.FirstOrDefault().ExpenseAccountId,
                                                            UnitId            = units.FirstOrDefault().Id,
                                                            OutputTaxId       = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultVatOutput)).FirstOrDefault().Id,
                                                            InputTaxId        = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultVatInput)).FirstOrDefault().Id,
                                                            WTaxTypeId        = taxTypes.Where(d => d.TaxType.Equals(objRoot.DefaultWTax)).FirstOrDefault().Id,
                                                            Price             = objSales.NAM,
                                                            Cost                 = 0,
                                                            IsInventory          = false,
                                                            Particulars          = "NA",
                                                            Address              = "NA",
                                                            TermId               = terms.FirstOrDefault().Id,
                                                            ContactNumber        = "NA",
                                                            ContactPerson        = "NA",
                                                            EmailAddress         = "NA",
                                                            TaxNumber            = "NA",
                                                            CreditLimit          = 0,
                                                            DateAcquired         = DateTime.Now,
                                                            UsefulLife           = 0,
                                                            SalvageValue         = 0,
                                                            ManualArticleOldCode = "NA",
                                                            Kitting              = 0,
                                                            DefaultSupplierId    = null,
                                                            IsLocked             = true,
                                                            CreatedById          = users.FirstOrDefault().Id,
                                                            CreatedDateTime      = DateTime.Now,
                                                            UpdatedById          = users.FirstOrDefault().Id,
                                                            UpdatedDateTime      = DateTime.Now
                                                        };

                                                        db.MstArticles.InsertOnSubmit(newItem);
                                                        db.SubmitChanges();

                                                        itemId = newItem.Id;

                                                        Data.MstArticleUnit newItemUnitConversion = new Data.MstArticleUnit
                                                        {
                                                            ArticleId   = Convert.ToInt32(itemId),
                                                            UnitId      = units.FirstOrDefault().Id,
                                                            Multiplier  = 1,
                                                            IsCountUnit = false
                                                        };

                                                        db.MstArticleUnits.InsertOnSubmit(newItemUnitConversion);
                                                        db.SubmitChanges();

                                                        Data.MstArticlePrice newItemPrice = new Data.MstArticlePrice
                                                        {
                                                            ArticleId        = Convert.ToInt32(itemId),
                                                            PriceDescription = "SRP",
                                                            Price            = objSales.NAM,
                                                            Remarks          = "NA"
                                                        };

                                                        db.MstArticlePrices.InsertOnSubmit(newItemPrice);
                                                        db.SubmitChanges();
                                                    }
                                                }

                                                if (itemId != 0)
                                                {
                                                    if (unitExist)
                                                    {
                                                        if (taxTypeExist)
                                                        {
                                                            if (discountExist)
                                                            {
                                                                Int32?itemInventoryId  = null;
                                                                var   articleInventory = from d in db.MstArticleInventories
                                                                                         where d.BranchId == currentBranchId &&
                                                                                         d.ArticleId == itemId
                                                                                         select d;

                                                                if (articleInventory.Any())
                                                                {
                                                                    itemInventoryId = articleInventory.FirstOrDefault().Id;
                                                                }

                                                                var conversionUnit = from d in db.MstArticleUnits
                                                                                     where d.ArticleId == itemId &&
                                                                                     d.UnitId == items.FirstOrDefault().UnitId
                                                                                     select d;

                                                                if (conversionUnit.Any())
                                                                {
                                                                    Decimal baseQuantity = 1;
                                                                    Decimal basePrice    = objSales.NAM;

                                                                    if (conversionUnit.FirstOrDefault().Multiplier > 0)
                                                                    {
                                                                        baseQuantity = 1 * (1 / conversionUnit.FirstOrDefault().Multiplier);
                                                                    }

                                                                    if (baseQuantity > 0)
                                                                    {
                                                                        basePrice = objSales.NAM / baseQuantity;
                                                                    }

                                                                    Data.TrnSalesInvoiceItem newSalesInvoiceItem = new Data.TrnSalesInvoiceItem
                                                                    {
                                                                        SIId               = salesInvoiceId,
                                                                        ItemId             = itemId,
                                                                        ItemInventoryId    = itemInventoryId,
                                                                        Particulars        = "NA",
                                                                        UnitId             = units.FirstOrDefault().Id,
                                                                        Quantity           = 1,
                                                                        Price              = objSales.NAM,
                                                                        DiscountId         = discounts.FirstOrDefault().Id,
                                                                        DiscountRate       = discounts.FirstOrDefault().DiscountRate,
                                                                        DiscountAmount     = 0,
                                                                        NetPrice           = objSales.NAM,
                                                                        Amount             = objSales.NAM,
                                                                        VATId              = items.FirstOrDefault().OutputTaxId,
                                                                        VATPercentage      = items.FirstOrDefault().MstTaxType.TaxRate,
                                                                        VATAmount          = (objSales.NAM / (1 + (items.FirstOrDefault().MstTaxType.TaxRate / 100))) * (items.FirstOrDefault().MstTaxType.TaxRate / 100),
                                                                        BaseUnitId         = items.FirstOrDefault().UnitId,
                                                                        BaseQuantity       = baseQuantity,
                                                                        BasePrice          = basePrice,
                                                                        SalesItemTimeStamp = DateTime.Now
                                                                    };

                                                                    db.TrnSalesInvoiceItems.InsertOnSubmit(newSalesInvoiceItem);
                                                                    db.SubmitChanges();
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                var currentSalesInvoice = from d in db.TrnSalesInvoices
                                                                          where d.Id == salesInvoiceId
                                                                          select d;

                                                if (currentSalesInvoice.Any())
                                                {
                                                    var salesInvoiceItems = from d in db.TrnSalesInvoiceItems
                                                                            where d.SIId == salesInvoiceId
                                                                            select d;

                                                    Decimal salesAmount      = 0;
                                                    Decimal paidAmount       = currentSalesInvoice.FirstOrDefault().PaidAmount;
                                                    Decimal adjustmentAmount = currentSalesInvoice.FirstOrDefault().AdjustmentAmount;

                                                    if (salesInvoiceItems.Any())
                                                    {
                                                        salesAmount = salesInvoiceItems.Sum(d => d.Amount);
                                                    }

                                                    var updateSalesInvoiceAmount = currentSalesInvoice.FirstOrDefault();
                                                    updateSalesInvoiceAmount.Amount        = salesAmount;
                                                    updateSalesInvoiceAmount.BalanceAmount = (salesAmount - paidAmount) + adjustmentAmount;

                                                    db.SubmitChanges();
                                                }

                                                inventory.InsertSalesInvoiceInventory(Convert.ToInt32(salesInvoiceId));
                                                journal.InsertSalesInvoiceJournal(Convert.ToInt32(salesInvoiceId));

                                                if (currentSalesInvoice.Any())
                                                {
                                                    // ==================
                                                    // Process Collection
                                                    // ==================
                                                    var defaultORNumber = "0000000001";
                                                    var lastCollection  = from d in db.TrnCollections.OrderByDescending(d => d.Id)
                                                                          where d.BranchId == currentBranchId
                                                                          select d;

                                                    if (lastCollection.Any())
                                                    {
                                                        var ORNumber = Convert.ToInt32(lastCollection.FirstOrDefault().ORNumber) + 0000000001;
                                                        defaultORNumber = ZeroFill(ORNumber, 10);
                                                    }

                                                    Data.TrnCollection newCollection = new Data.TrnCollection
                                                    {
                                                        BranchId        = currentBranchId,
                                                        ORNumber        = defaultORNumber,
                                                        ORDate          = DateTime.Today,
                                                        ManualORNumber  = "NA",
                                                        CustomerId      = customers.FirstOrDefault().Id,
                                                        Particulars     = "NA",
                                                        PreparedById    = users.FirstOrDefault().Id,
                                                        CheckedById     = users.FirstOrDefault().Id,
                                                        ApprovedById    = users.FirstOrDefault().Id,
                                                        IsLocked        = false,
                                                        CreatedById     = users.FirstOrDefault().Id,
                                                        CreatedDateTime = DateTime.Now,
                                                        UpdatedById     = users.FirstOrDefault().Id,
                                                        UpdatedDateTime = DateTime.Now
                                                    };

                                                    db.TrnCollections.InsertOnSubmit(newCollection);
                                                    db.SubmitChanges();

                                                    Boolean payTypeExist = false;
                                                    var     payTypes     = from d in db.MstPayTypes
                                                                           select d;

                                                    if (payTypes.Any())
                                                    {
                                                        payTypeExist = true;
                                                    }

                                                    Boolean bankExist = false;
                                                    var     banks     = from d in db.MstArticles
                                                                        where d.ArticleTypeId == 5
                                                                        select d;

                                                    if (banks.Any())
                                                    {
                                                        bankExist = true;
                                                    }

                                                    if (payTypeExist)
                                                    {
                                                        if (bankExist)
                                                        {
                                                            Data.TrnCollectionLine newCollectionLine = new Data.TrnCollectionLine
                                                            {
                                                                ORId             = newCollection.Id,
                                                                BranchId         = currentBranchId,
                                                                AccountId        = currentSalesInvoice.FirstOrDefault().MstArticle.AccountId,
                                                                ArticleId        = currentSalesInvoice.FirstOrDefault().CustomerId,
                                                                SIId             = currentSalesInvoice.FirstOrDefault().Id,
                                                                Particulars      = currentSalesInvoice.FirstOrDefault().Remarks,
                                                                Amount           = objSales.NAM,
                                                                PayTypeId        = payTypes.FirstOrDefault().Id,
                                                                CheckNumber      = "NA",
                                                                CheckDate        = DateTime.Today,
                                                                CheckBank        = "NA",
                                                                DepositoryBankId = banks.FirstOrDefault().Id,
                                                                IsClear          = false
                                                            };

                                                            db.TrnCollectionLines.InsertOnSubmit(newCollectionLine);
                                                            db.SubmitChanges();
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(listSINumber.ToList());
                }
                else
                {
                    return(new List <Entities.ReturnedDocument>());
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(new List <Entities.ReturnedDocument>());
            }
        }
예제 #10
0
        public HttpResponseMessage LockSalesInvoice(Entities.TrnSalesInvoice objSalesInvoice, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("SalesInvoiceDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var salesInvoice = from d in db.TrnSalesInvoices
                                               where d.Id == Convert.ToInt32(id)
                                               select d;

                            if (salesInvoice.Any())
                            {
                                if (!salesInvoice.FirstOrDefault().IsLocked)
                                {
                                    Decimal paidAmount = 0;

                                    var collectionLines = from d in db.TrnCollectionLines
                                                          where d.SIId == Convert.ToInt32(id) &&
                                                          d.TrnCollection.IsLocked == true
                                                          select d;

                                    if (collectionLines.Any())
                                    {
                                        paidAmount = collectionLines.Sum(d => d.Amount);
                                    }

                                    var lockSalesInvoice = salesInvoice.FirstOrDefault();
                                    lockSalesInvoice.SIDate            = Convert.ToDateTime(objSalesInvoice.SIDate);
                                    lockSalesInvoice.CustomerId        = objSalesInvoice.CustomerId;
                                    lockSalesInvoice.TermId            = objSalesInvoice.TermId;
                                    lockSalesInvoice.DocumentReference = objSalesInvoice.DocumentReference;
                                    lockSalesInvoice.ManualSINumber    = objSalesInvoice.ManualSINumber;
                                    lockSalesInvoice.Remarks           = objSalesInvoice.Remarks;
                                    lockSalesInvoice.Amount            = GetSalesInvoiceAmount(Convert.ToInt32(id));
                                    lockSalesInvoice.PaidAmount        = paidAmount;
                                    lockSalesInvoice.AdjustmentAmount  = 0;
                                    lockSalesInvoice.BalanceAmount     = GetSalesInvoiceAmount(Convert.ToInt32(id)) - paidAmount;
                                    lockSalesInvoice.SoldById          = objSalesInvoice.SoldById;
                                    lockSalesInvoice.CheckedById       = objSalesInvoice.CheckedById;
                                    lockSalesInvoice.ApprovedById      = objSalesInvoice.ApprovedById;
                                    lockSalesInvoice.IsLocked          = true;
                                    lockSalesInvoice.UpdatedById       = currentUserId;
                                    lockSalesInvoice.UpdatedDateTime   = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Inventory and Journal
                                    // =====================
                                    Business.Inventory inventory = new Business.Inventory();
                                    Business.Journal   journal   = new Business.Journal();

                                    if (lockSalesInvoice.IsLocked)
                                    {
                                        inventory.InsertSIInventory(Convert.ToInt32(id));
                                        journal.insertSIJournal(Convert.ToInt32(id));
                                    }

                                    // ============================
                                    // Check for Negative Inventory
                                    // ============================
                                    Boolean foundNegativeQuantity = false;
                                    if (salesInvoice.FirstOrDefault().TrnSalesInvoiceItems.Any())
                                    {
                                        foreach (var salesInvoiceItem in salesInvoice.FirstOrDefault().TrnSalesInvoiceItems)
                                        {
                                            if (salesInvoiceItem.MstArticle.IsInventory)
                                            {
                                                var mstArticleInventory = from d in db.MstArticleInventories
                                                                          where d.TrnSalesInvoiceItems.Contains(salesInvoiceItem)
                                                                          select d;

                                                if (mstArticleInventory.Any())
                                                {
                                                    if (salesInvoiceItem.MstArticleInventory.Quantity < 0)
                                                    {
                                                        foundNegativeQuantity = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (foundNegativeQuantity)
                                    {
                                        inventory.deleteSIInventory(Convert.ToInt32(id));
                                        journal.deleteSIJournal(Convert.ToInt32(id));

                                        lockSalesInvoice.IsLocked = false;
                                        db.SubmitChanges();

                                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Negative Inventory Found!"));
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.OK));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These sales invoice details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These sales invoice details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock sales invoice."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this sales invoice page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
예제 #11
0
        public HttpResponseMessage LockStockOut(Entities.TrnStockOut objStockOut, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId       = currentUser.FirstOrDefault().Id;
                    var currentUserBranchId = currentUser.FirstOrDefault().BranchId;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("StockOutDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var stockOut = from d in db.TrnStockOuts
                                           where d.Id == Convert.ToInt32(id)
                                           select d;

                            if (stockOut.Any())
                            {
                                Boolean duplicateManualOTNumber = false;

                                var duplicateManualOTNumberStockOut = from d in db.TrnStockOuts
                                                                      where d.BranchId == currentUserBranchId &&
                                                                      d.IsLocked == true &&
                                                                      d.ManualOTNumber.Equals(objStockOut.ManualOTNumber)
                                                                      select d;

                                if (duplicateManualOTNumberStockOut.Any())
                                {
                                    duplicateManualOTNumber = true;
                                    if (objStockOut.ManualOTNumber.Equals("NA") || objStockOut.ManualOTNumber.Equals("na"))
                                    {
                                        duplicateManualOTNumber = false;
                                    }
                                }

                                if (duplicateManualOTNumber)
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. The OT Ref. Number is already taken."));
                                }
                                else
                                {
                                    if (!stockOut.FirstOrDefault().IsLocked)
                                    {
                                        String oldObject = auditTrail.GetObjectString(stockOut.FirstOrDefault());

                                        var lockStockOut = stockOut.FirstOrDefault();
                                        lockStockOut.OTDate          = Convert.ToDateTime(objStockOut.OTDate);
                                        lockStockOut.AccountId       = objStockOut.AccountId;
                                        lockStockOut.ArticleId       = objStockOut.ArticleId;
                                        lockStockOut.Particulars     = objStockOut.Particulars;
                                        lockStockOut.ManualOTNumber  = objStockOut.ManualOTNumber;
                                        lockStockOut.CheckedById     = objStockOut.CheckedById;
                                        lockStockOut.ApprovedById    = objStockOut.ApprovedById;
                                        lockStockOut.Status          = objStockOut.Status;
                                        lockStockOut.IsLocked        = true;
                                        lockStockOut.UpdatedById     = currentUserId;
                                        lockStockOut.UpdatedDateTime = DateTime.Now;

                                        db.SubmitChanges();

                                        // =====================
                                        // Journal and Inventory
                                        // =====================
                                        Business.Journal   journal   = new Business.Journal();
                                        Business.Inventory inventory = new Business.Inventory();

                                        if (lockStockOut.IsLocked)
                                        {
                                            journal.InsertStockOutJournal(Convert.ToInt32(id));
                                            inventory.InsertStockOutInventory(Convert.ToInt32(id));
                                        }

                                        String newObject = auditTrail.GetObjectString(stockOut.FirstOrDefault());
                                        auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                        return(Request.CreateResponse(HttpStatusCode.OK));
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These stock out details are already locked."));
                                    }
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These stock out details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock stock out."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this stock out page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }