?
Solved

Explain what this section of code does

Posted on 2006-03-31
1
Medium Priority
?
319 Views
Last Modified: 2013-11-17
Hello,
     I am looking at one of the functions inside of an integration application that we use to remove shipcomplete holds since some orders have not been automatically released like they should.  I'm new to C++ Builder and I don't understand what the  function is doing.  The entire function is below.  

The main part that I don't understand is the record count and how it relates with removing the hold and how the values of the Add() function are calculated (the part with the formula atyalloc + qtyprinv != quantity - qtycance).  Please explain this in as much detail as possible.

Thanks for your help!

void __fastcall TKeleForm::ShipCompleteUpdate()
{
ShipCompleteQuery();
TDateTime wow = Now();
DM->GPXOPQuery->First();
while(!DM->GPXOPQuery->Eof)
    {
    salesordernmbr = DM->GPXOPQuery->FieldByName("SOPNUMBE")->AsString;
    DM->GPModifyQuery->Close();
    DM->GPModifyQuery->SQL->Clear();
    DM->GPModifyQuery->SQL->Add("Select Count(*) As Recordcount From SOP10200 ");
    DM->GPModifyQuery->SQL->Add("Where SOPNUMBE = '" + salesordernmbr + "'");
    //December 17, 2002 Added the "+ QTYPRINV" to following line
    //DM->GPModifyQuery->SQL->Add("And ATYALLOC + QTYPRINV != QUANTITY ");
    DM->GPModifyQuery->SQL->Add("And ATYALLOC + QTYPRINV != QUANTITY - QTYCANCE ");
    //January 7, 2005 Added the following And statements
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'FREIGHT' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'REPAIRS' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'LABOR' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'RESTOCKING FEE' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'EXPEDITE FEE' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'MISCELLANEOUS' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'UL FEES' ");
    DM->GPModifyQuery->SQL->Add("And ITEMNMBR != 'ENGINEERING FEE' ");
    DM->GPModifyQuery->Open();
    recordcount = DM->GPModifyQuery->FieldByName("RECORDCOUNT")->AsInteger;

    if(recordcount == 0)
       {
       DM->GPModifyQuery->Close();
       DM->GPModifyQuery->SQL->Clear();
       DM->GPModifyQuery->SQL->Add("Update SOP10104 ");
       DM->GPModifyQuery->SQL->Add("Set DELETE1 = '1', ");
       DM->GPModifyQuery->SQL->Add("USERID = 'Int', ");
       DM->GPModifyQuery->SQL->Add("HOLDDATE = '" + DateToStr(wow) + "', ");
       DM->GPModifyQuery->SQL->Add("TIME1 = '" + TimeToStr(wow) + "' ");
       DM->GPModifyQuery->SQL->Add("Where SOPNUMBE = '" + salesordernmbr + "' ");
       DM->GPModifyQuery->SQL->Add("And PRCHLDID = 'SHIPCOMPLETE'");
       DM->GPModifyQuery->ExecSQL();

       GPFascorInsert();

       }
    DM->GPXOPQuery->Next();
    }
}
0
Comment
Question by:jdr0606
1 Comment
 
LVL 25

Accepted Solution

by:
kode99 earned 2000 total points
ID: 16347023
The while loop "while(!DM->GPXOPQuery->Eof)"  is going to run the query for each entry in the GPXOPQuery untill there are no more records,  starting from the first.

Basically just a long SQL querty,  so just look at the actual SQL request as follows,

Select Count(*) As Recordcount From SOP10200    
Where SOPNUMBE = '" + salesordernmbr + "'"
And ATYALLOC + QTYPRINV != QUANTITY - QTYCANCE
And ITEMNMBR != 'FREIGHT'
And ITEMNMBR != 'REPAIRS'
And ITEMNMBR != 'LABOR'
And ITEMNMBR != 'RESTOCKING FEE'
And ITEMNMBR != 'EXPEDITE FEE'
And ITEMNMBR != 'MISCELLANEOUS'
And ITEMNMBR != 'UL FEES'
And ITEMNMBR != 'ENGINEERING FEE'

which means get count all the records from the table SOP10200
where SOPNUMBE equals salesordernmbr and a long list of additional requirements,  the simple ones are all just making sure that the ITEMNMBR field is not equal to varios items,  LABOR,  REPAIRS  etc.

The ATYALLOC + QTYPRINV != QUANTITY - QTYCANCE line is actually going to check each record and do the math for each record.  So if ATYALLOC + QTYPRINV  is not equal to QUANTITY - QTYCANCE,  no different than the rest except it will do the calculation for each side to determine if they are equal or not.
 
Then it opens the query and checks the checks the current record's field 'RECORDCOUNT',  which is the result from the select Count(*).  So if it is 0, meaning no records matched,  then it closes the GPModifyQuery query, clears the count request and puts in a new SQL request.

The new request will update existing records,

Update SOP10104
Set DELETE1 = '1', USERID = 'Int', HOLDDATE = '" + DateToStr(wow) + "',
"TIME1 = '" + TimeToStr(wow) + "'
Where SOPNUMBE = '" + salesordernmbr + " And PRCHLDID = 'SHIPCOMPLETE'"

So the columns named DELETE1, USERID, HOLDDATE, TIME1 are all going to be updated for records that have matching SOPNUMBE and PRCHLDID as required by the where command.

I would imagine that GPFascorInsert() is going to actually add some new records to something.

The Add function is just adding the lines of text to the SQL query that is sent to the server.  The SQL property is a list of strings (lookup a TStringList in the help for details).

In the line below it is all just plain text added to the query.  The ATYALLOC, QTYPRINV, QUANTITY and QTYCANCE are all just field or column names from the table that the query will be performed on.

DM->GPModifyQuery->SQL->Add("And ATYALLOC + QTYPRINV != QUANTITY - QTYCANCE ");

This line is just returning a field value from the table to a regular integer variable 'recordcount',
recordcount = DM->GPModifyQuery->FieldByName("RECORDCOUNT")->AsInteger;
If you actually looked at the query result you would see that the Count just returns a single record with the number of records that matched the request.

Sorry this explaination is a bit jumbled. Hope it helps.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question