prepare($sql); $stmh->bindValue(1, $num, PDO::PARAM_STR); $stmh->execute(); $customer = $stmh->fetch(PDO::FETCH_ASSOC); if (!$customer) { throw new Exception("거래처 정보를 찾을 수 없습니다."); } // Fetch sales details from output + output_extra $sql = "SELECT o.*, e.ET_total, e.estimateList, e.estimateSlatList, e.etcList, e.screen_unapprovedList, e.slat_unapprovedList, e.motorList, e.bendList, e.controllerList, e.accountList, e.ET_unapproved FROM " . $DB . ".output o LEFT JOIN " . $DB . ".output_extra e ON o.num = e.parent_num WHERE o.secondordnum = ? AND o.outdate BETWEEN ? AND ? AND (o.is_deleted IS NULL OR o.is_deleted = 0) ORDER BY o.outdate ASC"; $stmh = $pdo->prepare($sql); $stmh->bindValue(1, $num, PDO::PARAM_STR); $stmh->bindValue(2, $fromdate, PDO::PARAM_STR); $stmh->bindValue(3, $todate, PDO::PARAM_STR); $stmh->execute(); $rows = $stmh->fetchAll(PDO::FETCH_ASSOC); // Fetch payments from account table $sql = "SELECT registDate, amount FROM " . $DB . ".account WHERE secondordnum = ? AND registDate BETWEEN ? AND ? AND (is_deleted IS NULL OR is_deleted = 0) AND content = '거래처 수금' ORDER BY registDate ASC"; $stmh = $pdo->prepare($sql); $stmh->bindValue(1, $num, PDO::PARAM_STR); $stmh->bindValue(2, $fromdate, PDO::PARAM_STR); $stmh->bindValue(3, $todate, PDO::PARAM_STR); $stmh->execute(); $payments = $stmh->fetchAll(PDO::FETCH_ASSOC); // 다른 거래처는 기본적으로 마감 기준일 -1일 $lastMonthEnd = date("Y-m-t", strtotime($fromdate . " -1 days")); // echo '
';
// print_r($lastMonthEnd);
// echo '
'; // 이월잔액 계산 $salesBeforeSql = " SELECT SUM(COALESCE(e.ET_total, 0)) AS total_sales FROM " . $DB . ".output o LEFT JOIN " . $DB . ".output_extra e ON o.num = e.parent_num WHERE o.secondordnum = :secondordnum AND o.outdate <= :lastMonthEnd AND (o.is_deleted IS NULL OR o.is_deleted = 0) "; $paymentBeforeSql = " SELECT SUM(CAST(REPLACE(amount, ',', '') AS SIGNED)) AS total_payment FROM " . $DB . ".account WHERE secondordnum = :secondordnum AND registDate <= :lastMonthEnd AND (is_deleted IS NULL OR is_deleted = 0) AND content = '거래처 수금' "; $salesBeforeStmt = $pdo->prepare($salesBeforeSql); $salesBeforeStmt->execute([':secondordnum' => $secondordnum, ':lastMonthEnd' => $lastMonthEnd]); $salesBeforeData = $salesBeforeStmt->fetch(PDO::FETCH_ASSOC); $paymentBeforeStmt = $pdo->prepare($paymentBeforeSql); $paymentBeforeStmt->execute([':secondordnum' => $secondordnum, ':lastMonthEnd' => $lastMonthEnd]); $paymentBeforeData = $paymentBeforeStmt->fetch(PDO::FETCH_ASSOC); $initialSales = isset($salesBeforeData['total_sales']) ? (float)$salesBeforeData['total_sales'] : 0; $initialPayments = isset($paymentBeforeData['total_payment']) ? (float)$paymentBeforeData['total_payment'] : 0; // Calculate the initial balance $initialBalance = intval(round($initialSales, 2) - round($initialPayments)); // 마지막 자릿수가 1인지 확인 if (floatval($initialBalance) % 10 === 1) { // 마지막 자릿수를 제거 (정수로 처리) $initialBalance = floor($initialBalance / 10); } } catch (Exception $e) { echo "오류: " . $e->getMessage(); } ?>
   

관리대장

