Topic: dbUpdate and field value update

I thought this was a piece of cake but it turned out to be pie in the sky.


Based on purchases and sales I wanted to update product inventory quantities.
There is two issues I couldn't solve:
1. Updating form1 / PageControl1- products tab / tgMainProducts/  Qty in Stock column.
2. Getting current quantity value on purchInv and SaleInv forms using product combo onChange.


Please see the attached sample project:

Post's attachments

Attachment icon Stock Level.zip 12.43 kb, 391 downloads since 2017-05-31 

Adam
God... please help me become the person my dog thinks I am.

Re: dbUpdate and field value update

Looks like Dmitry run out support time for the day or questions weren't specific enough. However, If anybody else can and willing to do so, please do.


Her is another try:
With the image below I tried to highlight issues I couldn't resolve:

https://s30.postimg.org/ss6yc9djl/zzzzz_Temp37.png


On frmProduct I have read only Qty in Stock filed. It's calculated value based on purchase and sale invoices tGrids Qty column footer values.

procedure CalculateTotal; //
begin
  frmProduct.edProdQtyInStock.Value := frmProduct.tgProdPurchInv.Columns[3].Footer.FormulaValue - frmProduct.tgProdSaleInv.Columns[4].Footer.FormulaValue;
end;


procedure frmProduct_tgProdPurchInv_OnChange (Sender: string);
begin
  CalculateTotal;
end;

procedure frmProduct_tgProdSaleInv_OnChange (Sender: string);
begin
  CalculateTotal;
end;

ISSUE 1
On Form1 / PageControl1- products tab / tgMainProducts/  Qty in Stock column doesn't display calculated field value of frmProduct / Qty in Stock . I have tried dbUpdate but couldn't get it working:

procedure frmPurchInv_Button1_OnAfterClick (Sender: string);
begin
  Form1.tgMainProducts.dbUpdate;
  Form1.tgMainPurchInv.dbUpdate;
  frmProduct.tgProdPurchInv.dbUpdate;
end;

procedure frmSaleInv_Button1_OnAfterClick (Sender: string);
begin
  Form1.tgMainProducts.dbUpdate;
  Form1.tgMainSaleInv.dbUpdate;
  frmProduct.tgProdSaleInv.dbUpdate;
end;

ISSUE 2
I wanted to display Qty in Stock value from frmProduct inCurrent Stock Level fields on Sale and Purchase Invoice forms  according combo item and couldn't:

procedure frmPurchInv_cbPurchInvProduct_OnChange (Sender: string);
begin
  frmPurchInv.edPurchInvStockLevel.value := SQLExecute('SELECT qty FROM Product WHERE id='+frmPurchInv.cbPurchInvProduct.sqlValue);
end;
...

ISSUE 3
This one (hiding items on a form onShow) should have been easy. But one item hiding worked OK while second one not?

procedure frmProduct_OnShow (Sender: string; Action: string);
begin
if frmProduct.edProductName.text = '' then
   begin
   frmProduct.PanInvoices.Visible := False; // Worked OK
   frmProduct.panProdQty.Visible := False; // No idea as to why this one is not working?
   end else
   frmProduct.PanInvoices.Visible := True;
   frmProduct.panProdQty.Visible := True;
end;

Updated sample project attached.

Post's attachments

Attachment icon Stock Level 2.zip 13.47 kb, 358 downloads since 2017-06-01 

Adam
God... please help me become the person my dog thinks I am.

Re: dbUpdate and field value update

Adam, this type of updating another table can be tricky and involved because there are various situations you have to account for when processing data in the respective update forms, particularly when adjusting quantities and products simultaneously. This is not a criticism, but I believe your approach will not work. I put together the coding necessary to update stock levels. It manages the integrity of the stock levels so input mistakes will not cause stock levels to go below zero. Test out various situations to make sure I covered everything. I used the project from your first post, not your most recent one.

Post's attachments

Attachment icon Stock Level 3.zip 596.1 kb, 426 downloads since 2017-06-01 

