Edit file File name : instacart_feedfile_dnld.php Content :<?php //set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib1'); //include_once('phpseclib1/Net/SFTP.php'); include("../include/config.php"); //include("../include/functions.php"); //die('here25'); //validate_admin(); //error_reporting(E_ALL); //ini_set('display_errors', '1'); //$isCLI = is_cli(); if($_POST['dnld_ic_ff']!=''){ $icsid = $_POST['dnld_ic_ff']; $isCLI =true; if( !$isCLI ) { die("Sorry! Cannot run in a browser!"); } else { $imgPath = 'https://www.quicklly.com/upload_images/product/'; //Get data for FULL INVENTORY FEED FILE $section =1; $addCod=""; /*$sqlPrduct = "SELECT p.*,pp.size,pp.size_type,pp.mrp_price,pp.tax_method,pp.tax_value,pp.in_stock,pp.sell_price,c.id as sub_cat,c.parent_id as department, c.maincategory as asile, s.storename,pp.barcode,pp.weight,pp.weight_type,sz.state,sz.city FROM `insta_product` p JOIN insta_productprice pp ON p.id=pp.product_id JOIN tbl_maincategory c ON c.id=p.cat_id JOIN insta_stores s ON s.storeid=p.storeid JOIN insta_store_zip sz ON sz.storeid=p.storeid WHERE p.status=1 AND s.status=1 AND section='$section' AND p.storeid ='$icsid'";*/ //Condition for add All Item (FULL INVENTORY FILE- Master Data) //$addCod = " AND date(p.cron_date) IS NULL "; //Condition for File consists of only items you want changed (DELTA FILE). $updateCond= " AND date(p.updated_date) = date(now()) "; $sqlPrduct= "SELECT `lookup_code`, `store_identifier`, `price`, `brand_name`, `item_name`, `cost_unit`, `size`, `size_uom`, `balance_on_hand`, `available`, `par_weight`, `department`, `aisle`, `item_details`, `remote_image_URL`, `additional_images_url` FROM insta_ff_dnld WHERE `store_identifier` LIKE '$icsid' "; $sql=$obj->query($sqlPrduct . $addCod ); $numRows=$obj->numRows($sql); $prdResult=$obj->fetchNextObject($sql); //echo "<pre>===";print_r($prdResult);die; //Predefined column for inventroy feed file`lookup_code`, `store_identifier`, `price`, `brand_name`, `item_name`, `cost_unit`, `size`, `size_uom`, `balance_on_hand`, `available`, `par_weight`, `department`, `aisle`, `item_details`, `remote_image_URL`, `additional_images_url` $fullInventoryColumn = array("lookup_code","store_identifier","price", "brand_name","item_name","cost_unit", "size","size_uom","balance_on_hand", "available", "par_weight","department", "aisle","item_details","remote_image_URL", "additional_images_url" ); //$dateTime = date("Ymd").'_'.date("hi"); $dateTime = date("Ymd"); $dateTime = $dateTime.'_'.date('hi'); $filePath = ''; $sftpRemoteDir = 'public_html/upload_insta_flat_file/opt/'; //$sftpRemoteDir = 'inventory-files/';// For Live insta path $filePthCpy = 'upload_insta_flat_file/opt/'; if($numRows > 0 ) { while($prdResult=$obj->fetchNextObject($sql)) { //store_id-Product_id ==lookupcode //$lookup_code = 'QC-'.$prdResult->barcode.'-D'; $lookup_code =$prdResult->lookup_code; $item_name = ucwords(removeLineBreakSpaces($prdResult->item_name)); $size = $prdResult->size; $size_uom = ucfirst($prdResult->size_uom); $available = $prdResult->available; $cost_unit = ucfirst($prdResult->cost_unit); $price = $prdResult->price; $department = ucwords(strtolower($prdResult->department)); $aisle = ucwords($prdResult->aisle); $storeName = $prdResult->storename; //$storePrefix = storePrefix($storeName); $cityPrefix = ''; //$cityPrefix = '-JSQ'; //echo "<pre>";print_r($storePrefix);die; //This unique combination of storename first letter-store id //$storePrefix='ICC'; $store_indentifier = $prdResult->store_identifier; //$store_indentifier = $storePrefix.'-'.$prdResult->storeid; //$//store_indentifier = "FF-100"; $brand_name =''; $balance_on_hand = ''; $par_weight = ''; $item_details = $prdResult->item_details; $remote_image_URL= $prdResult->remote_image_URL; $additional_images_url=''; $storeZipcode=''; //$taxValue =$prdResult->tax_value; //$additional_images_url='[{""angle"":""angle_right"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""},{""angle"":""unknown"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""}]'; //$price = number_format(($price+$hikePrice),2); //echo "<pre>===";print_r($storeHikeArr);die; //$store_indentifier= 'IG-103-ED-NJ'; $fullInventoryData[] = array($lookup_code,$store_indentifier,$price, $brand_name,$item_name,$cost_unit, $size,$size_uom,$balance_on_hand, $available,$par_weight,$department, $aisle,$item_details,$remote_image_URL, $additional_images_url ); } //Use store id for store name which is setup on instacart as file name //$fileName = 'quicklly_store_'."_".$dateTime.'.csv'; //$fileName = $dateTime.'_quicklly_full'.'.csv'; //$fileName = $dateTime.'_quicklly_grocery_full_sipce_box'.'.csv'; //for dtc //$fileName = $dateTime.'_dtc_quicklly_full'.'.csv'; $fileName = $dateTime.'_quicklly_grocery_full'.'.csv'; $fileNamePath = $filePath.$fileName; //echo "<pre>";print_r($fullInventoryData);die; $generateCsv = genrateFlatFile($fileNamePath,$fullInventoryColumn,$fullInventoryData); if($generateCsv) { //$sftp = new Net_SFTP(INSTA_SFTP_HOST); //if (!$sftp->login(INSTA_SFTP_USER, INSTA_SFTP_PWD)) { //exit('Login Failed'); //} //For Upload transaction log file on quicklly sftp //$sftpFile = $sftp->put($sftpRemoteDir.$fileName, $fileNamePath, NET_SFTP_LOCAL_FILE); //if($sftpFile) { //rename($fileNamePath, $filePthCpy.$fileName); //} $msg = "Product feed file downloaded successfully".'<br>'; //header("location:ic-ff-dnld.php?success=Product feed file downloaded successfully!"); //exit; } }else { echo $msg = "Error in store id pls enter valid store id!".'<br>'; header("location:ic-ff-dnld.php?error=Error in store id pls enter valid store id!!"); exit; } //Get data for DELTA FILE (File consists of only items that need to changed/update on Instacart.) // $sqlDelta=$obj->query($sqlPrduct . $updateCond ); // $numRowsDelta=$obj->numRows($sqlDelta); if($numRowsDelta__VVV > 0 ) { while($prdDResult=$obj->fetchNextObject($sqlDelta)) { //store_id-Product_id ==lookupcode $lookup_code = $prdResult->barcode; //$lookup_code = $prdDResult->id; $item_name = removeLineBreakSpaces($prdResult->product_name); $size = $prdDResult->weight; $size_uom = $prdDResult->weight_type; $available = $prdDResult->in_stock; $cost_unit = $prdDResult->weight_type; $department = ucwords(strtolower(getParentname($prdResult->department))); $aisle = ucwords($prdResult->aisle); $price = $prdResult->sell_price; $storeName = $prdResult->storename; $storePrefix = storePrefix($storeName); //This unique combination of storename first letter-store id $store_indentifier = $storePrefix.'-'.$prdResult->storeid; $brand_name =''; $balance_on_hand = ''; $par_weight = ''; $item_details = $prdResult->product_details; $remote_image_URL= $imgPath.$prdResult->photo; //$additional_images_url='[{""angle"":""angle_right"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""},{""angle"":""unknown"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""}]'; $additional_images_url=''; $storeZipcode=''; $taxValue =$prdResult->tax_value; //$additional_images_url='[{""angle"":""angle_right"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""},{""angle"":""unknown"",""src"":""https://d2lnr5mha7bycj.cloudfront.net/product-image/file/73358771-b62e-4302-a481-974f52be7f9c.jpg""}]'; $storeHikeArr = getProductHikePrice($storeZipcode,$prdResult->storeid); $storeHike = $storeHikeArr['hike']; $hikePrice = number_format(getPercentOfNumber($price, $storeHike),2); $hikeTax = number_format(getPercentOfNumber($taxValue, $storeHike),2); $price = number_format(($price+$hikePrice),2); $price = hikerounder($price, $storeHike); $deltaFileData[] = array($lookup_code,$store_indentifier,$price, $brand_name,$item_name,$cost_unit, $size,$size_uom,$balance_on_hand, $available,$par_weight,$department, $aisle,$item_details,$remote_image_URL, $additional_images_url ); } //Use store id for store name which is setup on instacart // $fileNameDelta = 'quicklly_store_delta'."_".$dateTime.'.csv'; $fileNameDelta = $dateTime.'_quicklly_full'.'.csv'; $fileNamePath = $filePath.$fileNameDelta; $generateCsv = genrateFlatFile($fileNamePath,$fullInventoryColumn,$deltaFileData); //if($generateCsv==301) { //ftpFIleUpload($filePath,$sftpRemoteDir,$fileName); //$sftp = new Net_SFTP(INSTA_SFTP_HOST); //if (!$sftp->login(INSTA_SFTP_USER, INSTA_SFTP_PWD)) { //exit('Login Failed'); //} //For Upload transaction log file on quicklly sftp //$sftpFIle = $sftp->put($sftpRemoteDir.$fileNameDelta, $fileNamePath, NET_SFTP_LOCAL_FILE); //if($sftpFile) { //rename($fileNamePath, $filePthCpy.$fileNameDelta); //$msg .= "Product delta feed file uploaded successfully"; // } } //echo $msg; } }else { echo "Error in store id pls enter valid store id!"; } function genrateFlatFile($fileName,$columnArr,$dataArr ) { //echo "<pre>";print_r($columnArr); //echo "<pre>";print_r($dataArr);die; //$file = fopen($fileName, 'wb'); // save the column headers //fputcsv($file, $columnArr); // Set the content type to CSV header('Content-Type: text/csv; charset=utf-8'); // Set the response header to specify that the file should be downloaded as an attachment header('Content-Disposition: attachment; filename='.$fileName); //$dataArr .= $columnArr; // Open a file handle for writing $file = fopen('php://output', 'w'); $dataArr_header[] = ["lookup_code","store_identifier","price", "brand_name","item_name","cost_unit", "size","size_uom","balance_on_hand", "available", "par_weight","department", "aisle","item_details","remote_image_URL", "additional_images_url" ]; $dataArr1 = array_merge($dataArr_header,$dataArr); //echo "<pre>";print_r($dataArr1);die; // save each row of the data foreach ($dataArr1 as $row) { fputcsv($file, $row); } // Close the file fclose($file); return true; } //Method to get percentage of number function getPercentOfNumber($number, $percent){ return ($percent / 100) * $number; } //Get hike price function getProductHikePrice($zipcode='',$storeId) { $cond = ''; if($zipcode !='') { $cond = " and zip='$zipcode'"; } // echo "select hike,commission,minimum_fee from insta_store_zip where 1=1 $cond and storeid='$storeId'";die; $sql = mysqli_query($GLOBALS['conn'], "select hike,commission,minimum_fee from insta_store_zip where 1=1 $cond and storeid='$storeId'"); $result = mysqli_fetch_assoc($sql); return ($result); } function is_cli() { if( defined('STDIN') ) { return true; } if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) { return true; } return false; } //Method to remove space and line break from product name function removeLineBreakSpaces($str) { $string = trim(preg_replace('/\s\s+/', ' ', $str)); return $string; } function getParentname($p_id) { $sql = mysqli_query($GLOBALS['conn'], "select maincategory from tbl_maincategory where id='$p_id'"); $result = mysqli_fetch_assoc($sql); return ($result['maincategory']); } function hikerounder($price, $hike) { //$num=$num),2); //$price=number_format($price,2); $hikes = number_format(($price * $hike / 100), 2); $price = rounder(number_format($price + $hikes, 2)); return $price; } //Get the first letter of each word from store name function storePrefix($storeName) { $words = preg_split("/\s+/", $storeName); $acronym = ''; foreach ($words as $w) { $acronym .= ucfirst($w[0]); } return $acronym; } function rounder($num) { $fln = $num - floor($num); if ($fln > 0.00 and $fln < 0.1) { $fln = 0.09; } if ($fln > 0.10 and $fln < 0.2) { $fln = 0.19; } if ($fln > 0.20 and $fln < 0.3) { $fln = 0.29; } if ($fln > 0.30 and $fln < 0.4) { $fln = 0.39; } if ($fln > 0.40 and $fln < 0.5) { $fln = 0.49; } if ($fln > 0.50 and $fln < 0.6) { $fln = 0.59; } if ($fln > 0.60 and $fln < 0.7) { $fln = 0.69; } if ($fln > 0.70 and $fln < 0.8) { $fln = 0.79; } if ($fln > 0.80 and $fln < 0.9) { $fln = 0.89; } if ($fln > 0.90 and $fln < 0.99) { $fln = 0.99; } return floor($num) + $fln; } Save