Page 1 of 1

R5 adding item to existing order causes 100% profit

Posted: Tue Sep 03, 2013 1:51 pm
by AbleMods
When adding an item to an existing order, the COGS field is not populated on the new line item. As a result, sales/profit reports are no longer accurate as the item added now reflects a 100% profit margin.

If you have full source code, the fix to resolve this issue can be made in the /Orders/OrderItemRepository.cs class file. Look for this code in the CreateForProduct() method:

Code: Select all

                 // CALCULATE THE PRICE OF THE PRODUCT
                ProductCalculator pcalc = ProductCalculator.LoadForProduct(productId, quantity, optionList, kitList);
                baseItem.Sku = pcalc.Sku;
                baseItem.Price = pcalc.Price;
                baseItem.Weight = pcalc.Weight;

                // CHECK PRODUCT VARIANT
                ProductVariant variant = productVarientRepo.LoadForOptionList(productId, optionList);
                if (variant != null)
                {
                    baseItem.OptionList = optionList;
                    baseItem.VariantName = variant.VariantName;
                }

                // CHECK FOR DIGITAL GOODS
And replace it with this code:

Code: Select all

                // CALCULATE THE PRICE OF THE PRODUCT
                ProductCalculator pcalc = ProductCalculator.LoadForProduct(productId, quantity, optionList, kitList);
                baseItem.Sku = pcalc.Sku;
                baseItem.Price = pcalc.Price;
                baseItem.Weight = pcalc.Weight;

                // BEGIN MOD: AbleMods.com
                // DATE:  08/26/2013
                // BugFix:  CostofGoods is never populated when directly building OrderItem object
                
                // CHECK PRODUCT VARIANT
                ProductVariant variant = productVarientRepo.LoadForOptionList(productId, optionList);
                if (variant != null)
                {
                    baseItem.OptionList = optionList;
                    baseItem.VariantName = variant.VariantName;
                    decimal? variantCOGS = variant.CostOfGoods;
                    if (variantCOGS != null && variantCOGS > 0)
                    {
                        baseItem.CostOfGoods = variantCOGS.Value;
                    }
                }

                // Set CostOfGoods to base product COGS if new OrderItem object CostofGoods is still zero
                // In other words, only set it if a variant above hasn't already established as COGS value
                if (baseItem.Product != null && baseItem.CostOfGoods == 0) baseItem.CostOfGoods = baseItem.Product.CostOfGoods;

                // END MOD: AbleMods.com

                // CHECK FOR DIGITAL GOODS

Re: R5 adding item to existing order causes 100% profit

Posted: Wed Sep 04, 2013 8:09 am
by AbleMods
Repairing the data is also necessary since the above fix only resolves the problem going forward.

To update your SQL data, you can use this query against your database. The data can only be repaired if the item sold still exists in your catalog. This query only fixes COGS on regular products sold, not variants. Use at your own risk.

Code: Select all

update ac_OrderItems
	set CostOfGoods = p.costofgoods
	from ac_OrderItems oi 
	left outer join ac_products p on oi.productid = p.productid
	where oi.CostOfGoods = 0 and OrderItemTypeId = 0
		and p.CostOfGoods <> 0 and optionlist is null

Re: R5 adding item to existing order causes 100% profit

Posted: Wed Sep 04, 2013 8:32 am
by Katie
Thanks so much for posting this bug and the fix. It would be great if you could let me know via the green Feedback button in Gold. I would sure feel bad if we missed a bug report. You can just reference the forum post, so it shouldn't take more than a few seconds to let us know.

Thanks again for all you do!
Katie

Re: R5 adding item to existing order causes 100% profit

Posted: Wed Sep 04, 2013 3:13 pm
by Katie
This one is already reported, so my last comment applies only to future issues.