<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 16-3-18
 * Time: 下午5:53
 * To change this template use File | Settings | File Templates.
 */
class Warehouse_ProductInventoryLogController  extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "warehouse/views/inventory/";
        $this->serviceClass = new Service_ProductInventoryLog();
    }

    public function listAction(){
        $wh=Common_DataCache::getWarehouseSimple();
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();

            $condition = $this->serviceClass->getMatchFields($params);
            $condition['product_sku'] = $params['product_sku'];
            $condition['dateFor'] = $params['dateFor'];
            $condition['dateTo'] = $params['dateTo'];
            $condition['customer_code'] = $params['customerCode'];

            $count = $this->serviceClass->getJoinProductByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields = array(
                    'pil_id',
                    'product_id',
                    'product_barcode',
                    'warehouse_id',
                    'reference_code',
                    'pil_onway',
                    'pil_pending',
                    'pil_sellable',
                    'pil_unsellable',
                    'pil_reserved',
                    'pil_shipped',
                    'pil_add_time',
                    'product_sku',
                    'customer_code',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getJoinProductByCondition($condition, $showFields, $pageSize, $page, array('pil_add_time desc'));
                $return['data'] = $rows;
                $return['warehouse'] = $wh;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "product-inventory-log.tpl", 'layout');
    }

    public function exportAction(){
        header('Pragma:public');
        header('Content-Type:application/x-msexecl;name=productInventoryLog.xls' );
        header("Content-Disposition:inline;filename=productInventoryLog.xls");

        echo "<table border='1'>";
        echo "<tr>";
        echo "<td>产品代码</td>";
        echo "<td>产品SKU</td>";
        echo "<td>客户代码</td>";
        echo "<td>仓库</td>";
        echo "<td>在途</td>";
        echo "<td>已收货</td>";
        echo "<td>可用</td>";
        echo "<td>不良品</td>";
        echo "<td>冻结</td>";
        echo "<td>已出货</td>";
        echo "<td>时间</td>";
        echo "</tr>";

        $params = $this->_request->getParams();
        if($params['exportType'] == '1'){
            $pilIdArr = $this->_request->getParam('pilId','');
            foreach($pilIdArr as $pil_id){
                $productInventoryLog = Service_ProductInventoryLog::getByField($pil_id,"pil_id","*");
                echo "<tr>";
                echo "<td>{$productInventoryLog['product_barcode']}</td>";
                $product = Service_Product::getByField($productInventoryLog['product_id'],"product_id","*");
                echo "<td style='vnd.ms-excel.numberformat:@'>{$product['product_sku']}</td>";
                echo "<td>{$product['customer_code']}</td>";
                $warehouse = Service_Warehouse::getByField($productInventoryLog['warehouse_id'],"warehouse_id","*");
                echo "<td>{$warehouse['warehouse_code']}</td>";
                echo "<td>{$productInventoryLog['pil_onway']}</td>";
                echo "<td>{$productInventoryLog['pil_pending']}</td>";
                echo "<td>{$productInventoryLog['pil_sellable']}</td>";
                echo "<td>{$productInventoryLog['pil_unsellable']}</td>";
                echo "<td>{$productInventoryLog['pil_reserved']}</td>";
                echo "<td>{$productInventoryLog['pil_shipped']}</td>";
                echo "<td>{$productInventoryLog['pil_add_time']}</td>";
                echo "</tr>";
            }
        }else{
            $condition = $this->serviceClass->getMatchFields($params);
            $condition['product_sku'] = $params['product_sku'];
            $condition['dateFor'] = $params['dateFor'];
            $condition['dateTo'] = $params['dateTo'];
            $condition['customer_code'] = $params['customerCode'];
            $count = $this->serviceClass->getJoinProductByCondition($condition, 'count(*)');
            if($count){
                $pageSize = 20;
                $totalPage = ceil($count/$pageSize);
                for($page=1;$page<=$totalPage;$page++){
                    $showFields = array(
                        'pil_id',
                        'product_id',
                        'product_barcode',
                        'warehouse_id',
                        'reference_code',
                        'pil_onway',
                        'pil_pending',
                        'pil_sellable',
                        'pil_unsellable',
                        'pil_reserved',
                        'pil_shipped',
                        'pil_add_time',
                        'product_sku',
                        'customer_code',
                    );
                    $showFields = $this->serviceClass->getFieldsAlias($showFields);
                    $rows = $this->serviceClass->getJoinProductByCondition($condition, $showFields, $pageSize, $page, array('pil_add_time desc'));
                    foreach($rows as $key=>$value){
                        echo "<tr>";
                        echo "<td>{$value['E2']}</td>";
                        $product = Service_Product::getByField($value['E1'],'product_id',"*");
                        echo "<td style='vnd.ms-excel.numberformat:@'>{$value['product_sku']}</td>";
                        echo "<td>{$product['customer_code']}</td>";
                        $warehouse = Service_Warehouse::getByField($value['E3'],"warehouse_id","*");
                        echo "<td>{$warehouse['warehouse_code']}</td>";
                        echo "<td>{$value['E6']}</td>";
                        echo "<td>{$value['E7']}</td>";
                        echo "<td>{$value['E8']}</td>";
                        echo "<td>{$value['E9']}</td>";
                        echo "<td>{$value['E10']}</td>";
                        echo "<td>{$value['E11']}</td>";
                        echo "<td>{$value['E16']}</td>";
                        echo "</tr>";
                    }
                }
            }
        }
    }
}
