﻿<?xml version="1.0" encoding="utf-16"?>
<JiwaDocument xmlns:jiwa="http://www.jiwa.com.au/xml/schemas" Type="JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin">
  <RecID>e282f7e8-d50a-45a0-aca4-4d56d61fc165</RecID>
  <Name>BulkImportTest</Name>
  <IsEnabled>true</IsEnabled>
  <IsIsolatedToOwnAppDomain>false</IsIsolatedToOwnAppDomain>
  <ExecutionOrder>200</ExecutionOrder>
  <Author />
  <Version>2</Version>
  <Code>using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Data;
using System.Diagnostics;
using JiwaFinancials.Jiwa;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;
using JiwaFinancials.Jiwa.JiwaApplication;
using JiwaFinancials.Jiwa.JiwaServiceModel;
using System.Linq;
using ServiceStack.Text;
using ServiceStack;
using ServiceStack.OrmLite;

#region "BusinessLogicPlugin"
public class BusinessLogicPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaBusinessLogicPlugin
{

    public override object InitializeLifetimeService()
    {
        // returning null here will prevent the lease manager
        // from deleting the Object.
        return null;
    }

    public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaBusinessLogic JiwaBusinessLogic, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
        if (JiwaBusinessLogic is JiwaFinancials.Jiwa.JiwaInventory.Inventory)
        {
            JiwaFinancials.Jiwa.JiwaInventory.Inventory inventory = (JiwaFinancials.Jiwa.JiwaInventory.Inventory)JiwaBusinessLogic;

            inventory.SaveEnd += async delegate (object sender, EventArgs e)
            {
                InventoryHelper inventoryHelper = new InventoryHelper();

                //there is a purpose to get customfield using sql query
                var shopifyInventoryCustomfields = await inventoryHelper.GetInventoryCustomfield(Plugin.Manager, inventory.InventoryID);

                if (shopifyInventoryCustomfields)
                {
                    try
                    {
                        CustomInventory specialIventory = await inventoryHelper.GetInventory(inventory.InventoryID, Plugin.Manager);
                        await JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.Webhook(inventory.Manager, specialIventory.ToJson&lt;CustomInventory&gt;(), "inventory.specialcreated", JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.PluginName);
                    }
                    catch (Exception ex)
                    {
						// TODO: Do you really want to swallow these exceptions?
                    }
                }
            };
        }
    }
}
#endregion

namespace JiwaFinancials.Jiwa.JiwaServiceModel
{
    public class CustomInventoryWebhooks : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaRESTAPIPlugin
    {
        #region "Configuration"					
        public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaApplication.Manager JiwaApplicationManager)
        {
            JiwaFinancials.Jiwa.JiwaServiceModel.RESTAPIPlugin.WebHookEvents.Add(new JiwaFinancials.Jiwa.JiwaServiceModel.WebHookEvent { Name = "inventory.specialcreated", Description = "Occurs when a new inventory item (product) is created and has it's 'I'm Special' custom field ticked." });
        }
        #endregion
    }
}


public class InventoryHelper
{
    public async Task&lt;decimal&gt; GetQuantityLeft(JiwaFinancials.Jiwa.JiwaApplication.Manager manager, string inventoryID)
    {
        const string query = @"SELECT
								    COALESCE(MAX(QuantityLeft), 0) AS QuantityLeft
								FROM
								    IN_WarehouseSOH
								    JOIN IN_Main ON IN_WarehouseSOH.InventoryID = IN_Main.InventoryID
								    JOIN IN_Logical ON IN_WarehouseSOH.IN_LogicalID = IN_Logical.IN_LogicalID
								WHERE
								    IN_Main.InventoryID=@inventoryID
								GROUP BY
								IN_Main.InventoryID
								ORDER BY
								    IN_Main.InventoryID";
		
		var dbFactory = new OrmLiteConnectionFactory(manager.Database.ConnectionString, ServiceStack.OrmLite.SqlServer2012Dialect.Provider);
		using (var db = dbFactory.Open())
		{				
			return await db.SingleAsync&lt;decimal&gt;(query, new { inventoryID = inventoryID } );
		}
    }