(거래명세서별)
회사명 : (주) 경동기업 / 담당 : 정미영 차장
~
사업자등록번호 대표자
여신한도 0 전화 (모바일: )
Email Fax
주소
적요
$row['outdate'], 'type' => 'sale', 'data' => $row ]; } // 수금부분 foreach ($payments as $payment) { $events[] = [ 'date' => $payment['registDate'], // 올바른 키 이름 사용 'type' => 'payment', 'data' => [ 'amount' => $payment['amount'] // 올바른 키 이름 사용 ] ]; } // Sort events by date usort($events, function($a, $b) { return strcmp($a['date'], $b['date']); }); // 출고예정일 첫번째 요소 저장변수 $Last_deadline = ''; foreach ($events as $event) { $event_date = $event['date']; $event_type = $event['type']; $month = date('Y/m', strtotime($event_date)); if ($event_type == 'payment') { $payment = $event['data']; $payment_date = $event_date; // 날짜는 이벤트의 날짜를 사용 $payment_amount = (float)str_replace(',', '', $payment['amount']); // payment 대신 amount 키 사용 $total_balance -= $payment_amount; $total_payment_sum += $payment_amount; // 수금 합계에 추가 if (!isset($monthly_sales[$month])) { $monthly_sales[$month] = 0; } echo ""; } if ($current_month !== $month) { if ($current_month !== '') { echo ""; } $current_month = $month; } if ($event_type == 'sale') { $row = $event['data']; $outdate = $row['outdate']; $workplacename = isset($row['workplacename']) ? $row['workplacename'] : ''; $amount = (float)$row['ET_total']; $total_balance += round($amount); $sale_count++; if (!isset($monthly_sales[$month])) { $monthly_sales[$month] = 0; } $monthly_sales[$month] += $amount; $grand_total += $amount; echo ""; // 상세내역: estimateList, estimateSlatList, etcList, screen_unapprovedList, slat_unapprovedList, motorList, bendList, controllerList, accountList $detailColumns = [ 'estimateList', 'estimateSlatList', 'etcList', 'screen_unapprovedList', 'slat_unapprovedList', 'motorList', 'bendList', 'controllerList', 'accountList' ]; foreach ($detailColumns as $col) { $list = isset($row[$col]) ? json_decode($row[$col], true) : []; if (is_array($list)) { foreach ($list as $item) { // 품목명, 수량, 단가, 금액 등 컬럼명은 데이터 구조에 따라 다를 수 있음 $itemName = isset($item['col1']) ? $item['col1'] : ''; $spec = isset($item['col2']) ? $item['col2'] : ''; $qty = isset($item['col4']) ? $item['col4'] : (isset($item['col2']) ? $item['col2'] : 1); $unit = isset($item['col5']) ? str_replace(',', '', $item['col5']) : 0; $amountDetail = floatval($qty) * floatval($unit); if ($itemName && $amountDetail > 0) { echo ""; } } } } // 비인정 금액(ET_unapproved) 표시 if (isset($row['ET_unapproved']) && floatval($row['ET_unapproved']) > 0) { echo ""; } } } if ($current_month !== '') { // 마지막 1원 정리 if (round($monthly_sales[$current_month]) % 10 === 1) { $monthly_sales[$current_month] -= 1; // 1을 빼서 마지막 자리를 0으로 만듭니다. } echo ""; } if (round($grand_total) % 10 === 1) { $grand_total -= 1; // 1을 빼서 마지막 자리를 0으로 만듭니다. } if (round($total_balance) % 10 === 1) { $total_balance -= 1; // 1을 빼서 마지막 자리를 0으로 만듭니다. } ?>
판매/수금내역
일자 적요 판매 수금 잔액
이월잔액
{$payment_date} 입금 " . number_format($payment_amount) . " " . number_format($total_balance) . "
{$current_month} 계 " . number_format($monthly_sales[$current_month]) . "
{$outdate} {$workplacename} " . number_format($amount) . " " . number_format($total_balance) . "
{$itemName} {$spec} " . number_format($amountDetail) . "
비인정 금액 -" . number_format($row['ET_unapproved']) . "
{$current_month} 계 (VAT 포함) " . number_format($monthly_sales[$current_month]) . "
건 누계 (VAT 포함)