Stock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Scm\Lib;
  3. use Dever;
  4. class Stock
  5. {
  6. public function out_check($id, $name, $data)
  7. {
  8. $table = Dever::input('table');
  9. $col = Dever::input('col');
  10. $goods = Dever::param('goods', $data);
  11. list($goods_id, $sku_id) = explode('-', $goods);
  12. $where['goods_id'] = $goods_id;
  13. $where['sku_id'] = $sku_id;
  14. $where[$col] = Dever::input('update_' . $col);
  15. $info = Dever::db($table)->getOne($where);
  16. if ($info) {
  17. $num = Dever::param('num', $data);
  18. if ($info['total'] < $num) {
  19. $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
  20. return Dever::alert('【' . $goods_info['aname'] . '】剩余库存:' . $info['total'] . $goods_info['unit'] . ',已不足出库');
  21. }
  22. } else {
  23. $goods_info = Dever::load('scm_product/lib/info')->getBaseInfo($goods_id, $sku_id);
  24. return Dever::alert('【' . $goods_info['aname'] . '】剩余库存:0' . $goods_info['unit'] . ',已不足出库');
  25. }
  26. }
  27. # 更新库存
  28. public function update_commit($id, $name, $data)
  29. {
  30. $table = Dever::input('table');
  31. $type = Dever::input('type');
  32. $col = Dever::input('col');
  33. $stock = Dever::input('stock');
  34. $goods = Dever::db($table . '_goods')->find(array('order_id' => $id));
  35. if (!$goods) {
  36. Dever::db($table)->delete($id);
  37. Dever::alert('未选择商品,新增订单失败');
  38. }
  39. $info = Dever::db($table)->find($id);
  40. if ($table && $type && $info && $info['status'] == 1) {
  41. $audit = Dever::param('audit', $data);
  42. $admin = Dever::load('manage/auth.info');
  43. if ($admin) {
  44. $update['audit_admin'] = $admin['id'];
  45. }
  46. $update['status'] = $audit;
  47. $update['where_id'] = $id;
  48. Dever::db($table)->update($update);
  49. if ($audit == 2) {
  50. # 审核通过 更新库存
  51. $this->up($table, $type, $col, $stock, $info);
  52. }
  53. Dever::db($table . '_goods')->updates(array('option_order_id' => $info['id'], 'set_status' => $update['status']));
  54. }
  55. }
  56. public function up($table, $type, $col, $stock, $info)
  57. {
  58. if ($col && $stock && isset($info[$col])) {
  59. $order_goods = Dever::db($table . '_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  60. if ($order_goods) {
  61. foreach ($order_goods as $k => $v) {
  62. $update = array('goods_id' => $v['goods_id'], $col => $info[$col]);
  63. $goods = Dever::db($stock)->find($update);
  64. if (!$goods) {
  65. Dever::db($stock)->insert($update);
  66. }
  67. $result = Dever::load('scm/lib/price')->getByUnit($v['goods_id'], $v['sku_id'], $v['unit_id'], $v['num'], 'cost_price', Dever::input('supplier_id'));
  68. if ($result) {
  69. $v['unit_id'] = $result['base_unit_id'];
  70. $v['cash'] = $result['base_cash'];
  71. $v['num'] = $result['base_num'];
  72. } else {
  73. Dever::db($table)->delete($info['id']);
  74. Dever::alert('商品不存在');
  75. }
  76. # 入库
  77. if ($type == 'in') {
  78. if (!$v['batch']) {
  79. $v['batch'] = date('Ymd');
  80. }
  81. $update['sku_id'] = $v['sku_id'];
  82. $update['batch'] = $v['batch'];
  83. if ($v['sdate']) {
  84. $update['sdate'] = $v['sdate'];
  85. }
  86. $goods = Dever::db($stock . '_sku')->find($update);
  87. if (!$goods) {
  88. $update['cash'] = $v['cash'];
  89. $update['unit_id'] = $v['unit_id'];
  90. $id = Dever::db($stock . '_sku')->insert($update);
  91. $where['where_id'] = $id;
  92. } else {
  93. $where['where_id'] = $goods['id'];
  94. }
  95. }
  96. $where[$type . '_num'] = $v['num'];
  97. $method = $type . 'Update';
  98. Dever::db($stock . '_sku')->$method($where);
  99. //Dever::db($table . '_goods')->update(array('where_id' => $v['id'], 'status' => 2));
  100. }
  101. }
  102. }
  103. }
  104. }