global $_APIrunning,$_APIvars;
if($_APIrunning) exit('import items already running. try again later. '.$_APIvars['v1'].$_APIvars['v2']);
goAsync('ok :start run import items....'.$_APIvars['v1'].$_APIvars['v2']);
set_time_limit(3600);
$data = strip_slashes($_REQUEST['data']);
$oData = json_decode($data,true);
//_logvar($oData);
$st=time();
//_logadd('start acct:'.$_SESSION['__selaccountId']);
echo importItems($oData) ;
$durationt = time()-$st . 'sec';
//_logadd('finish acct:'.$_SESSION['__selaccountId'].' in '. $durationt);
function importItems($oData){
global $conns,$glConfig;
if($conns['idb']->readOnly ) die('ERR:10 :'.("User ".$_SESSION['userName']." is readonly. You must pay the access fee for this user.") );
//special dami pentru sync x5
$updPrice = cvBool($oData['updListPrice']);
$iditm_as_idart = cvBool($oData['iditm_as_idart']);
////////////
$vendor = putValue($oData['vendor'] ,'' );
$keyf = putValue($oData['keyf'] ,'code_vendor' );
// $keyf_alt = putValue($oData['keyf_alt'] ,'i3' );
$itype = $conns['idb']->executereturn("select idtype from item_types where lower(typename)=lower('".$oData['itemtype']."')")*1;
//_logadd('itype:'.$itype);
$arrCodes=$oData['update_codes'];
$aitems = $oData['items'];
// i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, ex1,ex2,ex3,ex4,ex5, tx, weight, volume, stock, price, ecotx, recom_price, ccy, offer, min_po, min_so, urlimg1, urlimg2, code_vendor
$sqlX=''; $wcIn='';
foreach($aitems as $aitem){
$wcIn.= "'".trim($aitem[$keyf])."',";
}
$wcIn = trim($wcIn,',');
if(!$wcIn){ _logadd('empty items to acct:'.$_SESSION['__selaccountId']);exit();};
if($arrCodes)foreach($arrCodes as $code){$code_list .= "'". strtolower(pgSafeStr($code)) ."',";}; $code_list=trim($code_list,',');
if($code_list){//set -1 to prevent stock update during import. zero stock all items from this vendor
$sqlZero ="update items set stock_vendor=0 where dvendor='".$vendor."' and ".$keyf." in (".$code_list.");".chr(13);
}else{//zero stock all items from this vendor
$sqlZero ="update items set stock_vendor=0 where dvendor='".$vendor."' and ".$keyf ." not in (".$wcIn.");".chr(13);
}
$conns['idb']->execute($sqlZero);
$arrX=Array();
$rsX = $conns['idb']->opendataset("select ".$keyf ." as k, * from items where ".$keyf ." in (".$wcIn.")");
while(!$rsX->eof){
$arrX['k'.$rsX->k] = Array("iditm"=>$rsX->iditm, "i0"=>$rsX->i0, "i1"=>$rsX->i1, "i2"=>$rsX->i2, "i3"=>$rsX->i3, "i4"=>$rsX->i4, "i5"=>$rsX->i5, "i6"=>$rsX->i6, "i7"=>$rsX->i7, "i8"=>$rsX->i8, "i9"=>$rsX->i9,
"ex1"=>$rsX->ex1, "ex2"=>$rsX->ex2, "ex3"=>$rsX->ex3, "ex4"=>$rsX->ex4, "ex5"=>$rsX->ex5,
"tx"=>$rsX->tx, "ecotx"=>$rsX->ecotx, "listcost"=>$rsX->listcost, "listprice"=>$rsX->listprice, "stock_vendor"=>$rsX->stock_vendor );
$rsX->next();
}
//_logvar($arrX);
$sqlUpd='';
foreach($aitems as $aitem){
$aitem = array_map("trim", $aitem);
$keyv = $aitem[$keyf];
$iditm= $arrX['k'.$keyv]['iditm'];
$price= round($aitem['price'],4);
$r_price= round($aitem['recom_price'],4);
$tx=round($aitem['tx'],2);
$ecotx= round($aitem['ecotx'],4);
$ccy=$glConfig['currency'];
if($iditm){//update
//_logadd('id:'.$iditm) ;
$upd = '';
for($i=0;$i<=9;$i++) if($aitem['i'.$i]!== $arrX['k'.$keyv]['i'.$i]) $upd .= "i".$i."= '".$aitem['i'.$i]."',";
for($i=1;$i<=5;$i++) if($aitem['ex'.$i]!== $arrX['k'.$keyv]['ex'.$i]) $upd .= "ex".$i."= '".$aitem['ex'.$i]."',";
if(!$updPrice){
if($price != $arrX['k'.$keyv]['listcost']*1) {
// _logadd('price:'.$price , 'listcost:'.$arrX['k'.$keyv]['listcost']*1);
$upd .= "listcost=".$price.", listprice = case when listmarg<>0 then ".$price." *(1+listmarg/100) else ".$r_price." end ,";
}
}else{ // fac update direct pe listprice
if($r_price != $arrX['k'.$keyv]['listprice']*1) {
// _logadd('price:'.$price , 'listcost:'.$arrX['k'.$keyv]['listcost']*1);
$upd .= "listprice = ".$r_price.",";
}
}
if($aitem['tx']!= $arrX['k'.$keyv]['tx']) $upd .= "tx= ".$tx.",";
if($aitem['ecotx']!= $arrX['k'.$keyv]['ecotx']) $upd .= "ecotx= ".$ecotx.",";
if($aitem['stock']!= $arrX['k'.$keyv]['stock_vendor']*1) {
// _logadd('stock:'.$aitem['stock'] , 'stock_vendor:'.$arrX['k'.$keyv]['stock_vendor']*1);
$upd .= "stock_vendor= ".$aitem['stock'].",";
}
//special dami
if($aitem['delivdays']!=''){//daca exista in array delivdays
if($aitem['delivdays']!= $arrX['k'.$keyv]['delivdays']) $upd .= "delivdays= ".$aitem['delivdays'].",";
}
if($upd) $sqlUpd.="update items set ".$upd." code_vendor='".$aitem['code_vendor']."' where iditm=".$iditm.";".chr(13);
}else{//addnew
//special dami - am pus campul si la insert
if($aitem['delivdays']==''){
$aitem['delivdays']=0;
}
$defiditm="DEFAULT";
if ($iditm_as_idart) $defiditm=$aitem['idart'];
$lmarg = (($aitem['recom_price']-$aitem['price'])/$aitem['price'])*100;
$offer=putValue($aitem['offer'],translate("New products"));
$sql="insert into items (iditm, itype, i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, ex1,ex2,ex3,ex4,ex5, stock_vendor, offer, listcost, listmarg, listprice, dvendor, code_vendor, onweb, lccy, tx, weight, volume, min_po, min_so, ecotx,delivdays)
values(".$defiditm.",".$itype.",'".$aitem['i0']."','".$aitem['i1']."','".$aitem['i2']."','".$aitem['i3']."','".$aitem['i4']."','".$aitem['i5']."','".$aitem['i6']."','".$aitem['i7']."','".$aitem['i8']."','".$aitem['i9']."',
'".$aitem['ex1']."', '".$aitem['ex2']."', '".$aitem['ex3']."', '".$aitem['ex4']."', '".$aitem['ex5']."',
".$aitem['stock'].",'".$offer."',".$price.",".$lmarg.",".$r_price.",'".$vendor."','".$aitem['code_vendor']."',true, '".$ccy."', ".$tx.", ".$aitem['weight'].", ".$aitem['volume'].", ".$aitem['min_po'].", ".$aitem['min_so'].", ".$ecotx.",".$aitem['delivdays'].")
returning iditm;";
// _logadd('sqlAddnew: '.$sql);
$itmid=$conns['idb']->executeReturn($sql);
//$fileContent1=file_get_contents($aitem['urlimg1']);$imgSize1=strlen($fileContent1); $imgData1 = chunk_split(base64_encode ($fileContent1));
//$fileContent2=file_get_contents($aitem['urlimg2']);$imgSize2=strlen($fileContent2); $imgData2 = chunk_split(base64_encode ($fileContent2));
getImageString($aitem['urlimg1'],$imgData1,$imgSize1,$img_type1);
getImageString($aitem['urlimg2'],$imgData2,$imgSize2,$img_type2);
$sqlImg='';
if($imgData1)$sqlImg .= "insert into items_img (itmid, imgname, imgdata, imgsize) values(".$itmid.", 'item".$itmid."_1".image_type_to_extension($img_type1)."' , '".$imgData1."', 0".$imgSize1.");";
if($imgData2)$sqlImg .= "insert into items_img (itmid, imgname, imgdata, imgsize) values(".$itmid.", 'item".$itmid."_2".image_type_to_extension($img_type2)."', '".$imgData2."', 0".$imgSize2.");";
if($sqlImg)$conns['idb']->execute($sqlImg);
}
}
// $sqlUpd .="update items set stock_vendor=0 where stock_vendor=-1 and dvendor='".$vendor."';".chr(13);
//_logadd('sqlUpdate: '.$sqlUpd);
$conns['idb']->execute($sqlUpd);
/*
//fix empty img
$emptyImgSize=5873;
$idiim=$conns['idb']->executereturn("select idiim from items_img where imgsize= ".$emptyImgSize." and itmid=".$iditm);
$imgcount=$conns['idb']->executereturn("select count(idiim) from items_img where itmid=".$iditm);
if($idiim || $imgcount<2){
// _logadd('emptyImgSize:'.$idiim. ' '. $aitem['urlimg1']);
getImageString($aitem['urlimg1'],$imgData1,$imgSize1,$img_type1);
getImageString($aitem['urlimg2'],$imgData2,$imgSize2,$img_type2);
$itmid=$iditm;
$sqlImg="delete from items_img where itmid=".$iditm.";";
if($imgData1)$sqlImg .= "insert into items_img (itmid, imgname, imgdata, imgsize) values(".$itmid.", 'item".$itmid."_1".image_type_to_extension($img_type1)."' , '".$imgData1."', 0".$imgSize1.");";
if($imgData2)$sqlImg .= "insert into items_img (itmid, imgname, imgdata, imgsize) values(".$itmid.", 'item".$itmid."_2".image_type_to_extension($img_type2)."', '".$imgData2."', 0".$imgSize2.");";
if($sqlImg)$conns['idb']->execute($sqlImg);
}
*/
return 'ok :';
}
function getImageString($url,&$imgData,&$imgSize,&$img_type){ $imgData='';$imgSize=0;$img_type=0;
$image = imageResizeFromFile($url, $img_type, $width=280, $height=400, true);
if(!$image) return;
$filename= tempnam("/tmp", "resample");
$img_type=IMAGETYPE_JPEG;
imageOutput($image, $filename, $img_type);
$imgData = chunk_split(base64_encode (file_get_contents($filename)));
$imgSize = filesize($filename);
unlink($filename);
return true;
}
?>