Esempio n. 1
0
        // accept adjustment request
        // done
        public static bool AcceptRequest(string voucherNo, int empId, string cmt)
        {
            using (SA46Team08ADProjectContext entities = new SA46Team08ADProjectContext())
            {
                try
                {
                    // for email
                    List <AdjustmentVM> adjListEmail = new List <AdjustmentVM>();

                    int toId = 0;
                    List <Adjustment> adjList = entities.Adjustments.Where(a => a.VoucherNo.Equals(voucherNo)).ToList();
                    for (int i = 0; i < adjList.Count; i++)
                    {
                        if (adjList[i].ApproverId == empId)
                        {
                            adjList[i].ApproverComment = cmt;
                            adjList[i].Status          = "Approved";

                            int      adjRaiseEmpId = adjList[i].EmpId;
                            Employee adjRaiseEmp   = entities.Employees.Where(x => x.EmpId == adjRaiseEmpId).FirstOrDefault();
                            if (adjRaiseEmp.Role.Equals("Store Clerk"))
                            {
                                toId = adjRaiseEmpId;
                            }
                            else
                            {
                                toId = entities.Employees.Where(x => x.Role.Equals("Store Clerk")).FirstOrDefault().EmpId;
                            }

                            // for email
                            AdjustmentVM adj = new AdjustmentVM();
                            adj.VoucherNo       = adjList[i].VoucherNo;
                            adj.EmpId           = adjList[i].EmpId;
                            adj.DateTimeIssued  = adjList[i].DateTimeIssued;
                            adj.ItemCode        = adjList[i].ItemCode;
                            adj.Reason          = adjList[i].Reason;
                            adj.QtyChange       = adjList[i].QtyChange;
                            adj.Status          = adjList[i].Status;
                            adj.ApproverId      = (int)adjList[i].ApproverId;
                            adj.ApproverComment = adjList[i].ApproverComment;
                            adjListEmail.Add(adj);

                            string itemCode = adjList[i].ItemCode;
                            Item   item     = entities.Items.Where(x => x.ItemCode.Equals(itemCode)).First();
                            item.Balance += adjList[i].QtyChange;

                            TransactionVM trans = new TransactionVM();
                            trans.TranDateTime = DateTime.Now;
                            trans.ItemCode     = itemCode;
                            trans.QtyChange    = adjList[i].QtyChange;
                            trans.UnitPrice    = (double)item.Price1;
                            trans.Desc         = "Adjustment";
                            trans.DeptCode     = "";
                            trans.SuppCode     = "";
                            trans.VoucherNo    = adjList[i].VoucherNo;
                            TransactionBL.AddTran(trans);

                            bool status = ItemBL.CheckLowStk(ItemUtility.Convert_ItemObj_To_ItemVMObj(item));

                            if (status)
                            {
                                NotificationBL.AddLowStkNotification(empId, item);
                            }
                        }
                    }
                    entities.SaveChanges();

                    int    fromEmpId = empId;
                    int    toEmpId   = toId;
                    string type      = "Adjustment Request";
                    string content   = voucherNo + " has been approved: No comment";

                    NotificationBL.AddNewNotification(fromEmpId, toEmpId, type, content);

                    // for email
                    EmailBL.SendAdjApprEmail(toId, adjListEmail);

                    return(true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }