using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;
using System.Web;
using Veeqo_Integration;
namespace VeeqoIntegration
{
class Program
{
static void Main(string args)
{
Console.WriteLine(“Welcome to Veeqo integration”);
ExportData();
ExportProducts();
// ExportProducts_CostPrice();
Console.WriteLine("Integration Completed");
Console.ReadLine();
}
static async Task ExportData()
{
modGeneral obj = new modGeneral();
Console.WriteLine("Exporting Products...");
await ExportProducts();
obj.captureErrorLog("Integration Completed ", "");
Environment.Exit(0);
Console.WriteLine("Successfully Updated...");
Console.ReadLine();
Environment.Exit(0);
}
static async Task ExportProducts()
{
modGeneral obj = new modGeneral();
try
{
var client = new HttpClient();
var APIKEY = ConfigurationManager.AppSettings.Get("APIkEY");
DateTime ProductLastUpdatedAt = obj.GetLastUpdated("Products");
obj.captureErrorLog("Products Exporting", "");
var uri = "https://api.veeqo.com/products";
DataTable dtProducts = obj.LoadDataTable("Select * from View_ExportProductstoAPI where isnull(APIID,'')='' and Modifieddatetime>= '" + obj.FormatDatetime(ProductLastUpdatedAt) + "' Order by Modifieddatetime ");
int i = 0;
if (dtProducts != null && dtProducts.Rows.Count > 0)
{
Rootobject ProductList = new Rootobject();
obj.captureErrorLog("Products records found", Convert.ToString(dtProducts.Rows.Count));
int k = dtProducts.Rows.Count;
foreach (DataRow row in dtProducts.Rows)
{
k--;
i++;
Rootobject ProductList1 = new Rootobject();
Product objPro = new Product();
Product_Variants_Attributes objVar = new Product_Variants_Attributes();
Images_Attributes objimg = new Images_Attributes();
List<Product_Variants_Attributes> Varlist = new List<Product_Variants_Attributes>();
List<Images_Attributes> ImgList = new List<Images_Attributes>();
try
{
objPro.title = obj.AlwaysAString(row["Product_Description"].ToString());
objPro.description = obj.AlwaysAString(row["Product_Description"].ToString());
objPro.notes = obj.AlwaysAString(row["PopUpNotes"].ToString());
// objPro.product_brand_id = obj.AlwaysAString(row["MainProductCode"].ToString());
// objVar.inventory_quantity = Convert.ToInt32(row["OnHand_Inventory"]); // Add this line to populate inventory quantity
objVar.title = obj.AlwaysAString(row["Product_Description"].ToString());
objVar.sku_code = obj.AlwaysAString(row["Central_Product_Code"].ToString());
objVar.cost_price = Convert.ToDecimal(row["Cost_Price"].ToString());
objVar.price = Convert.ToDecimal(row["SellingPrice"].ToString());
objVar.min_reorder_level = Convert.ToInt32(row["ReOrder_Level"]); ;
objVar.quantity_to_reorder = Convert.ToInt32(row["Stock_Max_Level"]);
objVar.tax_rate = Convert.ToInt32(row["Vat_Percentage"].ToString());
objVar.upc_code = obj.AlwaysAString(row["Br_Code"].ToString());
//objVar.Brand = obj.AlwaysAString(row["Supplier_Description"].ToString());
objVar.product_brand_id = obj.AlwaysAString(row["MainProductCode"].ToString());
objVar.on_hand_value = Convert.ToInt32(row["API_InventoryId"]);
Varlist.Add(objVar);
objPro.product_variants_attributes = Varlist;
DataTable DtImages;
DtImages = obj.LoadDataTable("Select Imageurl from ProductImages where centralProductCode='" + obj.AlwaysAString(row["Central_Product_Code"].ToString()) + "'");
if (DtImages.Rows.Count > 0)
{
int n = 0;
foreach (DataRow rowimage in DtImages.Rows)
{
n = n + 1;
objimg.src = rowimage["Imageurl"].ToString();
objimg.display_position = n;
ImgList.Add(objimg);
}
}
objPro.images_attributes = ImgList;
ProductList1.product = objPro;
using (var clientSS = new HttpClient())
{
string strProductsJSON = JsonConvert.SerializeObject(ProductList1);
HttpContent obj1 = new StringContent(strProductsJSON, Encoding.UTF8, "application/json");
try
{
clientSS.DefaultRequestHeaders.Accept.Clear();
clientSS.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
clientSS.DefaultRequestHeaders.Add("x-api-key", APIKEY);
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
clientSS.Timeout = TimeSpan.FromMinutes(10);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)3072;
var responce1 = clientSS.PostAsync(uri, obj1).Result;
var result1 = responce1.Content.ReadAsStringAsync().Result;
if (responce1.IsSuccessStatusCode)
{
{
// obj.captureErrorLog("Product Added --", row["Central_Product_Code"].ToString());
// var result2 = responce1.Content.ReadAsStringAsync().Result;
// Object lstCreditNotes = Newtonsoft.Json.JsonConvert.DeserializeObject<Object>(result2);
dynamic responseJson = JsonConvert.DeserializeObject(result1);
double productId = responseJson.id;
Console.WriteLine("Product ID: " + productId);
// double Product_VarientId = 0;
// ProductId = get_ProductResponce.product.id;
if (productId != 0)
{
// obj.ExecuteSQL("Update Product_Master set APIID=" + ProductId + ",VarientId=" + Product_VarientId + " where Central_Product_Code='" + obj.AlwaysAString(row["Central_Product_Code"].ToString()) + "' ");
// obj.ExecuteSQL("Update ShopifyAPILog set LastUpdatedAt='" + obj.AlwaysAString(row["ModifiedDateTime"].ToString()) + "' where Module='Products' ");
// obj.ExecuteSQL("Update ShopifyAPILog set LastUpdatedAt='" + obj.AlwaysAString(row["ModifiedDateTime"].ToString()) + "' where Module='Product_Updates' ");
//obj.ExecuteSQL("UPDATE Product_Master SET APIID = " + ProductId + " WHERE Central_Product_Code = '" + obj.AlwaysAString(row["Central_Product_Code"].ToString()) + "'");
string centralProductCode = obj.AlwaysAString(row["Central_Product_Code"].ToString());
string updateQuery = "UPDATE Product_Master SET APIId = " + productId + " WHERE Central_Product_Code = '" + centralProductCode + "'";
Console.WriteLine("Update Query: " + updateQuery); // Print update query to verify
obj.ExecuteSQL(updateQuery);
}
}
obj.captureErrorLog("Product Added --", row["Central_Product_Code"].ToString());
}
else
{
string error = responce1.ReasonPhrase.ToString();
obj.captureErrorLog("Error while adding product -- " + error, strProductsJSON);
}
}
catch (Exception e)
{
obj.captureErrorLog("Error While adding Product" + obj.AlwaysAString(row["Central_Product_Code"].ToString()), e.Message);
}
}
}
catch (Exception e)
{
obj.captureErrorLog("Error While adding Product" + obj.AlwaysAString(row["Central_Product_Code"].ToString()), e.Message);
}
}
}
}
catch (Exception e)
{
obj.captureErrorLog("Error While adding Product", e.Message);
}
}
}
}