<?php
        session_cache_expire(1440);
        session_start();
        session_name("insys");
        include "../include/functions.php";
        include "../class/UctVw.php";

        // $paramString = " WHERE fdmodified_date > '2019-01-20' ";
        
        $DB1_Host = "localhost";
        $DB1_User = "root";
        $DB1_Pswd = "";
        $DB1 = "isnet-abr";
        
        $DB2_Host = "jms-makassar.ddns.net:8080";
        $DB2_User = "root";
        $DB2_Pswd = "";
        $DB2 = "isnet-jms";
           
        // 1st connection (main)
        $conn1 = new mysqli($DB1_Host,$DB1_User,$DB1_Pswd,$DB1) or die("Could not connect");   
        mysqli_select_db($conn1,$DB1) or die("Could not select database");    
        
        // 2nd connection
        // $conn2 = new mysqli($DB2_Host,$DB2_User,$DB2_Pswd,$DB2) or die("Could not connect");   
        // mysqli_select_db($conn2,$DB2) or die("Could not select database");
        
        // Get Stock Hdr query
        $db1_ssql=" SELECT * FROM tlstock_hdr {$paramString} ORDER BY ftitem_code";
	$db1_rs1=mysqli_query($conn1,$db1_ssql);

        // $counter_item = 0;
        
        while($db1_r1=mysqli_fetch_assoc($db1_rs1)) {
                // pr("sql db1 : '{$db1_r1['ttl']}'");
                $ftitem_name = addslashes($db1_r1['ftitem_name']);
                $ftprint_name = addslashes($db1_r1['ftprint_name']);
                
                echo "INSERT INTO tlstock_hdr (ftbranch_code, ftitem_code, ftitem_name, ftitem_size, ftprint_name, ftalias_name, ftuom_code, 
                            ftstock_category_code, ftstock_type_code, ftbrand_code, ftstock_group_code, ftstock_subgroup_code, 
                            ftcolor_code, ftsize_code, ftstock_group_name, ftstock_subgroup_name, ftpart_no, 
                            ftsupplier_code, ftitem_code_supplier, ftitem_name_supplier, 
                            ffreorder_level, ffweight, 
                            ftweight_code, ftphoto, ftdescription, ffmaximum_order, ftgl_inv_acc, ftgl_sales_acc, 
                            ftgl_sales_ret_acc, ftgl_sales_disc_acc, ftgl_cogs_acc, ftgl_unbill_item_acc, ftcreated_user, fdcreated_date, 
                            ftmodified_user, fdmodified_date, fnstatus, fnpo_include, fnsales_include, ftcar_code) ";
                echo "<br>";

                echo "VALUES ('{$db1_r1['ftbranch_code']}', '{$db1_r1['ftitem_code']}', '{$ftitem_name}', '{$db1_r1['ftitem_size']}', '{$ftprint_name}', '{$db1_r1['ftalias_name']}', '{$db1_r1['ftuom_code']}', 
                            '{$db1_r1['ftstock_category_code']}', '{$db1_r1['ftstock_type_code']}', '{$db1_r1['ftbrand_code']}', '{$db1_r1['ftstock_group_code']}', '{$db1_r1['ftstock_subgroup_code']}', 
                            '{$db1_r1['ftcolor_code']}', '{$db1_r1['ftsize_code']}', '{$db1_r1['ftstock_group_name']}', '{$db1_r1['ftstock_subgroup_name']}', '{$db1_r1['ftpart_no']}', 
                            '{$db1_r1['ftsupplier_code']}', '{$db1_r1['ftitem_code_supplier']}', '{$db1_r1['ftitem_name_supplier']}', 
                            '{$db1_r1['ffreorder_level']}', '{$db1_r1['ffweight']}', 
                            '{$db1_r1['ftweight_code']}', '{$db1_r1['ftphoto']}', '{$db1_r1['ftdescription']}', '{$db1_r1['ffmaximum_order']}', '{$db1_r1['ftgl_inv_acc']}', '{$db1_r1['ftgl_sales_acc']}', 
                            '{$db1_r1['ftgl_sales_ret_acc']}', '{$db1_r1['ftgl_sales_disc_acc']}', '{$db1_r1['ftgl_cogs_acc']}', '{$db1_r1['ftgl_unbill_item_acc']}', '{$db1_r1['ftcreated_user']}', '{$db1_r1['fdcreated_date']}', 
                            'admin', now(), '{$db1_r1['fnstatus']}', '{$db1_r1['fnpo_include']}', '{$db1_r1['fnsales_include']}', '{$db1_r1['ftcar_code']}') ";
                echo "<br>";     

                echo "ON DUPLICATE KEY UPDATE";
                echo "<br>";
                echo "ftbranch_code = VALUES(ftbranch_code), ftitem_code = VALUES(ftitem_code), ftitem_name = VALUES(ftitem_name), ftitem_size = VALUES(ftitem_size), 
                        ftprint_name = VALUES(ftprint_name), ftalias_name = VALUES(ftalias_name), ftuom_code = VALUES(ftuom_code), 
                        ftstock_category_code = VALUES(ftstock_category_code), ftstock_type_code = VALUES(ftstock_type_code), ftbrand_code = VALUES(ftbrand_code), 
                        ftstock_group_code = VALUES(ftstock_group_code), ftstock_subgroup_code = VALUES(ftstock_subgroup_code), ftcolor_code = VALUES(ftcolor_code), 
                        ftsize_code = VALUES(ftsize_code), ftstock_group_name = VALUES(ftstock_group_name), ftstock_subgroup_name = VALUES(ftstock_subgroup_name), 
                        ftpart_no = VALUES(ftpart_no), ftsupplier_code = VALUES(ftsupplier_code), ftitem_code_supplier = VALUES(ftitem_code_supplier), 
                        ftitem_name_supplier = VALUES(ftitem_name_supplier), ffreorder_level = VALUES(ffreorder_level), ffweight = VALUES(ffweight), 
                        ftweight_code = VALUES(ftweight_code), ftphoto = VALUES(ftphoto), ftdescription = VALUES(ftdescription), 
                        ffmaximum_order = VALUES(ffmaximum_order), ftgl_inv_acc = VALUES(ftgl_inv_acc), ftgl_sales_acc = VALUES(ftgl_sales_acc), 
                        ftgl_sales_ret_acc = VALUES(ftgl_sales_ret_acc), ftgl_sales_disc_acc = VALUES(ftgl_sales_disc_acc), ftgl_cogs_acc = VALUES(ftgl_cogs_acc), 
                        ftgl_unbill_item_acc = VALUES(ftgl_unbill_item_acc), ftcreated_user = VALUES(ftcreated_user), fdcreated_date = VALUES(fdcreated_date), 
                        ftmodified_user = VALUES(ftmodified_user), fdmodified_date = VALUES(fdmodified_date), fnstatus = VALUES(fnstatus), 
                        fnpo_include = VALUES(fnpo_include), fnsales_include = VALUES(fnsales_include), ftcar_code = VALUES(ftcar_code);";
                echo "<br>";

                // $counter_item=$counter_item+1;
        }        
            
        echo "<br><br>*** Proses Selesai ***";
