This topic is locked

mass invoices

6/22/2023 3:49:56 AM
PHPRunner General questions
author

Hi all. I am trying to solve the problem of creating mass invoices. Everything is fast to create in PHPRunner till this moment.
What I have:
<ol> Invoice table Invoice details table* Partners table
</ol>I created a view and deleted everything from grid on designer page just to make the page blank and with only ONE CUSTOM BUTTON.

  • server code
    $rs = DB::Query("select from partners where Active=1 order by name"); //// select only active partners
    while( $data = $rs->fetchAssoc() )
    {
    $invoice = array();
    $invoice["date"]=$date;

    $invoice["partnersID"]=$data["partnersID"];
    // ... add other data

    DB::Insert("Invoice", $invoice );
    $LastInvoceID = DB::Query("select ID from Invoice order by ID desc limit 1");
    $invoiceID = $LastInvoceID->fetchAssoc(); /// get the ID of last inserted invoice
    $details = array();
    $details["item"] = "service for current month";
    $details["price"] = 10;
    // add other data
    DB::Insert("InvoiceDetails", $details );
    }
    I didn't want to copy all the code but I think everyone can understand this.
    Problem is - above code is NOT working!
    I have checked the sintax - ok, then I changed the code:
    global $dal;
    $tblInvoices = $dal->Table("Invoice");
    $tblInvoices->Value["date"]=$date;
    $tblInvoices->Value["partnersID"]=$data["partnersID"];
    // ... add other data
    $tblInvoices->Add();
    this code is working for invoice table, but I cannot get it working for inovicedetails table, don't know why.
    I am probably missing some basic logic for web apps...
    Can I put all of this inside one 'while' loop (partners loop)?
    To make it clear what I am trying to get:
    <ol>
    execute query on partners table to get only active partners for each one of partners, create a new record in invoice table get the last ID from invoice table* insert a new record into invoicedetails table with the last ID from invoice table (master/child tables)
    Maybe the problem can occur because I have several thousands records in partners table?
    Please any constructive suggestion is welcomed.
    remark: I have the same problem solved in desktop app created with other tool, so I was following the same logic.
    Regards!
    </ol>

D
david22585 6/22/2023

Not sure if this will help or not, but with my site, I insert transactions into the invoicedetails table with a flag of 'invoiced=0' to show it hasn't been invoiced yet. Then the cutom button runs through all of them, groups the debits and credits together by account, and creates the invoices for each account:
// Select all transactions that aren't invoiced yet //$transactionsearch = DB::PrepareSQL("SELECT id, account_id FROM invoicedetails WHERE invoiced='0' OR (invoice_id = '' AND price > '0') GROUP BY account_id", '0'); $transactions = DB::Query("SELECT id, account_id FROM invoicedetails WHERE invoiced='0' GROUP BY account_id"); while($data = $transactions->fetchAssoc()){ $arr = explode(",",$data["id"]); for ($i=0;$i<count($arr);$i++){ foreach ($data AS $invoice) { // Select the account_id for processing from transaction $newid = DB::DBLookup("SELECT account_id FROM invoicedetails WHERE id='".$arr[$i]."'"); $oldid = $oldid; $idlookup = $idlookup; // Check if there is a credit on file $creditamount = DB::DBLookup("SELECT balance FROM account_data WHERE id = ".$newid.""); // If going to new account_id, create a new invoice and update invoice number. if ($newid != $oldid){ // Update invoice number $invoiceid = DB::DBLookup("SELECT invnumber FROM invoice_number"); $invnum = $invoiceid + 1; DB::Exec("UPDATE invoice_number SET invnumber='".$invnum."'"); //Get total and create hash $charges = DBLookup("SELECT sum(price) FROM invoicedetails WHERE (account_id='".$newid."' AND invoiced='0' AND price > '0.00')"); $temphash = md5(now().'_'.$charges); if ($creditamount < '0'){ // If credit is larger than/equal to invoice total if (ABS($creditamount) >= $charges){ // Insert invoice data $remaining_credit = $creditamount + $charges; DB::Exec("INSERT INTO invoices SET account_id='".$newid."', total='".$charges."', total_paid='".$charges."', balance='0.00', hash='".$temphash."', invoice_number='".$invnum."', date=curdate(), due_date=curdate()+1"); DB::Exec("INSERT INTOinvoicedetails(account_id,item,price,invoiced,creationdate) VALUES ('".$newid."','Credit Applied','".$charges."','0',CURDATE())"); DB::Exec("UPDATE account_data SET balance = ".$remaining_credit." WHERE id='".$newid."'"); // If credit is less than invoice total } else if (ABS($creditamount) < $charges) { $invbalance = $creditamount + $charges; DB::Exec("INSERT INTO invoices SET account_id='".$newid."', total='".$charges."', total_paid='".$creditamount."', balance='".$invbalance."', hash='".$temphash."', invoice_number='".$invnum."', date=curdate(), due_date=curdate()+1"); DB::Exec("INSERT INTOinvoicedetails(account_id,item,price,invoiced,creationdate) VALUES ('".$newid."','Credit Applied','".$creditamount."','0',CURDATE())"); DB::Exec("UPDATE account_data SET balance = balance + ".$charges." WHERE id='".$newid."'"); } //No credit on file } else { // Insert invoice data DB::Exec("INSERT INTO invoices SET account_id='".$newid."', total='".$charges."', total_paid='0.00', balance='".$charges."', hash='".$temphash."', invoice_number='".$invnum."', date=curdate(), due_date=curdate()+1"); DB::Exec("UPDATE account_data SET balance = balance + ".$charges." WHERE id='".$newid."'"); } // Update hash $idlookup = DB::DBLookup("SELECT id FROM invoices WHERE hash='".$temphash."'"); $hash = md5($idlookup.'_'.$charges); DB::Exec("UPDATE invoices SET hash='".$hash."' WHERE id='".$idlookup."'"); $oldid = $newid; } // Update details to be marked as invoiced, and add the invoice_id to it. $data = array(); $data["id_invoice"] = $idlookup; $data["invoiced"] = "1"; DB::Update("invoicedetails", $data, "account_id=".$newid."" ); } } }

