private void DoWork()
{
using (new WorkerEndScope(this))
{
try
{
using (CRMConnection crm = new CRMConnection())
{
crm.Login();
foreach (string company in Properties.Settings.Default.CompanyList)
{
try
{
if (Stop) { return; } // Check stop before starting each batch.
using (Logger.Scope("ตรวจสอบข้อมูลรหัสบริษัท: " + company))
{
Importer importer = new Importer(this, company);
Logger.Log(Logger.LEVEL_INFO, "ดึงข้อมูล Sale Order จาก Baan");
DataTable orders = BaanStorage.GetSalesOrderByCompany(company);
//importer.FetchBAANSaleOrder();
Logger.Log(Logger.LEVEL_INFO, "จำนวน SO: " + orders.Rows.Count + " รายการ");
int count = 0;
foreach (DataRow row in orders.Rows)
{
try
{
count++;
if (Stop) { return; } // Check stop before starting each item.
if ((count - 1) % 100 == 0)
{
Logger.Log(Logger.LEVEL_INFO, "CHECKING SO: #" + count);
}
string so_orderno = Convert.ToString(row["t$orno"]); // Sales Order No.
using (Logger.Scope("SO: #" + count + " -- " + so_orderno, Logger.LEVEL_DEBUG))
{
bool new_so = false;
Guid crmso_guid;
Entity crmso = crm.FindByBaanCode(CRM.ENTITY_SO, so_orderno);
if (crmso == null)
{
Logger.Log(Logger.LEVEL_DEBUG, "CREATE SO: " + so_orderno);
crmso = new Entity(CRM.ENTITY_SO);
crmso_guid = SaveCRMSO(crm, company, row, crmso, true);
Logger.Log(Logger.LEVEL_DEBUG, "CREATE SO DONE: " + crmso_guid);
// Refresh
crmso = crm.service.Retrieve(CRM.ENTITY_SO, crmso_guid, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
new_so = true;
}
else
{
crmso_guid = crmso.Id;
new_so = false;
}
DataTable solines = BaanStorage.GetSalesOrderLineByCompany(company, so_orderno);
EntityCollection crm_solines = crm.FindSalesOrderLines(crmso_guid);
Logger.Log(Logger.LEVEL_DEBUG, "COUNT SOL BAAN: " + solines.Rows.Count + " CRM: " + crm_solines.Entities.Count);
if (solines.Rows.Count == 0)
{
// Always update old SO with no child.
if (!new_so)
{
Logger.Log(Logger.LEVEL_DEBUG, "UPDATE SO: " + so_orderno);
SaveCRMSO(crm, company, row, crmso, false);
}
}
else
{
bool line_changed = false;
foreach (DataRow linerow in solines.Rows)
{
string soline_position = Convert.ToString(linerow["t$pono"]); // Position Number
string soline_sequence = Convert.ToString(linerow["t$sqnb"]); // Sequence Number
Entity crmline = SearchCRMSoLine(soline_position, soline_sequence, crm_solines);
if (crmline == null)
{
line_changed = true;
Logger.Log(Logger.LEVEL_DEBUG, "CREATE SOL: " + soline_position + " " + soline_sequence);
crmline = new Entity(CRM.ENTITY_SOLINE);
SaveCRMSOLine(crm, company, crmso, linerow, crmline, true);
Logger.Log(Logger.LEVEL_DEBUG, "CREATE SOL DONE");
}
else
{
string invoice = Convert.ToString(crmline["am_txtinvoicenumber"]);
if (string.IsNullOrWhiteSpace(invoice))
{
line_changed = true;
Logger.Log(Logger.LEVEL_DEBUG, "UPDATE SOL: " + soline_position + " " + soline_sequence);
SaveCRMSOLine(crm, company, crmso, linerow, crmline, false);
Logger.Log(Logger.LEVEL_DEBUG, "UPDATE SOL DONE");
}
}
}
if (line_changed)
{
// Update SO if any of line child is changed.
Logger.Log(Logger.LEVEL_DEBUG, "UPDATE SO: " + so_orderno);
SaveCRMSO(crm, company, row, crmso, false);
}
}
}
}
catch (Exception ex)
{
Logger.Log(Logger.LEVEL_ERROR, "ERROR AT SO LEVEL");
Logger.Log(Logger.LEVEL_ERROR, ex.Message);
Logger.Log(Logger.LEVEL_ERROR, ex.StackTrace);
}
}
}
}
catch (Exception ex)
{
Logger.Log(Logger.LEVEL_ERROR, "ERROR AT COMPANY LEVEL");
Logger.Log(Logger.LEVEL_ERROR, ex.Message);
Logger.Log(Logger.LEVEL_ERROR, ex.StackTrace);
}
}
}
}
catch (Exception ex)
{
Logger.Log(Logger.LEVEL_ERROR, ex.Message);
Logger.Log(Logger.LEVEL_ERROR, ex.StackTrace);
}
}
}