Stock.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Scm\Lib;
  3. use Dever;
  4. class Stock
  5. {
  6. public function out_check($id, $name, $data)
  7. {
  8. $goods = Dever::param('goods', $data);
  9. if (!$goods) {
  10. $this->alert('未选择商品', $data);
  11. }
  12. $unit_id = Dever::param('unit_id', $data);
  13. if (!$unit_id) {
  14. $this->alert('未选择单位', $data);
  15. }
  16. $table = Dever::input('table');
  17. $col = Dever::input('col');
  18. list($goods_id, $sku_id) = explode('-', $goods);
  19. Dever::setInput($col, Dever::input('update_' . $col));
  20. $info = Dever::load('scm/lib/unit')->getData($goods_id, $sku_id, $unit_id);
  21. $error = false;
  22. if ($info) {
  23. $num = Dever::param('num', $data);
  24. if ($info['num'] < $num) {
  25. $error = true;
  26. }
  27. } else {
  28. $error = true;
  29. }
  30. if ($error) {
  31. $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
  32. $unit = Dever::db('scm/unit')->find($unit_id);
  33. $this->alert('下单失败!【' . $goods_info['aname'] . '】下单数量(' . $num . $unit['name'] . ')少于剩余库存(' . $info['num'] . $unit['name'] . ')', $data);
  34. }
  35. }
  36. private function alert($msg, $data)
  37. {
  38. $order_table = Dever::input('order_table');
  39. if ($order_table) {
  40. $order_id = Dever::param('order_id', $data);
  41. if ($order_id) {
  42. Dever::db($order_table)->delete($order_id);
  43. }
  44. }
  45. Dever::alert($msg);
  46. }
  47. # 更新库存
  48. public function update_commit($id, $name, $data)
  49. {
  50. $table = Dever::input('table');
  51. $type = Dever::input('type');
  52. $col = Dever::input('col');
  53. $stock = Dever::input('stock');
  54. $goods = Dever::db($table . '_goods')->find(array('order_id' => $id));
  55. if (!$goods) {
  56. Dever::db($table)->delete($id);
  57. Dever::alert('未选择商品,新增订单失败');
  58. }
  59. $info = Dever::db($table)->find($id);
  60. if ($table && $type && $info && $info['status'] == 1) {
  61. $audit = Dever::param('audit', $data);
  62. $admin = Dever::load('manage/auth.info');
  63. if ($admin) {
  64. $update['audit_admin'] = $admin['id'];
  65. }
  66. $update['status'] = $audit;
  67. $update['where_id'] = $id;
  68. Dever::db($table)->update($update);
  69. if ($audit == 2) {
  70. # 审核通过 更新库存
  71. $this->up($table, $type, $col, $stock, $info);
  72. }
  73. Dever::db($table . '_goods')->updates(array('option_order_id' => $info['id'], 'set_status' => $update['status']));
  74. }
  75. }
  76. public function up($table, $type, $col, $stock, $info)
  77. {
  78. if ($col && $stock && isset($info[$col])) {
  79. $order_goods = Dever::db($table . '_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  80. if ($order_goods) {
  81. foreach ($order_goods as $k => $v) {
  82. $result = Dever::load('scm/lib/unit')->convertBase($v['goods_id'], $v['sku_id'], $v['unit_id'], $v['num'], $v['cash']);
  83. if ($result) {
  84. $v['unit_id'] = $result['unit_id'];
  85. $v['cash'] = $result['cash'];
  86. $v['num'] = $result['num'];
  87. } else {
  88. Dever::db($table)->delete($info['id']);
  89. Dever::alert('商品不存在');
  90. }
  91. $update = array($col => $info[$col], 'clear' => true);
  92. $where = $update;
  93. $where['goods_id'] = $v['goods_id'];
  94. $where['sku_id'] = $v['sku_id'];
  95. $where['unit_id'] = $v['unit_id'];
  96. $goods = Dever::db($stock)->find($where);
  97. if (!$goods) {
  98. $update['store_goods_id'] = Dever::db($stock)->insert($where);
  99. } else {
  100. $update['store_goods_id'] = $goods['id'];
  101. }
  102. if (!$update['store_goods_id']) {
  103. continue;
  104. }
  105. $temp = explode('/', $stock);
  106. $list_table = $stock . '_list';
  107. $goods_list_id = $temp[1] . '_list_id';
  108. if ($type == 'in') {
  109. # 入库
  110. if (!$v['batch']) {
  111. $v['batch'] = date('Ymd');
  112. }
  113. $update['batch'] = $v['batch'];
  114. $update['cash'] = $v['cash'];
  115. if ($v['sdate']) {
  116. $update['sdate'] = $v['sdate'];
  117. }
  118. $goods = Dever::db($list_table)->find($update);
  119. if (!$goods) {
  120. $id = Dever::db($list_table)->insert($update);
  121. $where['where_id'] = $id;
  122. } else {
  123. $where['where_id'] = $goods['id'];
  124. }
  125. Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $where['where_id']));
  126. $where['in_num'] = $v['num'];
  127. Dever::db($list_table)->inUpdate($where);
  128. } elseif ($type == 'out') {
  129. # 出库
  130. # **** 出库形式,现在按照先入先出,之后这里可以根据商品,改成加权平均出库
  131. $goods = Dever::db($list_table)->getList($update);
  132. if ($goods) {
  133. $num = $v['num'];
  134. foreach ($goods as $k1 => $v1) {
  135. if ($v1['total'] >= $num) {
  136. Dever::db($list_table)->outUpdate(array('where_id' => $v1['id'], 'out_num' => $num));
  137. Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $v1['id']));
  138. } else {
  139. $num = $num - $v1['total'];
  140. Dever::db($list_table)->outUpdate(array('where_id' => $v1['id'], 'out_num' => $v1['total']));
  141. Dever::db($table . '_goods')->update(array('where_id' => $v['id'], $goods_list_id => $v1['id']));
  142. }
  143. }
  144. }
  145. }
  146. # 同步
  147. $list_info = Dever::db($list_table)->getOneByStore(array('store_goods_id' => $update['store_goods_id'], 'clear' => true));
  148. if ($list_info) {
  149. foreach ($list_info as $k1 => $v1) {
  150. Dever::db($stock)->update(array('where_id' => $update['store_goods_id'], 'in_num' => $v1['in_num'], 'out_num' => $v1['out_num'], 'cash' => $v1['cash']));
  151. }
  152. }
  153. //Dever::db($table . '_goods')->update(array('where_id' => $v['id'], 'status' => 2));
  154. }
  155. }
  156. }
  157. }
  158. }