Sergey Kornilov admin 6/22/2023

I don't think anyone can tell you what exactly is wrong with this code or with your logic.
The typical troubleshooting process involves isolating the piece of code or logic that doesn't work.
<ol> Make sure that this portion of the code is actually executed. If this is a SQL query - print this SQL query on the page to make sure it looks good. Maybe execute this SQL query manually against your database to see if it works.
</ol>In other words, do not try to solve the problem at whole but systematically narrow down what is not working and fix it.

Davor GeciDevClub member 6/22/2023

First,
for the last inserted id use this (from help):
https://xlinesoft.com/phprunner/docs/db_lastid.htm
DB::Insert("cars", $data );
// get the ID of the inserted record and print it on the page
echo DB::LastId();

502110 6/22/2023

david22585 - THANK YOU VERY MUCH!
Finally someone smart besides admins ;)
Due to my regular work obligations, I will try your code through the weekend and let you know how it works, even more, I will post exact code.
dageci - Yes I realized that just after posting this issue.
admin - that is exactly what I am doing on a regular base. I end with a lot of code commented until the code is working, then uncommenting part-by-part....
It would be nice if Sergej make an option button to comment selected block of code and uncomment... it would be very helpfull for developers.
Thank you all - I am using PHPRunner for several months now and I see there is a big potential in it, still, we all need more, more, more examples with a complete code.
Greetings to all!

Sergey Kornilov admin 6/22/2023

I guess you didn't know that in PHP or basically any language you can comment out large amounts of code quickly.
/* your code here */Really saves you lots of time.

502110 6/23/2023

david225856
I have checked your code and so far I noticed my error in sintax DB::Query.
How did you solve a problem of printing invoices because I would like to have 3 inovices in A4?
B-T-W how to insert a picture here? it is reporting me "an error occured" when I try to upload an image...
Actually, I need something like:
If footer cannot be printed on the same page as header - print invoice on the next page

fhumanes 6/24/2023

Hello everyone,
I remind you that I published an article of a very simple method for "debug" or "draw" code and observe the content of your variables and/or arrays/objects.
https://fhumanes.com/blog/guias-desarrollo/guia-34-metodo-basico-para-depuracion-codigo/
Greetings,
fernando

502110 7/20/2023

david22585
Getting back into this topic...
Does your invoices get created fast?
I was spending these days to get correct code of queries, because some customers get 2 detail's records, some 3, 4 or 5(no more than 5).
My speed test shows 1266 master records (together with child records) were created in 120 seconds.
Is that normal?
What if the user has 10.000 customers?
Please comment on this.