123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Model;
- class OfficeReceive extends Model
- {
- public function advancedStartOfficeReceive($id,$orgId,$userId,$formJson,$extra){
- $formJson = json_decode($formJson,true);
- $data = [
- 'org_id' => $orgId,
- 'user_id' => $userId,
- 'apply_id' => $id,
- 'dep_id' => $extra['depId'],
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ];
- $items = [];
- foreach ($formJson as $k=>$v){
- if($v['componentName'] == 'ddofficefield'){
- foreach ($v['components'] as $key=>$val){
- switch ($val['idx']){
- case '0':
- $items = json_decode($val['values'],true);
- break;
- }
- }
- break;
- }
- }
- $amount = 0;
- foreach ($items as $k=>$v){
- $amount += $v['snums']*$v['price'];
- }
- $data['amount'] = $amount;
- $receiveId = Db::name('office_receive')->insertGetId($data);
- // 减商品库存
- $sitems = [];
- foreach ($items as $k=>$v){
- $sitems[] = [
- 'receive_id' => $receiveId,
- 'items_id' => $v['id'],
- 'nums' => $v['snums'],
- 'price' => $v['price']
- ];
- $ret = Db::name('office_items')->where('id',$v['id'])->setDec('last_nums',$v['snums']);
- if(!$ret){
- return false;
- }
- }
- $count = Db::name('office_receive_items')->insertAll($sitems);
- if($count != count($sitems)){
- return false;
- }
- return true;
- }
- public function advancedEndOfficeReceive($id,$orgId,$userId,$formJson){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1
- ];
- $ret = Db::name('office_receive')->where('apply_id',$id)->update($data);
- return $ret?true:false;
- }
- public function advancedDisagreeOfficeReceive($id){
- $data = [
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 2
- ];
- $ret = Db::name('office_receive')->where('apply_id',$id)->update($data);
- if(!$ret){
- return false;
- }
- // 拒绝返还库存
- $info = Db::name('office_receive')->where('apply_id',$id)->find();
- $items = Db::name('office_receive_items')->where('receive_id',$info['id'])->select();
- foreach ($items as $k=>$v){
- $ret = Db::name('office_items')->where('id',$v['items_id'])->setInc('last_nums',$v['nums']);
- if(!$ret){
- return false;
- }
- }
- return true;
- }
- public function lists($page,$size,$title,$items,$startTime,$endTime,$orgId){
- $map[] = ['or.org_id','=',$orgId];
- $map[] = ['or.status','=',1];
- if($title){
- $map[] = ['ui.name','like','%'.$title.'%'];
- }
- if($items){
- $receive_ids = Db::name('office_receive_items')
- ->alias('ori')
- ->join('office_items oi','oi.id = ori.items_id')
- ->where('oi.title','like','%'.$items.'%')
- ->where('oi.org_id',$orgId)
- ->column('ori.receive_id');
- $receive_ids = $receive_ids?array_unique($receive_ids):[];
- if($receive_ids){
- $map[] = ['or.id','in',$receive_ids];
- }
- }
- if($startTime && $endTime){
- $map[] = ['or.create_time','>=',$startTime.' 00:00:00'];
- $map[] = ['or.create_time','<=',$endTime.' 23:59:59'];
- }
- $lists = Db::name('office_receive')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.user_id')
- ->field('or.*,ui.name as user_name')
- ->where($map)
- ->page($page,$size)
- ->order('or.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- }
- $total = Db::name('office_receive')
- ->alias('or')
- ->join('user_info ui','ui.user_id = or.user_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists
- ];
- return $data;
- }
- }
|