Commit 37bffa08 by cam

cleaned up SQL queries on the server end in preparation for PDF report.

parent f61278c7
...@@ -269,6 +269,10 @@ object FViewOrders: TFViewOrders ...@@ -269,6 +269,10 @@ object FViewOrders: TFViewOrders
Title = 'Job Name' Title = 'Job Name'
end end
item item
DataField = 'orderType'
Title = 'Order Type'
end
item
DataField = 'orderDate' DataField = 'orderDate'
Title = 'Order Date' Title = 'Order Date'
end end
...@@ -289,6 +293,14 @@ object FViewOrders: TFViewOrders ...@@ -289,6 +293,14 @@ object FViewOrders: TFViewOrders
Title = 'Art Done' Title = 'Art Done'
end end
item item
DataField = 'plateDue'
Title = 'Plate Due'
end
item
DataField = 'plateDone'
Title = 'Plate Done'
end
item
DataField = 'mountDue' DataField = 'mountDue'
Title = 'Mount Due' Title = 'Mount Due'
end end
...@@ -315,18 +327,6 @@ object FViewOrders: TFViewOrders ...@@ -315,18 +327,6 @@ object FViewOrders: TFViewOrders
item item
DataField = 'colors' DataField = 'colors'
Title = 'Colors' Title = 'Colors'
end
item
DataField = 'plateDue'
Title = 'Plate Due'
end
item
DataField = 'plateDone'
Title = 'Plate Done'
end
item
DataField = 'orderType'
Title = 'Order Type'
end> end>
DataSource = wdsOrders DataSource = wdsOrders
end end
...@@ -348,6 +348,9 @@ object FViewOrders: TFViewOrders ...@@ -348,6 +348,9 @@ object FViewOrders: TFViewOrders
object xdwdsOrdersjobName: TStringField object xdwdsOrdersjobName: TStringField
FieldName = 'jobName' FieldName = 'jobName'
end end
object xdwdsOrdersorderType: TStringField
FieldName = 'orderType'
end
object xdwdsOrdersorderDate: TStringField object xdwdsOrdersorderDate: TStringField
FieldName = 'orderDate' FieldName = 'orderDate'
end end
...@@ -363,6 +366,12 @@ object FViewOrders: TFViewOrders ...@@ -363,6 +366,12 @@ object FViewOrders: TFViewOrders
object xdwdsOrdersartDone: TStringField object xdwdsOrdersartDone: TStringField
FieldName = 'artDone' FieldName = 'artDone'
end end
object xdwdsOrdersplateDue: TStringField
FieldName = 'plateDue'
end
object xdwdsOrdersplateDone: TStringField
FieldName = 'plateDone'
end
object xdwdsOrdersmountDue: TStringField object xdwdsOrdersmountDue: TStringField
FieldName = 'mountDue' FieldName = 'mountDue'
end end
...@@ -384,15 +393,6 @@ object FViewOrders: TFViewOrders ...@@ -384,15 +393,6 @@ object FViewOrders: TFViewOrders
object xdwdsOrderscolors: TStringField object xdwdsOrderscolors: TStringField
FieldName = 'colors' FieldName = 'colors'
end end
object xdwdsOrdersplateDue: TStringField
FieldName = 'plateDue'
end
object xdwdsOrdersplateDone: TStringField
FieldName = 'plateDone'
end
object xdwdsOrdersorderType: TStringField
FieldName = 'orderType'
end
end end
object wdsOrders: TWebDataSource object wdsOrders: TWebDataSource
DataSet = xdwdsOrders DataSet = xdwdsOrders
......
<div class="container h-100 d-flex flex-column mt-0" style="max-width: 95%;"> <div class="container h-100 d-flex flex-column mt-0" style="max-width: 100%;">
<!-- Alert Section --> <!-- Alert Section -->
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
......
...@@ -428,7 +428,7 @@ begin ...@@ -428,7 +428,7 @@ begin
xdwdsOrders.Open; xdwdsOrders.Open;
asm asm
setTimeout(endSpinner, 2000); endSpinner();
end; end;
lblEntries.Caption := 'Total entries: ' + IntToStr(integer(orderList['count'])); lblEntries.Caption := 'Total entries: ' + IntToStr(integer(orderList['count']));
......
...@@ -94,7 +94,6 @@ ...@@ -94,7 +94,6 @@
<AppDPIAwarenessMode>PerMonitor</AppDPIAwarenessMode> <AppDPIAwarenessMode>PerMonitor</AppDPIAwarenessMode>
<TMSUseJSDebugger>2</TMSUseJSDebugger> <TMSUseJSDebugger>2</TMSUseJSDebugger>
<TMSWebSingleInstance>1</TMSWebSingleInstance> <TMSWebSingleInstance>1</TMSWebSingleInstance>
<TMSWebBrowser>5</TMSWebBrowser>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''"> <PropertyGroup Condition="'$(Cfg_2)'!=''">
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols> <DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
......
...@@ -78,6 +78,12 @@ type ...@@ -78,6 +78,12 @@ type
end; end;
TSQLQuery = class
Public
SQL,
whereSQL: string;
end;
TOrderList = class TOrderList = class
Public Public
count: integer; count: integer;
......
...@@ -11,7 +11,7 @@ interface ...@@ -11,7 +11,7 @@ interface
uses uses
XData.Server.Module, XData.Server.Module,
XData.Service.Common, XData.Service.Common,
Api.Database, Data.DB, frxClass, frxExportPDF, Api.Database, Data.DB, frxClass, frxExportPDF, JS,
Lookup.Service, System.Hash, System.JSON, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Lookup.Service, System.Hash, System.JSON, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MemDS, DBAccess, Uni, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MemDS, DBAccess, Uni,
hyiedefs, hyieutils, iexBitmaps, iesettings, iexLayers, iexRulers, hyiedefs, hyieutils, iexBitmaps, iesettings, iexLayers, iexRulers,
...@@ -36,7 +36,6 @@ type ...@@ -36,7 +36,6 @@ type
function EditUser(const editOptions: string): string; function EditUser(const editOptions: string): string;
function Search(phoneNum: string): TOrderList; function Search(phoneNum: string): TOrderList;
procedure GenerateReportPDF(searchOptions: string); procedure GenerateReportPDF(searchOptions: string);
function BuildOrderQuery(searchOptions: string): string;
function AddUser(userInfo: string): string; function AddUser(userInfo: string): string;
function AddItem(itemInfo: string): string; function AddItem(itemInfo: string): string;
function DelUser(username: string): string; function DelUser(username: string): string;
...@@ -45,6 +44,8 @@ type ...@@ -45,6 +44,8 @@ type
function AddCorrugatedOrder(orderInfo: string): TJSONObject; function AddCorrugatedOrder(orderInfo: string): TJSONObject;
function AddStatusSchedule(StatusType: string; order: TJSONObject; ORDER_ID: integer): string; function AddStatusSchedule(StatusType: string; order: TJSONObject; ORDER_ID: integer): string;
function EditStatusSchedule(StatusType: string; order: TJSONObject; ORDER_ID: string): string; function EditStatusSchedule(StatusType: string; order: TJSONObject; ORDER_ID: string): string;
function generateOrdersSQL(searchOptions: string): TSQLQuery;
function getColorCount(colors: string): string;
procedure AfterConstruction; override; procedure AfterConstruction; override;
procedure BeforeDestruction; override; procedure BeforeDestruction; override;
end; end;
...@@ -173,7 +174,7 @@ begin ...@@ -173,7 +174,7 @@ begin
rptOrders := TrptOrders.Create(nil); rptOrders := TrptOrders.Create(nil);
try try
// Generate SQL dynamically using the existing GetOrders logic // Generate SQL dynamically using the existing GetOrders logic
SQL := BuildOrderQuery(searchOptions); SQL := GenerateOrdersSQL(searchOptions).SQL;
// Prepare the report dataset // Prepare the report dataset
rptOrders.PrepareReport(SQL); rptOrders.PrepareReport(SQL);
...@@ -188,87 +189,6 @@ begin ...@@ -188,87 +189,6 @@ begin
end; end;
end; end;
function TLookupService.BuildOrderQuery(searchOptions: string): string;
var
params: TStringList;
PageNum, PageSize: integer;
startDate, endDate, filterType, statusType, statusSuffix: string;
statusTableShort, statusTableLong, whereSQL, SQL: string;
begin
params := TStringList.Create;
try
params.StrictDelimiter := true;
params.Delimiter := '&';
params.DelimitedText := searchOptions;
// Parse parameters
PageNum := StrToIntDef(params.Values['pagenumber'], 1);
PageSize := StrToIntDef(params.Values['pagesize'], 50);
startDate := params.Values['startDate'];
endDate := params.Values['endDate'];
filterType := params.Values['filterType'];
statusType := '';
statusSuffix := '';
if filterType <> '' then
begin
statusType := filterType.Split(['_'])[0];
statusSuffix := filterType.Split(['_'])[1];
filterType := statusType + '_' + statusSuffix;
end;
// Determine status table
if statusSuffix = 'DUE' then
begin
statusTableShort := 'oss';
statusTableLong := 'orders_status_schedule';
end
else
begin
statusTableShort := 'os';
statusTableLong := 'orders_status';
end;
// Build the SELECT query with dynamically generated subqueries
SQL := 'SELECT o.ORDER_ID, c.NAME AS COMPANY_NAME, o.JOB_NAME, o.ORDER_DATE, o.ORDER_TYPE, ';
// Add dynamically generated subqueries
SQL := SQL + generateSubQuery(filterType, statusType, 'PROOF');
SQL := SQL + generateSubQuery(filterType, statusType, 'ART');
SQL := SQL + generateSubQuery(filterType, statusType, 'PLATE');
SQL := SQL + generateSubQuery(filterType, statusType, 'MOUNT');
SQL := SQL + generateSubQuery(filterType, statusType, 'SHIP');
// Include additional static fields
SQL := SQL + 'o.PRICE, qb.QB_REF_NUM ';
// FROM clause
SQL := SQL + 'FROM orders o ' +
'JOIN customers c ON o.COMPANY_ID = c.CUSTOMER_ID ' +
'LEFT JOIN qb_sales_orders qb ON qb.ORDER_ID = o.ORDER_ID ';
// WHERE clause
whereSQL := 'WHERE 1=1 ';
if startDate <> '' then
whereSQL := whereSQL + ' AND o.ORDER_DATE >= ' + QuotedStr(startDate);
if endDate <> '' then
whereSQL := whereSQL + ' AND o.ORDER_DATE <= ' + QuotedStr(endDate);
if statusType <> '' then
whereSQL := whereSQL + ' AND ' + statusTableShort + '.ORDER_STATUS = ' + QuotedStr(statusType);
// Add WHERE and ORDER clauses
SQL := SQL + whereSQL +
' ORDER BY o.ORDER_DATE DESC ' +
' LIMIT ' + IntToStr(PageSize) +
' OFFSET ' + IntToStr((PageNum - 1) * PageSize);
Result := SQL;
Logger.log(5, 'Generated SQL in Build order Query: ' + SQL);
finally
params.Free;
end;
end;
function TLookupService.generateSelectSQL(filterType, startDate, endDate: string): string; function TLookupService.generateSelectSQL(filterType, startDate, endDate: string): string;
begin begin
...@@ -306,9 +226,9 @@ begin ...@@ -306,9 +226,9 @@ begin
'where oss.ORDER_ID = o.ORDER_ID and oss.ORDER_STATUS = ' + quotedStr(currStatus) + ') AS '+ currStatus + '_DUE,'; 'where oss.ORDER_ID = o.ORDER_ID and oss.ORDER_STATUS = ' + quotedStr(currStatus) + ') AS '+ currStatus + '_DUE,';
end; end;
function TLookupService.GetOrders(searchOptions: string): TOrderList; function TLookupService.generateOrdersSQL(searchOptions: string): TSQLQuery;
var var
params: TStringList; params: TStringList;
PageNum: integer; PageNum: integer;
PageSize: integer; PageSize: integer;
OrderBy: string; OrderBy: string;
...@@ -320,14 +240,11 @@ var ...@@ -320,14 +240,11 @@ var
statusType: string; statusType: string;
statusSuffix: string; statusSuffix: string;
SQL: string; SQL: string;
Order: TOrderItem;
colors: string;
ColorType: string;
whereSQL: string; whereSQL: string;
fromSQL: string;
statusTableShort: string; statusTableShort: string;
statusTableLong: string; statusTableLong: string;
begin begin
result := TSQLQuery.Create;
params := TStringList.Create; params := TStringList.Create;
params.StrictDelimiter := true; params.StrictDelimiter := true;
// parse the searchOptions // parse the searchOptions
...@@ -348,6 +265,8 @@ begin ...@@ -348,6 +265,8 @@ begin
statusSuffix := params.Values['filterType'].Split([' '])[1]; statusSuffix := params.Values['filterType'].Split([' '])[1];
filterType := params.Values['filterType'].Split([' '])[0] + '_' + params.Values['filterType'].Split([' '])[1]; filterType := params.Values['filterType'].Split([' '])[0] + '_' + params.Values['filterType'].Split([' '])[1];
end; end;
// Figure out what table the status belongs to
if statusSuffix = 'DUE' then if statusSuffix = 'DUE' then
begin begin
statusTableShort := 'oss'; statusTableShort := 'oss';
...@@ -373,11 +292,15 @@ begin ...@@ -373,11 +292,15 @@ begin
begin begin
SQL := 'SELECT o.ORDER_ID, o.LOCATION AS Loc, c.NAME AS COMPANY_NAME, o.JOB_NAME, o.ORDER_DATE, o.ORDER_TYPE, ' SQL := 'SELECT o.ORDER_ID, o.LOCATION AS Loc, c.NAME AS COMPANY_NAME, o.JOB_NAME, o.ORDER_DATE, o.ORDER_TYPE, '
end; end;
// Generate the subqueries that get the Status Dates(Due and Done)
SQL := SQL + generateSubquery(filterType, statusType, 'PROOF'); SQL := SQL + generateSubquery(filterType, statusType, 'PROOF');
SQL := SQL + generateSubquery(filterType, statusType, 'ART'); SQL := SQL + generateSubquery(filterType, statusType, 'ART');
SQL := SQL + generateSubquery(filterType, statusType, 'PLATE'); SQL := SQL + generateSubquery(filterType, statusType, 'PLATE');
SQL := SQL + generateSubquery(filterType, statusType, 'MOUNT'); SQL := SQL + generateSubquery(filterType, statusType, 'MOUNT');
SQL := SQL + generateSubquery(filterType, statusType, 'SHIP'); SQL := SQL + generateSubquery(filterType, statusType, 'SHIP');
// Generate the Where SQL based on if there is a filter applied or not
if filterType <> '' then if filterType <> '' then
begin begin
whereSQL := 'FROM orders o join ' + statusTableLong + ' ' + statusTableShort + whereSQL := 'FROM orders o join ' + statusTableLong + ' ' + statusTableShort +
...@@ -385,7 +308,7 @@ begin ...@@ -385,7 +308,7 @@ begin
'customers c on c.CUSTOMER_ID = o.COMPANY_ID left join ' + 'customers c on c.CUSTOMER_ID = o.COMPANY_ID left join ' +
'qb_sales_orders qb on qb.ORDER_ID = o.ORDER_ID ' + 'qb_sales_orders qb on qb.ORDER_ID = o.ORDER_ID ' +
'WHERE ' + statusTableShort + '.ORDER_ID = o.ORDER_ID AND + ' + 'WHERE ' + statusTableShort + '.ORDER_ID = o.ORDER_ID AND + ' +
statusTableShort + '.ORDER_STATUS = '+ quotedStr(statusType) + statusTableShort + '.ORDER_STATUS = ' + quotedStr(statusType) +
' AND ' + quotedStr(startDate) + ' <= '+ statusTableShort + '.STATUS_DATE AND ' + ' AND ' + quotedStr(startDate) + ' <= '+ statusTableShort + '.STATUS_DATE AND ' +
quotedStr(endDate) + ' >= ' + statusTableShort + '.STATUS_DATE'; quotedStr(endDate) + ' >= ' + statusTableShort + '.STATUS_DATE';
...@@ -399,6 +322,45 @@ begin ...@@ -399,6 +322,45 @@ begin
SQL := SQL + 'o.PRICE, qb.QB_REF_NUM ' + whereSQL + ' LIMIT ' + limit + ' OFFSET ' + offset; SQL := SQL + 'o.PRICE, qb.QB_REF_NUM ' + whereSQL + ' LIMIT ' + limit + ' OFFSET ' + offset;
end; end;
// send the SQL and whereSQL back. whereSQL is needed to get the total count.
// probably a way to getthe TotalCount in original SQL but it was giving me issues
result.SQL := SQL;
result.whereSQL := whereSQL;
end;
function TLookupService.getColorCount(colors: string): string;
var
colorObject: TJSONObject;
colorList: TJSONArray;
temp: string;
temp2: string;
begin
if colors = '' then
result := '0'
else
begin
colorObject := TJSONObject.ParseJSONValue(colors) as TJSONObject;
colorList := TJSONArray(colorObject.GetValue('items'));
//temp := colorList.toString;
result := IntToStr(colorList.Count);
end;
end;
function TLookupService.GetOrders(searchOptions: string): TOrderList;
var
SQL: string;
whereSQL: string;
Order: TOrderItem;
colors: string;
ColorType: string;
SQLArray: TArray<string>;
SQLQuery: TSQLQuery;
begin
SQLQuery := generateOrdersSQL(searchOptions);
SQL := SQLQuery.SQL;
whereSQL := SQLQuery.whereSQL;
doQuery(ordersDB.UniQuery1, SQL); doQuery(ordersDB.UniQuery1, SQL);
Result:= TOrderList.Create; Result:= TOrderList.Create;
...@@ -440,7 +402,7 @@ begin ...@@ -440,7 +402,7 @@ begin
doQuery(ordersDB.UniQuery2, SQL); doQuery(ordersDB.UniQuery2, SQL);
colors := ordersDB.UniQuery2.FieldByName(ColorType).AsString; colors := ordersDB.UniQuery2.FieldByName(ColorType).AsString;
order.colors := colors; order.colors := getColorCount(colors);
ordersDB.UniQuery1.Next; ordersDB.UniQuery1.Next;
end; end;
......
...@@ -20,25 +20,13 @@ uses ...@@ -20,25 +20,13 @@ uses
procedure LoadDatabaseSettings( uc: TUniConnection; iniFilename: string ); procedure LoadDatabaseSettings( uc: TUniConnection; iniFilename: string );
var var
iniFile: TIniFile; iniFile: TIniFile;
iniStr: string;
begin begin
iniFile := TIniFile.Create( ExtractFilePath(Application.ExeName) + iniFilename ); iniFile := TIniFile.Create( ExtractFilePath(Application.ExeName) + iniFilename );
try try
iniStr := iniFile.ReadString('Database', 'Server', ''); uc.Server := iniFile.ReadString('Database', 'Server', uc.Server);
if not iniStr.IsEmpty then uc.Database := iniFile.ReadString('Database', 'Database', uc.Database);
uc.Server := iniStr; uc.Username := iniFile.ReadString('Database', 'Username', uc.Username);
uc.Password := iniFile.ReadString('Database', 'Password', uc.Password);
iniStr := iniFile.ReadString('Database', 'Database', '');
if not iniStr.IsEmpty then
uc.Database := iniStr;
iniStr := iniFile.ReadString('Database', 'Username', '');
if not iniStr.IsEmpty then
uc.Username := iniStr;
iniStr := iniFile.ReadString('Database', 'Password', '');
if not iniStr.IsEmpty then
uc.Password := iniStr;
finally finally
iniFile.Free; iniFile.Free;
end; end;
......
[Settings] [Settings]
MemoLogLevel=4 MemoLogLevel=4
FileLogLevel=5 FileLogLevel=5
LogFileNum=31 LogFileNum=48
webClientVersion=1.0.0 webClientVersion=1.0.0
[Database] [Database]
--Server=192.168.159.132 Server=192.168.159.132
Server=192.168.60.129 --Server=192.168.60.129
--Server=192.168.75.133 --Server=192.168.75.133
--Database= --Database=
--Username= --Username=
Password=emsys!012 --Password=emsys!012
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment