
@{
    ViewData["Title"] = "Sku采购发货差异报表";
    Layout = "~/Pages/Shared/_MainLayout.cshtml";
    ViewBag.Nav = new string[] { "财务报表", "Sku采购发货差异报表" };
}

<div class="row">
    <div class="col-sm-12">
        <div id="leftcontain" style="position:relative;float:left;width:215px;margin-right:-220px;background-color:#fff;height:780px;border-top:solid 1px #E7EAEC;display:none;">
            <div id="ztree" class="ztree" style="margin:5px 10px;max-height: 800px;overflow: auto;">

            </div>
        </div>
        <div style="float:right;width:100%;">
            <div id="rightcontain">
                <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>&nbsp;</label>
                                <button type="button" class="btn btn-success" onclick="showleft();">分类筛选</button>
                            </div>
                            <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="warehousetype" name="warehousetype" class="form-control">
                                    <option value="">选择仓库类型</option>
                                    <option value="国内仓">国内仓</option>
                                    <option value="海外仓">海外仓</option>
                                    <option value="FBA仓">FBA仓</option>
                                    <option value="第三方仓库">第三方仓库</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>仓库：</label>
                                <select id="warehouse_code" name="warehouse_code" class="form-control" style="width:160px;">
                                    <option value="">选择仓库</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>统计时间</label>
                                <input id="start" name="start" type="text" class="form-control" style="width:130px;" value="@(ViewBag.start)" />
                                <span>至</span>
                                <input id="end" name="end" type="text" class="form-control" style="width:130px;" value="@(ViewBag.end)" />
                            </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-warning" onclick="exportlistpurchase()">导出采购下单明细</button>
                                <button type="button" class="btn btn-success" onclick="exportlistputin();">导出入库明细</button>
                                <button type="button" class="btn btn-success" onclick="exportlistshipping();">导出发货明细</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="ibox-content m-b-sm border-bottom">
                    <table id="roletable" style="table-layout:fixed;"></table>
                </div>
            </div>
        </div>
    </div>