Re: dbUpdate and field value update

Adam, An update to the project. I missed a scenario where the stock level could go below zero. I have the fix below.


Replace procedure Form1_Button9_OnClick.

With

procedure Form1_Button9_OnClick (Sender: string; var Cancel: boolean);
var
  Curr_Qty: Integer;
begin
    Adj_Qty := SqlExecute('Select Qty From PurchInv Where Id = ' + Form1.tgMainPurchInv.sqlValue);
    Prod_Id := SqlExecute('Select id_Product From PurchInv Where Id = ' + Form1.tgMainPurchInv.sqlValue);
    Curr_Qty := SqlExecute('Select Qty From Product Where id = ' + IntToStr(Prod_Id));
    If Curr_Qty - Adj_Qty < 0 then
      Begin
        MessageBox('Deleting this purchase invoice will cause the stock level to go below zero.','Error',MB_OK+MB_ICONERROR);
        Cancel := True;
        Exit;
      End;
end;

This will take care of the situation where for instance you have a stock level on an item of zero and then you add a purchase invoice for 5 which would increase the stock level to 5 and then you add a sales invoice of 1.  Ordinarily you can delete a purchase invoice and it will adjust the stock level for you. However, in this situation it would cause the stock level to go below zero if you deleted the purchase invoice because you have sales against the item.

5 (edited by AD1408 2017-06-01 18:34:52)

Re: dbUpdate and field value update

Hi EHW,


Excellent logic and coding as usual !
Thank you very much...................
Truly appreciated............................


Maintaining proper stock levels in coding seems to be a tricky business... at least for me. I'm in the process of reading and digesting your code at present. I see that I have to get a proper grasp of using variables.

Good spotting and thank you very much for the fix too.
I'll try to implement credit and debit invoices to achieve further stock level integrity, if I can.


