
@{
    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="t" name="t" class="form-control">
                            <option value="1">按销售维度</option>
                            <option value="2">按发货维度</option>
                        </select>
                    </div>
                    @*<div class="form-group">
                        <label>日期方式：</label>
                        <select id="dateWay" name="dateWay" class="form-control">
                            
                            <option value="2">按周</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>
                        <button type="button" class="btn btn-success" onclick="exportfinish();">导出成品采购明细</button>
                        <button type="button" class="btn btn-warning" onclick="exportnofinish();">导出半成品采购明细</button>
                        <button type="button" class="btn btn-danger" onclick="exportsemishippingdetail();">导出半成品消耗金额明细</button>
                        <button type="button" class="btn btn-success" onclick="exportfinishshipping();">导出成品发货明细</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="ibox-content m-b-sm border-bottom" style="display:none;">
            <table id="roletable" class="table table-hover table-bordered table-condensed table-striped">
                <thead>
                    <tr></tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <hr style="display:none;" />
        <label style="display:none;">总计</label>
        <div class="ibox-content m-b-sm border-bottom" style="display:none;">
            <table id="counttb" class="table table-hover table-bordered table-condensed table-striped">
                <thead>
                    <tr></tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <hr style="display:none;" />
        <label>子分析报表</label>
        <div class="ibox-content m-b-sm border-bottom">
            <table id="subcounttb" class="table table-hover table-bordered table-condensed table-striped">
                <thead>
                    <tr>
                        <td>外采类型</td>
                        <td>采购金额</td>
                        <td>采购数量</td>
                        <td>消耗金额</td>
                        <td>消耗数量</td>
                        <td>差异金额</td>
                        <td>差异数量</td>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
            <hr />
            <table id="subcounttb1" class="table table-hover table-bordered table-condensed table-striped">
                <thead>
                    <tr>
                        <td>生产下单类型</td>
                        <td>采购金额</td>
                        <td>采购数量</td>
                        <td>发货成本</td>
                        <td>发货数量</td>
                        <td>差异金额</td>
                        <td>差异数量</td>
                    </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/Warehouse/DailyPurchaseSalesCountJson")',
                paramData: $("#toolbar").serialize(),
                func: function (result) {
                    layer.close(lindex);
                    if (result.success) {
                        if (result.data.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.data.length; i++) {
                            var s = '<tr>';
                            var obj = result.data[i];

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

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

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

                        //counttb
                        $('#counttb').find('tbody').html('');
                        $('#counttb').find('thead').html('');
                        if (result.head.length > 0) {
                            var s = '<tr>';
                            for (var i = 0; i < result.counthead.length; i++) {
                                s += ('<th style="width:160px;">' + result.counthead[i] + '</th>');
                            }
                            s += '</tr>';

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

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

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

                            if (obj.list.length > 0) {
                                for (var j = 0; j < obj.list.length; j++) {
                                    s += ('<td style="width:160px;">' + obj.list[j] + '</td>');
                                }
                            }

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

                        //subcounttb
                        $('#subcounttb').find('tbody').html('');
                        if (result.subcount.length > 0) {
                            for (var i = 0; i < result.subcount.length; i++) {
                                var s = '<tr>';
                                var obj = result.subcount[i];

                                for (var c in obj) {
                                    s += ('<td>'+obj[c]+'</td>');
                                }

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

                        $('#subcounttb1').find('tbody').html('');
                        if (result.subcount1.length > 0) {
                            for (var i = 0; i < result.subcount1.length; i++) {
                                var s = '<tr>';
                                var obj = result.subcount1[i];

                                for (var c in obj) {
                                    s += ('<td>' + obj[c] + '</td>');
                                }

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

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

        function showdetail(categoryname, colname) {
            if (categoryname == '原料') {
                layer_show(categoryname +' '+ colname+' 的Sku明细', '@Url.Content("~/Reports/Warehouse/ListDailySemiPurchaseSales?")' + '&colname=' + colname + '&dateWay=' + $('#dateWay').val() + '&t=' + $('#t').val(), '90%', '90%');
                return false;
            }

            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>');
                        }
                    }
                }
            })
        }

        function exportfinish() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('开始日期不能为空');
                return;
            }

            if (end == '') {
                alert('结束日期不能为空');
                return;
            }

            window.open('@Url.Content("~/Reports/Purchase/ExportListPurchase?start=")' + start + '&end=' + end+'&t=0', '_blank');

        }

        function exportnofinish() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('开始日期不能为空');
                return;
            }

            if (end == '') {
                alert('结束日期不能为空');
                return;
            }

            window.open('@Url.Content("~/Reports/Purchase/ExportListPurchase?start=")' + start + '&end=' + end+'&t=1', '_blank');
        }

        function exportsemishippingdetail() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('开始日期不能为空');
                return;
            }

            if (end == '') {
                alert('结束日期不能为空');
                return;
            }

            window.open('@Url.Content("~/Reports/Warehouse/ExportSemiShippingDetail?start=")' + start + '&end=' + end, '_blank');
        }

        function exportfinishshipping() {
            var start = $('#start').val();
            var end = $('#end').val();

            if (start == '') {
                alert('开始日期不能为空');
                return;
            }

            if (end == '') {
                alert('结束日期不能为空');
                return;
            }

            window.open('@Url.Content("~/Reports/Warehouse/ExportFinishShippingDetail?start=")' + start + '&end=' + end, '_blank');
        }



    </script>
}

