/**
  * 查询出入库记录
  *
  * @param string $sdate	开始日期
  * @param string $edate	结束日期
  * @param string $goodsName	品名
  * @param string $code		条码
  * @param string $billNo	单据号
  * @param string $inoutType	出/入库
  * @param string $place		仓库
  * @param string $billType	单据类型
  * @return array
  */
 public function query($sdate = '', $edate = '', $goodsName = '', $code = '*', $billNo = '', $inoutType = '', $place = '', $billType = '', $client = '')
 {
     try {
         $dao = new StocksDao();
         $querycondition = '';
         //如果开始日期和结束日期都有则用between,否则用=
         if ($sdate != '' && $edate != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= " ( `billDate` between date('{$sdate}') and date('{$edate}') ) ";
         } else {
             if ($sdate != '') {
                 if ($querycondition != '') {
                     $querycondition .= " and ";
                 }
                 $querycondition .= " ( `billDate` = date('{$sdate}') ) ";
             } else {
                 if ($edate != '') {
                     if ($querycondition != '') {
                         $querycondition .= " and ";
                     }
                     $querycondition .= " ( `billDate` = date('{$edate}') ) ";
                 }
             }
         }
         //品名
         if ($goodsName != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= " ( `goodsName` = '{$goodsName}' ) ";
         }
         //条码
         if ($code != '*') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= " ( '{$code}' = `code` ) ";
         }
         //单据号
         if ($billNo != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= "( `billNo` = '{$billNo}') ";
         }
         //单据类型
         if ($inoutType != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= "( `inoutType` = '{$inoutType}') ";
         }
         //仓库
         if ($place != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= "( `place` = '{$place}') ";
         }
         //单据类型
         if ($billType != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= "( `billType` = '{$billType}') ";
         }
         //客户
         if ($client != '') {
             if ($querycondition != '') {
                 $querycondition .= " and ";
             }
             $querycondition .= "( `client` = '{$client}') ";
         }
         $table = StocksIndexAction::getTableNameByDate($sdate, $edate);
         if (!$table) {
             //system_out("StocksManagerAction.query error:获取表名出错,errcode:$table,
             //			sdate:$sdate,edate:$edate");
             //$this->setError("获取存储表名出错!");
             return false;
         }
         if (is_string($table)) {
             $table = array(array("tablename" => $table));
         }
         $volist = new VoList();
         //开始查询
         for ($i = 0; $i < sizeof($table); $i++) {
             $tablename = $table[$i]["tablename"];
             $result = $dao->findAll($querycondition, $tablename);
             $volist->addAll($result);
         }
         return $volist ? $volist->toResultSet() : false;
     } catch (Executive $e) {
         $this->setError("StocksManagerAction.query error:{$e},tablecondition:{$tablename}\n\t\t\t\t\t\t\tquerycondtion:{$querycondition}");
         throw new Exception($e);
     }
 }
 /**
  * 获取数据存放表名
  *
  * @param Date $date 日期
  * @return String 表名
  */
 public static function getSaveTableName($date)
 {
     try {
         $result = StocksIndexAction::getTableNameByDate($date);
         if ($result) {
             return $result;
         } else {
             //create new table
             $tablename = StocksIndexAction::createNewTable();
             //create table index
             if ($tablename) {
                 StocksIndexAction::createTableIndex($date, $tablename);
             }
             return $tablename;
         }
     } catch (Exception $e) {
         throw new Exception($e);
     }
 }
 /**
  * 确认表所放的位置
  *
  * @param $billno	单据号
  * @param $data  	数据 array("station"=>,"code"=>)
  * @return boolean
  */
 public function confirmInstation($billdata, $data)
 {
     $sysno = $billdata['sysno'];
     $billdate = $billdata['billDate'];
     //明细表
     $stocks = new StocksDao();
     $stocks->startTrans();
     //当前库存表
     $current = new CurrentStocksDao();
     $current->startTrans();
     //未确认表位
     $unconfirm = new UnconfirmstationDao();
     $unconfirm->startTrans();
     try {
         $newinsert = false;
         $station = $data["station"];
         $code = $data["code"];
         $result = $current->findAll("`code`='{$code}'")->toResultSet();
         if (sizeof($result) == 0 && NOBILL_SCAN == false) {
             $stocks->rollback();
             $current->rollback();
             $unconfirm->rollback();
             $this->setError("库存里找不到该条码[{$code}]对应的表计!");
             return false;
         } else {
             if (sizeof($result) == 0) {
                 $newinsert = true;
                 $codeinfo = new CodeInfo();
                 $codedetail = $codeinfo->getInfoByCode($code);
                 $vo = $stocks->createVo('add', '', 'id', 0, $codedetail);
                 $vo->id = 0;
                 $vo->inoutType = 'IN';
                 $vo->code = $code;
                 $vo->sysno = $billdata['sysno'];
                 $vo->placeno = $billdata['placeno'];
                 $vo->place = $billdata['place'];
                 $vo->billType = $billdata['billType'];
                 $vo->billDate = $billdata['billDate'];
                 $vo->billNo = $billdata['billNo'];
                 $vo->sendMan = $billdata['sendMan'];
                 $vo->saveMan = $billdata['saveMan'];
                 $vo->orderNo = $billdata['orderNo'];
                 $vo->client = $billdata['client'];
                 $vo->inqty = 1;
                 $vo->outqty = 0;
                 $vo->station = $station;
                 //inert new records
                 $stocksmanageraction = new StocksManagerAction();
                 $savedata["modify"] = array($vo->toArray());
                 $result = $stocksmanageraction->save($savedata);
                 if (!$result) {
                     system_out("create new recod error:" . $stocksmanageraction->getError());
                     $this->setError($stocksmanageraction->getError());
                     return false;
                 }
             }
         }
         $sql = "UPDATE " . (DB_PREFIX ? DB_PREFIX . "_" : "") . "currentstocks\n\t\t\t           SET `station`='{$station}'\n\t\t\t\t\t WHERE `code` ='{$code}'";
         system_out("update sql:" . $sql);
         $current->execute($sql);
         if (!$newinsert) {
             //返写stocks
             $result = StocksIndexAction::getTableNameByDate($billdate);
             if ($result == '') {
                 $stocks->rollback();
                 $current->rollback();
                 $unconfirm->rollback();
                 system_out("get tablename error,billdate:" . $billdate);
                 $this->setError("获取存储表出错!");
                 return false;
             }
             $tablename = $result;
             $sql = "UPDATE\t`{$tablename}`\n\t\t\t\t\t\t   SET `station` = '{$station}'\n\t\t\t\t\t\t WHERE `code` = '{$code}' and\n\t\t\t\t\t\t \t\t`sysno` = '{$sysno}' ";
             $stocks->execute($sql);
         }
         //标记已经确认的开灯记录
         $sql = "UPDATE " . (DB_PREFIX ? DB_PREFIX . "_" : "") . "unconfirmstation\n\t\t\t           SET  `confirm` = 1\n\t\t\t\t\t WHERE  `station` = '{$station}' and `state`='U'";
         $unconfirm->execute($sql);
     } catch (Exception $e) {
         $stocks->rollback();
         $current->rollback();
         $unconfirm->rollback();
         system_out("UnconfirmstationAction.confirmInstation error:{$e},\n\t\t\t\t\t\tsysno:{$sysno},data:" . print_r($data, true));
         throw new Exception($e);
     }
     //commit
     $stocks->commit();
     $current->commit();
     $unconfirm->commit();
     return true;
 }