<?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-right {
            text-align: right;
        }

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

        .text-red {
            color: red;
        }

        .cell-right {
            float: right;
        }

        td a {
            cursor: pointer;
        }

        .info-red {
            color: lightcoral;
        }
        .info-primary{
            color: #337ab7;
        }
        .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>
            <a target="_blank" href="/webht.php/apps/trippestordersync/vendor_money/report_precheck">[&nbsp;审核账单从这里开始&nbsp;]&nbsp;预审核&raquo;</a>
            <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/index" method="POST" role="form" id="request_form" name="request_form">
            <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-3">
                    <!-- <input type="text" class="form-control" id="end_date" placeholder="结束日期"> -->
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
        <p></p>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th rowspan="2">目的地</th>
                    <th colspan="3">总营收</th>
                    <th colspan="2">总成本</th>
                    <th rowspan="2">⑤利润</th>
                    <th rowspan="2">⑥海纳利润</th>
                    <th rowspan="2">⑦地接利润</th>
                    <th rowspan="2">⑧海纳应付地接</th>
                </tr>
                <tr>
                    <th>①海纳代收</th>
                    <th>②地接代收</th>
                    <th>①+②</th>
                    <th>③海纳成本</th>
                    <th>④地接成本</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($money)) {
                    foreach ($money as $kt => $trippest) {
                        ?>
                        <tr>
                            <td class="text-left"><?php echo $trippest['vendor_name'] ?>
                                <a onclick="detail_download(<?php echo $trippest['vendor_code'] ?>);" class="cell-right">导出账单明细&nbsp;&dArr;</a>
                            </td>
                            <td><?php echo $trippest['trippest']['trippest_sum'] ?></td>
                            <td><?php echo $trippest['trippest']['vendor_sum'] ?></td>
                            <td><?php echo bcadd($trippest['trippest']['trippest_sum'], $trippest['trippest']['vendor_sum']) ?></td>
                            <td><?php echo $trippest['trippest_cost'] ?></td>
                            <td><?php echo $trippest['vendor_cost'] ?></td>
                            <td><?php echo $trippest['trippest']['total_profit'] ?></td>
                            <td><?php echo $trippest['trippest']['trippest_profit'] ?></td>
                            <td><?php echo $trippest['trippest']['vendor_profit'] ?></td>
                            <td><?php echo $trippest['trippest']['payout'] ?></td>
                        </tr>
                        <?php
                                if ($trippest['trippest']['other_sum'] > 0) {
                                    ?>
                            <tr class="bg-grey">
                                <td>非包价产品的收款 <br></td>
                                <td><?php echo $trippest['trippest']['other_sum'] ?></td>
                                <td></td>
                                <td></td>
                                <td><?php echo $trippest['trippest']['other_sum_cost'] ?></td>
                                <td></td>
                                <td colspan="4"></td>
                            </tr>
                        <?php
                                }
                                ?>
                        <?php
                                //if ($trippest['trippest']['other_vendor_sum'] > 0) {
                                    ?>
                            <!-- <tr class="bg-grey">
                                <td>已扣减的其他地接的团款 <br></td>
                                <td><?php //echo $trippest['trippest']['other_vendor_sum'] ?></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td colspan="4"></td>
                            </tr> -->
                        <?php
                                //}
                                ?>
                <?php
                    }
                } ?>
                <?php if (!empty($col_sum)) {
                    ?>
                    <tr class="text-bold">
                        <td class="text-left">合计（仅包价产品）</td>
                        <td><?php echo $col_sum['trippest']['sum_trippest_sum'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_vendor_sum'] ?></td>
                        <td><?php echo bcadd($col_sum['trippest']['sum_trippest_sum'], $col_sum['trippest']['sum_vendor_sum']); ?></td>
                        <td><?php echo $col_sum['trippest']['sum_trippest_cost'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_vendor_cost'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_profit'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_trippest_profit'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_vendor_profit'] ?></td>
                        <td><?php echo $col_sum['trippest']['sum_payout'] ?></td>
                    </tr>
                <?php } ?>
                <?php if (!empty($trippest_order_multi_city)) {
                    ?>
                    <tr class="text-bold bg-grey">
                        <th>多地预订的团号</th>
                        <th>拼团号</th>
                        <th>总报价/USD</th>
                        <th>总收款/RMB</th>
                        <th colspan="6">上表地接社接待部分的总团款</th>
                        <!-- <th colspan="5">应扣除的其他地接收款: (实时计算, 存在汇率误差)</th> -->
                    </tr>
                    <?php foreach ($trippest_order_multi_city as $ko => $order) {
                            ?>
                        <tr>
                            <td class="text-left"><?php echo $order['COLI_GroupCode'] ?></td>
                            <td class="text-left"><?php echo $order['gci_no'] ?></td>
                            <td><?php echo $order['COLI_Price'] ?></td>
                            <td><?php echo $order['money_sum'] ?></td>
                            <td colspan="6" class="text-left"><?php echo $order['sync_price_sum'] ?>
                                <br> <?php echo ($order["vendor_name"]) ?>
                            </td>
                            <!-- <td colspan="5" class="text-left"><?php // echo $order["other_vendorprice_RMB"] ?></td> -->
                        </tr>
                <?php }
                } ?>
                <?php if (!empty($trippest_order_cost_debug)) {
                    ?>
                    <tr class="text-bold">
                        <td colspan="10"></td>
                    </tr>
                    <tr class="text-bold bg-grey">
                        <th><span class="text-red">成本异常,建议重新审核:</span> 订单号</th>
                        <th>拼团成本总额</th>
                        <th>拼团人等</th>
                        <th>订单: 成人数 + 儿童数</th>
                        <th>订单成本</th>
                        <th colspan="5">财务表备注</th>
                    </tr>
                    <?php foreach ($trippest_order_cost_debug as $ko => $order) {
                            ?>
                        <tr>
                            <td class="text-left"><?php echo $order['ordernumber'] ?></td>
                            <td><?php echo $order['RPT_Total'] ?></td>
                            <td><?php echo $order['RPT_PersonGrade'] ?></td>
                            <td><?php echo $order['tourRSd'] ?> + <?php echo $order['tourRSx'] ?></td>
                            <td><?php echo $order['tourcost'] ?></td>
                            <td colspan="5" class="text-left"> <?php echo ($order["tourBZ"]) ?></td>
                        </tr>
                <?php }
                } ?>
            </tbody>
        </table>

        <p>同步数据: &nbsp; 账单批量保存后, 下表应与地接社提供的汇总表数据一致, 否则需 &nbsp;[&nbsp;第四步&nbsp;]&nbsp;&DoubleDownArrow;</p>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th rowspan="2">目的地</th>
                    <th colspan="3">总营收</th>
                    <th colspan="2">总成本</th>
                    <th rowspan="2">⑤利润</th>
                    <th rowspan="2">⑥海纳利润</th>
                    <th rowspan="2">⑦地接利润</th>
                    <th rowspan="2">⑧海纳应付地接</th>
                </tr>
                <tr>
                    <th>①海纳代收</th>
                    <th>②地接代收</th>
                    <th>①+②</th>
                    <th>③海纳成本</th>
                    <th>④地接成本</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($money)) {
                    foreach ($money as $kv => $sync) {
                        ?>
                        <tr>
                            <td class="text-left"><?php echo $sync['vendor_name'] ?></td>
                            <td><?php echo $sync['sync']['trippest_sum'] ?></td>
                            <td><?php echo $sync['sync']['vendor_sum'] ?></td>
                            <td><?php echo bcadd($sync['sync']['trippest_sum'], $sync['sync']['vendor_sum']) ?></td>
                            <td><?php echo $sync['sync']['trippest_cost'] ?></td>
                            <td><?php echo $sync['sync']['vendor_cost'] ?></td>
                            <td><?php echo $sync['sync']['total_profit'] ?></td>
                            <td><?php echo $sync['sync']['trippest_profit'] ?></td>
                            <td><?php echo $sync['sync']['vendor_profit'] ?></td>
                            <td><?php echo $sync['sync']['payout'] ?></td>
                        </tr>
                <?php }
                } ?>
                <?php if (!empty($col_sum)) {
                    ?>
                    <tr class="text-bold">
                        <td class="text-left">合计</td>
                        <td><?php echo $col_sum['sync']['sum_trippest_sum']
                                        ?></td>
                        <td><?php echo $col_sum['sync']['sum_vendor_sum'] ?></td>
                        <td><?php echo $col_sum['sync']['sum_price_sum']
                                ?></td>
                        <td></td>
                        <td><?php echo $col_sum['sync']['sum_cost_sum']
                                ?></td>
                        <td><?php echo $col_sum['sync']['sum_profit']
                                ?></td>
                        <td><?php echo $col_sum['sync']['sum_trippest_profit']
                                ?></td>
                        <td><?php echo $col_sum['sync']['sum_vendor_profit']
                                ?></td>
                        <td><?php echo $col_sum['sync']['sum_payout'] ?></td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php if (!empty($diff_cost)) { ?>
            <p>财务表成本与实际成本不一致, <span class="info-red"> &nbsp;[&nbsp;第四步&nbsp;]&nbsp;</span>检查是否需要重新生成账单:</p>
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>拼团号</th>
                        <th class="text-left">
                            <p class="text-center"> 实际总成本</p>
                        </th>
                        <th class="text-left">
                            <p class="text-center"> 自动生成的财务表所使用的成本金额</p>
                            <p>对含有多个地接社的订单，由于审核状态不一致，不会自动生成或更新财务表，避免覆盖数据导致错误， 请手动处理。检查金额正确之后, 此处可忽略该团.</p>
                            <ol class="info-primary">
                                <li> 数据为空: 虚拟订单没有团号, 无法计算. 如:
                                    <ul>
                                        <li> TA导游奖励计入成本的虚拟订单. 人工检查金额正确之后, 此处可忽略该团. </li>
                                        <li> 团号修改后无法匹配. </li>
                                        <li> HT Trippest小组不存在该产品, 可能在Day Trip中或其他小组下, Trippest中应是产品最全的. </li>
                                    </ul>
                                </li>
                                <li> 数据不为空:
                                    <ul>
                                        <li> HT自动生成账单后, 地接再次修改了成本数据, HT不会再次自动生成. 目前HT自动生成账单的时间策略是 每月初生成上上个月的团. 比如20191201自动生成201910月份的账单. </li>
                                        <li> 拼团号下的个别订单, 由于同时预订了其他产品已经提前审核账单, 账单中的图兰朵产品未更新成本数据, 对已审核的账单, 自动账单程序会跳过, 不覆盖数据. </li>
                                    </ul>
                                </li>
                            </ol>
                        </th>
                        <!-- <th class="text-left">
                            <p class="text-center"> 财务表计算总成本</p>
                            <p>计算相同拼团号下所有订单的财务表中的产品成本总和, 判断是否存在数据未根据实际成本计算.</p>
                            <ol class="info-primary">
                                <li> 与左列值不相等，且左列值正确
                                    <ul>
                                        <li> 拼团下个别订单需要重新生成账单，未采用实际成本计算并且订单已审核账单（不会自动重新生成）. </li>
                                    </ul>
                                </li>
                            </ol>
                        </th> -->
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($diff_cost as $kdc => $diff) { ?>
                        <tr>
                            <td><?php echo $diff['gci_no'] ?>&nbsp;&nbsp;
                                <?php //if($diff['to_fresh']===1) { ?>
                                    <!-- <a onclick="refresh_report('<?php echo $diff['gci_no'] ?>')" class="cell-right">重新生成账单&nbsp;&circlearrowleft;</a> -->
                                <?php //} ?>
                            </td>
                            <td><?php echo $diff['total_cost'] ?></td>
                            <td><?php echo $diff['report_total_cost0'] ?></td>
                            <!-- <td><?php // echo $diff['report_cost'] ?></td> -->
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        <?php } ?>
    </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">group number: </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() {
        $("#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) {
            var content_html='';
            for (let i = 0; i < response.data.length; i++) {
                const element = response.data[i];
                content_html += "<p>" + element.COLI_ID + " : " + element.COLI_GroupCode + "</p>";
            }
            $('#gci_coli_id').html(content_html);
            $('#gci_coli').modal('show')
        })
    }
</script>

</html>
