<?php
ob_start();
session_start();
include("../_init.php");
header('Content-Type: application/json; charset=UTF-8');

if (!is_loggedin()) { 
  http_response_code(422); 
  echo json_encode(['error'=>'login']); 
  exit; 
}
if (user_group_id()!=1 && !has_permission('access','read_customer_profile')) { 
  http_response_code(422); 
  echo json_encode(['error'=>'perm']); 
  exit; 
}

$customer_id = isset($_POST['customer_id']) ? (int)$_POST['customer_id'] : 0;
if ($customer_id <= 0) { 
  http_response_code(422); 
  echo json_encode(['error'=>'bad id']); 
  exit; 
}

$store_id = store_id();
$st = db()->prepare("SELECT due FROM customer_to_store WHERE store_id=? AND customer_id=?");
$st->execute([$store_id, $customer_id]);
$due = (float)$st->fetchColumn();

echo json_encode(['due'=>$due]);
