Symfony 年度销售报告集成
Symfony Yearly Sales Report Integration
你好,我正在尝试制作公司销售报告生成器,但我被卡住了。我尝试编写如下代码,但 SQL 函数不起作用:
public function getMonthlyReport($company, $year)
{
$searchYear = New \DateTimeImmutable("$year-01T-01T00:00:00");
$searchYearEnd = New \DateTimeImmutable("$year-12-31T23:59:59");
$query = $this->createQueryBuilder('s')
->select('SUM(expenses.price)')
->select('SUM(s.price)')
->select('MONTHNAME(s.created_at)')
->leftJoin('s.Expenses', 'expenses')
->andWhere('s.Company = :company')
->setParameter('company', $company)
->andWhere('s.created_at BETWEEN :startDate AND :endDate')
->setParameter('startDate ', $searchYear)
->setParameter('endDate', $searchYearEnd);
return $query
->getQuery()
->getResult();
}
我想要如下结果:
Month Sales Expenses
January 1250 0
February 10000 54879
March 0 4578
April 54000 44567
May 7854 2135
June 6399 8000
July 0 0
August 128000 1200
September 128000 1200
October 128000 1200
November 128000 1200
December 128000 1200
感谢您的帮助,
此致
编辑 1:
我写了一个 SQL 查询,如下所示,但它仅在该月有记录时才获取(如果该月没有记录则为 0)并且由于 SUM 和 MONTHNAME 函数我无法在 Symfony 中执行它
SELECT SUM(expenses.price), SUM(sales.price), MONTHNAME(sales.created_at) FROM sales left join expenses On expenses.sales_id = sales.sales_id WHERE 1 GROUP BY MONTHNAME(sales.created_at) ORDER BY sales.created_at ASC;
结果:
Month Sales Expenses
November 1650000 5275000
February 2750000 10550000
April 21450000 79125000
March 4950000 15825000
我已经想好要做什么了,将分 3 个步骤进行说明。
第一步:
创建一个带有 'month' 属性作为日期时间的 Symfony 'Calendar' 实体。
并插入 12 个月,例如 dd.mm.yy; 01.01.2022, 01.02.2022,...
第二步:
写一个 SQL 查询来获取 Monthnames 并左连接你的相关 table(销售和费用 table 对于这个问题)如下。
SELECT MONTH(c.month) as mOrder,MONTHNAME(c.month) AS mName, (CASE WHEN SUM(e.price) IS NULL THEN 0 ELSE SUM(e.price) END), (CASE WHEN SUM(s.price) IS NULL THEN 0 ELSE SUM(s.price) END) FROM calender c LEFT JOIN sales On MONTH(c.month) = MONTH(s.created_at) LEFT JOIN expenses e On e.sale_id=s.id
第三步:
将您的 SQL 查询一一转换为 DQL。按照以下指南安装 DoctrineExtensions 以使用 MONTH()、YEAR()、DAY()、MONTHNAME() 等函数:
How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2?
并在 join 语句后添加附加条件而不是 'where' 子句。
对我来说,功能如下。
public function getMonthlyReportRowsbyCompany($company, $year, $state)
{
$searchYear = New \DateTimeImmutable("$year-01T-01T00:00:00");
$fields = array('MONTH(c.month) as mOrder,MONTHNAME(c.month) AS mName', '(CASE WHEN SUM(e.price) IS NULL THEN 0 ELSE SUM(e.price) END)' ,'(CASE WHEN SUM(s.price) IS NULL THEN 0 ELSE SUM(s.price) END)');
if($state == "0")
$sales_states= ['1', '2', '3'];
else
$sales_states[] = $siparis_durumu;
if($company== "0")
$company= true;
$query = $this->createQueryBuilder('c')
->set('lc_time_names', 'tr_TR')
->select($fields)
->leftJoin('App:Sales', 's', 'WITH', 'MONTH(c.month) = MONTH(s.created_at) AND s.Company= :company AND s.state IN (:state) AND YEAR(s.created_at) = YEAR(:searchYear)')
->setParameter('company', $company)
->setParameter('state', $sales_states)
->setParameter(':searchYear', $searchYear)
->leftJoin('App:Expenses', 'e', 'WITH', 'e.sales=s.id')
->orderBy('mOrder', 'ASC')
->groupBy('c.month');
return $query
->getQuery()
->getResult();
}
唯一的问题是我找不到使“set('lc_time_names', 'tr_TR')”语句起作用的方法。除此之外,它完美无缺。
你好,我正在尝试制作公司销售报告生成器,但我被卡住了。我尝试编写如下代码,但 SQL 函数不起作用:
public function getMonthlyReport($company, $year)
{
$searchYear = New \DateTimeImmutable("$year-01T-01T00:00:00");
$searchYearEnd = New \DateTimeImmutable("$year-12-31T23:59:59");
$query = $this->createQueryBuilder('s')
->select('SUM(expenses.price)')
->select('SUM(s.price)')
->select('MONTHNAME(s.created_at)')
->leftJoin('s.Expenses', 'expenses')
->andWhere('s.Company = :company')
->setParameter('company', $company)
->andWhere('s.created_at BETWEEN :startDate AND :endDate')
->setParameter('startDate ', $searchYear)
->setParameter('endDate', $searchYearEnd);
return $query
->getQuery()
->getResult();
}
我想要如下结果:
Month Sales Expenses
January 1250 0
February 10000 54879
March 0 4578
April 54000 44567
May 7854 2135
June 6399 8000
July 0 0
August 128000 1200
September 128000 1200
October 128000 1200
November 128000 1200
December 128000 1200
感谢您的帮助, 此致
编辑 1: 我写了一个 SQL 查询,如下所示,但它仅在该月有记录时才获取(如果该月没有记录则为 0)并且由于 SUM 和 MONTHNAME 函数我无法在 Symfony 中执行它
SELECT SUM(expenses.price), SUM(sales.price), MONTHNAME(sales.created_at) FROM sales left join expenses On expenses.sales_id = sales.sales_id WHERE 1 GROUP BY MONTHNAME(sales.created_at) ORDER BY sales.created_at ASC;
结果:
Month Sales Expenses
November 1650000 5275000
February 2750000 10550000
April 21450000 79125000
March 4950000 15825000
我已经想好要做什么了,将分 3 个步骤进行说明。
第一步:
创建一个带有 'month' 属性作为日期时间的 Symfony 'Calendar' 实体。 并插入 12 个月,例如 dd.mm.yy; 01.01.2022, 01.02.2022,...
第二步:
写一个 SQL 查询来获取 Monthnames 并左连接你的相关 table(销售和费用 table 对于这个问题)如下。
SELECT MONTH(c.month) as mOrder,MONTHNAME(c.month) AS mName, (CASE WHEN SUM(e.price) IS NULL THEN 0 ELSE SUM(e.price) END), (CASE WHEN SUM(s.price) IS NULL THEN 0 ELSE SUM(s.price) END) FROM calender c LEFT JOIN sales On MONTH(c.month) = MONTH(s.created_at) LEFT JOIN expenses e On e.sale_id=s.id
第三步:
将您的 SQL 查询一一转换为 DQL。按照以下指南安装 DoctrineExtensions 以使用 MONTH()、YEAR()、DAY()、MONTHNAME() 等函数:
How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2?
并在 join 语句后添加附加条件而不是 'where' 子句。 对我来说,功能如下。
public function getMonthlyReportRowsbyCompany($company, $year, $state)
{
$searchYear = New \DateTimeImmutable("$year-01T-01T00:00:00");
$fields = array('MONTH(c.month) as mOrder,MONTHNAME(c.month) AS mName', '(CASE WHEN SUM(e.price) IS NULL THEN 0 ELSE SUM(e.price) END)' ,'(CASE WHEN SUM(s.price) IS NULL THEN 0 ELSE SUM(s.price) END)');
if($state == "0")
$sales_states= ['1', '2', '3'];
else
$sales_states[] = $siparis_durumu;
if($company== "0")
$company= true;
$query = $this->createQueryBuilder('c')
->set('lc_time_names', 'tr_TR')
->select($fields)
->leftJoin('App:Sales', 's', 'WITH', 'MONTH(c.month) = MONTH(s.created_at) AND s.Company= :company AND s.state IN (:state) AND YEAR(s.created_at) = YEAR(:searchYear)')
->setParameter('company', $company)
->setParameter('state', $sales_states)
->setParameter(':searchYear', $searchYear)
->leftJoin('App:Expenses', 'e', 'WITH', 'e.sales=s.id')
->orderBy('mOrder', 'ASC')
->groupBy('c.month');
return $query
->getQuery()
->getResult();
}
唯一的问题是我找不到使“set('lc_time_names', 'tr_TR')”语句起作用的方法。除此之外,它完美无缺。