    public async Task&lt;List&lt;AttributeGroupTemplate&gt;&gt; GetProductMetadata(Manager manager, string inventoryId)
    {        
        const string query = @"SELECT
                             IN_AttributeGroupTemplate.Name AS AttributeTemplate,
                             IN_AttributeGroupTemplateAttribute.Name AS Attribute,
                             IN_AttributeGroupAttributeValue.Contents AS AttributeValue,
                             IN_AttributeGroup.Description
                            FROM 
                             IN_Main
                             JOIN IN_AttributeGroup ON IN_Main.InventoryID = IN_AttributeGroup.IN_Main_InventoryID
                             JOIN IN_AttributeGroupAttributeValue On IN_AttributeGroup.RecID = IN_AttributeGroupAttributeValue.IN_AttributeGroup_RecID
                             JOIN IN_AttributeGroupTemplate ON IN_AttributeGroup.IN_AttributeGroupTemplate_RecID = IN_AttributeGroupTemplate.RecID
                             JOIN IN_AttributeGroupTemplateAttribute ON IN_AttributeGroupTemplate.RecID = IN_AttributeGroupTemplateAttribute.IN_AttributeGroupTemplate_RecID
        								                        AND IN_AttributeGroupAttributeValue.IN_AttributeGroupTemplateAttribute_RecID = IN_AttributeGroupTemplateAttribute.RecID
                            WHERE
                             IN_AttributeGroupTemplate.Name = 'Product Metadata'
                             AND IN_Main.InventoryId = @InventoryId";
		
		var dbFactory = new OrmLiteConnectionFactory(manager.Database.ConnectionString, ServiceStack.OrmLite.SqlServer2012Dialect.Provider);
		using (var db = dbFactory.Open())
		{				
			return await db.SqlListAsync&lt;AttributeGroupTemplate&gt;(query, new { InventoryId = inventoryId } );
		}
    }

    public async Task&lt;bool&gt; GetInventoryCustomfield(Manager manager, string inventoryId)
    {
        SqlDataReader sqlDataReader = null;

        const string query = @"SELECT
                                    COALESCE(CASE WHEN sp_we.Contents = 'True' THEN 1 ELSE 0 END, 0) AS SpecialInventory
                                FROM
	                                IN_CustomSettingValues a
	                                LEFT OUTER JOIN IN_CustomSettingValues sp_we ON a.InventoryID = sp_we.InventoryID
												                                AND sp_we.SettingID = (SELECT x.SettingID FROM IN_CustomSetting x WHERE x.SettingName = 'SpecialInventory')
                                WHERE
                                    a.InventoryID = @InventoryID
                                GROUP BY 
	                                a.InventoryID,
	                                sp_we.Contents
                                ORDER BY 
	                                a.InventoryID";
		
		var dbFactory = new OrmLiteConnectionFactory(manager.Database.ConnectionString, ServiceStack.OrmLite.SqlServer2012Dialect.Provider);
		using (var db = dbFactory.Open())
		{				
			return await db.SingleAsync&lt;bool&gt;(query, new { InventoryID = inventoryId } );
		}
    }

    public async Task&lt;CustomInventory&gt; GetInventory(string inventoryID, Manager manager)
    {
        decimal soh = await GetQuantityLeft(manager, inventoryID);
        var metadata = await GetProductMetadata(manager, inventoryID);

        return new CustomInventory
        {
            Soh = soh,
            Metadata = metadata
        };
    }
}

public class AttributeGroupTemplate
{
    public string Attribute { get; set; }
    public string AttributeTemplate { get; set; }
    public string AttributeValue { get; set; }
    public string Description { get; set; }
}

