123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Service;
- use App\Model\Orders;
- use App\Model\SaleOrdersProduct;
- use App\Model\SaleOrdersProductStockDetail;
- use Illuminate\Support\Facades\DB;
- class FyyOrderService extends Service
- {
- public function edit($data){
- return [true,'保存成功!'];
- }
- public function setOrderNo(){
- date_default_timezone_set('PRC');
- $order_no = date('Ymd') . time() . rand(1000, 9999);
- return $order_no;
- }
- public function add($data,$user){
- //获取数据
- $sqlServerModel = new FyySqlServerService();
- list($status,$return,$return_stock_detail) = $sqlServerModel->getDataFromSqlServer($data);
- if(! $status) return [false, $return];
- //数据校验
- // list($status,$msg) = $this->orderRule($return);
- // if(!$status) return [$status,$msg];
- try{
- DB::beginTransaction();
- $keys = array_unique(array_column($return,'out_order_no'));
- $map = array_fill_keys($keys, 0);
- $orders = [];
- foreach ($return as $key => $value){
- if(! empty($map[$value['out_order_no']])){
- $order_no = $map[$value['out_order_no']];
- }else{
- $order_no = $this->setOrderNo();
- $map[$value['out_order_no']] = $order_no;
- $orders[] = [
- 'order_no' => $order_no,
- 'out_order_no' => $value['out_order_no'],
- 'crt_time' => time()
- ];
- }
- $return[$key]['order_no'] = $order_no;
- $return[$key]['crt_id'] = $user['id'];
- $return[$key]['crt_time'] = time();
- }
- $args = '';
- if(! empty($return_stock_detail)){
- foreach ($return_stock_detail as $value){
- $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
- }
- $args = rtrim($args,'OR ');
- SaleOrdersProductStockDetail::where('del_time',0)
- ->whereRaw("($args)")
- ->update(['del_time' => time()]);
- }
- Orders::insert($orders);
- SaleOrdersProduct::insert($return);
- SaleOrdersProductStockDetail::insert($return_stock_detail);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getLine().':'.$e->getMessage()];
- }
- return [true,'保存成功!'];
- }
- public function del($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- Orders::whereIn('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- SaleOrdersProduct::where('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- return [true,'删除成功'];
- }
- public function orderDetail($data){
- return [200,''];
- }
- public function orderList($data){
- $model = SaleOrdersProduct::where('del_time',0)
- ->select('id','order_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','order_quantity','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','out_checker_man','out_checker_time','production_quantity','box_num','finished_num')
- ->orderBy('id','desc');
- if(! empty($data['order_no'])) $model->where('order_no', 'LIKE', '%'.$data['order_no'].'%');
- if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
- if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
- if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
- if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
- if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
- if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
- if(! empty($data['wood_name'])) $model->where('wood_name', 'LIKE', '%'.$data['wood_name'].'%');
- if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
- if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
- if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
- if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
- if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
- if(! empty($data['out_checker_time'][0]) && ! empty($data['out_checker_time'][1])) $model->whereBetween('out_checker_time',[$data['out_checker_time'][0],$data['out_checker_time'][1]]);
- if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
- if(isset($data['status'])) $model->where('status',$data['status']);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $product_no = array_filter(array_column($data['data'],'product_no'));
- $detail = SaleOrdersProductStockDetail::where('del_time',0)
- ->whereIn('product_no',$product_no)
- ->select('product_no','product_quantity_on_hand','technology_name','wood_name','warehouse_name')
- ->get()->toArray();
- $detailMap = $detail_map = [];
- foreach ($detail as $value){
- $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
- $detail_map[$keys][] = [
- 'warehouse_name' => $value['warehouse_name'],
- 'product_quantity_on_hand' => $value['product_quantity_on_hand']
- ];
- if(isset($detailMap[$keys])){
- $detailMap[$keys] += $value['product_quantity_on_hand'];
- }else{
- $detailMap[$keys] = $value['product_quantity_on_hand'];
- }
- }
- date_default_timezone_set("PRC");
- foreach ($data['data'] as $key => $value){
- $keys = $value['product_no'] . $value['technology_name'] . $value['wood_name'];
- $data['data'][$key]['sub'] = $detail_map[$keys] ?? [];
- $data['data'][$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
- $data['data'][$key]['out_checker_time'] = $value['out_checker_time'] ? date('Y-m-d',$value['out_checker_time']) : '';
- $data['data'][$key]['product_quantity_on_hand'] = $detailMap[$keys] ?? 0;
- }
- $data['order_quantity'] = array_sum(array_column($data['data'], 'order_quantity'));
- $data['finished_num'] = array_sum(array_column($data['data'], 'finished_num'));
- $data['product_quantity_on_hand'] = array_sum(array_column($data['data'], 'product_quantity_on_hand'));
- return $data;
- }
- public function orderRule($data){
- $result = Orders::where('del_time',0)
- ->whereIn('out_order_no',array_column($data,'out_order_no'))
- ->select('out_order_no')
- ->get()->toArray();
- if(! empty($result)) return [false,'查询区间内销售订单号已存在'];
- return [true,''];
- }
- public function fyyRefreshOnHandQuantity($data){
- //获取数据
- $sqlServerModel = new FyySqlServerService();
- list($status,$return,$return_product) = $sqlServerModel->getDataFromSqlServerForOnHand($data);
- if(! $status) return [false, $return];
- DB::beginTransaction();
- try {
- $args = '';
- foreach ($return_product as $value){
- $args .= "(product_no = '{$value['product_no']}' and technology_name = '{$value['technology_name']}' and wood_name = '{$value['wood_name']}') OR ";
- }
- $args = rtrim($args,'OR ');
- SaleOrdersProductStockDetail::where('del_time',0)
- ->whereRaw("($args)")
- ->update(['del_time' => time()]);
- SaleOrdersProductStockDetail::insert($return);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- public function fyySaveOutOrder($data){
- if(empty($data['order_number'])) return [false,'销售订单号不能为空!'];
- //销售出库单生成
- $sqlServerModel = new FyySqlServerService();
- list($status,$return) = $sqlServerModel->U8Rdrecord32Save($data);
- if(! $status) return [false, $return];
- return [true,''];
- }
- }
|