I posted a question regarding this script/query the other day, but it was regarding strictly being able to insert values, not including new logic.
I currently have a PHP script that 'works' in terms of selecting and inserting but I need to apply some logical modifications to it in order to do a bit more.
I have a DB2 select query:
//DB2 Select query
$plcQueryDB2 = "
select
invnoc as INVOICE,
cstnoc AS DEALER,
framec AS FRAME,
covr1c AS COVER,
colr1c AS COLOR ,
extd1d AS SHIPDATE,
orqtyc AS QUANTITY
FROM GPORPCFL
where extd1d >= curdate()
group by invnoc,cstnoc, orqtyc, framec, covr1c,colr1c, extd1d
order by invnoc asc
";
Then I basically prepare and exucute that, and loop it in order to do a PDO call to insert into mysql. I'm actually using some variables from the DB2 query to 'join' with some MySQL data in another table, and then taking all the data from that and inserting into a final Mysql table.
Again, this works currently. However, I need to apply some additional logic and looping around my insert so that I can determine whether a record is updated or inserted.
I have some commented lines in my while loop below to give a pseudo code approach to what I need to do, and it includes the working insert. With the following points, I'm assuming a hypothetical unique index/combo of sku_id and dealer_id
Basically, I need to address these points:
- Within the while loop, query the mysql 'placements' table first to see if a record exists for the combo of sku_id and dealer. (So I would have to iterate over each held record from my db2 select and check to see if any records exist for that sku/dealer combo exists)
- If there are no records for that sku_dealer combo, then I will only do an insert. However, I need to insert a number of records equal to the value of 'QUANTITY' from the DB2 query. So insert the same record (I know this is redundant, but there's a reason for it) n times until n = QUANTITY
- If records exists with the sku/dealer combo AND today/curdate() is before expire_date of those records, then update those existing records, still making sure the number of rows/records = the value of 'QUANTITY' from above.
- Else, if records exist but today/curdate() is past the expiration, then do the insert from step 2 ensuring row count = QUANTITY
I think that should clear it up but maybe the pseudo code in my loop below will help clarify that.
I just need some guidance here, and maybe I've been looking at it for too long since I got it to this point. But what is a good way to apply new loops and logic to my existing code to achieve those points or at least get on the right path? This will run daily and I'm NOT concerned with efficiency at all right now, I just need to get it working and I can address speed or performance later.
Any ideas, suggestions or guidance will be much appreciated.
Here's the rest of the script:
//prepare and execute on db2
$prep = odbc_prepare($DB2Conn, $plcQueryDB2);
$exec = odbc_execute($prep);
$result = odbc_exec($DB2Conn, $plcQueryDB2);
try{
while($arr = odbc_fetch_array($result))
{
//loop and query the placements table to see if a record exists for the sku_id/group_id/dealer_ID
//if it exists and curdate() < expire_date and row_count = locations
// update starting with oldest expire_date
//else if row_count < locations
$stmt = $PDO->prepare("
INSERT IGNORE INTO placements (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
SELECT
id,
sku_group_id,
:DEALER,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date,
:QUANTITY,
:INVOICE
FROM skus s
WHERE s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
");
$PDO->beginTransaction();
$i = 0;
while($db2row = odbc_fetch_array($result)) {
if(++$i % 1000 == 0) {
$PDO->commit();
$PDO->beginTransaction();
}
$stmt->execute($db2row);
}
$PDO->commit();
}
}catch (PDOException $e){
print "ERROR!: " . $e->getMessage() . "<br/>";
die();
}
This may also help with the logic behind the update/insert:
If trying to insert this record:
sku_id | group_id | dealer_id | start_date | expire_date | locations
-------------------------------------------------------------------------------------------
123 10 15 2018-02-02 (start + 127 days) 3
If there are already any records for the same sku/group/dealer combo (123/10/15) AND the current date is before the expire_date for those records, we will be updating.
If the expire_date for those records has already passed, we will be inserting 3 new records (because locations has a value of 3)
So for the following records:
sku_id | dealer_id | locations
--------------------------------------
123 10 3
123 10 3
123 10 3
The logic would say "There are already records with the hypothetical unique index of sku_id/group_id/dealer_id (123/10/15) and the expire date has passed, so insert 3 records for
sku_id | group_id | dealer_id | start_date | expire_date | locations
-------------------------------------------------------------------------------------------
123 10 15 2018-02-02 (start + 127 days) 3
Otherwise, if those 3 records above had an expire_date of 2018-05-01, then we would update each row with the new expire_date (start_date + 127 days)