<?php

/**
 * 账单结算
 */
?>
<!DOCTYPE html>
<html>

<head>
    <title>Trippest &amp; 地接账单预审核</title>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1.0, user-scalable=no" name="viewport">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta name="referrer" content="always">
    <link href="http://www.mycht.cn/css/webht/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="http://www.mycht.cn/css/bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
    <!-- <?php // include 'flatpickr.css.php';
            ?> -->
    <style type="text/css">
        form {
            border-bottom: 1px solid #ccc;
        }

        .navbar-header h1 {
            display: inline-block;
            margin-left: 30px;
            margin-right: 30px;
        }

        label {
            display: inline-block;
            max-width: none;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-check-label {
            font-weight: normal;
            margin-left: 5px;
        }

        thead th {
            text-align: center;
            min-width: 150px;
        }

        tbody td {
            text-align: right;
        }

        .bg-grey {
            background-color: #f5f5f5;
        }

        .text-left {
            text-align: left;
        }

        .text-left-top {
            text-align: left;
            vertical-align: top !important;
        }

        .text-right {
            text-align: right;
        }

        .text-bold {
            font-weight: bold;
        }

        .text-red {
            color: red;
        }

        .info-red {
            color: lightcoral;
        }

        .info-primary {
            color: #337ab7;
        }

        .cell-right {
            float: right;
        }

        .bgred {
            background-color: lightcoral;
        }

        td a {
            cursor: pointer;
        }

        ol,
        ul {
            padding-left: 1em;
        }
        .fade.in{
            background-color: rgba(0,0,0,.5);
        }
    </style>
</head>

<body>
    <div id="wrapper" class="chkVisible print-none">
        <div class="navbar-header" style="float:none;border-bottom: 1px solid #ccc;">
            <a class="navbar-brand text-muted" style="height: 52px;padding-top: 7px;padding-left: 30px;" href="http://www.mycht.cn/webht.php/index/index">
                <img width="150" style="height:40px;" src="/css/nav/img/6000.png">
            </a>
            <h1>Trippest &amp; 地接账单预审核</h1>
            <ul class="nav navbar-nav navbar-right pull-right" style="margin:7.5px 0;">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php $userdata = $this->session->userdata('admin_chtcdn'); echo $userdata['OPI_Name']; ?> <span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="<?php echo site_url('login/logout'); ?>">退出</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <p></p>
        <form action="/webht.php/apps/trippestordersync/vendor_money/precheck" method="POST" role="form" id="request_form" name="request_form" enctype="multipart/form-data">
            <div class="form-group row">
                <label for="" class="col-md-2">地接社</label>
                <div class="">
                    <label class="form-check-label"><input type="checkbox" class="" name="vendors[]" id="" value="1343" <?php if (in_array(1343, $default_vendor)) { ?>checked<?php } ?>>北京图兰朵</label>
                    <label class="form-check-label"><input type="checkbox" class="" name="vendors[]" id="" value="29188" <?php if (in_array(29188, $default_vendor)) { ?>checked<?php } ?>>上海图兰朵</label>
                    <label class="form-check-label"><input type="checkbox" class="" name="vendors[]" id="" value="30548" <?php if (in_array(30548, $default_vendor)) { ?>checked<?php } ?>>西安图兰朵</label>
                    <!-- <label class="form-check-label"><input type="checkbox" class="" name="vendors[]" id="" value="628">桂林地接</label> -->
                </div>
            </div>
            <div class="form-group row">
                <!-- <label for="" class="col-md-2">发团日期</label> -->
                <div class="col-md-6">
                    <input type="text" class="form-control" name="date_range" id="date_range" placeholder="选择日期范围" required>
                </div>
                <div class="col-md-6">
                    <input type="file" class="form-control" name="upload_cost" id="upload_cost" placeholder="上传成本文件">
                </div>
                <div class="">
                    <!-- <input type="text" class="form-control" id="end_date" placeholder="结束日期"> -->
                    <button type="submit" class="btn btn-primary">开始预审</button>
                    <span>(&nbsp;务必上传与所选&nbsp;[&nbsp;发团日期&nbsp;,地接社&nbsp;]相对应的成本文件.&nbsp;)</span>
                </div>
            </div>
        </form>
        <?php if (!empty($no_check_cancel)) { ?>
        <p></p>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th class="text-left" colspan="3">不成行或已取消的团, 账单未审核</th>
                </tr>
                <tr>
                    <th >HT团号</th>
                    <th >拼团号</th>
                    <th >团款</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach ($no_check_cancel as $ko => $cc) { ?>
                <tr>
                    <td class="text-left"><?php echo $cc['COLI_GroupCode']; ?></td>
                    <td class="text-left"><?php echo $cc['GCI_combineNo']; ?></td>
                    <td class="text-left"><?php echo $cc['GCI_priceCNY']; ?></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php } ?>

        <?php if (!empty($agreement_diff)) { ?>
        <p></p>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th class="text-left" colspan="5">
                    <span class="info-red"> [&nbsp;第〇步&nbsp;]&nbsp;解决结算协议异常</span>
                        订单在HT中设置为综费协议结算, 不应在excel账单中, 请核实后重新设置结算协议 或 更新excel账单
                    </th>
                </tr>
                <tr>
                    <th >HT团号</th>
                    <th >报价: 综费协议</th>
                    <th >报价: 目的地协议</th>
                    <th >地接账单: 总团款(从同步数据)</th>
                    <th >HT总收款</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach ($agreement_diff as $kad => $vad) { ?>
                <tr>
                    <td class="text-left"><?php echo $vad['COLI_GroupCode']; ?></td>
                    <td >
                        <?php echo $vad['COLI_Currency']; ?>&nbsp;
                        <?php echo $vad['com_sum_price']; ?>
                        <?php if ($vad['COLI_Currency'] !== 'RMB' && $vad['com_sum_price'] != 0) {?>
                        (&asymp;&nbsp;CNY&nbsp;<?php echo $vad['com_Currency_CNY_price']; ?>)
                        <?php } ?>
                    </td>
                    <td >
                        <?php echo $vad['COLI_Currency']; ?>&nbsp;
                        <?php echo $vad['not_com_sum_price']; ?>
                        <?php if ($vad['COLI_Currency'] !== 'RMB' && $vad['not_com_sum_price'] != 0) {?>
                        (&asymp;&nbsp;CNY&nbsp;<?php echo $vad['not_com_Currency_CNY_price']; ?>)
                        <?php } ?>
                    </td>
                    <td ><?php echo $vad['in_gci']; ?></td>
                    <td ><?php echo $vad['total_income']; ?></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php } ?>

        <p></p>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th colspan="10" class="text-left">
                        团款异常: <span class="info-red"> [&nbsp;第一步&nbsp;]&nbsp;解决下表刷红的团款异常问题</span>
                    </th>
                </tr>
                <tr>
                    <th>HT团号</th>
                    <th>拼团号</th>
                    <th class="text-left-top">
                        总收入差额
                        <p>=&nbsp;财务表总收入&nbsp;-&nbsp;收款记录总额</p>
                        <ol class="info-primary">
                            <li>渠道价格不对:
                                <ul>
                                    <li>产品库价格变更导致, 或渠道收客价错误(如.未使用节庆价)</li>
                                </ul>
                            </li>
                        </ol>
                    </th>
                    <th class="text-left-top">
                        总团款差额
                        <p>=&nbsp;账单总团款&nbsp;-&nbsp;(&nbsp;HT收款&nbsp;+&nbsp;HT代收&nbsp;)</p>
                        <p class="info-primary">
                            数据来自右侧两列,&nbsp;地接代收&nbsp;或&nbsp;海纳收&nbsp;不相等 <br>
                            如右侧两列均显示红色但本列正常, 则是金额正确, 但是收款方设置错误
                        </p>
                    </th>
                    <th class="text-left-top">
                        地接代收差额
                        <p>=&nbsp;账单代收&nbsp;-&nbsp;HT代收</p>
                        <ol class="info-primary">
                            <li> &gt;0
                                <ul>
                                    <li> HT收款记录漏记了地接代收的款项, 或收款方未设置为地接 </li>
                                </ul>
                            </li>
                            <li> &lt;0
                                <ul>
                                    <li> 地接账单未记代收, 或 收款方错误 </li>
                                </ul>
                            </li>
                        </ol>
                    </th>
                    <th class="text-left-top">
                        海纳收差额
                        <p>=&nbsp;账单收款&nbsp;-&nbsp;HT收款</p>
                        <ol class="info-primary">
                            <li> &gt;0
                                <ul>
                                    <li> 地接账单的代收款记到了海纳收 </li>
                                    <li> 地接账单的未更新退款 </li>
                                </ul>
                            </li>
                            <li> &lt;0
                                <ul>
                                    <li> 地接账单收款方错误 </li>
                                    <li> 地接账单收款=0: 拆分拼团, 团款已全部记在拆分后的其他团, 本团=0 </li>
                                    <li> 地接账单未更新补充团款 </li>
                                    <li> HT未更新退款 </li>
                                </ul>
                            </li>
                        </ol>
                    </th>
                    <th>账单:总团款</th>
                    <th>账单:地接代收</th>
                    <th>HT:海纳收款</th>
                    <th>HT:地接代收</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($price_error)) {
                    $col_sum = array(
                        "sum_total_income" => 0, "sum_total_price" => 0, "sum_agency_receipt" => 0, "sum_HT_receipt" => 0
                    );
                    foreach ($price_error as $ko => $ep) {
                        $col_sum['sum_total_income'] = bcadd($col_sum['sum_total_income'],  $ep['total_income_err']);
                        $col_sum['sum_total_price'] = bcadd($col_sum['sum_total_price'],  $ep['total_price_err']);
                        $col_sum['sum_agency_receipt'] = bcadd($col_sum['sum_agency_receipt'],  $ep['agency_receipt_err']);
                        $col_sum['sum_HT_receipt'] = bcadd($col_sum['sum_HT_receipt'],  $ep['HT_receipt_err']);
                        ?>
                        <tr>
                            <td class="text-left"><?php echo $ep['GRI_Name']; ?>
                            </td>
                            <td class="text-left"><?php echo $ep['combineNo']; ?></td>
                            <td <?php echo abs($ep['total_income_err']) <= 1 ? "" : 'class=bgred'; ?>><?php echo $ep['total_income_err']; ?></td>
                            <td <?php //echo abs($ep['total_price_err']) <= 1 ? "" : 'class=bgred'; ?>>
                                <?php echo $ep['total_price_err']; ?>
                                <!-- <p><?php // echo "=" . $ep['TLD_total_price'] . "-(" . $ep['HT_haina_receipt'] . "+" . $ep['HT_agency_receipt'] . ")";
                                                ?></p> -->
                            </td>
                            <td <?php echo abs($ep['agency_receipt_err']) <= 1 ? "" : 'class=bgred'; ?>>
                                <?php if ($ep['agency_receipt_err'] > 0 &&
                                    $ep['agency_receipt_err']==$ep['TLD_agency_receipt']
                                ) {
                                    echo "<p>HT未记录地接代收: " . $ep['TLD_agency_receipt'] . "</p>";
                                    echo "<p class=\"text-left\">" . $ep["COLI_Memo"] . "</p>";
                                } else {
                                    echo $ep['agency_receipt_err'];
                                }
                                ?>
                            </td>
                            <td <?php echo abs($ep['HT_receipt_err']) <= 1 ? "" : 'class=bgred'; ?>>
                                <?php echo "<p>" . $ep['HT_receipt_err'] . "</p>"; ?>
                                <?php if ($ep['HT_receipt_err']==$ep['refund']) {
                                    echo "<p>地接账单未更新退款: " . $ep['refund'] . "</p>";
                                    echo "<p class=\"text-left\">" . $ep["COLI_Memo"] . "</p>";
                                    echo "<p class=\"text-left\">" . $ep["refund_memo"] . "</p>";
                                } elseif(abs($ep['HT_receipt_err'])==$ep['HT_haina_receipt']
                                    && mb_stripos($ep['combineNo'],'取消') > 0
                                ) {
                                    echo "<p>HT未更新退款: " . $ep['HT_receipt_err'] . "</p>";
                                } elseif ($ep['HT_receipt_err'] > 0) {
                                    echo "<p class=\"text-left\">" . $ep["refund_memo"] . "</p>";
                                } elseif ($ep['other_price'] !== NULL) {
                                    echo "<p>其他供应商接待的报价<br>或综费结算部分<br>" . $ep['COLI_Currency'] . " " . $ep['other_price'] . "</p>";
                                } elseif ($ep['vendor_order'] !== null) {
                                    // echo "<p>数据未更新, 请到HT中操作.</p>";
                                }
                                if ($ep['ht_receipt_memo']!==null) {
                                    echo "<p>收款未填写实收金额: " . $ep['ht_receipt_memo'] . "</p>";
                                }
                                ?>
                            </td>
                            <td><?php echo $ep['TLD_total_price']; ?></td>
                            <td><?php echo $ep['TLD_agency_receipt']; ?></td>
                            <td><?php echo $ep['HT_haina_receipt']; ?></td>
                            <td><?php echo $ep['HT_agency_receipt']; ?></td>
                        </tr>
                    <?php     } ?>
                    <tr class="text-bold">
                        <td colspan="2">合计</td>
                        <td><?php echo $col_sum['sum_total_income']; ?></td>
                        <td><?php echo $col_sum['sum_total_price']; ?></td>
                        <td><?php echo $col_sum['sum_agency_receipt']; ?></td>
                        <td><?php echo $col_sum['sum_HT_receipt']; ?></td>
                    </tr>
                <?php } else { ?>
                    <tr>
                        <td colspan="6" class="text-left">数据正常</td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
        <p></p>
        <?php if (!empty($not_found)) { ?>
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th colspan="2" class="text-left">未找到同步的数据,需检查是否在账单中存在</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($not_found as $kn => $vn) { ?>
                        <tr><?php echo $vn['COLI_ID']; ?></tr>
                        <tr><?php echo $vn['COLI_GroupCode']; ?></tr>
                    <?php } ?>
                </tbody>
            </table>
        <?php } ?>

        <?php if (!empty($error_msg)) { ?>
            <p><?php echo $error_msg; ?></p>
        <?php } ?>

        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th colspan="3" class="text-left">成本异常:
                        <span class="info-red">
                            [&nbsp;第二步&nbsp;]&nbsp;解决下表列出的成本异常问题,
                            完成后 &xrArr; [ &nbsp; 第三步 &nbsp;]&nbsp;在HT中批量确认并保存账单&xrArr;
                            <a href="/webht.php/apps/trippestordersync/vendor_money/index" target="_blank" rel="noopener noreferrer">[ &nbsp; 第四步 &nbsp;]&nbsp; &raquo; </a>
                        </span>
                    </th>
                </tr>
                <tr>
                    <th width='30%' >拼团号</th>
                    <th width='30%' class="text-left">
                    <p class="text-center"> HT成本</p>
                        <ol class="info-primary">
                            <li> 未找到: HT系统中未找到该团
                                <ul>
                                    <li> 未同步: 在HT&nbsp;&raquo;&nbsp;单团财务表&nbsp;&raquo;&nbsp;输入团号, 点击`同步图兰朵订单` </li>
                                    <li> 团号被修改后无法匹配 </li>
                                    <li> 拼团中有订单的预订信息在HT中设置为综费结算 </li>
                                </ul>
                            </li>
                            <li> 其他值
                                <ul>
                                    <li> 一般情况是数据未更新导致, 在单团财务表中重新生成账单即可 </li>
                                </ul>
                            </li>
                        </ol>
                    </th>
                    <th width='30%' class="text-left">
                        <p class="text-center"> 账单成本</p>
                        <ol class="info-primary">
                            <li> 未找到: 上传的excel中未找到该团
                                <ul>
                                    <li>已取消: 成本应为0 </li>
                                    <li> 不属于合作范畴: 可将该订单设为无效 </li>
                                    <li> 已在其他月份结算: 直接忽略 </li>
                                </ul>
                            </li>
                        </ol>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($cost_error)) {
                    foreach ($cost_error as $ko => $ec) {  ?>
                        <tr>
                            <td><?php echo $ec['GCI_combineNo']; ?>&nbsp;&nbsp;
                                    <!-- <a onclick="refresh_report('<?php echo $ec['GCI_combineNo'] ?>')" class="cell-right">重新生成账单&nbsp;&circlearrowleft;</a> -->
                                </td>
                            <td><?php echo $ec['cost']; ?></td>
                            <td><?php echo $ec['group_cost']; ?></td>
                        </tr>
                <?php     }
                } ?>
                <?php if (!empty($ht_no_match)) {
                    foreach ($ht_no_match as $ko => $ht) {
                        if ($ht['cost'] == 0) {
                            continue;
                        }
                        ?>
                        <tr>
                            <td><?php echo $ht['GCI_combineNo']; ?></td>
                            <td><?php echo $ht['cost']; ?></td>
                            <td>未找到</td>
                        </tr>
                <?php     }
                } ?>
                <?php if (!empty($xls_no_match)) {
                    foreach ($xls_no_match as $ko => $xls) {
                        ?>
                        <tr>
                            <td><?php echo $xls['group_name']; ?></td>
                            <td>未找到</td>
                            <td><?php echo $xls['group_cost']; ?></td>
                        </tr>
                <?php     }
                } ?>
                <?php if (empty($cost_error) && empty($ht_no_match) && empty($xls_no_match)) { ?>
                    <tr>
                        <td colspan="3" class="text-left">数据正常</td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>

    </div>

    <div class="modal fade" tabindex="-1" role="dialog" id="gci_coli">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">拼团号: <span id="gci_no"></span> </h4>
        </div>
        <div class="modal-body" id="gci_coli_id">
            <p>One fine body&hellip;</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</body>
<script src="/js/jquery.min.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script type="text/javascript">
    $(document).ready(function() {
        document.getElementById("request_form").reset();

        $("#date_range").flatpickr({
            dateFormat: 'Y-m-d',
            mode: 'range',
            allowInput: true,
            defaultDate: ['<?php echo $default_date1 ?>', '<?php echo $default_date2 ?>']
        });

    })

    function detail_download(vendor) {
        document.request_form.action = "/webht.php/apps/trippestordersync/vendor_money/index" + "/" + vendor;
        document.querySelector('#request_form').submit();
        document.request_form.action = "/webht.php/apps/trippestordersync/vendor_money/index";
    }

    function refresh_report(gci) {
        const formData = new FormData();
        formData.append('gci', gci);
        fetch('/webht.php/apps/trippestordersync/vendor_money/get_gci_coli',{
            method: 'POST',
            body: formData
        })
        .then (function (data) {
            return data.json();
        })
        .then(function (response) {
            $('#gci_no').html(gci);
            if (response.msg) {
                $('#gci_coli_id').html(response.msg);
                $('#gci_coli').modal('show')
                return false;
            }
            var content_html='';
            for (let i = 0; i < response.data.length; i++) {
                const element = response.data[i];
                content_html += "<p>" + (i+1) + ". " + element.COLI_ID + " : " + element.COLI_GroupCode + "</p>";
            }
            $('#gci_coli_id').html(content_html);
            $('#gci_coli').modal('show')
        })
    }
</script>

</html>
