DeliveryAppointmentService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityBarcode;
  4. use App\DeliveryAppointmentDetail;
  5. use App\Owner;
  6. use App\Services\common\QueryService;
  7. use App\Traits\ServiceAppAop;
  8. use App\DeliveryAppointment;
  9. use App\Warehouse;
  10. use Carbon\Carbon;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Support\Facades\Auth;
  13. use phpDocumentor\Reflection\Types\Integer;
  14. class DeliveryAppointmentService
  15. {
  16. use ServiceAppAop;
  17. protected $modelClass=DeliveryAppointment::class;
  18. /**
  19. * 搜索QUERY
  20. *
  21. * @param array $params
  22. *
  23. * @return Builder
  24. */
  25. public function query(array $params)
  26. {
  27. $owners = app("UserService")->getPermittingOwnerIds(Auth::user());
  28. return app(QueryService::class)->query($params,DeliveryAppointment::query(),[
  29. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  30. 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'],
  31. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  32. 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'],
  33. "owner_id"=>['multi' => ','],
  34. "asn_number"=>['like' => '','timeLimit' => 15],
  35. "id"=>['multi' => ','],
  36. ])->whereIn("owner_id",$owners)->with(["cars.car","details.commodity.barcodes"]);
  37. }
  38. /**
  39. * 根据吨,立方,SKU减产系数计算产能
  40. *
  41. * @param double $tonne
  42. * @param double $cubicMeter
  43. * @param int $amount
  44. * @param int $rpcc reduced_production_capacity_coefficient
  45. *
  46. * @return double
  47. */
  48. public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
  49. {
  50. $need = max(($tonne*config("appointment.production_capacity.tonne")),
  51. ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
  52. $coefficient = config("appointment.production_capacity.coefficient");
  53. if ($amount && ($need/$amount < $coefficient)){
  54. $need *= 1+(($coefficient-($need/$amount))*($rpcc/100));
  55. }
  56. return $need;
  57. }
  58. /**
  59. * 根据提供的详情数组插入详情
  60. *
  61. * @param DeliveryAppointment|object $parent
  62. * @param array $details
  63. * @param string $name
  64. * @param string $amount
  65. * @param string $barCode
  66. */
  67. public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
  68. {
  69. $codes = [];
  70. $names = [];
  71. $insert = [];//插入的元素
  72. foreach ($details as $index=>$detail){
  73. if (isset($detail["commodity_id"]) && $detail["commodity_id"]){
  74. $insert[] = [
  75. "delivery_appointment_id" => $parent->id,
  76. "commodity_id" => $detail["commodity_id"],
  77. "bar_code" => null,
  78. "name" => null,
  79. "amount" => $detail[$amount],
  80. ];
  81. unset($details[$index]);continue;
  82. }
  83. if ($detail[$barCode])$codes[] = $detail[$barCode];
  84. else $names[] = $detail[$name];
  85. }
  86. $map = [];//存储条码与ID的映射
  87. if ($codes || $names){
  88. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  89. /** @var Builder $query */
  90. $query->where("owner_id",$parent->owner_id);
  91. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  92. /** @var Builder $query */
  93. $query->whereIn("name",$names);
  94. })->get()->each(function ($commodity)use(&$map){
  95. $map[$commodity->code] = $commodity->commodity_id;
  96. $map[$commodity->commodity->name] = $commodity->commodity_id;
  97. });
  98. }
  99. foreach ($details as $detail){
  100. $code = $detail[$barCode];
  101. $insert[] = [
  102. "delivery_appointment_id" => $parent->id,
  103. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  104. "bar_code" => $code,
  105. "name" => $detail[$name],
  106. "amount" => $detail[$amount],
  107. ];
  108. }
  109. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  110. }
  111. /**
  112. * 获取一个KEY值用于验证二维码 21-4-2 切换为永久二维码
  113. *
  114. * @return string
  115. */
  116. public function getKey()
  117. {
  118. $y = 'w';//date("y");
  119. $m = 'a';//date("m");
  120. $d = 's';//date("d");
  121. $k1 = substr(env("APP_KEY"),7,2);
  122. $k2 = substr(env("APP_KEY"),10,2);
  123. $k3 = substr(env("APP_KEY"),15,2);
  124. return md5($k3.$y.$k2.$m.$k1.$d);
  125. }
  126. /**
  127. * 验证key值
  128. *
  129. * @param string $key
  130. * @return bool
  131. */
  132. public function checkKey(string $key):bool
  133. {
  134. if ($key!=$this->getKey())return false;
  135. return true;
  136. }
  137. /**
  138. * 获取当前时段
  139. *
  140. * @param int|null $hour
  141. *
  142. * @return int|bool
  143. */
  144. public function getPeriod($hour=null)
  145. {
  146. if (!$hour)$hour = date("H");
  147. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  148. $arr = explode("-",$period);
  149. if (count($arr)!=2)continue;
  150. if ($hour>=$arr[0] && $hour<$arr[1])return $key;
  151. }
  152. return false;
  153. }
  154. /**
  155. * 验证预约单完整性修改状态
  156. *
  157. * @param integer $id
  158. * @return int
  159. */
  160. public function checkFull($id)
  161. {
  162. /** @var DeliveryAppointment|\stdClass $delivery */
  163. $delivery = DeliveryAppointment::query()->withCount(["cars"=>function($query){
  164. /** @var Builder $query */
  165. $query->whereNull("delivery_time")->where("status","!=",2);
  166. }])->find($id);
  167. if ($delivery->cars_count != 0)return 0;
  168. $status = 2;
  169. if (Carbon::parse($delivery->appointment_date." ".explode("-",DeliveryAppointment::PERIOD[$delivery->date_period])[1].":00:00")->lt(Carbon::parse($delivery->car->delivery_time)))
  170. $status = 4;
  171. $delivery->update(["status"=>$status]);
  172. return $status;
  173. }
  174. /**
  175. * 获取指定时段可用产能
  176. *
  177. * @param string $date
  178. * @param int $period
  179. * @param integer $warehouseId
  180. *
  181. * @return double
  182. */
  183. public function getAvailableCapacity($date,$period,$warehouseId)
  184. {
  185. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  186. ->where("appointment_date",$date)
  187. ->where("date_period",$period)
  188. ->where("warehouse_id",$warehouseId)
  189. ->where("status",0)
  190. ->groupBy(["appointment_date","date_period"])
  191. ->lockForUpdate()->first();
  192. if (!$result)return 0;
  193. /** @var \stdClass $warehouse */
  194. $warehouse = Warehouse::query()->find($warehouseId);
  195. if (!$warehouse)return 0;
  196. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$period];
  197. return $total-$result->capacity;
  198. }
  199. /**
  200. * 检查可操作ASN
  201. *
  202. * @param string $asn
  203. * @param string $ownerCode
  204. * @return bool
  205. */
  206. public function checkOperableAsn(string $asn,string $ownerCode):bool
  207. {
  208. if (!Owner::query()->where("code",$ownerCode)->where("is_check_asn",1)->first())return true;
  209. return !!DeliveryAppointment::query()->selectRaw("1")->where("status",'!=',1)
  210. ->where("appointment_date",date("Y-m-d"))
  211. ->where("asn_number",'like',"%{$asn}%")->first();
  212. }
  213. }