Stock.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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'] = $goods_info['unit_id'];
  70. $v['cash'] = $v['base_cash'];
  71. $v['num'] = 1;
  72. }
  73. # 入库
  74. if ($type == 'in') {
  75. if (!$v['batch']) {
  76. $v['batch'] = date('Ymd');
  77. }
  78. $update['sku_id'] = $v['sku_id'];
  79. $update['batch'] = $v['batch'];
  80. $goods = Dever::db($stock . '_sku')->find($update);
  81. if (!$goods) {
  82. $update['cash'] = $v['cash'];
  83. $update['unit_id'] = $v['unit_id'];
  84. $id = Dever::db($stock . '_sku')->insert($update);
  85. $where['where_id'] = $id;
  86. } else {
  87. $where['where_id'] = $goods['id'];
  88. }
  89. }
  90. $where[$type . '_num'] = $v['num'];
  91. $method = $type . 'Update';
  92. Dever::db($stock . '_sku')->$method($where);
  93. //Dever::db($table . '_goods')->update(array('where_id' => $v['id'], 'status' => 2));
  94. }
  95. }
  96. }
  97. }
  98. }