Explain what this section of code does

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();
    }
}
jdr0606Asked:
Who is Participating?
 
kode99Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.