//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 itemCode, string pacA, string pacB, string small, string medium, string large, string xlarge)
        {
            try
            {
                using (linqDBContext db = new linqDBContext())
                {
                    int pacA_   = (Convert.ToInt32(pacA));
                    int pacB_   = (Convert.ToInt32(pacB));
                    int small_  = Convert.ToInt32(small);
                    int medium_ = Convert.ToInt32(medium);
                    int large_  = Convert.ToInt32(large);
                    int xlarge_ = Convert.ToInt32(xlarge);

                    int s = 0, m = 0, l = 0, xl = 0;
                    int pacA_H = 0, pacB_H = 0, S_H = 0, M_H = 0, L_H = 0, XL_H = 0;

                    //tblIssue - ID
                    int issueID = Convert.ToInt32(Session["sessionIssueID"]);

                    var itemData = (from us in db.tblItems
                                    where us.itemCode.Contains(itemCode)
                                    select us).FirstOrDefault();
                    if (itemData != null)
                    {
                        var issueData = (from us in db.tblIssues
                                         where us.id == issueID
                                         select us).FirstOrDefault();

                        //Previously issued - Total
                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.packetA.Value).Sum();
                            if (pach > 0)
                            {
                                pacA_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.packetB.Value).Sum();
                            if (pach > 0)
                            {
                                pacB_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.small.Value).Sum();
                            if (pach > 0)
                            {
                                S_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.medium.Value).Sum();
                            if (pach > 0)
                            {
                                M_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.large.Value).Sum();
                            if (pach > 0)
                            {
                                L_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        try
                        {
                            var pach = (from us in db.tblsubInventoryDetails
                                        where us.issueId == issueID
                                        select us.xLarge.Value).Sum();
                            if (pach > 0)
                            {
                                XL_H = pach;
                            }
                        }
                        catch (Exception x)
                        { }

                        if ((pacA_ + pacA_H) > issueData.packetA)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.packetA - pacA_H) + " A packets.");
                        }
                        if ((pacB_ + pacB_H) > issueData.packetB)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.packetB - pacB_H) + " B packets.");
                        }
                        if ((small_ + S_H) > issueData.small)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.small - S_H) + " S items.");
                        }
                        if ((medium_ + M_H) > issueData.medium)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.medium - M_H) + " M items.");
                        }
                        if ((large_ + L_H) > issueData.large)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.large - L_H) + " L items.");
                        }
                        if ((xlarge_ + XL_H) > issueData.xLarge)
                        {
                            throw new Exception("Cannot issue more than " + (issueData.xLarge - XL_H) + " XL items.");
                        }

                        //int assignedID = 0;
                        var issueTable = issueData;

                        tblsubInventoryDetail siDetail;
                        tblSubInventory       si;

                        var subDetails = (from a in db.tblsubInventoryDetails
                                          where a.issueId == issueData.id &&
                                          a.itemId == itemData.id
                                          select a).FirstOrDefault();
                        if (subDetails != null) //Edit
                        {
                            subDetails.packetA += pacA_;
                            subDetails.packetB += pacB_;
                            subDetails.small   += small_;
                            subDetails.medium  += medium_;
                            subDetails.large   += large_;
                            subDetails.xLarge  += xlarge_;
                            db.SaveChanges();

                            //Inv
                            var invData = (from a in db.tblSubInventories
                                           where a.itemId == itemData.id &&
                                           a.networkID == issueData.networkID
                                           select a).FirstOrDefault();
                            if (invData != null)
                            {
                                invData.small  += (Program.small * (pacA_)) + (Program.small * (pacB_)) + small_;
                                invData.medium += (Program.medium * (pacA_)) + (Program.medium * (pacB_)) + medium_;
                                invData.large  += (Program.large * (pacA_)) + (Program.large * (pacB_)) + large_;
                                invData.xLarge += (Program.xLarge * (pacA_)) + (Program.xLarge * (pacB_)) + xlarge_;
                                db.SaveChanges();
                            }
                            else //New inv
                            {
                                tblSubInventory inv = new tblSubInventory();
                                inv.small         = (Program.small * (pacA_)) + (Program.small * (pacB_)) + small_;
                                inv.medium        = (Program.medium * (pacA_)) + (Program.medium * (pacB_)) + medium_;
                                inv.large         = (Program.large * (pacA_)) + (Program.large * (pacB_)) + large_;
                                inv.xLarge        = (Program.xLarge * (pacA_)) + (Program.xLarge * (pacB_)) + xlarge_;
                                inv.itemId        = itemData.id;
                                inv.networkID     = issueData.networkID;
                                inv.issueRetailId = issueData.issuedRetailId;
                                db.tblSubInventories.Add(inv);
                                db.SaveChanges();
                            }
                        }
                        else //Add
                        {
                            tblsubInventoryDetail sd = new tblsubInventoryDetail();
                            sd.packetA = pacA_;
                            sd.packetB = pacB_;
                            sd.small   = small_;
                            sd.medium  = medium_;
                            sd.large   = large_;
                            sd.xLarge  = xlarge_;
                            sd.itemId  = itemData.id;
                            sd.issueId = issueData.id;
                            db.tblsubInventoryDetails.Add(sd);
                            db.SaveChanges();

                            var invCheck = (from us in db.tblSubInventories
                                            where us.itemId == itemData.id
                                            & us.networkID == 3
                                            select us).FirstOrDefault();
                            if (invCheck != null) //Update existing
                            {
                                invCheck.small  += (Program.small * (pacA_)) + (Program.small * (pacB_)) + small_;
                                invCheck.medium += (Program.medium * (pacA_)) + (Program.medium * (pacB_)) + medium_;
                                invCheck.large  += (Program.large * (pacA_)) + (Program.large * (pacB_)) + large_;
                                invCheck.xLarge += (Program.xLarge * (pacA_)) + (Program.xLarge * (pacB_)) + xlarge_;
                                db.SaveChanges();
                            }
                            else //New inv
                            {
                                tblSubInventory inv = new tblSubInventory();
                                inv.small         = (Program.small * (pacA_)) + (Program.small * (pacB_)) + small_;
                                inv.medium        = (Program.medium * (pacA_)) + (Program.medium * (pacB_)) + medium_;
                                inv.large         = (Program.large * (pacA_)) + (Program.large * (pacB_)) + large_;
                                inv.xLarge        = (Program.xLarge * (pacA_)) + (Program.xLarge * (pacB_)) + xlarge_;
                                inv.itemId        = itemData.id;
                                inv.networkID     = issueData.networkID;
                                inv.issueRetailId = issueData.issuedRetailId;
                                db.tblSubInventories.Add(inv);
                                db.SaveChanges();
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid product code.");
                    }
                }

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