| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Service;
- use App\Model\CustomerSupply;
- use App\Model\Employee;
- use App\Model\CostManagement;
- use App\Model\CostManagementDetails;
- use Illuminate\Support\Facades\DB;
- class CostManagementService extends Service
- {
- public function costManagementEdit($data,$user){
- list($status,$msg) = $this->costManagementRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = CostManagement::where('id',$data['id'])->first();
- $model->customer_supply_id = $data['customer_supply_id'] ?? 0;
- $model->order_time = $data['order_time'] ?? 0;
- $model->man_mark = $data['man_mark'] ?? '';
- $model->mark = $data['mark'] ?? '';
- $model->amount = $data['amount'] ?? 0;
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function costManagementAdd($data,$user){
- list($status,$msg) = $this->costManagementRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new CostManagement();
- $model->customer_supply_id = $data['customer_supply_id'] ?? 0;
- $model->order_time = $data['order_time'] ?? 0;
- $model->man_mark = $data['man_mark'] ?? '';
- $model->mark = $data['mark'] ?? '';
- $model->amount = $data['amount'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail($id, $time, $data){
- }
- private function getDetail($id){
- }
- public function costManagementDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- CostManagement::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function costManagementDetail($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = CostManagement::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'费用不存在或已被删除'];
- $customer = $customer->toArray();
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
- $customer['customer_supply_title'] = $customer['customer_supply_id'] ? CustomerSupply::where('id',$customer['customer_supply_id'])->value('title') : "";
- return [true, $customer];
- }
- public function costManagementCommon($data,$user, $field = []){
- if(empty($field)) $field = CostManagement::$field;
- $model = CostManagement::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['man_mark'])) $model->where('man_mark', 'LIKE', '%'.$data['man_mark'].'%');
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->where('crt_time','>=',$return[0]);
- $model->where('crt_time','<=',$return[1]);
- }
- return $model;
- }
- public function costManagementList($data,$user){
- $model = $this->costManagementCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user,$data);
- $list['count'] = $this->countTotal($list['data'], $user['header_default']);
- return [true, $list];
- }
- public function costManagementRule(&$data, $user, $is_add = true){
- if(empty($data['order_time'])) return [false, '费用日期不能为空'];
- $data['order_time'] = $this->changeDateToDate($data['order_time']);
- if(empty($data['amount'])) return [false, '总金额不能为空'];
- $res = $this->checkNumber($data['amount'],2,'positive');
- if(! $res['valid']) return [false,'总金额:' . $res['error']];
- if($is_add){
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = CostManagement::where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- if(! $bool) return [false, '费用不存在'];
- }
- return [true, ''];
- }
- public function fillData($data, $user, $search){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
- $emp_2 = (new CustomerSupplyService())->getEmployeeMap(array_unique(array_column($data['data'],'customer_supply_id')));
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['customer_supply_title'] = $emp_2[$value['customer_supply_id']] ?? '';
- }
- return $data;
- }
- }
|