/*
	$ssql=" SELECT count(*) as ttl FROM tlstock_hdr ORDER BY ftitem_code";
	$rs1=mysql_query($ssql);
        // pr("sql 1 : {$ssql}");

        $counter_item = 0;
	while($r1=mysql_fetch_assoc($rs1)) {
                pr("sql db1 : '{$r1['ttl']}'");
                
                $ssql2="SELECT a.ftlocation_code, a.ftwarehouse_code FROM tllocation a
                        WHERE a.ftlocation_code NOT IN (SELECT ftlocation_code FROM tlstock_det WHERE ftitem_code = '{$r1['ftitem_code']}') ";
                $rs2=mysql_query($ssql2);     
                // pr("sql 2 : {$ssql2}");
                
                while($r2=mysql_fetch_assoc($rs2)) {  
                    $ssql3="INSERT INTO tlstock_det(ftBranch_Code,ftWarehouse_Code,ftLocation_Code,ftItem_Code,ftitem_name,
				ftBatch_Code,fnqty_on_hand,fnqty_available,ftCreated_User,fdCreated_Date,ftModified_User,fdModified_Date)
                            SELECT 'PUSAT','{$r2['ftwarehouse_code']}','{$r2['ftlocation_code']}','{$r1['ftitem_code']}','{$r1['ftitem_name']}',
                                    '' AS ftBatch_Code,'0' AS fnqty_on_hand,'0' AS fnqty_available,'admin',now(),'admin',now()
                          
                            ";

                    mysql_query($ssql3);
                    // pr("sql 3 : {$ssql3}");
                }    
                $counter_item=$counter_item+1;
                
	}*/


        /*
        $db1_ssql=" SELECT count(*) as ttl FROM tlstock_hdr ORDER BY ftitem_code";
	$db1_rs1=mysqli_query($conn1,$db1_ssql);

        while($db1_r1=mysqli_fetch_assoc($db1_rs1)) {
                pr("sql db1 : '{$db1_r1['ttl']}'");
        }        
            
        pr("<br>");
        
	$db2_ssql=" SELECT count(*) as ttl FROM tlstock_hdr ORDER BY ftitem_code";
	$db2_rs1=mysqli_query($conn2,$db2_ssql);

        while($db2_r1=mysqli_fetch_assoc($db2_rs1)) {
                pr("sql db2 : '{$db2_r1['ttl']}'");
        }        
         
        */
        // pr("TOTAL ITEM : {$counter_item} . *** SELESAI ***");
        
?>