예제 #1
0
        public ProductInfo ImportFacebook(FBImportProduct info, string access_token, long subdomainid)
        {
            // check for duplicate sku
            var p = new product
            {
                subdomainid = subdomainid,
                details     = info.description,
                title       = info.title,
            };

            p.facebook_imports.Add(new facebook_import()
            {
                facebookID = info.id
            });

            if (!string.IsNullOrEmpty(info.sellingprice))
            {
                decimal price;
                if (decimal.TryParse(info.sellingprice, out price))
                {
                    p.sellingPrice = price;
                }
            }
            p.updated = DateTime.UtcNow;
            p.created = DateTime.UtcNow;

            // handle photos
            var photourls = info.photoids.Select(x => x.ToFacebookPhotoUrl(access_token)).Take(MaxPhotoImport);

            // create inventoryLocItem
            long locationid;

            using (var repository = new TradelrRepository())
            {
                locationid = repository.GetInventoryLocation(GeneralConstants.INVENTORY_LOCATION_DEFAULT,
                                                             subdomainid).id;
            }
            var inventoryLocItem = new inventoryLocationItem
            {
                locationid = locationid,
                lastUpdate = DateTime.UtcNow
            };

            // create variant
            var variant = new product_variant
            {
                sku = string.IsNullOrEmpty(info.sku) ? info.id.ToString(): info.sku
            };

            variant.inventoryLocationItems.Add(inventoryLocItem);

            // add photourls
            // create our add object
            var pinfo = new ProductInfo {
                p = p
            };

            foreach (var photourl in photourls)
            {
                pinfo.photo_urls.Add(photourl);
            }

            pinfo.p.product_variants.Add(variant);

            return(pinfo);
        }
예제 #2
0
        public void UpdateInventoryItem(orderItem orderItem, int inventoryDelta)
        {
            // no need for updates if delta is zero
            if (inventoryDelta == 0)
            {
                return;
            }

            inventoryLocation location;

            // throws null if we just check on value. happens when creating a new order
            if (orderItem.order.inventoryLocation1 != null)
            {
                location = orderItem.order.inventoryLocation1;
            }
            else
            {
                if (orderItem.order.inventoryLocation.HasValue)
                {
                    location = repository.GetInventoryLocation(orderItem.order.inventoryLocation.Value, senderDomain.id);
                }
                else
                {
                    location = repository.GetInventoryLocation(GeneralConstants.INVENTORY_LOCATION_DEFAULT, senderDomain.id);
                }
            }

            inventoryLocationItem locationItem;
            bool trackInventory;
            bool isDigital;

            if (orderItem.product_variant == null)
            {
                var variant = repository.GetProductVariant(orderItem.variantid, senderDomain.id);
                locationItem   = variant.inventoryLocationItems.SingleOrDefault(x => x.locationid == location.id);
                trackInventory = variant.product.trackInventory;
                isDigital      = variant.IsDigital();
            }
            else
            {
                locationItem   = orderItem.product_variant.inventoryLocationItems.SingleOrDefault(x => x.locationid == location.id);
                trackInventory = orderItem.product_variant.product.trackInventory;
                isDigital      = orderItem.product_variant.IsDigital();
            }

            if (locationItem == null)
            {
                locationItem = new inventoryLocationItem
                {
                    locationid = location.id,
                    lastUpdate = DateTime.UtcNow
                };
                if (orderItem.product_variant == null)
                {
                    var variant = repository.GetProductVariant(orderItem.variantid, senderDomain.id);
                    variant.inventoryLocationItems.Add(locationItem);
                }
                else
                {
                    orderItem.product_variant.inventoryLocationItems.Add(locationItem);
                }
            }

            var isNewEntry  = orderItem.id == 0;
            var isDelete    = orderItem.quantity == 0;
            var invWorker   = new InventoryWorker(locationItem, senderDomain.id, trackInventory, isDigital);
            var description = order.ToHtmlLink();

            switch (GetOrderStatus())
            {
            case OrderStatus.DRAFT:
                if (isNewEntry)
                {
                    description += " created";
                }
                else if (isDelete)
                {
                    description += string.Format(": {0} deleted", orderItem.product_variant.ToHtmlLink());
                }
                else
                {
                    description += " updated";
                }

                if (transactionType == TransactionType.INVOICE)
                {
                    invWorker.SetValues(description,
                                        -inventoryDelta, // available
                                        null,            // on order
                                        inventoryDelta,  // reserved
                                        null);           // sold
                }
                else
                {
                    invWorker.SetValues(description,
                                        null,           // available
                                        inventoryDelta, // on order
                                        null,           // reserved
                                        null);          // sold
                }
                break;

            case OrderStatus.SENT:
                if (transactionType == TransactionType.INVOICE)
                {
                    if (isNewEntry)
                    {
                        invWorker.SetValues(order.ToHtmlLink() + " created",
                                            -inventoryDelta, // available
                                            null,            // on order
                                            inventoryDelta,  // reserved
                                            null);           // sold
                    }
                }
                else
                {
                    if (isNewEntry)
                    {
                        invWorker.SetValues(order.ToHtmlLink() + " created",
                                            null,           // available
                                            inventoryDelta, // on order
                                            null,           // reserved
                                            null);          // sold
                    }
                }
                break;

            case OrderStatus.VIEWED:
                break;

            case OrderStatus.PARTIAL:
                break;

            case OrderStatus.PAID:
                if (transactionType == TransactionType.INVOICE)
                {
                    if (isNewEntry)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        invWorker.SetValues(order.ToHtmlLink() + " paid",
                                            null,            // available
                                            null,            // on order
                                            -inventoryDelta, // reserved
                                            inventoryDelta); // sold
                    }
                }
                break;

            case OrderStatus.SHIPPED:
                if (transactionType == TransactionType.ORDER)
                {
                    if (isNewEntry)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        invWorker.SetValues(order.ToHtmlLink() + " received",
                                            inventoryDelta,  // available
                                            -inventoryDelta, // on order
                                            null,            // reserved
                                            null);           // sold
                    }
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("status");
            }
        }