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
Title = 'Job Name'
end
item
DataField = 'orderType'
Title = 'Order Type'
end
item
DataField = 'orderDate'
Title = 'Order Date'
end
......@@ -289,6 +293,14 @@ object FViewOrders: TFViewOrders
Title = 'Art Done'
end
item
DataField = 'plateDue'
Title = 'Plate Due'
end
item
DataField = 'plateDone'
Title = 'Plate Done'
end
item
DataField = 'mountDue'
Title = 'Mount Due'
end
......@@ -315,18 +327,6 @@ object FViewOrders: TFViewOrders
item
DataField = '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>
DataSource = wdsOrders
end
......@@ -348,6 +348,9 @@ object FViewOrders: TFViewOrders
object xdwdsOrdersjobName: TStringField
FieldName = 'jobName'
end
object xdwdsOrdersorderType: TStringField
FieldName = 'orderType'
end
object xdwdsOrdersorderDate: TStringField
FieldName = 'orderDate'
end
......@@ -363,6 +366,12 @@ object FViewOrders: TFViewOrders
object xdwdsOrdersartDone: TStringField
FieldName = 'artDone'
end
object xdwdsOrdersplateDue: TStringField
FieldName = 'plateDue'
end
object xdwdsOrdersplateDone: TStringField
FieldName = 'plateDone'
end
object xdwdsOrdersmountDue: TStringField
FieldName = 'mountDue'
end
......@@ -384,15 +393,6 @@ object FViewOrders: TFViewOrders
object xdwdsOrderscolors: TStringField
FieldName = 'colors'
end
object xdwdsOrdersplateDue: TStringField
FieldName = 'plateDue'
end
object xdwdsOrdersplateDone: TStringField
FieldName = 'plateDone'
end
object xdwdsOrdersorderType: TStringField
FieldName = 'orderType'
end
end
object wdsOrders: TWebDataSource
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 -->
<div class="row">
<div class="col-sm">
......
......@@ -428,7 +428,7 @@ begin
xdwdsOrders.Open;
asm
setTimeout(endSpinner, 2000);
endSpinner();
end;
lblEntries.Caption := 'Total entries: ' + IntToStr(integer(orderList['count']));
......
......@@ -94,7 +94,6 @@
<AppDPIAwarenessMode>PerMonitor</AppDPIAwarenessMode>
<TMSUseJSDebugger>2</TMSUseJSDebugger>
<TMSWebSingleInstance>1</TMSWebSingleInstance>
<TMSWebBrowser>5</TMSWebBrowser>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''">
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
......
......@@ -78,6 +78,12 @@ type
end;
TSQLQuery = class
Public
SQL,
whereSQL: string;
end;
TOrderList = class
Public
count: integer;
......
......@@ -11,7 +11,7 @@ interface
uses
XData.Server.Module,
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,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MemDS, DBAccess, Uni,
hyiedefs, hyieutils, iexBitmaps, iesettings, iexLayers, iexRulers,
......@@ -36,7 +36,6 @@ type
function EditUser(const editOptions: string): string;
function Search(phoneNum: string): TOrderList;
procedure GenerateReportPDF(searchOptions: string);
function BuildOrderQuery(searchOptions: string): string;
function AddUser(userInfo: string): string;
function AddItem(itemInfo: string): string;
function DelUser(username: string): string;
......@@ -45,6 +44,8 @@ type
function AddCorrugatedOrder(orderInfo: string): TJSONObject;
function AddStatusSchedule(StatusType: string; order: TJSONObject; ORDER_ID: integer): 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 BeforeDestruction; override;
end;
......@@ -173,7 +174,7 @@ begin
rptOrders := TrptOrders.Create(nil);
try
// Generate SQL dynamically using the existing GetOrders logic
SQL := BuildOrderQuery(searchOptions);
SQL := GenerateOrdersSQL(searchOptions).SQL;
// Prepare the report dataset
rptOrders.PrepareReport(SQL);
......@@ -188,87 +189,6 @@ begin
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;
begin
......@@ -306,9 +226,9 @@ begin
'where oss.ORDER_ID = o.ORDER_ID and oss.ORDER_STATUS = ' + quotedStr(currStatus) + ') AS '+ currStatus + '_DUE,';
end;
function TLookupService.GetOrders(searchOptions: string): TOrderList;
function TLookupService.generateOrdersSQL(searchOptions: string): TSQLQuery;
var
params: TStringList;
params: TStringList;
PageNum: integer;
PageSize: integer;
OrderBy: string;
......@@ -320,14 +240,11 @@ var
statusType: string;
statusSuffix: string;
SQL: string;
Order: TOrderItem;
colors: string;
ColorType: string;
whereSQL: string;
fromSQL: string;
statusTableShort: string;
statusTableLong: string;
begin
result := TSQLQuery.Create;
params := TStringList.Create;
params.StrictDelimiter := true;
// parse the searchOptions
......@@ -348,6 +265,8 @@ begin
statusSuffix := params.Values['filterType'].Split([' '])[1];
filterType := params.Values['filterType'].Split([' '])[0] + '_' + params.Values['filterType'].Split([' '])[1];
end;
// Figure out what table the status belongs to
if statusSuffix = 'DUE' then
begin
statusTableShort := 'oss';
......@@ -373,11 +292,15 @@ 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, '
end;
// Generate the subqueries that get the Status Dates(Due and Done)
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');
// Generate the Where SQL based on if there is a filter applied or not
if filterType <> '' then
begin
whereSQL := 'FROM orders o join ' + statusTableLong + ' ' + statusTableShort +
......@@ -385,7 +308,7 @@ begin
'customers c on c.CUSTOMER_ID = o.COMPANY_ID left join ' +
'qb_sales_orders qb on qb.ORDER_ID = o.ORDER_ID ' +
'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 ' +
quotedStr(endDate) + ' >= ' + statusTableShort + '.STATUS_DATE';
......@@ -399,6 +322,45 @@ begin
SQL := SQL + 'o.PRICE, qb.QB_REF_NUM ' + whereSQL + ' LIMIT ' + limit + ' OFFSET ' + offset;
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);
Result:= TOrderList.Create;
......@@ -440,7 +402,7 @@ begin
doQuery(ordersDB.UniQuery2, SQL);
colors := ordersDB.UniQuery2.FieldByName(ColorType).AsString;
order.colors := colors;
order.colors := getColorCount(colors);
ordersDB.UniQuery1.Next;
end;
......
......@@ -20,25 +20,13 @@ uses
procedure LoadDatabaseSettings( uc: TUniConnection; iniFilename: string );
var
iniFile: TIniFile;
iniStr: string;
begin
iniFile := TIniFile.Create( ExtractFilePath(Application.ExeName) + iniFilename );
try
iniStr := iniFile.ReadString('Database', 'Server', '');
if not iniStr.IsEmpty then
uc.Server := iniStr;
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;
uc.Server := iniFile.ReadString('Database', 'Server', uc.Server);
uc.Database := iniFile.ReadString('Database', 'Database', uc.Database);
uc.Username := iniFile.ReadString('Database', 'Username', uc.Username);
uc.Password := iniFile.ReadString('Database', 'Password', uc.Password);
finally
iniFile.Free;
end;
......
[Settings]
MemoLogLevel=4
FileLogLevel=5
LogFileNum=31
LogFileNum=48
webClientVersion=1.0.0
[Database]
--Server=192.168.159.132
Server=192.168.60.129
Server=192.168.159.132
--Server=192.168.60.129
--Server=192.168.75.133
--Database=
--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