public class CustomInventory
{
    public decimal Soh { get; set; }
    public List&lt;AttributeGroupTemplate&gt; Metadata { get; set; }
}</Code>
  <ExceptionPolicy>Abort</ExceptionPolicy>
  <Language>CSharp</Language>
  <BusinessLogicCollection>
    <BusinessLogic>
      <RecID>6dd9ddb0-8b8e-4a84-b7be-13d2b419bfb4</RecID>
      <Description>Inventory Maintenance</Description>
      <ClassName>JiwaFinancials.Jiwa.JiwaInventory.Inventory</ClassName>
      <Assembly>JiwaInventory, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</Assembly>
    </BusinessLogic>
  </BusinessLogicCollection>
  <ReferenceCollection>
    <Reference>
      <RecID>fb21e2b6-176e-495e-93bc-d2661f372bd7</RecID>
      <AssemblyFullName>JiwaApplication, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>JiwaApplication.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\JiwaApplication.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>5d1f17c0-fd8f-4480-a2a2-6096fdfd1ab6</RecID>
      <AssemblyFullName>mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>mscorlib.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>ec37546d-8b0e-460e-87bf-1dffe22694d1</RecID>
      <AssemblyFullName>System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>60ac2f9d-018c-4ff7-acfa-d5708ba8d856</RecID>
      <AssemblyFullName>System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</AssemblyFullName>
      <AssemblyName>System.Drawing.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>285f50cd-7f3c-4ac2-a250-128ec75ff1de</RecID>
      <AssemblyFullName>JiwaODBC, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>JiwaODBC.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\JiwaODBC.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>78ef7e85-db3e-4466-9efc-16f5a2565182</RecID>
      <AssemblyFullName>System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.Data.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>0224c374-60cc-4fca-8d78-a4e0e1371774</RecID>
      <AssemblyFullName>System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.Xml.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>eaec58f3-a6e8-4b24-b9a3-c7516cb44a3b</RecID>
      <AssemblyFullName>System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.Runtime.Serialization.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>71ce9037-b093-42ff-8ca4-1037601fe7cf</RecID>
      <AssemblyFullName>System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.Windows.Forms.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>3827991b-ddbb-4198-9182-d975b4f5f719</RecID>
      <AssemblyFullName>JiwaServiceModel, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>JiwaServiceModel.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\JiwaServiceModel.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>b962331f-46f9-4733-8a3b-0a2368f00d27</RecID>
      <AssemblyFullName>ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>2c4fe1d2-9dac-4c04-be6d-2e7cdaf7ea2d</RecID>
      <AssemblyFullName>System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</AssemblyFullName>
      <AssemblyName>System.Core.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>1bd92652-79f8-4983-963b-61608ffb20bc</RecID>
      <AssemblyFullName>ZetaHtmlEditControl, Version=1.1.0.3, Culture=neutral, PublicKeyToken=2e2e5ba5da72b6c0</AssemblyFullName>
      <AssemblyName>ZetaHtmlEditControl.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ZetaHtmlEditControl.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>49f7948e-8ac6-4d9c-954a-517d74e970d9</RecID>
      <AssemblyFullName>ServiceStack.Text, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.Text.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.Text.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>8549724d-b5dc-4e36-8326-77e9f56d9de1</RecID>
      <AssemblyFullName>System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</AssemblyFullName>
      <AssemblyName>System.Security.dll</AssemblyName>
      <AssemblyLocation>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Security\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Security.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>8343ec2f-5668-4a48-b1e3-862b79d55092</RecID>
      <AssemblyFullName>ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.Interfaces.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.Interfaces.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>949415cf-47bd-4148-958d-8c60afe3f8b4</RecID>
      <AssemblyFullName>ServiceStack.Server, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = 02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.Server.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.Server.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>13af8003-d785-40b5-8007-d3947b81abc2</RecID>
      <AssemblyFullName>ServiceStack.OrmLite, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = 02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.OrmLite.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.OrmLite.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>fd72ba4a-03e8-4514-ac6a-fc18c2d195e1</RecID>
      <AssemblyFullName>JiwaInventory, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>JiwaInventory.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\JiwaInventory.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>8467e10c-6d1b-4a70-a26d-66a6c696e533</RecID>
      <AssemblyFullName>JiwaJournalSets, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>JiwaJournalSets.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\JiwaJournalSets.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>4cd0f560-2497-4396-92c0-9f99605a5754</RecID>
      <AssemblyFullName>LumenWorks.Framework.IO, Version=7.2.1.0, Culture=neutral, PublicKeyToken=e30ce81e37f29c8c</AssemblyFullName>
      <AssemblyName>LumenWorks.Framework.IO.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\LumenWorks.Framework.IO.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>b32ac10c-5b55-4af3-be2c-d1a79bffd8af</RecID>
      <AssemblyFullName>ServiceStack.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.Common.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.Common.dll</AssemblyLocation>
    </Reference>
    <Reference>
      <RecID>bbdbd790-4700-4774-bd90-88b2485f436f</RecID>
      <AssemblyFullName>ServiceStack.OrmLite.SqlServer, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587</AssemblyFullName>
      <AssemblyName>ServiceStack.OrmLite.SqlServer.dll</AssemblyName>
      <AssemblyLocation>C:\VSTS\Jiwa 7\07.02.00\Built Files\ServiceStack.OrmLite.SqlServer.dll</AssemblyLocation>
    </Reference>
  </ReferenceCollection>
  <CustomFieldCollection>
    <CustomField>
      <RecID>fcf0196f-1e9a-4212-bf99-313b61af4fc1</RecID>
      <Name>SpecialInventory</Name>
      <Description>Special Inventory</Description>
      <DisplayOrder>1</DisplayOrder>
      <CellType>Checkbox</CellType>
      <CustomFieldModule>Inventory Maintenance</CustomFieldModule>
    </CustomField>
  </CustomFieldCollection>
  <PluginReferenceCollection>
    <PluginReference>
      <RecID>f4628297-3db1-4cb8-bd54-436678ceeae1</RecID>
      <Plugin>
        <RecID xmlns="Entities.Plugin">fb0c1ea8-c560-41ef-b718-f0dc70ba89b9</RecID>
        <Name xmlns="Entities.Plugin">REST API</Name>
        <Description xmlns="Entities.Plugin">Provides a REST HTTP endpoint with some routes.</Description>
        <Author xmlns="Entities.Plugin">Jiwa Financials</Author>
        <IsEnabled xmlns="Entities.Plugin">false</IsEnabled>
        <ExecutionOrder xmlns="Entities.Plugin">0</ExecutionOrder>
      </Plugin>
    </PluginReference>
  </PluginReferenceCollection>
</JiwaDocument>