BoxService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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['params'])) return [false,'not found params'];
  35. // $param = $data['params'];
  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 ($data as $v) {
  54. $ids[] = $v['id'];
  55. $total = 0;
  56. foreach ($v['params'] as $vv) {
  57. $total += $vv;
  58. }
  59. $key_list[$v['id']] = [
  60. 'detail' => $v['params'],
  61. 'total' => $total,
  62. ];
  63. }
  64. $insert = [];
  65. try{
  66. DB::beginTransaction();
  67. $key = $this->lock_key.'_'.$v['id'];
  68. $lock_status = $this->isLock($key);
  69. if(!$lock_status) return [false,'操作过于频繁'];
  70. $product_list = OrdersProduct::wherein('id', $ids)->get()->toArray();
  71. $box_insert = [];
  72. foreach ($product_list as $v) {
  73. $num_list = $key_list[$v['id']];
  74. $total = $num_list['total'];
  75. $detail = $num_list['detail'];
  76. $un_box_num = $v['order_quantity'] - $v['box_num'];
  77. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  78. $ext_1 = $v['customer_no'];
  79. $ext_2 = $v['customer_name'];
  80. $ext_3 = $v['product_no'];
  81. $ext_4 = $v['product_title'];
  82. $ext_5 = $v['product_size'];
  83. $out_order_no = $insert['out_order_no'] = $v['out_order_no'];
  84. $top_id = $v['sale_orders_product_id'];
  85. foreach ($detail as $vv){
  86. $box_insert[] = [
  87. 'out_order_no' => $out_order_no,
  88. 'top_id' => $top_id,
  89. 'ext_1' => $ext_1,
  90. 'ext_2' => $ext_2,
  91. 'ext_3' => $ext_3,
  92. 'ext_4' => $ext_4,
  93. 'ext_5' => $ext_5,
  94. 'num' => $vv,
  95. ];
  96. }
  97. SaleOrdersProduct::where('id',$v['sale_orders_product_id'])->update([
  98. 'box_num' => DB::raw('box_num + '.$total),
  99. ]);
  100. }
  101. $insert['detail'] = $box_insert;
  102. list($status,$msg) = self::$box_hook->boxInsert($insert);
  103. if(!$status) {
  104. $this->delLock($key);
  105. DB::rollBack();
  106. return [false,$msg];
  107. }
  108. $this->delLock($key);
  109. DB::commit();
  110. return [true,''];
  111. }catch (\Exception $e){
  112. DB::rollBack();
  113. return [false,$e->getLine().':'.$e->getMessage()];
  114. }
  115. }
  116. /**
  117. * 包装详情1
  118. * @param $data
  119. * @return array
  120. */
  121. public function boxDetail($data)
  122. {
  123. list($status, $data) = self::$box_hook->boxDetail($data);
  124. if (!$status) return [false, $data];
  125. return [true, $data];
  126. }
  127. public function boxProductList($data){
  128. if(!isset($data['id'])) return [false,'id not found'];
  129. $sale_order_ids = $data['id'];
  130. $model = OrdersProduct::where('del_time',0)->wherein('sale_orders_product_id',$sale_order_ids)
  131. ->select('id','out_order_no','customer_no','customer_name','product_no','product_title','product_size','production_time','production_no','sale_orders_product_id','dispatch_complete_quantity','box_num','technology_name','wood_name','crt_time')
  132. ->orderBy('id','desc')->get()->toArray();
  133. $sale_list = SaleOrdersProduct::wherein('id',$sale_order_ids)->select('order_quantity','id','production_quantity','box_num')->get()->toArray();
  134. $sale_key_list = [];
  135. foreach ($sale_list as $v){
  136. $sale_key_list[$v['id']] = [
  137. 'order_quantity' => $v['order_quantity'],
  138. 'production_quantity' => $v['production_quantity'],
  139. 'box_num' => $v['box_num'],
  140. ];
  141. }
  142. $return = [];
  143. $product_key_list = [];
  144. foreach ($model as $v){
  145. if(!isset($product_key_list[$v['sale_orders_product_id']])) {
  146. $p = $sale_key_list[$v['sale_orders_product_id']];
  147. $product_key_list[$v['sale_orders_product_id']] = [
  148. 'out_order_no' => $v['out_order_no'],
  149. 'production_time' => '未下生产',
  150. 'customer_no' => $v['customer_no'],
  151. 'customer_name' => $v['customer_name'],
  152. 'product_no' => $v['production_no'],
  153. 'product_title' => $v['product_title'],
  154. 'product_size' => $v['product_size'],
  155. 'id' => -$v['sale_orders_product_id'],
  156. 'type' => '2',
  157. 'is_box_num' => $p['box_num'],
  158. 'un_box_num' => $p['order_quantity'] - $p['box_num'] - $p['production_quantity'],
  159. 'sale_num' => $p['order_quantity'],
  160. ];
  161. }
  162. $return[] = [
  163. 'id' => $v['id'],
  164. 'out_order_no' => $v['out_order_no'],
  165. 'production_time' => date('Y-m-d',$v['crt_time']),
  166. 'customer_no' => $v['customer_no'],
  167. 'customer_name' => $v['customer_name'],
  168. 'product_no' => $v['production_no'],
  169. 'product_title' => $v['product_title'],
  170. 'product_size' => $v['product_size'],
  171. 'type' => '1',
  172. 'is_box_num' => $v['box_num'],
  173. 'un_box_num' => $v['dispatch_complete_quantity'] - $v['box_num'],
  174. 'sale_num' => $sale_key_list[$v['sale_orders_product_id']]['order_quantity'],
  175. ];
  176. }
  177. foreach ($product_key_list as $v){
  178. $return[] = $v;
  179. }
  180. // $data = [
  181. // [
  182. // 'id' => 1,
  183. // 'out_order_no' => '销售订单号',
  184. // 'production_time' => '下生产时间',
  185. // 'customer_no' => '客户编码',
  186. // 'customer_name' => '客户名称',
  187. // 'product_no' => '产品编码',
  188. // 'product_title' => '产品名称',
  189. // 'product_size' => '产品规格',
  190. // 'type' => '1生产包装2备用包装',
  191. // 'is_box_num' => '已包装数量',
  192. // 'un_box_num' => '未包装',
  193. // 'sale_num' => '销售数量',
  194. // ]
  195. // ];
  196. return [true,$return];
  197. }
  198. }