Example #1
0
        // Approve Department Request
        public ActionResult Ajax_Approve_Department_Request(ajax_model ajax_model_data)
        {
            string quantity_status = "";
            int    stock_level     = 0;
            string order_id_status = "";

            Dictionary <string, int> item_and_quantity_of_department = new Dictionary <string, int>();

            ajax_model ajax_data = new ajax_model
            {
                name      = ajax_model_data.name,
                main_data = ajax_model_data.main_data,
            };

            using (var db = new DataBaseContext())
            {
                List <orders> order_lis = db.orders_repository.Where(or => or.staff_obj.department_obj.department_name == ajax_data.name && or.order_status == "Approved_by_Head").ToList();

                Dictionary <int, int> item_quantity = new Dictionary <int, int>();

                List <items_warehouse> item_ware_lis = db.item_warehouses_repository.ToList();

                foreach (items_warehouse temp_item in item_ware_lis)
                {
                    item_quantity.Add(temp_item.item.itemId, temp_item.stock_balance);
                }

                for (int i = 0; i < order_lis.Count; i++)
                {
                    orders temp_order = order_lis[i];

                    stock_level = item_quantity[temp_order.item_obj.itemId];

                    stock_level = stock_level - temp_order.proposed_quantity;

                    order_id_status = temp_order.ordersId.ToString();

                    // For Stock Card
                    item_and_quantity_of_department.Add(temp_order.item_obj.item_description, temp_order.proposed_quantity);

                    if (stock_level < 0)
                    {
                        quantity_status = "OUT_OF_STOCK";
                        break;
                    }
                    else
                    {
                        item_quantity[temp_order.item_obj.itemId] = stock_level;
                    }
                }

                if (quantity_status != "OUT_OF_STOCK")
                {
                    foreach (KeyValuePair <int, int> data in item_quantity)
                    {
                        items_warehouse item_ware_obj = db.item_warehouses_repository.Where(k => k.item.itemId == data.Key).FirstOrDefault();
                        item_ware_obj.stock_balance = data.Value;
                        db.SaveChanges();
                        quantity_status = "QUANTITY_SUFFICIENT";

                        // Add ACTUAL_QUANTITY and DELIVERY DATE
                        foreach (orders temp_order in order_lis)
                        {
                            temp_order.actual_delivered_quantity_by_clerk = temp_order.proposed_quantity;
                            temp_order.delivered_order_date = DateTime.Now.ToString();
                            db.SaveChanges();
                        }
                    }

                    // For Stock Card
                    foreach (KeyValuePair <string, int> temp_data in item_and_quantity_of_department)
                    {
                        item item_obj = db.item_warehouse_repository.Where(i => i.item_description == temp_data.Key).FirstOrDefault();

                        // STOCK CARD UPDATE
                        int        stockbalance   = StockcardData.GetStockBalanceByItemId(item_obj.itemId);
                        stock_card stock_card_obj = new stock_card(ajax_data.name, DateTime.Now.ToString(), " - " + temp_data.Value, item_obj, stockbalance - temp_data.Value);
                        db.stock_card_repository.Add(stock_card_obj);
                        db.SaveChanges();
                    }

                    foreach (orders temp_order in order_lis)
                    {
                        temp_order.order_status = "Approved_by_Clerk";
                        db.SaveChanges();
                    }
                }
            }
            object reply_to_client = new
            {
                item_quantity_status  = quantity_status,
                stock_level_status    = stock_level,
                order_identity_status = order_id_status,
            };

            //Email Notification
            staff  rep      = StaffData.GetStaffByName(DepartmentData.GetRepresentativebyDepName(ajax_data.name));
            string emailadd = rep.email;
            Task   task     = Task.Run(() => {
                EmailNotification.SendNotificationEmailToEmployee(emailadd, "New Disbursment Order Reminder", "There is a new disbursment order to your department was just approved by store clerk. please get ready to receive it.");
            });


            return(Json(reply_to_client, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public JsonResult Ajax_Purchase_Order(ajax_model ajax_data)
        {
            ajax_model data = new ajax_model
            {
                name      = ajax_data.name,
                main_data = ajax_data.main_data,
            };
            Dictionary <string, int> supplier_and_quantity;
            string supplier_status = "NOT_OUT_OF_STOCK_IN_SUPPLIER";

            using (var db = new DataBaseContext())
            {
                int temp_item_ware_id = Int32.Parse(data.name);

                items_warehouse item_ware_obj = db.item_warehouses_repository.Where(it => it.items_warehouseId == temp_item_ware_id).FirstOrDefault();

                supplier_and_quantity = SEND_ORDER_TO_SUPPLIERS(item_ware_obj);

                // Add Total Quantity From Suppliers
                foreach (KeyValuePair <string, int> temp_data in supplier_and_quantity)
                {
                    item_ware_obj.stock_balance = item_ware_obj.stock_balance + temp_data.Value;
                    suppliers sup_obj = db.suppliers_repository.Where(s => s.name == temp_data.Key).FirstOrDefault();

                    if (temp_data.Key == "OUT_OF_STOCK_IN_SUPPLIER")
                    {
                        supplier_status = "OUT_OF_STOCK_IN_SUPPLIER";
                    }
                    else if (sup_obj.suppliersId == 1)
                    {
                        item_ware_obj.first_supplier_balance = item_ware_obj.first_supplier_balance + temp_data.Value;
                        db.SaveChanges();

                        // STOCK CARD UPDATE
                        int        stockbalance   = StockcardData.GetStockBalanceByItemId(item_ware_obj.item.itemId);
                        stock_card stock_card_obj = new stock_card(sup_obj.name, DateTime.Now.ToString(), "+ " + temp_data.Value, item_ware_obj.item, stockbalance + temp_data.Value);
                        db.stock_card_repository.Add(stock_card_obj);
                        db.SaveChanges();
                    }
                    else if (sup_obj.suppliersId == 2)
                    {
                        item_ware_obj.second_supplier_balance = item_ware_obj.second_supplier_balance + temp_data.Value;
                        db.SaveChanges();

                        // STOCK CARD UPDATE
                        int        stockbalance   = StockcardData.GetStockBalanceByItemId(item_ware_obj.item.itemId);
                        stock_card stock_card_obj = new stock_card(sup_obj.name, DateTime.Now.ToString(), "+ " + temp_data.Value, item_ware_obj.item, stockbalance + temp_data.Value);
                        db.stock_card_repository.Add(stock_card_obj);
                        db.SaveChanges();
                    }
                    else if (sup_obj.suppliersId == 3)
                    {
                        item_ware_obj.third_supplier_balance = item_ware_obj.third_supplier_balance + temp_data.Value;
                        db.SaveChanges();

                        // STOCK CARD UPDATE
                        int        stockbalance   = StockcardData.GetStockBalanceByItemId(item_ware_obj.item.itemId);
                        stock_card stock_card_obj = new stock_card(sup_obj.name, DateTime.Now.ToString(), "+ " + temp_data.Value, item_ware_obj.item, stockbalance + temp_data.Value);
                        db.stock_card_repository.Add(stock_card_obj);
                        db.SaveChanges();
                    }
                }
            }
            object reply_to_client = new
            {
                supplier_and_quantity_key    = supplier_and_quantity,
                supplier_out_of_stock_status = supplier_status,
            };

            return(Json(reply_to_client, JsonRequestBehavior.AllowGet));
        }
        /////////////////////////////////////////////////////////Trend Report
        public async Task <ActionResult> View_Trend_Analysis_Detail(int itemId, int?depId)
        {
            List <orders> listorders = new List <orders>();

            listorders = OrdersData.GetAllDeliveredOrders();

            int           dId         = depId ?? 0;
            List <orders> listorders1 = new List <orders>();

            if (dId != 0)
            {
                listorders1    = listorders.Where(x => x.staff_obj.department_obj.departmentId == dId).ToList();
                ViewBag.depart = DepartmentData.GetDepartmentById(dId);
            }
            else
            {
                listorders1 = listorders;
            }


            Dictionary <string, int> itemsbtrend = new Dictionary <string, int>();
            Dictionary <string, int> itemtrend   = new Dictionary <string, int>();



            Dictionary <int, Dictionary <string, int> > trendlist = (Dictionary <int, Dictionary <string, int> >)Session["trendlist"];
            item          item    = ItemData.GetItemById(itemId); // get the item by given itemId
            List <string> monlist = new List <string>();


            for (int i = 11; i >= 0; i--)
            {
                //string dt = string.Format("{0}/{1}", DateTime.Today.AddMonths(-i).Month, DateTime.Today.AddMonths(-i).Year);
                string dt = DateTime.Today.AddMonths(-i).ToString("MM/yyyy");
                monlist.Add(dt);//list of last 12 months
                /////////////////////////////////////////////////////////////////////////
                //Get stockbalance on given month, put in dictionary "itemsbtrend", key is month string, value is stockbalance of the month.
                //////////////////////////////////////////////////////////////////

                char[] separator = { '/', ' ' };



                List <stock_card> list = StockcardData.GetStockCardByItemId(itemId);//give proper value here.


                List <stock_card> mlist = list.Where(x => x.Arrival_Date.Split(separator)[0].Equals(dt.Split(separator)[0]) && x.Arrival_Date.Split(separator)[2].Equals(dt.Split(separator)[1])).ToList();
                int stockbalance        = mlist[mlist.Count - 1].stockbalance;
                itemsbtrend.Add(dt, stockbalance);
            }

            int[] cons = trendlist[itemId].Values.ToArray();


            foreach (string m in monlist)
            {
                char[]   separator   = { '/', ' ' };
                string[] dtmonthyear = m.Split(separator);
                //Get monthly consumption quant on given month
                int consumedquant = listorders1.Where(x => x.delivered_order_date.Split(separator)[0].Equals(dtmonthyear[0]) && x.delivered_order_date.Split(separator)[2].Equals(dtmonthyear[1])).Select(x => x.actual_received_quantity_by_representative).Sum();//Get correct value here
                itemtrend.Add(m, consumedquant);
            }


            // send request and get response from python server
            int[] prelist  = new int[4];
            int[] paralist = new int[13];
            paralist[0] = itemId;
            for (int i = 1; i < 13; i++)
            {
                paralist[i] = cons[i - 1];
            }
            string conshist = string.Join(", ", paralist);


            using (var client = new HttpClient())
            {
                string xValue = conshist;


                // send a GET request to the server uri with the data and get the response as HttpResponseMessage object
                HttpResponseMessage res = await client.GetAsync("http://127.0.0.1:5000/model1?x=" + xValue);

                // Return the result from the server if the status code is 200 (everything is OK)
                // should raise exception or error if it's not
                if (res.IsSuccessStatusCode)
                {
                    // pass the result to update the user preference
                    // have to read as string for the data in response.body
                    //pre = Convert.ToInt32(res.Content.ReadAsStringAsync().Result); //if only display one month prediction.
                    string arr = res.Content.ReadAsStringAsync().Result;
                    prelist = arr.Split(',').Select(str => int.Parse(str)).ToArray();
                }
                else
                {
                    prelist = new int[4] {
                        0, 0, 0, 0
                    }
                };
            }

            items_warehouse iwh = ItemWarehouseData.FindByItemId(itemId);

            ViewBag.departList    = DepartmentData.GetAllDep();
            ViewBag.cons          = itemtrend.Values.ToArray();
            ViewBag.months        = monlist.ToArray();
            ViewBag.Item          = item;
            ViewBag.sbalance      = itemsbtrend.Values.ToArray();
            ViewBag.prediction    = prelist;
            ViewBag.itemwarehouse = iwh;
            return(View());
        }