123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace Scm\Lib;
- use Dever;
- class Stock
- {
- public function out_check($id, $name, $data)
- {
- $goods = Dever::param('goods', $data);
- if (!$goods) {
- $this->alert('未选择商品', $data);
- }
- $unit_id = Dever::param('unit_id', $data);
- if (!$unit_id) {
- $this->alert('未选择单位', $data);
- }
- $table = Dever::input('table');
- $col = Dever::input('col');
- list($goods_id, $sku_id) = explode('-', $goods);
-
- Dever::setInput($col, Dever::input('update_' . $col));
- $info = Dever::load('scm/lib/unit')->getData($goods_id, $sku_id, $unit_id);
- $error = false;
- if ($info) {
- $num = Dever::param('num', $data);
- if ($info['num'] < $num) {
- $error = true;
- }
- } else {
- $error = true;
- }
- if ($error) {
- $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
- $unit = Dever::db('scm/unit')->find($unit_id);
- $this->alert('下单失败!【' . $goods_info['aname'] . '】下单数量(' . $num . $unit['name'] . ')少于剩余库存(' . $info['num'] . $unit['name'] . ')', $data);
- }
- }
- private function alert($msg, $data)
- {
- $order_table = Dever::input('order_table');
- if ($order_table) {
- $order_id = Dever::param('order_id', $data);
- if ($order_id) {
- Dever::db($order_table)->delete($order_id);
- }
- }
- Dever::alert($msg);
- }
- # 更新库存
- public function update_commit($id, $name, $data)
- {
- $table = Dever::input('table');
- $type = Dever::input('type');
- $col = Dever::input('col');
- $stock = Dever::input('stock');
- $goods = Dever::db($table . '_goods')->find(array('order_id' => $id));
- if (!$goods) {
- Dever::db($table)->delete($id);
- Dever::alert('未选择商品,新增订单失败');
- }
- $info = Dever::db($table)->find($id);
- if ($table && $type && $info && $info['status'] == 1) {
- $audit = Dever::param('audit', $data);
- $admin = Dever::load('manage/auth.info');
- if ($admin) {
- $update['audit_admin'] = $admin['id'];
- }
- $update['status'] = $audit;
- $update['where_id'] = $id;
- Dever::db($table)->update($update);
- if ($audit == 2) {
- # 审核通过 更新库存
- $this->up($table, $type, $col, $stock, $info);
- }
- Dever::db($table . '_goods')->updates(array('option_order_id' => $info['id'], 'set_status' => $update['status']));
- }
- }
- public function up($table, $type, $col, $stock, $info)
- {
- if ($col && $stock && isset($info[$col])) {
- $order_goods = Dever::db($table . '_goods')->select(array('order_id' => $info['id'], 'status' => 1));
- if ($order_goods) {
- foreach ($order_goods as $k => $v) {
- $result = Dever::load('scm/lib/unit')->convertBase($v['goods_id'], $v['sku_id'], $v['unit_id'], $v['num'], $v['cash']);
- if ($result) {
- $v['unit_id'] = $result['unit_id'];
- $v['cash'] = $result['cash'];
- $v['num'] = $result['num'];
- } else {
- Dever::db($table)->delete($info['id']);
- Dever::alert('商品不存在');
- }
- $update = array($col => $info[$col], 'clear' => true);
- $where = $update;
- $where['goods_id'] = $v['goods_id'];
- $where['sku_id'] = $v['sku_id'];
- $where['unit_id'] = $v['unit_id'];
- $goods = Dever::db($stock)->find($where);
- if (!$goods) {
- $update['store_goods_id'] = Dever::db($stock)->insert($where);
- } else {
- $update['store_goods_id'] = $goods['id'];
- }
- if (!$update['store_goods_id']) {
- continue;
- }
- $temp = explode('/', $stock);
- $list_table = $stock . '_list';
- $goods_list_id = $temp[1] . '_list_id';
- if ($type == 'in') {
- # 入库
- if (!$v['batch']) {
- $v['batch'] = date('Ymd');
- }
- $update['batch'] = $v['batch'];
- $update['cash'] = $v['cash'];
- if ($v['sdate']) {
- $update['sdate'] = $v['sdate'];
- }
- $goods = Dever::db($list_table)->find($update);
- if (!$goods) {
- $id = Dever::db($list_table)->insert($update);
- $where['where_id'] = $id;
- } else {
- $where['where_id'] = $goods['id'];
- }
- Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $where['where_id']));
- $where['in_num'] = $v['num'];
- Dever::db($list_table)->inUpdate($where);
- } elseif ($type == 'out') {
- # 出库
- # **** 出库形式,现在按照先入先出,之后这里可以根据商品,改成加权平均出库
- $goods = Dever::db($list_table)->getList($update);
- if ($goods) {
- $num = $v['num'];
- foreach ($goods as $k1 => $v1) {
- if ($v1['total'] >= $num) {
- Dever::db($list_table)->outUpdate(array('where_id' => $v1['id'], 'out_num' => $num));
- Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $v1['id']));
- } else {
- $num = $num - $v1['total'];
- Dever::db($list_table)->outUpdate(array('where_id' => $v1['id'], 'out_num' => $v1['total']));
- Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $v1['id']));
- }
- }
- }
- }
- # 同步
- $list_info = Dever::db($list_table)->getOneByStore(array('store_goods_id' => $update['store_goods_id'], 'clear' => true));
- if ($list_info) {
- foreach ($list_info as $k1 => $v1) {
- Dever::db($stock)->update(array('where_id' => $update['store_goods_id'], 'in_num' => $v1['in_num'], 'out_num' => $v1['out_num'], 'cash' => $v1['cash']));
- }
- }
- //Dever::db($table . '_goods')->update(array('where_id' => $v['id'], 'status' => 2));
- }
- }
- }
- }
- }
|