PurchaseOrderService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\BookingList;
  5. use App\Model\Depart;
  6. use App\Model\Employee;
  7. use App\Model\PaymentReceipt;
  8. use App\Model\PaymentReceiptInfo;
  9. use App\Model\PurchaseOrder;
  10. use App\Model\PurchaseOrderFile;
  11. use App\Model\PurchaseOrderInfo;
  12. use App\Model\ReturnExchangeOrder;
  13. use App\Model\SalesOrder;
  14. use App\Model\SeeRange;
  15. use App\Model\Storehouse;
  16. use App\Model\Supplier;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 采购单
  21. */
  22. class PurchaseOrderService extends Service
  23. {
  24. /**
  25. * 采购单编辑
  26. * @param $data
  27. * @param $user
  28. * @return array
  29. */
  30. public function edit($data,$user){
  31. list($status,$msg) = $this->orderRule($data,$user, false);
  32. if(!$status) return [$status,$msg];
  33. $params = $this->getDataFile($data);
  34. (new OperationLogService())->setOperationList($params,$user,2);
  35. try{
  36. DB::beginTransaction();
  37. $material_model = PurchaseOrder::where('order_number',$data['order_number'])->first();
  38. $material_model->purchase_type = $data['purchase_type'] ?? 0;
  39. $material_model->order_number = $data['order_number'];
  40. // $material_model->depart_id = $data['depart_id'] ?? 0;
  41. // $material_model->top_depart_id = $data['top_depart_id'] ?? 0;
  42. $material_model->total = $data['total'] ?? 0;
  43. $material_model->other_fee = $data['other_fee'] ?? 0;
  44. $material_model->discount_fee = $data['discount_fee'] ?? 0;
  45. $material_model->purchase_total = $data['purchase_total'] ?? 0;
  46. $material_model->supplier = $data['supplier'] ?? 0;
  47. $material_model->purchase_id = $data['purchase_id'] ?? 0;
  48. $material_model->order_type = $data['order_type'];
  49. $material_model->sales_order_id = $data['sales_order_id'] ?? 0;
  50. $material_model->mark = $data['mark'] ?? '';
  51. $material_model->storehouse_id = $data['storehouse_id'];
  52. $material_model->save();
  53. $time = time();
  54. PurchaseOrderInfo::where('order_number',$data['order_number'])
  55. ->where('del_time',0)
  56. ->update(['del_time' => time()]);
  57. if(!empty($data['product'])){
  58. $sub = [];
  59. foreach ($data['product'] as $value){
  60. $sub[] = [
  61. 'purchase_order_id' => $material_model->id,
  62. 'order_number' => $data['order_number'],
  63. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  64. 'product_id' => $value['product_id'],
  65. 'number' => $value['number'],
  66. 'basic_type_id' => $value['basic_type_id'],
  67. 'price' => $value['price'],
  68. 'mark' => $value['mark'] ?? '',
  69. 'rate' => $value['rate'] ?? 0,
  70. ];
  71. }
  72. PurchaseOrderInfo::insert($sub);
  73. }
  74. $old = PurchaseOrderFile::where('del_time',0)
  75. ->where('purchase_order_id',$data['id'])
  76. ->whereIn('type',[PurchaseOrderFile::type_one])
  77. ->select('file')
  78. ->get()->toArray();
  79. $old = array_column($old,'file');
  80. PurchaseOrderFile::where('order_number',$data['order_number'])
  81. ->where('del_time',0)
  82. ->update(['del_time' => time()]);
  83. $new = [];
  84. if(! empty($data['file'])){
  85. $insert = [];
  86. foreach ($data['file'] as $value){
  87. $insert[] = [
  88. 'purchase_order_id' => $material_model->id,
  89. 'order_number' => $data['order_number'],
  90. 'file' => $value['url'],
  91. 'type' => PurchaseOrderFile::type_one,
  92. 'name' => $value['name'],
  93. 'crt_time' => $time,
  94. ];
  95. if(in_array($value['url'], $old)) {
  96. foreach ($old as $o_k => $o_v){
  97. if($o_v == $value['url']) unset($old[$o_k]);
  98. }
  99. }else{
  100. $new[] = $value['url'];
  101. }
  102. }
  103. PurchaseOrderFile::insert($insert);
  104. }
  105. DB::commit();
  106. }catch (\Throwable $e){
  107. DB::rollBack();
  108. return [false,$e->getMessage()];
  109. }
  110. if(! empty($data['check'])) {
  111. list($status,$msg) = (new CheckService())->checkAll([
  112. "id" => $material_model->id,
  113. "order_number" => $data['order_number'],
  114. "opt_case" => CheckService::seven,
  115. "menu_id" => $data['menu_id']
  116. ],$user);
  117. // if(! $status) return [true, '保存成功,采购单确认失败,异常信息:' . $msg];
  118. }
  119. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  120. }
  121. /**
  122. * 采购单新增
  123. * @param $data
  124. * @param $user
  125. * @return array
  126. */
  127. public function add($data,$user){
  128. list($status,$msg) = $this->orderRule($data, $user);
  129. if(!$status) return [$status,$msg];
  130. try{
  131. DB::beginTransaction();
  132. $material_model = new PurchaseOrder();
  133. $material_model->order_number = $data['order_number'];
  134. $material_model->purchase_type = $data['purchase_type'] ?? 0;
  135. $material_model->depart_id = $data['depart_id'] ?? 0;
  136. $material_model->top_depart_id = $data['top_depart_id'] ?? 0;
  137. $material_model->total = $data['total'] ?? 0;
  138. $material_model->other_fee = $data['other_fee'] ?? 0;
  139. $material_model->discount_fee = $data['discount_fee'] ?? 0;
  140. $material_model->purchase_total = $data['purchase_total'] ?? 0;
  141. $material_model->supplier = $data['supplier'] ?? 0;
  142. $material_model->purchase_id = $data['purchase_id'] ?? 0;
  143. $material_model->order_type = $data['order_type'];
  144. $material_model->sales_order_id = $data['sales_order_id'] ?? 0;
  145. $material_model->mark = $data['mark'] ?? '';
  146. $material_model->crt_id = $user['id'];
  147. $material_model->storehouse_id = $data['storehouse_id'];
  148. $material_model->save();
  149. $time = time();
  150. if(!empty($data['product'])){
  151. $sub = [];
  152. foreach ($data['product'] as $value){
  153. $sub[] = [
  154. 'purchase_order_id' => $material_model->id,
  155. 'order_number' => $data['order_number'],
  156. 'storehouse_id' => $data['storehouse_id'] ?? 0,
  157. 'product_id' => $value['product_id'],
  158. 'number' => $value['number'],
  159. 'basic_type_id' => $value['basic_type_id'],
  160. 'price' => $value['price'],
  161. 'mark' => $value['mark'] ?? '',
  162. 'rate' => $value['rate'] ?? 0,
  163. ];
  164. }
  165. PurchaseOrderInfo::insert($sub);
  166. }
  167. $new = [];
  168. if(! empty($data['file'])){
  169. $insert = [];
  170. foreach ($data['file'] as $value){
  171. $insert[] = [
  172. 'purchase_order_id' => $material_model->id,
  173. 'order_number' => $data['order_number'],
  174. 'file' => $value['url'],
  175. 'type' => PurchaseOrderFile::type_one,
  176. 'name' => $value['name'],
  177. 'crt_time' => $time,
  178. ];
  179. if(! empty($value['url'])) $new[] = $value['url'];
  180. }
  181. PurchaseOrderFile::insert($insert);
  182. }
  183. DB::commit();
  184. }catch (\Throwable $exception){
  185. DB::rollBack();
  186. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  187. return [false, '网络波动,请重新操作!'];
  188. }
  189. return [false,$exception->getMessage()];
  190. }
  191. (new OperationLogService())->setOperationList($data,$user);
  192. if(! empty($data['check'])) {
  193. list($status,$msg) = (new CheckService())->checkAll([
  194. "id" => $material_model->id,
  195. "order_number" => $data['order_number'],
  196. "opt_case" => CheckService::seven,
  197. "menu_id" => $data['menu_id']
  198. ],$user);
  199. // if(! $status) return [true, '保存成功,采购单确认失败,异常信息:' . $msg];
  200. }
  201. return [true, ['file' => ['new' => $new]]];
  202. }
  203. /**
  204. * 采购单详情
  205. * @param $data
  206. * @return array
  207. */
  208. public function detail($data,$user){
  209. if($this->isEmpty($data,'order_number')) return [false,'请选择数据'];
  210. $order = PurchaseOrder::where('order_number',$data['order_number'])
  211. ->where('del_time',0)
  212. ->select('id','order_number','depart_id','mark','state','crt_id','order_type','sales_order_id','supplier','purchase_id','total','other_fee','discount_fee','purchase_total','crt_time','storehouse_id','top_depart_id','purchase_type')
  213. ->first();
  214. if(empty($order)) return [false, '采购订单不存在或已被删除'];
  215. $order = $order->toArray();
  216. $order['file'] = [];
  217. $order['purchase_type_title'] = BasicType::where('id',$order['purchase_type'])->value('title') ?? '';
  218. $order['storehouse_title'] = Storehouse::where('id',$order['storehouse_id'])->value('title');
  219. $supplier = Supplier::where('id',$order['supplier'])->first();
  220. $order['supplier_title'] = $supplier->title ?? '';
  221. $order['order_type_title'] = PurchaseOrder::$order_type[$order['order_type']] ?? '';
  222. $sales = SalesOrder::where('id',$order['sales_order_id'])->value('order_number');
  223. $order['sales_order_number'] = $sales;
  224. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$order['purchase_id']],[$order['crt_id']])))
  225. ->pluck('emp_name','id')
  226. ->toArray();
  227. $order['purchase_title'] = $emp_map[$order['purchase_id']] ?? '';
  228. $p_info = PurchaseOrderInfo::where('del_time',0)
  229. ->where('order_number',$data['order_number'])
  230. ->get()->toArray();
  231. $basic_price = BasicType::whereIn('id',array_unique(array_column($p_info,'basic_type_id')))->pluck('title','id')->toArray();
  232. $map = (new ProductService())->getProductDetail(array_column($p_info,'product_id'));
  233. foreach ($p_info as $value){
  234. $tmp = $map[$value['product_id']] ?? [];
  235. $value['title'] = $tmp['title'] ?? "";
  236. $value['code'] = $tmp['code'] ?? "";
  237. $value['size'] = $tmp['size'] ?? "";
  238. $value['unit'] = $tmp['unit'] ?? "";
  239. $value['bar_code'] = $tmp['bar_code'] ?? "";
  240. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  241. $order['product'][] = $value;
  242. }
  243. $file = PurchaseOrderFile::where('del_time',0)
  244. ->where('order_number',$data['order_number'])
  245. ->get()->toArray();
  246. $fileUploadService = new FileUploadService();
  247. foreach ($file as $value){
  248. if($value['type'] == PurchaseOrderFile::type_one){
  249. $tmp = [
  250. 'url' => $value['file'],
  251. 'name' => $value['name'],
  252. 'show_url' => $fileUploadService->getFileShow($value['file']),
  253. ];
  254. $order['file'][] = $tmp;
  255. }
  256. }
  257. $order['crt_name'] = $emp_map[$order['crt_id']] ?? '';
  258. $order['crt_time'] = $order['crt_time'] ? date("Y-m-d H:i:s",$order['crt_time']): '';
  259. $state_array = $this->getStateMake([$order]);
  260. $order['state_title'] = $this->makeState($order,$state_array);
  261. //可见范围
  262. $return = (new RangeService())->RangeDetail($order['id'],SeeRange::type_five);
  263. $order['depart'] = $return[0] ?? [];
  264. $order['employee'] = $return[1] ?? [];
  265. //对应的分社订货合同
  266. $order['sales_order_number2'] = SalesOrder::where('del_time',0)->where('contact_order_no',$data['order_number'])->value('order_number') ?? "";
  267. //退换货信息
  268. $order['return_exchange'] = (new ReturnExchangeOrderService())->getDifferentAmount($order['id'],ReturnExchangeOrder::Order_type2);
  269. $order['return_exchange_amount'] = 0;
  270. foreach ($order['return_exchange'] as $value){
  271. if($value['state'] == ReturnExchangeOrder::State_two) $order['return_exchange_amount'] += $value['difference_amount'];
  272. }
  273. //回款单信息 审核过的
  274. $order['payment_receipt_list'] = (new PaymentReceiptService())->getPaymentReceiptDataList($order,PaymentReceipt::data_type_two);
  275. //坏账金额 = 已审核退货退款金额 - 红冲金额
  276. $order['payment_receipt_list']['bad_amount'] = bcsub($order['return_exchange_amount'], $order['payment_receipt_list']['red_amount'],2);
  277. //未回款金额 = 总金额 - 已回款金额 - 坏账金额 - 红冲
  278. $order['payment_receipt_list']['not_receipt_amount'] = bcsub(bcsub(bcsub($order['purchase_total'], $order['payment_receipt_list']['receipt_amount'],2), $order['payment_receipt_list']['bad_amount'], 2),$order['payment_receipt_list']['red_amount'],2);
  279. //回款单信息 审核过的
  280. $order['not_payment_receipt_amount'] = bcsub($order['purchase_total'],$order['payment_receipt_list']['not_confirm_receipt_amount'],2);
  281. return [true, $order];
  282. }
  283. /**
  284. * 采购单删除
  285. * @param $data
  286. * @return array
  287. */
  288. public function del($data){
  289. if($this->isEmpty($data,'order_number')) return [false,'请选择数据!'];
  290. $order = PurchaseOrder::where('del_time',0)->where('order_number',$data['order_number'])->first();
  291. if(empty($order)) return [false,'采购单不存在或已被删除'];
  292. $order = $order->toArray();
  293. if($order['state'] > PurchaseOrder::STATE_ZERO) return [false,'请确认采购订单状态,删除失败!'];
  294. $bool = PaymentReceiptInfo::where('del_time',0)
  295. ->where('type',PaymentReceiptInfo::type_three)
  296. ->where('data_order_no', $order['order_number'])
  297. ->exists();
  298. if($bool) return [false, '采购已存在收付款单记录,请先删除收付款单据!'];
  299. try{
  300. DB::beginTransaction();
  301. PurchaseOrder::where('order_number',$data['order_number'])->where('del_time',0)->update([
  302. 'del_time'=>time()
  303. ]);
  304. PurchaseOrderInfo::where('order_number',$data['order_number'])->where('del_time',0)->update([
  305. 'del_time'=>time()
  306. ]);
  307. $old = PurchaseOrderFile::where('del_time',0)
  308. ->where('purchase_order_id',$order['id'])
  309. ->whereIn('type',[PurchaseOrderFile::type_one])
  310. ->select('file')
  311. ->get()->toArray();
  312. $old = array_column($old,'file');
  313. PurchaseOrderFile::where('order_number',$data['order_number'])
  314. ->where('del_time',0)
  315. ->update(['del_time' => time()]);
  316. (new RangeService())->RangeDelete($order['id'],SeeRange::type_five);
  317. DB::commit();
  318. }catch (\Throwable $e){
  319. DB::rollBack();
  320. return [false,$e->getMessage()];
  321. }
  322. return [true, ['file' => ['old' => $old]]];
  323. }
  324. /**
  325. * 获取采购单列表
  326. * @param $data
  327. * @param $user
  328. * @return array
  329. */
  330. public function getList($data,$user){
  331. $model = PurchaseOrder::Clear($user,$data);
  332. $model = $model->where('del_time',0)->select('order_number','id','depart_id','top_depart_id','mark','crt_time','state','crt_id','order_type','sales_order_id','supplier','purchase_id','purchase_total','discount_fee','other_fee','total','storehouse_id','invoice_state')
  333. ->orderby('id', 'desc');
  334. if(! empty($data['purchase_id'])) $model->where('id', $data['purchase_id']);
  335. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  336. if(! empty($data['depart_id'])) $model->where('depart_id', $data['depart_id']);
  337. if(! empty($data['company_id'])) $model->where('company_id', $data['company_id']);
  338. if(isset($data['state'])) $model->where('state',$data['state']);
  339. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  340. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  341. $model->whereBetween('crt_time',[$return[0],$return[1]]);
  342. }
  343. if(! empty($data['time_type'])) {
  344. if($data['time_type'] == 1) {
  345. $start = strtotime('today');
  346. $end = strtotime('tomorrow') - 1;
  347. }elseif ($data['time_type'] == 2){
  348. $start = strtotime('this week',strtotime('today'));
  349. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  350. }
  351. if(! empty($start) && ! empty($end)) {
  352. $model->where('crt_time','>=',$start);
  353. $model->where('crt_time','<=',$end);
  354. }
  355. }
  356. if(! empty($data['order_type'])) {
  357. if($data['order_type'] == PurchaseOrder::Order_type_five){
  358. $model->whereIn('order_type',[PurchaseOrder::Order_type_two,PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four]);
  359. }else{
  360. $model->where('order_type',$data['order_type']);
  361. }
  362. }
  363. if(! empty($data['sale_order'])){
  364. $model2 = SalesOrder::Clear($user,$data);
  365. $sale = $model2->where('del_time',0)
  366. ->where('order_number', 'LIKE', '%'.$data['sale_order'].'%')
  367. ->select('id')
  368. ->get()->toArray();
  369. $model->whereIn('sales_order_id',array_unique(array_column($sale,'id')));
  370. }
  371. if(! empty($data['supplier_man'])){
  372. $emp = Supplier::where('del_time',0)
  373. ->where('title', 'LIKE', '%'.$data['supplier_man'].'%')
  374. ->select('id')
  375. ->get()->toArray();
  376. $model->whereIn('supplier',array_unique(array_column($emp,'id')));
  377. }
  378. if(! empty($data['purchase_man'])){
  379. $emp = Employee::where('del_time',0)
  380. ->where('emp_name', 'LIKE', '%'.$data['purchase_man'].'%')
  381. ->select('id')
  382. ->get()->toArray();
  383. $model->whereIn('purchase_id',array_unique(array_column($emp,'id')));
  384. }
  385. if(! empty($data['get_for_purchase'])){
  386. $model->whereIn('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  387. ->where('state',">=",PurchaseOrder::STATE_TWO);
  388. }
  389. $list = $this->limit($model,'',$data);
  390. $list = $this->fillListData($list,$data,$user);
  391. return [true, $list];
  392. }
  393. /**
  394. * 拼接数据
  395. * @param $data
  396. * @return array
  397. */
  398. public function fillListData($data,$ergs,$user){
  399. if(empty($data['data'])) return $data;
  400. $depart_id = array_unique(array_column($data['data'],'depart_id'));
  401. $depart_map = Depart::whereIn('id',$depart_id)
  402. ->pluck('title','id')
  403. ->toArray();
  404. $emp_id = array_unique(array_merge_recursive(array_column($data['data'],'crt_id'),array_column($data['data'],'purchase_id')));
  405. $emp_map = Employee::whereIn('id',$emp_id)
  406. ->pluck('emp_name','id')
  407. ->toArray();
  408. $sales = SalesOrder::whereIn('id',array_unique(array_column($data['data'],'sales_order_id')))->pluck('order_number','id')->toArray();
  409. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  410. ->pluck('title','id')
  411. ->toArray();
  412. $supplier = Supplier::whereIn('id',array_unique(array_column($data['data'],'supplier')))
  413. ->select('title','id','is_main')
  414. ->get()
  415. ->toArray();
  416. $supplier_map = array_column($supplier,null,'id');
  417. $product = [];
  418. if(! empty($ergs['product'])){
  419. $sales_p_info = PurchaseOrderInfo::where('del_time',0)
  420. ->whereIn('purchase_order_id',array_column($data['data'],'id'))
  421. ->get()->toArray();
  422. $basic_price = BasicType::whereIn('id',array_unique(array_column($sales_p_info,'basic_type_id')))->pluck('title','id')->toArray();
  423. $map = (new ProductService())->getProductDetail(array_column($sales_p_info,'product_id'));
  424. foreach ($sales_p_info as $value){
  425. $tmp = $map[$value['product_id']] ?? [];
  426. $value['title'] = $tmp['title'] ?? "";
  427. $value['code'] = $tmp['code'] ?? "";
  428. $value['size'] = $tmp['size'] ?? "";
  429. $value['unit'] = $tmp['unit'] ?? "";
  430. $value['bar_code'] = $tmp['bar_code'] ?? "";
  431. $value['basic_type_title'] = $basic_price[$value['basic_type_id']] ?? "";
  432. $product[$value['purchase_order_id']][] = $value;
  433. }
  434. }
  435. //分社订货合同
  436. $sales_array = [];
  437. if(! empty($ergs['get_for_purchase'])) $sales_array = $this->fsdhOrder($data);
  438. list($for_amount_list, $for_amount_list1) = (new PaymentReceiptService())->getPaymentReceiptDataCountList(array_column($data['data'],'order_number'));
  439. list($getDifferentAmountALL,$getDifferentAmountALL1) = (new ReturnExchangeOrderService())->getDifferentAmountALLNew(array_column($data['data'],'id'),1);
  440. //获取发货信息
  441. // $invoice_map = (new InvoiceOrderService())->getInvoiceData($data['data']);
  442. //订单状态数据组织
  443. $state_array = $this->getStateMake($data['data']);
  444. foreach ($data['data'] as $key => $value){
  445. $supplier_tmp = $supplier_map[$value['supplier']] ?? [];
  446. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
  447. $data['data'][$key]['depart_name'] = $depart_map[$value['depart_id']] ?? '';
  448. $data['data'][$key]['crt_name'] = $emp_map[$value['crt_id']] ?? '';
  449. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  450. $data['data'][$key]['purchase_name'] = $emp_map[$value['purchase_id']] ?? '';
  451. $data['data'][$key]['order_type_title'] = PurchaseOrder::$order_type[$value['order_type']] ?? '';
  452. $data['data'][$key]['sales_order_number'] = $sales[$value['sales_order_id']] ?? '';
  453. $data['data'][$key]['fsdh_id'] = $sales_array[$value['order_number']] ?? 0;
  454. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  455. $data['data'][$key]['supplier_title'] = $supplier_tmp['title'] ?? '';
  456. $data['data'][$key]['product'] = $product[$value['id']] ?? [];
  457. //发货信息 分社向总社采购
  458. // $invoice_state = "未发货";
  459. // if(! empty($invoice_map[$value['order_number']])) $invoice_state = implode(',',$invoice_map[$value['order_number']]);
  460. if(in_array($value['order_type'], [PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])){
  461. $invoice_state = PurchaseOrder::$invoice_name[$value['invoice_state']] ?? "";
  462. $data['data'][$key]['state_message'] = "订单状态:" . $data['data'][$key]['state_title'] . "\n"."是否发货:" . $invoice_state;
  463. }else{
  464. $data['data'][$key]['state_message'] = "订单状态:" . $data['data'][$key]['state_title'];
  465. }
  466. //用于计算--------
  467. //已收(审核后)
  468. $one1 = $for_amount_list1[$value['order_number'] . PaymentReceipt::type_one] ?? 0;
  469. $data['data'][$key]['checked_payment_receipt_amount'] = $one1;
  470. //已收
  471. $one = $for_amount_list[$value['order_number'] . PaymentReceipt::type_one] ?? 0;
  472. $data['data'][$key]['payment_receipt_amount'] = $one;
  473. //已红冲
  474. $three = $for_amount_list[$value['order_number'] . PaymentReceipt::type_three] ?? 0;
  475. $data['data'][$key]['red_amount'] = $three;
  476. //已退货退款
  477. $return_exchange_amount = $getDifferentAmountALL[$value['id']] ?? 0;
  478. $data['data'][$key]['return_exchange_amount'] = $return_exchange_amount;
  479. //用于计算--------
  480. //用于统计--------
  481. $red = $for_amount_list1[$value['order_number'] . PaymentReceipt::type_three] ?? 0;
  482. $return_exchange_amount1 = $getDifferentAmountALL1[$value['id']] ?? 0;
  483. $bad = bcsub($return_exchange_amount1 ,$red,2);
  484. //已收金额
  485. $fee1 = bcsub($one1 ,$red,2);
  486. $data['data'][$key]['fee1'] = $fee1;
  487. //未收金额
  488. $fee2 = bcsub(bcsub(bcsub($value['purchase_total'], $fee1,2), $bad, 2),$red,2);
  489. $data['data'][$key]['fee2'] = $fee2;
  490. //红冲金额
  491. $data['data'][$key]['fee3'] = $red;
  492. //坏账金额
  493. $data['data'][$key]['fee4'] = $bad;
  494. //用于统计--------
  495. }
  496. return $data;
  497. }
  498. public function fsdhOrder($data){
  499. if(empty($data['data'])) return [];
  500. $order_no = [];
  501. foreach ($data['data'] as $value){
  502. if(in_array($value['order_type'] , [PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])) $order_no[] = $value['order_number'];
  503. }
  504. if(empty($order_no)) return [];
  505. $sale_order = SalesOrder::where('del_time',0)
  506. ->whereIn('contact_order_no', $order_no)
  507. ->pluck('id','contact_order_no')
  508. ->toArray();
  509. return $sale_order;
  510. }
  511. public function getStateMake($data){
  512. if(empty($data)) return [];
  513. $order_no = [];
  514. foreach ($data as $value){
  515. if(! in_array($value['state'], [PurchaseOrder::State_minus_one,PurchaseOrder::STATE_ONE])) continue;
  516. $order_no[] = $value['order_number'];
  517. }
  518. return (new OaService())->getOaTeamDetailList($order_no);
  519. }
  520. public function makeState($value, $state_array){
  521. if(! empty($state_array[$value['order_number']])){
  522. $return = $state_array[$value['order_number']];
  523. if($value['state'] == PurchaseOrder::State_minus_one){
  524. $state = "驳回:" . $return;
  525. }else{
  526. $state = "待" . $return . "审核";
  527. }
  528. }elseif($value['state'] == PurchaseOrder::STATE_ZERO){
  529. $state = "待" . $value['crt_name'] . "提交";
  530. }else{
  531. $state = PurchaseOrder::$name[$value['state']] ?? '';
  532. }
  533. return $state;
  534. }
  535. /**
  536. * 采购单参数规则
  537. * @param $data
  538. * @param $is_check
  539. * @return array
  540. */
  541. public function orderRule(&$data, $user, $is_check = true){
  542. if(empty($data['order_type'])) return [false,'采购订单类型不能为空'];
  543. if($data['order_type'] == PurchaseOrder::Order_type_two && ! empty($data['supplier'])) {
  544. $supplier = Supplier::where('id',$data['supplier'])->first();
  545. if(! empty($supplier->is_main)) return [false,PurchaseOrder::$order_type[PurchaseOrder::Order_type_two] . '供应商请选择非总社供应商'];
  546. }
  547. if($data['order_type'] == PurchaseOrder::Order_type_three || $data['order_type'] == PurchaseOrder::Order_type_four) {
  548. if(empty($data['supplier'])){
  549. return [false,'请选择总部供应商'];
  550. }else{
  551. $supplier = Supplier::where('id',$data['supplier'])->first();
  552. if(empty($supplier->is_main)) return [false,'请选择总社供应商'];
  553. }
  554. }
  555. if($data['order_type'] == PurchaseOrder::Order_type_three && ! empty($data['sales_order_id'])) return [false,PurchaseOrder::$order_type[PurchaseOrder::Order_type_three] . '不需要选择合同'];
  556. if($data['order_type'] == PurchaseOrder::Order_type_four && empty($data['sales_order_id'])) return [false, PurchaseOrder::$order_type[PurchaseOrder::Order_type_four] . '必须选择合同'];
  557. if($this->isEmpty($data,'product')) return [false,'产品不能为空'];
  558. if(empty($data['storehouse_id'])) return [false,'请选择仓库'];
  559. if(empty($data['menu_id'])) return [false, '菜单信息不能为空'];
  560. if(! empty($data['other_fee'])){
  561. $res = $this->checkNumber($data['other_fee']);
  562. if(! $res) return [false, '其它费用请输入不超过两位小数并且大于0的数值'];
  563. }
  564. if(! empty($data['discount_fee'])){
  565. $res = $this->checkNumber($data['discount_fee']);
  566. if(! $res) return [false, '优惠金额请输入不超过两位小数并且大于0的数值'];
  567. }
  568. if(! empty($data['total'])){
  569. $res = $this->checkNumber($data['total']);
  570. if(! $res) return [false, '产品总价请输入不超过两位小数并且大于0的数值'];
  571. }
  572. if(! empty($data['purchase_total'])){
  573. $res = $this->checkNumber($data['purchase_total']);
  574. if(! $res) return [false, '采购总价请输入不超过两位小数并且大于0的数值'];
  575. }
  576. foreach ($data['product'] as $value){
  577. if(empty($value['number'])) return [false,'产品数量不能为空'];
  578. $res = $this->checkNumber($value['number']);
  579. if(! $res) return [false,'请输入正确的产品数量'];
  580. }
  581. //所属部门 以及 顶级部门
  582. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  583. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  584. if($is_check){
  585. $order_number = (new OrderNoService())->createOrderNumber(PurchaseOrder::prefix);
  586. if(empty($order_number)) return [false,'采购单号生成失败!'];
  587. $data['order_number'] = $order_number;
  588. }else{
  589. if($this->isEmpty($data,'order_number')) return [false,'采购单号不能为空!'];
  590. $purchase = PurchaseOrder::where('del_time',0)->where('order_number',$data['order_number'])->first();
  591. if(empty($purchase)) return [false, '采购单不存在或已被删除'];
  592. if($purchase->state > PurchaseOrder::STATE_ZERO) return [false,'请确认采购单单据状态,修改失败'];
  593. }
  594. return [true, ''];
  595. }
  596. }