In order to make code reading easy and minimize mistakes I try to use idiot proof (that's me) naming convention. i.e. Instead of  'Adj_Qty' I use 'AdjQtyVAR'  This way I don't mix it with events (as _ is used in them by default) and I can see them their own variables, buttons and field names. It's nothing to do with your style, it's just shortcoming on my side.
During renaming variables, changing  var Init_Qty to InitialQtyVAR returns "Undeclared identifier" around //...(frmSaleInv.InitialQtyVAR.Text);//  After changing it to back Init_Qty it works. All other var renaming worked fine. I tried to find if anywhere else it's referenced beside script tab page but couldn't find. I replaced them using find and replace.

Adam
God... please help me become the person my dog thinks I am.

Re: dbUpdate and field value update

Yes you're right, it can get rather complex for anybody when building inventory control systems or supply chain systems.


Sometimes I get a little lax on naming conventions. You absolutely should have a methodology in naming conventions. It definitely helps later when you have to come back to make changes or enhancements to the project. Don't look at that as a shortcoming. It's very essential.


During renaming variables, changing  var Init_Qty to InitialQtyVAR returns "Undeclared identifier" around //...(frmSaleInv.InitialQtyVAR.Text);// 


In the case of Init_Qty, I called a variable that name and I also called the form field that name. Sorry for the confusion. So for the frmSaleInv field name you should change it to your naming convention of "edInitQty" not InitialQtyVAR.  It should be frmSaleInv.edInitQty.text.  Hope this helps.

Re: dbUpdate and field value update

Thanks a lot EHW............


Renamed variables except Qty as I couldn't recognize clearly which ones refers to var and which ones to field name. No worries, it's OK.


I wanted to add another condition to sale inv, where sale qty value entered bigger than products' current stock qty value, when adding a new sale inv with the following but didn't work. Getting minus stock qty on products.

If (frmSaleInv.cbSaleInvProduct.sqlValue = frmSaleInv.SaleInvInitProd.Text) and (frmSaleInv.dbAction = 'NewRecord') then
     Begin
        If CurrQtyVAR - Qty <= 0 then
          Begin
            MessageBox('This sale qty value will cause the stock level to go below zero.','Error',MB_OK+MB_ICONERROR);
            frmSaleInv.edSaleInvQty.SetFocus;
            Cancel := True;
            Exit;
          End;
     End;
Adam
God... please help me become the person my dog thinks I am.

8 (edited by ehwagner 2017-06-02 03:12:43)

Re: dbUpdate and field value update

Field names in a script are part of the form property, such as in your case "frmsaleInv.edSaleInvQty.text". Where I have Qty by itself, with nothing around it, is a variable.


I had that new sale invoice situation covered, but I must have inadvertently removed the check. Anyway, change your "If" statement to the following. You do not need the other parts of the "If" statement. In fact, it will never be true on a "NewRecord" action.


If frmSaleInv.dbAction = 'NewRecord' then

Also change

If CurrQtyVAR - Qty <= 0 then

To

If CurrQtyVAR - Qty < 0 then

9 (edited by AD1408 2017-06-05 19:59:47)

Re: dbUpdate and field value update

Hi EHW,


Managing stock levels is very tricky indeed. I have tried to add refunds to "Stock Level" sample project that you kindly coded but failed miserably.


I have added DBCR table and relevant fields to sale and purchase invoices without all the checks as priority was getting quantities working when product/s returned. There are few things needs to be taken into account:
-- Not all refunds may be full, there will be partial refunds.
-- Purchase refunds subs qty from product current stock level.
-- Sale refunds adds back qty to product current stock level but not all sale refund qty added back to current stock level;
----- Customer may return faulty product which are not added back to current stock level.
----- Customer may ask money back due to not receiving products  which are not added back to current stock level.
For these kind of situations, I have added new field to sale inv where user can enter qty to be added back to current stock level. Therefore refund qty and qty added back to current stock level may be two different values.
edit: Partial refunds would apply to purchase invoice refunds too. I missed adding extra field on form purch inv. There again, additional fields on sale an purch invs may not be needed.


There seems to be issue with "Add New Product" button on both sale and purchase invs when clicked on Add sale/purch invs buttons on Form1.
Issue seems to be caused by checks on Save buttons on both sale/purch inv forms. I have commented out product selected from combo check as I have checked not null on tables. I may be totally wrong here.


I know you have limited time and to date you have been very kind on helping. Please don't feel any pressure or whatsoever. If and when you have a free time your help on this matter of completing stock levels project with refunds would be greatly appreciated as always.


I have attached sample project file.

Post's attachments

Attachment icon Stock Level 2 DbCr.zip 22.96 kb, 393 downloads since 2017-06-05 

Adam
God... please help me become the person my dog thinks I am.

Re: dbUpdate and field value update

Adam, I had a little bit of time to look at your project and make modifications to incorporate refunds. I think I have all your basis covered. Check it out and let me know. Sorry for all the script code. It was necessary in order to maintain accurate stock levels and to keep transaction integrity between invoices and the respective refunds of invoices. I rearranged the procedures so all are grouped with their respective forms and I put comments in so hopefully you will be able to follow the logic of the code.


I kept all quantities as positive numbers. I handled the refunds mathematically within the script. Also, I removed the "Add New Product" button from the Sale Invoice form. Logically speaking, it does not make sense to add a new product when doing a sale transaction because you need to purchase some before you sell some.


Since the "Add New Product" is only on the Purchase Invoice form, I moved all the validity checking on the purchase form to a pre-Save procedure and if it passes the checks, then it clicks the real save button. At this point I did not add another qty field on the Purchase Invoice form. I don't think you actually need it, but you may have a reason for it.

Post's attachments

Attachment icon Stock Level 2 DbCr Revised.zip 615.85 kb, 462 downloads since 2017-06-08 

Re: dbUpdate and field value update

Hi EHW,

Adam, I had a little bit of time to look at your project and make modifications to incorporate refunds. I think I have all your basis covered...


Indeed it does.........
Thank you very much......... Truly appreciated.................
Extra thank you for re-arranging the code and comments.........
You are very kind person.....

Adam
God... please help me become the person my dog thinks I am.