<?php
class Warehouse_InventoryChangeController extends Ec_Controller_Action {
  public function preDispatch() {
    $this->tplDir = 'warehouse/views/inventory/';
    $this->user = new Zend_Session_Namespace('userAuthorization');
  }

  public function indexAction() {
    echo Ec::renderTpl($this->tplDir . 'inventory-change-list.tpl', 'layout');
  }

  public function listAction() {
    $customerCode   = trim($this->getRequest()->getParam('customer_code', ''));
    $productSku     = trim($this->getRequest()->getParam('product_sku', ''));
    $productBarcode = trim($this->getRequest()->getParam('product_barcode', ''));
    $size           = (int) $this->getRequest()->getParam('pageSize', 20);
    $page           = (int) $this->getRequest()->getParam('page', 1);
    $needToSearch   = TRUE;
    $condition      = array(
      'warehouse_id' => (int) $this->user->warehouseId,
      //'warehouse_id' => (int) $this->user->warehouse['warehouse_id'],
    );
    //echo '<pre>',print_r($condition,TRUE),'</pre>';exit();
    $warehouse = Common_DataCache::getWarehouse(0, $this->user->warehouseId);
    $response       = array(
      'state'   => 1,
      'message' => '没有检索到相关数据',
      'data'    => array(),
      'total'   => 0,
      'size'    => 0,
      'page'    => 0,
      'search'  => array(
        'customer_code'   => $customerCode,
        'product_sku'     => $productSku,
        'product_barcode' => $productBarcode,
        'warehouse'       => $this->user->warehouse['warehouse_code'],
      ),
    );
//echo '<pre>',print_r($response,TRUE),'</pre>';

    if ('' !== $customerCode) { // 把客户代码映射为客户ID
      $customer = Service_Customer::getByField($customerCode, 'customer_code');

      if (!empty($customer)) {
        $condition['customer_id'] = (int) $customer['customer_id'];
      }
      else {
        $needToSearch = FALSE;
      }
    }

    if ($needToSearch && '' !== $productSku) { // 把产品SKU映射为产品ID
      $product = Service_Product::getByField($productSku, 'product_sku');

      if (!empty($product)) {
        $condition['product_id'] = (int) $product['product_id'];
      }
      else {
        $needToSearch = FALSE;
      }
    }

    if ($needToSearch && '' !== $productBarcode) { // 把产品BARCODE映射为产品ID
      $product = Service_Product::getByField($productBarcode, 'product_barcode');

      if (!empty($product)) {
        if (isset($condition['product_id']) && $condition['product_id'] !== (int) $product['product_id']) {
          $needToSearch = FALSE;
        }
        else {
          $condition['product_id'] = (int) $product['product_id'];
        }
      }
      else {
        $needToSearch = FALSE;
      }
    }

    if ($needToSearch) { // 检索后取数据
      $response['total'] = Service_InventoryChangeProcess::getByConditon($condition, 'count(*)');

      if ($response['total'] > 0) {
        $size   = $size < 1 ? 20 : ($size > 1000 ? 1000 : $size);
        $page   = $page < 1 ? 1 : ($page > ceil($response['total'] / $size) ? ceil($response['total'] / $size) : $page);
        $column = array(
          'customer_code',
          'product_sku',
          'product_barcode',
          'original_shelf_code',
          'to_shelf_code',
          'original_quantity',
          'to_quantity',
          'operation_time',
          'change_reason',
        );
        $data   = Service_InventoryChangeProcess::getByConditon($condition, '*', $size, $page, 'operation_time DESC');
        $operator = array();
        foreach( $data as $key => $value ) {
            if( !isset($operator[$value['operator']]) ) {
                if( $value['operator'] <= 0 ) {
                    $operator[$value['operator']] = '';
                } else {
                    $user = Service_User::getByField( $value['operator'], 'user_id' );
                    $operator[$value['operator']] = $user ? $user['user_code'] : '';
                }
            }
            $data[$key]['operator'] = $operator[$value['operator']];
        }
        $response['message']   = '';
        $response['size']      = $size;
        $response['page']      = $page;
        $response['data']      = $data;
        $response['warehouse'] = $warehouse;
      }
    }

    echo Zend_Json::encode($response);
    exit();
  }
}
