BoxService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if(!isset($data['id'])) return [false,'id not found'];
  126. $sale_order_ids = $data['id'];
  127. $model = OrdersProduct::where('del_time',0)->wherein('sale_orders_product_id',$sale_order_ids)
  128. ->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')
  129. ->orderBy('id','desc')->get()->toArray();
  130. $sale_list = SaleOrdersProduct::wherein('id',$sale_order_ids)->select('order_quantity','id','production_quantity','box_num')->get()->toArray();
  131. $sale_key_list = [];
  132. foreach ($sale_list as $v){
  133. $sale_key_list[$v['id']] = [
  134. 'order_quantity' => $v['order_quantity'],
  135. 'production_quantity' => $v['production_quantity'],
  136. 'box_num' => $v['box_num'],
  137. ];
  138. }
  139. $return = [];
  140. $product_key_list = [];
  141. foreach ($model as $v){
  142. if(!isset($product_key_list[$v['sale_orders_product_id']])) {
  143. $p = $sale_key_list[$v['sale_orders_product_id']];
  144. $product_key_list[$v['sale_orders_product_id']] = [
  145. 'out_order_no' => $v['out_order_no'],
  146. 'production_time' => '未下生产',
  147. 'customer_no' => $v['customer_no'],
  148. 'customer_name' => $v['customer_name'],
  149. 'product_no' => $v['production_no'],
  150. 'product_title' => $v['product_title'],
  151. 'product_size' => $v['product_size'],
  152. 'id' => -$v['sale_orders_product_id'],
  153. 'type' => '2',
  154. 'is_box_num' => $p['box_num'],
  155. 'un_box_num' => $p['order_quantity'] - $p['box_num'] - $p['production_quantity'],
  156. 'sale_num' => $p['order_quantity'],
  157. ];
  158. }
  159. $return[] = [
  160. 'id' => $v['id'],
  161. 'out_order_no' => $v['out_order_no'],
  162. 'production_time' => date('Y-m-d',$v['crt_time']),
  163. 'customer_no' => $v['customer_no'],
  164. 'customer_name' => $v['customer_name'],
  165. 'product_no' => $v['production_no'],
  166. 'product_title' => $v['product_title'],
  167. 'product_size' => $v['product_size'],
  168. 'type' => '1',
  169. 'is_box_num' => $v['box_num'],
  170. 'un_box_num' => $v['dispatch_complete_quantity'] - $v['box_num'],
  171. 'sale_num' => $sale_key_list[$v['sale_orders_product_id']]['order_quantity'],
  172. ];
  173. }
  174. foreach ($product_key_list as $v){
  175. $return[] = $v;
  176. }
  177. // $data = [
  178. // [
  179. // 'id' => 1,
  180. // 'out_order_no' => '销售订单号',
  181. // 'production_time' => '下生产时间',
  182. // 'customer_no' => '客户编码',
  183. // 'customer_name' => '客户名称',
  184. // 'product_no' => '产品编码',
  185. // 'product_title' => '产品名称',
  186. // 'product_size' => '产品规格',
  187. // 'type' => '1生产包装2备用包装',
  188. // 'is_box_num' => '已包装数量',
  189. // 'un_box_num' => '未包装',
  190. // 'sale_num' => '销售数量',
  191. // ]
  192. // ];
  193. return [true,$return];
  194. }
  195. }