1010use App \Classes \Output ;
1111use App \Crud \ProductHistoryCrud ;
1212use App \Crud \ProductUnitQuantitiesCrud ;
13+ use App \Exceptions \NotAllowedException ;
1314use App \Exceptions \NotFoundException ;
1415use App \Http \Controllers \DashboardController ;
1516use App \Models \ProcurementProduct ;
2526use App \Models \Unit ;
2627use App \Models \ProductUnitQuantity ;
2728use App \Services \CrudService ;
29+ use App \Services \DateService ;
2830use App \Services \Helper ;
2931use App \Services \ProductService ;
3032use App \Services \Options ;
@@ -37,13 +39,20 @@ class ProductsController extends DashboardController
3739 /** @var ProductService */
3840 protected $ productService ;
3941
42+ /**
43+ * @var DateService
44+ */
45+ protected $ dateService ;
46+
4047 public function __construct (
41- ProductService $ productService
48+ ProductService $ productService ,
49+ DateService $ dateService
4250 )
4351 {
4452 parent ::__construct ();
4553
4654 $ this ->productService = $ productService ;
55+ $ this ->dateService = $ dateService ;
4756 }
4857
4958 public function saveProduct ( Request $ request )
@@ -523,19 +532,71 @@ public function createAdjustment( Request $request )
523532
524533 public function searchUsingArgument ( $ reference )
525534 {
526- $ product = Product::barcode ( $ reference )
535+ $ procurementProduct = ProcurementProduct::barcode ( $ reference )->first ();
536+ $ productUnitQuantity = ProductUnitQuantity::barcode ( $ reference )->first ();
537+ $ product = Product::barcode ( $ reference )
527538 ->searchable ()
528- ->with ( 'unit_quantities ' )
529539 ->first ();
530540
541+ if ( $ procurementProduct instanceof ProcurementProduct ) {
542+
543+ $ product = $ procurementProduct ->product ;
544+
545+ /**
546+ * check if the product has expired
547+ * and the sales are disallowed.
548+ */
549+ if (
550+ $ this ->dateService ->copy ()->greaterThan ( $ procurementProduct ->expiration_date ) &&
551+ $ product ->expires &&
552+ $ product ->on_expiration === Product::EXPIRES_PREVENT_SALES ) {
553+ throw new NotAllowedException ( __ ( 'Unable to add the product to the cart as it has expired. ' ) );
554+ }
555+
556+ /**
557+ * We need to add a reference of the procurement product
558+ * in order to deplete the available quantity accordingly.
559+ * Will also be helpful to track how products are sold.
560+ */
561+ $ product ->procurement_product_id = $ procurementProduct ->id ;
562+
563+ } else if ( $ productUnitQuantity instanceof ProductUnitQuantity ) {
564+
565+ /**
566+ * if a product unit quantity is loaded. Then we make sure to return the parent
567+ * product with the selected unit quantity.
568+ */
569+ $ productUnitQuantity ->load ( 'unit ' );
570+
571+ $ product = Product::find ( $ productUnitQuantity ->product_id );
572+ $ product ->load ( 'unit_quantities ' );
573+ $ product ->selectedUnitQuantity = $ productUnitQuantity ;
574+
575+ } else if ( $ product instanceof Product ) {
576+
577+ $ product ->load ( 'unit_quantities ' );
578+
579+ if ( $ product ->accurate_tracking ) {
580+ throw new NotAllowedException ( __ ( 'Unable to add a product that has accurate tracking enabled, using an ordinary barcode. ' ) );
581+ }
582+ }
583+
531584 if ( $ product instanceof Product ) {
532585 return [
533586 'type ' => 'product ' ,
534587 'product ' => $ product
535588 ];
536589 }
537590
538- throw new NotFoundException ( __ ( 'The product request cannot be found. ' ) );
591+ throw new NotFoundException ( __ ( 'There is no products matching the current request. ' ) );
592+ }
593+
594+ public function printLabels ()
595+ {
596+ return $ this ->view ( 'pages.dashboard.products.print-labels ' , [
597+ 'title ' => __ ( 'Print Labels ' ),
598+ 'description ' => __ ( 'Customize and print products labels. ' ),
599+ ]);
539600 }
540601}
541602
0 commit comments