Exemple #1
0
 def create_if_not_exist(cls, tracking_id, combine_id, warehouse_id,
                         inbound_carrier, latest_ship_datetime):
     CPInboundParcel.create_if_not_exist(
         tracking_id=tracking_id,
         combine_id=combine_id,
         warehouse_id=warehouse_id,
         inbound_carrier=inbound_carrier,
         latest_ship_datetime=latest_ship_datetime)
Exemple #2
0
 def set_ready_to_ship(cls):
     CPInboundParcel.update({
         "status": CPInboundParcel.Status.Inbound
     },
         {
         "$set": {
             "ready_to_ship": True,
             "updated_datetime": datetime.utcnow()
         }
     })
Exemple #3
0
 def bulk_set_ready_to_ship(cls, tracking_ids):
     with CPInboundParcel.bulk() as bulk_context:
         utc_now = datetime.utcnow()
         for tracking_id in tracking_ids:
             CPInboundParcel.bulk_update(bulk_context,
                                         {"tracking_id": tracking_id}, {
                                             "$set": {
                                                 "ready_to_ship": True,
                                                 "updated_datetime": utc_now
                                             }
                                         },
                                         multi=False)
Exemple #4
0
 def get_inbound_parcels(cls, warehouse_id):
     return CPInboundParcel.find_iter({
         "warehouse_id": warehouse_id,
         "status": {
             "$in": [CPInboundParcel.Status.Inbound]
         },
         "ready_to_ship": False
     })
Exemple #5
0
def set_ready_to_ship():
    inbound_parcels = CPInboundParcel.find(
        {
            "created_datetime": {
                "$gte": datetime(2019, 4, 18, 0, 0, 0),
                "$lte": datetime(2019, 4, 19, 0, 0, 0)
            }
        }
    )

    for parcel in inbound_parcels:
        parcel.set(ready_to_ship=True)
Exemple #6
0
    def __init__(self, tracking_id, *args, **kwargs):
        if not tracking_id:
            raise ValueError("tracking_id is null or empty.")

        self._tracking_id = tracking_id
        self.inbound_parcel = CPInboundParcel.by_tracking_id(self._tracking_id)

        if not self.inbound_parcel:
            raise ValueError(
                "Parcel with tracking id {0} doesn't exist.".format(
                    tracking_id))

        super(CPInboundParcelAccessor, self).__init__(*args, **kwargs)
Exemple #7
0
    def allocate_cabinet_for_parcels(cls, job_id):
        job_accessor = SortJobAccessor(job_id)
        job_accessor.check_job_type(CPSortJob.Type.AllocateCabinetLattice)
        job_accessor.start_calculation()
        job_accessor.flush()

        job = job_accessor.sort_job

        try:
            warehouse = Warehouse.by_warehouse_id(job.warehouse_id)
            inbound_parcels = CPInboundParcel.find_iter({
                "warehouse_id":
                job.warehouse_id,
                "status":
                CPInboundParcel.Status.Inbound,
                "ready_to_ship":
                True
            })

            parcel_groups = defaultdict(list)
            for parcel in inbound_parcels:
                parcel_groups[parcel.combine_id].append(parcel)

            combine_ids = parcel_groups.keys()
            if combine_ids:
                group_ids = CPSortAllocateGroupId.allocate(
                    len(combine_ids), warehouse.sort_batch_size)
                group_dict = dict(zip(combine_ids, group_ids))

                lattice_id = 1
                cabinet_id = str(ObjectId())
                for combine_id, parcels in parcel_groups.items():
                    group_length = len(parcels)
                    sort_type = CPSortPool.SortType.DirectShip if group_length == 1 else CPSortPool.SortType.Combined
                    for parcel in parcels:
                        group_id = group_dict[combine_id]
                        CPSortPool.create(job_id=job_id,
                                          tracking_id=parcel.tracking_id,
                                          sort_type=sort_type,
                                          group_ids=group_id,
                                          cabinet_id=cabinet_id,
                                          lattice_id=lattice_id)
                    lattice_id += 1

            job_accessor.success()
        except Exception as ex:
            job_accessor.fail(str(ex))
        finally:
            job_accessor.flush()
Exemple #8
0
    def on_get(self, req, resp):
        tracking_id = req.get_param("tracking_id", required=True)
        job_id = req.get_param("job_id", required=True)
        round_id = req.get_param_as_int("round_id", required=True)

        try:
            sort_info = SortJobUtil.get_parcel_sort_info(
                job_id, tracking_id, round_id)
        except ValueError as ex:
            raise falcon.HTTPNotFound(description=str(ex))

        parcel = CPInboundParcel.by_tracking_id(tracking_id)
        sort_info["weight"] = parcel.weight
        sort_info["inbound_datetime"] = parcel.timeline.inbound.strftime(
            "%Y-%m-%d %H:%M:%S")

        resp.media = {"sort_info": sort_info}
Exemple #9
0
    def get_combine_cabinet_from_first_tracking_id(cls, job_id, tracking_id):
        job_accessor = SortJobAccessor(job_id)
        parcel = CPSortPool.by_tracking_id(job_id, tracking_id)

        if not parcel:
            raise ValueError(
                "Parcel with tracking id {0} didn't exist in job {1}.".format(
                    tracking_id, job_id))

        parcels = CPSortPool.find({
            "job_id": job_id,
            "cabinet_id": parcel.cabinet_id
        })

        tracking_ids = [parcel.tracking_id for parcel in parcels]
        inbound_parcels = CPInboundParcel.by_tracking_ids(tracking_ids)
        inbound_parcel_dict = {
            parcel.tracking_id: parcel
            for parcel in inbound_parcels
        }

        parcels = [parcel.to_dict() for parcel in parcels]

        def key_func(x):
            return x["lattice_id"]

        parcels = sorted(parcels, key=key_func)
        parcel_groups = groupby(parcels, key=key_func)

        res = []
        for group in parcel_groups:
            lattice_id = group[0]
            parcels_in_lattice = list(group[1])
            for parcel in parcels_in_lattice:
                tracking_id = parcel["tracking_id"]
                inbound_parcel = inbound_parcel_dict[tracking_id]
                parcel["inbound_weight"] = inbound_parcel.weight
                parcel[
                    "inbound_datetime"] = inbound_parcel.timeline.inbound.strftime(
                        "%Y-%m-%d %H:%M:%S")
                parcel["lattice_id"] = lattice_id

            res.append(parcels_in_lattice)

        return job_accessor.sort_job, res
