Commit dd3cf055 by emsys

finished all QB links needed to update Dave's database

parent 487df666
......@@ -261,6 +261,9 @@ object fViewAddItem: TfViewAddItem
object xdwdsCustomersstatus: TStringField
FieldName = 'status'
end
object xdwdsCustomersqb_items_qb_id: TStringField
FieldName = 'qb_items_qb_id'
end
end
object wdsCustomers: TWebDataSource
DataSet = xdwdsCustomers
......
......@@ -29,6 +29,7 @@ type
xdwdsCustomersqb_item_name: TStringField;
xdwdsCustomersitem_desc: TStringField;
xdwdsCustomersstatus: TStringField;
xdwdsCustomersqb_items_qb_id: TStringField;
procedure WebFormCreate(Sender: TObject);
procedure WebFormShow(Sender: TObject);
procedure TMSFNCGrid1CellClick(Sender: TObject; ACol, ARow: Integer);
......@@ -157,6 +158,7 @@ begin
ItemJSON.AddPair('qb_item_name', xdwdsCustomers.FieldByName('qb_item_name').AsString);
ItemJSON.AddPair('item_desc', xdwdsCustomers.FieldByName('item_desc').AsString);
ItemJSON.AddPair('status', xdwdsCustomers.FieldByName('status').AsString);
ItemJSON.AddPair('qb_items_qb_id', xdwdsCustomers.FieldByName('qb_items_qb_id').AsString);
ItemJSON.AddPair('mode', 'ADD');
......
......@@ -12,7 +12,7 @@ uses
System.SysUtils, System.Classes, Web, WEBLib.Graphics, WEBLib.Forms, WEBLib.Dialogs,
Vcl.Controls, Vcl.StdCtrls, WEBLib.StdCtrls, WEBLib.Controls, WEBLib.Grids, WebLib.Lists,
XData.Web.Client, WEBLib.ExtCtrls, DB, XData.Web.JsonDataset,
XData.Web.Dataset, XData.Web.Connection, Vcl.Forms, DateUtils, WebAudio;
XData.Web.Dataset, XData.Web.Connection, Vcl.Forms, DateUtils;
type
TFViewItems = class(TWebForm)
......@@ -404,7 +404,7 @@ var
begin
newform := TFViewAddItem.CreateNew;
newform.Caption := 'Select Customer and Order Type';
newform.Caption := 'Select Item to Add';
newForm.Popup := True;
newForm.position:= poScreenCenter;
newForm.Border := fbDialog;
......
......@@ -1271,6 +1271,7 @@ object FOrderEntryCorrugated: TFOrderEntryCorrugated
Top = 256
end
object XDataWebDataSet1: TXDataWebDataSet
BeforePost = XDataWebDataSet1BeforePost
Connection = DMConnection.ApiConnection
Left = 1060
Top = 182
......
......@@ -237,6 +237,7 @@ type
procedure ShowAddAddressForm();
[async] procedure SendAddressToServer(AddressJSON: TJSONObject);
procedure edtJobNameChange(Sender: TObject);
procedure XDataWebDataSet1BeforePost(DataSet: TDataSet);
private
FModalAction: string;
FAgencyCode: string;
......@@ -878,6 +879,8 @@ begin
XDataWebDataSet1.Close;
XDataWebDataSet1.SetJsonData(order);
XDataWebDataSet1.Open;
XDataWebDataSet1.Edit;
if XDataWebDataSet1colors_colors.Value <> '' then
begin
colorObject := TJSObject(TJSJSON.parse(XDataWebDataSet1colors_colors.Value));
......@@ -1075,6 +1078,12 @@ begin
end;
procedure TFOrderEntryCorrugated.XDataWebDataSet1BeforePost(DataSet: TDataSet);
begin
if not (DataSet.State in [dsEdit, dsInsert]) then
DataSet.Edit; // Force edit mode if autofill tries to post
end;
procedure TFOrderEntryCorrugated.xdwdsQBItemAfterEdit(DataSet: TDataSet);
begin
EditMode();
......
......@@ -2009,6 +2009,8 @@ begin
else
ordersDB.UniQuery1.FieldByName('status').AsString := 'INACTIVE';
ordersDB.UniQuery1.FieldByName('qb_items_qb_id').AsString := JSONData.GetValue<string>('qb_items_qb_id');
ordersDB.UniQuery1.Post;
Result.AddPair('msg', 'Success: Item successfully added');
end
......@@ -2656,6 +2658,7 @@ begin
else
ParsedItem.AddPair('item_desc', 'N/A');
ParsedItem.AddPair('status', item.GetValue<string>('Active'));
ParsedItem.AddPair('qb_items_qb_id', item.GetValue<string>('Id'));
Result.AddElement(ParsedItem);
end;
......
......@@ -69,9 +69,9 @@ object fQB: TfQB
end
object Memo1: TMemo
Left = 0
Top = 53
Top = 63
Width = 982
Height = 228
Height = 218
Align = alBottom
Lines.Strings = (
'')
......@@ -239,7 +239,7 @@ object fQB: TfQB
SortSettings.HeaderColorTo = clWhite
SortSettings.HeaderMirrorColor = clWhite
SortSettings.HeaderMirrorColorTo = clWhite
Version = '9.1.4.1'
Version = '9.1.4.5'
ColWidths = (
64
64
......@@ -394,7 +394,7 @@ object fQB: TfQB
SortSettings.HeaderColorTo = clWhite
SortSettings.HeaderMirrorColor = clWhite
SortSettings.HeaderMirrorColorTo = clWhite
Version = '9.1.4.1'
Version = '9.1.4.5'
ColWidths = (
64
64
......@@ -423,6 +423,42 @@ object fQB: TfQB
TabOrder = 10
OnClick = Button8Click
end
object Button9: TButton
Left = 121
Top = 32
Width = 95
Height = 25
Caption = 'Get Estimate'
TabOrder = 11
OnClick = Button9Click
end
object Button10: TButton
Left = 222
Top = 32
Width = 75
Height = 25
Caption = 'Update QB ID'
TabOrder = 12
OnClick = Button10Click
end
object Button11: TButton
Left = 316
Top = 32
Width = 113
Height = 25
Caption = 'update QB Items'
TabOrder = 13
OnClick = Button11Click
end
object Button12: TButton
Left = 435
Top = 32
Width = 190
Height = 25
Caption = 'Delete Customers without orders'
TabOrder = 14
OnClick = Button12Click
end
object uq: TUniQuery
Connection = ApiDatabase.ucKG
SQL.Strings = (
......
unit qbAPI;
unit qbAPI;
interface
......@@ -117,6 +117,10 @@ type
asgData: TAdvStringGrid;
Splitter2: TSplitter;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
......@@ -131,6 +135,10 @@ type
procedure Panel1Resize(Sender: TObject);
procedure Panel2Resize(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
private
{ Private declarations }
strict private
......@@ -140,6 +148,7 @@ type
var
AccessToken,RefreshToken,CompanyID,Client,Secret: string;
LastRefresh: TDateTime;
ID, QB_ID: string;
public
{ Public declarations }
procedure findMatches(CustomerList: TJSONArray; JSONfield, DBField: string);
......@@ -150,6 +159,12 @@ type
procedure ConfigureSSL(IOHandler: TIdSSLIOHandlerSocketOpenSSL);
procedure SaveTokens(AccessToken, RefreshToken: string);
procedure getCustomers();
procedure addEstimate(orderInfo: string);
procedure getQBCustomers();
procedure ShowDeleteConfirm(msg: string);
procedure ShowLinkConfirm(msg: string);
procedure DeleteCustomers();
procedure LinkCustomers();
end;
var
......@@ -168,21 +183,239 @@ var
begin
origin := TPoint.Create(1,1);
point := asgData.find(origin, asgData2.Cells[2, ARow], []);
if point.Y = -1 then
point.Y := 0;
if point.X = -1 then
point.X := 0;
asgData.Row := point.Y;
asgData.Col := point.X;
if ( (point.X <> 0) and (point.Y <> 0) ) then
begin
QB_ID := asgData.Cells[1, asgData.Row];
ID := asgData2.Cells[1, asgData2.Row];
end
else
ID := asgData2.Cells[1, asgData2.Row];
end;
procedure TfQB.asgDataClickCell(Sender: TObject; ARow, ACol: Integer);
var
point, origin: TPoint;
begin
origin := TPoint.Create(1,1);
point := asgData2.find(origin, asgData.Cells[2, ARow], []);
asgData2.Row := point.Y;
asgData2.Col := point.X;
QB_ID := asgData.Cells[1, asgData.Row];
end;
procedure TfQB.Button10Click(Sender: TObject);
var
msg: string;
begin
if( ( QB_ID = '' ) or ( ID = '' ) ) then
begin
Memo1.Lines.Add('Please select a Quickbooks Customer and a KG Orders Customer');
Memo1.Lines.Add('Current QB ID: ' + QB_ID);
Memo1.Lines.Add('Current KG Orders ID: ' + ID);
end
else
begin
msg := 'Are you sure you want to link QB ID: ' + QB_ID + ' with KG Orders ID: ' + ID;
ShowLinkConfirm(msg);
end;
end;
procedure TfQB.LinkCustomers();
var
sql: string;
begin
sql := 'select QB_LIST_ID from customers where CUSTOMER_ID = ' + ID;
doQuery(ordersDB.UniQuery1, SQL);
ordersDB.UniQuery1.Edit;
ordersDB.UniQuery1.FieldByName('QB_LIST_ID').AsString := QB_ID;
ordersDB.UniQuery1.Post;
getCustomers();
QB_ID := '';
ID := '';
end;
procedure TfQB.ShowLinkConfirm(msg: string);
begin
if MessageDlg(msg, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
// User confirmed
LinkCustomers();
end
else
begin
// User canceled
ShowMessage('Cancelled.');
end;
end;
procedure TfQB.Button11Click(Sender: TObject);
var
restClient: TRESTClient;
restRequest: TRESTRequest;
restResponse: TRESTResponse;
param: TRESTRequestParameter;
res: string;
jsValue: TJSONValue;
jsObj: TJSONObject;
ItemList: TJSONArray;
pair: TJSONPair;
ModifiedList: TJSONArray;
ParsedCustomer, Item: TJSONObject;
I: integer;
BillAddr: TJSONObject;
ShipAddr: TJSONObject;
sql: string;
begin
restClient := TRESTClient.Create(nil);
restClient.BaseURL := 'https://sandbox-quickbooks.api.intuit.com';
restRequest := TRESTRequest.Create(nil);
restRequest.Client := restClient;
restResponse := TRESTResponse.Create(nil);
restRequest.Response := restResponse;
if MinutesBetween(Now, LastRefresh) > 58 then
begin
RefreshAccessToken();
end;
restRequest.Method := rmGET;
//res := '/v3/company/' + companyID + '/preferences';
res := '/v3/company/' + companyID + '/query?query=select * from Item&minorversion=75'; // '/customer/58';
restRequest.Resource := res;
param := restRequest.Params.AddItem;
param.Name := 'Authorization';
param.Kind := pkHTTPHEADER;
param.Options := param.Options + [TRESTRequestParameterOption.poDoNotEncode];
param.Value := 'Bearer ' + AccessToken;
restRequest.Execute;
//memo1.Lines.Add(restresponse.Content);
jsValue := restResponse.JSONValue;
jsObj := TJSONObject(jsValue);
//CustomerList := TJSONArray(restResponse.JSONValue);
Memo1.Lines.Add( jsObj.Format(2) );
ItemList := TJSONArray( TJSONObject( jsObj.GetValue('QueryResponse') ).GetValue('Item'));
for i := 0 to ItemList.Count - 1 do
begin
Item := ItemList.Items[i] as TJSONObject;
sql := 'select * from qb_items where qb_item_name = ' + quotedStr(Item.GetValue<string>('Name'));
doQuery(ordersDB.UniQuery1, sql);
if ( not ordersDB.UniQuery1.IsEmpty ) then
begin
ordersDB.UniQuery1.Edit;
ordersDB.uniquery1.FieldByName('qb_items_qb_id').AsString := Item.GetValue<string>('Id');
ordersDB.UniQuery1.Post;
end;
end;
// Load customer info
restClient.Free;
restRequest.Free;
restResponse.Free;
end;
procedure TfQB.Button12Click(Sender: TObject);
var
SQL: string;
cust_count, ship_count: string;
msg: string;
begin
SQL := 'select count(*) as cust_count from customers c ' +
' WHERE NOT EXISTS (SELECT 1 FROM corrugated_plate_orders cpo WHERE cpo.COMPANY_ID = c.CUSTOMER_ID) '+
'AND NOT EXISTS (SELECT 1 FROM web_plate_orders wpo WHERE wpo.COMPANY_ID = c.CUSTOMER_ID) ORDER BY c.SHORT_NAME';
DoQuery(ordersDB.UniQuery1, SQL);
cust_count := ordersDB.UniQuery1.FieldByName('cust_count').AsString;
SQL := 'select count(*) as ship_count from customers c join customers_ship cs on c.CUSTOMER_ID = cs.customer_id' +
' WHERE NOT EXISTS (SELECT 1 FROM corrugated_plate_orders cpo WHERE cpo.COMPANY_ID = c.CUSTOMER_ID) '+
'AND NOT EXISTS (SELECT 1 FROM web_plate_orders wpo WHERE wpo.COMPANY_ID = c.CUSTOMER_ID) ORDER BY c.SHORT_NAME';
DoQuery(ordersDB.UniQuery1, SQL);
ship_count := ordersDB.UniQuery1.FieldByName('ship_count').AsString;
msg := 'You will be deleting ' + cust_count + ' customers and their ' + ship_count + ' assoicated shipping addresses. Are you sure you want to continue?';
ShowDeleteConfirm(msg);
end;
procedure TfQB.DeleteCustomers();
var
SQL: string;
count: integer;
ModifiedList: TJSONArray;
Customer, ParsedCustomer: TJSONObject;
I: integer;
ship_count, cust_count: integer;
begin
Memo1.Lines.Add('Beginning to Delete Customers');
SQL := 'SELECT * FROM customers c ' +
'WHERE NOT EXISTS (SELECT 1 FROM corrugated_plate_orders cpo WHERE cpo.COMPANY_ID = c.CUSTOMER_ID) '+
'AND NOT EXISTS (SELECT 1 FROM web_plate_orders wpo WHERE wpo.COMPANY_ID = c.CUSTOMER_ID) ORDER BY c.SHORT_NAME';
doQuery(ordersDB.UniQuery1, SQL);
cust_count := 0;
ship_count := 0;
while( not ordersDB.UniQuery1.Eof ) do
begin
SQL := 'Select * from customers_ship where customer_id = ' + ordersDB.UniQuery1.FieldByName('CUSTOMER_ID').AsString;
doQuery(ordersDB.UniQuery2, SQL);
ordersDB.UniQuery2.First;
while ( not ordersDB.UniQuery2.Eof) do
begin
ordersDB.UniQuery2.Delete;
ship_count := ship_count + 1;
end;
cust_count := cust_count + 1;
ordersDB.UniQuery1.Delete;
end;
Memo1.Lines.Add('Customers deleted: ' + inttostr(cust_count));
memo1.Lines.Add('Shipping Addresses deleted: '+ inttostr(ship_count));
end;
procedure TfQB.ShowDeleteConfirm(msg: string);
begin
if MessageDlg(msg, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
// User confirmed
DeleteCustomers();
end
else
begin
// User canceled
ShowMessage('Cancelled.');
end;
end;
procedure TfQB.Button1Click(Sender: TObject);
begin
getCustomers();
end;
procedure TfQB.getCustomers();
var
SQL: string;
count: integer;
......@@ -204,21 +437,22 @@ begin
try
//ParsedCustomer.AddPair('In KGOrders', not(ordersDB.UniQuery1.IsEmpty));
ParsedCustomer.AddPair('Id', ordersDB.UniQuery1.FieldByName('CUSTOMER_ID').AsString);
ParsedCustomer.AddPair('CompanyName', ordersDB.UniQuery1.FieldByName('NAME').AsString);
ParsedCustomer.AddPair('BillAddrCity', ordersDB.UniQuery1.FieldByName('BILL_CITY').AsString);
ParsedCustomer.AddPair('BillAddrLine1', ordersDB.UniQuery1.FieldByName('BILL_ADDRESS').AsString);
ParsedCustomer.AddPair('BillAddrState', ordersDB.UniQuery1.FieldByName('BILL_STATE').AsString);
ParsedCustomer.AddPair('BillAddrZip', ordersDB.UniQuery1.FieldByName('BILL_ZIP').AsString);
ParsedCustomer.AddPair('BillAddr', ordersDB.UniQuery1.FieldByName('BILL_ADDRESS_BLOCK').AsString);
ModifiedList.AddElement(ParsedCustomer);
ordersDB.UniQuery1.Next;
except
ParsedCustomer.Free;
raise;
end;
ParsedCustomer.AddPair('Id', ordersDB.UniQuery1.FieldByName('CUSTOMER_ID').AsString);
ParsedCustomer.AddPair('CompanyName', ordersDB.UniQuery1.FieldByName('NAME').AsString);
ParsedCustomer.AddPair('BillAddrCity', ordersDB.UniQuery1.FieldByName('BILL_CITY').AsString);
ParsedCustomer.AddPair('BillAddrLine1', ordersDB.UniQuery1.FieldByName('BILL_ADDRESS').AsString);
ParsedCustomer.AddPair('BillAddrState', ordersDB.UniQuery1.FieldByName('BILL_STATE').AsString);
ParsedCustomer.AddPair('BillAddrZip', ordersDB.UniQuery1.FieldByName('BILL_ZIP').AsString);
ParsedCustomer.AddPair('BillAddr', ordersDB.UniQuery1.FieldByName('BILL_ADDRESS_BLOCK').AsString);
ParsedCustomer.AddPair('QB_ID', TJSONString.Create(ordersDB.UniQuery1.FieldByName('QB_LIST_ID').AsString));
ModifiedList.AddElement(ParsedCustomer);
ordersDB.UniQuery1.Next;
except
ParsedCustomer.Free;
raise;
end;
end;
LoadJSONArray2(ModifiedList);
......@@ -250,7 +484,7 @@ end;
procedure TfQB.Button2Click(Sender: TObject);
begin
GetCustomers();
GetQBCustomers();
end;
procedure TfQB.Button3Click(Sender: TObject);
......@@ -313,11 +547,21 @@ end;
procedure TfQB.Button4Click(Sender: TObject);
var
orderJSON: TJSONObject;
begin
orderJSON := TJSONObject.Create;
orderJSON.AddPair('ORDER_ID', '19814');
addEstimate(orderJSON.ToString);
end;
procedure tfQb.addEstimate(orderInfo: string);
var
restClient: TRESTClient;
restRequest: TRESTRequest;
restResponse: TRESTResponse;
param: TRESTRequestParameter;
res: string;
i: integer;
jsValue: TJSONValue;
Customer: TJSONValue;
jsObj: TJSONObject;
......@@ -326,17 +570,149 @@ var
estimateJSON: TJSONObject;
SQL: string;
estimate: TEstimate;
CustomerRef: TRef;
CustomerRefJSON: TJSONObject;
Lines: TArray<string>;
BillAddr: TAddress;
BillAddrJSON: TJSONObject;
CustomField:TJSONObject;
CustomFields: TJSONArray;
ShipAddr: TAddress;
ShipAddrJSON: TJSONObject;
Items: TArray<TLine>;
ItemRef: TRef;
SalesItemLineDetail: TSalesItemLineDetail;
LineArray: TJSONArray;
LineObj, DetailObj, ItemRefObj: TJSONObject;
TaxCodeRef: TJSONObject;
TxnTaxCodeRef: TJSONObject;
TxnTaxDetail: TJSONObject;
JSONData: TJSONObject;
ORDER_ID: string;
table: string;
ShipMethodRef: TJSONObject;
begin
BillAddrJSON := TJSONObject.Create;
ShipAddrJSON := TJSONObject.Create;
EstimateJSON := TJSONObject.Create;
CustomerRefJSON := TJSONObject.Create;
CustomFields := TJSONArray.Create;
CustomField := TJSONObject.Create;
LineArray := TJSONArray.Create;
LineObj := TJSONObject.Create;
DetailObj := TJSONObject.Create;
ItemRefObj := TJSONObject.Create;
TaxCodeRef := TJSONObject.Create;
TxnTaxCodeRef := TJSONObject.Create;
TxnTaxDetail := TJSONObject.Create;
JSONData := TJSONObject.ParseJSONValue(orderInfo) as TJSONObject;
ORDER_ID := JSONData.GetValue<string>('ORDER_ID');
// add logic to figure out what type of order it is
SQL := 'select * from orders where ORDER_ID = ' + ORDER_ID;
doQuery(ordersDB.UniQuery1, SQL);
if ordersDB.UniQuery1.FieldByName('ORDER_TYPE').AsString = 'corrugated_plate' then
begin
table := 'corrugated_plate_orders';
end
else if ordersDB.UniQuery1.FieldByName('ORDER_TYPE').AsString = 'web_plate' then
begin
table := 'web_plate_orders';
end
else
table := 'cutting_die_orders';
SQL := 'select * from corrugated_plate_orders cpo join customers c on cpo.COMPANY_ID = c.CUSTOMER_ID where ORDER_ID = 18690';
SQL := 'select * from ' + table + ' cpo join customers c on cpo.COMPANY_ID = c.CUSTOMER_ID join customers_ship cs on cpo.staff_fields_ship_to = cs.ship_block JOIN qb_items ON cpo.staff_fields_quickbooks_item = qb_items.qb_item_name where ORDER_ID = ' + ORDER_ID;
doQuery(ordersDB.UniQuery1, SQL);
estimate.TxnDate := DateTimeToStr(Now);
estimate.PrivateNote := ordersDB.UniQuery1.FieldByName('general_special_instructions').AsString;
//estimate.CustomerRef := order
estimateJSON.AddPair('TxnDate', ordersDB.UniQuery1.FieldByName('staff_fields_order_date').AsString);
// Update the Quickbooks list ID in the database
// CustomerRef.value :=
CustomerRefJSON := TJSONObject.Create;
CustomerRefJSON.AddPair('value', ordersDB.UniQuery1.FieldByName('QB_LIST_ID').AsString);
//CustomerRef.value := '59'
;
estimateJSON.AddPair('CustomerRef', CustomerRefJSON);
Lines := ordersDB.UniQuery1.FieldByName('staff_fields_invoice_to').AsString.Split([sLineBreak]);
for i := 0 to Length(Lines) - 2 do
begin
case i of
0: BillAddrJSON.AddPair('Line1', Lines[0]);
1: BillAddrJSON.AddPair('Line2', Lines[1]);
2: BillAddrJSON.AddPair('Line3', Lines[2]);
3: BillAddrJSON.AddPair('Line4', Lines[3]);
4: BillAddrJSON.AddPair('Line5', Lines[4]);
end;
end;
BillAddrJSON.AddPair('City', ordersDB.UniQuery1.FieldByName('BILL_CITY').AsString);
BillAddrJSON.AddPair('CountrySubDivisionCode', ordersDB.UniQuery1.FieldByName('BILL_STATE').AsString);
BillAddrJSON.AddPair('PostalCode', ordersDB.UniQuery1.FieldByName('BILL_ZIP').AsString);
estimateJSON.AddPair('BillAddr', BillAddrJSON);
CustomField.AddPair('DefinitionId', '1');
CustomField.AddPair('Name', 'CUSTID');
CustomField.AddPair('Type', 'StringType');
CustomField.AddPair('StringValue', ordersDB.UniQuery1.FieldByName('SHORT_NAME').AsString);
CustomFields.AddElement(CustomField);
EstimateJSON.AddPair('CustomField', CustomFields);
Lines := ordersDB.UniQuery1.FieldByName('ship_block').AsString.Split([sLineBreak]);
for i := 0 to Length(Lines) - 2 do
begin
case i of
0: ShipAddrJSON.AddPair('Line1', Lines[0]);
1: ShipAddrJSON.AddPair('Line2', Lines[1]);
2: ShipAddrJSON.AddPair('Line3', Lines[2]);
3: ShipAddrJSON.AddPair('Line4', Lines[3]);
4: ShipAddrJSON.AddPair('Line5', Lines[4]);
end;
end;
ShipAddrJSON.AddPair('City', ordersDB.UniQuery1.FieldByName('city').AsString);
ShipAddrJSON.AddPair('CountrySubDivisionCode', ordersDB.UniQuery1.FieldByName('state').AsString);
ShipAddrJSON.AddPair('PostalCode', ordersDB.UniQuery1.FieldByName('zip').AsString);
estimateJSON.AddPair('ShipAddr', ShipAddrJSON);
// 🔹 Line object representing one line item
LineObj := TJSONObject.Create;
if ( table = 'corrugated_plate_orders') or (table = 'cutting_die_orders' ) then
begin
if ordersDB.UniQuery1.FieldByName('general_special_instructions').AsString <> '' then
LineObj.AddPair('Description', ordersDB.UniQuery1.FieldByName('item_desc').AsString + ' - ' + ordersDB.UniQuery1.FieldByName('general_special_instructions').AsString);
end;
memo1.Lines.Add(ordersDB.UniQuery1.FieldByName('staff_fields_price').AsString);
memo1.Lines.Add(ordersDB.UniQuery1.FieldByName('staff_fields_quantity').AsString);
LineObj.AddPair('Amount', TJSONNumber.Create(ordersDB.UniQuery1.FieldByName('staff_fields_price').AsFloat));
memo1.Lines.Add(ordersDB.UniQuery1.FieldByName('staff_fields_price').AsString);
LineObj.AddPair('DetailType', 'SalesItemLineDetail');
ItemRefObj.AddPair('value', ordersDB.UniQuery1.FieldByName('qb_items_qb_id').AsString);
DetailObj.AddPair('ItemRef', ItemRefObj);
DetailObj.AddPair('Qty', TJSONNumber.Create(ordersDB.UniQuery1.FieldByName('staff_fields_quantity').AsFloat));
DetailObj.AddPair('UnitPrice', TJSONNumber.Create(ordersDB.UniQuery1.FieldByName('staff_fields_price').AsFloat));
LineObj.AddPair('SalesItemLineDetail', DetailObj);
LineArray.AddElement(LineObj);
EstimateJSON.AddPair('Line', LineArray);
if ordersDB.UniQuery1.FieldByName('staff_fields_ship_date').AsString <> '' then
begin
estimateJSON.AddPair('ShipDate', ordersDB.UniQuery1.FieldByName('staff_fields_ship_date').AsString)
end;
///v3/company/<realmID>/estimate
restClient := TRESTClient.Create(nil);
restClient.BaseURL := 'https://sandbox-quickbooks.api.intuit.com';
......@@ -362,6 +738,10 @@ begin
param.Options := param.Options + [TRESTRequestParameterOption.poDoNotEncode];
param.Value := 'Bearer ' + AccessToken;
Memo1.Lines.Add( estimateJSON.Format(2) );
restRequest.AddBody(estimateJSON.ToJSON, TRESTContentType.ctAPPLICATION_JSON);
restRequest.Execute;
//memo1.Lines.Add(restresponse.Content);
......@@ -593,7 +973,9 @@ begin
//CustomerList := TJSONArray(restResponse.JSONValue);
Memo1.Lines.Add( jsObj.Format(2) );
CustomerList := TJSONArray( TJSONObject( jsObj.GetValue('QueryResponse') ).GetValue('Customer')) ;
CustomerList := TJSONArray( TJSONObject( jsObj.GetValue('QueryResponse') ).GetValue('Item'));
loadJSONArray(CustomerList);
// Load customer info
......@@ -604,7 +986,68 @@ begin
end;
procedure TfQB.getCustomers();
procedure TfQB.Button9Click(Sender: TObject);
var
restClient: TRESTClient;
restRequest: TRESTRequest;
restResponse: TRESTResponse;
param: TRESTRequestParameter;
res: string;
jsValue: TJSONValue;
jsObj: TJSONObject;
CustomerList: TJSONArray;
pair: TJSONPair;
ModifiedList: TJSONArray;
ParsedCustomer, Customer: TJSONObject;
I: integer;
BillAddr: TJSONObject;
ShipAddr: TJSONObject;
begin
restClient := TRESTClient.Create(nil);
restClient.BaseURL := 'https://sandbox-quickbooks.api.intuit.com';
restRequest := TRESTRequest.Create(nil);
restRequest.Client := restClient;
restResponse := TRESTResponse.Create(nil);
restRequest.Response := restResponse;
if MinutesBetween(Now, LastRefresh) > 58 then
begin
RefreshAccessToken();
end;
restRequest.Method := rmGET;
//GET /v3/company/9341454243251288/query?query=<selectStatement>&minorversion=75
res := '/v3/company/' + companyID + '/query?query=select * from estimate where DocNumber = ' + quotedStr('1002') + '&minorversion=75';
restRequest.Resource := res;
param := restRequest.Params.AddItem;
param.Name := 'Authorization';
param.Kind := pkHTTPHEADER;
param.Options := param.Options + [TRESTRequestParameterOption.poDoNotEncode];
param.Value := 'Bearer ' + AccessToken;
restRequest.Execute;
memo1.Lines.Add(restresponse.Content);
jsValue := restResponse.JSONValue;
jsObj := TJSONObject(jsValue);
Memo1.Lines.Add( jsObj.Format(2) );
restClient.Free;
restRequest.Free;
restResponse.Free;
end;
procedure TfQB.getQBCustomers();
var
restClient: TRESTClient;
restRequest: TRESTRequest;
......@@ -762,6 +1205,10 @@ begin
Secret := iniFile.ReadString('Quickbooks', 'ClientSecret', '');
CompanyID := iniFile.ReadString('Quickbooks', 'CompanyID', '');
RefreshToken := iniFile.ReadString('Quickbooks', 'RefreshToken', '');
AccessToken := iniFile.ReadString('Quickbooks', 'AccessToken', '');
LastRefresh := StrToDateTime(iniFile.ReadString('Quickbooks', 'LastRefresh', ''));
QB_ID := '';
ID := '';
end;
procedure TfQB.FormDestroy(Sender: TObject);
......@@ -929,16 +1376,19 @@ var
row: integer;
begin
Memo1.Lines.Add( '---------------------------------------------------------------' );
Memo1.Lines.Add( 'LoadJsonArray into asgData' );
Memo1.Lines.Add( 'LoadJsonArray into asgData2' );
asgData2.ClearAll;
asgData2.RowCount := 1;
asgData2.StartUpdate;
jso := TJSONObject(jaData.Items[0]);
asgData2.ColCount := jso.Count;
asgData2.ColCount := jso.Count+1;
for i := 0 to jso.Count - 1 do
begin
asgData2.Cells[i+1, 0] := jso.Pairs[i].JsonString.Value;
Memo1.Lines.Add('Header Key: ' + jso.Pairs[i].JsonString.Value);
end;
for i := 0 to jaData.Count - 1 do
begin
......
......@@ -2,10 +2,10 @@
MemoLogLevel=3
FileLogLevel=5
webClientVersion=0.9.6
LogFileNum=758
LogFileNum=819
[Database]
Server=192.168.159.153
Server=192.168.159.144
--Server=192.168.102.130
--Server=192.168.75.133
Database=kg_order_entry
......@@ -17,6 +17,6 @@ Password=emsys01
CompanyID=9341454272655710
ClientID=ABgO14uvjh8XqLud7spQ8lkb98AUpcdA7HbyMJfCAtl65sQ5yy
ClientSecret=bQ06TRemHeAGFzVHRaTUvUoBU9jpU9itK6MOMgqN
RefreshToken=RT1-251-H0-1759499151mghzkhn74tjkqi2vg5c0
AccessToken=eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwieC5vcmciOiJIMCJ9..BPFIXN1xTjdWuZGjkh1jUQ.IghcYUEoPWx47KnmuIbgZBNm1EhInMZjAXUi68M-l6zZkuKnlQYmbYq6gC3gIyTd66cx85t7pMmdBAH_q-v3qkUKx2ZqVSRZyYVKSrxYWFl0Vafzbj_BY-4kjrGEqZRFtR4dfQfbFmmlDW215Vjn8MO9v3tvgAVbWO9kafJ3Frv4KtNRgJQPmC01nz4ixF5RD91EnCSo-q4DuuRtCERkjjIiEW9D8ODt1GexshbHxam7dWCDGM2yHX_V0trWi37X5jBBrWgTYRogXhTPfpvlfkCQKTrNmKWhVDuVUxJB3aPREI43IMlW6h3hf445quAjINvgK5wokDCnmVPW3XvIDvnckgAaa9qOLzpl5UktpjqDRRiI4zVNs6BXP7kCsZs9ez0Ai5oeWgj0_j0NKbuVW1exkLcrinmYrMny-s8XpADNDslW8XCmJnHPsOWqZZL5bEXFJXPcYfPkGkV7rt-ln84gRD9413Ji05cmOzO1X5ClhrRhFRwjT3dbrgh88Dro-vMNs3weRdYOVpYFvA5aDXs9xkmHx8auphCdZo5a3sB8n9iKiM618HR-mSeYwCIU3aF2xE8kx0ljH8qVgX1bI2w9ByfM-mNBkasu3uKDH3HcpfqJBKWNOkG692JjzBoy.6H4UaInosVyqlMSCqGMAaA
LastRefresh=6/24/2025 10:51:36 AM
RefreshToken=RT1-10-H0-1760618473k44t73jittvqlr4wynn6
AccessToken=eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwieC5vcmciOiJIMCJ9..IwosInYT2Am8LdNGXuY-zQ._QdW6AZ0up2uAna580wQy7qAZPo3nkfHo0eeSpKllcJ_s69nh33Ld41MnTkxeAv1v1bxhvkDxEvkevfNGnggNHU3XSqIqb6tTKcAqR4zPf14JYgOkn82tw_q2VXWLgboMMpP6CfcBHeZPIbJcEOkmX5g4CY0NOum1-_beYmDHE2sxAxnKxcg3rhjBo33BTFjXSg1xknvkTO_GVbyAWdyZmQOpFRfdXAeUDnPAEZhYNVfACFkEDubdK7PyfcAs_7un_E0NZf270tMc2W0HTyk24wCSLbfeeH2Su-G2wi21JszcNsXSNt9ayGtHEXNcWG6A54k1M8U7o_mvpCXLPEXDtJkx8HnBvTz_ZXrFaztQf0_c8DLld5zW8FC1wn-SzHyoCbYbR2M6tBjVLiK80_FlAal_YjBI8fk9BX7nBab6LbCBIcunt7hRsLWCPiRI8_fwM_Nj8rlvxtIoClcyV7_VpIpxCVUgmxKmgf7zuWD3fYc3i8Bg-DpgGgWPVATXSlfNKhOWIDZfyy0YOlkreXPlNdQwCJ1Su3_3yzu_DioV76C5orqoeWk25NiopiAqJ7cBMj15EiuFx-w3jaqCv6ovzEc5r3by510TNu6msjUh2RQ-nxVqLjyLt6VIp3O00e_.2itJJ0rFwI-foW2v1hDSrw
LastRefresh=7/7/2025 1:49:30 PM
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