| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace app\api\controller;
- use app\common\model\StockConfig;
- use app\api\service\StockService;
- use app\common\model\StockLog;
- use think\exception\ValidateException;
- use app\api\validate\Stock as StockValidate;
- use think\facade\Db;
- class Inventory extends Base
- {
- //品种包装箱/出入库列表
- public function stockList(StockConfig $stockConfig)
- {
- $type_id = $this->request->post('type_id', 'variety_name');
- if(!in_array($type_id, [$stockConfig::VarietyName, $stockConfig::PackingBox, $stockConfig::Material])) $this->error(__('参数错误'));
- $list = $stockConfig::where('type_id', $type_id)->field('id,title,field_name,unit')->select();
- $this->success('ok', $list);
- }
- //添加出入库
- public function stock(StockLog $stockLog, StockService $stockService)
- {
- $data = $this->request->post();
- $result = false;
- Db::startTrans();
- try {
-
- validate(StockValidate::class)->scene('add')->check($data);
-
- $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
-
- $result = $stockLog->saveAll($resData);
- Db::commit();
- }catch (ValidateException $e) {
-
- return $this->error($e->getError());
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
- //出入库记录
- public function stocklog(StockLog $stockLog)
- {
- $where = [];
- $type_id = $this->request->post('type_id/s', '');
- if(!empty($type_id)) $where['a.type_id'] = $type_id;
- $list = $stockLog::alias('a')
- ->leftjoin('stock_config b', 'a.variety_id = b.id')
- ->where('a.user_id', $this->userinfo['id'])
- ->where($where)
- ->whereTime('a.createtime', '-2 days')
- ->field('a.*,b.title')
- ->select();
- $this->success('ok', $list);
- }
- //编辑出入库
- public function stockedit(StockLog $stockLog)
- {
- $data = $this->request->post();
- $result = false;
- Db::startTrans();
- try {
-
- validate(StockValidate::class)->scene('add')->check($data);
-
- $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
-
- $result = $stockLog->saveAll($resData);
- Db::commit();
- }catch (ValidateException $e) {
-
- return $this->error($e->getError());
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
- //删除出入库
- public function stockdel(StockLog $stockLog)
- {
- $data = $this->request->post();
- $result = false;
- Db::startTrans();
- try {
-
- validate(StockValidate::class)->scene('add')->check($data);
-
- $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
-
- $result = $stockLog->saveAll($resData);
- Db::commit();
- }catch (ValidateException $e) {
-
- return $this->error($e->getError());
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
-
- }
|