Exemple #10
0
    def inbound_all_parcels(cls, user):
        inbound_parcels = CPInboundParcel.find(
            {
                "status": CPInboundParcel.Status.Pending
            }
        )

        for parcel in inbound_parcels:
            InboundParcelUtil.inbound_parcel(
                tracking_id=parcel.tracking_id,
                parcel_type=CPInboundParcel.ParcelType.Ordinary,
                weight=random.uniform(0.1, 1.2),
                has_battery=False,
                has_liquid=False,
                has_sensitive=False,
                sensitive_reason=None,
                user=user
            )
def create_combine_pool():
    warehouse_id = "SHYW"
    warehouse = Warehouse.by_warehouse_id(warehouse_id)
    job_prefix = datetime.utcnow().strftime("%Y%m%d")
    job_id = SequenceIdGenerator.get_sequence_id(job_prefix)
    job = SortJobAccessor.create(
        job_id, CPSortJob.Type.AllocateCabinetLattice, "SHYW")

    inbound_parcels = CPInboundParcel.find({
        "created_datetime": {
            "$gte": datetime(2019, 4, 18, 0, 0, 0),
            "$lte": datetime(2019, 4, 19, 0, 0, 0)
        }
    })

    if not inbound_parcels:
        raise Exception("inbound_parcels is empty")

    combine_ids = list(set(parcel.combine_id for parcel in inbound_parcels))
    parcel_groups = defaultdict(list)
    for parcel in inbound_parcels:
        parcel_groups[parcel.combine_id].append(parcel)

    group_ids = CPSortAllocateGroupId.allocate(len(combine_ids), warehouse.sort_batch_size)
    group_dict = dict(zip(combine_ids, group_ids))

    lattice_id = 1
    cabinet_id = str(ObjectId())
    for combine_id, parcels in parcel_groups.items():
        group_length = len(parcels)
        sort_type = CPSortPool.SortType.DirectShip if group_length == 1 else CPSortPool.SortType.Combined
        for parcel in parcels:
            group_id = group_dict[combine_id]
            CPSortPool.create(
                job_id=job_id,
                tracking_id=parcel.tracking_id,
                sort_type=sort_type,
                group_ids=group_id,
                cabinet_id=cabinet_id,
                lattice_id=lattice_id
            )
        lattice_id += 1
Exemple #12
0
 def by_outbound_logistics_order_id(cls, outbound_logistics_order_id):
     return CPInboundParcel.find(
         {"outbound_logistics_order_id": outbound_logistics_order_id})
Exemple #13
0
 def by_tracking_ids(cls, tracking_ids):
     return CPInboundParcel.by_tracking_ids(tracking_ids)
Exemple #14
0
 def cancel_all_parcels(cls):
     CPInboundParcel.update(
         {}, {"$set": {"status": CPInboundParcel.Status.Cancelled}})
Exemple #15
0
    def check_inbound_parcel_ready_to_ship(cls, job_id):
        job_accessor = SortJobAccessor(job_id)
        job_accessor.check_job_type(
            CPSortJob.Type.CheckInboundParcelReadyToShip)
        job_accessor.start_calculation()
        job_accessor.flush()

        job = job_accessor.sort_job

        try:
            inbound_parcels = CPInboundParcel.find_iter({
                "warehouse_id":
                job.warehouse_id,
                "status":
                CPInboundParcel.Status.Inbound,
                "ready_to_ship":
                False
            })

            parcel_groups = defaultdict(list)
            for parcel in inbound_parcels:
                parcel_groups[parcel.combine_id].append(parcel)

            combine_ids = list(parcel_groups.keys())
            if combine_ids:
                utc_now = datetime.utcnow()
                all_parcels = CPInboundParcel.find_iter(
                    {"combine_id": {
                        "$in": combine_ids
                    }})
                all_parcel_groups = defaultdict(list)
                for parcel in all_parcels:
                    all_parcel_groups[parcel.combine_id].append(parcel)

                ready_to_ship_parcels = defaultdict(list)
                for combine_id, parcels in parcel_groups.items():
                    all_combine_id_parcels = all_parcel_groups[combine_id]
                    if len([
                            parcel for parcel in all_combine_id_parcels
                            if not parcel.ready_to_ship
                    ]) == len(parcels):
                        ready_to_ship_parcels[combine_id] = parcels
                    else:
                        for parcel in parcels:
                            if parcel.latest_ship_datetime < utc_now:
                                ready_to_ship_parcels[combine_id].append(
                                    parcel)

                if ready_to_ship_parcels:
                    ready_to_ship_tracking_ids = []
                    for parcels in ready_to_ship_parcels.values():
                        for parcel in parcels:
                            ready_to_ship_tracking_ids.append(
                                parcel.tracking_id)
                    if ready_to_ship_tracking_ids:
                        CPInboundParcelAccessor.bulk_set_ready_to_ship(
                            ready_to_ship_tracking_ids)

            job_accessor.success()
        except Exception as ex:
            job_accessor.fail(str(ex))
        finally:
            job_accessor.flush()