whereHas("cars",function ($query)use($params){ $query->where("appointment_number",'like',$params["appointment_number"]."%"); }); unset($params["appointment_number"]); } return app(QueryService::class)->query($params,$query,[ 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'], 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'], 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'], 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'], "owner_id"=>['multi' => ','], "asn_number"=>['like' => '','timeLimit' => 15], "id"=>['multi' => ','], ])->whereIn("owner_id",app("OwnerService")->getQuery())->with(["cars.car","details.commodity.barcodes"]); } /** * 根据吨,立方,SKU减产系数计算产能 * * @param double $tonne * @param double $cubicMeter * @param int $amount * @param int $rpcc reduced_production_capacity_coefficient * * @return double */ public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc) { $need = max(($tonne*config("appointment.production_capacity.tonne")), ($cubicMeter*config("appointment.production_capacity.cubic_meter"))); $coefficient = config("appointment.production_capacity.coefficient"); if ($amount && ($need/$amount < $coefficient)){ $need *= 1+(($coefficient-($need/$amount))*($rpcc/100)); } return $need; } /** * 根据提供的详情数组插入详情 * * @param DeliveryAppointment|object $parent * @param array $details * @param string $name * @param string $amount * @param string $barCode */ public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code") { $codes = []; $names = []; $insert = [];//插入的元素 foreach ($details as $index=>$detail){ if (isset($detail["commodity_id"]) && $detail["commodity_id"]){ $insert[] = [ "delivery_appointment_id" => $parent->id, "commodity_id" => $detail["commodity_id"], "bar_code" => null, "name" => null, "amount" => $detail[$amount], ]; unset($details[$index]);continue; } if ($detail[$barCode])$codes[] = $detail[$barCode]; else $names[] = $detail[$name]; } $map = [];//存储条码与ID的映射 if ($codes || $names){ CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){ /** @var Builder $query */ $query->where("owner_id",$parent->owner_id); })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){ /** @var Builder $query */ $query->whereIn("name",$names); })->get()->each(function ($commodity)use(&$map){ $map[$commodity->code] = $commodity->commodity_id; $map[$commodity->commodity->name] = $commodity->commodity_id; }); } foreach ($details as $detail){ $code = $detail[$barCode]; $insert[] = [ "delivery_appointment_id" => $parent->id, "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null), "bar_code" => $code, "name" => $detail[$name], "amount" => $detail[$amount], ]; } if ($insert)DeliveryAppointmentDetail::query()->insert($insert); } /** * 获取一个KEY值用于验证二维码 21-4-2 切换为永久二维码 * * @return string */ public function getKey() { $y = 'w';//date("y"); $m = 'a';//date("m"); $d = 's';//date("d"); $k1 = substr(env("APP_KEY"),7,2); $k2 = substr(env("APP_KEY"),10,2); $k3 = substr(env("APP_KEY"),15,2); return md5($k3.$y.$k2.$m.$k1.$d); } /** * 验证key值 * * @param string $key * @return bool */ public function checkKey(string $key):bool { if ($key!=$this->getKey())return false; return true; } /** * 获取当前时段 * * @param int|null $hour * * @return int|bool */ public function getPeriod($hour=null) { if (!$hour)$hour = date("H"); foreach (DeliveryAppointment::PERIOD as $key=>$period){ $arr = explode("-",$period); if (count($arr)!=2)continue; if ($hour>=$arr[0] && $hour<$arr[1])return $key; } return false; } /** * 验证预约单完整性修改状态 * * @param integer $id * @return int */ public function checkFull($id) { /** @var DeliveryAppointment|\stdClass $delivery */ $delivery = DeliveryAppointment::query()->withCount(["cars"=>function($query){ /** @var Builder $query */ $query->whereNull("delivery_time")->where("status","!=",2); }])->find($id); if ($delivery->cars_count != 0)return 0; $status = 2; if (Carbon::parse($delivery->appointment_date." ".explode("-",DeliveryAppointment::PERIOD[$delivery->date_period])[1].":00:00")->lt(Carbon::parse($delivery->car->delivery_time))) $status = 4; $delivery->update(["status"=>$status]); return $status; } /** * 获取指定时段可用产能 * * @param string $date * @param int $period * @param integer $warehouseId * * @return double */ public function getAvailableCapacity($date,$period,$warehouseId) { $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity") ->where("appointment_date",$date) ->where("date_period",$period) ->where("warehouse_id",$warehouseId) ->where("status",0) ->groupBy(["appointment_date","date_period"]) ->lockForUpdate()->first(); if (!$result)return 0; /** @var \stdClass $warehouse */ $warehouse = Warehouse::query()->find($warehouseId); if (!$warehouse)return 0; $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$period]; return $total-$result->capacity; } /** * 检查可操作ASN * * @param string $asn * @param string $ownerCode * @return bool */ public function checkOperableAsn(string $asn,string $ownerCode):bool { if (!Owner::query()->where("code",$ownerCode)->where("is_check_asn",1)->first())return true; return !!DeliveryAppointment::query()->selectRaw("1")->where("status",'!=',1) ->where("appointment_date",date("Y-m-d")) ->where("asn_number",'like',"%{$asn}%")->first(); } /** * 是否为送货禁止日期 */ public function isBanOnDeliveryDate($date):bool { if (!$date || strlen($date)<10){ return false; } $date = substr($date,0,10); $list = Cache::get(DeliveryAppointment::BAN,[]); foreach ($list as $index=>$val){ if ($date==$val){ return true; } if (strtotime($date." 00:00:00") > strtotime($val." 00:00:00")){ unset($list[$index]); } } Cache::forever(DeliveryAppointment::BAN,array_values($list)); return false; } /** * 添加送货禁止日期 * * @param $date * @return bool */ public function addBanOnDeliveryDate($date):bool { $list = Cache::get(DeliveryAppointment::BAN,[]); array_push($list,$date); return Cache::forever(DeliveryAppointment::BAN,$list); } }