CORPORATION: Fix "Use same 'Sell Amount' for all cities" toggle for products (#775)

This commit is contained in:
Caldwell 2023-09-12 10:31:51 +02:00 committed by GitHub
parent d2b3659512
commit bf5e638891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 26 deletions

@ -166,6 +166,8 @@ export function SellMaterial(material: Material, amount: string, price: string):
export function SellProduct(product: Product, city: CityName, amt: string, price: string, all: boolean): void { export function SellProduct(product: Product, city: CityName, amt: string, price: string, all: boolean): void {
//Parse price //Parse price
// initliaze newPrice with oldPrice as default
let newPrice = product.cityData[city].desiredSellPrice;
if (price.includes("MP")) { if (price.includes("MP")) {
//Dynamically evaluated quantity. First test to make sure its valid //Dynamically evaluated quantity. First test to make sure its valid
//Sanitize input, then replace dynamic variables with arbitrary numbers //Sanitize input, then replace dynamic variables with arbitrary numbers
@ -181,17 +183,19 @@ export function SellProduct(product: Product, city: CityName, amt: string, price
if (temp == null || isNaN(parseFloat(temp))) { if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell price field."); throw new Error("Invalid value or expression for sell price field.");
} }
product.cityData[city].desiredSellPrice = price; //Use sanitized price newPrice = price; //Use sanitized price
} else { } else {
const cost = parseFloat(price); const cost = parseFloat(price);
if (isNaN(cost)) { if (isNaN(cost)) {
throw new Error("Invalid value for sell price field"); throw new Error("Invalid value for sell price field");
} }
product.cityData[city].desiredSellPrice = cost; newPrice = cost;
} }
// Parse quantity // Parse quantity
amt = amt.toUpperCase(); amt = amt.toUpperCase();
//initialize newAmount with old as default
let newAmount = product.cityData[city].desiredSellAmount;
if (amt.includes("MAX") || amt.includes("PROD") || amt.includes("INV")) { if (amt.includes("MAX") || amt.includes("PROD") || amt.includes("INV")) {
//Dynamically evaluated quantity. First test to make sure its valid //Dynamically evaluated quantity. First test to make sure its valid
let qty = amt.replace(/\s+/g, ""); let qty = amt.replace(/\s+/g, "");
@ -208,14 +212,7 @@ export function SellProduct(product: Product, city: CityName, amt: string, price
if (temp == null || isNaN(parseFloat(temp))) { if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell quantity field"); throw new Error("Invalid value or expression for sell quantity field");
} }
newAmount = qty; //Use sanitized input
if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = qty; //Use sanitized input
}
} else {
product.cityData[city].desiredSellAmount = qty; //Use sanitized input
}
} else if (isNaN(parseFloat(amt)) || parseFloat(amt) < 0) { } else if (isNaN(parseFloat(amt)) || parseFloat(amt) < 0) {
throw new Error("Invalid value for sell quantity field! Must be numeric or 'PROD' or 'MAX'"); throw new Error("Invalid value for sell quantity field! Must be numeric or 'PROD' or 'MAX'");
} else { } else {
@ -223,21 +220,17 @@ export function SellProduct(product: Product, city: CityName, amt: string, price
if (isNaN(qty)) { if (isNaN(qty)) {
qty = 0; qty = 0;
} }
if (qty === 0) { newAmount = qty;
}
//apply new price and amount to all or just current
if (all) { if (all) {
for (const cityName of Object.values(CityName)) { for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = 0; product.cityData[cityName].desiredSellAmount = newAmount;
product.cityData[cityName].desiredSellPrice = newPrice;
} }
} else { } else {
product.cityData[city].desiredSellAmount = 0; product.cityData[city].desiredSellAmount = newAmount;
} product.cityData[city].desiredSellPrice = newPrice;
} else if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = qty;
}
} else {
product.cityData[city].desiredSellAmount = qty;
}
} }
} }

@ -97,7 +97,7 @@ export function SellProductModal(props: IProps): React.ReactElement {
<Button onClick={sellProduct}>Confirm</Button> <Button onClick={sellProduct}>Confirm</Button>
<FormControlLabel <FormControlLabel
control={<Switch checked={checked} onChange={onCheckedChange} />} control={<Switch checked={checked} onChange={onCheckedChange} />}
label={<Typography>Use same 'Sell Amount' for all cities</Typography>} label={<Typography>Set for all cities</Typography>}
/> />
</Modal> </Modal>
); );

@ -7138,7 +7138,7 @@ export interface WarehouseAPI {
* @param productName - Name of the product * @param productName - Name of the product
* @param amt - Amount to sell, can be "MAX" * @param amt - Amount to sell, can be "MAX"
* @param price - Price to sell, can be "MP" * @param price - Price to sell, can be "MP"
* @param all - Sell in all city * @param all - Set sell amount and price in all cities
*/ */
sellProduct( sellProduct(
divisionName: string, divisionName: string,