
@{
    ViewData["Title"] = "每日发生额汇总";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "财务", "每日发生额汇总" };
}

<div class="row">
    <div class="col-sm-12">
        @*<div class="alert alert-warning">
                说明：数据统计截止到昨日
            </div>*@
        <div class="ibox-content m-b-sm border-bottom">
            <form id="toolbar">
                <div class="form-inline" style="line-height:40px;">
                    <div class="form-group">
                        <label>日期方式：</label>
                        <select id="dateWay" name="dateWay" class="form-control">
                            <option value="1">按日</option>
                            <option value="2" selected>按周</option>
                            <option value="3">按月</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>统计时间</label>
                        <input id="start" name="start" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"))" />
                        <span>至</span>
                        <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="@(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"))" />
                    </div>
                    <div class="form-group">
                        <label>&nbsp;</label>
                        <button type="button" class="btn btn-primary" onclick="list();"><i class="fa fa-search"></i>&nbsp;查询</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="ibox-content m-b-sm border-bottom">
            <table id="roletable" class="table table-hover table-bordered table-condensed table-striped">
                <thead>
                    <tr></tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>

@section css{
    <link href="~/css/bootstrap-table-fixed-columns.css" rel="stylesheet" />
    <style>
        .mules {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
}
@section scripts{
    <script src="~/js/bootstrap-table-fixed-columns.js" type="text/javascript"></script>
    <script type="text/javascript">
        var tb;
        var companyid = 0;
        $(document).ready(function () {
            laydate.render({ elem: '#start' });
            laydate.render({ elem: '#end' });

            var height = document.body.clientHeight;
            $("#roletable").attr("data-height", (height - 170));

            list();

            //$('#warehousetype').change(function () {
            //    listwarehouse();
            //})
        })

        function list() {
            var lindex = layer.load(1, { shade: false }); //0代表加载的风格，支持0-2
            $.submit({
                type:'POST',
                url: '@Url.Content("~/Reports/Finance/HappenAmountCountJson")',
                paramData: $("#toolbar").serialize(),
                func: function (result) {
                    layer.close(lindex);
                    if (result.success) {
                        if (result.list.length == 0) {
                            alert('没有找到符合条件的数据');
                            return false;
                        }

                        var w = $('#roletable').parent().width()+40;

                        if (w < (160 * result.head.length + 40)) {
                            w = (160 * result.head.length + 40);
                        }

                        $('#roletable').parent().css('width', (w) + 'px');
                        $('#roletable').css('width', (w-40) + 'px');

                        $('#roletable').find('tbody').html('');
                        $('#roletable').find('thead').html('');

                        if (result.head.length > 0) {
                            var s = '<tr>';
                            for (var i = 0; i < result.head.length; i++) {
                                s += ('<th style="width:160px;">' + result.head[i] + '</th>');
                            }
                            s += '</tr>';

                            $('#roletable').find('thead').append(s);
                        }

                        for (var i = 0; i < result.list.length; i++) {
                            var s = '<tr>';
                            var obj = result.list[i];

                            s += ('<td style="width:160px;">' + obj.typename + '</td>');

                            if (obj.list.length > 0) {
                                for (var j = 0; j < obj.list.length; j++) {
                                    if (obj.type == 99) {
                                        s += ('<td style="width:160px;">' + obj.list[j] + '</td>');
                                    }
                                    else {
                                        s += ('<td style="width:160px;"><a  onclick="showdetail(' + obj.type + ',\'' + result.head[j + 1] + '\')">' + obj.list[j] + '</a></td>');
                                    }
                                }
                            }

                            s += "</tr>";
                            $('#roletable').find('tbody').append(s);
                        }

                    }
                    else {
                        layer.msg('出现异常。');
                    }
                }
            })
        }

        function showdetail(rows, categoryname) {
            var start_time;
            var end_time;

            var dateWay = $('#dateWay').val();
            if (dateWay == 2) {  //按周
                var s = categoryname.substr(0, 9);
                var arr = s.split('-');

                var now = new Date();
                start_time = new Date(now.getFullYear() + '-' + arr[0].substr(0, 2) + '-' + arr[0].substr(2, 2));
                end_time = new Date(now.getFullYear() + '-' + arr[1].substr(0, 2) + '-' + arr[1].substr(2, 2));

                if (end_time < start_time) {
                    end_time = new Date((now.getFullYear() + 1) + '-' + arr[1].substr(0, 2) + '-' + arr[1].substr(2, 2))
                }
            }
            else if (dateWay == 3) {  //按月
                var s = categoryname.substr(0, 2);
                var now = new Date();
                var day = new Date(now.getFullYear(), s, 0).getDate();

                start_time = new Date(now.getFullYear() + '-' + s + '-01');
                end_time = new Date(now.getFullYear() + '-' + s + '-' + day);
            }
            else if (dateWay == 1) {   //按日
                var s = categoryname.substr(0, 4);
                var arr = s.substr(0, 2);
                var now = new Date();

                start_time = new Date(now.getFullYear() + "-" + s.substr(0, 2) + '-' + s.substr(2, 2));
                end_time = start_time;
            }


            if (rows == 1) { //管理成本
                if (categoryname.indexOf('销售') > 0) {
                    layer_show(categoryname + ' 管理成本明细', '@Url.Content("~/Reports/Finance/ListPayAdminCost?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString() +'&type=3', '90%', '90%');
                }
                else {
                    layer_show(categoryname + ' 管理成本明细', '@Url.Content("~/Reports/Finance/ListPayAdminCostCount?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
            }
            else if (rows == 2) {  //头程物流
                if (categoryname.indexOf("付款") > 0) {
                    layer_show(categoryname + ' 头程物流付款额明细', '@Url.Content("~/Reports/Finance/ListHeadLogistic?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("发生") > 0) {
                    layer_show(categoryname + ' 头程物流发生额明细', '@Url.Content("~/Reports/Finance/ListHeadLogisticHappen?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("销售") > 0) {
                    layer_show(categoryname + ' 头程物流销售额明细', '@Url.Content("~/Reports/Finance/ListHeadLogisticSale?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
            }
            else if (rows == 3) {  //尾程物流
                if (categoryname.indexOf("付款") > 0) {
                    alert('尾程付款额已和头程付款额合并。');
                }
                else if (categoryname.indexOf("发生") > 0) {
                    layer_show(categoryname + ' 尾程物流发生额明细', '@Url.Content("~/Reports/Finance/ListTailLogisticHappen?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("销售") > 0) {
                    layer_show(categoryname + ' 尾程物流销售额明细', '@Url.Content("~/Reports/Finance/ListTailLogisticSale?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
            }
            else if (rows == 4) {  //一级供应商采购
                if (categoryname.indexOf("付款") > 0) {
                    layer_show(categoryname + ' 一级供应商付款额明细', '@Url.Content("~/Reports/Finance/ListFirstSupplierPay?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("发生") > 0) {
                    layer_show(categoryname + ' 一级供应商发生额明细', '@Url.Content("~/Reports/Finance/ListFirstSupplierHappen?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("销售") > 0) {
                    layer_show(categoryname + ' 一级供应商销售额明细', '@Url.Content("~/Reports/Finance/ListFirstSupplierSale?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
            }
            else if (rows == 5)  //二级供应商采购
            {
                if (categoryname.indexOf("付款") > 0) {
                    layer_show(categoryname + ' 二级供应商付款额明细', '@Url.Content("~/Reports/Finance/ListSecondSupplierPay?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("发生") > 0) {
                    layer_show(categoryname + ' 二级供应商发生额明细', '@Url.Content("~/Reports/Finance/ListSecondSupplierHappen?start=")' + start_time.toLocaleDateString() + '&end=' + end_time.toLocaleDateString(), '90%', '90%');
                }
                else if (categoryname.indexOf("销售") > 0) {
                    alert('二级供应商销售额已和一级供应商销售额合并。');
                }
            }
            //layer_show(categoryname +' '+ colname+' 的Sku明细', '@Url.Content("~/Reports/Warehouse/ListDailyPurchaseSales?categoryname=")' + categoryname + '&colname=' + colname + '&dateWay=' + $('#dateWay').val() + '&t=' + $('#t').val(), '90%', '90%');
        }

        function listwarehouse() {
            var type = $('#warehousetype').val();

            $.submit({
                url: '@Url.Content("~/Home/ListWarehouseFromStockAndSales")',
                paramData: 'warehousetype=' + type,
                type:'POST',
                func: function (result) {
                    if (result != null && result != undefined) {
                        $('#warehouse_code').html('<option value="">选择仓库</option>');
                        for (var i = 0; i < result.length; i++) {
                            $('#warehouse_code').append('<option value="' + result[i].warehouse_code + '">' + result[i].warehouse_name + '</option>');
                        }
                    }
                }
            })
        }

    </script>
}