</div>
@section css{
    <link href="~/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/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/jquery.ztree.core.min.js"></script>
    <script src="~/js/jquery.ztree.excheck.min.js"></script>
    <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));

            $('#t').val(@(ViewBag.t));

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

            listsimplecategory();

            list('@ViewBag.categoryid');
        })

        function list(cid) {
            var columns = [
                {
                    field: 'day', title: '统计日期', width: '110'
                },
                {
                    field: 'warehouse_name', title: '仓库名称', width: '200'
                },
                {
                    field: 'bailun_sku', title: 'Sku', width: '160', iscount: true, formatter: function (idx, data) {
                        return '<div class="mules" title="' + data.bailun_sku + '">' + data.bailun_sku+'</div>';
                    }
                },
                { field: 'stock_start', title: '期初库存', width: '100' },
                { field: 'stockamount_start', title: '期初库存金额', width: '130' },
                { field: 'count_purchase', title: '采购(入库)数', width: '130', sortable: true, iscount: true },
                { field: 'amount_purchase', title: '采购(入库)金额', width: '130', sortable: true, iscount: true },
                { field: 'count_sales', title: '销售(发货)数', width: '130', sortable: true, iscount: true },
                { field: 'amount_sales', title: '销售(发货)金额', width: '130', sortable: true, iscount: true },
                { field: 'diff_count', title: '差异数', width: '100', sortable: true, iscount: true },
                { field: 'diff_amount', title: '差异金额', width: '110', sortable: true, iscount: true },
                { field: 'stock_end', title: '期末库存', width: '100', sortable: true, iscount: true },
                { field: 'stockamount_end', title: '期末库存金额', width: '130', sortable: true, iscount: true }
            ];

            var s = '';

            try {
                s = getChkNode();
            } catch (e) {
                s = '';
            }

            if (cid != undefined) {
                s = cid
            }

            var url = '@Url.Content("~/Reports/Warehouse/ListDailyPurchaseSalesJson")' + '?' + $("#toolbar").serialize() + '&skucategoryids=' + s;

            if (tb == undefined) {
                tb = OnlyTable("roletable", columns, url, "", {
                    showfooter: true, loadsuccess: function (d) {
                        //替换汇总行的相关列值
                        var tr = $('.fixed-table-footer').find('tr');
                        for (var c in columns) {
                            var key = columns[c].field;
                            if (columns[c].iscount) {
                                for (var v in d.count_row) {
                                    if (key == v) {
                                        tr.find('td').eq(c).children('div').first().html(d.count_row[v]);
                                        break;
                                    }
                                }
                            }
                        }

                        companyid = d.companyid;
                        if (cid != undefined) {
                            showleft();
                        }
                    }
                }, {
                        fixedcol: true,
                        fixedcolnum:1
                    });
            }
            else {
                tb.bootstrapTable('refresh', { url: url });
            }
        }

        function listsimplecategory() {
            $.submit({
                url: '@Url.Content("~/Home/ListSimpleCategory")',
                paramData: '',
                type: 'POST',
                func: function (result) {
                    var setting = {
                        check: {
                            chkStyle: 'checkbox',
                            enable: true
                        }
                    };

                    var s = '|' + ('@ViewBag.categoryid') + '|';

                    for (var i = 0; i < result.length; i++) {
                        var obj = result[i];
                        if (s.indexOf('|'+obj.id+'|')>=0) {
                            obj.checked = true;
                            break;
                        }

                        if (obj.children.length > 0) {
                            for (var j = 0; j < obj.children.length; j++) {
                                var o = obj.children[j];
                                if (s.indexOf('|'+o.id+'|')>=0) {
                                    o.checked = true;
                                    obj.checked = true;
                                }
                            }
                        }
                    }

                    zTreeObj = $.fn.zTree.init($("#ztree"), setting, result);
                }
            })
        }

        function getChkNode() {
            var s = '';
            var o = $.fn.zTree.getZTreeObj("ztree");
            var nodes = o.getCheckedNodes();
            for (var i in nodes) {
                if (nodes[i].children == null || nodes[i].children.length <= 0) {
                    s += (nodes[i].id) + '|';
                }
            }

            return s;
        }

        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 exportlistputin() {
            if (confirm("确定要导出入库明细吗?")) {

                var s = '';

                try {
                    s = getChkNode();
                } catch (e) {
                    s = '';
                }

                window.open('@Url.Content("~/ExportFile/Putin/ExportPurchasePutin")' + '?' + $("#toolbar").serialize() + '&companyid=' + companyid + '&skucategoryids=' + s, '_blank');
                window.open('@Url.Content("~/ExportFile/Putin/ExportAllotPutin")' + '?' + $("#toolbar").serialize() + '&companyid=' + companyid + '&skucategoryids=' + s, '_blank');
            }
        }

        function exportlistshipping() {
            if (confirm("确定要导出发货明细吗?")) {
                var s = '';

                try {
                    s = getChkNode();
                } catch (e) {
                    s = '';
                }


                window.open('@Url.Content("~/ExportFile/Shipping/ListShipping")' + '?' + $("#toolbar").serialize() + '&companyid=' + companyid+'&skucategoryids=' + s, '_blank');
            }
        }

        function exportlistpurchase() {
            if (confirm('确定要导出采购下单明细吗？')) {
                var s = '';

                try {
                    s = getChkNode();
                } catch (e) {
                    s = '';
                }

                window.open('@Url.Content("~/ExportFile/Purchase/ExportPurchase")' + '?' + $("#toolbar").serialize() + '&companyid=' + companyid + '&skucategoryids=' + s, '_blank');
            }
        }

        function showleft() {
            var c = $('#leftcontain').css('display');
            if (c == 'none') {
                $('#leftcontain').css('display', 'inline');
                $("#rightcontain").animate({ marginLeft: "220px" });
            }
            else {
                $('#leftcontain').css('display', 'none');
                $("#rightcontain").animate({ marginLeft: "0px" });
            }
        }

    </script>
}




