// name this code block
//https://workflow-automation.podio.com/inc/ajaxPreps.php?tt=U&wid=win630e71a19d5f4&s=5
$var_item_id = [*item_value_podio_item_id*]
$var_loai_tu_dong_hay_bang_tay = "Từ file import tự động"
//var_giaidoan_baogia_nhacungcap_json = json_decode(urldecode("[*item_value_pfprepfield:var_giai_doan_bao_gia_tu_nhacungcap*]"),true)
//print var_giaidoan_baogia_nhacungcap_json
//var_giaidoan_baogia_nhacungcap_output = find_in_json(var_giaidoan_baogia_nhacungcap_json,"giaidoan_baogia_nhacungcap")
//print var_giaidoan_baogia_nhacungcap_output
var_giaidoan_baogia_nhacungcap_output = podio_item_field_get($var_item_id,"bao-gia-tu-nha-cung-cap-thuoc-giai-doan",0)
print "var_giaidoan_baogia_nhacungcap_output"
print var_giaidoan_baogia_nhacungcap_output
//var_nhacungcap_duan_podioitemid_json = json_decode(urldecode("[*item_value_pfprepfield:var_nha_cung_cap_id_va_duan_id*]"),true)
//print var_giaidoan_baogia_nhacungcap_json
//var_nhacungcap_duan_podioitemid_output = find_in_json(var_nhacungcap_duan_podioitemid_json,"nhacungcap_duan_podioitemid")
//print var_nhacungcap_duan_podioitemid_output
var_nhacungcap_duan_podioitemid_output = podio_item_field_get($var_item_id,"podioitemid-cua-ncc-va-du-an",0)
print "var_nhacungcap_duan_podioitemid_output"
print var_nhacungcap_duan_podioitemid_output
var_duan_podioitemid_in_importfile = podio_item_field_get($var_item_id,"du-an-podioitemid",0)
print "var_duan_podioitemid_in_importfile"
print var_duan_podioitemid_in_importfile
ketqua = podio_item_files_search($var_item_id,$tenfile)
//print $ketqua
varkey = "file_id"
chisofile_id = find_in_json($ketqua,$varkey)
//ketqua = podio_item_files_search(1301465424,$tenfile)
//print ketqua
//print chisofile_id
//1506506572
//return ketqua
ketqua_noidungfile = podio_file_xls_parse($chisofile_id)
//print ketqua_noidungfile
//ketqua_noidungfile = ketqua_noidungfile.replace (/:(\d+)([,\}])/g, ':"$1"$2');
//return ketqua_noidungfile
//print ketqua_noidungfile
ketqua_noidungfile = json_encode($ketqua_noidungfile, true);
outputend = str_replace('Stt','so-thu-tu', $ketqua_noidungfile)
//outputend = str_replace("Luong hang thang","money", $outputend)
outputend = str_replace("itemID","ma-so-id-cua-san-phamdich-vu-nay", $outputend)
outputend = str_replace("item ID","ma-so-id-cua-san-phamdich-vu-nay", $outputend)
outputend = str_replace("Unit price","gia-goc-rieng-cho-item-nay", $outputend)
outputend = str_replace("AAA","don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num", $outputend)
outputend = str_replace("Total","op01-don-tong-gia-goc-mua-vao-duoc-chon-text", $outputend)
//print "outputend"
//print outputend
//ketqua = str_replace("Stt","staff-id", $ketqua)
//print outputend
//data = '[{"staff-id":147,"staff-name":"a111 a1","money":100},{"staff-id":2,"staff-name":"b2 b2","money":"222"},{"staff-id":3,"staff-name":"c1 c2","money":"333"},{"staff-id":4,"staff-name":"d1 d2","money":"456"},{"staff-id":5,"staff-name":"e1 e2","money":"567"}]'
json = json_decode((outputend),true)
foreach (json as key => giatri_field){
//newname = field["ten-nhan-vien-o-may-cham-cong"];
//new_ngay= field["ngay"];
//print new_ngay
new_sanpham_dichvu_id = strval(giatri_field["ma-so-id-cua-san-phamdich-vu-nay"]);
new_don_gia_goc_tu_nhacungcap = strval(giatri_field["don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num"]);
//new_don_gia_goc_tu_nhacungcap = field["don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num"];
//print "new_don_gia_goc_tu_nhacungcap"
//print new_don_gia_goc_tu_nhacungcap
//print "Mã số SPDV"
//print $new_sanpham_dichvu_id
new_tong_toan_bo = (giatri_field["op01-don-tong-gia-goc-mua-vao-duoc-chon-text"]);
//App product and service: https://podio.com/benhvienotovn/warehouse/apps/19949735/hooks
var_app_sanpham_dichvu_id = "19949735";
var_field_sanpham_dichvu_id = "243495971";
varsearch_sanpham_dichvu = stream_podio_app_search($var_app_sanpham_dichvu_id,$var_field_sanpham_dichvu_id,$new_sanpham_dichvu_id,'e',0)
//print varsearch
varget_sanpham_dichvu_itemid_json = stream_fetch($varsearch_sanpham_dichvu,100);
var_sanpham_dichvu_import_podioitemid= find_in_json($varget_sanpham_dichvu_itemid_json,"podioitemid-cua-san-pham-dich-vu")
//print "var_sanpham_dichvu_import_podioitemid"
//print var_sanpham_dichvu_import_podioitemid
//print "var_nhacungcap_duan_podioitemid_output"
//print var_nhacungcap_duan_podioitemid_output
var_sanpham_dichvu_duan_nhacungcap_podioitemid = "spdv"+var_sanpham_dichvu_import_podioitemid + var_nhacungcap_duan_podioitemid_output
//var_sanpham_dichvu_duan_nhacungcap_podioitemid = substr("_" + var_nhacungcap_duan_podioitemid_output + var_sanpham_dichvu_import_podioitemid, 1)
//print "var_sanpham_dichvu_duan_nhacungcap_podioitemid"
//print var_sanpham_dichvu_duan_nhacungcap_podioitemid
//newmoney = strval(field["money"]);
addrelated_sanpham_dichvu_import_podioitemid = floatval($var_sanpham_dichvu_import_podioitemid);
//print "addrelated_sanpham_dichvu_import_podioitemid"
//print addrelated_sanpham_dichvu_import_podioitemid
addrelated_importitem = floatval($var_item_id);
var_created_on_time= date("Y-m-d h:i:s", strtotime("now"+"1 hours"))
//print $addrelated_importitem
//newfield = {"staff-id":newid,"staff-name":newname,"money":newmoney}
//newfield = {"staff-id":newid,"staff-name":newname,"money":newmoney,"noi-bao-cao-thang":2125635339}
//newfield = {"staff-id":newid,"staff-name":newname,"money":newmoney,"noi-bao-cao-thang":addrelated_report}
//newfield = {"ten-nhan-su-nhap-file-tu-may-cham-cong":$var_tennhansuimportfile,"nguon-item-link-file-import-excel":$var_importfilelink,"ma-so-nhan-su-o-may-cham-cong":$newid,"ten-nhan-vien-o-may-cham-cong":$newname}
//newfield = {"noi-voi-item-import-file-excel-vao":$var_item_id,"ma-so-nhan-su-o-may-cham-cong":$newid,"ten-nhan-vien-o-may-cham-cong":$newname}
//newfield = {"noi-voi-item-import-file-excel-vao":$addrelated_importitem,"ngay":new_ngay,"tong-toan-bo":new_tong_toan_bo,"gio-vao":$new_giovao,"gio-ra":$new_giora,"ma-so-nhan-su-o-may-cham-cong":$newid,"ten-nhan-vien-o-may-cham-cong":$newname}
//newfield = {"noi-voi-item-import-file-excel-vao":$addrelated_importitem}
//print newfield
// Tạo item mới ở APP Báo giá item: https://podio.com/benhvienotovn/quotation/apps/24413459/hooks
if (var_giaidoan_baogia_nhacungcap_output =="Giai đoạn chốt Trước Nghiệm Thu"){
replace_field = '"gia-goc-duoc-tao-ra":var_loai_tu_dong_hay_bang_tay,"don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num":new_don_gia_goc_tu_nhacungcap '
//print "Giai đoạn chốt Trước Nghiệm Thu"
//newfield = {"san-pham":$addrelated_sanpham_dichvu_import_podioitemid,"gia-goc-duoc-tao-ra":var_loai_tu_dong_hay_bang_tay,"nhap-du-an":var_duan_itemid,"don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num":new_don_gia_goc_tu_nhacungcap};
print "Giai đoạn chốt Trước Nghiệm Thu"
}else{
if (var_giaidoan_baogia_nhacungcap_output =="Giai đoạn chốt Sau Nghiệm Thu") {
print "Giai đoạn chốt Sau Nghiệm Thu"
replace_field = '"gia-goc-duoc-tao-ra":var_loai_tu_dong_hay_bang_tay,"don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num":new_don_gia_goc_tu_nhacungcap ';
//newfield = {"san-pham":$addrelated_sanpham_dichvu_import_podioitemid,"gia-goc-duoc-tao-ra":var_loai_tu_dong_hay_bang_tay,"nhap-du-an":var_duan_itemid,"don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num":new_don_gia_goc_tu_nhacungcap};
}else{
print "Giai đoạn Còn lại"
replace_field ='"don-gia-giai-doan-nhap-nha-cung-cap":'+$new_don_gia_goc_tu_nhacungcap+''
//print "replace_field "
//print replace_field
//newfield = {"tao-luc":$var_created_on_time,"san-pham":$addrelated_sanpham_dichvu_import_podioitemid,"gia-goc-duoc-tao-ra":var_loai_tu_dong_hay_bang_tay,"nhap-du-an":var_duan_itemid,"don-gia-mua-vao-goc-nha-cung-cap-rieng-cho-item-nay-num":new_don_gia_goc_tu_nhacungcap};
}
}
new_update_field = '{"san-pham":'+$addrelated_sanpham_dichvu_import_podioitemid+',"gia-goc-duoc-tao-ra":"'+var_loai_tu_dong_hay_bang_tay+'",'+$replace_field+'} '
new_create_field = '{"san-pham":'+$addrelated_sanpham_dichvu_import_podioitemid+',"nhap-du-an":var_duan_podioitemid_in_importfile,"gia-goc-duoc-tao-ra":"'+var_loai_tu_dong_hay_bang_tay+'",'+$replace_field+'} '
//newfield_format= preg_replace("/ +/", ,$newfield)
//print "new_create_field"
//print new_create_field
//print "new_update_field"
//print new_update_field
//print "newfield_format"
//print newfield_format
//App Báo giá item: https://podio.com/benhvienotovn/quotation/apps/bao-gia-item
var_app_bao_gia_item_id = "24413459";
var_field_sanpham_dichvu_duan_nhacungcap_id = "243495877";
varsearch_sanpham_dichvu_duan_nhacungcap = stream_podio_app_search($var_app_bao_gia_item_id,$var_field_sanpham_dichvu_duan_nhacungcap_id,$var_sanpham_dichvu_duan_nhacungcap_podioitemid,'e',0)
//print varsearch
varget_sanpham_dichvu_duan_nhacungcap_itemid_json = stream_fetch($varsearch_sanpham_dichvu_duan_nhacungcap,100);
print "varget_sanpham_dichvu_duan_nhacungcap_itemid_json"
print varget_sanpham_dichvu_duan_nhacungcap_itemid_json
var_sanpham_dichvu_duan_nhacungcap_import_podioitemid= find_in_json($varget_sanpham_dichvu_duan_nhacungcap_itemid_json,"ma-gop-id-spdv-va-du-an-va-nha-cung-cap")
print var_sanpham_dichvu_duan_nhacungcap_import_podioitemid
var_baogiaitem_podioitemid= find_in_json($varget_sanpham_dichvu_duan_nhacungcap_itemid_json,"bao-gia-itemid")
print var_baogiaitem_podioitemid
if (var_baogiaitem_podioitemid == "" ){
print "abc"
//new_item_in_bao_gia_item_id = podio_item_create(24413459,new_create_field,true,true);
print new_item_in_bao_gia_item_id;
}else{
//update_old_item_in_bao_gia_item_id = podio_item_fields_update($var_baogiaitem_podioitemid,$new_update_field,true,true)
print "xyz"
}
}