示例#1
0
        private static void UpsertItems(AvaTaxClient client, List <ItemModel> itemModels)
        {
            foreach (ItemModel newItemModel in itemModels)
            {
                try
                {
                    //Does the item already exist in this company?
                    var existingItem = client.QueryItems(string.Format("itemCode EQ \"{0}\" AND companyId EQ {1}", newItemModel.itemCode, PRD_COMPANY_ID), string.Empty, null, null, string.Empty).value.FirstOrDefault();

                    //Yes, the item exists. Load the HS Codes to this item.
                    if (existingItem != null)
                    {
                        var existingClassifications = client.ListItemClassifications(PRD_COMPANY_ID, existingItem.id, string.Empty, null, null, string.Empty).value;

                        //Check if there are Classifications.
                        if (newItemModel.classifications != null)
                        {
                            //Yes, load the Classifications.
                            foreach (var cm in newItemModel.classifications)
                            {
                                ItemClassificationInputModel clsInputModel = new ItemClassificationInputModel {
                                    productCode = cm.productCode, systemCode = cm.systemCode
                                };

                                //Does the classification already exist? And is it the same?
                                var sameClassificationSystem = existingClassifications.Where(ec => ec.systemCode == cm.systemCode).FirstOrDefault();
                                var exactSameClassification  = existingClassifications.Where(ec => ec.systemCode == cm.systemCode && ec.productCode == cm.productCode).FirstOrDefault();

                                //Classification does not exist. Add it.
                                if (sameClassificationSystem == null)
                                {
                                    client.CreateItemClassifications(PRD_COMPANY_ID, existingItem.id, new List <ItemClassificationInputModel> {
                                        clsInputModel
                                    });

                                    continue;
                                }

                                //Classification exists, but is different. Update it.
                                if (sameClassificationSystem != null &&
                                    exactSameClassification == null)
                                {
                                    client.UpdateItemClassification(PRD_COMPANY_ID, existingItem.id, sameClassificationSystem.id.Value, clsInputModel);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Item does not yet exist. Create a new Item.
                        newItemModel.companyId = PRD_COMPANY_ID;
                        client.CreateItems(PRD_COMPANY_ID, new List <ItemModel>()
                        {
                            newItemModel
                        });
                    }
                }
                catch (AvaTaxError exc)
                {
                    Console.WriteLine(string.Format("Error loading/updating item {0}", newItemModel.id));
                    Console.WriteLine(string.Format("More information: {0}", exc.error));
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }
            }
        }