Advertisement
mayonismasin

IMPORT CSV

Feb 17th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.53 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers;
  3.  
  4. use Maatwebsite\Excel\Facades\Excel;
  5. use Illuminate\Support\Facades\Input;
  6.  
  7. use App\Http\Controllers\Controller;
  8. use App\Models\DailyStockControlHeader;
  9. use Illuminate\Http\Request;
  10. use App\Models\SpecialOrder;
  11.  
  12. //solehin add
  13. use App\Models\OutletsModel;
  14. use App\Models\OutletGroupModel;
  15. use App\Models\ProductModel;
  16. use App\Models\StockCategoryModel;
  17. use App\Models\RoutesModel;
  18. use App\Models\PriceGroup;
  19. use App\Models\PriceList;
  20. use App\Models\DVS_Header;
  21. use App\Models\DvsDetailsModel as DVS_Detail;
  22. use App\Models\SpecialOrderItem;
  23. use Rap2hpoutre\FastExcel\FastExcel;
  24. use App\Jobs\ProcessCsvUpload;
  25. use App\Models\GlobalSettings;
  26.  
  27. class DespatchController extends Controller
  28. {
  29.  
  30.  
  31.     public function fileStoreExcel(Request $request)
  32.     {  
  33.  
  34.         $folderPath = GlobalSettings::get();
  35.  
  36.         $fileTblOutlets                    = resource_path($folderPath[0]->import_folder_path.'/SBIntCustTableARV.csv');
  37.         $fileTblOutletGroup                = resource_path($folderPath[0]->import_folder_path.'/SBIntCustGrpARV.csv');
  38.         $fileTblCustPriceCode              = resource_path($folderPath[0]->import_folder_path.'/SBIntCustPriceCodeARV.csv');
  39.  
  40.         $fileTblProduct                    = resource_path($folderPath[0]->import_folder_path.'/SBIntInventTableARV.csv');
  41.         $fileTblProd_ActualTray            = resource_path($folderPath[0]->import_folder_path.'/SBIntInventTableActualTrayARV.csv');
  42.         $fileTblProductUnitPrice           = resource_path($folderPath[0]->import_folder_path.'/SBIntInventPriceARV.csv');
  43.         $fileTblProductCat                 = resource_path($folderPath[0]->import_folder_path.'/SBIntInventCatARV.csv');
  44.        
  45.         $fileTblPriceCode                  = resource_path($folderPath[0]->import_folder_path.'/SBIntPriceCodeMasterARV.csv');
  46.         $fileTblPriceGroup                 = resource_path($folderPath[0]->import_folder_path.'/SBIntInventPriceGroupARV.csv');
  47.  
  48.         $fileTblSalesRoute                 = resource_path($folderPath[0]->import_folder_path.'/SBIntSalesRouteARV.csv');
  49.  
  50.         $fileTblForecast                   = resource_path($folderPath[0]->import_folder_path.'/t-customer_recomqty.csv');
  51.         $fileTblDVSLine                    = resource_path($folderPath[0]->import_folder_path.'/SBCustItemForecastLineARV.csv');
  52.         $fileTblDVSHeader                  = resource_path($folderPath[0]->import_folder_path.'/SBCustItemForecastTableARV.csv');
  53.  
  54.         if(!empty($fileTblOutlets))
  55.         {
  56.             $this->readInsertTableOutlets($fileTblOutlets);
  57.         }
  58.  
  59.         if(!empty($fileTblOutletGroup))
  60.         {
  61.             $this->readInsertTableOutletGroup($fileTblOutletGroup);
  62.         }
  63.  
  64.         if(!empty($fileTblCustPriceCode))
  65.         {
  66.             $this->readInsertTableCustPriceCode($fileTblCustPriceCode);
  67.         }
  68.  
  69.         if(!empty($fileTblProduct))
  70.         {
  71.             $this->readInsertTableProduct($fileTblProduct);
  72.         }
  73.  
  74.         if(!empty($fileTblProd_ActualTray))
  75.         {
  76.             $this->readInsertTableProduct_ActualTray($fileTblProd_ActualTray);
  77.         }
  78.  
  79.         if(!empty($fileTblProductUnitPrice))
  80.         {
  81.             $this->readInsertTableProductUnitPrice($fileTblProductUnitPrice);
  82.         }
  83.  
  84.         if(!empty($fileTblProductCat))
  85.         {
  86.             $this->readInsertTableProductCat($fileTblProductCat);
  87.         }
  88.  
  89.         if(!empty($fileTblPriceCode))
  90.         {
  91.             $this->readInsertTablePriceCode($fileTblPriceCode);
  92.         }
  93.  
  94.         if(!empty($fileTblPriceGroup))
  95.         {
  96.             $this->readInsertTablePriceGroup($fileTblPriceGroup);
  97.         }
  98.  
  99.         if(!empty($fileTblSalesRoute))
  100.         {
  101.             $this->readInsertTableSalesRoute($fileTblSalesRoute);
  102.         }
  103.  
  104.         if(!empty($fileTblDVSHeader))
  105.         {
  106.             $this->readInsertTableDVSHeader($fileTblDVSHeader);
  107.         }
  108.  
  109.         if(!empty($fileTblDVSLine))
  110.         {
  111.             $this->readInsertTableDVSLine($fileTblDVSLine);
  112.         }
  113.  
  114.         // if(!empty($fileTblForecast))
  115.         // {
  116.         //     $this->readInsertTableForecast($fileTblForecast);
  117.         // }      
  118.          
  119.     }
  120.  
  121.     public function readInsertTableOutlets($file)
  122.     {
  123.         // insert in table outlets
  124.        (new FastExcel)->import($file, function ($line) {
  125.  
  126.             if($line['PAYMTERMID'] == '1')
  127.             {
  128.                 $payment_method = 'C';
  129.             }else{
  130.                 $payment_method = 'R';
  131.             }
  132.  
  133.             OutletsModel::UpdateOrCreate([
  134.                 'outlet_code'    => $line['ACCOUNTNUM']
  135.              ],
  136.              [
  137.                 'outlet_name'    => $line['NAME'],
  138.                 'outlet_group'   => $line['CUSTGROUP'],
  139.                 'segment'        => $line['SEGMENTID'],
  140.                 'subsegment'     => $line['SUBSEGMENTID'],
  141.                 'address1'       => $line['ADDR1'],
  142.                 'address2'       => $line['ADDR2'],
  143.                 'address3'       => $line['ADDR3'],
  144.                 'address4'       => $line['ADDR4'],
  145.                 'phone'          => $line['PHONE'],
  146.                 'fax'            => $line['TELEFAX'],
  147.                 'route_code'     => $line['AREA'],
  148.                 'payment_method' => $payment_method,
  149.                 'short_name'     => $line['ShortName'],
  150.             ]);
  151.  
  152.         });        
  153.     }
  154.  
  155.    
  156.     public function readInsertTableOutletGroup($file)
  157.     {
  158.          // insert in table outlets group
  159.         (new FastExcel)->import($file, function ($line) {
  160.  
  161.             OutletGroupModel::UpdateOrCreate([
  162.                 'outlet_group'   => $line['CHAINID'],
  163.                 'group_name'     => $line['DESCRIPTION']
  164.              ],
  165.              [
  166.                 'created_at'                  => $line['STARTDATETIME'],
  167.                 'invoice_by_product_brand'    => '1',
  168.             ]);
  169.  
  170.  
  171.             });        
  172.     }
  173.  
  174.     public function readInsertTableCustPriceCode($file)
  175.     {
  176.       // update table outlets - price group code
  177.         (new FastExcel)->import($file, function ($row) {
  178.  
  179.             OutletsModel::where('outlet_code' , $row['CUSTOMERCODE'])
  180.                  ->update([
  181.                      'price_group_code' => $row['PRICECODE']
  182.                      ]);
  183.  
  184.         });
  185.     }
  186.  
  187.     public function readInsertTableProduct($file)
  188.     {
  189.        // insert in table product
  190.         $product = (new FastExcel)->import($file, function ($row) {
  191.  
  192.             ProductModel::UpdateOrCreate([
  193.                 'product_code' => $row['ITEMCODE']
  194.              ],
  195.              [
  196.                 'description'           => $row['DESCRIPTION'],
  197.                 'name'                  => $row['NAME'],
  198.                 'brand'                 => 'AOQI',
  199.                 'uom'                   => $row['UOM'],
  200.                 'stock_category_code'   => $row['CATEGORY'],
  201.                 'parent_product_code'   => $row['ITEMCODE'],
  202.                 'subcategory'           => $row['SUBCATEGORY'],
  203.             ]);
  204.            
  205.         });
  206.     }
  207.  
  208.     public function readInsertTableProduct_ActualTray($file)
  209.     {
  210.         // update table product - update actual tray
  211.         (new FastExcel)->import($file, function ($row) {
  212.  
  213.         ProductModel::where('product_code' , $row['ItemCode'])
  214.                 ->update([
  215.                     'actual_tray' => $row['Conv_Pack']
  216.                     ]);
  217.         });
  218.     }
  219.  
  220.     public function readInsertTableProductUnitPrice($file)
  221.     {
  222.         // update table product - update unit price
  223.         (new FastExcel)->import($file, function ($row) {
  224.  
  225.             ProductModel::where('product_code',$row['ITEMCODE'])
  226.                  ->update([
  227.                      'unit_price'=> $row['UNITPRICE']
  228.                      ]);
  229.  
  230.         });
  231.     }
  232.  
  233.     public function readInsertTableProductCat($file)
  234.     {
  235.         //need to insert in table stock category
  236.         (new FastExcel)->import($file, function ($row) {
  237.  
  238.         StockCategoryModel::UpdateOrCreate([
  239.             'code' => $row['CATEGORY']
  240.             ],
  241.             [
  242.             'description'      => $row['DESCRIPTION'],
  243.             'is_active'        => '1',
  244.         ]);
  245.  
  246.         });  
  247.     }
  248.  
  249.     public function readInsertTablePriceCode($file)
  250.     {
  251.          // need to insert in table price group
  252.          (new FastExcel)->import($file, function ($row) {
  253.  
  254.             PriceGroup::UpdateOrCreate([
  255.                 'code' => $row['PRICECODE']
  256.              ],
  257.              [
  258.                 'description'      => $row['DESCRIPTION'],
  259.                 'is_active'        => '1',
  260.             ]);
  261.  
  262.          });  
  263.     }
  264.  
  265.     public function readInsertTablePriceGroup($file)
  266.     {
  267.          //fifth , need to insert in table price list
  268.          (new FastExcel)->import($file, function ($row) {
  269.             return PriceList::create([
  270.                 'product_code'            => $row['ITEMCODE'],
  271.                 'unit_price'              => $row['UNITPRICE'],
  272.                 'code'                    => $row['PRICECODE']
  273.                 ]);      
  274.             });  
  275.     }
  276.  
  277.     public function readInsertTableSalesRoute($file)
  278.     {
  279.         //then , need to insert in table route
  280.         (new FastExcel)->import($file, function ($row) {
  281.  
  282.             RoutesModel::UpdateOrCreate([
  283.                 'route_code' => $row['ROUTEID']
  284.              ],
  285.              [
  286.                 'route_description'             => $row['DESCRIPTION'],
  287.                 'created_at'                    => $row['STARTDATETIME'],
  288.                 'activity_type'                 => '1',
  289.             ]);
  290.  
  291.             });  
  292.     }
  293.  
  294.     public function readInsertTableDVSHeader($file)
  295.     {
  296.  
  297.        (new FastExcel)->import($file, function ($row) {
  298.  
  299.             if($row['DOCTYPE'] == 'DIR' || $row['DOCTYPE'] == 'DIM')
  300.             {
  301.  
  302.                 SpecialOrder::UpdateOrCreate([
  303.                     'order_no' => $row['CUSTORDERNUM']
  304.                 ],
  305.                 [
  306.                     'order_date'         => $row['ORDERDATE'],
  307.                     'delivery_date'      => $row['DELIVERYDATE'],
  308.                     'route_code'         => $row['SALESROUTEID'],
  309.                     'outlet_code'        => $row['CUSTACCOUNT'],
  310.                     'order_amount'       => $row['SALESAMOUNT'],
  311.                 ]);
  312.  
  313.             }elseif ($row['DOCTYPE'] == "DVS") {
  314.  
  315.                 DVS_Header::UpdateOrCreate([
  316.                     'order_no' => $row['CUSTORDERNUM']
  317.                 ],
  318.                 [
  319.                     'order_date'         => $row['ORDERDATE'],
  320.                     'delivery_date'      => $row['DELIVERYDATE'],
  321.                     'route_code'         => $row['SALESROUTEID'],
  322.                     'outlet_code'        => $row['CUSTACCOUNT'],
  323.                     'order_amount'       => $row['SALESAMOUNT'],
  324.                 ]);
  325.  
  326.             }
  327.  
  328.          });    
  329.     }
  330.  
  331.     public function readInsertTableDVSLine($file)
  332.     {
  333.  
  334.         (new FastExcel)->import($file, function ($row) {
  335.  
  336.             //checking if order no is same as dvs header (order no)
  337.             //then can insert in table dvs detail
  338.  
  339.             if($row['DOCTYPE'] == 'DVS')
  340.             {
  341.  
  342.                 $data = DVS_Header::select('id' , 'order_no')
  343.                 ->where('order_no', '=', $row['SALESID'])
  344.                 ->get();
  345.  
  346.                 if( $data != '' )
  347.                 {
  348.                     $record = DVS_Detail::create([
  349.                         'order_no'           => $row['SALESID'],
  350.                         'order_quantity'     => $row['SALESQTY'],
  351.                         'product_code'       => $row['ITEMID'],
  352.                         'dvs_id'             => $data[0]->id
  353.                         ]);  
  354.  
  355.                 }
  356.  
  357.             }elseif ( $row['DOCTYPE'] == 'DIR' || $row['DOCTYPE'] == 'DIM') {
  358.                
  359.                     $data = SpecialOrder::select('id' , 'order_no')
  360.                     ->where('order_no', '=', $row['SALESID'])
  361.                     ->get();
  362.        
  363.                     if($data != '')
  364.                     {    
  365.                         $record = SpecialOrderItem::create([
  366.                          //   'order_no'   => $row['SALESID'],
  367.                             'order_quantity'     => $row['SALESQTY'],
  368.                             'product_code'       => $row['ITEMID'],
  369.                             'special_order_id'   => $data[0]->id
  370.                             ]);  
  371.                     }
  372.             }
  373.  
  374.             });    
  375.     }
  376.  
  377.     public function testing()
  378.     {
  379.         $a = DB::connection()->getPDO();
  380.         $path = file($request->file('file')->getRealPath());
  381.  
  382.            Excel::filter('chunk')->load($path)->chunk(500, function($rows){
  383.              foreach($rows as $row) {
  384.                  $data = collect([]);
  385.              }
  386.          });  
  387.     }
  388.  
  389.     public function readInsertTableForecast($file)
  390.     {
  391.  
  392.          Excel::load($file, function($reader) {
  393.           foreach ($reader->skipRows(0)->toArray() as $row) {
  394.                
  395.                 $dataSet[] = [
  396.                     'parent_route_code'        => $row['route'],
  397.                     'outlet_code'              => $row['pptid'],
  398.                     'product_code'             => $row['productid'],
  399.                     'plan_issue_date'          => $row['orderdeliverydate'],
  400.                     'final_recom_issue_qty'    => $row['planload'],
  401.                 ];
  402.  
  403.         }
  404.  
  405.         if(!empty($dataSet))
  406.         {
  407.            DB::table('forecast_issue')->insert($dataSet);
  408.         }
  409.  
  410.       });
  411.      
  412.     }
  413.  
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement