public InstanceSelectorVM() { _cachedInventory = ScrapYard.Instance.TheInventory; _shouldSell = ScrapYard.Instance.Settings.CurrentSaveSettings.OverrideFunds; ApplyPart = null; BasePart = EditorLogic.SelectedPart; }
/// <summary> /// Applies the inventory to a vessel, specifically the part tracker module. Happens in the Editor /// </summary> /// <param name="input">The vessel as a list of parts</param> public static void ApplyInventoryToVessel(IEnumerable <Part> input) { PartInventory copy = ScrapYard.Instance.TheInventory.Copy(); foreach (Part part in input) { //convert it to an inventorypart InventoryPart iPart = new InventoryPart(part); //find a corresponding one in the inventory and remove it InventoryPart inInventory = copy.RemovePart(iPart.ID); if (inInventory == null) { inInventory = copy.RemovePart(iPart, ComparisonStrength.MODULES); } //if one was found... if (inInventory != null) { Logging.DebugLog("Found a part in inventory for " + inInventory.Name); //copy it's part tracker over if (inInventory.TrackerModule != null && part.Modules?.Contains("ModuleSYPartTracker") == true) { ModuleSYPartTracker tracker = part.Modules["ModuleSYPartTracker"] as ModuleSYPartTracker; tracker.TimesRecovered = inInventory.TrackerModule.TimesRecovered; tracker.Inventoried = inInventory.TrackerModule.Inventoried; tracker.ID = inInventory.ID; Logging.Log($"Copied tracker. Recovered {tracker.TimesRecovered} times with id {tracker.ID}"); } } } ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire(); GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship); }
public void UpdatePartList(PartInventory inventory, string partName, bool selling) { Parts = new List <List <PartInstance> >(); if (partName == null) { return; } IEnumerable <InventoryPart> foundParts; foundParts = inventory?.FindPartsByName(partName) ?? new List <InventoryPart>(); foundParts = EditorHandling.FilterOutUsedParts(foundParts); foreach (InventoryPart iPart in foundParts) { PartInstance instance = new PartInstance(inventory, iPart, selling, ApplyPart); instance.Updated += Instance_Updated; List <PartInstance> list = Parts.FirstOrDefault(l => l.FirstOrDefault()?.BackingPart.IsSameAs(iPart, ComparisonStrength.TRACKER) == true); if (list == null) { list = new List <PartInstance>(); Parts.Add(list); } list.Add(instance); } //sort by number of uses Parts.Sort(leastToMostSorter); }
/// <summary> /// Orders contain some inventory info, so update what we can from there /// </summary> /// <param name="inv">The inventory entity</param> /// <param name="remarks">BL Remark, becomes location</param> /// <param name="unit_price_final">The price this part is listed on BL</param> /// <param name="description">Description</param> /// <param name="inventory_id">Inventory id</param> public void UpdatePartInventoryFromOrder(PartInventory inv, string remarks, string unit_price_final, string description, int inventory_id) { if (inv == null) { var model = GetPartModel(inventory_id); inv = model.PartInventory; } if (inv.Quantity == 0) { inv.Location = ""; _partInventoryRepo.AddLocationHistory(inv, remarks); } else { inv.Location = remarks; } inv.MyPrice = decimal.Parse(unit_price_final); inv.Description = description; if (inv.InventoryId == 0) { inv.InventoryId = inventory_id; } _partInventoryRepo.Update(inv); }
public InstanceSelectorVM(PartInventory inventory, Part basePart, Part applyToPart, bool shouldSell) { _cachedInventory = inventory; _shouldSell = shouldSell; ApplyPart = (applyToPart == basePart) ? applyToPart : null; BasePart = basePart; }
private PartInventory SetPartOutOfStock(ref PartInventory partInv) { partInv.Quantity = 0; partInv.InventoryId = 0; partInv.Location = ""; partInv.LastUpdated = DateTime.UtcNow; return(partInv); }
public void AddPartInvFromOrder(PartInventory inv, string no, string type) { var part = GetPart(no, type); var price = _api.UpdatePartPricingFromApi(no, type, inv.ColourId, inv.Condition); inv.Notes = "Added from order"; _partInventoryRepo.AddPartInv(ref inv, ref price, ref part); }
private PartInventory CreateBlankPartInventory() { var newPartInventory = new PartInventory(NUMBER_OF_PARTS_SLOTS); SavePartInventory(newPartInventory); Debug.LogWarningFormat("No PartInventory data found. Making a new one"); return(newPartInventory); }
public PartInstance(PartInventory inventory, InventoryPart iPart, bool selling, Part toApply) { _backingPart = iPart; _backingInventory = inventory; _selling = selling; if (selling) { _sellOrDiscard = "Sell"; } _toApply = toApply; _moduleVM = new InstanceModulesVM(_backingPart); }
public Part RecoverPartFromPartInv(PartInventory partInv, Part part) { if (partInv == null) { return(part); } var response = GetRequest <GetInventoryResponseModel>($"inventories/{partInv.InventoryId}"); var number = response.data.item.no; var type = response.data.item.type; return(UpdatePartFromApi(number, type, part)); }
public static void UpdateFromModel(this PartInventory inv, PartInventoryModel model) { inv.InventoryId = model.InventoryId; inv.Quantity = model.Quantity; inv.MyPrice = model.MyPrice; inv.ColourId = model.ColourId; inv.ColourName = model.ColourName; inv.Condition = model.Condition; inv.Location = model.Location; inv.Image = model.Image; inv.Description = model.Description; inv.Notes = model.Notes; inv.LastUpdated = model.LastUpdated; }
public PartInventory UpdateInventoryFromApi(int inventoryId, PartInventory partInv = null) { if (partInv == null) { partInv = new PartInventory(); } var response = GetRequest <GetInventoryResponseModel>($"inventories/{inventoryId}"); var item = response.data; if (item == null) { return(null); } partInv.InventoryId = item.inventory_id; partInv.Quantity = item.quantity; partInv.MyPrice = decimal.Parse(item.unit_price); partInv.ColourId = item.color_id; partInv.ColourName = GetColour(item.color_id).Name; partInv.Condition = item.new_or_used; partInv.Location = item.remarks; partInv.Description = item.description; if (string.IsNullOrEmpty(partInv.Image)) { partInv.Image = GetItemImage(item.item.type, item.item.no, item.color_id); } if (string.IsNullOrEmpty(partInv.Location)) { partInv.Location = item.description; } if (item.is_stock_room) { partInv.Quantity = 0; partInv.Location = ""; } partInv.LastUpdated = DateTime.UtcNow; return(partInv); }
/// <summary> /// Gets an inventory item from the api, puts it into an entity /// </summary> /// <param name="type"></param> /// <param name="categoryId"></param> /// <param name="colourId"></param> /// <param name="number"></param> /// <param name="condition"></param> /// <param name="partInv"></param> /// <returns></returns> public PartInventory UpdateInventoryFromApi(string type, int categoryId, int colourId, string number, string condition = "N", PartInventory partInv = null, string description = "") { if (partInv == null) { partInv = new PartInventory(); } var response = GetRequest <GetInventoriesResponseModel>($"inventories?item_type={type}&category_id={categoryId}&color_id={colourId}"); var item = response.data.FirstOrDefault(x => x.item.no == number && x.new_or_used == condition && x.description == description); if (item == null) { return(null); } partInv.InventoryId = item.inventory_id; partInv.Quantity = item.quantity; partInv.MyPrice = decimal.Parse(item.unit_price); partInv.ColourId = colourId; partInv.ColourName = GetColour(colourId).Name; partInv.Condition = condition; partInv.Location = item.remarks; partInv.Description = item.description; if (string.IsNullOrEmpty(partInv.Image)) { partInv.Image = GetItemImage(type, number, colourId); } if (string.IsNullOrEmpty(partInv.Location)) { partInv.Location = item.description; } if (item.is_stock_room) { partInv.Quantity = 0; partInv.Location = ""; } partInv.LastUpdated = DateTime.UtcNow; return(partInv); }
public PartInventoryModel FromEntity(PartInventory inv) { return(new PartInventoryModel { Id = inv.Id, InventoryId = inv.InventoryId, ColourId = inv.ColourId, ColourName = inv.ColourName, Condition = inv.Condition, Location = inv.Location, Image = inv.Image, LastUpdated = inv.LastUpdated, Number = inv.Part.Number, Name = inv.Part.Name, ItemType = inv.Part.ItemType, CategoryId = inv.Part.CategoryId, Quantity = inv.Quantity }); }
/// <summary> /// Splits a list of parts into a list of those that are in the inventory and those that are not /// </summary> /// <param name="input"></param> /// <param name="inInventory"></param> /// <param name="notInInventory"></param> public static void SplitParts(IEnumerable <Part> input, out IList <InventoryPart> inInventory, out IList <InventoryPart> notInInventory) { inInventory = new List <InventoryPart>(); notInInventory = new List <InventoryPart>(); PartInventory InventoryCopy = ScrapYard.Instance.TheInventory.Copy(); foreach (Part part in input) { InventoryPart inputPart = new InventoryPart(part); if (InventoryCopy.RemovePart(inputPart) != null) { inInventory.Add(inputPart); } else { notInInventory.Add(inputPart); } } }
private void AddPartInventoryFromOrder(OrderItemResponseModel item) { var inv = new PartInventory { InventoryId = item.inventory_id, Quantity = 0, MyPrice = decimal.Parse(item.unit_price_final), ColourId = item.color_id, ColourName = item.color_name, Condition = item.new_or_used, Location = item.remarks, Image = _apiService.GetItemImage(item.item.type, item.item.no, item.color_id), Description = item.description, LastUpdated = DateTime.UtcNow }; _dataService.AddPartInvFromOrder(inv, item.item.no, item.item.type); }
public Part RecoverPartFromPartInv(PartInventory partInv, Part part) { if (partInv == null) { return(part); } var response = GetRequest <GetInventoryResponseModel>($"inventories/{partInv.InventoryId}"); // TODO: What if this fails, probably delete stuff? if (response.data == null) { return(null); } var number = response.data.item.no; var type = response.data.item.type; return(UpdatePartFromApi(number, type, part)); }
public void FixDuplicateInventoryItem(PartInventory inv, PartInventory newInv) { // Delete price var price = inv.Pricing; if (price != null && price.Id != newInv.Pricing.Id) { _partPricingRepo.Remove(price); } // history - transfer to new inv if not there, otherwise delete var locations = inv.LocationHistory; locations.ToList().ForEach(FixLocation); // order items - point to new inv var orderItems = _orderItemRepo.Find(x => x.Part.Id == inv.Id).ToList(); foreach (var item in orderItems) { item.Part = newInv; _orderItemRepo.Update(item); } // remove it _partInventoryRepo.Remove(inv); void FixLocation(PartInventoryLocationHistory loc) { if (newInv.LocationHistory.Any(x => x.Location == loc.Location)) { _historyRepo.Remove(loc); } else { loc.PartInventory = newInv; _historyRepo.Update(loc); } } }
/// <summary> /// Applies the inventory to a vessel, specifically the part tracker module. Happens in the Editor /// </summary> /// <param name="input">The vessel as a list of part ConfigNodes</param> public static void ApplyInventoryToVessel(IEnumerable <ConfigNode> input) { PartInventory copy = ScrapYard.Instance.TheInventory.Copy(); foreach (ConfigNode partNode in input) { //convert it to an inventorypart InventoryPart iPart = new InventoryPart(partNode); //find a corresponding one in the inventory and remove it InventoryPart inInventory = copy.RemovePart(iPart.ID); if (inInventory == null) { inInventory = copy.RemovePart(iPart, ComparisonStrength.MODULES); } //if one was found... if (inInventory != null) { Logging.DebugLog("Found a part in inventory for " + inInventory.Name); //copy it's part tracker over ConfigNode trackerNode; if (inInventory.TrackerModule != null && (trackerNode = partNode.GetModuleNode("ModuleSYPartTracker")) != null) { string id = inInventory.ID.ToString(); int recovered = inInventory.TrackerModule.TimesRecovered; bool inventoried = inInventory.TrackerModule.Inventoried; trackerNode.SetValue("ID", id); trackerNode.SetValue("TimesRecovered", recovered); trackerNode.SetValue("Inventoried", inventoried); Logging.DebugLog($"Copied tracker. Recovered {recovered} times with id {id}"); } } } ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire(); GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship); }
private PartModel UpdatedPartModel(PartInventory partInv, bool updateInv = false, bool updatePrice = false, bool updatePart = false, DateTime?updateInvDate = null) { var colourId = partInv.ColourId; var condition = partInv.Condition; // Get the entities, make sure they're up to date, return them if (partInv.LastUpdated < DateTime.UtcNow.AddDays(-1) || updateInv || updateInvDate.HasValue && updateInvDate > partInv.LastUpdated) { var updatedInv = _api.UpdateInventoryFromApi(partInv); if (updatedInv == null || updatedInv.Quantity == 0) { SetPartOutOfStock(ref partInv); } else { partInv = updatedInv; } _partInventoryRepo.Update(partInv); } var part = partInv.Part; if (part.Number == null) { part = _api.RecoverPartFromPartInv(partInv, part); if (part == null) { return(new PartModel { Part = new Part { CategoryId = 0, Number = "N/A", Name = "Not Found" }, PartInventory = partInv, PartPriceInfo = new PartPriceInfo { AveragePrice = 0 }, InvIsPlaceHolder = true }); } _partrepo.Update(part); } else if (part.LastUpdated < DateTime.UtcNow.AddDays(-14) || updatePart) { part = _api.UpdatePartFromApi(part.Number, part.ItemType, part); _partrepo.Update(part); } var pricing = partInv.Pricing; if (pricing.LastUpdated < DateTime.UtcNow.AddDays(-14) || updatePrice) { pricing = _api.UpdatePartPricingFromApi(part.Number, part.ItemType, colourId, condition, pricing); if (pricing.InventoryItem != null) { _partPricingRepo.Update(pricing); } } return(new PartModel { Part = part, PartInventory = partInv, PartPriceInfo = pricing }); }
// Part Methods public static void addPart(Part part) { PartInventory.Add(part); }
public PartModel GetPartModel(PartInventory inv, bool updateInv = false, bool updatePrice = false, bool updatePart = false, DateTime?updateInvDate = null) { return(GetPartModel(inv.Part.Number, inv.ColourId, inv.Part.ItemType, inv.Condition, updateInv, updatePrice, updatePart, updateInvDate)); }
public static void deletePart(Part part) { PartInventory.Remove(part); }
/// <summary> /// Gets an order with its order items /// </summary> /// <param name="orderId">Order Id</param> /// <returns>Model with order and items</returns> public OrderWithItemsModel GetOrderWithItems(string orderId, int id = 0) { var order = _apiService.GetRequest <GetOrderResponseModel>("orders/" + orderId); var orderItems = _apiService.GetRequest <GetOrderItemsResponseModel>("orders/" + orderId + "/items"); // Add order to the DB var orderEntity = _dataService.AddOrder(order, orderItems, id); // Items come through in a very strange format, so get them from the DB after having just added them to the DB. var items = orderItems.data .SelectMany(x => x) .Select(item => { // Find this item in the DB var itemEntity = orderEntity.OrderItems.FirstOrDefault(x => x.Part.InventoryId == item.inventory_id); var unitPrice = decimal.Parse(item.unit_price_final); var totalPrice = unitPrice * item.quantity; int quantity; string image; var partModel = _dataService.GetPartModel(item.inventory_id, updateInvDate: order.data.date_ordered); if (partModel == null) { partModel = _dataService.GetPartModel(item.item.no, item.color_id, item.item.type, item.new_or_used, description: item.description, updateInvDate: order.data.date_ordered); } PartInventory inv = partModel?.PartInventory; if (inv != null) { image = inv.Image; quantity = inv.Quantity; } else if (itemEntity != null) { quantity = itemEntity.Quantity; image = _apiService.GetItemImage(item.item.type, item.item.no, item.color_id); } else { quantity = 0; image = _apiService.GetItemImage(item.item.type, item.item.no, item.color_id); } var itemModel = new OrderItemModel { InventoryId = item.inventory_id.ToString(), Name = HttpUtility.HtmlDecode(item.item.name), Condition = item.new_or_used == "N" ? "New" : "Used", Colour = item.color_name, Remarks = item.remarks, Quantity = item.quantity, UnitPrice = unitPrice, TotalPrice = totalPrice, Description = item.description, Type = item.item.type, Weight = item.weight, ItemsRemaining = quantity, Image = image }; if (inv != null && order.data.date_ordered > inv.LastUpdated) { // Update the inventory from the info we have in this order _dataService.UpdatePartInventoryFromOrder(inv, item.remarks, item.unit_price_final, item.description, item.inventory_id); } else if (inv == null) { AddPartInventoryFromOrder(item); } // Works out some stuff related to the remarks and the location ordering itemModel.FillRemarks(); return(itemModel); }) .OrderBy(x => x.Condition) .ThenBy(x => x.RemarkLetter3) .ThenBy(x => x.RemarkLetter2) .ThenBy(x => x.RemarkLetter1) .ThenBy(x => x.RemarkNumber) .ThenBy(x => x.Colour) .ThenBy(x => x.Name) .ToList(); var data = order.data; return(new OrderWithItemsModel { BuyerName = orderEntity.BuyerName, UserName = data.buyer_name, ShippingMethod = PostageHelper.FriendlyPostageName(orderEntity.ShippingMethod), OrderTotal = orderEntity.Subtotal.ToString(), Buyer = new Buyer(data.shipping.address), OrderNumber = orderEntity.OrderId, OrderDate = orderEntity.OrderDate.ToString("yyyy-MM-dd"), OrderPaid = data.payment.date_paid.ToString("yyyy-MM-dd"), OrderRemarks = data.remarks, SubTotal = StaticFunctions.FormatCurrencyStr(data.cost.subtotal), ServiceCharge = StaticFunctions.FormatCurrencyStr(data.cost.etc1), Coupon = StaticFunctions.FormatCurrencyStr(data.cost.coupon), PostagePackaging = StaticFunctions.FormatCurrencyStr(data.cost.shipping), Total = StaticFunctions.FormatCurrencyStr(data.cost.grand_total), Items = items }); }
public static bool updatePart(Part part, int partID) { PartInventory.RemoveAt(partID); //RemoveAt() PartInventory.Insert(partID, part); return(true); }
public void SavePartInventory(PartInventory partInventory) { var json = JsonUtility.ToJson(partInventory, true); File.WriteAllText(PARTS_PATH + PARTS_FILE_NAME, json); }
/// <summary> /// Given an existing inventory entity, updates it from the api /// </summary> /// <param name="partInv">An inventory entity</param> /// <returns>The same entity, updated</returns> public PartInventory UpdateInventoryFromApi(PartInventory partInv) { return (UpdateInventoryFromApi(partInv.InventoryId, partInv) ?? UpdateInventoryFromApi(partInv.Part.ItemType, partInv.Part.CategoryId, partInv.ColourId, partInv.Part.Number, partInv.Condition, partInv)); }