BoxService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Service\Box;
  3. use App\Model\Box;
  4. use App\Model\BoxDetail;
  5. use App\Model\Header_ext;
  6. use App\Model\OrdersProduct;
  7. use App\Model\SaleOrdersProduct;
  8. use App\Service\Service;
  9. use Illuminate\Support\Facades\DB;
  10. /**
  11. * 包装相关
  12. * @package App\Models
  13. */
  14. class BoxService extends Service
  15. {
  16. protected static $instance;
  17. protected static $box_header;
  18. protected static $box_detail_header;
  19. protected static $box_hook;
  20. public $lock_key = 'box';
  21. public function __construct()
  22. {
  23. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  24. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  25. self::$box_hook = BoxHookService::getInstance();
  26. }
  27. /**
  28. * 包装
  29. * @param $data
  30. * @return array
  31. */
  32. public function boxIn($data)
  33. {
  34. if(!isset($data['param'])) return [false,'not found param'];
  35. $param = $data['param'];
  36. // $param = [
  37. // [
  38. // 'id' => 716,
  39. // 'param' => [
  40. // '1',
  41. // '1'
  42. // ],
  43. // ], [
  44. // 'id' => 723,
  45. // 'param' => [
  46. // '40',
  47. // '40'
  48. // ],
  49. // ],
  50. // ];
  51. $ids = [];
  52. $key_list = [];
  53. foreach ($param as $v) {
  54. $ids[] = $v['id'];
  55. $total = 0;
  56. foreach ($v['param'] as $vv) {
  57. $total += $vv;
  58. }
  59. $key_list[$v['id']] = [
  60. 'detail' => $v['param'],
  61. 'total' => $total,
  62. ];
  63. }
  64. try{
  65. DB::beginTransaction();
  66. $product_list = SaleOrdersProduct::wherein('id', $ids)->get()->toArray();
  67. foreach ($product_list as $v) {
  68. $key = $this->lock_key.'_'.$v['id'];
  69. $lock_status = $this->isLock($key);
  70. if(!$lock_status) return [false,'操作过于频繁'];
  71. $num_list = $key_list[$v['id']];
  72. $total = $num_list['total'];
  73. $detail = $num_list['detail'];
  74. $un_box_num = $v['order_quantity'] - $v['box_num'];
  75. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  76. $ext_1 = $v['customer_no'];
  77. $ext_2 = $v['customer_name'];
  78. $ext_3 = $v['product_no'];
  79. $ext_4 = $v['product_title'];
  80. $ext_5 = $v['product_size'];
  81. $out_order_no = $v['out_order_no'];
  82. $top_id = $v['id'];
  83. foreach ($detail as $vv){
  84. $box_insert = [
  85. 'out_order_no' => $out_order_no,
  86. 'top_id' => $top_id,
  87. 'ext_1' => $ext_1,
  88. 'ext_2' => $ext_2,
  89. 'ext_3' => $ext_3,
  90. 'ext_4' => $ext_4,
  91. 'ext_5' => $ext_5,
  92. 'num' => $vv,
  93. ];
  94. list($status,$msg) = self::$box_hook->boxInsert($box_insert);
  95. if(!$status) {
  96. $this->delLock($key);
  97. DB::rollBack();
  98. return [false,$msg];
  99. }
  100. }
  101. $this->delLock($key);
  102. SaleOrdersProduct::where('id',$top_id)->update([
  103. 'box_num' => DB::raw('box_num + '.$total),
  104. ]);
  105. }
  106. DB::commit();
  107. return [true,''];
  108. }catch (\Exception $e){
  109. DB::rollBack();
  110. return [false,$e->getLine().':'.$e->getMessage()];
  111. }
  112. }
  113. /**
  114. * 包装详情1
  115. * @param $data
  116. * @return array
  117. */
  118. public function boxDetail($data)
  119. {
  120. list($status, $data) = self::$box_hook->boxDetail($data);
  121. if (!$status) return [false, $data];
  122. return [true, $data];
  123. }
  124. public function boxProductList($data){
  125. $box = new Box();
  126. if(!isset($data['id'])) return [false,'id not found'];
  127. $model = OrdersProduct::where('del_time',0)->wherein('sale_orders_product_id',$data['id'])
  128. ->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','production_time','production_no','status','sale_orders_product_id')
  129. ->orderBy('id','desc')->get()->toArray();
  130. $data = [
  131. [
  132. 'id' => 1,
  133. 'out_order_no' => '销售订单号',
  134. 'production_time' => '下生产时间时间戳',
  135. 'customer_no' => '客户编码',
  136. 'customer_name' => '客户名称',
  137. 'product_no' => '产品编码',
  138. 'product_title' => '产品名称',
  139. 'product_size' => '产品规格',
  140. 'type' => '1生产包装2备用包装',
  141. 'is_box_num' => '已包装数量',
  142. 'un_box_num' => '未包装',
  143. 'sale_num' => '销售数量',
  144. ]
  145. ];
  146. return [true,$data];
  147. }
  148. }