public override Int64 Set(string parametersArr)
        {
            try
            {
                new CustomDAO().GenerateAdjustmentBuild();
                List <Foodics.NetSuite.Shared.Model.AdjustmentBuild> ColLstAll = new CustomDAO().SelectAdjustmentLocation();
                var ColLst =
                    ColLstAll.DistinctBy(x => new { x.Location_Id, x.Netsuite_Id }).Select(x => new AdjustmentBuild
                {
                    Location_Id   = x.Location_Id,
                    Subsidiary_Id = x.Subsidiary_Id,
                }).Distinct().ToList();
                if (ColLst.Count <= 0)
                {
                    return(0);
                }

                com.netsuite.webservices.InventoryAdjustment[] AdjustArr = new com.netsuite.webservices.InventoryAdjustment[ColLst.Count];
                for (int i = 0; i < ColLst.Count; i++)
                {
                    com.netsuite.webservices.InventoryAdjustment  AdjustBuildObject;
                    Foodics.NetSuite.Shared.Model.AdjustmentBuild Obj_info;
                    try
                    {
                        Obj_info = ColLst[i];
                        //Netsuite invoice type
                        AdjustBuildObject = new com.netsuite.webservices.InventoryAdjustment();
                        Setting objSetting = new GenericeDAO <Setting>().GetWhere("Subsidiary_Netsuite_Id=" + Obj_info.Subsidiary_Id).FirstOrDefault();

                        AdjustBuildObject.tranDateSpecified = true;
                        //AdjustBuildObject.tranDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, TimeZoneInfo.Local);
                        AdjustBuildObject.tranDate = TimeZoneInfo.ConvertTimeToUtc(Utility.ConvertToDateTime(ConfigurationManager.AppSettings["InvoiceDate"]), TimeZoneInfo.Local);

                        // adjustment account
                        RecordRef adjustment_account = new RecordRef();
                        adjustment_account.type          = RecordType.account;
                        adjustment_account.typeSpecified = true;
                        adjustment_account.internalId    = objSetting.AdjustmentAccount_Netsuite_Id.ToString();//"122";
                        AdjustBuildObject.account        = adjustment_account;

                        // adjustment location
                        RecordRef adjustment_location = new RecordRef();
                        adjustment_location.type          = RecordType.location;
                        adjustment_location.typeSpecified = true;
                        adjustment_location.internalId    = Obj_info.Location_Id.ToString();
                        AdjustBuildObject.location        = adjustment_location;
                        AdjustBuildObject.adjLocation     = adjustment_location;

                        // subsidiary
                        RecordRef adjustment_subsidiary = new RecordRef();
                        adjustment_subsidiary.type          = RecordType.subsidiary;
                        adjustment_subsidiary.typeSpecified = true;
                        adjustment_subsidiary.internalId    = Obj_info.Subsidiary_Id.ToString();
                        AdjustBuildObject.subsidiary        = adjustment_subsidiary;

                        List <Foodics.NetSuite.Shared.Model.AdjustmentBuild> adjustment_items = ColLstAll.Where(x => x.Subsidiary_Id == Obj_info.Subsidiary_Id && x.Location_Id == Obj_info.Location_Id).ToList();
                        InventoryAdjustmentInventory[] invadjustmentItemArray = new InventoryAdjustmentInventory[adjustment_items.Count()];
                        for (int x = 0; x < adjustment_items.Count(); x++)
                        {
                            RecordRef item = new RecordRef();
                            item.internalId = adjustment_items[x].Item_Id.ToString();

                            invadjustmentItemArray[x]                      = new InventoryAdjustmentInventory();
                            invadjustmentItemArray[x].item                 = item;
                            invadjustmentItemArray[x].location             = adjustment_location;
                            invadjustmentItemArray[x].adjustQtyBy          = (adjustment_items[x].Quantity * -1);
                            invadjustmentItemArray[x].adjustQtyBySpecified = true;
                        }

                        InventoryAdjustmentInventoryList invList = new InventoryAdjustmentInventoryList();
                        invList.inventory = invadjustmentItemArray;
                        AdjustBuildObject.inventoryList = invList;



                        AdjustArr[i] = AdjustBuildObject;
                    }
                    catch (Exception ex)
                    {
                        ColLst.RemoveAt(i);
                        LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
                    }
                }

                WriteResponseList wr = Service(true).addList(AdjustArr);
                bool result          = wr.status.isSuccess;
                if (result)
                {
                    //Update database with returned Netsuite ids
                    Updatedlst(ColLst, wr);
                }
            }
            catch (Exception ex)
            {
                LogDAO.Integration_Exception(LogIntegrationType.Error, this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Error " + ex.Message);
            }



            return(0);
        }
コード例 #2
0
        public ActionResult InventoryAdjustment()
        {
            List <int> summmary = SummaryOfCurrentCount();

            if (summmary[2] != 0 || !AdminEmails.Contains(this.email)) // If there are items left to count, return them to the home page
            {
                return(RedirectToAction("Index"));
            }

            NetSuiteService service = this.service;

            // Create the adjustment object
            InventoryAdjustment adjustment = new InventoryAdjustment();

            adjustment.account = new RecordRef(RecordType.account, 147);    // Set the adjustment account (147 is NetSuite ID)

            adjustment.adjLocation = new RecordRef(RecordType.location, 1); // Set adjustment location of South Burlington (1 is NetSuite ID)

            // All items
            var items = from m in db.Items
                        select m;

            // Determine the Week for the count
            // Take the one code that is greater than 39 (Class C items) and determine the week by subtracting 39 from that code
            int[] CycleCodes = items.Select(x => x.CycleCountCode).Distinct().ToArray();
            int   week       = 0;

            foreach (int cyclecode in CycleCodes)
            {
                if (cyclecode > 39)
                {
                    week = cyclecode - 39;
                }
            }
            if (this.NonWeeklyCount != true)
            {
                adjustment.memo = "cycle count " + week.ToString() + "--" + DateTime.Now.Year; // Set the adjustment memo
            }
            else
            {
                adjustment.memo = "entire column count " + DateTime.Now.Year;
            }

            // List of items that have been changed
            var itemsChanged = items.ToList().Where(x => x.OnHand != x.NewOnHand); // We only care about items that have different qty for Adjustments

            // List of items with bin location changes
            var binLocationChanged = items.ToList().Where(x => x.NewBinLocation != x.BinLocation);

            // Create a List of InventoryAdjustmentInventory
            InventoryAdjustmentInventoryList inventoryList = new InventoryAdjustmentInventoryList(); // Required for Adjustment

            inventoryList.inventory = new InventoryAdjustmentInventory[itemsChanged.Count()];        // An array of items to be changed

            int currentIndex = 0;                                                                    // Keep track of the index

            // Change the Bin Location for items that have different Bins
            foreach (var item in binLocationChanged)
            {
                var           id = item.ID.ToString();
                InventoryItem ii = new InventoryItem();
                ii.internalId = id;
                ii.xSetCustomField("custitem_bin_location", item.NewBinLocation.ToString());
                service.xUpdate(ii);
            }

            if (itemsChanged == null && binLocationChanged != null)
            {
                ViewBag.BinOnly = "You have only adjusted the Bin Location, no Inventory adjustment has been created.";
            }

            // Create Inventory adjustment for items that have different quantities
            if (itemsChanged.Count() != 0)
            {
                foreach (var item in itemsChanged) // Set the required fields for the Adjustment
                {
                    InventoryAdjustmentInventory iai = new InventoryAdjustmentInventory();
                    iai.item     = new RecordRef((item.ID).ToString());
                    iai.location = new RecordRef("1");
                    iai.xSetAdjustQtyBy(item.NewOnHand - item.OnHand);
                    iai.memo = item.CounterInitials + ": " + item.Notes;
                    inventoryList.inventory[currentIndex] = iai;                              // Add to array
                    currentIndex++;                                                           // Update the Index
                }
                adjustment.inventoryList = inventoryList;                                     // Required for adjustment

                var sumAdjusted = adjustment.inventoryList.inventory.Sum(x => x.adjustQtyBy); // Check to make sure that the local count and NetSuite count match

                WriteResponse adjustmentAdded = service.xAdd(adjustment);                     // Push to Netsuite



                if (adjustmentAdded.status.isSuccess) // Checks to make sure that the adjustment push was successfull
                {
                    // Create a copy Adjustment to check against the local copy
                    var id = adjustment.internalId;
                    var CountItemsChanged  = itemsChanged.Count();
                    InventoryAdjustment ia = service.xGetRecord <InventoryAdjustment>(id);
                    var sumAdjustedCheck   = ia.inventoryList.inventory.Sum(x => x.adjustQtyBy);

                    if (sumAdjusted == sumAdjustedCheck) // Makes sure the quantity changes are the same
                    {
                        ViewBag.adjustmentID = id;
                        return(View(adjustmentAdded)); // We are good to delete the count
                    }
                    else // The quantities are different
                    {
                        ViewBag.ErrorMessage("Something went wrong. Please check NetSuite and make sure everything is EXACTLY as you have entered in this software.");
                        return(View(adjustmentAdded));// Need to check NetSuite, something went wrong
                    }
                }
                else // The NetSuite push was not successfull
                {
                    ViewBag.ErrorMessage = adjustmentAdded.status.statusDetail[0].message.ToString();
                    return(View(adjustmentAdded));
                }
            }
            // The user only changed the notes
            ViewBag.NotesOnly = "You did not change the quantity or bin location of any item in the count. Nothing has changed in NetSuite.";
            return(View());
        }