//add
        public ActionResult Add(string reason, string bit)
        {
            try
            {
                int localid_  = Convert.ToInt32(TempData.Peek("local"));
                int retailid_ = Convert.ToInt32(TempData.Peek("type"));

                // ViewBag.localid = localid_;
                if (localid_ == 0 || retailid_ == 0)
                {
                    throw new Exception("No items In Table To Return");
                }
                using (linqDBContext db = new linqDBContext())
                {
                    var v = (from a in db.tblSubInventories
                             join c in db.tblItems on a.itemId equals c.id
                             join d in db.issuedRetailers on a.issueRetailId equals d.Id
                             where d.localId == localid_ && d.retailId == retailid_



                             select new { id = a.id, issuedid = a.issueRetailId, itmid = c.id, small = a.small, medium = a.medium, large = a.large, xlarge = a.xLarge });
                    int firstTime = 0;
                    int counter   = 0;

                    foreach (var item in v)
                    {
                        tblSubInventory tsi = (from a in dc.tblSubInventories
                                               where a.itemId == item.itmid && a.issueRetailId == item.issuedid
                                               select a).FirstOrDefault();


                        tblreturnHistoryDetail trd = new tblreturnHistoryDetail();
                        if (firstTime == 0)
                        {
                            var chk = (from a in dc.tblreturnHistoryDetails select a).ToList();
                            if (chk.Count == 0)
                            {
                                counter = 0;
                            }
                            else
                            {
                                counter = chk.Last().RID.Value;
                            }

                            counter++;
                            firstTime = 1;
                        }
                        trd.RID = counter;

                        trd.issueRetailId = item.issuedid;
                        trd.itmId         = item.itmid;
                        trd.small         = item.small;
                        trd.medium        = item.medium;
                        trd.large         = item.large;
                        trd.xlarge        = item.xlarge;
                        trd.date          = DateTimeOffset.Now.Date.ToShortDateString();
                        dc.tblreturnHistoryDetails.Add(trd);
                        dc.SaveChanges();

                        tsi.small  -= item.small;
                        tsi.medium -= item.medium;
                        tsi.large  -= item.large;
                        tsi.xLarge -= item.xlarge;

                        // dc.tblSubInventories.Remove(tsi);
                        dc.SaveChanges();
                    }
                    // now group the ReturnHistorydetail data Category wise and save it ito Returnhistory Table
                    tblReturnHistory Rh;
                    var xx = (from a in dc.tblreturnHistoryDetails
                              where a.RID == counter
                              join b in dc.tblItems on a.itmId equals b.id
                              select new { isu = a.issueRetailId, cat = b.mainCategory, small = a.small, med = a.medium, large = a.large, xlarge = a.xlarge, reason = a.reason }).GroupBy(x => x.cat);


                    foreach (var item in xx)
                    {
                        int getcat = item.First().cat.Value;
                        Rh = new tblReturnHistory();
                        int S = 0, M = 0, L = 0, XL = 0;
                        foreach (var list in item)
                        {
                            S  += (int)list.small;
                            M  += (int)list.med;
                            L  += (int)list.large;
                            XL += (int)list.xlarge;

                            Rh.issueRetailId = list.isu;
                        }

                        Rh.small       = S;
                        Rh.medium      = M;
                        Rh.large       = L;
                        Rh.xLarge      = XL;
                        Rh.category_Id = getcat;
                        Rh.date        = DateTimeOffset.Now.Date;
                        if (bit.Equals("True"))
                        {  // if checkbox is cheked he want to return to main inventory then...
                            Rh.save_to_main_inventory = true;
                            // tblMainInventry tmi;
                            var tmi = (from a in dc.tblMainInventries where a.mainCatId == getcat select a).FirstOrDefault();
                            if (tmi != null)
                            {
                                tmi.small          += S;
                                tmi.medium         += M;
                                tmi.large          += L;
                                tmi.xLarge         += XL;
                                db.Entry(tmi).State = EntityState.Modified;
                                db.SaveChanges();
                            }
                            else
                            {
                                tmi           = new tblMainInventry();
                                tmi.mainCatId = getcat;
                                tmi.small     = S;
                                tmi.medium    = M;
                                tmi.large     = L;
                                tmi.xLarge    = XL;
                                db.tblMainInventries.Add(tmi);
                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            //if checkbox is uncheked then items will save into return inventory
                            Rh.save_to_main_inventory = false;
                            //tblReturnInventory tri;
                            var tri = (from a in dc.tblReturnInventories where a.mainCatId == getcat select a).FirstOrDefault();
                            if (tri != null)
                            {
                                tri.small  += S;
                                tri.medium += M;
                                tri.large  += L;
                                tri.xLarge += XL;
                            }
                            else
                            {
                                tri           = new tblReturnInventory();
                                tri.mainCatId = getcat;
                                tri.small     = S;
                                tri.medium    = M;
                                tri.large     = L;
                                tri.xLarge    = XL;
                                db.tblReturnInventories.Add(tri);
                            }
                        }
                        db.tblReturnHistories.Add(Rh);
                        db.SaveChanges();
                    }



                    return(Json(new JsonResult()
                    {
                        Data = "Success"
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception x)
            {
                return(Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet));
            }
        }
示例#2
0
        public ActionResult add(string retailID_, string localID_, string categoryID_, string pacA, string pacB, string small, string med, string large, string xLarge, string date, string savetomain, string reason)
        {
            tblReturnHistory      rh;
            tblReturnInventory    tri;
            tblMainInventry       tmi;
            tblsubInventoryDetail tsid;

            try
            {
                int retailId   = Convert.ToInt32(retailID_);
                int localId    = Convert.ToInt32(localID_);
                int categoryId = Convert.ToInt32(categoryID_);
                using (linqDBContext db = new linqDBContext())
                {
                    //Check if any item had been issued to this retiler in
                    int get_issuedRetailId = (from a in db.issuedRetailers where a.localId == localId && a.retailId == retailId select a.Id).FirstOrDefault();
                    //check if the returned item already exist in the sub inventory with selected category or not if yes then procced to return
                    var chkSubInvDet = (from a in db.tblsubInventoryDetails
                                        join b in db.tblIssues on a.issueId equals b.id
                                        join c in db.tblLots on b.lotId equals c.id
                                        where c.mainCatId == categoryId select a).FirstOrDefault();

                    if (get_issuedRetailId == 0 || chkSubInvDet == null)
                    {
                        throw new Exception("Sorry , No Item Had Been Issued To This Retailer Or With the Selected Category");
                    }
                    else
                    {
                        int getCatId = (from a in db.tblMainCategories where a.id == categoryId select a.id).FirstOrDefault();

                        var checkReturntable = (from b in db.tblReturnHistories where b.category_Id == getCatId select b).FirstOrDefault();
                        int pacA_            = (Convert.ToInt32(pacA));
                        int pacB_            = (Convert.ToInt32(pacB));
                        int small_           = Convert.ToInt32(small);
                        int medium_          = Convert.ToInt32(med);
                        int large_           = Convert.ToInt32(large);
                        int xlarge_          = Convert.ToInt32(xLarge);

                        // check if category id already exist in return history table if not create new instance of return history
                        if (checkReturntable != null)
                        {
                            rh = checkReturntable;
                        }
                        else
                        {
                            rh = new tblReturnHistory();
                        }
                        rh.issueRetailId = get_issuedRetailId;
                        rh.category_Id   = getCatId;
                        rh.pacA          = pacA_;
                        rh.pacB          = pacB_;
                        rh.small         = small_;
                        rh.medium        = medium_;
                        rh.large         = large_;
                        rh.xLarge        = xlarge_;
                        rh.date          = Convert.ToDateTime(date).Date;
                        rh.reason        = reason;
                        //  rh.date = date;
                        if (savetomain.Equals("False"))
                        {
                            rh.save_to_main_inventory = false;
                            tri = (from a in db.tblReturnInventories where a.mainCatId == categoryId select a).FirstOrDefault();
                            if (tri == null)
                            {
                                tri           = new tblReturnInventory();
                                tri.mainCatId = categoryId;

                                tri.pacA   += pacA_;
                                tri.pacB   += pacB_;
                                tri.small  += small_;
                                tri.medium += medium_;
                                tri.large  += large_;
                                tri.xLarge += xlarge_;
                                db.tblReturnInventories.Add(tri);
                                db.SaveChanges();
                            }
                            else
                            {
                                tri.pacA   += pacA_;
                                tri.pacB   += pacB_;
                                tri.small  += small_;
                                tri.medium += medium_;
                                tri.large  += large_;
                                tri.xLarge += xlarge_;
                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            rh.save_to_main_inventory = true;
                            tmi = (from a in db.tblMainInventries where a.mainCatId == categoryId select a).FirstOrDefault();

                            if (tmi != null)
                            {
                                tmi.pacA   += pacA_;
                                tmi.pacB   += pacB_;
                                tmi.small  += small_;
                                tmi.medium += medium_;
                                tmi.large  += large_;
                                tmi.xLarge += xlarge_;
                                // db.SaveChanges();
                            }
                            else
                            {
                                throw new Exception("Main Inventory Does'nt have this Category ! Don'nt save in Main Inventory");
                            }
                        }
                        // remove values from subInventory details
                        tsid = (from a in db.tblsubInventoryDetails
                                join b in db.tblIssues on a.issueId equals b.id
                                select a).FirstOrDefault();

                        if (tsid == null)
                        {
                            throw new Exception("Item(s) Doesn't Exist is the Sub Inventory ");
                        }
                        else
                        {
                            ///start fromhere ........
                            tsid.packetA -= pacA_;
                            tsid.packetB -= pacB_;
                            tsid.small   -= small_;
                            tsid.medium  -= medium_;
                            tsid.large   -= large_;
                            tsid.xLarge  -= xlarge_;
                        }
                        db.tblReturnHistories.Add(rh);
                        db.SaveChanges();
                    }
                }

                return(Json(new JsonResult()
                {
                    Data = "Done"
                }, JsonRequestBehavior.AllowGet));
            }

            catch (Exception x)
            {
                return(Json(new { status = "error", Data = x.Message }, JsonRequestBehavior.AllowGet));
            }
        }