Topic: Table fields

https://s16.postimg.org/ahmp04pw5/tables_fields.png


Is my understanding of database tables and fields correct, as illustrated with the diagram above?


Invoice and it's sub form Invoice item forms uses fields from Customers and Products tables beside it's own table named Invoice. Do I duplicate fields from customers and products on invoice table or the approach on the diagram above is correct?.


If the approach on the diagram above to be used; I'd get needed fields from multiple tables (customers, products and invoice) with invoice table grid settings and with save button for save action I'd save into invoice table?

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

Re: Table fields

Hi Adam,

Ask 100 people and you'll get 100 different answers!  And while the theory of relational database design MIGHT be a good place to start, there are also practical reasons why you might end up doing things differently.

To give a brief response solely to your diagram, I would make the following comments (just my view, not saying it's right or wrong!)
1. I would remove 'invoice' table and have 'invoice_header' table (with fields for invoice number and invoice date) and 'invoice_line' table (with fields for qty).  Curently, you are repeating the invoice number and invoice date - a 10 line invoice will have the invoice number and the date repeating 10 times
2. Storing Net price is redundant -it is simply the result of the calculation of invoice_line.qty*products.unit_price
3. Obviously, you'll need your joins (customer to invoice_header, invoice_header to invoice_line, invoice_line to products)

But now the fun starts.  Consider these scenarios:
1.  The price of the product changes - you probably don't want the new price reflected in the invoice.  So you might deliberately choose to copy the unit_price from the 'products' table and then hold it on the invoice_line table (introducing data redundancy).
2.  The same goes for tax_rate - that could change as well.
The theorists would probably have you create new tables for product price changes and tax rate changes with 'from' and 'to dates and relate it back to the invoice date.  The pragmatists would probably suggest you initially look-up the 'products' table to get the unit_price and the tax_rate, but then actually hold unit_price and tax_rate on the 'invoice_line' table.
3.  Do you need to know the value of the invoice?  The theorists might have you perform invoice_line.qty * products.unit_price and then sum up the result for all of the lines in the invoice.  The pragmatists might say that it's something you frequently need to know and it takes too long to perform the calculation every time.  So, for the sake of a quick response time for the user, it's okay to deliberately introduce data redundancy and have a field called invoice_header.invoicetotal where the calculation is performed once and then stored. 

These are just a couple of simple issues you need to consider when designing your application. 
Even more important, you might have to consider whether MVD does it easily (with no script) one way, but doing it another way might involve significant complexity and a lot of script writing.   I know the software SHOULDN'T determine the approach you take, but it's a fact of life!

Not sure if any of that helps and, as I said, just my view!

Derek.

Re: Table fields

Hello AD1408

No hesitation ! You must link your three tables together (by the way (Table -> RelationShip).

An invoice may involve many customers, a Product may be bought par another customer and so one.

Relationship between tables the links between tables facilitate research at requests

Customers Table is interested in the client's pedigree.
Invoice table is concerned only by informations about invoice.
Products table is concerned by characteristics of products

And relationships allow to brew all this informations in a single request (on the client, on the invoice or on the product)

JB

4 (edited by AD1408 2016-10-02 16:26:20)

Re: Table fields

Hi Derek,

Wow... There are lots of info. It would take me some time for all that info starting to make better sense - as I learn more. Thanks a lot...


I'm always interested in your ingenious solutions. I have attached a sample blank project with the hope that you'd apply your approach. It may open whole new horizons for me in respect of tables and fields organization, calling in data from different tables, displaying and saving them to different table.


I came to understanding that creating an application beyond basics with MVD scripting is required. This sample project definitely requires some script. I learn by copy paste from you guys. Your scripts help a lot. I really appreciate your kind help.


----------------------------------------------------------------------


Hi JB,


Thanks for the info...


What I could understand from your info, I should create relational field on invoice table for all customer and product fields. However, I am not sure how I could make them work when it comes to pulling data in invoice form and displaying then on invoice table grid then saving. Perhaps you could show on attached sample project by making it work as intended.

Post's attachments

Attachment icon Tables and Fields.zip 9.21 kb, 486 downloads since 2016-10-02 

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

Re: Table fields

Hello AD1408

OK I prepare a sample project about that

JB

Re: Table fields

Hello Adam,


You're already in good hands with Jean and Derek and, as Derek already stated, you'll get as many different answers as people you ask.


As Derek and Jean also already stated, you need relationships between your tables, you can not just save in a table an invoice, a client and all the products involved in the ordrer.


Attached is what I'd do, very simple schema that can be further improved.
I name the table ORDER because, before being an invoice, that document will probably be a QUOTE and then, if validated by the client, become an INVOICE : that's the reason of the boolean fields "is_quote", "is_ordre" and "is_paid" just to make the difference between what's coming and what's have already been accounted for.


You might also want to had some dates to your scheme, like order date, max payment date and so on.


I'll leave you with that for now. have fun


Mathias

Post's attachments

Attachment icon invoices.PNG 17.22 kb, 247 downloads since 2016-10-03 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

7 (edited by AD1408 2016-10-03 10:18:20)

Re: Table fields

Hi Mathias,


Thanks for the info... Nice to see u around too.


I'm wishfully waiting Derek's and JB's implementations.. If u can steal some time from BuckRoger, I'd love to see your implementation too.


--------------------------------------------------------------
Edit, 03-Oct-16 12:13:51 PM


Derek, I'd be happy to go with pragmatists approach.

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

Re: Table fields

Hi Guys,


Please see the attached sample project. I have done basics. Thanks to Dmitry's kind help with script, I have managed to get customer and product details on inv and inv item windows I think.


1. However, stuck on adding invoice item. On save of inv item it returns error. So I couldn't test the inv window yet.


2 Also I'm not sure how to resolve the issue of calculating totals. I don't know if it's possible to get net total from table net totals column sum, tax total needs to be calculated field and inv total needs net total and tax total.


3. I Tried counter field for simple inv num and product code but it didn't work for me. (I'm assuming it increments +1 with every record)
It'd be much better to use texbox with numbers and prefix letters. Is this possible if so how? Perhaps there is an easier way to assign an auto incremental method for product, inv etc numbering and assigning code.


Please help.......

Post's attachments

Attachment icon Tables and Fields 1.zip 13.95 kb, 487 downloads since 2016-10-04 

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

Re: Table fields

I spent a lot of time on this but couldn't solve..


Nobody here can help?

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

Re: Table fields

Hi Adam,
Just had a quick look (rushed for time) and a few issues stand out.  However, the 2 that immediately caught my eye are:
1.  the invoiceitem table needs to be joined to the taxrates table (that should fix the error you're getting when saving the invoice item)
2.  the invoiceitem table needs to be joined to the invoiceheader table (the invoiceline currently doesn't know which invoiceheader it belongs to).
Hope that moves you on a bit.
Derek.

Re: Table fields

Hi Derek,


Thanks for the info... My apologies for being bit thick, but I'm not clear about your advice of joining tables...


Looks like this one for Dmitry.

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

Re: Table fields

AD1408 wrote:

Hi Derek,


Thanks for the info... My apologies for being bit thick, but I'm not clear about your advice of joining tables...


Looks like this one for Dmitry.

Hello.


I made some changes in your project:

Post's attachments

Attachment icon Tables and Fields 1_fixed.zip 14.07 kb, 485 downloads since 2016-10-05 

Dmitry.

Re: Table fields

Hi Dmitry,


Thank you very very very much................


Now invoice saved correctly!!!!


I was starting to think, I had to give up on this invoicing etc project with sadness. But with your kind help it's coming back to life again.


Only major bit left is doing the calculations on frmInvoiceHedaer (Net price, Net total, Tax Total and Inv Total) fields. And making all currency related fields displayed with thousand separator + 2 decimal points including on table grid sum used. They are simple math calcs but doing it in MVD is something else. Would yo be soooooooo kind to fix these as well please Dmitry?

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

Re: Table fields

AD1408
Done.

Post's attachments

Attachment icon Tables and Fields 2_fixed.zip 14.89 kb, 492 downloads since 2016-10-06 

Dmitry.

Re: Table fields

Hi Dmitry,


Thank you VERY much.. Appreciated.... That solves major stumbling block on the way of my project.

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

Re: Table fields

DriveSoft wrote:
AD1408 wrote:

Hi Derek,


Thanks for the info... My apologies for being bit thick, but I'm not clear about your advice of joining tables...


Looks like this one for Dmitry.

Hello.


I made some changes in your project:


Hi Dmitry,


Once again thanks a lot for the fix...


I wanted add thousand separator for prices. I managed tgrid columns sums but couldn't do calc fields.


What do I need to add the following calc formulas to format them so that they are showing thousand separators beside decimals and right align them on the tgrid.


printf("%.2f",
  (
  SELECT TOTAL(InvoiceItems.unit_price * InvoiceItems.qty * InvoiceItems.tax_rate / 100 + (InvoiceItems.unit_price * InvoiceItems.qty)) FROM InvoiceItems
  WHERE InvoiceItems.id_InvoiceHeader=InvoiceHeader.id
  )
)


printf("%.2f",
  (
  SELECT TOTAL(InvoiceItems.unit_price * InvoiceItems.qty * InvoiceItems.tax_rate / 100) FROM InvoiceItems
  WHERE InvoiceItems.id_InvoiceHeader=InvoiceHeader.id
  )
)


printf("%.2f",
(SELECT TOTAL(InvoiceItems.unit_price * InvoiceItems.qty) FROM InvoiceItems WHERE InvoiceItems.id_InvoiceHeader=InvoiceHeader.id)
)
Adam
God... please help me become the person my dog thinks I am.

Re: Table fields

AD1408
I added some scripts for formatting data in the grid.

Post's attachments

Attachment icon Tables and Fields 3_fixed.zip 16.11 kb, 499 downloads since 2016-10-11 

Dmitry.

Re: Table fields

Thank you very much Dmitry......


Wonderful !!!.

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

Re: Table fields

Hi Dmitry,


I was testing fix 3. Found followings:
Added 1 new customer and 1 new product.
Issued inv to selected customer, it saved ok.
However, on adding second inv to same or different customer inv header tgrid displays the product added with first inv.
It doesn't seems to clear inv header tgrid therefore it doesn't save inv correctly.


any solution?

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

Re: Table fields

AD1408 wrote:

Hi Dmitry,


I was testing fix 3. Found followings:
Added 1 new customer and 1 new product.
Issued inv to selected customer, it saved ok.
However, on adding second inv to same or different customer inv header tgrid displays the product added with first inv.
It doesn't seems to clear inv header tgrid therefore it doesn't save inv correctly.


any solution?

Hello,


please attach a screenshot to show the problem.

Dmitry.

Re: Table fields

Thanks Dmitry... Here it's:

Post's attachments

Attachment icon tf_fix3.png 66.25 kb, 250 downloads since 2016-10-13 

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

Re: Table fields

AD1408 wrote:

Thanks Dmitry... Here it's:

Done.

Post's attachments

Attachment icon Tables and Fields 4_fixed.zip 16.4 kb, 479 downloads since 2016-10-14 

Dmitry.

Re: Table fields

Thank you very much Dmitry......


Now it works perfectly.

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

24 (edited by AD1408 2016-10-15 14:46:24)

Re: Table fields

Hi Dimitry,


Sorry to bother you again. I searched with the hope that I could do it myself but no success.


I wanted if invoice header tgrid empty it doesn't save the invoice. Instead it shows error dialog with message 'You must add at least 1 Invoice item to save' When invHeader tgrid is null (no item added or added then deleted) then only option for the user is to cancel without saving.


https://s3.postimg.org/5bwpylgub/tgrid_null1.png


https://s22.postimg.org/hghepzlwx/tgrid_null2b.png

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

Re: Table fields

Hi Adam,
Not being technically minded, I probably approach the problem from a different angle to other users. 
I find there are occasions when I want to 'check' what has been entered (or not entered) but it's too late once 'save' has been clicked.  So, I create a 'pre-save' button which just runs any required validation in a script.  If okay, the script then clicks on the 'real save' button.
There are a couple of extra things;
1.  I always set the 'real save' button enabled property to false initially;  this prevents the record being saved if the user accidently presses 'ENTER / RETURN' instead of 'TAB'.
2. I also set the 'real save' button visible and tab-stop properties to false.  Therefore, the button you actually see and click is the 'pre-save' button, not the 'real save' button.
As always, it is much easier just to see it working, so I've added it to your script.
Hope this helps,
Derek.

Post's attachments

Attachment icon Tables and Fields 4_fixed.zip 351.35 kb, 528 downloads since 2016-10-15