| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Console\Commands;
- use App\OwnerAreaReport;
- use App\OwnerFeeStorage;
- use App\OwnerPriceSystem;
- use App\Services\LogService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class CreateOwnerBillReport extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'createOwnerBillReport';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'create owner bill report';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * 1号生成账单确认,确认金额由人工填写 原始金额默认为:即时账单月记录金额+仓储计费与面积计算金额
- *
- * @return void
- */
- public function handle()
- {
- $year = (int)date('Y');
- $month = (int)date('m');
- if ($month == 1){
- $year--;
- $lastMonth = '12';
- }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
- $sql = "SELECT owner_id,SUM(IFNULL(work_fee,0)) AS work_fee,SUM(IFNULL(logistic_fee,0)) AS logistic_fee FROM owner_fee_details WHERE worked_at LIKE ? AND ((type = '发货' AND logistic_fee IS NOT NULL AND work_fee IS NOT NULL) OR (type <> '发货' AND work_fee IS NOT NULL)) GROUP BY owner_id";
- $billDetails = DB::select(DB::raw($sql),[$year."-".$lastMonth."%"]);
- $areas = OwnerAreaReport::query()->with(["ownerStoragePriceModel.timeUnit","ownerStoragePriceModel.taxRate","ownerStoragePriceModel.unit"])
- ->where("counting_month","like",$year."-".$lastMonth."%")->get();
- $map = [];
- $mapTax = [];
- foreach($areas as $area){
- if (!$area->ownerStoragePriceModel)continue;
- //信息提取模板
- $GLOBALS["FEE_INFO"] = [
- "area_id" =>$area->id,
- "counting_type" =>$area->ownerStoragePriceModel->counting_type,
- "using_type" =>$area->ownerStoragePriceModel->using_type,
- "fee_description" =>"",
- "total_fee" =>0,
- "tax_rate" =>0,
- ];
- $key = $area->owner_id."_".$area->counting_month;
- if (!isset($map[$key]))$map[$key] = $mapTax[$key] = 0;
- list($money,$taxFee) = app('OwnerStoragePriceModelService')
- ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
- $map[$key] += $money;
- $mapTax[$key] += $taxFee;
- $GLOBALS["FEE_INFO"]["total_fee"] = $money;
- OwnerFeeStorage::query()->create($GLOBALS["FEE_INFO"]);
- }
- foreach (OwnerPriceSystem::query()->with(["timeUnit","taxRate"])->select("owner_id","usage_fee")->whereNull("operation")->orWhere("operation","")->get() as $system){
- list($systemFee[$system->owner_id],$systemTaxFee[$system->owner_id]) = app("OwnerAreaReportService")->systemFee($system,$year."-".$lastMonth);
- }
- $chunks = array_chunk($billDetails,50);
- foreach ($chunks as $bills){
- $date = date('Y-m-d H:i:s');
- $createOwnerBillReport = [];
- foreach ($bills as $bill){
- $key = $bill->owner_id."_".$year."-".$lastMonth;
- $createOwnerBillReport[] = [
- "owner_id" => $bill->owner_id, //项目ID
- "counting_month" => $year."-".$lastMonth."-01", //结算月
- "work_fee" => $bill->work_fee,
- "logistic_fee" => $bill->logistic_fee,
- "storage_fee" => $map[$key] ?? 0,
- "storage_tax_fee" => $mapTax[$key] ?? 0,
- "other_fee" => $systemFee[$bill->owner_id] ?? null,
- "other_tax_fee" => $systemTaxFee[$bill->owner_id] ?? null,
- "created_at" => $date,
- ];
- }
- LogService::log(__METHOD__,"项目管理-生成确认账单",json_encode($createOwnerBillReport));
- DB::table("owner_bill_reports")->insert($createOwnerBillReport);
- }
- }
- }
|